From 16d8f4c7747789a1adfc331aaaf9f2c8d2690911 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Jun 2018 15:25:41 +0800 Subject: [PATCH 001/565] cleanup --- examples/simpledb.lua | 1 + lualib-src/lua-skynet.c | 16 +- lualib/skynet.lua | 330 +++++++++++++++++++++------------------- 3 files changed, 184 insertions(+), 163 deletions(-) diff --git a/examples/simpledb.lua b/examples/simpledb.lua index af6bac5e8..b53184718 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -33,5 +33,6 @@ skynet.start(function() error(string.format("Unknown command %s", tostring(cmd))) end end) +-- skynet.traceproto("lua", false) -- true off tracelog skynet.register "SIMPLEDB" end) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 17aa4be5e..5192fc3fa 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -417,19 +417,25 @@ struct source_info { /* string tag string userstring - thread co (default nil) - integer level + thread co (default nil/current L) + integer level (default nil) */ static int ltrace(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * tag = luaL_checkstring(L, 1); const char * user = luaL_checkstring(L, 2); - if (lua_isthread(L, 3)) { - lua_State * co = lua_tothread (L, 3); + if (!lua_isnoneornil(L, 3)) { + lua_State * co = L; + int level; + if (lua_isthread(L, 3)) { + co = lua_tothread (L, 3); + level = luaL_optinteger(L, 4, 1); + } else { + level = luaL_optinteger(L, 3, 1); + } struct source_info si[MAX_LEVEL]; lua_Debug d; - int level = luaL_checkinteger(L, 4); int index = 0; do { if (!lua_getstack(co, level, &d)) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5b1114461..64d16a3ea 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -10,8 +10,15 @@ local table = table local profile = require "skynet.profile" -local coroutine_resume = profile.resume +local cresume = profile.resume +local running_thread = nil + +local function coroutine_resume(co, ...) + running_thread = co + return cresume(co, ...) +end local coroutine_yield = profile.yield +local coroutine_create = coroutine.create local proto = {} local skynet = { @@ -97,6 +104,18 @@ local function _error_dispatch(error_session, error_source) 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 + -- coroutine reuse local coroutine_pool = setmetatable({}, { __mode = "kv" }) @@ -104,7 +123,7 @@ local coroutine_pool = setmetatable({}, { __mode = "kv" }) local function co_create(f) local co = table.remove(coroutine_pool) if co == nil then - co = coroutine.create(function(...) + co = coroutine_create(function(...) f(...) while true do local session = session_coroutine_id[co] @@ -117,7 +136,19 @@ local function co_create(f) end f = nil coroutine_pool[#coroutine_pool+1] = co - f = coroutine_yield "EXIT" + -- coroutine exit + local tag = session_coroutine_tracetag[co] + if tag then + c.trace(tag, "end") + session_coroutine_tracetag[co] = nil + end + local address = session_coroutine_address[co] + if address then + release_watching(address) + session_coroutine_id[co] = nil + end + + f = coroutine_yield "SUSPEND" f(coroutine_yield()) end end) @@ -141,18 +172,6 @@ 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, param2) if not result then @@ -171,141 +190,21 @@ function suspend(co, result, command, param, param2) end error(debug.traceback(co,tostring(command))) end - if command == "CALLTRACE" then - local tag = session_coroutine_tracetag[co] - if tag then - c.trace(tag, "call", co, 2) - c.send(param, skynet.PTYPE_TRACE, 0, tag) - end - return suspend(co, coroutine_resume(co)) - elseif command == "CALL" then - session_id_coroutine[param] = co - elseif command == "SLEEP" then - local tag = session_coroutine_tracetag[co] - if tag then c.trace(tag, "sleep", co, 2) end - session_id_coroutine[param] = co - if sleep_session[param2] then - error(debug.traceback(co, "token duplicative")) - end - sleep_session[param2] = param - elseif command == "RETURN" then - local tag = session_coroutine_tracetag[co] - if tag then c.trace(tag, "response") end - local co_session = session_coroutine_id[co] - session_coroutine_id[co] = nil - if co_session == 0 then - if param2 ~= nil then - c.trash(param, param2) - end - return suspend(co, coroutine_resume(co, false)) -- send don't need ret - end - local co_address = session_coroutine_address[co] - if param == nil or not co_session then - error(debug.traceback(co)) - end - local ret - if not dead_service[co_address] then - ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, param2) ~= 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 param2 ~= nil then - c.trash(param, param2) - ret = false - end - return suspend(co, coroutine_resume(co, ret)) - elseif command == "RESPONSE" then - local co_session = session_coroutine_id[co] - if not co_session then - error(debug.traceback(co)) - end - session_coroutine_id[co] = nil - local co_address = session_coroutine_address[co] - local f = param - local function response(ok, ...) - if ok == "TEST" then - if dead_service[co_address] then - release_watching(co_address) - unresponse[response] = nil - 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 - -- 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 - -- 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 - else - ret = false - end - release_watching(co_address) - unresponse[response] = nil - f = nil - return ret - end - watching_service[co_address] = watching_service[co_address] + 1 - unresponse[response] = true - return suspend(co, coroutine_resume(co, response)) - elseif command == "EXIT" then - -- coroutine exit - local tag = session_coroutine_tracetag[co] - if tag then c.trace(tag, "end") end - local address = session_coroutine_address[co] - if address then - release_watching(address) - session_coroutine_id[co] = nil - session_coroutine_address[co] = nil - session_coroutine_tracetag[co] = nil - end + if command == "SUSPEND" then + dispatch_wakeup() + dispatch_error_queue() 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 == "IGNORERET" then - -- We use session for other uses - session_coroutine_id[co] = nil - return suspend(co, coroutine_resume(co)) - elseif command == "TRACE" then - if param then - session_coroutine_tracetag[co] = param - if param2 then - c.trace(param, "trace " .. param2) - else - c.trace(param, "trace") - end - else - param = session_coroutine_tracetag[co] - end - return suspend(co, coroutine_resume(co, param)) elseif command == nil then -- debug trace return else error("Unknown command : " .. command .. "\n" .. debug.traceback(co)) end - dispatch_wakeup() - dispatch_error_queue() end function skynet.timeout(ti, func) @@ -316,11 +215,20 @@ function skynet.timeout(ti, func) session_id_coroutine[session] = co end +local function suspend_sleep(session, token) + local tag = session_coroutine_tracetag[running_thread] + if tag then c.trace(tag, "sleep", 2) end + session_id_coroutine[session] = running_thread + assert(sleep_session[token] == nil, "token duplicative") + sleep_session[token] = session + + return coroutine_yield "SUSPEND" +end + function skynet.sleep(ti, token) local session = c.intcommand("TIMEOUT",ti) assert(session) - token = token or coroutine.running() - local succ, ret = coroutine_yield("SLEEP", session, token) + local succ, ret = suspend_sleep(session, token or coroutine.running()) sleep_session[token] = nil if succ then return @@ -339,7 +247,7 @@ end function skynet.wait(token) local session = c.genid() token = token or coroutine.running() - local ret, msg = coroutine_yield("SLEEP", session, token) + local ret, msg = suspend_sleep(session, token or coroutine.running()) sleep_session[token] = nil session_id_coroutine[session] = nil end @@ -358,11 +266,18 @@ skynet.hpc = c.hpc -- high performance counter local traceid = 0 function skynet.trace(info) traceid = traceid + 1 - coroutine_yield("TRACE", string.format(":%08x-%d",skynet.self(), traceid), info) + + local tag = string.format(":%08x-%d",skynet.self(), traceid) + session_coroutine_tracetag[running_thread] = tag + if info then + c.trace(tag, "trace " .. info) + else + c.trace(tag, "trace") + end end function skynet.tracetag() - return coroutine_yield "TRACE" + return session_coroutine_tracetag[running_thread] end local starttime @@ -437,7 +352,8 @@ skynet.trash = assert(c.trash) local function yield_call(service, session) watching_session[session] = service - local succ, msg, sz = coroutine_yield("CALL", session) + session_id_coroutine[session] = running_thread + local succ, msg, sz = coroutine_yield "SUSPEND" watching_session[session] = nil if not succ then error "call failed" @@ -446,7 +362,12 @@ local function yield_call(service, session) end function skynet.call(addr, typename, ...) - coroutine_yield("CALLTRACE", addr) + local tag = session_coroutine_tracetag[running_thread] + if tag then + c.trace(tag, "call", 2) + c.send(addr, skynet.PTYPE_TRACE, 0, tag) + end + local p = proto[typename] local session = c.send(addr, p.id , nil , p.pack(...)) if session == nil then @@ -456,7 +377,11 @@ function skynet.call(addr, typename, ...) end function skynet.rawcall(addr, typename, msg, sz) - coroutine_yield("CALLTRACE", addr) + local tag = session_coroutine_tracetag[running_thread] + if tag then + c.trace(tag, "call", 2) + c.send(addr, skynet.PTYPE_TRACE, 0, tag) + end local p = proto[typename] local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address") return yield_call(addr, session) @@ -474,16 +399,88 @@ end function skynet.ret(msg, sz) msg = msg or "" - return coroutine_yield("RETURN", msg, sz) + local tag = session_coroutine_tracetag[running_thread] + if tag then c.trace(tag, "response") end + local co_session = session_coroutine_id[running_thread] + session_coroutine_id[running_thread] = nil + if co_session == 0 then + if sz ~= nil then + c.trash(msg, sz) + end + return false -- send don't need ret + end + local co_address = session_coroutine_address[running_thread] + if not co_session then + error "No session" + end + local ret + if not dead_service[co_address] then + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, msg, sz) ~= 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 sz ~= nil then + c.trash(msg, sz) + return false + end + return ret end function skynet.ignoreret() - coroutine_yield "IGNORERET" + -- We use session for other uses + session_coroutine_id[running_thread] = nil end function skynet.response(pack) pack = pack or skynet.pack - return coroutine_yield("RESPONSE", pack) + + local co_session = assert(session_coroutine_id[running_thread], "no session") + session_coroutine_id[running_thread] = nil + local co_address = session_coroutine_address[running_thread] + local function response(ok, ...) + if ok == "TEST" then + if dead_service[co_address] then + release_watching(co_address) + unresponse[response] = nil + pack = false + return false + else + return true + end + end + if not pack then + if pack == false then + pack = nil + return false + end + error "Can't response more than once" + end + + local ret + -- 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, pack(...)) ~= 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 + else + ret = false + end + release_watching(co_address) + unresponse[response] = nil + pack = nil + return ret + end + watching_service[co_address] = watching_service[co_address] + 1 + unresponse[response] = true + + return response end function skynet.retpack(...) @@ -556,11 +553,6 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) suspend(co, coroutine_resume(co, true, msg, sz)) end else - local tag = trace_source[source] - if tag then - c.trace(tag, "request") - trace_source[source] = nil - end local p = proto[prototype] if p == nil then if prototype == skynet.PTYPE_TRACE then @@ -573,6 +565,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) end return end + local f = p.dispatch if f then local ref = watching_service[source] @@ -584,12 +577,25 @@ 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 - session_coroutine_tracetag[co] = tag + local traceflag = p.trace + local tag = trace_source[source] + if tag then + trace_source[source] = nil + if traceflag ~= false then + c.trace(tag, "request") + session_coroutine_tracetag[co] = tag + end + elseif traceflag then + skynet.trace() + end 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) + trace_source[source] = nil + if session ~= 0 then + c.send(source, skynet.PTYPE_ERROR, session, "") + else + unknown_request(session, source, msg, sz, proto[prototype].name) + end end end end @@ -650,6 +656,14 @@ end skynet.error = c.error skynet.tracelog = c.trace +-- true: force on +-- false: force off +-- nil: optional (use skynet.trace() to trace one message) +function skynet.traceproto(prototype, flag) + local p = assert(proto[prototype]) + p.trace = flag +end + ----- register protocol do local REG = skynet.register_protocol From b1ec1f4d18b2135f3f0d3b34a29f88eadf8ab928 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Jun 2018 16:10:57 +0800 Subject: [PATCH 002/565] token bugfix --- lualib/skynet.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 64d16a3ea..a9a2b5c2b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -228,7 +228,8 @@ end function skynet.sleep(ti, token) local session = c.intcommand("TIMEOUT",ti) assert(session) - local succ, ret = suspend_sleep(session, token or coroutine.running()) + token = token or coroutine.running() + local succ, ret = suspend_sleep(session, token) sleep_session[token] = nil if succ then return @@ -247,7 +248,7 @@ end function skynet.wait(token) local session = c.genid() token = token or coroutine.running() - local ret, msg = suspend_sleep(session, token or coroutine.running()) + local ret, msg = suspend_sleep(session, token) sleep_session[token] = nil session_id_coroutine[session] = nil end From 4b5fbab12941b5bc550636312e06ceecdfa78c72 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Jun 2018 17:27:21 +0800 Subject: [PATCH 003/565] recycle coroutine bugfix --- lualib/skynet.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a9a2b5c2b..5208c5f85 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -134,8 +134,6 @@ local function co_create(f) skynet.address(session_coroutine_address[co]), source.source, source.linedefined)) end - f = nil - coroutine_pool[#coroutine_pool+1] = co -- coroutine exit local tag = session_coroutine_tracetag[co] if tag then @@ -148,12 +146,19 @@ local function co_create(f) session_coroutine_id[co] = nil end + -- recycle co into pool + f = nil + coroutine_pool[#coroutine_pool+1] = co + -- recv new main function f f = coroutine_yield "SUSPEND" f(coroutine_yield()) end end) else + -- pass the main function f to coroutine, and restore running thread + local running = running_thread coroutine_resume(co, f) + running_thread = running end return co end From 3a1556e1fd7ab08adf6d90e0c26e9db67fcee2c6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 8 Jun 2018 11:24:27 +0800 Subject: [PATCH 004/565] add new debug command trace --- lualib/skynet.lua | 25 ++++++++++++++++++------- lualib/skynet/debug.lua | 10 ++++++++++ service/debug_console.lua | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5208c5f85..8b50d2097 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -136,8 +136,8 @@ local function co_create(f) end -- coroutine exit local tag = session_coroutine_tracetag[co] - if tag then - c.trace(tag, "end") + if tag ~= nil then + if tag then c.trace(tag, "end") end session_coroutine_tracetag[co] = nil end local address = session_coroutine_address[co] @@ -271,6 +271,11 @@ skynet.hpc = c.hpc -- high performance counter local traceid = 0 function skynet.trace(info) + skynet.error("TRACE", session_coroutine_tracetag[running_thread]) + if session_coroutine_tracetag[running_thread] == false then + -- force off trace log + return + end traceid = traceid + 1 local tag = string.format(":%08x-%d",skynet.self(), traceid) @@ -584,15 +589,21 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) session_coroutine_id[co] = session session_coroutine_address[co] = source local traceflag = p.trace - local tag = trace_source[source] - if tag then + if traceflag == false then + -- force off trace_source[source] = nil - if traceflag ~= false then + session_coroutine_tracetag[co] = false + else + local tag = trace_source[source] + if tag then + trace_source[source] = nil c.trace(tag, "request") session_coroutine_tracetag[co] = tag + elseif traceflag then + -- set running_thread for trace + running_thread = co + skynet.trace() end - elseif traceflag then - skynet.trace() end suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz))) else diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index f776c96e6..802548ee7 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -78,6 +78,16 @@ local function init(skynet, export) skynet.response() -- get response , but not return. raise error when exit end + function dbgcmd.TRACELOG(proto, flag) + if type(proto) ~= "string" then + flag = proto + proto = "lua" + end + skynet.error(string.format("Turn trace log %s for %s", flag, proto)) + skynet.traceproto(proto, flag) + skynet.ret() + end + return dbgcmd end -- function init_dbgcmd diff --git a/service/debug_console.lua b/service/debug_console.lua index 33cc06ee0..6ca3a6c68 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -158,6 +158,7 @@ function COMMAND.help() shrtbl = "Show shared short string table info", ping = "ping address", call = "call address ...", + trace = "trace address [proto] [on|off]", } end @@ -343,6 +344,22 @@ function COMMAND.ping(address) return tostring(ti) end +local function toboolean(x) + return x and (x == "true" or x == "on") +end + +function COMMAND.trace(address, proto, flag) + address = adjust_address(address) + if flag == nil then + if proto == "on" or proto == "off" then + proto = toboolean(proto) + end + else + flag = toboolean(flag) + end + skynet.call(address, "debug", "TRACELOG", proto, flag) +end + function COMMANDX.call(cmd) local address = adjust_address(cmd[2]) local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments") From a8e5c3138123904a2e0ee454ae0aeb4fe3cfc2d6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Jun 2018 12:10:30 +0800 Subject: [PATCH 005/565] check udp address, see #852 --- skynet-src/socket_server.c | 41 +++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 5332e5a68..5ca04f0e9 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -602,12 +602,26 @@ udp_socket_address(struct socket *s, const uint8_t udp_address[UDP_ADDRESS_SIZE] return 0; } +static void +drop_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct write_buffer *tmp) { + s->wb_size -= tmp->sz; + list->head = tmp->next; + if (list->head == NULL) + list->tail = NULL; + write_buffer_free(ss,tmp); +} + 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); + if (sasz == 0) { + fprintf(stderr, "socket-server : udp (%d) type mismatch.\n", s->id); + drop_udp(ss, s, list, tmp); + return -1; + } int err = sendto(s->fd, tmp->ptr, tmp->sz, 0, &sa.s, sasz); if (err < 0) { switch(errno) { @@ -616,16 +630,8 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, return -1; } fprintf(stderr, "socket-server : udp (%d) sendto error %s.\n",s->id, strerror(errno)); + drop_udp(ss, s, list, tmp); return -1; -/* // ignore udp sendto error - - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = NULL; - - return SOCKET_ERR; -*/ } s->wb_size -= tmp->sz; @@ -830,6 +836,12 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock } union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp_address, &sa); + if (sasz == 0) { + // udp type mismatch, just drop it. + fprintf(stderr, "socket-server: udp socket (%d) type mistach.\n", id); + so.free_func(request->buffer); + return -1; + } 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); @@ -1529,6 +1541,12 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz } else { union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, s->p.udp_address, &sa); + if (sasz == 0) { + fprintf(stderr, "socket-server : udp (%d) type mismatch.\n", id); + socket_unlock(&l); + so.free_func((void *)buffer); + return -1; + } n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); } if (n<0) { @@ -1796,6 +1814,11 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp send_object_init(ss, &so, (void *)buffer, sz); union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp_address, &sa); + if (sasz == 0) { + socket_unlock(&l); + so.free_func((void *)buffer); + return -1; + } int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); if (n >= 0) { // sendto succ From d8ab9dea1bdef5e2f7edf23fa32b0c092ceb9cb4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Jun 2018 14:24:04 +0800 Subject: [PATCH 006/565] fix #852 --- lualib/skynet/dns.lua | 22 +++++++++++++++++----- skynet-src/socket_server.c | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index 31c49eaab..96470e126 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -286,17 +286,29 @@ local function resolve(content) skynet.wakeup(resp.co) end +local function connect_server() + local fd = socket.udp(function(str, from) + resolve(str) + end) + socket.udp_connect(fd, dns_server.address, dns_server.port) + + if dns_server.fd then + socket.close(fd) + else + dns_server.fd = fd + skynet.error(string.format("Udp server open %s:%s (%d)", dns_server.address, dns_server.port, fd)) + end +end + local DNS_SERVER_RETIRE = 60 * 100 local function touch_server() dns_server.retire = skynet.now() if dns_server.fd then return end - dns_server.fd = socket.udp(function(str, from) - resolve(str) - end) - skynet.error(string.format("Udp server open %s:%s (%d)", dns_server.address, dns_server.port, dns_server.fd)) - socket.udp_connect(dns_server.fd, dns_server.address, dns_server.port) + + connect_server() + local function check_alive() if skynet.now() > dns_server.retire + DNS_SERVER_RETIRE then local fd = dns_server.fd diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 5ca04f0e9..79158c5a2 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1542,7 +1542,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, s->p.udp_address, &sa); if (sasz == 0) { - fprintf(stderr, "socket-server : udp (%d) type mismatch.\n", id); + fprintf(stderr, "socket-server : set udp (%d) address first.\n", id); socket_unlock(&l); so.free_func((void *)buffer); return -1; From 8224b5a62a3e780c641065873f50515af3fe5506 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Jun 2018 09:49:18 +0800 Subject: [PATCH 007/565] udp_connect may raise error --- lualib/skynet/dns.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index 96470e126..fb6c2f238 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -290,14 +290,14 @@ local function connect_server() local fd = socket.udp(function(str, from) resolve(str) end) - socket.udp_connect(fd, dns_server.address, dns_server.port) - - if dns_server.fd then + local ok, err = pcall(socket.udp_connect,fd, dns_server.address, dns_server.port) + if not ok then socket.close(fd) - else - dns_server.fd = fd - skynet.error(string.format("Udp server open %s:%s (%d)", dns_server.address, dns_server.port, fd)) + error(err) end + + dns_server.fd = fd + skynet.error(string.format("Udp server open %s:%s (%d)", dns_server.address, dns_server.port, fd)) end local DNS_SERVER_RETIRE = 60 * 100 From 0e412312985079ab5b1e80f301d82830391f0371 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Jul 2018 15:10:32 +0800 Subject: [PATCH 008/565] fix #857 --- lualib/skynet/datasheet/dump.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/datasheet/dump.lua b/lualib/skynet/datasheet/dump.lua index 7e586e13f..b93512f02 100644 --- a/lualib/skynet/datasheet/dump.lua +++ b/lualib/skynet/datasheet/dump.lua @@ -59,7 +59,7 @@ function ctd.dump(root) local index = dump_table(v) return '\4', string.pack("= -(0x7FFFFFFF+1) then return '\1', string.pack(" Date: Thu, 5 Jul 2018 18:35:43 +0800 Subject: [PATCH 009/565] fix kqueue eof event --- skynet-src/socket_epoll.h | 1 + skynet-src/socket_kqueue.h | 6 ++++-- skynet-src/socket_poll.h | 1 + skynet-src/socket_server.c | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index cd1f9018c..58a26afd5 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -60,6 +60,7 @@ sp_wait(int efd, struct event *e, int max) { e[i].write = (flag & EPOLLOUT) != 0; e[i].read = (flag & (EPOLLIN | EPOLLHUP)) != 0; e[i].error = (flag & EPOLLERR) != 0; + e[i].eof = false; } return n; diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index 8aa312039..fc282a9a7 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -73,9 +73,11 @@ sp_wait(int kfd, struct event *e, int max) { for (i=0;idata = (char *)err; return SOCKET_ERR; } + if(e->eof) { + force_close(ss, s, &l, result); + return SOCKET_CLOSE; + } break; } } From 07db2580c4a7dbec65ae3e9d8fd2ac6ff168dd8c Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 5 Jul 2018 18:47:25 +0800 Subject: [PATCH 010/565] fix kqueue error flag --- skynet-src/socket_kqueue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index fc282a9a7..d154bcdd7 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -76,7 +76,7 @@ sp_wait(int kfd, struct event *e, int max) { bool eof = (ev[i].flags & EV_EOF) != 0; e[i].write = (filter == EVFILT_WRITE) && (!eof); e[i].read = (filter == EVFILT_READ) && (!eof); - e[i].error = false; // kevent has not error event + e[i].error = (ev[i].flags & EV_ERROR) != 0; e[i].eof = eof; } From ea7c6402ddae8398b0ce0b65c2b911f7dda50ecb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 10 Jul 2018 22:11:56 +0800 Subject: [PATCH 011/565] update lua to 5.3.5 --- 3rd/lua/lapi.c | 2 +- 3rd/lua/lapi.h | 2 +- 3rd/lua/lauxlib.c | 2 +- 3rd/lua/lauxlib.h | 2 +- 3rd/lua/lbaselib.c | 2 +- 3rd/lua/lbitlib.c | 2 +- 3rd/lua/lcode.c | 2 +- 3rd/lua/lcode.h | 2 +- 3rd/lua/lcorolib.c | 2 +- 3rd/lua/lctype.c | 2 +- 3rd/lua/lctype.h | 2 +- 3rd/lua/ldblib.c | 2 +- 3rd/lua/ldebug.c | 2 +- 3rd/lua/ldebug.h | 2 +- 3rd/lua/ldo.c | 2 +- 3rd/lua/ldo.h | 2 +- 3rd/lua/ldump.c | 2 +- 3rd/lua/lfunc.c | 2 +- 3rd/lua/lfunc.h | 2 +- 3rd/lua/lgc.c | 2 +- 3rd/lua/lgc.h | 2 +- 3rd/lua/linit.c | 2 +- 3rd/lua/liolib.c | 13 +++++++++---- 3rd/lua/llex.c | 2 +- 3rd/lua/llex.h | 2 +- 3rd/lua/llimits.h | 2 +- 3rd/lua/lmathlib.c | 2 +- 3rd/lua/lmem.c | 2 +- 3rd/lua/lmem.h | 2 +- 3rd/lua/loadlib.c | 2 +- 3rd/lua/lobject.c | 5 +++-- 3rd/lua/lobject.h | 2 +- 3rd/lua/lopcodes.c | 2 +- 3rd/lua/lopcodes.h | 2 +- 3rd/lua/loslib.c | 8 +++++--- 3rd/lua/lparser.c | 4 ++-- 3rd/lua/lparser.h | 2 +- 3rd/lua/lprefix.h | 2 +- 3rd/lua/lstate.c | 2 +- 3rd/lua/lstate.h | 20 +++++++++++++++++++- 3rd/lua/lstring.c | 2 +- 3rd/lua/lstring.h | 2 +- 3rd/lua/lstrlib.c | 8 ++++---- 3rd/lua/ltable.c | 35 +++++++++++++++++++++++++++-------- 3rd/lua/ltable.h | 4 ++-- 3rd/lua/ltablib.c | 2 +- 3rd/lua/ltm.c | 2 +- 3rd/lua/ltm.h | 2 +- 3rd/lua/lua.c | 4 ++-- 3rd/lua/lua.h | 8 ++++---- 3rd/lua/luac.c | 5 +++-- 3rd/lua/luaconf.h | 9 ++++++++- 3rd/lua/lualib.h | 2 +- 3rd/lua/lundump.c | 2 +- 3rd/lua/lundump.h | 2 +- 3rd/lua/lutf8lib.c | 4 ++-- 3rd/lua/lvm.c | 2 +- 3rd/lua/lvm.h | 2 +- 3rd/lua/lzio.c | 2 +- 3rd/lua/lzio.h | 2 +- 60 files changed, 137 insertions(+), 84 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 1450598cc..348b149fb 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp $ +** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index 6d36dee3f..8e16ad53d 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $ +** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 5988509e1..e54d33767 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp $ +** $Id: lauxlib.c,v 1.289.1.1 2017/04/19 17:20:42 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 9a2e66aa0..9857d3a83 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp $ +** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 08523e6e7..6460e4f8d 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp $ +** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lbitlib.c b/3rd/lua/lbitlib.c index 1cb1d5b93..4786c0d48 100644 --- a/3rd/lua/lbitlib.c +++ b/3rd/lua/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $ +** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 18116445c..6392d4b9b 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp $ +** $Id: lcode.c,v 2.112.1.1 2017/04/19 17:20:42 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 7a867434e..8aa4e98c4 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $ +** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index 2303429e7..0b17af9e3 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp $ +** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ ** Coroutine Library ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lctype.c b/3rd/lua/lctype.c index ae9367e69..f8ad7a2ed 100644 --- a/3rd/lua/lctype.c +++ b/3rd/lua/lctype.c @@ -1,5 +1,5 @@ /* -** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ +** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lctype.h b/3rd/lua/lctype.h index 99c7d1223..b09b21a33 100644 --- a/3rd/lua/lctype.h +++ b/3rd/lua/lctype.h @@ -1,5 +1,5 @@ /* -** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ +** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 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 786f6cd95..9d29afb0a 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $ +** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 roberto Exp $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index ed98b27f5..aae85534e 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp $ +** $Id: ldebug.c,v 2.121.1.2 2017/07/10 17:21:50 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 0579311ba..584605333 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ +** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 955af4ef5..64795db12 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.157 2016/12/13 15:52:21 roberto Exp $ +** $Id: ldo.c,v 2.157.1.1 2017/04/19 17:20:42 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 4f5d51c3c..3b2983a38 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $ +** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index dc100ca1f..9749c9394 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp $ +** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index b0d9db4b3..1d011aa03 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ +** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 21cbe34ea..71795b41a 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ +** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 39e258606..eeb5401e8 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp $ +** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index aed3e18a5..425cd7cef 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $ +** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index afcaf98b2..480da52c7 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $ +** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 156840358..8a9e75cd0 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.151 2016/12/20 18:37:00 roberto Exp $ +** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -206,11 +206,16 @@ static int aux_close (lua_State *L) { } +static int f_close (lua_State *L) { + tofile(L); /* make sure argument is an open stream */ + return aux_close(L); +} + + static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); + return f_close(L); } @@ -712,7 +717,7 @@ static const luaL_Reg iolib[] = { ** methods for file handles */ static const luaL_Reg flib[] = { - {"close", io_close}, + {"close", f_close}, {"flush", f_flush}, {"lines", f_lines}, {"read", f_read}, diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 70328273f..66fd411ba 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.96 2016/05/02 14:02:12 roberto Exp $ +** $Id: llex.c,v 2.96.1.1 2017/04/19 17:20:42 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/llex.h b/3rd/lua/llex.h index 2363d87e4..2ed0af66a 100644 --- a/3rd/lua/llex.h +++ b/3rd/lua/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $ +** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index f21377fef..d1036f6bc 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp $ +** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 roberto Exp $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index b7f8baee0..7ef7e593f 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp $ +** $Id: lmathlib.c,v 1.119.1.1 2017/04/19 17:20:42 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index 0a0476cc7..0241cc3ba 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ +** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lmem.h b/3rd/lua/lmem.h index 30f484895..357b1e43e 100644 --- a/3rd/lua/lmem.h +++ b/3rd/lua/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ +** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 4791e748b..45f44d322 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp $ +** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 2da76899a..2218c8cdd 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp $ +** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -435,7 +435,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } 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 *)); + void *p = va_arg(argp, void *); + int l = lua_pointer2str(buff, sizeof(buff), p); pushstr(L, buff, l); break; } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index ec6a1f0cb..8318005cd 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp $ +** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lopcodes.c b/3rd/lua/lopcodes.c index a1cbef857..5ca3eb261 100644 --- a/3rd/lua/lopcodes.c +++ b/3rd/lua/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ +** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index bbc4b6196..6feaa1cd0 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp $ +** $Id: lopcodes.h,v 1.149.1.1 2017/04/19 17:20:42 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 5a94eb906..de590c6b7 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp $ +** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -293,7 +293,8 @@ static int os_date (lua_State *L) { else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ - luaL_error(L, "time result cannot be represented in this installation"); + return 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 */ setallfields(L, stm); @@ -340,7 +341,8 @@ static int os_time (lua_State *L) { 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"); + return 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 7419987f6..2a5cbf1e3 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp $ +** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1395,7 +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 semicolons */ + while (testnext(ls, ';')) {} /* skip colons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 02e9b03ae..f45b23cba 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $ +** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lprefix.h b/3rd/lua/lprefix.h index 02daa837f..9a749a3f3 100644 --- a/3rd/lua/lprefix.h +++ b/3rd/lua/lprefix.h @@ -1,5 +1,5 @@ /* -** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ +** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ ** Definitions for Lua code that must come before any other header file ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 9194ac341..c1a76643c 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp $ +** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index a469466c4..56b374100 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp $ +** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -26,6 +26,24 @@ ** 'tobefnz': all objects ready to be finalized; ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). +** +** Moreover, there is another set of lists that control gray objects. +** These lists are linked by fields 'gclist'. (All objects that +** can become gray have such a field. The field is not the same +** in all objects, but it always has this name.) Any gray object +** must belong to one of these lists, and all objects in these lists +** must be gray: +** +** 'gray': regular gray objects, still waiting to be visited. +** 'grayagain': objects that must be revisited at the atomic phase. +** That includes +** - black objects got in a write barrier; +** - all kinds of weak tables during propagation phase; +** - all threads. +** 'weak': tables with weak values to be cleared; +** 'ephemeron': ephemeron tables with white->white entries; +** 'allweak': tables with weak keys and/or weak values to be cleared. +** The last three lists are used only during the atomic phase. */ diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 71443a914..b7a47fae4 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $ +** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index ca5f0a356..e6a38b885 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $ +** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index c7aa755fa..b4bed7e93 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp $ +** $Id: lstrlib.c,v 1.254.1.1 2017/04/19 17:29:57 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -879,7 +879,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, buff[i] = toupper(uchar(buff[i])); } else if (fmt[SIZELENMOD] != 'a') - luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); + return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } @@ -1199,8 +1199,8 @@ static int getnum (const char **fmt, int df) { 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 luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); return sz; } diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index d080189f2..ea4fe7fcb 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp $ +** $Id: ltable.c,v 2.118.1.4 2018/06/08 16:22:51 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -223,7 +223,9 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { unsigned int na = 0; /* number of elements to go to array part */ 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) { + for (i = 0, twotoi = 1; + twotoi > 0 && *pna > twotoi / 2; + i++, twotoi *= 2) { if (nums[i] > 0) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ @@ -330,17 +332,34 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { } +typedef struct { + Table *t; + unsigned int nhsize; +} AuxsetnodeT; + + +static void auxsetnode (lua_State *L, void *ud) { + AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); + setnodevector(L, asn->t, asn->nhsize); +} + + void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize) { unsigned int i; int j; + AuxsetnodeT asn; unsigned int oldasize = t->sizearray; int oldhsize = allocsizenode(t); Node *nold = t->node; /* save old hash ... */ if (nasize > oldasize) /* array part must grow? */ setarrayvector(L, t, nasize); /* create new hash part with appropriate size */ - setnodevector(L, t, nhsize); + asn.t = t; asn.nhsize = nhsize; + if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ + setarrayvector(L, t, oldasize); /* array back to its original size */ + luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ + } if (nasize < oldasize) { /* array part must shrink? */ t->sizearray = nasize; /* re-insert elements from vanishing slice */ @@ -610,13 +629,13 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { } -static int unbound_search (Table *t, unsigned int j) { - unsigned int i = j; /* i is zero or a present index */ +static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { + lua_Unsigned i = j; /* i is zero or a present index */ j++; /* find 'i' and 'j' such that i is present and j is not */ while (!ttisnil(luaH_getint(t, j))) { i = j; - if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ + if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ /* table was built with bad purposes: resort to linear search */ i = 1; while (!ttisnil(luaH_getint(t, i))) i++; @@ -626,7 +645,7 @@ static int unbound_search (Table *t, unsigned int j) { } /* now do a binary search between them */ while (j - i > 1) { - unsigned int m = (i+j)/2; + lua_Unsigned m = (i+j)/2; if (ttisnil(luaH_getint(t, m))) j = m; else i = m; } @@ -638,7 +657,7 @@ static int unbound_search (Table *t, unsigned int j) { ** 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) { +lua_Unsigned luaH_getn (Table *t) { unsigned int j = t->sizearray; if (j > 0 && ttisnil(&t->array[j - 1])) { /* there is a boundary in the array part: (binary) search for it */ diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 6da9024fe..92db0ac7b 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $ +** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -54,7 +54,7 @@ LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 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); +LUAI_FUNC lua_Unsigned luaH_getn (Table *t); #if defined(LUA_DEBUG) diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index 98b2f8713..c5349578e 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp $ +** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 14e525788..0e7c71321 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $ +** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 63db7269b..8170688da 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $ +** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index febd98763..232179674 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.230 2017/01/12 17:14:26 roberto Exp $ +** $Id: lua.c,v 1.230.1.1 2017/04/19 17:29:57 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -139,7 +139,7 @@ static void print_usage (const char *badoption) { "Available options are:\n" " -e stat execute string 'stat'\n" " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name'\n" + " -l name require library 'name' into global 'name'\n" " -v show version information\n" " -E ignore environment variables\n" " -- stop handling options\n" diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 24a8886d7..7bf639f1f 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp $ +** $Id: lua.h,v 1.332.1.2 2018/06/13 16:58:17 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 "4" +#define LUA_VERSION_RELEASE "5" #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-2017 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 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-2017 Lua.org, PUC-Rio. +* Copyright (C) 1994-2018 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 86d475182..46dda035c 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,5 +1,5 @@ /* -** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ +** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ ** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ @@ -208,7 +208,7 @@ int main(int argc, char* argv[]) } /* -** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ +** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ @@ -350,6 +350,7 @@ static void PrintCode(const Proto* f) case OP_ADD: case OP_SUB: case OP_MUL: + case OP_MOD: case OP_POW: case OP_DIV: case OP_IDIV: diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index f37bea096..9eeeea69e 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp $ +** $Id: luaconf.h,v 1.259.1.1 2017/04/19 17:29:57 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -620,6 +620,13 @@ #endif +/* +@@ lua_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + /* @@ lua_number2strx converts a float to an hexadecimal numeric string. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 54ae8df7c..422b9ea55 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $ +** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ ** Lua standard libraries ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 39674cd96..bd7dd32db 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $ +** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index aa5cc82f1..ce492d689 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $ +** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index de9e3dcdd..10bd238a7 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp $ +** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -171,7 +171,7 @@ static int byteoffset (lua_State *L) { } else { if (iscont(s + posi)) - luaL_error(L, "initial position is a continuation byte"); + return 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 */ diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index eeaa10089..f968ba019 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.268 2016/02/05 19:59:14 roberto Exp $ +** $Id: lvm.c,v 2.268.1.1 2017/04/19 17:39:34 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 422f87194..a8f954f04 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $ +** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lzio.c b/3rd/lua/lzio.c index c9e1f491f..6f7909441 100644 --- a/3rd/lua/lzio.c +++ b/3rd/lua/lzio.c @@ -1,5 +1,5 @@ /* -** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $ +** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lzio.h b/3rd/lua/lzio.h index e7b6f34b1..d89787081 100644 --- a/3rd/lua/lzio.h +++ b/3rd/lua/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $ +** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ From 16f060f86465bc0c723fffdcee06d87d5ed38e70 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 20 Jul 2018 14:46:53 +0800 Subject: [PATCH 012/565] hook aligned_alloc --- skynet-src/malloc_hook.c | 7 +++++++ skynet-src/skynet_malloc.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 7174f0a91..35d2260e3 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -202,6 +202,13 @@ skynet_memalign(size_t alignment, size_t size) { return fill_prefix(ptr); } +void * +skynet_aligned_alloc(size_t alignment, size_t size) { + void* ptr = je_aligned_alloc(alignment, size + (PREFIX_SIZE + alignment -1) & ~(alignment-1)); + if(!ptr) malloc_oom(size); + return fill_prefix(ptr); +} + #else // for skynet_lalloc use diff --git a/skynet-src/skynet_malloc.h b/skynet-src/skynet_malloc.h index e2f0aa8d9..81c1c603b 100644 --- a/skynet-src/skynet_malloc.h +++ b/skynet-src/skynet_malloc.h @@ -8,6 +8,7 @@ #define skynet_realloc realloc #define skynet_free free #define skynet_memalign memalign +#define skynet_aligned_alloc aligned_alloc void * skynet_malloc(size_t sz); void * skynet_calloc(size_t nmemb,size_t size); @@ -16,5 +17,6 @@ void skynet_free(void *ptr); char * skynet_strdup(const char *str); void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua void * skynet_memalign(size_t alignment, size_t size); +void * skynet_aligned_alloc(size_t alignment, size_t size); #endif From 1a0c9997ec279b3ee0560a98e3c38586d4db942f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Jul 2018 14:53:01 +0800 Subject: [PATCH 013/565] fix #863 --- skynet-src/malloc_hook.c | 2 +- skynet-src/socket_server.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 35d2260e3..00e635c00 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -204,7 +204,7 @@ skynet_memalign(size_t alignment, size_t size) { void * skynet_aligned_alloc(size_t alignment, size_t size) { - void* ptr = je_aligned_alloc(alignment, size + (PREFIX_SIZE + alignment -1) & ~(alignment-1)); + void* ptr = je_aligned_alloc(alignment, size + (size_t)((PREFIX_SIZE + alignment -1) & ~(alignment-1))); if(!ptr) malloc_oom(size); return fill_prefix(ptr); } diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 0ab233ba2..b74dc1650 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -359,6 +359,7 @@ socket_server_create() { s->type = SOCKET_TYPE_INVALID; clear_wb_list(&s->high); clear_wb_list(&s->low); + spinlock_init(&s->dw_lock); } ss->alloc_id = 0; ss->event_n = 0; @@ -429,6 +430,7 @@ socket_server_release(struct socket_server *ss) { if (s->type != SOCKET_TYPE_RESERVE) { force_close(ss, s, &l, &dummy); } + spinlock_destroy(&s->dw_lock); } close(ss->sendctrl_fd); close(ss->recvctrl_fd); @@ -464,7 +466,6 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->warn_size = 0; check_wb_list(&s->high); check_wb_list(&s->low); - spinlock_init(&s->dw_lock); s->dw_buffer = NULL; s->dw_size = 0; return s; From 9421815327bd9ad693cbb84cf06cfc9089278a84 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Jul 2018 15:32:35 +0800 Subject: [PATCH 014/565] add posix_memalign hook --- skynet-src/malloc_hook.c | 8 ++++++++ skynet-src/skynet_malloc.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 00e635c00..02b6c0912 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -209,6 +209,14 @@ skynet_aligned_alloc(size_t alignment, size_t size) { return fill_prefix(ptr); } +int +skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { + int err = je_posix_memalign(memptr, alignment, size + PREFIX_SIZE); + if (err) malloc_oom(size); + fill_prefix(*memptr); + return err; +} + #else // for skynet_lalloc use diff --git a/skynet-src/skynet_malloc.h b/skynet-src/skynet_malloc.h index 81c1c603b..57f2db139 100644 --- a/skynet-src/skynet_malloc.h +++ b/skynet-src/skynet_malloc.h @@ -9,6 +9,7 @@ #define skynet_free free #define skynet_memalign memalign #define skynet_aligned_alloc aligned_alloc +#define skynet_posix_memalign posix_memalign void * skynet_malloc(size_t sz); void * skynet_calloc(size_t nmemb,size_t size); @@ -18,5 +19,6 @@ char * skynet_strdup(const char *str); void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua void * skynet_memalign(size_t alignment, size_t size); void * skynet_aligned_alloc(size_t alignment, size_t size); +int skynet_posix_memalign(void **memptr, size_t alignment, size_t size); #endif From 2df1446c37ba2035fadb78c7ffdeb2c1c29901f9 Mon Sep 17 00:00:00 2001 From: yenshan98 Date: Mon, 30 Jul 2018 14:31:37 +0800 Subject: [PATCH 015/565] Rearranged sentence to be reader friendly. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7040656f7..2028e3ab8 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Skynet now uses a modified version of lua 5.3.4 ( https://github.com/ejoy/lua/tr You can also use official Lua versions, just edit the Makefile by yourself. -## How To Use (Sorry, Only in Chinese now) +## How To Use (Sorry, currently only available in Chinese) * Read Wiki for documents https://github.com/cloudwu/skynet/wiki * The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ From c9942deaa9b8c1e9bafd346f760be1fecd58a4c3 Mon Sep 17 00:00:00 2001 From: hong Date: Thu, 2 Aug 2018 11:37:55 +0800 Subject: [PATCH 016/565] =?UTF-8?q?queryname=E5=B9=B6=E5=8F=91=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=B6=88=E6=81=AF=E4=B9=B1=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当有1个以上的co进入queryname,后面的协程有在第1个以后某个的co先执行(第一个已经返回),导致cluster消息乱序 --- service/clusteragent.lua | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 04b14eab7..b4af47e42 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -11,14 +11,29 @@ fd = tonumber(fd) local large_request = {} local register_name = {} +local inquery_name = {} setmetatable(register_name, { __index = function(self, name) - local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' - if addr then - self[name] = addr + local waitco = inquery_name[name] + if waitco then + local co=coroutine.runnging() + table.insert(waitco, co) + skynet.wait(co) + return rawget(self, name) + else + waitco = {} + inquery_name[name] = waitco + local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' + if addr then + self[name] = addr + end + inquery_name[name] = nil + for _, co in ipairs(waitco) do + skynet.wakeup(co) + end + return addr end - return addr end }) From b5653a68eae58337c209fe36e34188745b910cf5 Mon Sep 17 00:00:00 2001 From: hong Date: Thu, 2 Aug 2018 19:19:44 +0800 Subject: [PATCH 017/565] =?UTF-8?q?runnging=E6=8B=BC=E5=86=99=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/clusteragent.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index b4af47e42..2cd5bc704 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -17,7 +17,7 @@ setmetatable(register_name, { __index = function(self, name) local waitco = inquery_name[name] if waitco then - local co=coroutine.runnging() + local co=coroutine.running() table.insert(waitco, co) skynet.wait(co) return rawget(self, name) From c101d9df176c54d9e6c5a11887b52649d4ba51af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Aug 2018 11:24:14 +0800 Subject: [PATCH 018/565] see #870 --- service/clusteragent.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 2cd5bc704..c6b61ccf3 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -10,10 +10,9 @@ gate = tonumber(gate) fd = tonumber(fd) local large_request = {} -local register_name = {} local inquery_name = {} -setmetatable(register_name, { __index = +local register_name_mt = { __index = function(self, name) local waitco = inquery_name[name] if waitco then @@ -37,6 +36,12 @@ setmetatable(register_name, { __index = end }) +local function new_register_name() + return setmetatable({}, register_name_mt) +end + +local register_name = new_register_name() + local tracetag local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push) @@ -132,7 +137,7 @@ skynet.start(function() socket.close(fd) skynet.exit() elseif cmd == "namechange" then - register_name = {} + register_name = new_register_name() else skynet.error(string.format("Invalid command %s from %s", cmd, skynet.address(source))) end From d402619347615d8ebb5f25d43cc83e8d59dfba51 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Aug 2018 11:24:14 +0800 Subject: [PATCH 019/565] see #870 --- service/clusteragent.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 2cd5bc704..0f0d3ec43 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -10,10 +10,9 @@ gate = tonumber(gate) fd = tonumber(fd) local large_request = {} -local register_name = {} local inquery_name = {} -setmetatable(register_name, { __index = +local register_name_mt = { __index = function(self, name) local waitco = inquery_name[name] if waitco then @@ -35,7 +34,13 @@ setmetatable(register_name, { __index = return addr end end -}) +} + +local function new_register_name() + return setmetatable({}, register_name_mt) +end + +local register_name = new_register_name() local tracetag @@ -132,7 +137,7 @@ skynet.start(function() socket.close(fd) skynet.exit() elseif cmd == "namechange" then - register_name = {} + register_name = new_register_name() else skynet.error(string.format("Invalid command %s from %s", cmd, skynet.address(source))) end From e55ad63d3c744b13499590689924ac785ad97553 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 7 Aug 2018 09:43:50 +0800 Subject: [PATCH 020/565] fix #873 --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 8b50d2097..15d08f6da 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -193,6 +193,7 @@ function suspend(co, result, command, param, param2) session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil end + skynet.timeout(0,function() end) -- trigger command "SUSPEND" error(debug.traceback(co,tostring(command))) end if command == "SUSPEND" then From d2ab6863e3a23920d1926064d05c16d0a747de9b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 7 Aug 2018 10:49:21 +0800 Subject: [PATCH 021/565] fix #872 --- lualib/skynet/multicast.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/multicast.lua b/lualib/skynet/multicast.lua index 21ebf9748..1c76b8770 100644 --- a/lualib/skynet/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -3,7 +3,7 @@ local mc = require "skynet.multicast.core" local multicastd local multicast = {} -local dispatch = setmetatable({} , {__mode = "kv" }) +local dispatch = {} local chan = {} local chan_meta = { @@ -70,6 +70,7 @@ function chan:unsubscribe() local c = assert(self.channel) skynet.send(multicastd, "lua", "USUB", c) self.__subscribe = nil + dispatch[c] = nil end local function dispatch_subscribe(channel, source, pack, msg, sz) From 281acf4c71204134d14e7678dc66ddff79c9877d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 7 Aug 2018 14:55:26 +0800 Subject: [PATCH 022/565] Use fork instead of timeout 0, see #873 --- lualib/skynet.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 15d08f6da..a9fc92af1 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -193,7 +193,7 @@ function suspend(co, result, command, param, param2) session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil end - skynet.timeout(0,function() end) -- trigger command "SUSPEND" + skynet.fork(function() end) -- trigger command "SUSPEND" error(debug.traceback(co,tostring(command))) end if command == "SUSPEND" then @@ -540,10 +540,14 @@ function skynet.dispatch_unknown_response(unknown) end function skynet.fork(func,...) - local args = table.pack(...) - local co = co_create(function() - func(table.unpack(args,1,args.n)) - end) + local n = select("#", ...) + local co + if n == 0 then + co = co_create(func) + else + local args = { ... } + co = co_create(function() func(table.unpack(args,1,n)) end) + end table.insert(fork_queue, co) return co end From 168a07d65885459cc59a0ad13bf5a1fba6b4b559 Mon Sep 17 00:00:00 2001 From: fanyh Date: Thu, 9 Aug 2018 18:42:06 +0800 Subject: [PATCH 023/565] sleep time 1s-> 1m, sleep 1s prodcut more message --- service/sharedatad.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 3877ec398..d74394937 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -8,7 +8,7 @@ local NORET = {} local pool = {} local pool_count = {} local objmap = {} -local collect_tick = 600 +local collect_tick = 10 local function newobj(name, tbl) assert(pool[name] == nil) @@ -20,17 +20,17 @@ local function newobj(name, tbl) pool_count[name] = { n = 0, threshold = 16 } end -local function collect10sec() - if collect_tick > 10 then - collect_tick = 10 +local function collect1min() + if collect_tick > 1 then + collect_tick = 1 end end local function collectobj() while true do - skynet.sleep(100) -- sleep 1s + skynet.sleep(60*100) -- sleep 1min if collect_tick <= 0 then - collect_tick = 600 -- reset tick count to 600 sec + collect_tick = 10 -- reset tick count to 10 min collectgarbage() for obj, v in pairs(objmap) do if v == true then @@ -121,7 +121,7 @@ function CMD.update(name, t, ...) response(true, newobj) end end - collect10sec() -- collect in 10 sec + collect1min() -- collect in 1 min end local function check_watch(queue) From 143baec319a7ae25e726231ea27378797000647b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Aug 2018 14:12:15 +0800 Subject: [PATCH 024/565] remove duplicate code --- service/clusteragent.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index c59d5aa5a..0f0d3ec43 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -42,12 +42,6 @@ end local register_name = new_register_name() -local function new_register_name() - return setmetatable({}, register_name_mt) -end - -local register_name = new_register_name() - local tracetag local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push) From 4613fde654b6dfb76d999a3de768c612770ff2af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 17 Aug 2018 21:16:35 +0800 Subject: [PATCH 025/565] fix #877 --- lualib/skynet/socketchannel.lua | 49 +++++++++++++-------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 79935e363..c11626b9c 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -77,17 +77,6 @@ local function wakeup_all(self, errmsg) end end -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 - local function dispatch_by_session(self) local response = self.__response -- response() return session @@ -124,7 +113,7 @@ local function dispatch_by_session(self) wakeup_all(self, errormsg) end end - exit_thread(self) + self.__dispatch_thread = nil end local function pop_response(self) @@ -201,7 +190,7 @@ local function dispatch_by_order(self) wakeup_all(self, errmsg) end end - exit_thread(self) + self.__dispatch_thread = nil end local function dispatch_function(self) @@ -233,11 +222,21 @@ local function connect_backup(self) end end +local function term_dispatch_thread(self) + if not self.__response and self.__dispatch_thread then + -- dispatch by order, send close signal to dispatch thread + push_response(self, true, false) -- (true, false) is close signal + end +end + local function connect_once(self) if self.__closed then return false end assert(not self.__sock and not self.__authcoroutine) + -- term current dispatch thread (send a sigal) + term_dispatch_thread(self, self.__dispatch_thread) + local fd,err = socket.open(self.__host, self.__port) if not fd then fd = connect_backup(self) @@ -249,6 +248,11 @@ local function connect_once(self) socketdriver.nodelay(fd) end + while self.__dispatch_thread do + -- wait for dispatch thread exit + skynet.yield() + end + self.__sock = setmetatable( {fd} , channel_socket_meta ) self.__dispatch_thread = skynet.fork(dispatch_function(self), self) @@ -353,18 +357,7 @@ local function block_connect(self, once) 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 - + self.__closed = false return block_connect(self, once) end @@ -437,13 +430,9 @@ end function channel:close() if not self.__closed then - local thread = self.__dispatch_thread + term_dispatch_thread(self) self.__closed = true close_channel_socket(self) - 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 end end From 7b3c1631d1beb784b0c4294f315499a17d96e1c7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 17 Aug 2018 21:27:14 +0800 Subject: [PATCH 026/565] more stable --- lualib/skynet/socketchannel.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index c11626b9c..1ebfc0475 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -113,7 +113,6 @@ local function dispatch_by_session(self) wakeup_all(self, errormsg) end end - self.__dispatch_thread = nil end local function pop_response(self) @@ -190,7 +189,6 @@ local function dispatch_by_order(self) wakeup_all(self, errmsg) end end - self.__dispatch_thread = nil end local function dispatch_function(self) @@ -254,7 +252,11 @@ local function connect_once(self) end self.__sock = setmetatable( {fd} , channel_socket_meta ) - self.__dispatch_thread = skynet.fork(dispatch_function(self), self) + self.__dispatch_thread = skynet.fork(function() + pcall(dispatch_function(self), self) + -- clear dispatch_thread + self.__dispatch_thread = nil + end) if self.__auth then self.__authcoroutine = coroutine.running() From 18a36c171fd82ca8aa7512ed43d20d9a985025e3 Mon Sep 17 00:00:00 2001 From: btt Date: Thu, 23 Aug 2018 16:54:06 +0800 Subject: [PATCH 027/565] debug console inject cmd support args --- lualib/skynet/debug.lua | 5 +++-- lualib/skynet/inject.lua | 4 ++-- service/debug_console.lua | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 802548ee7..aeec0e3eb 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -50,9 +50,10 @@ local function init(skynet, export) skynet.exit() end - function dbgcmd.RUN(source, filename) + function dbgcmd.RUN(source, filename, ...) local inject = require "skynet.inject" - local ok, output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) + local args = table.pack(...) + local ok, output = inject(skynet, source, filename, args, export.dispatch, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(ok, table.concat(output, "\n"))) end diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua index 46c1c8ae1..e95775979 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(skynet, source, filename , ...) +return function(skynet, source, filename, args, ...) if filename then filename = "@" .. filename else @@ -56,7 +56,7 @@ return function(skynet, source, filename , ...) if not func then return false, { err } end - local ok, err = skynet.pcall(func) + local ok, err = skynet.pcall(func, table.unpack(args, 1, args.n)) if not ok then table.insert(output, err) return false, output diff --git a/service/debug_console.lua b/service/debug_console.lua index 6ca3a6c68..ab1af39f5 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -237,7 +237,7 @@ function COMMAND.exit(address) skynet.send(adjust_address(address), "debug", "EXIT") end -function COMMAND.inject(address, filename) +function COMMAND.inject(address, filename, ...) address = adjust_address(address) local f = io.open(filename, "rb") if not f then @@ -245,7 +245,7 @@ function COMMAND.inject(address, filename) end local source = f:read "*a" f:close() - local ok, output = skynet.call(address, "debug", "RUN", source, filename) + local ok, output = skynet.call(address, "debug", "RUN", source, filename, ...) if ok == false then error(output) end From c72d4d2134b73d8b444b89c646457461af1fc94f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Aug 2018 15:15:38 +0800 Subject: [PATCH 028/565] support string address name, see #883 --- service/debug_console.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index ab1af39f5..7ec47453d 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -207,7 +207,10 @@ function COMMAND.service() end local function adjust_address(address) - if address:sub(1,1) ~= ":" then + local prefix = address:sub(1,1) + if prefix == '.' then + return assert(skynet.localname(address), "Not a valid name") + elseif prefix ~= ':' then address = assert(tonumber("0x" .. address), "Need an address") | (skynet.harbor(skynet.self()) << 24) end return address From 2fb52f6fce5f871ee39f2683c51188190dc65c32 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Aug 2018 15:29:26 +0800 Subject: [PATCH 029/565] cleanup --- lualib/skynet/socketchannel.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 1ebfc0475..737f0926a 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -232,8 +232,8 @@ local function connect_once(self) return false end assert(not self.__sock and not self.__authcoroutine) - -- term current dispatch thread (send a sigal) - term_dispatch_thread(self, self.__dispatch_thread) + -- term current dispatch thread (send a signal) + term_dispatch_thread(self) local fd,err = socket.open(self.__host, self.__port) if not fd then From 8ce816de7f316c443186e6b72204a343f2938744 Mon Sep 17 00:00:00 2001 From: LiuYang Date: Wed, 29 Aug 2018 21:39:07 +0800 Subject: [PATCH 030/565] fix mongodb testcase --- test/testmongodb.lua | 63 +++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 138a970b1..11d819be5 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -2,55 +2,64 @@ local skynet = require "skynet" local mongo = require "skynet.db.mongo" local bson = require "bson" -local host, db_name = ... +local host, port, db_name, username, password = ... + +local function _create_client() + return mongo.client( + { + host = host, port = port, + username = username, password = password, + authdb = db_name, + } + ) +end function test_insert_without_index() - local db = mongo.client({host = host}) - + local db = _create_client() 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 ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) - local ret = db[db_name].testdb:safe_insert({test_key = 1}); - assert(ret and ret.n == 1) + local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) end function test_insert_with_index() - local db = mongo.client({host = host}) + local db = _create_client() 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 ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ok and ret and ret.n == 1) - local ret = db[db_name].testdb:safe_insert({test_key = 1}) - assert(ret and ret.n == 0) + local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ok == false and string.find(err, "duplicate key error")) end function test_find_and_remove() - local db = mongo.client({host = host}) + local db = _create_client() db[db_name].testdb:dropIndex("*") db[db_name].testdb:drop() 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, test_key2 = 1}) - assert(ret and ret.n == 1) + local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1}) + assert(ok and ret and ret.n == 1, err) - local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2}) - assert(ret and ret.n == 1) + local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2}) + assert(ok and ret and ret.n == 1, err) - local ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3}) - assert(ret and ret.n == 1) + local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3}) + assert(ok and ret and ret.n == 1, err) local ret = db[db_name].testdb:findOne({test_key2 = 1}) - assert(ret and ret.test_key2 == 1) + assert(ret and ret.test_key2 == 1, err) 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) @@ -69,7 +78,7 @@ end function test_expire_index() - local db = mongo.client({host = host}) + local db = _create_client() db[db_name].testdb:dropIndex("*") db[db_name].testdb:drop() @@ -77,21 +86,21 @@ function test_expire_index() 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 ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + assert(ok and ret and ret.n == 1, err) local ret = db[db_name].testdb:findOne({test_key = 1}) assert(ret and ret.test_key == 1) - for i = 1, 1000 do - skynet.sleep(1); - + for i = 1, 60 do + skynet.sleep(100); + print("check expire", i) local ret = db[db_name].testdb:findOne({test_key = 1}) if ret == nil then return end end - + print("test expire index failed") assert(false, "test expire index failed"); end From e6e69ae8058eb01426493d0014e9811a65ce384b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Aug 2018 19:52:41 +0800 Subject: [PATCH 031/565] add mongo config option: authdb, see issue #884 --- lualib/skynet/db/mongo.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 7522da20d..04b278058 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -87,13 +87,17 @@ local function mongo_auth(mongoc) local pass = rawget(mongoc, "password") local authmod = rawget(mongoc, "authmod") or "scram_sha1" authmod = "auth_" .. authmod + local authdb = rawget(mongoc, "authdb") + if authdb then + authdb = mongo_client.getDB(mongoc, authdb) -- mongoc has not set metatable yet + end return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" local auth_func = mongoc[authmod] assert(auth_func , "Invalid authmod") - assert(auth_func(mongoc,user, pass)) + assert(auth_func(authdb or mongoc, user, pass)) end local rs_data = mongoc:runCommand("ismaster") if rs_data.ok == 1 then @@ -156,6 +160,7 @@ function mongo.client( conf ) username = first.username, password = first.password, authmod = first.authmod, + authdb = first.authdb, } obj.__id = 0 From c4334be9c3c3d840dfbc17f89284f40ed0531864 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 30 Aug 2018 09:51:55 +0800 Subject: [PATCH 032/565] add mongo_db:auth --- lualib/skynet/db/mongo.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 04b278058..ec8063a6e 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -82,6 +82,8 @@ local function __parse_addr(addr) return host, tonumber(port) end +local auth_method = {} + local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") @@ -95,7 +97,7 @@ local function mongo_auth(mongoc) return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" - local auth_func = mongoc[authmod] + local auth_func = auth_method[authmod] assert(auth_func , "Invalid authmod") assert(auth_func(authdb or mongoc, user, pass)) end @@ -211,7 +213,7 @@ function mongo_client:runCommand(...) return self.admin:runCommand(...) end -function mongo_client:auth_mongodb_cr(user,password) +function auth_method: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 @@ -234,7 +236,7 @@ local function salt_password(password, salt, iter) return output end -function mongo_client:auth_scram_sha1(username,password) +function auth_method:auth_scram_sha1(username,password) local user = string.gsub(string.gsub(username, '=', '=3D'), ',' , '=2C') local nonce = crypt.base64encode(crypt.randomkey()) local first_bare = "n=" .. user .. ",r=" .. nonce @@ -305,6 +307,13 @@ function mongo_client:logout() return result.ok == 1 end +function mongo_db:auth(user, pass) + local authmod = rawget(self.connection, "authmod") or "scram_sha1" + local auth_func = auth_method[authmod] + assert(auth_func , "Invalid authmod") + return auth_func(self, user, pass) +end + function mongo_db:runCommand(cmd,cmd_v,...) local conn = self.connection local request_id = conn:genId() From 1c8ed8a46e0ef141e9ba9d5401bedd3ed4f12a08 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Aug 2018 19:52:41 +0800 Subject: [PATCH 033/565] add mongo config option: authdb, see issue #884 --- lualib/skynet/db/mongo.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 7522da20d..04b278058 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -87,13 +87,17 @@ local function mongo_auth(mongoc) local pass = rawget(mongoc, "password") local authmod = rawget(mongoc, "authmod") or "scram_sha1" authmod = "auth_" .. authmod + local authdb = rawget(mongoc, "authdb") + if authdb then + authdb = mongo_client.getDB(mongoc, authdb) -- mongoc has not set metatable yet + end return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" local auth_func = mongoc[authmod] assert(auth_func , "Invalid authmod") - assert(auth_func(mongoc,user, pass)) + assert(auth_func(authdb or mongoc, user, pass)) end local rs_data = mongoc:runCommand("ismaster") if rs_data.ok == 1 then @@ -156,6 +160,7 @@ function mongo.client( conf ) username = first.username, password = first.password, authmod = first.authmod, + authdb = first.authdb, } obj.__id = 0 From 7e63080c037a9f7b5ce49b60d45624c8d89fb950 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 30 Aug 2018 09:51:55 +0800 Subject: [PATCH 034/565] add mongo_db:auth --- lualib/skynet/db/mongo.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 04b278058..ec8063a6e 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -82,6 +82,8 @@ local function __parse_addr(addr) return host, tonumber(port) end +local auth_method = {} + local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") @@ -95,7 +97,7 @@ local function mongo_auth(mongoc) return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" - local auth_func = mongoc[authmod] + local auth_func = auth_method[authmod] assert(auth_func , "Invalid authmod") assert(auth_func(authdb or mongoc, user, pass)) end @@ -211,7 +213,7 @@ function mongo_client:runCommand(...) return self.admin:runCommand(...) end -function mongo_client:auth_mongodb_cr(user,password) +function auth_method: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 @@ -234,7 +236,7 @@ local function salt_password(password, salt, iter) return output end -function mongo_client:auth_scram_sha1(username,password) +function auth_method:auth_scram_sha1(username,password) local user = string.gsub(string.gsub(username, '=', '=3D'), ',' , '=2C') local nonce = crypt.base64encode(crypt.randomkey()) local first_bare = "n=" .. user .. ",r=" .. nonce @@ -305,6 +307,13 @@ function mongo_client:logout() return result.ok == 1 end +function mongo_db:auth(user, pass) + local authmod = rawget(self.connection, "authmod") or "scram_sha1" + local auth_func = auth_method[authmod] + assert(auth_func , "Invalid authmod") + return auth_func(self, user, pass) +end + function mongo_db:runCommand(cmd,cmd_v,...) local conn = self.connection local request_id = conn:genId() From ce0cdc7bd307b29ca37f43fb0e6854626955b307 Mon Sep 17 00:00:00 2001 From: LiuYang Date: Thu, 30 Aug 2018 11:38:08 +0800 Subject: [PATCH 035/565] add mongo test case: auth --- examples/config.mongodb | 11 +++++++++++ examples/main_mongodb.lua | 12 ++++++++++++ lualib/skynet/db/mongo.lua | 2 +- test/testmongodb.lua | 28 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 examples/config.mongodb create mode 100644 examples/main_mongodb.lua diff --git a/examples/config.mongodb b/examples/config.mongodb new file mode 100644 index 000000000..6413b57dc --- /dev/null +++ b/examples/config.mongodb @@ -0,0 +1,11 @@ +root = "./" +thread = 8 +logger = nil +harbor = 0 +start = "main_mongodb" -- 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_mongodb.lua b/examples/main_mongodb.lua new file mode 100644 index 000000000..0f505ce65 --- /dev/null +++ b/examples/main_mongodb.lua @@ -0,0 +1,12 @@ +local skynet = require "skynet" + + +skynet.start(function() + print("Main Server start") + local console = skynet.newservice( + "testmongodb", "127.0.0.1", 27017, "testdb", "test", "test" + ) + + print("Main Server exit") + skynet.exit() +end) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index ec8063a6e..e020ebcb5 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -309,7 +309,7 @@ end function mongo_db:auth(user, pass) local authmod = rawget(self.connection, "authmod") or "scram_sha1" - local auth_func = auth_method[authmod] + local auth_func = auth_method["auth_" .. authmod] assert(auth_func , "Invalid authmod") return auth_func(self, user, pass) end diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 11d819be5..cda41ec4a 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -3,6 +3,11 @@ local mongo = require "skynet.db.mongo" local bson = require "bson" local host, port, db_name, username, password = ... +if port then + port = math.tointeger(port) +end + +-- print(host, port, db_name, username, password) local function _create_client() return mongo.client( @@ -14,6 +19,25 @@ local function _create_client() ) end +function test_auth() + local c = mongo.client( + { + host = host, port = port, + } + ) + db = c[db_name] + db:auth(username, password) + + db.testdb:dropIndex("*") + db.testdb:drop() + + local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) + + local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) +end + function test_insert_without_index() local db = _create_client() db[db_name].testdb:dropIndex("*") @@ -105,6 +129,10 @@ function test_expire_index() end skynet.start(function() + if username then + print("Test auth") + test_auth() + end print("Test insert without index") test_insert_without_index() print("Test insert index") From c96e20e6c35f951a599546d53a130d147445db54 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 6 Sep 2018 18:59:33 +0800 Subject: [PATCH 036/565] add skynet.context and enhance debug console command service --- lualib/skynet.lua | 14 ++++++++++++++ lualib/skynet/debug.lua | 12 ++++++++---- service/service_mgr.lua | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a9fc92af1..68aa5d9c1 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -439,6 +439,12 @@ function skynet.ret(msg, sz) return ret end +function skynet.context() + local co_session = session_coroutine_id[running_thread] + local co_address = session_coroutine_address[running_thread] + return co_session, co_address +end + function skynet.ignoreret() -- We use session for other uses session_coroutine_id[running_thread] = nil @@ -782,6 +788,14 @@ function skynet.stat(what) end function skynet.task(ret) + if type(ret) == "number" then + local co = session_id_coroutine[ret] + if co then + return debug.traceback(co) + else + return "No session" + end + end local t = 0 for session,co in pairs(session_id_coroutine) do if ret then diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index aeec0e3eb..3b540fdab 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -32,10 +32,14 @@ local function init(skynet, export) skynet.ret(skynet.pack(stat)) end - function dbgcmd.TASK() - local task = {} - skynet.task(task) - skynet.ret(skynet.pack(task)) + function dbgcmd.TASK(session) + if session then + skynet.ret(skynet.pack(skynet.task(session))) + else + local task = {} + skynet.task(task) + skynet.ret(skynet.pack(task)) + end end function dbgcmd.INFO(...) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index dcd901fd5..e1efeb413 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -16,7 +16,7 @@ local function request(name, func, ...) end for _,v in ipairs(s) do - skynet.wakeup(v) + skynet.wakeup(v.co) end if ok then @@ -47,7 +47,12 @@ local function waitfor(name , func, ...) return request(name, func, ...) end - table.insert(s, co) + local session, source = skynet.context() + table.insert(s, { + co = co, + session = session, + source = source, + }) skynet.wait() s = service[name] if type(s) == "string" then @@ -91,7 +96,11 @@ local function list_service() if type(v) == "string" then v = "Error: " .. v elseif type(v) == "table" then - v = "Querying" + local querying = { "Querying:" } + for _, detail in ipairs(v) do + table.insert(querying, skynet.address(detail.source) .. " " .. tostring(skynet.call(detail.source, "debug", "TASK", detail.session))) + end + v = table.concat(querying, "\n") else v = skynet.address(v) end From 61980c8a1f23a87e96cc128716680c11a38c64a8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 7 Sep 2018 16:11:27 +0800 Subject: [PATCH 037/565] add socket.netstat --- lualib-src/lua-socket.c | 65 +++++++++++++++++ lualib/skynet/socket.lua | 1 + service/debug_console.lua | 48 +++++++++++++ skynet-src/skynet_socket.c | 12 +++- skynet-src/skynet_socket.h | 5 ++ skynet-src/skynet_start.c | 1 + skynet-src/socket_info.h | 28 ++++++++ skynet-src/socket_server.c | 142 +++++++++++++++++++++++++++++++++++-- skynet-src/socket_server.h | 6 +- 9 files changed, 299 insertions(+), 9 deletions(-) create mode 100644 skynet-src/socket_info.h diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index a1ef02952..eba3743d1 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -680,6 +680,70 @@ ludp_address(lua_State *L) { return 2; } +static void +getinfo(lua_State *L, struct socket_info *si) { + lua_newtable(L); + lua_pushinteger(L, si->id); + lua_setfield(L, -2, "id"); + lua_pushinteger(L, si->opaque); + lua_setfield(L, -2, "address"); + switch(si->type) { + case SOCKET_INFO_LISTEN: + lua_pushstring(L, "LISTEN"); + lua_setfield(L, -2, "type"); + lua_pushinteger(L, si->read); + lua_setfield(L, -2, "accept"); + if (si->name[0]) { + lua_pushstring(L, si->name); + lua_setfield(L, -2, "sock"); + } + return; + case SOCKET_INFO_TCP: + lua_pushstring(L, "TCP"); + break; + case SOCKET_INFO_UDP: + lua_pushstring(L, "UDP"); + break; + case SOCKET_INFO_BIND: + lua_pushstring(L, "BIND"); + break; + default: + lua_pushstring(L, "UNKNOWN"); + lua_setfield(L, -2, "type"); + return; + } + lua_setfield(L, -2, "type"); + lua_pushinteger(L, si->read); + lua_setfield(L, -2, "read"); + lua_pushinteger(L, si->write); + lua_setfield(L, -2, "write"); + lua_pushinteger(L, si->wbuffer); + lua_setfield(L, -2, "wbuffer"); + lua_pushinteger(L, si->rtime); + lua_setfield(L, -2, "rtime"); + lua_pushinteger(L, si->wtime); + lua_setfield(L, -2, "wtime"); + if (si->name[0]) { + lua_pushstring(L, si->name); + lua_setfield(L, -2, "peer"); + } +} + +static int +linfo(lua_State *L) { + lua_newtable(L); + struct socket_info * si = skynet_socket_info(); + struct socket_info * temp = si; + int n = 0; + while (temp) { + getinfo(L, temp); + lua_seti(L, -2, ++n); + temp = temp->next; + } + socket_info_release(si); + return 1; +} + LUAMOD_API int luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); @@ -693,6 +757,7 @@ luaopen_skynet_socketdriver(lua_State *L) { { "readline", lreadline }, { "str2p", lstr2p }, { "header", lheader }, + { "info", linfo }, { "unpack", lunpack }, { NULL, NULL }, diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 357e780fc..fe7e108bc 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -438,6 +438,7 @@ end socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) +socket.netstat = assert(driver.info) function socket.warning(id, callback) local obj = socket_pool[id] diff --git a/service/debug_console.lua b/service/debug_console.lua index 7ec47453d..dbde18ea8 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -159,6 +159,7 @@ function COMMAND.help() ping = "ping address", call = "call address ...", trace = "trace address [proto] [on|off]", + netstat = "netstat : show netstat", } end @@ -374,3 +375,50 @@ function COMMANDX.call(cmd) local rets = table.pack(skynet.call(address, "lua", table.unpack(args, 2, args.n))) return rets end + +local function bytes(size) + if size == nil or size == 0 then + return + end + if size < 1024 then + return size + end + if size < 1024 * 1024 then + return tostring(size/1024) .. "K" + end + return tostring(size/(1024*1024)) .. "M" +end + +local function convert_stat(info) + local now = skynet.now() + local function time(t) + if t == nil then + return + end + t = now - t + if t < 6000 then + return tostring(t/100) .. "s" + end + local hour = t // (100*60*60) + t = t - hour * 100 * 60 * 60 + local min = t // (100*60) + t = t - min * 100 * 60 + local sec = t / 100 + return string.format("%s%d:%.2gs",hour == 0 and "" or (hour .. ":"),min,sec) + end + + info.address = skynet.address(info.address) + info.read = bytes(info.read) + info.write = bytes(info.write) + info.wbuffer = bytes(info.wbuffer) + info.rtime = time(info.rtime) + info.wtime = time(info.wtime) +end + +function COMMAND.netstat() + local stat = socket.netstat() + for _, info in ipairs(stat) do + convert_stat(info) + end + return stat +end \ No newline at end of file diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index ef255060b..1b56024cb 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -15,7 +15,7 @@ static struct socket_server * SOCKET_SERVER = NULL; void skynet_socket_init() { - SOCKET_SERVER = socket_server_create(); + SOCKET_SERVER = socket_server_create(skynet_now()); } void @@ -29,6 +29,11 @@ skynet_socket_free() { SOCKET_SERVER = NULL; } +void +skynet_socket_updatetime() { + socket_server_updatetime(SOCKET_SERVER, skynet_now()); +} + // mainloop thread static void forward_message(int type, bool padding, struct socket_message * result) { @@ -190,3 +195,8 @@ skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) { sm.data = msg->buffer; return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz); } + +struct socket_info * +skynet_socket_info() { + return socket_server_info(SOCKET_SERVER); +} diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 21dfcef43..2fd38eb31 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -1,6 +1,8 @@ #ifndef skynet_socket_h #define skynet_socket_h +#include "socket_info.h" + struct skynet_context; #define SKYNET_SOCKET_TYPE_DATA 1 @@ -22,6 +24,7 @@ void skynet_socket_init(); void skynet_socket_exit(); void skynet_socket_free(); int skynet_socket_poll(); +void skynet_socket_updatetime(); int skynet_socket_send(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); @@ -38,4 +41,6 @@ int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * a 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); +struct socket_info * skynet_socket_info(); + #endif diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 2f5ddea10..266e8ea25 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -131,6 +131,7 @@ thread_timer(void *p) { skynet_initthread(THREAD_TIMER); for (;;) { skynet_updatetime(); + skynet_socket_updatetime(); CHECK_ABORT wakeup(m,m->count-1); usleep(2500); diff --git a/skynet-src/socket_info.h b/skynet-src/socket_info.h new file mode 100644 index 000000000..78facf6c2 --- /dev/null +++ b/skynet-src/socket_info.h @@ -0,0 +1,28 @@ +#ifndef socket_info_h +#define socket_info_h + +#define SOCKET_INFO_UNKNOWN 0 +#define SOCKET_INFO_LISTEN 1 +#define SOCKET_INFO_TCP 2 +#define SOCKET_INFO_UDP 3 +#define SOCKET_INFO_BIND 4 + +#include + +struct socket_info { + int id; + int type; + uint64_t opaque; + uint64_t read; + uint64_t write; + uint64_t rtime; + uint64_t wtime; + int64_t wbuffer; + char name[128]; + struct socket_info *next; +}; + +struct socket_info * socket_info_create(struct socket_info *last); +void socket_info_release(struct socket_info *); + +#endif diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index b74dc1650..ddee29431 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -75,11 +75,19 @@ struct wb_list { struct write_buffer * tail; }; +struct socket_stat { + uint64_t rtime; + uint64_t wtime; + uint64_t read; + uint64_t write; +}; + struct socket { uintptr_t opaque; struct wb_list high; struct wb_list low; int64_t wb_size; + struct socket_stat stat; volatile uint32_t sending; int fd; int id; @@ -98,6 +106,7 @@ struct socket { }; struct socket_server { + volatile uint64_t time; int recvctrl_fd; int sendctrl_fd; int checkctrl; @@ -188,6 +197,7 @@ struct request_udp { T Set opt U Create UDP socket C set udp address + Q query info */ struct request_package { @@ -326,7 +336,7 @@ clear_wb_list(struct wb_list *list) { } struct socket_server * -socket_server_create() { +socket_server_create(uint64_t time) { int i; int fd[2]; poll_fd efd = sp_create(); @@ -349,6 +359,7 @@ socket_server_create() { } struct socket_server *ss = MALLOC(sizeof(*ss)); + ss->time = time; ss->event_fd = efd; ss->recvctrl_fd = fd[0]; ss->sendctrl_fd = fd[1]; @@ -371,6 +382,11 @@ socket_server_create() { return ss; } +void +socket_server_updatetime(struct socket_server *ss, uint64_t time) { + ss->time = time; +} + static void free_wb_list(struct socket_server *ss, struct wb_list *list) { struct write_buffer *wb = list->head; @@ -468,9 +484,22 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, check_wb_list(&s->low); s->dw_buffer = NULL; s->dw_size = 0; + memset(&s->stat, 0, sizeof(s->stat)); return s; } +static inline void +stat_read(struct socket_server *ss, struct socket *s, int n) { + s->stat.read += n; + s->stat.rtime = ss->time; +} + +static inline void +stat_write(struct socket_server *ss, struct socket *s, int n) { + s->stat.write += n; + s->stat.wtime = ss->time; +} + // return -1 when connecting static int open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result) { @@ -563,6 +592,7 @@ send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, force_close(ss,s,l,result); return SOCKET_CLOSE; } + stat_write(ss,s,(int)sz); s->wb_size -= sz; if (sz != tmp->sz) { tmp->ptr += sz; @@ -634,7 +664,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, drop_udp(ss, s, list, tmp); return -1; } - + stat_write(ss,s,tmp->sz); s->wb_size -= tmp->sz; list->head = tmp->next; write_buffer_free(ss,tmp); @@ -847,6 +877,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock if (n != so.sz) { append_sendbuffer_udp(ss,s,priority,request,udp_address); } else { + stat_write(ss,s,n); so.free_func(request->buffer); return -1; } @@ -1193,6 +1224,8 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo return -1; } + stat_read(ss,s,n); + if (n == sz) { s->p.size *= 2; } else if (sz > MIN_READ_BUFFER && n*2 < sz) { @@ -1242,6 +1275,8 @@ forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_lo } return -1; } + stat_read(ss,s,n); + uint8_t * data; if (slen == sizeof(sa.v4)) { if (s->protocol != PROTOCOL_UDP) @@ -1298,6 +1333,20 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l } } +static int +getname(union sockaddr_all *u, char *buffer, size_t sz) { + char tmp[INET6_ADDRSTRLEN]; + void * sin_addr = (u->s.sa_family == AF_INET) ? (void*)&u->v4.sin_addr : (void *)&u->v6.sin6_addr; + int sin_port = ntohs((u->s.sa_family == AF_INET) ? u->v4.sin_port : u->v6.sin6_port); + if (inet_ntop(u->s.sa_family, sin_addr, tmp, sizeof(tmp))) { + snprintf(buffer, sz, "%s:%d", tmp, sin_port); + return 1; + } else { + buffer[0] = '\0'; + return 0; + } +} + // return 0 when failed, or -1 when file limit static int report_accept(struct socket_server *ss, struct socket *s, struct socket_message *result) { @@ -1327,17 +1376,16 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message close(client_fd); return 0; } + // accept new one connection + stat_read(ss,s,1); + ns->type = SOCKET_TYPE_PACCEPT; result->opaque = s->opaque; result->id = s->id; result->ud = id; result->data = NULL; - void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr; - 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); + if (getname(&u, ss->buffer, sizeof(ss->buffer))) { result->data = ss->buffer; } @@ -1558,6 +1606,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz // ignore error, let socket thread try again n = 0; } + stat_write(ss,s,n); if (n == so.sz) { // write done socket_unlock(&l); @@ -1827,6 +1876,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); if (n >= 0) { // sendto succ + stat_write(ss,s,n); socket_unlock(&l); so.free_func((void *)buffer); return 0; @@ -1915,3 +1965,81 @@ socket_server_udp_address(struct socket_server *ss, struct socket_message *msg, } return (const struct socket_udp_address *)address; } + + +struct socket_info * +socket_info_create(struct socket_info *last) { + struct socket_info *si = skynet_malloc(sizeof(*si)); + memset(si, 0 , sizeof(*si)); + si->next = last; + return si; +} + +void +socket_info_release(struct socket_info *si) { + while (si) { + struct socket_info *temp = si; + si = si->next; + skynet_free(temp); + } +} + +static int +query_info(struct socket *s, struct socket_info *si) { + union sockaddr_all u; + socklen_t slen = sizeof(u); + switch (s->type) { + case SOCKET_TYPE_BIND: + si->type = SOCKET_INFO_BIND; + si->name[0] = '\0'; + break; + case SOCKET_TYPE_LISTEN: + si->type = SOCKET_INFO_LISTEN; + if (getsockname(s->fd, &u.s, &slen) == 0) { + getname(&u, si->name, sizeof(si->name)); + } + break; + case SOCKET_TYPE_CONNECTED: + if (s->protocol == PROTOCOL_TCP) { + si->type = SOCKET_INFO_TCP; + if (getpeername(s->fd, &u.s, &slen) == 0) { + getname(&u, si->name, sizeof(si->name)); + } + } else { + si->type = SOCKET_INFO_UDP; + if (udp_socket_address(s, s->p.udp_address, &u)) { + getname(&u, si->name, sizeof(si->name)); + } + } + break; + default: + return 0; + } + si->id = s->id; + si->opaque = (uint64_t)s->opaque; + si->read = s->stat.read; + si->write = s->stat.write; + si->rtime = s->stat.rtime; + si->wtime = s->stat.wtime; + si->wbuffer = s->wb_size; + + return 1; +} + +struct socket_info * +socket_server_info(struct socket_server *ss) { + int i; + struct socket_info * si = NULL; + for (i=0;islot[i]; + int id = s->id; + struct socket_info temp; + if (query_info(s, &temp) && s->id == id) { + // socket_server_info may call in different thread, so check socket id again + si = socket_info_create(si); + temp.next = si->next; + *si = temp; + } + } + return si; +} diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 622b086fd..965685027 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -2,6 +2,7 @@ #define skynet_socket_server_h #include +#include "socket_info.h" #define SOCKET_DATA 0 #define SOCKET_CLOSE 1 @@ -21,8 +22,9 @@ struct socket_message { char * data; }; -struct socket_server * socket_server_create(); +struct socket_server * socket_server_create(uint64_t time); void socket_server_release(struct socket_server *); +void socket_server_updatetime(struct socket_server *, uint64_t time); int socket_server_poll(struct socket_server *, struct socket_message *result, int *more); void socket_server_exit(struct socket_server *); @@ -64,4 +66,6 @@ struct socket_object_interface { // if you send package sz == -1, use soi. void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi); +struct socket_info * socket_server_info(struct socket_server *); + #endif From f8434d34b6b9f8ef70f406f885b4d358d034e145 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 7 Sep 2018 16:26:56 +0800 Subject: [PATCH 038/565] report last accept time --- 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 eba3743d1..1cb8dcc26 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -693,6 +693,8 @@ getinfo(lua_State *L, struct socket_info *si) { lua_setfield(L, -2, "type"); lua_pushinteger(L, si->read); lua_setfield(L, -2, "accept"); + lua_pushinteger(L, si->rtime); + lua_setfield(L, -2, "rtime"); if (si->name[0]) { lua_pushstring(L, si->name); lua_setfield(L, -2, "sock"); From 823b7730517e61770ce787fe4a92af2145cf12a4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Sep 2018 11:31:12 +0800 Subject: [PATCH 039/565] remove unused require --- examples/agent.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/agent.lua b/examples/agent.lua index a27645a04..61628435b 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -1,5 +1,4 @@ local skynet = require "skynet" -local netpack = require "skynet.netpack" local socket = require "skynet.socket" local sproto = require "sproto" local sprotoloader = require "sprotoloader" From c83915947091dcf97f164f64a07494579a099683 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Sep 2018 11:45:41 +0800 Subject: [PATCH 040/565] remove netpack from gate service --- service/gate.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/service/gate.lua b/service/gate.lua index 5d81ef4b4..ef4c24af9 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,6 +1,5 @@ local skynet = require "skynet" local gateserver = require "snax.gateserver" -local netpack = require "skynet.netpack" local watchdog local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } @@ -22,9 +21,12 @@ function handler.message(fd, msg, sz) local c = connection[fd] local agent = c.agent if agent then + -- It's safe to redirect msg directly , gateserver framework will not free msg. skynet.redirect(agent, c.client, "client", fd, msg, sz) else - skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) + skynet.send(watchdog, "lua", "socket", "data", fd, skynet.tostring(msg, sz)) + -- skynet.tostring will copy msg to a string, so we must free msg here. + skynet.trash(msg,sz) end end From 43d02ac45605b4aa6f7fac6ddd17660ab85942b7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Sep 2018 15:23:59 +0800 Subject: [PATCH 041/565] update jemalloc to 5.1.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 896ed3a8b..61efbda70 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 896ed3a8b3f41998d4fb4d625d30ac63ef2d51fb +Subproject commit 61efbda7098de6fe64c362d309824864308c36d4 From 89e7a06deb6d353f6e4d46ebfcd919a06f6e933d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Sep 2018 18:27:05 +0800 Subject: [PATCH 042/565] add more info when service init blocked --- lualib/skynet.lua | 40 +++++++++++++++++++++++++++++++--------- service/launcher.lua | 14 ++++++++++++++ service/service_mgr.lua | 28 ++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 68aa5d9c1..789cb77c9 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -12,6 +12,7 @@ local profile = require "skynet.profile" local cresume = profile.resume local running_thread = nil +local init_thread = nil local function coroutine_resume(co, ...) running_thread = co @@ -219,6 +220,7 @@ function skynet.timeout(ti, func) local co = co_create(func) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co + return co -- for debug end local function suspend_sleep(session, token) @@ -770,8 +772,9 @@ end function skynet.start(start_func) c.callback(skynet.dispatch_message) - skynet.timeout(0, function() + init_thread = skynet.timeout(0, function() skynet.init_service(start_func) + init_thread = nil end) end @@ -788,22 +791,41 @@ function skynet.stat(what) end function skynet.task(ret) - if type(ret) == "number" then + if ret == nil then + local t = 0 + for session,co in pairs(session_id_coroutine) do + t = t + 1 + end + return t + end + if ret == "init" then + if init_thread then + return debug.traceback(init_thread) + else + return + end + end + local tt = type(ret) + if tt == "table" then + for session,co in pairs(session_id_coroutine) do + ret[session] = debug.traceback(co) + end + return + elseif tt == "number" then local co = session_id_coroutine[ret] if co then return debug.traceback(co) else return "No session" end - end - local t = 0 - for session,co in pairs(session_id_coroutine) do - if ret then - ret[session] = debug.traceback(co) + elseif tt == "thread" then + for session, co in pairs(session_id_coroutine) do + if co == ret then + return session + end end - t = t + 1 + return end - return t end function skynet.term(service) diff --git a/service/launcher.lua b/service/launcher.lua index 272a459a8..d458d13ef 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -6,6 +6,7 @@ local string = string local services = {} local command = {} local instance = {} -- for confirm (function command.LAUNCH / command.ERROR / command.LAUNCHOK) +local launch_session = {} -- for command.QUERY, service_address -> session local function handle_to_address(handle) return tonumber("0x" .. string.sub(handle , 2)) @@ -68,6 +69,7 @@ function command.REMOVE(_, handle, kill) -- instance is dead response(not kill) -- return nil to caller of newservice, when kill == false instance[handle] = nil + launch_session[handle] = nil end -- don't return (skynet.ret) because the handle may exit @@ -77,10 +79,12 @@ end local function launch_service(service, ...) local param = table.concat({...}, " ") local inst = skynet.launch(service, param) + local session = skynet.context() local response = skynet.response() if inst then services[inst] = service .. " " .. param instance[inst] = response + launch_session[inst] = session else response(false) return @@ -107,6 +111,7 @@ function command.ERROR(address) local response = instance[address] if response then response(false) + launch_session[address] = nil instance[address] = nil end services[address] = nil @@ -119,11 +124,20 @@ function command.LAUNCHOK(address) if response then response(true, address) instance[address] = nil + launch_session[address] = nil end return NORET end +function command.QUERY(_, request_session) + for address, session in pairs(launch_session) do + if session == request_session then + return address + end + end +end + -- for historical reasons, launcher support text command (for C service) skynet.register_protocol { diff --git a/service/service_mgr.lua b/service/service_mgr.lua index e1efeb413..c4d6273ce 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -42,12 +42,17 @@ local function waitfor(name , func, ...) assert(type(s) == "table") - if not s.launch and func then - s.launch = true + local session, source = skynet.context() + + if s.launch == nil and func then + s.launch = { + session = session, + source = source, + co = co, + } return request(name, func, ...) end - local session, source = skynet.context() table.insert(s, { co = co, session = session, @@ -96,9 +101,20 @@ local function list_service() if type(v) == "string" then v = "Error: " .. v elseif type(v) == "table" then - local querying = { "Querying:" } - for _, detail in ipairs(v) do - table.insert(querying, skynet.address(detail.source) .. " " .. tostring(skynet.call(detail.source, "debug", "TASK", detail.session))) + local querying = {} + if v.launch then + local session = skynet.task(v.launch.co) + local launching_address = skynet.call(".launcher", "lua", "QUERY", session) + table.insert(querying, "Init as " .. skynet.address(launching_address)) + table.insert(querying, tostring(skynet.call(launching_address, "debug", "TASK", "init"))) + table.insert(querying, "Launching from " .. skynet.address(v.launch.source)) + table.insert(querying, tostring(skynet.call(v.launch.source, "debug", "TASK", v.launch.session))) + end + if #v > 0 then + table.insert(querying , "Querying:" ) + for _, detail in ipairs(v) do + table.insert(querying, skynet.address(detail.source) .. " " .. tostring(skynet.call(detail.source, "debug", "TASK", detail.session))) + end end v = table.concat(querying, "\n") else From 34a99792550bb2aaf6946ab6f18f06d0d185313f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Sep 2018 18:59:03 +0800 Subject: [PATCH 043/565] check launching address --- service/service_mgr.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index c4d6273ce..093459555 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -105,10 +105,12 @@ local function list_service() if v.launch then local session = skynet.task(v.launch.co) local launching_address = skynet.call(".launcher", "lua", "QUERY", session) - table.insert(querying, "Init as " .. skynet.address(launching_address)) - table.insert(querying, tostring(skynet.call(launching_address, "debug", "TASK", "init"))) - table.insert(querying, "Launching from " .. skynet.address(v.launch.source)) - table.insert(querying, tostring(skynet.call(v.launch.source, "debug", "TASK", v.launch.session))) + if launching_address then + table.insert(querying, "Init as " .. skynet.address(launching_address)) + table.insert(querying, skynet.call(launching_address, "debug", "TASK", "init")) + table.insert(querying, "Launching from " .. skynet.address(v.launch.source)) + table.insert(querying, skynet.call(v.launch.source, "debug", "TASK", v.launch.session)) + end end if #v > 0 then table.insert(querying , "Querying:" ) From d9fb4d06ec3681700772d8a9ebe346d5a9a95678 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Sun, 16 Sep 2018 13:24:15 +0800 Subject: [PATCH 044/565] remove useless local function --- lualib/skynet/multicast.lua | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lualib/skynet/multicast.lua b/lualib/skynet/multicast.lua index 1c76b8770..d122fead6 100644 --- a/lualib/skynet/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -16,14 +16,6 @@ local chan_meta = { end, } -local function default_conf(conf) - conf = conf or {} - conf.pack = conf.pack or skynet.pack - conf.unpack = conf.unpack or skynet.unpack - - return conf -end - function multicast.new(conf) assert(multicastd, "Init first") local self = {} From 46b49adf9e213884e75e205fbf949eefefd02c0b Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Sun, 16 Sep 2018 13:20:34 +0800 Subject: [PATCH 045/565] remove useless file --- lualib/skynet/reload.lua | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 lualib/skynet/reload.lua diff --git a/lualib/skynet/reload.lua b/lualib/skynet/reload.lua deleted file mode 100644 index 33e234b09..000000000 --- a/lualib/skynet/reload.lua +++ /dev/null @@ -1,9 +0,0 @@ -local core = require "skynet.reload.core" -local skynet = require "skynet" - -local function reload(...) - local args = SERVICE_NAME .. " " .. table.concat({...}, " ") - print(args) -end - -return reload From f217a56f6eff62d6675f1a114276de222ab982e0 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Wed, 19 Sep 2018 19:46:42 +0800 Subject: [PATCH 046/565] Uniform indent to tab and clear up. --- lualib/snax/interface.lua | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 07c1b7281..5a403cfd4 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -1,24 +1,23 @@ local skynet = require "skynet" local function dft_loader(path, name, G) - local errlist = {} + 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 + 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")) + error(table.concat(errlist, "\n")) end return function (name , G, loader) - loader = loader or dft_loader - local mainfunc + loader = loader or dft_loader local function func_id(id, group) local tmp = {} @@ -42,7 +41,6 @@ return function (name , G, loader) assert(getmetatable(G) == nil) assert(G.init == nil) assert(G.exit == nil) - assert(G.accept == nil) assert(G.response == nil) end @@ -75,10 +73,8 @@ return function (name , G, loader) end end - local pattern - - local path = assert(skynet.getenv "snax" , "please set snax in config file") - mainfunc, pattern = loader(path, name, G) + local path = assert(skynet.getenv "snax" , "please set snax in config file") + local mainfunc, pattern = loader(path, name, G) setmetatable(G, { __index = env , __newindex = init_system }) local ok, err = xpcall(mainfunc, debug.traceback) From d23e6f8d8efd7106b288bf04f22f67ce7c1f5f94 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Wed, 19 Sep 2018 20:25:47 +0800 Subject: [PATCH 047/565] 1. remove unused code; 2. weird indents fixed. --- lualib/skynet/dns.lua | 1 - lualib/snax/hotfix.lua | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index fb6c2f238..c280f06a1 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -372,7 +372,6 @@ local function suspend(tid, name, qtype) end end) skynet.wait(req.co) - local answers = req.answers request_pool[tid] = nil if not req.answers then local answers = lookup_cache(name, qtype, true) diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index a73d680ea..910fbda3b 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -71,7 +71,7 @@ local dummy_env = {} for k,v in pairs(_ENV) do dummy_env[k] = v end local function _patch(global, f) - local i = 1 + local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then @@ -81,10 +81,10 @@ local function _patch(global, 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 + else + if type(value) == "function" then + _patch(global, value) + end end i = i + 1 end @@ -92,7 +92,7 @@ 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) + _patch(global, f) desc[4] = f end From 818ff8314d2eab2f60a373c643f5f96852513b0b Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Wed, 19 Sep 2018 19:51:13 +0800 Subject: [PATCH 048/565] remove unused code --- lualib/skynet.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 789cb77c9..d0acc653b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -1,7 +1,6 @@ -- read https://github.com/cloudwu/skynet/wiki/FAQ for the module "skynet.core" local c = require "skynet.core" local tostring = tostring -local tonumber = tonumber local coroutine = coroutine local assert = assert local pairs = pairs @@ -69,9 +68,6 @@ local fork_queue = {} -- suspend is function local suspend -local function string_to_handle(str) - return tonumber("0x" .. string.sub(str , 2)) -end ----- monitor exit @@ -179,7 +175,7 @@ local function dispatch_wakeup() end -- suspend is local function -function suspend(co, result, command, param, param2) +function suspend(co, result, command) if not result then local session = session_coroutine_id[co] if session then -- coroutine may fork by others (session is nil) From 02d5f4fbe9534eafa2ffca34ad29c7e83c0f79d9 Mon Sep 17 00:00:00 2001 From: sundream Date: Wed, 19 Sep 2018 11:47:02 +0800 Subject: [PATCH 049/565] rediscluster: support eval/evalshal + subscribe/publish --- lualib/skynet/db/redis/cluster.lua | 217 ++++++++++++++++++++--------- test/testrediscluster.lua | 36 ++++- 2 files changed, 183 insertions(+), 70 deletions(-) diff --git a/lualib/skynet/db/redis/cluster.lua b/lualib/skynet/db/redis/cluster.lua index 9c83a929c..d7ab6468b 100644 --- a/lualib/skynet/db/redis/cluster.lua +++ b/lualib/skynet/db/redis/cluster.lua @@ -8,24 +8,60 @@ local crc16 = require "skynet.db.redis.crc16" local RedisClusterHashSlots = 16384 local RedisClusterRequestTTL = 16 +local sync = { + once = { + tasks = {}, + } +} + +function sync.once.Do(id,func,...) + local tasks = sync.once.tasks + local task = tasks[id] + if not task then + task = { + waiting = {}, + result = nil, + } + tasks[id] = task + local rettbl = table.pack(xpcall(func,debug.traceback,...)) + task.result = rettbl + local waiting = task.waiting + tasks[id] = nil + if next(waiting) then + for i,co in ipairs(waiting) do + skynet.wakeup(co) + end + end + return table.unpack(task.result,1,task.result.n) + else + local co = coroutine.running() + table.insert(task.waiting,co) + skynet.wait(co) + return table.unpack(task.result,1,task.result.n) + end +end + local _M = {} local rediscluster = {} rediscluster.__index = rediscluster -_M.rediscluster = rediscluster -function _M.new(startup_nodes,opt) +function _M.new(startup_nodes,opt,onmessage) 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"}), + max_connections = opt.max_connections or 256, + connections = {}, opt = opt, refresh_table_asap = false, + + -- for subscribe/publish + __onmessage = onmessage, + __watching = {}, } setmetatable(self,rediscluster) self:initialize_slots_cache() @@ -53,13 +89,6 @@ 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 @@ -67,11 +96,9 @@ end 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 conn = self:get_connection(startup_node) local list = conn:cluster("slots") for _,result in ipairs(list) do local ip,port = table.unpack(result[3]) @@ -100,9 +127,6 @@ function rediscluster:initialize_slots_cache() 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 @@ -154,6 +178,21 @@ function rediscluster:get_key_from_command(argv) cmd == "shutdown" then return nil end + if cmd == "eval" or cmd == "evalsha" then + -- eval script numkeys key [key...] arg [arg...] + local numkeys = argv[3] + local firstkey_slot = nil + for i=4,4+numkeys-1 do + local slot = self:keyslot(argv[i]) + if not firstkey_slot then + firstkey_slot = slot + elseif firstkey_slot ~= slot then + error(string.format("%s - all keys must map to the same key slot",cmd)) + end + end + -- numkeys <=0 will return nil + return argv[4] + end -- Unknown commands, and all the commands having the key -- as first argument are handled here: -- set, get, ... @@ -179,21 +218,28 @@ end function rediscluster:close_all_connection() local connections = self.connections - self.connections = setmetatable({},{__mode = "kv"}) + self.connections = {} for name,conn in pairs(connections) do pcall(conn.disconnect,conn) end end function rediscluster:get_connection(node) - node.port = assert(tonumber(node.port)) - 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 + if not node.name then + node.name = nodename(node) end - return self.connections[name] + local name = node.name + local ok,conn = sync.once.Do(name,function () + local conn = self.connections[name] + if not conn then + self:close_existing_connection() + conn = self:get_redis_link(node) + self.connections[name] = conn + end + return conn + end) + assert(ok,conn) + return conn end -- Return a link to a random node, or raise an error if no node can be @@ -219,25 +265,15 @@ function rediscluster:get_random_connection() 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 + local conn = self:get_connection(node) + if conn:ping() == "PONG" then + return conn else - -- The node was already connected, test the connection. - if conn:ping() == "PONG" then - return conn - end + -- If the connection is not good close it ASAP in order + -- to avoid waiting for the GC finalizer. File + -- descriptors are a rare resource. + self.connetions[node.name] = nil + conn:disconnect() end end) if ok and conn then @@ -256,33 +292,40 @@ function rediscluster:get_connection_by_slot(slot) 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 + local ok,conn = pcall(self.get_connection,self,node) + 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 - -- 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 + -- 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 - return self.connections[node.name] + return conn end -- Dispatch commands. function rediscluster:call(...) local argv = table.pack(...) + local cmd = argv[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 + if cmd == "subscribe" or cmd == "unsubscribe" or cmd == "psubscribe" or cmd == "punsubscribe" then + local slot = self:keyslot(key) + local conn = self:get_watch_connection_by_slot(slot) + local func = conn[cmd] + return func(conn,table.unpack(argv,2)) + end + if self.refresh_table_asap then self:initialize_slots_cache() end @@ -292,10 +335,6 @@ function rediscluster:call(...) 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 @@ -312,7 +351,6 @@ function rediscluster:call(...) conn:asking() end asking = false - local cmd = argv[1] local func = conn[cmd] return func(conn,table.unpack(argv,2)) end)} @@ -321,7 +359,7 @@ function rediscluster:call(...) err = table.unpack(result,2) err = tostring(err) if err == "[Error: socket]" then - -- may be nerver come here? + -- may be never come here? try_random_node = true if ttl < RedisClusterRequestTTL/2 then skynet.sleep(10) @@ -351,6 +389,10 @@ function rediscluster:call(...) port = node_port, } if not asking then + local oldnode = self.slots[newslot] + if oldnode then + node.slaves = oldnode.slaves + end self:set_node_name(node) self.slots[newslot] = node else @@ -365,6 +407,44 @@ function rediscluster:call(...) error(string.format("Too many Cluster redirections?,maybe node is disconnected (last error: %q)",err)) end +function rediscluster:get_watch_connection_by_slot(slot) + if not self.slots[slot] then + self:initialize_slots_cache() + end + local node = assert(self.slots[slot]) + return self:get_watch_connection(node) +end + +function rediscluster:get_watch_connection(node) + local name = node.name + local ok,conn = sync.once.Do(name,function () + if not self.__watching[name] then + local conf = { + host = node.host, + port = node.port, + auth = self.opt.auth, + db = self.opt.db or 0, + } + local db = redis.watch(conf) + self.__watching[name] = db + local onmessage = rawget(self,"__onmessage") + skynet.fork(function () + while true do + local ok,data,channel,pchannel = pcall(db.message,db) + if ok then + if data and onmessage then + pcall(onmessage,data,channel,pchannel) + end + end + end + end) + end + return self.__watching[name] + end) + assert(ok,conn) + return conn +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 @@ -379,5 +459,4 @@ setmetatable(rediscluster,{ end, }) - return _M diff --git a/test/testrediscluster.lua b/test/testrediscluster.lua index 5c6efdf0e..cf3e9aae8 100644 --- a/test/testrediscluster.lua +++ b/test/testrediscluster.lua @@ -3,11 +3,17 @@ local rediscluster = require "skynet.db.redis.cluster" local test_more = ... +-- subscribe mode's callback +local function onmessage(data,channel,pchannel) + print("onmessage",data,channel,pchannel) +end + 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,} + {read_slave=true,auth=nil,db=0,}, + onmessage ) db:del("list") db:del("map") @@ -87,6 +93,34 @@ skynet.start(function () end end + -- test subscribe/publish + db:subscribe("world") + db:subscribe("myteam") + db:publish("world","hello,world") + db:publish("myteam","hello,my team") + -- low-version(such as 3.0.2) redis-server psubscribe is locally + -- if publish and psubscribe not map to same node, + -- we may lost message. so upgrade your redis or use tag to resolved! + db:psubscribe("{tag}*team") + db:publish("{tag}1team","hello,1team") + db:publish("{tag}2team","hello,2team") + + -- i test in redis-4.0.9, it's ok + db:psubscribe("*team") + db:publish("1team","hello,1team") + db:publish("2team","hello,2team") + + -- test eval + db:set("A",1) + local script = [[ + if redis.call("get",KEYS[1]) == ARGV[1] then + return "ok" + else + return "fail" + end]] + print("eval#get",db:eval(script,1,"A",1)) + db:del("A") + if not test_more then skynet.exit() return From 98b5c7c7c655db658f1f92b9cd5175b56b1d9298 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Oct 2018 14:20:53 +0800 Subject: [PATCH 050/565] address 0 is reserved --- skynet-src/skynet_handle.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index d904288ec..f20324ee7 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -39,8 +39,12 @@ skynet_handle_register(struct skynet_context *ctx) { for (;;) { int i; - for (i=0;islot_size;i++) { - uint32_t handle = (i+s->handle_index) & HANDLE_MASK; + uint32_t handle = s->handle_index; + for (i=0;islot_size;i++,handle++) { + if (handle > HANDLE_MASK) { + // 0 is reserved + handle = 1; + } int hash = handle & (s->slot_size-1); if (s->slot[hash] == NULL) { s->slot[hash] = ctx; From 7e57e498f6df4338e515a3a2d3f7bac3be81cdcb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Oct 2018 15:44:12 +0800 Subject: [PATCH 051/565] remove dead_service table, address may reused --- lualib-src/lua-skynet.c | 5 +++ lualib/skynet.lua | 82 ++++++++++++-------------------------- skynet-src/skynet_server.c | 9 ++++- 3 files changed, 38 insertions(+), 58 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 5192fc3fa..e94a395c2 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -278,6 +278,11 @@ send_message(lua_State *L, int source, int idx_type) { luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } if (session < 0) { + if (session == -2) { + // package is too large + lua_pushboolean(L, 0); + return 1; + } // send to invalid address // todo: maybe throw an error would be better return 0; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d0acc653b..00d662643 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -59,9 +59,7 @@ local unresponse = {} local wakeup_queue = {} local sleep_session = {} -local watching_service = {} local watching_session = {} -local dead_service = {} local error_queue = {} local fork_queue = {} @@ -83,10 +81,12 @@ end local function _error_dispatch(error_session, error_source) skynet.ignoreret() -- don't return for error if error_session == 0 then - -- service is down - -- Don't remove from watching_service , because user may call dead service - if watching_service[error_source] then - dead_service[error_source] = true + -- error_source is down, clear unreponse set + for resp, address in pairs(unresponse) do + if error_source == address then + print("UNRESP", address) + unresponse[resp] = nil + end end for session, srv in pairs(watching_session) do if srv == error_source then @@ -101,18 +101,6 @@ local function _error_dispatch(error_session, error_source) 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 - -- coroutine reuse local coroutine_pool = setmetatable({}, { __mode = "kv" }) @@ -139,7 +127,6 @@ local function co_create(f) end local address = session_coroutine_address[co] if address then - release_watching(address) session_coroutine_id[co] = nil end @@ -423,18 +410,14 @@ function skynet.ret(msg, sz) if not co_session then error "No session" end - local ret - if not dead_service[co_address] then - ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, msg, sz) ~= 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 sz ~= nil then - c.trash(msg, sz) - return false + local ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, msg, sz) + if ret then + return true + elseif ret == false 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 - return ret + return false end function skynet.context() @@ -454,47 +437,38 @@ function skynet.response(pack) local co_session = assert(session_coroutine_id[running_thread], "no session") session_coroutine_id[running_thread] = nil local co_address = session_coroutine_address[running_thread] + if co_session == 0 then + -- do not response when session == 0 (send) + return function() end + end local function response(ok, ...) if ok == "TEST" then - if dead_service[co_address] then - release_watching(co_address) - unresponse[response] = nil - pack = false - return false - else - return true - end + return unresponse[response] ~= nil end if not pack then - if pack == false then - pack = nil - return false - end error "Can't response more than once" end local ret - -- do not response when session == 0 (send) - if co_session ~= 0 and not dead_service[co_address] then + if unresponse[response] then if ok then - ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, pack(...)) ~= nil - if not ret then + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, pack(...)) + if ret == false 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 + ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") end + unresponse[response] = nil + ret = ret ~= nil else ret = false end - release_watching(co_address) - unresponse[response] = nil pack = nil return ret end - watching_service[co_address] = watching_service[co_address] + 1 - unresponse[response] = true + unresponse[response] = co_address return response end @@ -588,12 +562,6 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) 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/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index fc79bdd7f..e49340ce3 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -703,7 +703,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } - return -1; + return -2; } _filter_args(context, type, &session, (void **)&data, &sz); @@ -753,6 +753,13 @@ skynet_sendname(struct skynet_context * context, uint32_t source, const char * a return -1; } } else { + if ((sz & MESSAGE_TYPE_MASK) != sz) { + skynet_error(context, "The message to %s is too large", addr); + if (type & PTYPE_TAG_DONTCOPY) { + skynet_free(data); + } + return -2; + } _filter_args(context, type, &session, (void **)&data, &sz); struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg)); From 08e56f4316cc2261fb893fda96f82c61714db0d9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 11 Oct 2018 17:38:49 +0800 Subject: [PATCH 052/565] fix #793 --- skynet-src/skynet_server.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e49340ce3..9ee423001 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -712,6 +712,12 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati } if (destination == 0) { + if (data) { + skynet_error(context, "Destination address can't be 0"); + skynet_free(data); + return -1; + } + return session; } if (skynet_harbor_message_isremote(destination)) { From e0f16b8df5d74122aba4a51e9dec94314cee2e13 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Oct 2018 15:44:12 +0800 Subject: [PATCH 053/565] remove dead_service table, address may reused --- lualib/skynet.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 00d662643..4cbb69fbc 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -84,7 +84,6 @@ local function _error_dispatch(error_session, error_source) -- error_source is down, clear unreponse set for resp, address in pairs(unresponse) do if error_source == address then - print("UNRESP", address) unresponse[resp] = nil end end From f110b51290dbb5222ef74363aa6f67d791e2b86d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Oct 2018 14:54:44 +0800 Subject: [PATCH 054/565] add socketchannel.overload --- lualib/skynet/db/mongo.lua | 1 + lualib/skynet/db/mysql.lua | 14 +++----------- lualib/skynet/db/redis.lua | 1 + lualib/skynet/socketchannel.lua | 28 ++++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index e020ebcb5..441b74bf9 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -173,6 +173,7 @@ function mongo.client( conf ) auth = mongo_auth(obj), backup = backup, nodelay = true, + overload = conf.overload, } setmetatable(obj, client_meta) obj.__sock:connect(true) -- try connect only once diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index f08995b06..606c7efae 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -21,26 +21,17 @@ local sha1= crypt.sha1 local setmetatable = setmetatable local error = error local tonumber = tonumber -local new_tab = function (narr, nrec) return {} end +local new_tab = function (narr, nrec) return {} end -local _M = { _VERSION = '0.13' } +local _M = { _VERSION = '0.14' } -- 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) @@ -640,6 +631,7 @@ function _M.connect(opts) host = opts.host, port = opts.port or 3306, auth = _mysql_login(self,user,password,database,opts.on_connect), + overload = opts.overload, } self.sockchannel = channel -- try connect first only once diff --git a/lualib/skynet/db/redis.lua b/lualib/skynet/db/redis.lua index e838a978b..37aeb00ea 100644 --- a/lualib/skynet/db/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -143,6 +143,7 @@ function redis.connect(db_conf) port = db_conf.port or 6379, auth = redis_login(db_conf.auth, db_conf.db), nodelay = true, + overload = db_conf.overload, } -- try connect first only once channel:connect(true) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 737f0926a..2fbf08afb 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -39,6 +39,8 @@ function socket_channel.channel(desc) __closed = false, __authcoroutine = false, __nodelay = desc.nodelay, + __overload_notify = desc.overload, + __overload = false, } return setmetatable(c, channel_meta) @@ -246,6 +248,32 @@ local function connect_once(self) socketdriver.nodelay(fd) end + -- register overload warning + + local overload = self.__overload_notify + if overload then + local function overload_trigger(id, size) + if id == self.__sock[1] then + if size == 0 then + if self.__overload then + self.__overload = false + overload(false) + end + else + if not self.__overload then + self.__overload = true + overload(true) + else + skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port)) + end + end + end + end + + skynet.fork(overload_trigger, fd, 0) + socket.warning(fd, overload_trigger) + end + while self.__dispatch_thread do -- wait for dispatch thread exit skynet.yield() From b4752f239417052e983bebd5d2b340a203ad0fae Mon Sep 17 00:00:00 2001 From: Sean Feng Date: Wed, 17 Oct 2018 18:32:11 +0800 Subject: [PATCH 055/565] multiresultset spelling mistake --- lualib/skynet/db/mysql.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 606c7efae..3288395d1 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -592,22 +592,22 @@ local function _query_resp(self) if err ~= "again" then return true, res end - local mulitresultset = {res} - mulitresultset.mulitresultset = true + local multiresultset = {res} + multiresultset.multiresultset = true local i =2 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 + multiresultset.badresult = true + multiresultset.err = err + multiresultset.errno = errno + multiresultset.sqlstate = sqlstate + return true, multiresultset end - mulitresultset[i]=res + multiresultset[i]=res i=i+1 end - return true, mulitresultset + return true, multiresultset end end From 8a3a0c1b7108511d6550ae6f0702847671292be6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 21 Oct 2018 13:21:51 +0800 Subject: [PATCH 056/565] register logger name in framework, see #909 --- examples/userlog.lua | 2 +- service-src/service_logger.c | 1 - skynet-src/skynet_start.c | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/userlog.lua b/examples/userlog.lua index 3a8c5d225..22e2683ae 100644 --- a/examples/userlog.lua +++ b/examples/userlog.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" require "skynet.manager" +-- register protocol text before skynet.start would be better. skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, @@ -21,5 +22,4 @@ skynet.register_protocol { } skynet.start(function() - skynet.register ".logger" end) \ No newline at end of file diff --git a/service-src/service_logger.c b/service-src/service_logger.c index e73300e3a..eadf2f280 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -65,7 +65,6 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) } if (inst->handle) { skynet_callback(ctx, inst, logger_cb); - skynet_command(ctx, "REG", ".logger"); return 0; } return 1; diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 266e8ea25..258e41b4b 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -272,6 +272,8 @@ skynet_start(struct skynet_config * config) { exit(1); } + skynet_handle_namehandle(skynet_context_handle(ctx), "logger"); + bootstrap(ctx, config->bootstrap); start(config->thread); From 2ee85a91f9f263b386205eee96c6386f209a4f20 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Nov 2018 11:11:29 +0800 Subject: [PATCH 057/565] Release 1.2.0 --- HISTORY.md | 19 +++++++++++++++++++ README.md | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index fae79a750..4b7341bc6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,22 @@ +v1.2.0 (2018-11-6) +----------- +* Improve cluster support +* Improve mongodb driver +* Improve redis driver +* Improve socket concurrent write +* Improve socket channel +* Improve service gate +* Improve udp support +* Add skynet.ignoreret +* Add skynet.trace +* Add skynet.context +* Improve skynet.wait/wakeup +* Add socket.netstat +* Add socketchannel.overload +* Fix memory leak for dead service +* lua update to 5.3.5 +* jemalloc update to 5.1.0 + v1.1.0 (2017-10-31) ----------- * add socket.disconnected() diff --git a/README.md b/README.md index 2028e3ab8..9dc623b55 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.3.4 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. +Skynet now uses a modified version of lua 5.3.5 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. You can also use official Lua versions, just edit the Makefile by yourself. From 2102d8b9f02be1db98174213b8055fbfeaad21d7 Mon Sep 17 00:00:00 2001 From: Oneoeigh Date: Tue, 13 Nov 2018 16:55:56 -0800 Subject: [PATCH 058/565] Revise README.md Slight revisions of readme to make it more concise. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9dc623b55..fbc6fbcab 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## ![skynet logo](https://github.com/cloudwu/skynet/wiki/image/skynet_metro.jpg) -Skynet is a lightweight online game framework, and it can be used in many other fields. +Skynet is a lightweight online game framework which can be used in many other fields. ## Build @@ -12,7 +12,7 @@ cd skynet make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now ``` -Or you can: +Or: ``` export PLAT=linux @@ -34,7 +34,7 @@ Run these in different consoles: Skynet now uses a modified version of lua 5.3.5 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. -You can also use official Lua versions, just edit the Makefile by yourself. +Official Lua versions can also be used as long as the Makefile is edited. ## How To Use (Sorry, currently only available in Chinese) From 304272249e667c7dbc9a293e59d981692588946c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Nov 2018 11:43:23 +0800 Subject: [PATCH 059/565] fix #919 --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 4cbb69fbc..73055d44a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -127,6 +127,7 @@ local function co_create(f) local address = session_coroutine_address[co] if address then session_coroutine_id[co] = nil + session_coroutine_address[co] = nil end -- recycle co into pool From 68c3762abf14563e92d0849fe67741ba54f0d52a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jan 2019 00:08:57 +0800 Subject: [PATCH 060/565] bugfix : handshake error handle --- lualib/skynet/db/mysql.lua | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 3288395d1..8c8d600f8 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -363,35 +363,25 @@ local function _recv_field_packet(self, sock) end local function _recv_decode_packet_resp(self) - return function(sock) - -- don't return more than 2 results - return true, (_recv_packet(self,sock)) - end -end - -local function _recv_auth_resp(self) return function(sock) 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) - --return nil,err + return false, "failed to receive the result packet"..err end if typ == 'ERR' then local errno, msg, sqlstate = _parse_err_packet(packet) - error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) - --return nil, errno,msg, sqlstate + return false, strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate) end if typ == 'EOF' then - error "old pre-4.1 authentication protocol not supported" + return false, "old pre-4.1 authentication protocol not supported" end if typ ~= 'OK' then - error "bad packet type: " + return false, "bad packet type: " end - return true, true + return true, packet end end @@ -399,16 +389,8 @@ end local function _mysql_login(self,user,password,database,on_connect) 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( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) - end + local dispatch_resp = _recv_decode_packet_resp(self) + local packet = sockchannel:response( dispatch_resp ) self.protocol_ver = strbyte(packet) @@ -468,7 +450,7 @@ local function _mysql_login(self,user,password,database,on_connect) local packet_len = #req local authpacket=_compose_packet(self,req,packet_len) - sockchannel:request(authpacket,_recv_auth_resp(self)) + sockchannel:request(authpacket, dispatch_resp) if on_connect then on_connect(self) end From d71c8f8b7d255fe8d8ae61a793b89467f14258f8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jan 2019 00:12:15 +0800 Subject: [PATCH 061/565] remove new_tab --- lualib/skynet/db/mysql.lua | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 8c8d600f8..4c3c2a45d 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -21,8 +21,6 @@ local sha1= crypt.sha1 local setmetatable = setmetatable local error = error local tonumber = tonumber -local new_tab = function (narr, nrec) return {} end - local _M = { _VERSION = '0.14' } -- constants @@ -33,7 +31,7 @@ local SERVER_MORE_RESULTS_EXISTS = 8 local mt = { __index = _M } -- mysql field value type converters -local converters = new_tab(0, 8) +local converters = {} for i = 0x01, 0x05 do -- tiny, short, long, float, double @@ -46,22 +44,22 @@ converters[0xf6] = tonumber -- newdecimal local function _get_byte2(data, i) - return strunpack(" Date: Sat, 12 Jan 2019 00:25:28 +0800 Subject: [PATCH 062/565] remove mysqlaux.c --- Makefile | 1 - lualib-src/lua-mysqlaux.c | 166 ------------------------------------- lualib/skynet/db/mysql.lua | 14 +++- 3 files changed, 12 insertions(+), 169 deletions(-) delete mode 100644 lualib-src/lua-mysqlaux.c diff --git a/Makefile b/Makefile index 4f8b17a78..7ea2bd25b 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,6 @@ LUA_CLIB_SKYNET = \ lua-crypt.c lsha1.c \ lua-sharedata.c \ lua-stm.c \ - lua-mysqlaux.c \ lua-debugchannel.c \ lua-datasheet.c \ \ diff --git a/lualib-src/lua-mysqlaux.c b/lualib-src/lua-mysqlaux.c deleted file mode 100644 index 46fbc67d7..000000000 --- a/lualib-src/lua-mysqlaux.c +++ /dev/null @@ -1,166 +0,0 @@ -#define LUA_LIB - -#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} -}; - - -LUAMOD_API int luaopen_skynet_mysqlaux_c (lua_State *L) { - lua_newtable(L); - luaL_setfuncs(L, mysqlauxlib, 0); - return 1; -} - diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 4c3c2a45d..022c7f9b6 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -5,7 +5,6 @@ -- Modified by Cloud Wu (remove bit32 for lua 5.3) local socketchannel = require "skynet.socketchannel" -local mysqlaux = require "skynet.mysqlaux.c" local crypt = require "skynet.crypt" @@ -637,9 +636,20 @@ function _M.server_ver(self) return self._server_ver end +local escape_map = { + ['\0'] = "\\0", + ['\b'] = "\\b", + ['\n'] = "\\n", + ['\r'] = "\\r", + ['\t'] = "\\t", + ['\26'] = "\\Z", + ['\\'] = "\\\\", + ["'"] = "\\'", + ['"'] = '\\"', +} function _M.quote_sql_str( str) - return mysqlaux.quote_sql_str(str) + return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) end function _M.set_compact_arrays(self, value) From 0fbbd5f4d566813c6828db316c0e6279cae1a6cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jan 2019 05:58:59 +0800 Subject: [PATCH 063/565] type can be DATA --- lualib/skynet/db/mysql.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 022c7f9b6..d336e957d 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -43,22 +43,22 @@ converters[0xf6] = tonumber -- newdecimal local function _get_byte2(data, i) - return (strunpack(" Date: Wed, 16 Jan 2019 16:09:30 +0800 Subject: [PATCH 064/565] dns reInit bug recall dns.server() false ./lualib/skynet/dns.lua:340: assertion failed! --- test/testhttp.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testhttp.lua b/test/testhttp.lua index f9524930e..835752c07 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -3,7 +3,7 @@ local httpc = require "http.httpc" local dns = require "skynet.dns" local function main() - httpc.dns() -- set dns server + --httpc.dns() -- set dns server httpc.timeout = 100 -- set timeout 1 second print("GET baidu.com") local respheader = {} From 1422cae7fb75bf8bfc36848f278856f05fb3d57c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Feb 2019 15:36:55 +0800 Subject: [PATCH 065/565] Consider __pairs, see #942 --- lualib-src/sproto/lsproto.c | 90 ++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c4184d9b3..cb3320270 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -57,9 +57,20 @@ static int64_t lua_tointegerx(lua_State *L, int idx, int *isnum) { } #endif -// work around , use push & lua_gettable may be better -#define lua_geti lua_rawgeti -#define lua_seti lua_rawseti +static void +lua_geti(lua_State *L, int index, lua_Integer i) { + index = lua_absindex(L, index); + lua_pushinteger(L, i); + lua_gettable(L, index); +} + +static void +lua_seti(lua_State *L, int index, lua_Integer n) { + index = lua_absindex(L, index); + lua_pushinteger(L, n); + lua_insert(L, -2); + lua_settable(L, index); +} #endif @@ -110,13 +121,35 @@ struct encode_ud { const char * array_tag; int array_index; int deep; - int iter_index; + int iter_func; + int iter_table; + int iter_key; }; +static int +next_list(lua_State *L, struct encode_ud * self) { + // todo: check the key is equal to mainindex value + if (self->iter_func) { + lua_pushvalue(L, self->iter_func); + lua_pushvalue(L, self->iter_table); + lua_pushvalue(L, self->iter_key); + lua_call(L, 2, 2); + if (lua_isnil(L, -2)) { + lua_pop(L, 2); + return 0; + } + return 1; + } else { + lua_pushvalue(L,self->iter_key); + return lua_next(L, self->array_index); + } +} + static int encode(const struct sproto_arg *args) { struct encode_ud *self = args->ud; lua_State *L = self->L; + luaL_checkstack(L, 12, NULL); if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); if (args->index > 0) { @@ -131,29 +164,38 @@ encode(const struct sproto_arg *args) { self->array_index = 0; return SPROTO_CB_NOARRAY; } - if (!lua_istable(L, -1)) { - 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); } else { self->array_index = lua_gettop(L); } + + if (luaL_getmetafield(L, self->array_index, "__pairs")) { + lua_pushvalue(L, self->array_index); + lua_call(L, 1, 3); + int top = lua_gettop(L); + self->iter_func = top - 2; + self->iter_table = top - 1; + self->iter_key = top; + } else if (!lua_istable(L,self->array_index)) { + return luaL_error(L, ".*%s(%d) should be a table or an userdata with metamethods (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } else { + lua_pushnil(L); + self->iter_func = 0; + self->iter_table = 0; + self->iter_key = lua_gettop(L); + } } 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)) { + if (!next_list(L, self)) { // iterate end lua_pushnil(L); - lua_replace(L, self->iter_index); + lua_replace(L, self->iter_key); return SPROTO_CB_NIL; } lua_insert(L, -2); - lua_replace(L, self->iter_index); + lua_replace(L, self->iter_key); } else { lua_geti(L, self->array_index, args->index); } @@ -222,18 +264,15 @@ 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 = top; sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; - lua_pushnil(L); // prepare an iterator slot - sub.iter_index = sub.tbl_index + 1; + sub.iter_func = 0; + sub.iter_table = 0; + sub.iter_key = 0; r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); lua_settop(L, top-1); // pop the value if (r < 0) @@ -281,8 +320,6 @@ lencode(lua_State *L) { lua_pushstring(L, ""); return 1; // response nil } - luaL_checktype(L, tbl_index, LUA_TTABLE); - luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); self.L = L; self.st = st; self.tbl_index = tbl_index; @@ -293,8 +330,9 @@ lencode(lua_State *L) { self.deep = 0; lua_settop(L, tbl_index); - lua_pushnil(L); // for iterator (stack slot 3) - self.iter_index = tbl_index+1; + self.iter_func = 0; + self.iter_table = 0; + self.iter_key = 0; r = sproto_encode(st, buffer, sz, encode, &self); if (r<0) { @@ -323,6 +361,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"); + luaL_checkstack(L, 12, NULL); if (args->index != 0) { // It's array if (args->tagname != self->array_tag) { @@ -461,7 +500,6 @@ ldecode(lua_State *L) { if (!lua_istable(L, -1)) { lua_newtable(L); } - luaL_checkstack(L, ENCODE_DEEPLEVEL*3 + 8, NULL); self.L = L; self.result_index = lua_gettop(L); self.array_index = 0; From 48e5c36b5e95d8d893bb5cc3bee9cbb877dac08c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 1 Mar 2019 16:47:29 +0800 Subject: [PATCH 066/565] volatile for signal, see #950 --- 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 258e41b4b..b29910341 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -33,7 +33,7 @@ struct worker_parm { int weight; }; -static int SIG = 0; +static volatile int SIG = 0; static void handle_hup(int signal) { From 1d4308f33af66269faf11dd29ee91315aeb599ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 6 Mar 2019 00:19:51 +0800 Subject: [PATCH 067/565] Fix memory leak, See #952 --- 3rd/jemalloc | 2 +- lualib-src/lua-multicast.c | 1 + service/multicastd.lua | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 61efbda70..896ed3a8b 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 61efbda7098de6fe64c362d309824864308c36d4 +Subproject commit 896ed3a8b3f41998d4fb4d625d30ac63ef2d51fb diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 8903b36e8..4588bd10f 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -133,6 +133,7 @@ mc_remote(lua_State *L) { lua_pushlightuserdata(L, pack->data); lua_pushinteger(L, (lua_Integer)(pack->size)); skynet_free(pack); + skynet_free(ptr); return 2; } diff --git a/service/multicastd.lua b/service/multicastd.lua index bcc5ea979..a00eb3d2b 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -97,7 +97,10 @@ skynet.register_protocol { unpack = function(msg, sz) return mc.packremote(msg, sz) end, - dispatch = publish, + dispatch = function (...) + skynet.ignoreret() + publish(...) + end, } -- publish a message, if the caller is remote, forward the message to the owner node (by remote_publish) From 3f38a711ad4df2e0dfb1d30df59cd4a25bc0aadf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 14 Mar 2019 09:54:40 +0800 Subject: [PATCH 068/565] Shared K --- 3rd/lua/lapi.c | 36 ++++++++++++++++++++++++++---------- 3rd/lua/lauxlib.c | 3 +++ 3rd/lua/lfunc.c | 25 ++++++++++++++++--------- 3rd/lua/lgc.c | 2 +- 3rd/lua/lobject.h | 2 ++ 3rd/lua/lparser.c | 10 ++++++++++ 3rd/lua/lstring.c | 3 ++- 3rd/lua/lundump.c | 4 ++++ 8 files changed, 64 insertions(+), 21 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 348b149fb..3d8545091 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1017,16 +1017,32 @@ static void cloneproto (lua_State *L, Proto *f, const Proto *src) { /* copy constants and nested proto */ int i,n; 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_clonestring(L,tsvalue(s)); - setsvalue2n(L,o,str); - } else { - setobj(L,o,s); + if (src->sp->sharedk) + f->k = src->sp->k; + else { + lu_byte sharedk = 1; + 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 * sstr = tsvalue(s); + if (ttisshrstring(s)) { + TString * str = luaS_clonestring(L,sstr); + setsvalue2n(L,o,str); + if (str != sstr) + sharedk = 0; + } else { + setsvalue2n(L,o,sstr); + } + } else { + setobj(L,o,s); + } + } + if (sharedk) { + luaM_freearray(L, f->k, n); + f->k = src->sp->k; } } n = src->sp->sizep; diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index e54d33767..63387b7b4 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1152,6 +1152,8 @@ static int cache_mode(lua_State *L) { return 0; } +LUA_API void luaS_expandshr(int n); + LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { int level = cache_level(L); @@ -1171,6 +1173,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lua_pushliteral(L, "New state failed"); return LUA_ERRMEM; } + luaS_expandshr(4096); int err = luaL_loadfilex_(eL, filename, mode); if (err != LUA_OK) { size_t sz = 0; diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 1d011aa03..9c0c911f4 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -122,26 +122,33 @@ Proto *luaF_newproto (lua_State *L, SharedProto *sp) { sp->linedefined = 0; sp->lastlinedefined = 0; sp->source = NULL; + sp->k = NULL; + sp->sharedk = 0; } f->sp = sp; return f; } -static void freesharedproto (lua_State *L, SharedProto *f) { - if (f == NULL || G(L) != f->l_G) +static void freesharedproto (lua_State *L, Proto *pf) { + SharedProto *f = pf->sp; + if (f == NULL) 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); + if (G(L) == f->l_G) { + 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_freearray(L, f->k, f->sizek); + luaM_free(L, f); + } else if (pf->k != f->k) { + luaM_freearray(L, pf->k, f->sizek); + } } 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); + freesharedproto(L, f); luaM_free(L, f); } diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index eeb5401e8..a56771300 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -497,7 +497,7 @@ static int traverseproto (global_State *g, Proto *f) { f->cache = NULL; /* allow cache to be collected */ if (f->sp == NULL) return sizeof(Proto); - nk = (f->k == NULL) ? 0 : f->sp->sizek; + nk = (f->k == f->sp->k && g != f->sp->l_G) ? 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]); diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 8318005cd..b537d3923 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -404,6 +404,7 @@ typedef struct SharedProto { lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ + lu_byte sharedk; /* constants can be shared directly */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; @@ -418,6 +419,7 @@ typedef struct SharedProto { LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ TString *source; /* used for debug information */ + TValue *k; /* Shared constants */ } SharedProto; /* diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 2a5cbf1e3..9e0af8bce 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -550,6 +550,14 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { enterblock(fs, bl, 0); } +static lu_byte check_shortstring(const TValue *k, int sizek) { + int i; + for (i=0;iL; @@ -563,6 +571,8 @@ static void close_func (LexState *ls) { 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->sharedk = check_shortstring(f->k, fs->nk); + sp->k = f->k; sp->sizek = fs->nk; luaM_reallocvector(L, f->p, sp->sizep, fs->np, Proto *); sp->sizep = fs->np; diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index b7a47fae4..3bd7da697 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -397,7 +397,8 @@ internshrstr (lua_State *L, const char *str, size_t l) { LUA_API void luaS_expandshr(int n) { - ATOM_ADD(&SSM.n, n); + if (SSM.n < n) + ATOM_ADD(&SSM.n, n); } LUAI_FUNC TString * diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index bd7dd32db..860570371 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -119,7 +119,9 @@ static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->k = luaM_newvector(S->L, n, TValue); + f->sp->k = f->k; f->sp->sizek = n; + f->sp->sharedk = 1; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -139,6 +141,8 @@ static void LoadConstants (LoadState *S, Proto *f) { setivalue(o, LoadInteger(S)); break; case LUA_TSHRSTR: + f->sp->sharedk = 0; + //fall-through case LUA_TLNGSTR: setsvalue2n(S->L, o, LoadString(S)); break; From 7746193b5fc4ff855ea7b2b11cc05cf9356563ba Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 14 Mar 2019 10:36:02 +0800 Subject: [PATCH 069/565] add https client and server support by bios --- Makefile | 5 +- examples/simpleweb.lua | 58 +++++- lualib-src/ltls.c | 405 ++++++++++++++++++++++++++++++++++++++ lualib/http/httpc.lua | 74 ++++++- lualib/http/tlshelper.lua | 94 +++++++++ test/testhttp.lua | 18 +- 6 files changed, 634 insertions(+), 20 deletions(-) create mode 100644 lualib-src/ltls.c create mode 100644 lualib/http/tlshelper.lua diff --git a/Makefile b/Makefile index 7ea2bd25b..bffc43074 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ update3rd : CSERVICE = snlua logger gate harbor LUA_CLIB = skynet \ client \ - bson md5 sproto lpeg + bson md5 sproto lpeg ltls LUA_CLIB_SKYNET = \ lua-skynet.c lua-seri.c \ @@ -106,6 +106,9 @@ $(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt. $(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)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include $^ -o $@ -lssl + $(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 $@ diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 283b7e914..6c70efae2 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -6,26 +6,64 @@ local urllib = require "http.url" local table = table local string = string -local mode = ... +local mode, protocol = ... +protocol = protocol or "http" if mode == "agent" then -local function response(id, ...) - local ok, err = httpd.write_response(sockethelper.writefunc(id), ...) +local function response(id, write, ...) + local ok, err = httpd.write_response(write, ...) if not ok then -- if err == sockethelper.socket_error , that means socket closed. skynet.error(string.format("fd = %d, %s", id, err)) end end + +local SSLCTX_SERVER = nil +local function gen_interface(protocol, fd) + if protocol == "http" then + return { + init = nil, + close = nil, + read = sockethelper.readfunc(fd), + write = sockethelper.writefunc(fd), + } + elseif protocol == "https" then + local tls = require "http.tlshelper" + if not SSLCTX_SERVER then + SSLCTX_SERVER = tls.newctx() + -- gen cert and key + -- openssl req -x509 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem -out server-cert.pem + local certfile = skynet.getenv("certfile") or "./server-cert.pem" + local keyfile = skynet.getenv("keyfile") or "./server-key.pem" + print(certfile, keyfile) + SSLCTX_SERVER:set_cert(certfile, keyfile) + end + local tls_ctx = tls.newtls("server", SSLCTX_SERVER) + return { + init = tls.init_responsefunc(fd, tls_ctx), + close = tls.closefunc(tls_ctx), + read = tls.readfunc(fd, tls_ctx), + write = tls.writefunc(fd, tls_ctx), + } + else + error(string.format("Invalid protocol: %s", protocol)) + end +end + skynet.start(function() skynet.dispatch("lua", function (_,_,id) socket.start(id) + local interface = gen_interface(protocol, id) + if interface.init then + interface.init() + end -- 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) + local code, url, method, header, body = httpd.read_request(interface.read, 8192) if code then if code ~= 200 then - response(id, code) + response(id, interface.write, code) else local tmp = {} if header.host then @@ -44,7 +82,7 @@ skynet.start(function() table.insert(tmp, string.format("%s = %s",k,v)) end table.insert(tmp, "-----body----\n" .. body) - response(id, code, table.concat(tmp,"\n")) + response(id, interface.write, code, table.concat(tmp,"\n")) end else if url == sockethelper.socket_error then @@ -54,6 +92,9 @@ skynet.start(function() end end socket.close(id) + if interface.close then + interface.close() + end end) end) @@ -61,12 +102,13 @@ else skynet.start(function() local agent = {} + local protocol = "http" for i= 1, 20 do - agent[i] = skynet.newservice(SERVICE_NAME, "agent") + agent[i] = skynet.newservice(SERVICE_NAME, "agent", protocol) end local balance = 1 local id = socket.listen("0.0.0.0", 8001) - skynet.error("Listen web port 8001") + skynet.error(string.format("Listen web port 8001 protocol:%s", protocol)) 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/ltls.c b/lualib-src/ltls.c new file mode 100644 index 000000000..7494e64ab --- /dev/null +++ b/lualib-src/ltls.c @@ -0,0 +1,405 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static bool TLS_IS_INIT = false; + +struct tls_context { + SSL* ssl; + BIO* in_bio; + BIO* out_bio; + bool is_server; + bool is_close; +}; + +struct ssl_ctx { + SSL_CTX* ctx; +}; + +// static int +// _ssl_verify_peer(int ok, X509_STORE_CTX* ctx) { +// return 1; +// } + + +static void +_init_bio(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { + tls_p->ssl = SSL_new(ctx_p->ctx); + if(!tls_p->ssl) { + luaL_error(L, "SSL_new faild"); + } + + tls_p->in_bio = BIO_new(BIO_s_mem()); + if(!tls_p->in_bio) { + luaL_error(L, "new in bio faild"); + } + BIO_set_mem_eof_return(tls_p->in_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ + + tls_p->out_bio = BIO_new(BIO_s_mem()); + if(!tls_p->out_bio) { + luaL_error(L, "new out bio faild"); + } + BIO_set_mem_eof_return(tls_p->out_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ + + SSL_set_bio(tls_p->ssl, tls_p->in_bio, tls_p->out_bio); +} + + +static void +_init_client_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { + tls_p->is_server = false; + _init_bio(L, tls_p, ctx_p); + SSL_set_connect_state(tls_p->ssl); +} + +static void +_init_server_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { + tls_p->is_server = true; + _init_bio(L, tls_p, ctx_p); + SSL_set_accept_state(tls_p->ssl); +} + +static struct tls_context * +_check_context(lua_State* L, int idx) { + struct tls_context* tls_p = (struct tls_context*)lua_touserdata(L, idx); + if(!tls_p) { + luaL_error(L, "need tls context"); + } + if(tls_p->is_close) { + luaL_error(L, "context is closed"); + } + return tls_p; +} + +static struct ssl_ctx * +_check_sslctx(lua_State* L, int idx) { + struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_touserdata(L, idx); + if(!ctx_p) { + luaL_error(L, "need sslctx"); + } + return ctx_p; +} + +static int +_ltls_context_finished(lua_State* L) { + struct tls_context* tls_p = _check_context(L, 1); + int b = SSL_is_init_finished(tls_p->ssl); + lua_pushboolean(L, b); + return 1; +} + +static int +_ltls_context_close(lua_State* L) { + struct tls_context* tls_p = lua_touserdata(L, 1); + if(!tls_p->is_close) { + SSL_free(tls_p->ssl); + tls_p->ssl = NULL; + tls_p->in_bio = NULL; //in_bio and out_bio will be free when SSL_free is called + tls_p->out_bio = NULL; + tls_p->is_close = true; + } + return 0; +} + +static int +_bio_read(lua_State* L, struct tls_context* tls_p) { + char outbuff[4096]; + int all_read = 0; + int read = 0; + int pending = BIO_ctrl_pending(tls_p->out_bio); + if(pending >0) { + luaL_Buffer b; + luaL_buffinit(L, &b); + do { + read = BIO_read(tls_p->out_bio, outbuff, sizeof(outbuff)); + // printf("_bio_read:%d pending:%d\n", read, pending); + if(read > sizeof(outbuff)) { + luaL_error(L, "invalid BIO_read:%d", read); + }else if(read == -2) { + luaL_error(L, "BIO_read not implemented in the specific BIO type"); + }else if (read > 0) { + all_read += read; + luaL_addlstring(&b, (const char*)outbuff, read); + } + }while(read == sizeof(outbuff)); + if(all_read>0){ + luaL_pushresult(&b); + } + } + return all_read; +} + + +static void +_bio_write(lua_State* L, struct tls_context* tls_p, const char* s, size_t len) { + char* p = (char*)s; + size_t sz = len; + while(sz > 0) { + int written = BIO_write(tls_p->in_bio, p, sz); + if(written > sz) { + luaL_error(L, "invalid BIO_write"); + }else if(written > 0) { + p += written; + sz -= written; + }else if (written == -2) { + luaL_error(L, "BIO_write not implemented in the specific BIO type"); + } + } +} + + +static int +_ltls_context_handshake(lua_State* L) { + struct tls_context* tls_p = _check_context(L, 1); + size_t slen = 0; + const char* exchange = lua_tolstring(L, 2, &slen); + + // check handshake is finished + if(SSL_is_init_finished(tls_p->ssl)) { + luaL_error(L, "handshake is finished"); + } + + // handshake exchange + if(slen > 0 && exchange != NULL) { + _bio_write(L, tls_p, exchange, slen); + } + + // first handshake; initiated by client + if(!SSL_is_init_finished(tls_p->ssl)) { + int ret = SSL_do_handshake(tls_p->ssl); + if(ret == 1) { + return 0; + } else if (ret == -1) { + int all_read = _bio_read(L, tls_p); + if(all_read>0) { + return 1; + } + } else { + int err = SSL_get_error(tls_p->ssl, ret); + luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret); + } + } + return 0; +} + + +static int +_ltls_context_read(lua_State* L) { + struct tls_context* tls_p = _check_context(L, 1); + size_t slen = 0; + const char* encrypted_data = lua_tolstring(L, 2, &slen); + + // write encrypted data + if(slen>0 && encrypted_data) { + _bio_write(L, tls_p, encrypted_data, slen); + } + + char outbuff[4096]; + int read = 0; + luaL_Buffer b; + luaL_buffinit(L, &b); + + do { + read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); + if(read < 0) { + int err = SSL_get_error(tls_p->ssl, read); + if(err == SSL_ERROR_WANT_READ) { + break; + } + luaL_error(L, "SSL_read error:%d", err); + }else if(read > sizeof(outbuff)) { + luaL_error(L, "invalid SSL_read"); + }else if (read > 0) { + luaL_addlstring(&b, outbuff, read); + } + }while(read == sizeof(outbuff)); + luaL_pushresult(&b); + return 1; +} + + +static int +_ltls_context_write(lua_State* L) { + struct tls_context* tls_p = _check_context(L, 1); + size_t slen = 0; + char* unencrypted_data = (char*)lua_tolstring(L, 2, &slen); + + while(slen >0) { + int written = SSL_write(tls_p->ssl, unencrypted_data, slen); + if(written < 0) { + int err = SSL_get_error(tls_p->ssl, written); + luaL_error(L, "SSL_write error:%d", err); + }else if(written > slen) { + luaL_error(L, "invalid SSL_write"); + }else if(written>0) { + unencrypted_data += written; + slen -= written; + } + } + + int all_read = _bio_read(L, tls_p); + if(all_read <= 0) { + luaL_error(L, "bio_read error when _ltls_context_write"); + } + return 1; +} + + +static int +_lctx_gc(lua_State* L) { + struct ssl_ctx* ctx_p = _check_sslctx(L, 1); + if(ctx_p->ctx) { + SSL_CTX_free(ctx_p->ctx); + ctx_p->ctx = NULL; + } + return 0; +} + +static int +_lctx_cert(lua_State* L) { + struct ssl_ctx* ctx_p = _check_sslctx(L, 1); + const char* certfile = lua_tostring(L, 2); + const char* key = lua_tostring(L, 3); + if(!certfile) { + luaL_error(L, "need certfile"); + } + + if(!key) { + luaL_error(L, "need private key"); + } + + int ret = SSL_CTX_use_certificate_file(ctx_p->ctx, certfile, SSL_FILETYPE_PEM); + if(ret != 1) { + luaL_error(L, "SSL_CTX_use_certificate_file error:%d", ret); + } + + ret = SSL_CTX_use_PrivateKey_file(ctx_p->ctx, key, SSL_FILETYPE_PEM); + if(ret != 1) { + luaL_error(L, "SSL_CTX_use_PrivateKey_file error:%d", ret); + } + ret = SSL_CTX_check_private_key(ctx_p->ctx); + if(ret != 1) { + luaL_error(L, "SSL_CTX_check_private_key error:%d", ret); + } + return 0; +} + +static int +_lctx_ciphers(lua_State* L) { + struct ssl_ctx* ctx_p = _check_sslctx(L, 1); + const char* s = lua_tostring(L, 2); + if(!s) { + luaL_error(L, "need cipher list"); + } + int ret = SSL_CTX_set_tlsext_use_srtp(ctx_p->ctx, s); + if(ret != 0) { + luaL_error(L, "SSL_CTX_set_tlsext_use_srtp error:%d", ret); + } + return 0; +} + + +static int +lnew_ctx(lua_State* L) { + struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdata(L, sizeof(*ctx_p)); + ctx_p->ctx = SSL_CTX_new(SSLv23_method()); + if(!ctx_p->ctx) { + luaL_error(L, "SSL_CTX_new client faild."); + } + + if(luaL_newmetatable(L, "_TLS_SSLCTX_METATABLE_")) { + luaL_Reg l[] = { + {"set_ciphers", _lctx_ciphers}, + {"set_cert", _lctx_cert}, + {NULL, NULL}, + }; + + luaL_newlib(L, l); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, _lctx_gc); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + return 1; +} + + +static int +lnew_tls(lua_State* L) { + struct tls_context* tls_p = (struct tls_context*)lua_newuserdata(L, sizeof(*tls_p)); + tls_p->is_close = false; + const char* method = luaL_optstring(L, 1, "nil"); + struct ssl_ctx* ctx_p = _check_sslctx(L, 2); + lua_pushvalue(L, 2); + lua_setuservalue(L, -2); // set ssl_ctx associated to tls_context + + if(strcmp(method, "client") == 0) { + _init_client_context(L, tls_p, ctx_p); + }else if(strcmp(method, "server") == 0) { + _init_server_context(L, tls_p, ctx_p); + } else { + luaL_error(L, "invalid method:%s e.g[server, client]", method); + } + + if(luaL_newmetatable(L, "_TLS_CONTEXT_METATABLE_")) { + luaL_Reg l[] = { + {"close", _ltls_context_close}, + {"finished", _ltls_context_finished}, + {"handshake", _ltls_context_handshake}, + {"read", _ltls_context_read}, + {"write", _ltls_context_write}, + {NULL, NULL}, + }; + luaL_newlib(L, l); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, _ltls_context_close); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + return 1; +} + + +int +luaopen_ltls_c(lua_State* L) { + luaL_Reg l[] = { + {"newctx", lnew_ctx}, + {"newtls", lnew_tls}, + {NULL, NULL}, + }; + luaL_checkversion(L); + luaL_newlib(L, l); + return 1; +} + +void __attribute__((constructor)) ltls_init(void) { +#ifndef OPENSSL_EXTERNAL_INITIALIZATION + SSL_library_init(); + SSL_load_error_strings(); + ERR_load_BIO_strings(); + OpenSSL_add_all_algorithms(); + TLS_IS_INIT = true; +#endif +} + + +void __attribute__((destructor)) ltls_destory(void) { + if(TLS_IS_INIT) { + ERR_remove_state(0); + ENGINE_cleanup(); + CONF_modules_unload(1); + ERR_free_strings(); + EVP_cleanup(); + sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); + CRYPTO_cleanup_all_ex_data(); + } +} diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 975ccaa93..4f5880bf8 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -8,9 +8,9 @@ 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 function request(interface, method, host, url, recvheader, header, content) + local read = interface.read + local write = interface.write local header_content = "" if header then if not header.host then @@ -74,7 +74,7 @@ local function request(fd, method, host, url, recvheader, header, content) end else -- no content-length, read all - body = body .. socket.readall(fd) + body = body .. interface.readall() end end @@ -88,11 +88,60 @@ function httpc.dns(server,port) dns.server(server,port) end + +local function check_protocol(host) + local protocol = host:match("^[Hh][Tt][Tt][Pp][Ss]?://") + if protocol then + host = string.gsub(host, "^"..protocol, "") + protocol = string.lower(protocol) + if protocol == "https://" then + return "https", host + elseif protocol == "http://" then + return "http", host + else + error(string.format("Invalid protocol: %s", protocol)) + end + else + return "http", host + end +end + +local SSLCTX_CLIENT = nil +local function gen_interface(protocol, fd) + if protocol == "http" then + return { + init = nil, + close = nil, + read = socket.readfunc(fd), + write = socket.writefunc(fd), + readall = function () + return socket.readall(fd) + end, + } + elseif protocol == "https" then + local tls = require "http.tlshelper" + SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() + local tls_ctx = tls.newtls("client", SSLCTX_CLIENT) + return { + init = tls.init_requestfunc(fd, tls_ctx), + close = tls.closefunc(tls_ctx), + read = tls.readfunc(fd, tls_ctx), + write = tls.writefunc(fd, tls_ctx), + readall = tls.readallfunc(fd, tls_ctx), + } + else + error(string.format("Invalid protocol: %s", protocol)) + end +end + + function httpc.request(method, host, url, recvheader, header, content) + local protocol local timeout = httpc.timeout -- get httpc.timeout before any blocked api + protocol, host = check_protocol(host) local hostname, port = host:match"([^:]+):?(%d*)$" if port == "" then - port = 80 + port = protocol=="http" and 80 or protocol=="https" and 443 else port = tonumber(port) end @@ -101,20 +150,31 @@ function httpc.request(method, host, url, recvheader, header, content) end local fd = socket.connect(hostname, port, timeout) if not fd then - error(string.format("http connect error host:%s, port:%s, timeout:%s", hostname, port, timeout)) + error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostname, port, timeout)) return end + -- print("protocol hostname port", protocol, hostname, port) + local interface = gen_interface(protocol, fd) local finish if timeout then skynet.timeout(timeout, function() if not finish then socket.shutdown(fd) -- shutdown the socket fd, need close later. + if interface.close then + interface.close() + end end end) end - local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) + if interface.init then + interface.init() + end + local ok , statuscode, body = pcall(request, interface, method, host, url, recvheader, header, content) finish = true socket.close(fd) + if interface.close then + interface.close() + end if ok then return statuscode, body else diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua new file mode 100644 index 000000000..553acd9e8 --- /dev/null +++ b/lualib/http/tlshelper.lua @@ -0,0 +1,94 @@ +local socket = require "http.sockethelper" +local c = require "ltls.c" + +local tlshelper = {} + +function tlshelper.init_requestfunc(fd, tls_ctx) + local readfunc = socket.readfunc(fd) + local writefunc = socket.writefunc(fd) + return function () + local ds1 = tls_ctx:handshake() + writefunc(ds1) + while not tls_ctx:finished() do + local ds2 = readfunc() + local ds3 = tls_ctx:handshake(ds2) + if ds3 then + writefunc(ds3) + end + end + end +end + + +function tlshelper.init_responsefunc(fd, tls_ctx) + local readfunc = socket.readfunc(fd) + local writefunc = socket.writefunc(fd) + return function () + while not tls_ctx:finished() do + local ds1 = readfunc() + local ds2 = tls_ctx:handshake(ds1) + if ds2 then + writefunc(ds2) + end + end + local ds3 = tls_ctx:write() + writefunc(ds3) + end +end + +function tlshelper.closefunc(tls_ctx) + return function () + tls_ctx:close() + end +end + +function tlshelper.readfunc(fd, tls_ctx) + local readfunc = socket.readfunc(fd) + local read_buff = "" + return function (sz) + if not sz then + local s = "" + if #read_buff == 0 then + local ds = readfunc(sz) + s = tls_ctx:read(ds) + end + return read_buff .. s + else + while #read_buff < sz do + local ds = readfunc() + local s = tls_ctx:read(ds) + read_buff = read_buff .. s + end + local s = string.sub(read_buff, 1, sz) + read_buff = string.sub(read_buff, sz+1, #read_buff) + return s + end + end +end + +function tlshelper.writefunc(fd, tls_ctx) + local writefunc = socket.writefunc(fd) + return function (s) + local ds = tls_ctx:write(s) + return writefunc(ds) + end +end + +function tlshelper.readallfunc(fd, tls_ctx) + local readfunc = socket.readfunc(fd) + return function () + local ds = socket.readall(fd) + local s = tls_ctx:read(ds) + return s + end +end + +function tlshelper.newctx() + return c.newctx() +end + +function tlshelper.newtls(method, ssl_ctx) + return c.newtls(method, ssl_ctx) +end + +return tlshelper \ No newline at end of file diff --git a/test/testhttp.lua b/test/testhttp.lua index 835752c07..e4a4b45f7 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -2,12 +2,15 @@ local skynet = require "skynet" local httpc = require "http.httpc" local dns = require "skynet.dns" -local function main() +local function http_test(protocol) --httpc.dns() -- set dns server httpc.timeout = 100 -- set timeout 1 second print("GET baidu.com") + protocol = protocol or "http" local respheader = {} - local status, body = httpc.get("baidu.com", "/", respheader) + local host = string.format("%s://baidu.com", protocol) + print("geting... ".. host) + local status, body = httpc.get(host, "/", respheader) print("[header] =====>") for k,v in pairs(respheader) do print(k,v) @@ -16,13 +19,20 @@ local function main() 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" }) + local status, body = httpc.get(host, "/", respheader, { host = "baidu.com" }) print(status) end + +local function main() + dns.server() + http_test("http") + print("---------") + http_test("https") +end + skynet.start(function() print(pcall(main)) skynet.exit() From 000fa2be3a2fe0ac589715beb26227b1b8bc3e1c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 Mar 2019 17:56:39 +0800 Subject: [PATCH 070/565] multi cluster sender --- lualib/skynet/cluster.lua | 15 +++++-- service/clusterd.lua | 75 ++++++++------------------------- service/clusterproxy.lua | 3 +- service/clustersender.lua | 87 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 61 deletions(-) create mode 100644 service/clustersender.lua diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index dd3b9dde5..c1633d3ec 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -2,15 +2,24 @@ local skynet = require "skynet" local clusterd local cluster = {} +local sender = {} + +local function get_sender(t, node) + local c = skynet.call(clusterd, "lua", "sender", node) + t[node] = c + return c +end + +setmetatable(sender, { __index = get_sender } ) function cluster.call(node, address, ...) -- skynet.pack(...) will free by cluster.core.packrequest - return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...)) + return skynet.call(sender[node], "lua", "req", address, skynet.pack(...)) end function cluster.send(node, address, ...) -- push is the same with req, but no response - skynet.send(clusterd, "lua", "push", node, address, skynet.pack(...)) + skynet.send(sender[node], "lua", "push", address, skynet.pack(...)) end function cluster.open(port) @@ -45,7 +54,7 @@ function cluster.register(name, addr) end function cluster.query(node, name) - return skynet.call(clusterd, "lua", "req", node, 0, skynet.pack(name)) + return skynet.call(sender[node], "lua", "req", 0, skynet.pack(name)) end skynet.init(function() diff --git a/service/clusterd.lua b/service/clusterd.lua index 9a0627965..b73b07433 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" local sc = require "skynet.socketchannel" local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" @@ -6,6 +7,7 @@ local cluster = require "skynet.cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} local node_session = {} +local node_sender = {} local command = {} local config = {} local nodename = cluster.nodename() @@ -40,13 +42,20 @@ local function open_channel(t, key) local succ, err, c if address then local host, port = string.match(address, "([^:]+):(.*)$") - c = sc.channel { - host = host, - port = tonumber(port), - response = read_response, - nodelay = true, - } - succ, err = pcall(c.connect, c, true) + c = node_sender[key] + if c == nil then + c = skynet.newservice "clustersender" + if node_sender[key] then + -- double check + skynet.kill(c) + c = node_sender[key] + else + node_sender[key] = c + end + end + + succ = pcall(skynet.call, c, "lua", "changenode", host, port) + if succ then t[key] = c ct.channel = c @@ -120,56 +129,8 @@ function command.listen(source, addr, port) skynet.ret(skynet.pack(nil)) 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, 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] - - local tracetag = skynet.tracetag() - if tracetag then - if tracetag:sub(1,1) ~= "(" then - -- add nodename - local newtag = string.format("(%s-%s-%d)%s", nodename, node, session, tracetag) - skynet.tracelog(tracetag, string.format("session %s", newtag)) - tracetag = newtag - end - skynet.tracelog(tracetag, string.format("cluster %s", node)) - c:request(cluster.packtrace(tracetag)) - end - return c:request(request, session, padding) -end - -function command.req(...) - local ok, msg = pcall(send_request, ...) - if ok then - if type(msg) == "table" then - skynet.ret(cluster.concat(msg)) - else - skynet.ret(msg) - end - else - skynet.error(msg) - skynet.response()(false) - 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. +function command.sender(source, node) + skynet.ret(skynet.pack(node_channel[node])) end local proxy = {} diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index e0cc5daa6..d82ca44ca 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -22,11 +22,12 @@ skynet.forward_type( forward_map ,function() if n then address = n end + local sender = skynet.call(clusterd, "lua", "sender", node) skynet.dispatch("system", function (session, source, 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))) + skynet.ret(skynet.rawcall(sender, "lua", skynet.pack("req", address, msg, sz))) end end) end) diff --git a/service/clustersender.lua b/service/clustersender.lua new file mode 100644 index 000000000..d9fa29aba --- /dev/null +++ b/service/clustersender.lua @@ -0,0 +1,87 @@ +local skynet = require "skynet" +local sc = require "skynet.socketchannel" +local socket = require "skynet.socket" +local cluster = require "skynet.cluster.core" +local ignoreret = skynet.ignoreret + +local channel +local session = 1 + +local command = {} + +local function send_request(addr, msg, sz) + -- msg is a local pointer, cluster.packrequest will free it + local current_session = session + local request, new_session, padding = cluster.packrequest(addr, session, msg, sz) + session = new_session + + -- node_channel[node] may yield or throw error + local tracetag = skynet.tracetag() + if tracetag then + if tracetag:sub(1,1) ~= "(" then + -- add nodename + local newtag = string.format("(%s-%s-%d)%s", nodename, node, session, tracetag) + skynet.tracelog(tracetag, string.format("session %s", newtag)) + tracetag = newtag + end + skynet.tracelog(tracetag, string.format("cluster %s", node)) + channel:request(cluster.packtrace(tracetag)) + end + return channel:request(request, current_session, padding) +end + +function command.req(...) + local ok, msg = pcall(send_request, ...) + if ok then + if type(msg) == "table" then + skynet.ret(cluster.concat(msg)) + else + skynet.ret(msg) + end + else + skynet.error(msg) + skynet.response()(false) + end +end + +function command.push(addr, msg, sz) + local request, new_session, padding = cluster.packpush(addr, session, msg, sz) + if padding then -- is multi push + session = new_session + end + + -- node_channel[node] may yield or throw error + channel:request(request, nil, padding) +end + +local function read_response(sock) + local sz = socket.header(sock:read(2)) + local msg = sock:read(sz) + return cluster.unpackresponse(msg) -- session, ok, data, padding +end + +function command.changenode(host, port) + local c = sc.channel { + host = host, + port = tonumber(port), + response = read_response, + nodelay = true, + } + succ, err = pcall(c.connect, c, true) + if succ then + channel = c + skynet.ret(skynet.pack(nil)) + else + skynet.response()(false) + end +end + +skynet.start(function() + skynet.dispatch("lua", function(session , source, cmd, ...) + local f = assert(command[cmd]) + f(...) + end) +end) + + + From 9ffdf2dbde20934a74b0d5959249c2aaaa14c863 Mon Sep 17 00:00:00 2001 From: Dalton Date: Wed, 20 Mar 2019 18:02:07 +0800 Subject: [PATCH 071/565] Update skynet_daemon.c --- skynet-src/skynet_daemon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c index 902086e3d..8cb7faae7 100644 --- a/skynet-src/skynet_daemon.c +++ b/skynet-src/skynet_daemon.c @@ -37,7 +37,7 @@ write_pid(const char *pidfile) { fprintf(stderr, "Can't create pidfile [%s].\n", pidfile); return 0; } - f = fdopen(fd, "r+"); + f = fdopen(fd, "w+"); if (f == NULL) { fprintf(stderr, "Can't open pidfile [%s].\n", pidfile); return 0; From 1c3b563ef8133816752ee45499ab0b645500c357 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 Mar 2019 19:44:08 +0800 Subject: [PATCH 072/565] add wait queue --- service/clustersender.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/service/clustersender.lua b/service/clustersender.lua index d9fa29aba..250d70b24 100644 --- a/service/clustersender.lua +++ b/service/clustersender.lua @@ -8,6 +8,7 @@ local channel local session = 1 local command = {} +local waiting = {} local function send_request(addr, msg, sz) -- msg is a local pointer, cluster.packrequest will free it @@ -30,7 +31,16 @@ local function send_request(addr, msg, sz) return channel:request(request, current_session, padding) end +local function wait() + local co = coroutine.running() + table.insert(waiting, co) + skynet.wait(co) +end + function command.req(...) + if channel == nil then + wait() + end local ok, msg = pcall(send_request, ...) if ok then if type(msg) == "table" then @@ -45,6 +55,9 @@ function command.req(...) end function command.push(addr, msg, sz) + if channel == nil then + wait() + end local request, new_session, padding = cluster.packpush(addr, session, msg, sz) if padding then -- is multi push session = new_session @@ -68,10 +81,18 @@ function command.changenode(host, port) nodelay = true, } succ, err = pcall(c.connect, c, true) + if channel then + channel:close() + end if succ then channel = c + for k, co in ipairs(waiting) do + waiting[k] = nil + skynet.wakeup(co) + end skynet.ret(skynet.pack(nil)) else + channel = nil -- reset channel skynet.response()(false) end end From ea1affda4c7e304ed24270204325f2523e1eb083 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Mar 2019 09:42:53 +0800 Subject: [PATCH 073/565] fix #962 --- service/clusterd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 9a0627965..a6b75cf2d 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -235,7 +235,7 @@ function command.socket(source, subcmd, fd, msg) local agent = cluster_agent[fd] if type(agent) == "boolean" then cluster_agent[fd] = true - else + elseif agent then skynet.send(agent, "lua", "exit") cluster_agent[fd] = nil end From bfbbe91efa1e8d9c02a71074b9f38e61dc831839 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Mar 2019 10:31:47 +0800 Subject: [PATCH 074/565] turn off ltls by default --- Makefile | 12 +++++++++--- lualib-src/ltls.c | 1 - test/testhttp.lua | 8 ++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index bffc43074..f1daa3a2b 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,13 @@ LUA_INC ?= 3rd/lua $(LUA_STATICLIB) : cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) -# jemalloc +# https : turn on TLS_MODULE to add https support + +# TLS_MODULE=ltls +TLS_LIB= +TLS_INC= + +# jemalloc JEMALLOC_STATICLIB := 3rd/jemalloc/lib/libjemalloc_pic.a JEMALLOC_INC := 3rd/jemalloc/include/jemalloc @@ -47,7 +53,7 @@ update3rd : CSERVICE = snlua logger gate harbor LUA_CLIB = skynet \ client \ - bson md5 sproto lpeg ltls + bson md5 sproto lpeg $(TLS_MODULE) LUA_CLIB_SKYNET = \ lua-skynet.c lua-seri.c \ @@ -107,7 +113,7 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ $(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include $^ -o $@ -lssl + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $(TLS_LIB) $(TLS_INC) $^ -o $@ -lssl $(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 $@ diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 7494e64ab..edf74f41d 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -394,7 +394,6 @@ void __attribute__((constructor)) ltls_init(void) { void __attribute__((destructor)) ltls_destory(void) { if(TLS_IS_INIT) { - ERR_remove_state(0); ENGINE_cleanup(); CONF_modules_unload(1); ERR_free_strings(); diff --git a/test/testhttp.lua b/test/testhttp.lua index e4a4b45f7..f8019cad7 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -29,8 +29,12 @@ end local function main() dns.server() http_test("http") - print("---------") - http_test("https") + + if not pcall(require,"ltls") then + print "No ltls module, https is not supported" + else + http_test("https") + end end skynet.start(function() From 45afa0fdcf5f23026ca425eb31c09b436940bfa0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Mar 2019 18:46:31 +0800 Subject: [PATCH 075/565] fix #965 --- test/testhttp.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testhttp.lua b/test/testhttp.lua index f8019cad7..13c4d3db3 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -30,7 +30,7 @@ local function main() dns.server() http_test("http") - if not pcall(require,"ltls") then + if not pcall(require,"ltls.c") then print "No ltls module, https is not supported" else http_test("https") From 0c66252cd1960be1722ed8a65e4180ff029d756b Mon Sep 17 00:00:00 2001 From: zixun Date: Tue, 26 Mar 2019 20:43:35 +0800 Subject: [PATCH 076/565] fix post with https --- lualib-src/ltls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index edf74f41d..74b3a3bb3 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -248,7 +248,7 @@ _ltls_context_write(lua_State* L) { int all_read = _bio_read(L, tls_p); if(all_read <= 0) { - luaL_error(L, "bio_read error when _ltls_context_write"); + lua_pushstring(L, ""); } return 1; } From 062f767bbf1dafc71b2d223be2c962e83d95bfbb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Mar 2019 23:45:33 +0800 Subject: [PATCH 077/565] fix #967 --- service/clusterproxy.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index d82ca44ca..d435e3add 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -25,7 +25,7 @@ skynet.forward_type( forward_map ,function() local sender = skynet.call(clusterd, "lua", "sender", node) skynet.dispatch("system", function (session, source, msg, sz) if session == 0 then - skynet.send(clusterd, "lua", "push", node, address, msg, sz) + skynet.send(sender, "lua", "push", address, msg, sz) else skynet.ret(skynet.rawcall(sender, "lua", skynet.pack("req", address, msg, sz))) end From 1af78bc585190f2d2ed1da0f0c4c3da3c5f8c29c Mon Sep 17 00:00:00 2001 From: CoolDesert Date: Wed, 27 Mar 2019 10:38:09 +0800 Subject: [PATCH 078/565] fix tls make --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f1daa3a2b..f3f69f812 100644 --- a/Makefile +++ b/Makefile @@ -113,7 +113,7 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ $(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $(TLS_LIB) $(TLS_INC) $^ -o $@ -lssl + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -I$(TLS_LIB) -L$(TLS_INC) $^ -o $@ -lssl $(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 $@ From 2651929f5b620432cb4e2b343978a3938aee1831 Mon Sep 17 00:00:00 2001 From: CoolDesert Date: Wed, 27 Mar 2019 10:47:00 +0800 Subject: [PATCH 079/565] fix tls make --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f3f69f812..127d75526 100644 --- a/Makefile +++ b/Makefile @@ -113,7 +113,7 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ $(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -I$(TLS_LIB) -L$(TLS_INC) $^ -o $@ -lssl + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L$(TLS_LIB) -I$(TLS_INC) $^ -o $@ -lssl $(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 $@ From 305a29d8859faab7d2d554af6f2f82e2a828bcb1 Mon Sep 17 00:00:00 2001 From: "drew.zxj" Date: Wed, 27 Mar 2019 16:12:16 +0800 Subject: [PATCH 080/565] dns: parse /etc/hosts --- lualib/skynet/dns.lua | 144 ++++++++++++++++++++++++++++++++++++------ test/testdns.lua | 1 - 2 files changed, 126 insertions(+), 19 deletions(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index c280f06a1..08f3c0f52 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -88,6 +88,79 @@ local CACHE = {} local dns = {} local request_pool = {} +local local_hosts -- local static table lookup for hostnames + +dns.DEFAULT_HOSTS = "/etc/hosts" +dns.DEFAULT_RESOLV_CONF = "/etc/resolv.conf" + +-- return name type: 'ipv4', 'ipv6', or 'hostname' +local function guess_name_type(name) + if name:match("^[%d%.]+$") then + return "ipv4" + end + + if name:find(":") then + return "ipv6" + end + + return "hostname" +end + +-- http://man7.org/linux/man-pages/man5/hosts.5.html +local function parse_hosts() + if not dns.DEFAULT_HOSTS then + return + end + + local f = io.open(dns.DEFAULT_HOSTS) + if not f then + return + end + + local rts = {} + for line in f:lines() do + local ip, hosts = string.match(line, "^%s*([%[%]%x%.%:]+)%s+([^#;]*)") + local family = guess_name_type(ip) + if hosts and family ~= "hostname" then + for host in hosts:gmatch("%S+") do + host = host:lower() + local rt = rts[host] + if not rt then + rt = {} + rts[host] = rt + end + + if not rt[family] then + rt[family] = {} + end + table.insert(rt[family], ip) + end + end + end + return rts +end + +-- http://man7.org/linux/man-pages/man5/resolv.conf.5.html +local function parse_resolv_conf() + if not dns.DEFAULT_RESOLV_CONF then + return + end + + local f = io.open(dns.DEFAULT_RESOLV_CONF) + if not f then + return + end + + local server + for line in f:lines() do + server = line:match("%s*nameserver%s+([^#;%s]+)") + if server then + break + end + end + f:close() + return server +end function dns.flush() CACHE[QTYPE.A] = setmetatable({},weak) @@ -290,6 +363,14 @@ local function connect_server() local fd = socket.udp(function(str, from) resolve(str) end) + + if not dns_server.address then + dns_server.address = parse_resolv_conf() + dns_server.port = 53 + end + + assert(dns_server.address, "Call dns.server first") + local ok, err = pcall(socket.udp_connect,fd, dns_server.address, dns_server.port) if not ok then socket.close(fd) @@ -326,22 +407,8 @@ local function touch_server() 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 - f:close() - assert(server, "Can't get nameserver") - end - assert(dns_server.fd == nil) -- only set dns.server once dns_server.address = server dns_server.port = port or 53 - touch_server() - return dns_server.address end local function lookup_cache(name, qtype, ignorettl) @@ -383,10 +450,30 @@ local function suspend(tid, name, qtype) return req.answers[1], req.answers end -function dns.resolve(name, ipv6) +-- lookup local static table +local function local_resolve(name, ipv6) + if not local_hosts then + local_hosts = parse_hosts() + end + + if not local_hosts then + return + end + + local family = ipv6 and "ipv6" or "ipv4" + local t = local_hosts[name] + if t then + local answers = t[family] + if answers then + return answers[1], answers + end + end + return nil +end + +-- lookup dns server +local function remote_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 @@ -397,10 +484,31 @@ function dns.resolve(name, ipv6) qdcount = 1, } local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN) - assert(dns_server.address, "Call dns.server first") touch_server() socket.write(dns_server.fd, req) return suspend(question_header.tid, name, qtype) end +function dns.resolve(name, ipv6) + local name = name:lower() + local ntype = guess_name_type(name) + if ntype ~= "hostname" then + if (ipv6 and name == "ipv4") or (not ipv6 and name == "ipv6") then + return nil, "illegal ip address" + end + return name + end + + if not verify_domain_name(name) then + return nil, "illegal name" + end + + local answer, answers = local_resolve(name, ipv6) + if answer then + return answer, answers + end + + return remote_resolve(name, ipv6, timeout) +end + return dns diff --git a/test/testdns.lua b/test/testdns.lua index 0754f2ad2..fdec4056d 100644 --- a/test/testdns.lua +++ b/test/testdns.lua @@ -8,7 +8,6 @@ local resolve_list = { } skynet.start(function() - print("nameserver:", dns.server()) -- set nameserver -- you can specify the server like dns.server("8.8.4.4", 53) for _ , name in ipairs(resolve_list) do local ip, ips = dns.resolve(name) From ee1f5055eae87391cff650d260f9ea8aaded567c Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 28 Mar 2019 14:26:23 +0800 Subject: [PATCH 081/565] bugfix: dns parse hosts.conf --- lualib/skynet/dns.lua | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index 08f3c0f52..ae75cef2c 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -119,23 +119,31 @@ local function parse_hosts() local rts = {} for line in f:lines() do - local ip, hosts = string.match(line, "^%s*([%[%]%x%.%:]+)%s+([^#;]*)") + local ip, hosts = string.match(line, "^%s*([%[%]%x%.%:]+)%s+([^#;]+)") + if not ip or not hosts then + goto continue + end + local family = guess_name_type(ip) - if hosts and family ~= "hostname" then - for host in hosts:gmatch("%S+") do - host = host:lower() - local rt = rts[host] - if not rt then - rt = {} - rts[host] = rt - end - - if not rt[family] then - rt[family] = {} - end - table.insert(rt[family], ip) + if family == "hostname" then + goto continue + end + + for host in hosts:gmatch("%S+") do + host = host:lower() + local rt = rts[host] + if not rt then + rt = {} + rts[host] = rt + end + + if not rt[family] then + rt[family] = {} end + table.insert(rt[family], ip) end + + ::continue:: end return rts end From ea0cc1fadecaa538dabaae45ec31427fabffb827 Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 28 Mar 2019 16:53:46 +0800 Subject: [PATCH 082/565] fix check header limit --- lualib/http/internal.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 5bb357523..6f76b86dc 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -48,9 +48,6 @@ function M.recvheader(readbytes, lines, header) 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) @@ -59,6 +56,9 @@ function M.recvheader(readbytes, lines, header) if header:find "^\r\n" then return header:sub(3) end + if #header > LIMIT then + return + end end end for v in header:gmatch("(.-)\r\n") do From 3c6c0f7aa413499698fb4af7399ef04b1d884cce Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 28 Mar 2019 19:58:51 +0800 Subject: [PATCH 083/565] bugfix #976 long string instead of global seed --- 3rd/lua/lstring.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 3bd7da697..6aea4509d 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -11,7 +11,7 @@ #include - +#include #include "lua.h" #include "ldebug.h" @@ -143,9 +143,19 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { return ts; } +static unsigned int LNGSTR_SEED; +static unsigned int make_lstr_seed() { + size_t buff[4]; + unsigned int h = time(NULL); + buff[0] = cast(size_t, h); + buff[1] = cast(size_t, &LNGSTR_SEED); + buff[2] = cast(size_t, &make_lstr_seed); + buff[3] = cast(size_t, &luaS_createlngstrobj); + return luaS_hash((const char*)buff, sizeof(buff), h); +} TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); + TString *ts = createstrobj(L, l, LUA_TLNGSTR, LNGSTR_SEED); ts->u.lnglen = l; return ts; } @@ -284,6 +294,7 @@ luaS_initshr() { for (i=0;ih[i].lock); } + LNGSTR_SEED = make_lstr_seed(); } LUA_API void From 2011596de04ebc9a67a861420d16a9f027339847 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 17:43:24 +0800 Subject: [PATCH 084/565] fix #979 --- service/clusterd.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 59e2ae967..cc82005a7 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -143,10 +143,18 @@ function command.proxy(source, node, name) end end local fullname = node .. "." .. name - if proxy[fullname] == nil then - proxy[fullname] = skynet.newservice("clusterproxy", node, name) + local p = proxy[fullname] + if p == nil then + p = skynet.newservice("clusterproxy", node, name) + -- double check + if proxy[fullname] then + skynet.kill(p) + p = proxy[fullname] + else + proxy[fullname] = p + end end - skynet.ret(skynet.pack(proxy[fullname])) + skynet.ret(skynet.pack(p)) end local cluster_agent = {} -- fd:service From 5bf635c75c4d36650ff70550b75bd368976ee12c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 14:46:00 +0800 Subject: [PATCH 085/565] SSM expanding --- 3rd/lua/lstring.c | 304 ++++++++++++++++++++++++++++---------- 3rd/lua/lstring.h | 2 +- service/debug_console.lua | 4 +- 3 files changed, 231 insertions(+), 79 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 6aea4509d..7a05f97d4 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -271,9 +271,8 @@ Udata *luaS_newudata (lua_State *L, size_t s) { #include "atomic.h" #include -#define SHRSTR_SLOT 0x10000 -#define HASH_NODE(h) ((h) % SHRSTR_SLOT) #define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) +#define SHRSTR_INITSIZE 0x10000 struct shrmap_slot { struct rwlock lock; @@ -281,63 +280,180 @@ struct shrmap_slot { }; struct shrmap { - struct shrmap_slot h[SHRSTR_SLOT]; - int n; + struct rwlock lock; + unsigned int n; + unsigned int mask; + unsigned int total; + unsigned int roslots; + struct shrmap_slot * readwrite; + struct shrmap_slot * readonly; }; static struct shrmap SSM; -LUA_API void -luaS_initshr() { - struct shrmap * s = &SSM; - int i; - for (i=0;ih[i].lock); +static struct shrmap_slot * +shrstr_newpage(unsigned int sz) { + unsigned int i; + struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s)); + for (i=0;iu.hnext; + free(str); + str = next; + } + } + free(s); + } +} + +static int +shrstr_allocpage(struct shrmap * s, unsigned int osz, unsigned int sz, struct shrmap_slot * newpage) { + if (s->readonly != NULL) + return 0; + if ((s->mask + 1) != osz) + return 0; + s->readonly = s->readwrite; + s->readwrite = newpage; + s->roslots = s->mask + 1; + s->mask = sz - 1; + + return 1; +} + +static void +shrstr_rehash(struct shrmap *s, unsigned int slotid) { + struct shrmap_slot *slot = &s->readonly[slotid]; + rwlock_wlock(&slot->lock); + TString *str = slot->str; while (str) { TString * next = str->u.hnext; - free(str); + unsigned int newslotid = str->hash & s->mask; + struct shrmap_slot *newslot = &s->readwrite[newslotid]; + rwlock_wlock(&newslot->lock); + str->u.hnext = newslot->str; + newslot->str = str; + rwlock_wunlock(&newslot->lock); str = next; } + + slot->str = NULL; + rwlock_wunlock(&slot->lock); +} + +/* + 1. writelock SSM if readonly == NULL, (Only one thread can expand) + 2. move old page (readwrite) to readonly + 3. new (empty) page with double size to readwrite + 4. unlock SSM + 5. rehash every slots + 6. remove temporary readonly (writelock SSM) + */ +static void +shrstr_expandpage(unsigned int cap) { + struct shrmap * s = &SSM; + if (s->readonly) + return; + unsigned int osz = s->mask + 1; + unsigned int sz = osz * 2; + while (sz < cap) + sz = sz * 2; + struct shrmap_slot * newpage = shrstr_newpage(sz); + rwlock_wlock(&s->lock); + int succ = shrstr_allocpage(s, osz, sz, newpage); + rwlock_wunlock(&s->lock); + if (!succ) { + shrstr_deletepage(newpage, sz); + return; } + unsigned int i; + for (i=0;ilock); + struct shrmap_slot * oldpage = s->readonly; + s->readonly = NULL; + rwlock_wunlock(&s->lock); + shrstr_deletepage(oldpage, osz); +} + +LUA_API void +luaS_initshr() { + struct shrmap * s = &SSM; + rwlock_init(&s->lock); + s->n = 0; + s->mask = SHRSTR_INITSIZE - 1; + s->readwrite = shrstr_newpage(SHRSTR_INITSIZE); + s->readonly = NULL; + LNGSTR_SEED = make_lstr_seed(); +} + +LUA_API void +luaS_exitshr() { + struct shrmap * s = &SSM; + rwlock_wlock(&s->lock); + unsigned int sz = s->mask + 1; + shrstr_deletepage(s->readwrite, sz); + shrstr_deletepage(s->readonly, s->roslots); + s->readwrite = NULL; + s->readonly = NULL; } 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; +find_string(TString *t, struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { + TString *ts = slot->str; + if (t) { + while (ts) { + if (ts == t) + break; + ts = ts->u.hnext; + } + } else { + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + break; + } + ts = ts->u.hnext; } - ts = ts->u.hnext; } - rwlock_runlock(&s->lock); return ts; } +/* + 1. readlock SSM + 2. find string in readwrite page + 3. find string in readonly (if exist, during exapnding) + 4. unlock SSM + */ static TString * -query_ptr(TString *t) { - unsigned int h = t->hash; - struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; +query_string(TString *t, unsigned int h, const char *str, lu_byte l) { + struct shrmap * s = &SSM; + TString *ts = NULL; rwlock_rlock(&s->lock); - TString *ts = s->str; - while (ts) { - if (ts == t) - break; - ts = ts->u.hnext; - } + struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + rwlock_rlock(&slot->lock); + ts = find_string(t, slot, h, str, l); + rwlock_runlock(&slot->lock); + if (ts == NULL && s->readonly != NULL) { + unsigned int mask = s->roslots - 1; + slot = &s->readonly[h & mask]; + rwlock_rlock(&slot->lock); + ts = find_string(t, slot, h, str, l); + rwlock_runlock(&slot->lock); + } rwlock_runlock(&s->lock); return ts; } @@ -354,31 +470,47 @@ new_string(unsigned int h, const char *str, lu_byte l) { return ts; } +static TString * +shrstr_exist(struct shrmap * s, unsigned int h, const char *str, lu_byte l) { + TString *found; + if (s->readonly) { + unsigned int mask = s->roslots - 1; + struct shrmap_slot *slot = &s->readonly[h & mask]; + rwlock_rlock(&slot->lock); + found = find_string(NULL, slot, h, str, l); + rwlock_runlock(&slot->lock); + if (found) + return found; + } + struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + rwlock_wlock(&slot->lock); + found = find_string(NULL, slot, h, str, l); + if (found) { + rwlock_wunlock(&slot->lock); + return found; + } + // not found, lock slot and return. + return NULL; +} + static TString * add_string(unsigned int h, const char *str, lu_byte l) { + struct shrmap * s = &SSM; 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; + rwlock_rlock(&s->lock); + struct TString *ts = shrstr_exist(s, h, str, l); + if (ts) { + // string is create by other thread, so free tmp + free(tmp); + } else { + struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + ts = tmp; + ts->u.hnext = slot->str; + slot->str = ts; + rwlock_wunlock(&slot->lock); + ATOM_INC(&SSM.total); } - 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); - } + rwlock_runlock(&s->lock); return ts; } @@ -394,7 +526,7 @@ internshrstr (lua_State *L, const char *str, size_t l) { return ts; // lookup SSM again h0 = luaS_hash(str, l, 0); - ts = query_string(h0, str, l); + ts = query_string(NULL, h0, str, l); if (ts) return ts; // If SSM.n greate than 0, add it to SSM @@ -407,9 +539,15 @@ internshrstr (lua_State *L, const char *str, size_t l) { } LUA_API void -luaS_expandshr(int n) { - if (SSM.n < n) - ATOM_ADD(&SSM.n, n); +luaS_expandshr(unsigned int n) { + struct shrmap * s = &SSM; + if (s->n < n) { + ATOM_ADD(&s->n, n); + unsigned int t = (s->total + s->n) * 5 / 4; + if (t > s->mask) { + shrstr_expandpage(t); + } + } } LUAI_FUNC TString * @@ -428,11 +566,11 @@ luaS_clonestring(lua_State *L, TString *ts) { if (result) return result; // look up SSM by ptr - result = query_ptr(ts); + result = query_string(ts, 0, NULL, 0); if (result) return result; h = luaS_hash(str, l, 0); - result = query_string(h, str, l); + result = query_string(NULL, h, str, l); if (result) return result; // ts is not in SSM, so recalc hash, and add it to SSM @@ -462,20 +600,34 @@ luaS_shrinfo(lua_State *L) { struct slotinfo total; struct slotinfo tmp; memset(&total, 0, sizeof(total)); - int i; - int len = 0; - for (i=0;i total.len) { - total.len = tmp.len; + struct shrmap * s = &SSM; + rwlock_rlock(&s->lock); + unsigned int i; + unsigned int sz = s->mask + 1; + for (i=0;ireadwrite[i]; + getslot(slot, &tmp); + if (tmp.len > total.len) { + total.len = tmp.len; + } + total.size += tmp.size; } - total.size += tmp.size; - } - lua_pushinteger(L, len); + if (s->readonly) { + sz = s->roslots; + for (i=0;ireadonly[i]; + getslot(slot, &tmp); + if (tmp.len > total.len) { + total.len = tmp.len; + } + total.size += tmp.size; + } + } + rwlock_runlock(&s->lock); + lua_pushinteger(L, SSM.total); lua_pushinteger(L, total.size); lua_pushinteger(L, total.len); lua_pushinteger(L, SSM.n); - return 4; + lua_pushinteger(L, sz); + return 5; } diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index e6a38b885..5947bfcfc 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -49,7 +49,7 @@ LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); LUA_API void luaS_initshr(); LUA_API void luaS_exitshr(); -LUA_API void luaS_expandshr(int n); +LUA_API void luaS_expandshr(unsigned int n); LUAI_FUNC TString *luaS_clonestring(lua_State *L, TString *); LUA_API int luaS_shrinfo(lua_State *L); diff --git a/service/debug_console.lua b/service/debug_console.lua index dbde18ea8..e478b99b2 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -336,8 +336,8 @@ function COMMAND.cmem() end function COMMAND.shrtbl() - local n, total, longest, space = memory.ssinfo() - return { n = n, total = total, longest = longest, space = space } + local n, total, longest, space, slots = memory.ssinfo() + return { n = n, total = total, longest = longest, space = space, slots = slots } end function COMMAND.ping(address) From 64e0c4daffe3ec4de8e19af0c9dd1e5c36bb315c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 17:28:23 +0800 Subject: [PATCH 086/565] calc variance --- 3rd/lua/lstring.c | 56 ++++++++++++++++++++++++++++++--------- service/debug_console.lua | 4 +-- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 7a05f97d4..92bc6778f 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -595,39 +595,71 @@ getslot(struct shrmap_slot *s, struct slotinfo *info) { rwlock_runlock(&s->lock); } + +struct variance { + int count; + double mean; + double m2; +}; + +static void +variance_update(struct variance *v, int newValue_) { + double newValue = (double)newValue_; + ++v->count; + double delta = newValue - v->mean; + v->mean += delta / v->count; + double delta2 = newValue - v->mean; + v->m2 += delta * delta2; +} + LUA_API int luaS_shrinfo(lua_State *L) { struct slotinfo total; struct slotinfo tmp; memset(&total, 0, sizeof(total)); struct shrmap * s = &SSM; + struct variance v = { 0,0,0 }; + unsigned int slots = 0; rwlock_rlock(&s->lock); unsigned int i; unsigned int sz = s->mask + 1; for (i=0;ireadwrite[i]; getslot(slot, &tmp); - if (tmp.len > total.len) { - total.len = tmp.len; + if (tmp.len > 0) { + if (tmp.len > total.len) { + total.len = tmp.len; + } + total.size += tmp.size; + variance_update(&v, tmp.len); + ++slots; } - total.size += tmp.size; } if (s->readonly) { sz = s->roslots; for (i=0;ireadonly[i]; getslot(slot, &tmp); - if (tmp.len > total.len) { - total.len = tmp.len; + if (tmp.len > 0) { + if (tmp.len > total.len) { + total.len = tmp.len; + } + total.size += tmp.size; + variance_update(&v, tmp.len); } - total.size += tmp.size; } } rwlock_runlock(&s->lock); - lua_pushinteger(L, SSM.total); - lua_pushinteger(L, total.size); - lua_pushinteger(L, total.len); - lua_pushinteger(L, SSM.n); - lua_pushinteger(L, sz); - return 5; + lua_pushinteger(L, SSM.total); // count + lua_pushinteger(L, total.size); // total size + lua_pushinteger(L, total.len); // longest + lua_pushinteger(L, SSM.n); // space + lua_pushinteger(L, slots); // slots + lua_pushnumber(L, v.mean); // average + if (v.count > 1) { + lua_pushnumber(L, v.m2 / v.count); // variance + } else { + lua_pushnumber(L, 0); // variance + } + return 7; } diff --git a/service/debug_console.lua b/service/debug_console.lua index e478b99b2..0335f250f 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -336,8 +336,8 @@ function COMMAND.cmem() end function COMMAND.shrtbl() - local n, total, longest, space, slots = memory.ssinfo() - return { n = n, total = total, longest = longest, space = space, slots = slots } + local n, total, longest, space, slots, avg, variance = memory.ssinfo() + return { n = n, total = total, longest = longest, space = space, slots = slots, average = avg, variace = variance } end function COMMAND.ping(address) From 56d24f46d6ff2512fb0cf830676ba13bb4a8d5ac Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 17:58:28 +0800 Subject: [PATCH 087/565] overflow check --- 3rd/lua/lstring.c | 6 ++++-- service/debug_console.lua | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 92bc6778f..c38e2ccb9 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -367,6 +367,9 @@ shrstr_expandpage(unsigned int cap) { return; unsigned int osz = s->mask + 1; unsigned int sz = osz * 2; + // overflow check + if (sz == 0) + return; while (sz < cap) sz = sz * 2; struct shrmap_slot * newpage = shrstr_newpage(sz); @@ -655,11 +658,10 @@ luaS_shrinfo(lua_State *L) { lua_pushinteger(L, total.len); // longest lua_pushinteger(L, SSM.n); // space lua_pushinteger(L, slots); // slots - lua_pushnumber(L, v.mean); // average if (v.count > 1) { lua_pushnumber(L, v.m2 / v.count); // variance } else { lua_pushnumber(L, 0); // variance } - return 7; + return 6; } diff --git a/service/debug_console.lua b/service/debug_console.lua index 0335f250f..daa2e688e 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -336,8 +336,8 @@ function COMMAND.cmem() end function COMMAND.shrtbl() - local n, total, longest, space, slots, avg, variance = memory.ssinfo() - return { n = n, total = total, longest = longest, space = space, slots = slots, average = avg, variace = variance } + local n, total, longest, space, slots, variance = memory.ssinfo() + return { n = n, total = total, longest = longest, space = space, slots = slots, average = n / slots, variace = variance } end function COMMAND.ping(address) From f8d55affbae97262d0b169f5c8d2dce3a85f2b08 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 19:32:44 +0800 Subject: [PATCH 088/565] use sign n for SSM --- 3rd/lua/lstring.c | 46 +++++++++++++++++++++++----------------------- 3rd/lua/lstring.h | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index c38e2ccb9..68de011d4 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -281,10 +281,10 @@ struct shrmap_slot { struct shrmap { struct rwlock lock; - unsigned int n; - unsigned int mask; - unsigned int total; - unsigned int roslots; + int n; + int mask; + int total; + int roslots; struct shrmap_slot * readwrite; struct shrmap_slot * readonly; }; @@ -292,8 +292,8 @@ struct shrmap { static struct shrmap SSM; static struct shrmap_slot * -shrstr_newpage(unsigned int sz) { - unsigned int i; +shrstr_newpage(int sz) { + int i; struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s)); for (i=0;ireadonly != NULL) return 0; if ((s->mask + 1) != osz) @@ -333,13 +333,13 @@ shrstr_allocpage(struct shrmap * s, unsigned int osz, unsigned int sz, struct sh } static void -shrstr_rehash(struct shrmap *s, unsigned int slotid) { +shrstr_rehash(struct shrmap *s, int slotid) { struct shrmap_slot *slot = &s->readonly[slotid]; rwlock_wlock(&slot->lock); TString *str = slot->str; while (str) { TString * next = str->u.hnext; - unsigned int newslotid = str->hash & s->mask; + int newslotid = str->hash & s->mask; struct shrmap_slot *newslot = &s->readwrite[newslotid]; rwlock_wlock(&newslot->lock); str->u.hnext = newslot->str; @@ -361,14 +361,14 @@ shrstr_rehash(struct shrmap *s, unsigned int slotid) { 6. remove temporary readonly (writelock SSM) */ static void -shrstr_expandpage(unsigned int cap) { +shrstr_expandpage(int cap) { struct shrmap * s = &SSM; if (s->readonly) return; - unsigned int osz = s->mask + 1; - unsigned int sz = osz * 2; + int osz = s->mask + 1; + int sz = osz * 2; // overflow check - if (sz == 0) + if (sz <= 0) return; while (sz < cap) sz = sz * 2; @@ -380,7 +380,7 @@ shrstr_expandpage(unsigned int cap) { shrstr_deletepage(newpage, sz); return; } - unsigned int i; + int i; for (i=0;ilock); - unsigned int sz = s->mask + 1; + int sz = s->mask + 1; shrstr_deletepage(s->readwrite, sz); shrstr_deletepage(s->readonly, s->roslots); s->readwrite = NULL; @@ -451,7 +451,7 @@ query_string(TString *t, unsigned int h, const char *str, lu_byte l) { ts = find_string(t, slot, h, str, l); rwlock_runlock(&slot->lock); if (ts == NULL && s->readonly != NULL) { - unsigned int mask = s->roslots - 1; + int mask = s->roslots - 1; slot = &s->readonly[h & mask]; rwlock_rlock(&slot->lock); ts = find_string(t, slot, h, str, l); @@ -542,11 +542,11 @@ internshrstr (lua_State *L, const char *str, size_t l) { } LUA_API void -luaS_expandshr(unsigned int n) { +luaS_expandshr(int n) { struct shrmap * s = &SSM; if (s->n < n) { ATOM_ADD(&s->n, n); - unsigned int t = (s->total + s->n) * 5 / 4; + int t = (s->total + s->n) * 5 / 4; if (t > s->mask) { shrstr_expandpage(t); } @@ -622,10 +622,10 @@ luaS_shrinfo(lua_State *L) { memset(&total, 0, sizeof(total)); struct shrmap * s = &SSM; struct variance v = { 0,0,0 }; - unsigned int slots = 0; + int slots = 0; rwlock_rlock(&s->lock); - unsigned int i; - unsigned int sz = s->mask + 1; + int i; + int sz = s->mask + 1; for (i=0;ireadwrite[i]; getslot(slot, &tmp); diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 5947bfcfc..e6a38b885 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -49,7 +49,7 @@ LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); LUA_API void luaS_initshr(); LUA_API void luaS_exitshr(); -LUA_API void luaS_expandshr(unsigned int n); +LUA_API void luaS_expandshr(int n); LUAI_FUNC TString *luaS_clonestring(lua_State *L, TString *); LUA_API int luaS_shrinfo(lua_State *L); From 6f40120ad83affadc6d3b8678b2644548bb283bb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Apr 2019 20:47:27 +0800 Subject: [PATCH 089/565] expand SSM during loadfile --- 3rd/lua/lauxlib.c | 1 + 3rd/lua/lstring.c | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 63387b7b4..fa775d47f 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1175,6 +1175,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } luaS_expandshr(4096); int err = luaL_loadfilex_(eL, filename, mode); + luaS_expandshr(-4096); if (err != LUA_OK) { size_t sz = 0; const char * msg = lua_tolstring(eL, -1, &sz); diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 68de011d4..b0593436e 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -544,8 +544,13 @@ internshrstr (lua_State *L, const char *str, size_t l) { LUA_API void luaS_expandshr(int n) { struct shrmap * s = &SSM; - if (s->n < n) { - ATOM_ADD(&s->n, n); + if (n < 0) { + if (-n > s->n) { + n = -s->n; + } + } + ATOM_ADD(&s->n, n); + if (n > 0) { int t = (s->total + s->n) * 5 / 4; if (t > s->mask) { shrstr_expandpage(t); From 6ab841beecc622370beb028ebe3c8f05e763047e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Apr 2019 10:17:20 +0800 Subject: [PATCH 090/565] turn on SSM during lua_load --- 3rd/lua/lauxlib.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index fa775d47f..40c5e08c4 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -699,6 +699,7 @@ static int skipcomment (LoadF *lf, int *cp) { else return 0; /* no comment */ } +LUA_API void luaS_expandshr(int n); static int luaL_loadfilex_ (lua_State *L, const char *filename, const char *mode) { @@ -724,7 +725,9 @@ static int luaL_loadfilex_ (lua_State *L, const char *filename, } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ + luaS_expandshr(4096); status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); + luaS_expandshr(-4096); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { @@ -1152,8 +1155,6 @@ static int cache_mode(lua_State *L) { return 0; } -LUA_API void luaS_expandshr(int n); - LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { int level = cache_level(L); @@ -1173,9 +1174,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lua_pushliteral(L, "New state failed"); return LUA_ERRMEM; } - luaS_expandshr(4096); int err = luaL_loadfilex_(eL, filename, mode); - luaS_expandshr(-4096); if (err != LUA_OK) { size_t sz = 0; const char * msg = lua_tolstring(eL, -1, &sz); From 8e8a92ca2dd9d4be53517bbfbb8215bdbd0c9ff2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Apr 2019 11:15:33 +0800 Subject: [PATCH 091/565] overflow check --- 3rd/lua/lstring.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index b0593436e..7e3f95a60 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -367,11 +367,12 @@ shrstr_expandpage(int cap) { return; int osz = s->mask + 1; int sz = osz * 2; - // overflow check - if (sz <= 0) - return; - while (sz < cap) + while (sz < cap) { + // overflow check + if (sz <= 0) + return; sz = sz * 2; + } struct shrmap_slot * newpage = shrstr_newpage(sz); rwlock_wlock(&s->lock); int succ = shrstr_allocpage(s, osz, sz, newpage); From fbd932e8468e83318045f9e0ce5f5f960be6b4ba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Apr 2019 11:29:37 +0800 Subject: [PATCH 092/565] OOM check --- 3rd/lua/lstring.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 7e3f95a60..7c77dba62 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -295,6 +295,8 @@ static struct shrmap_slot * shrstr_newpage(int sz) { int i; struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s)); + if (s == NULL) + return NULL; for (i=0;ilock); int succ = shrstr_allocpage(s, osz, sz, newpage); rwlock_wunlock(&s->lock); From cf18b59d90b7a420800dc3d728f3b64ede9a6441 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Apr 2019 15:27:55 +0800 Subject: [PATCH 093/565] fix #981 --- lualib/skynet/datasheet/builder.lua | 26 +++++++++++++++++++++++++- lualib/skynet/datasheet/init.lua | 4 +--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/datasheet/builder.lua b/lualib/skynet/datasheet/builder.lua index 1683a105d..7e43c0e50 100644 --- a/lualib/skynet/datasheet/builder.lua +++ b/lualib/skynet/datasheet/builder.lua @@ -69,6 +69,13 @@ local skynet = require "skynet" local datasheet = {} local handles = {} -- handle:{ ref:count , name:name , collect:resp } local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} } +local customers = {} -- source: { handle:true } + +setmetatable(customers, { __index = function(c, source) + local v = {} + c[source] = v + return v +end } ) local function releasehandle(source, handle) local h = handles[handle] @@ -109,6 +116,7 @@ function datasheet.query(source, name) local handle = t.handle local h = handles[handle] h.ref = h.ref + 1 + customers[source][handle] = true skynet.ret(skynet.pack(handle)) end @@ -117,19 +125,35 @@ function datasheet.monitor(source, handle) local h = assert(handles[handle], "Invalid data handle") local t = dataset[h.name] if t.handle ~= handle then -- already changes + customers[source][t.handle] = true skynet.ret(skynet.pack(t.handle)) else assert(not t.monitor[source]) - t.monitor[source]=skynet.response() + local resp = skynet.response() + t.monitor[source]= function(ok, handle) + if ok then + customers[source][handle] = true + end + resp(ok, handle) + end end end -- from customers, release handle , ref count - 1 function datasheet.release(source, handle) -- send message, don't ret + customers[source][handle] = nil releasehandle(source, handle) end +-- customer closed, clear all handles it queried +function datasheet.close(source) + for handle in pairs(customers[source]) do + releasehandle(source, handle) + end + customers[source] = nil +end + -- from builder, monitor handle release function datasheet.collect(source, handle) local h = assert(handles[handle], "Invalid data handle") diff --git a/lualib/skynet/datasheet/init.lua b/lualib/skynet/datasheet/init.lua index c902b982b..69c9c6d63 100644 --- a/lualib/skynet/datasheet/init.lua +++ b/lualib/skynet/datasheet/init.lua @@ -11,9 +11,7 @@ end) local datasheet = {} local sheets = setmetatable({}, { __gc = function(t) - for _,v in pairs(t) do - skynet.send(datasheet_svr, "lua", "release", v.handle) - end + skynet.send(datasheet_svr, "lua", "close") end, }) From 4c17005c03294fc315118a8747a6ba747d9c0eaf Mon Sep 17 00:00:00 2001 From: hong Date: Wed, 17 Apr 2019 13:58:56 +0800 Subject: [PATCH 094/565] =?UTF-8?q?=E4=BC=98=E5=8C=96SSM=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 7c77dba62..095771e1e 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -579,7 +579,7 @@ luaS_clonestring(lua_State *L, TString *ts) { if (result) return result; // look up SSM by ptr - result = query_string(ts, 0, NULL, 0); + result = query_string(ts, ts->hash, NULL, 0); if (result) return result; h = luaS_hash(str, l, 0); From 9ba0860c701850a04154b4f9799f391f9d388fa3 Mon Sep 17 00:00:00 2001 From: hong Date: Wed, 17 Apr 2019 11:18:51 +0800 Subject: [PATCH 095/565] =?UTF-8?q?bugfix:cluster=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E8=8A=82=E7=82=B9=E7=9A=84=E6=97=B6=E5=80=99=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=B6=88=E6=81=AF=E4=B9=B1?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/cluster.lua | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index c1633d3ec..926d387f9 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -3,11 +3,26 @@ local skynet = require "skynet" local clusterd local cluster = {} local sender = {} +local inquery_name = {} local function get_sender(t, node) - local c = skynet.call(clusterd, "lua", "sender", node) - t[node] = c - return c + local waitco = inquery_name[node] + if waitco then + local co=coroutine.running() + table.insert(waitco, co) + skynet.wait(co) + return rawget(t, node) + else + waitco = {} + inquery_name[node] = waitco + local c = skynet.call(clusterd, "lua", "sender", node) + inquery_name[node] = nil + t[node] = c + for _, co in ipairs(waitco) do + skynet.wakeup(co) + end + return c + end end setmetatable(sender, { __index = get_sender } ) From 77e264372816f22f96eb936149da0a901b7dc846 Mon Sep 17 00:00:00 2001 From: hong Date: Wed, 17 Apr 2019 11:42:03 +0800 Subject: [PATCH 096/565] =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E5=8D=A1=E4=BD=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add pcall --- lualib/skynet/cluster.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 926d387f9..e32569f45 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -11,16 +11,19 @@ local function get_sender(t, node) local co=coroutine.running() table.insert(waitco, co) skynet.wait(co) - return rawget(t, node) + return assert(rawget(t, node)) else waitco = {} inquery_name[node] = waitco - local c = skynet.call(clusterd, "lua", "sender", node) + local ok,c = pcall(skynet.call, clusterd, "lua", "sender", node) + if ok then + t[node] = c + end inquery_name[node] = nil - t[node] = c for _, co in ipairs(waitco) do skynet.wakeup(co) end + assert(ok,c) return c end end From 201becd3d163ddca333ad9f078a1fbea901d597e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 18 Apr 2019 15:40:09 +0800 Subject: [PATCH 097/565] =?UTF-8?q?Revert=20"=E9=81=BF=E5=85=8D=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E5=8D=A1=E4=BD=8F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 77e264372816f22f96eb936149da0a901b7dc846. --- lualib/skynet/cluster.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index e32569f45..926d387f9 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -11,19 +11,16 @@ local function get_sender(t, node) local co=coroutine.running() table.insert(waitco, co) skynet.wait(co) - return assert(rawget(t, node)) + return rawget(t, node) else waitco = {} inquery_name[node] = waitco - local ok,c = pcall(skynet.call, clusterd, "lua", "sender", node) - if ok then - t[node] = c - end + local c = skynet.call(clusterd, "lua", "sender", node) inquery_name[node] = nil + t[node] = c for _, co in ipairs(waitco) do skynet.wakeup(co) end - assert(ok,c) return c end end From e0729a483d6b5b86c76af14b167f2527f84e89a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 18 Apr 2019 15:40:09 +0800 Subject: [PATCH 098/565] =?UTF-8?q?Revert=20"bugfix:cluster=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E8=8A=82=E7=82=B9=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E6=9C=89=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=B9=B1=E5=BA=8F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 9ba0860c701850a04154b4f9799f391f9d388fa3. --- lualib/skynet/cluster.lua | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 926d387f9..c1633d3ec 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -3,26 +3,11 @@ local skynet = require "skynet" local clusterd local cluster = {} local sender = {} -local inquery_name = {} local function get_sender(t, node) - local waitco = inquery_name[node] - if waitco then - local co=coroutine.running() - table.insert(waitco, co) - skynet.wait(co) - return rawget(t, node) - else - waitco = {} - inquery_name[node] = waitco - local c = skynet.call(clusterd, "lua", "sender", node) - inquery_name[node] = nil - t[node] = c - for _, co in ipairs(waitco) do - skynet.wakeup(co) - end - return c - end + local c = skynet.call(clusterd, "lua", "sender", node) + t[node] = c + return c end setmetatable(sender, { __index = get_sender } ) From 3048eed2aaaf3595d7e22ac792a41f60214a8b87 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 11:17:07 +0800 Subject: [PATCH 099/565] try to fix #988 --- lualib/skynet/cluster.lua | 60 +++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index c1633d3ec..d86011450 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -3,23 +3,67 @@ local skynet = require "skynet" local clusterd local cluster = {} local sender = {} +local task_queue = {} -local function get_sender(t, node) - local c = skynet.call(clusterd, "lua", "sender", node) - t[node] = c - return c +local function request_sender(q, node) + local ok, c = pcall(skynet.call, clusterd, "lua", "sender", node) + if not ok then + skynet.error(c) + c = nil + end + -- run tasks in queue + local confirm = coroutine.running() + q.confirm = confirm + q.sender = c + for _, task in ipairs(q) do + if type(task) == "table" then + if c then + skynet.send(c, "lua", "push", table.unpack(task,1,task.n)) + end + else + skynet.wakeup(task) + skynet.wait(confirm) + end + end + task_queue[node] = nil + sender[node] = c end -setmetatable(sender, { __index = get_sender } ) +local function get_queue(t, node) + local q = {} + t[node] = q + skynet.fork(request_sender, q, node) + return q +end + +setmetatable(task_queue, { __index = get_queue } ) + +local function get_sender(node) + local s = sender[node] + if not s then + local q = task_queue[node] + local task = coroutine.running() + table.insert(q, task) + skynet.wait(task) + skynet.wakeup(q.confirm) + return q.sender + end + return s +end function cluster.call(node, address, ...) -- skynet.pack(...) will free by cluster.core.packrequest - return skynet.call(sender[node], "lua", "req", address, skynet.pack(...)) + return skynet.call(get_sender(node), "lua", "req", address, skynet.pack(...)) end function cluster.send(node, address, ...) -- push is the same with req, but no response - skynet.send(sender[node], "lua", "push", address, skynet.pack(...)) + local s = sender[node] + if not s then + table.insert(task_queue[node], table.pack(address, ...)) + else + skynet.send(sender[node], "lua", "push", address, skynet.pack(...)) + end end function cluster.open(port) @@ -54,7 +98,7 @@ function cluster.register(name, addr) end function cluster.query(node, name) - return skynet.call(sender[node], "lua", "req", 0, skynet.pack(name)) + return skynet.call(get_sender(node), "lua", "req", 0, skynet.pack(name)) end skynet.init(function() From 302fc580a5125b0ab5b3e893a1a1e18b219c84e5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 14:31:42 +0800 Subject: [PATCH 100/565] bugfix --- lualib/skynet/cluster.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index d86011450..2472b700c 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -18,7 +18,7 @@ local function request_sender(q, node) for _, task in ipairs(q) do if type(task) == "table" then if c then - skynet.send(c, "lua", "push", table.unpack(task,1,task.n)) + skynet.send(c, "lua", "push", task[1], skynet.pack(table.unpack(task,2,task.n))) end else skynet.wakeup(task) From 95cb848772a04b60639b1badbf18e7106c13ea7f Mon Sep 17 00:00:00 2001 From: wudeng Date: Fri, 19 Apr 2019 11:57:31 +0800 Subject: [PATCH 101/565] collectgarbage count change to lua 5.3 --- lualib/skynet/debug.lua | 4 ++-- service/launcher.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 3b540fdab..e20279508 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -14,8 +14,8 @@ local function init(skynet, export) dbgcmd = {} function dbgcmd.MEM() - local kb, bytes = collectgarbage "count" - skynet.ret(skynet.pack(kb,bytes)) + local kb = collectgarbage "count" + skynet.ret(skynet.pack(kb)) end function dbgcmd.GC() diff --git a/service/launcher.lua b/service/launcher.lua index d458d13ef..77300a23f 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -45,7 +45,7 @@ end function command.MEM() local list = {} for k,v in pairs(services) do - local ok, kb, bytes = pcall(skynet.call,k,"debug","MEM") + local ok, kb = pcall(skynet.call,k,"debug","MEM") if not ok then list[skynet.address(k)] = string.format("ERROR (%s)",v) else From 04e2a36c4af149a2344632bcb5229c04801d8dc4 Mon Sep 17 00:00:00 2001 From: hong Date: Sat, 27 Apr 2019 16:55:19 +0800 Subject: [PATCH 102/565] Update service_provider.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit boot成功后的launch消息异常 --- service/service_provider.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service/service_provider.lua b/service/service_provider.lua index b04067cab..1d0e238a2 100644 --- a/service/service_provider.lua +++ b/service/service_provider.lua @@ -41,6 +41,9 @@ end function provider.launch(name, code, ...) local s = svr[name] + if s.address then + return skynet.ret(skynet.pack(s.address)) + end if s.booting then table.insert(s.queue, skynet.response()) else From 4159600fbc1c8bec97b01d48f6a5f4b4559d9096 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 28 Apr 2019 09:53:52 +0800 Subject: [PATCH 103/565] order fork, see #1000 --- lualib/skynet.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 73055d44a..8901814b5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -6,6 +6,7 @@ local assert = assert local pairs = pairs local pcall = pcall local table = table +local tremove = table.remove local profile = require "skynet.profile" @@ -70,7 +71,7 @@ local suspend ----- monitor exit local function dispatch_error_queue() - local session = table.remove(error_queue,1) + local session = tremove(error_queue,1) if session then local co = session_id_coroutine[session] session_id_coroutine[session] = nil @@ -105,7 +106,7 @@ end local coroutine_pool = setmetatable({}, { __mode = "kv" }) local function co_create(f) - local co = table.remove(coroutine_pool) + local co = tremove(coroutine_pool) if co == nil then co = coroutine_create(function(...) f(...) @@ -148,7 +149,7 @@ local function co_create(f) end local function dispatch_wakeup() - local token = table.remove(wakeup_queue,1) + local token = tremove(wakeup_queue,1) if token then local session = sleep_session[token] if session then @@ -597,11 +598,10 @@ end function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do - local key,co = next(fork_queue) + local co = tremove(fork_queue,1) if co == nil then break end - fork_queue[key] = nil local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co)) if not fork_succ then if succ then From 35aa00fb138604c2bccdb122550dfee7ea1ba221 Mon Sep 17 00:00:00 2001 From: wudeng Date: Tue, 30 Apr 2019 15:35:43 +0800 Subject: [PATCH 104/565] Add jemalloc heap profilling in debug console --- lualib-src/lua-memory.c | 22 ++++++++++++++++++++++ service/debug_console.lua | 19 ++++++++++++++++++- skynet-src/malloc_hook.c | 35 ++++++++++++++++++++++++++--------- skynet-src/malloc_hook.h | 3 +++ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 5fc75752f..426acf18d 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -49,6 +49,26 @@ lcurrent(lua_State *L) { return 1; } +static int +ldumpheap(lua_State *L) { + mallctl_cmd("prof.dump"); + return 0; +} + +static int +lprofactive(lua_State *L) { + bool *pval, active; + if (lua_isnone(L, 1)) { + pval = NULL; + } else { + active = lua_toboolean(L, 1) ? true : false; + pval = &active; + } + bool ret = mallctl_bool("prof.active", pval); + lua_pushboolean(L, ret); + return 1; +} + LUAMOD_API int luaopen_skynet_memory(lua_State *L) { luaL_checkversion(L); @@ -62,6 +82,8 @@ luaopen_skynet_memory(lua_State *L) { { "ssinfo", luaS_shrinfo }, { "ssexpand", lexpandshrtbl }, { "current", lcurrent }, + { "dumpheap", ldumpheap }, + { "profactive", lprofactive }, { NULL, NULL }, }; diff --git a/service/debug_console.lua b/service/debug_console.lua index daa2e688e..0323c2152 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -160,6 +160,8 @@ function COMMAND.help() call = "call address ...", trace = "trace address [proto] [on|off]", netstat = "netstat : show netstat", + profactive = "profactive [on|off] : active/deactive jemalloc heap profilling", + dumpheap = "dumpheap : dump heap profilling", } end @@ -421,4 +423,19 @@ function COMMAND.netstat() convert_stat(info) end return stat -end \ No newline at end of file +end + +function COMMAND.dumpheap() + memory.dumpheap() +end + +function COMMAND.profactive(flag) + if flag ~= nil then + if flag == "on" or flag == "off" then + flag = toboolean(flag) + end + memory.profactive(flag) + end + local active = memory.profactive() + return "heap profilling is ".. (active and "active" or "deactive") +end diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 02b6c0912..a3ce6693d 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -65,10 +65,10 @@ get_allocated_field(uint32_t handle) { return &data->allocated; } -inline static void +inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { ATOM_ADD(&_used_memory, __n); - ATOM_INC(&_memory_block); + ATOM_INC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { ATOM_ADD(allocated, __n); @@ -126,12 +126,29 @@ static void malloc_oom(size_t size) { abort(); } -void +void memory_info_dump(void) { je_malloc_stats_print(0,0,0); } -size_t +bool +mallctl_bool(const char* name, bool* newval) { + bool v = 0; + size_t len = sizeof(v); + if(newval) { + je_mallctl(name, &v, &len, newval, sizeof(bool)); + } else { + je_mallctl(name, &v, &len, NULL, 0); + } + return v; +} + +int +mallctl_cmd(const char* name) { + return je_mallctl(name, NULL, NULL, NULL, 0); +} + +size_t mallctl_int64(const char* name, size_t* newval) { size_t v = 0; size_t len = sizeof(v); @@ -144,7 +161,7 @@ mallctl_int64(const char* name, size_t* newval) { return v; } -int +int mallctl_opt(const char* name, int* newval) { int v = 0; size_t len = sizeof(v); @@ -223,18 +240,18 @@ skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { #define raw_realloc realloc #define raw_free free -void +void memory_info_dump(void) { skynet_error(NULL, "No jemalloc"); } -size_t +size_t mallctl_int64(const char* name, size_t* newval) { skynet_error(NULL, "No jemalloc : mallctl_int64 %s.", name); return 0; } -int +int mallctl_opt(const char* name, int* newval) { skynet_error(NULL, "No jemalloc : mallctl_opt %s.", name); return 0; @@ -275,7 +292,7 @@ skynet_strdup(const char *str) { return ret; } -void * +void * skynet_lalloc(void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { raw_free(ptr); diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index 4da4123a4..04f522ac7 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -2,6 +2,7 @@ #define SKYNET_MALLOC_HOOK_H #include +#include #include extern size_t malloc_used_memory(void); @@ -9,6 +10,8 @@ extern size_t malloc_memory_block(void); 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 bool mallctl_bool(const char* name, bool* newval); +extern int mallctl_cmd(const char* name); extern void dump_c_mem(void); extern int dump_mem_lua(lua_State *L); extern size_t malloc_current_memory(void); From 6231f1c884c9fc72cd4beac452fc2f1b09fd502d Mon Sep 17 00:00:00 2001 From: wudeng Date: Fri, 3 May 2019 14:48:17 +0800 Subject: [PATCH 105/565] build jemalloc with --enable-prof --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 127d75526..004aabb97 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ $(JEMALLOC_STATICLIB) : 3rd/jemalloc/Makefile git submodule update --init 3rd/jemalloc/Makefile : | 3rd/jemalloc/autogen.sh - cd 3rd/jemalloc && ./autogen.sh --with-jemalloc-prefix=je_ --disable-valgrind + cd 3rd/jemalloc && ./autogen.sh --with-jemalloc-prefix=je_ --enable-prof jemalloc : $(MALLOC_STATICLIB) From 046fb69f843200543e2712bda3820ff404dbe373 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 6 May 2019 10:20:15 +0800 Subject: [PATCH 106/565] bugfix --- 3rd/lua/lauxlib.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 40c5e08c4..b77062b48 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1094,21 +1094,20 @@ save(const char *key, const void * proto) { SPIN_LOCK(&CC) if (CC.L == NULL) { init(); - L = CC.L; + } + 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 { - 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); - } + lua_pop(L,2); } + SPIN_UNLOCK(&CC) return result; } From 9b69af0a4d4a34dc061ae471e5f975e635a640b3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 6 May 2019 10:29:23 +0800 Subject: [PATCH 107/565] use tinsert instead of table.insert --- lualib/skynet.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 8901814b5..a49f08134 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -7,6 +7,7 @@ local pairs = pairs local pcall = pcall local table = table local tremove = table.remove +local tinsert = table.insert local profile = require "skynet.profile" @@ -90,13 +91,13 @@ local function _error_dispatch(error_session, error_source) end for session, srv in pairs(watching_session) do if srv == error_source then - table.insert(error_queue, session) + tinsert(error_queue, session) end end else -- capture an error for error_session if watching_session[error_session] then - table.insert(error_queue, error_session) + tinsert(error_queue, error_session) end end end @@ -480,7 +481,7 @@ end function skynet.wakeup(token) if sleep_session[token] then - table.insert(wakeup_queue, token) + tinsert(wakeup_queue, token) return true end end @@ -527,7 +528,7 @@ function skynet.fork(func,...) local args = { ... } co = co_create(function() func(table.unpack(args,1,n)) end) end - table.insert(fork_queue, co) + tinsert(fork_queue, co) return co end @@ -689,7 +690,7 @@ function skynet.init(f, name) if init_func == nil then f() else - table.insert(init_func, f) + tinsert(init_func, f) if name then assert(type(name) == "string") assert(init_func[name] == nil) From 2c8d373236b57a6e761bf791cbdc0c88d2e21257 Mon Sep 17 00:00:00 2001 From: zixun Date: Mon, 6 May 2019 11:14:08 +0800 Subject: [PATCH 108/565] fix use NOUSE_JEMALLOC --- skynet-src/malloc_hook.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index a3ce6693d..150c2cc61 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -257,6 +257,18 @@ mallctl_opt(const char* name, int* newval) { return 0; } +bool +mallctl_bool(const char* name, bool* newval) { + skynet_error(NULL, "No jemalloc : mallctl_bool %s.", name); + return 0; +} + +int +mallctl_cmd(const char* name) { + skynet_error(NULL, "No jemalloc : mallctl_cmd %s.", name); + return 0; +} + #endif size_t From 431feb6f16477eb77f4e95d9fde2989248290481 Mon Sep 17 00:00:00 2001 From: zixun Date: Mon, 6 May 2019 14:16:04 +0800 Subject: [PATCH 109/565] bugfix #1007 --- lualib/http/tlshelper.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua index 553acd9e8..e25b22922 100644 --- a/lualib/http/tlshelper.lua +++ b/lualib/http/tlshelper.lua @@ -52,7 +52,9 @@ function tlshelper.readfunc(fd, tls_ctx) local ds = readfunc(sz) s = tls_ctx:read(ds) end - return read_buff .. s + s = read_buff .. s + read_buff = "" + return s else while #read_buff < sz do local ds = readfunc() From db61ec376aa07216c7ff8503b16d217b2fdb8013 Mon Sep 17 00:00:00 2001 From: hong Date: Mon, 6 May 2019 21:57:10 +0800 Subject: [PATCH 110/565] clustersender tracetag bug --- service/clusterd.lua | 11 +---------- service/clustersender.lua | 10 +++------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index cc82005a7..f891df2a1 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -1,23 +1,14 @@ local skynet = require "skynet" require "skynet.manager" -local sc = require "skynet.socketchannel" -local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} -local node_session = {} local node_sender = {} local command = {} local config = {} local nodename = cluster.nodename() -local function read_response(sock) - local sz = socket.header(sock:read(2)) - local msg = sock:read(sz) - return cluster.unpackresponse(msg) -- session, ok, data, padding -end - local connecting = {} local function open_channel(t, key) @@ -44,7 +35,7 @@ local function open_channel(t, key) local host, port = string.match(address, "([^:]+):(.*)$") c = node_sender[key] if c == nil then - c = skynet.newservice "clustersender" + c = skynet.newservice("clustersender", key, nodename) if node_sender[key] then -- double check skynet.kill(c) diff --git a/service/clustersender.lua b/service/clustersender.lua index 250d70b24..992373ef6 100644 --- a/service/clustersender.lua +++ b/service/clustersender.lua @@ -2,21 +2,21 @@ local skynet = require "skynet" local sc = require "skynet.socketchannel" local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" -local ignoreret = skynet.ignoreret local channel local session = 1 +local node, nodename = ... local command = {} local waiting = {} + local function send_request(addr, msg, sz) -- msg is a local pointer, cluster.packrequest will free it local current_session = session local request, new_session, padding = cluster.packrequest(addr, session, msg, sz) session = new_session - -- node_channel[node] may yield or throw error local tracetag = skynet.tracetag() if tracetag then if tracetag:sub(1,1) ~= "(" then @@ -63,7 +63,6 @@ function command.push(addr, msg, sz) session = new_session end - -- node_channel[node] may yield or throw error channel:request(request, nil, padding) end @@ -80,7 +79,7 @@ function command.changenode(host, port) response = read_response, nodelay = true, } - succ, err = pcall(c.connect, c, true) + local succ, err = pcall(c.connect, c, true) if channel then channel:close() end @@ -103,6 +102,3 @@ skynet.start(function() f(...) end) end) - - - From 6addb504e4c956dbbb22fb9e3ed1ed1e87972986 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 15 May 2019 10:49:19 +0800 Subject: [PATCH 111/565] fix #1012 --- lualib/snax/loginserver.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index be88b97aa..7ea36b21a 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -24,7 +24,6 @@ Protocol: 10. Server->Client : 200 base64(subid) 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) @@ -70,7 +69,6 @@ local function launch_slave(auth_handler) local hmac = crypt.hmac64(challenge, secret) if hmac ~= crypt.base64decode(response) then - write("auth", fd, "400 Bad Request\n") error "challenge failed" end From be590cfa8cbcbb3f653d211fbf7900d0a9da5926 Mon Sep 17 00:00:00 2001 From: zixun Date: Tue, 21 May 2019 19:35:04 +0800 Subject: [PATCH 112/565] #1016 bugfix SSL_read and SSL_do_handshake --- lualib-src/ltls.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 74b3a3bb3..4c1275abd 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -178,10 +178,15 @@ _ltls_context_handshake(lua_State* L) { int ret = SSL_do_handshake(tls_p->ssl); if(ret == 1) { return 0; - } else if (ret == -1) { - int all_read = _bio_read(L, tls_p); - if(all_read>0) { - return 1; + } else if (ret < 0) { + int err = SSL_get_error(tls_p->ssl, ret); + if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { + int all_read = _bio_read(L, tls_p); + if(all_read>0) { + return 1; + } + } else { + luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret); } } else { int err = SSL_get_error(tls_p->ssl, ret); @@ -212,7 +217,7 @@ _ltls_context_read(lua_State* L) { read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); if(read < 0) { int err = SSL_get_error(tls_p->ssl, read); - if(err == SSL_ERROR_WANT_READ) { + if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { break; } luaL_error(L, "SSL_read error:%d", err); @@ -221,7 +226,7 @@ _ltls_context_read(lua_State* L) { }else if (read > 0) { luaL_addlstring(&b, outbuff, read); } - }while(read == sizeof(outbuff)); + }while(true); luaL_pushresult(&b); return 1; } From 194940822e92bb3037509459727f251016091147 Mon Sep 17 00:00:00 2001 From: zixun Date: Wed, 22 May 2019 12:12:57 +0800 Subject: [PATCH 113/565] fix SSL_read and SSL_write return 0 --- lualib-src/ltls.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 4c1275abd..aff3b9f2a 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -215,16 +215,16 @@ _ltls_context_read(lua_State* L) { do { read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); - if(read < 0) { + if(read <= 0) { int err = SSL_get_error(tls_p->ssl, read); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { break; } luaL_error(L, "SSL_read error:%d", err); - }else if(read > sizeof(outbuff)) { - luaL_error(L, "invalid SSL_read"); - }else if (read > 0) { + }else if(read <= sizeof(outbuff)) { luaL_addlstring(&b, outbuff, read); + }else { + luaL_error(L, "invalid SSL_read"); } }while(true); luaL_pushresult(&b); @@ -238,16 +238,16 @@ _ltls_context_write(lua_State* L) { size_t slen = 0; char* unencrypted_data = (char*)lua_tolstring(L, 2, &slen); - while(slen >0) { + while(slen > 0) { int written = SSL_write(tls_p->ssl, unencrypted_data, slen); - if(written < 0) { + if(written <= 0) { int err = SSL_get_error(tls_p->ssl, written); luaL_error(L, "SSL_write error:%d", err); - }else if(written > slen) { - luaL_error(L, "invalid SSL_write"); - }else if(written>0) { + }else if(written <= slen) { unencrypted_data += written; slen -= written; + }else { + luaL_error(L, "invalid SSL_write"); } } From 2d6d2c75a400eecfe193996815ed7890f9753d3d Mon Sep 17 00:00:00 2001 From: zixun Date: Wed, 22 May 2019 16:28:03 +0800 Subject: [PATCH 114/565] fix BIO_read and BIO_write return 0 --- lualib-src/ltls.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index aff3b9f2a..00716b29d 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -119,19 +119,20 @@ _bio_read(lua_State* L, struct tls_context* tls_p) { if(pending >0) { luaL_Buffer b; luaL_buffinit(L, &b); - do { + while(pending > 0) { read = BIO_read(tls_p->out_bio, outbuff, sizeof(outbuff)); - // printf("_bio_read:%d pending:%d\n", read, pending); - if(read > sizeof(outbuff)) { - luaL_error(L, "invalid BIO_read:%d", read); - }else if(read == -2) { - luaL_error(L, "BIO_read not implemented in the specific BIO type"); - }else if (read > 0) { + // printf("BIO_read read:%d pending:%d\n", read, pending); + if(read <= 0) { + luaL_error(L, "BIO_read error:%d", read); + }else if(read <= sizeof(outbuff)) { all_read += read; luaL_addlstring(&b, (const char*)outbuff, read); + }else { + luaL_error(L, "invalid BIO_read:%d", read); } - }while(read == sizeof(outbuff)); - if(all_read>0){ + pending = BIO_ctrl_pending(tls_p->out_bio); + } + if(all_read>0) { luaL_pushresult(&b); } } @@ -145,13 +146,14 @@ _bio_write(lua_State* L, struct tls_context* tls_p, const char* s, size_t len) { size_t sz = len; while(sz > 0) { int written = BIO_write(tls_p->in_bio, p, sz); - if(written > sz) { - luaL_error(L, "invalid BIO_write"); - }else if(written > 0) { + // printf("BIO_write written:%d sz:%zu\n", written, sz); + if(written <= 0) { + luaL_error(L, "BIO_write error:%d", written); + }else if (written <= sz) { p += written; sz -= written; - }else if (written == -2) { - luaL_error(L, "BIO_write not implemented in the specific BIO type"); + }else { + luaL_error(L, "invalid BIO_write:%d", written); } } } @@ -224,7 +226,7 @@ _ltls_context_read(lua_State* L) { }else if(read <= sizeof(outbuff)) { luaL_addlstring(&b, outbuff, read); }else { - luaL_error(L, "invalid SSL_read"); + luaL_error(L, "invalid SSL_read:%d", read); } }while(true); luaL_pushresult(&b); @@ -247,7 +249,7 @@ _ltls_context_write(lua_State* L) { unencrypted_data += written; slen -= written; }else { - luaL_error(L, "invalid SSL_write"); + luaL_error(L, "invalid SSL_write:%d", written); } } From 2ceb642b5d62abd9de18bd13ba923e33f0a69fe4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 16 Apr 2019 21:16:13 +0800 Subject: [PATCH 115/565] rewrite SSM and clonefunction --- 3rd/lua/lapi.c | 59 +-- 3rd/lua/lauxlib.c | 7 +- 3rd/lua/lauxlib.h | 2 + 3rd/lua/lcode.c | 36 +- 3rd/lua/lcode.h | 2 +- 3rd/lua/ldebug.c | 24 +- 3rd/lua/ldebug.h | 4 +- 3rd/lua/ldo.c | 6 +- 3rd/lua/ldump.c | 21 +- 3rd/lua/lfunc.c | 89 +++-- 3rd/lua/lfunc.h | 3 +- 3rd/lua/lgc.c | 94 ++--- 3rd/lua/lgc.h | 6 +- 3rd/lua/lobject.h | 26 +- 3rd/lua/lparser.c | 87 ++--- 3rd/lua/lstate.c | 29 +- 3rd/lua/lstate.h | 7 +- 3rd/lua/lstring.c | 788 +++++++++++++++++++++++++++----------- 3rd/lua/lstring.h | 28 +- 3rd/lua/lua.c | 4 +- 3rd/lua/lua.h | 10 +- 3rd/lua/luac.c | 30 +- 3rd/lua/lualib.h | 2 - 3rd/lua/lundump.c | 29 +- 3rd/lua/lvm.c | 60 +-- Makefile | 1 + lualib-src/lua-memory.c | 10 - lualib-src/lua-ssm.c | 72 ++++ service/bootstrap.lua | 6 +- service/debug_console.lua | 6 - skynet-src/luashrtbl.h | 21 +- skynet-src/skynet_main.c | 4 +- 32 files changed, 931 insertions(+), 642 deletions(-) create mode 100644 lualib-src/lua-ssm.c diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 3d8545091..ffea5e531 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1013,52 +1013,11 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, return status; } -static void cloneproto (lua_State *L, Proto *f, const Proto *src) { - /* copy constants and nested proto */ - int i,n; - n = src->sp->sizek; - if (src->sp->sharedk) - f->k = src->sp->k; - else { - lu_byte sharedk = 1; - 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 * sstr = tsvalue(s); - if (ttisshrstring(s)) { - TString * str = luaS_clonestring(L,sstr); - setsvalue2n(L,o,str); - if (str != sstr) - sharedk = 0; - } else { - setsvalue2n(L,o,sstr); - } - } else { - setobj(L,o,s); - } - } - if (sharedk) { - luaM_freearray(L, f->k, n); - f->k = src->sp->k; - } - } - n = src->sp->sizep; - f->p=luaM_newvector(L,n,struct Proto *); - for (i=0; ip[i]=NULL; - for (i=0; ip[i]= luaF_newproto(L, src->p[i]->sp); - cloneproto(L, f->p[i], src->p[i]); - } -} - 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)) { + if (f->p->l_G == G(L)) { setclLvalue(L,L->top,f); api_incr_top(L); lua_unlock(L); @@ -1067,8 +1026,7 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { cl = luaF_newLclosure(L,f->nupvalues); setclLvalue(L,L->top,cl); api_incr_top(L); - cl->p = luaF_newproto(L, f->p->sp); - cloneproto(L, cl->p, f->p); + cl->p = f->p; luaF_initupvals(L, cl); if (cl->nupvalues >= 1) { /* does it have an upvalue? */ @@ -1082,6 +1040,15 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { lua_unlock(L); } +LUA_API void lua_sharefunction (lua_State *L, int index) { + if (!lua_isfunction(L,index) || lua_iscfunction(L,index)) { + lua_pushstring(L, "Only Lua function can share"); + lua_error(L); + } + LClosure *f = cast(LClosure *, lua_topointer(L, index)); + luaF_shareproto(f->p); +} + LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; @@ -1276,7 +1243,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]; @@ -1328,7 +1295,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->sp->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 */ } diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index b77062b48..58c6d996b 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -699,9 +699,7 @@ static int skipcomment (LoadF *lf, int *cp) { else return 0; /* no comment */ } -LUA_API void luaS_expandshr(int n); - -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; @@ -725,9 +723,7 @@ static int luaL_loadfilex_ (lua_State *L, const char *filename, } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ - luaS_expandshr(4096); status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); - luaS_expandshr(-4096); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { @@ -1181,6 +1177,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lua_close(eL); return err; } + lua_sharefunction(eL, -1); proto = lua_topointer(eL, -1); const void * oldv = save(filename, proto); if (oldv) { diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 9857d3a83..f0a289dc7 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -82,6 +82,8 @@ LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, const char *mode); +LUALIB_API int (luaL_loadfilex_) (lua_State *L, const char *filename, + const char *mode); #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 6392d4b9b..12619f54a 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -65,7 +65,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) { /* previous is LOADNIL? */ int pfrom = GETARG_A(*previous); /* get previous range */ int pl = pfrom + GETARG_B(*previous); @@ -88,7 +88,7 @@ void luaK_nil (FuncState *fs, int from, int n) { ** a list of jumps. */ 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 @@ -101,7 +101,7 @@ static int getjump (FuncState *fs, int pc) { ** (Jump addresses are relative in Lua) */ 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) @@ -177,7 +177,7 @@ int luaK_getlabel (FuncState *fs) { ** unconditional. */ 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 @@ -278,10 +278,10 @@ void luaK_patchlist (FuncState *fs, int list, int target) { void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ 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); + 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); } } @@ -294,13 +294,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++; } @@ -360,11 +360,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 needs too many registers"); - fs->f->sp->maxstacksize = cast_byte(newstack); + fs->f->maxstacksize = cast_byte(newstack); } } @@ -438,13 +438,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); @@ -1175,7 +1175,7 @@ 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; + fs->f->lineinfo[fs->pc - 1] = line; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 8aa4e98c4..882dc9c15 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -41,7 +41,7 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; /* get (pointer to) instruction of given 'expdesc' */ -#define getinstruction(fs,e) ((fs)->f->sp->code[(e)->u.info]) +#define getinstruction(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/ldebug.c b/3rd/lua/ldebug.c index aae85534e..e1389296e 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -125,14 +125,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 >= cast_int(ci->u.l.base - ci->func) - nparams) return NULL; /* no such vararg */ else { @@ -216,7 +216,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; @@ -234,12 +234,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 */ } } @@ -279,8 +279,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; } @@ -387,7 +387,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) { @@ -437,7 +437,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: { @@ -463,7 +463,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"; @@ -493,7 +493,7 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 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 */ + Instruction i = p->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; @@ -658,7 +658,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->sp->source, currentline(ci)); + 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 584605333..8cea0ee0a 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)->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) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 64795db12..316e45c8f 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -290,7 +290,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; @@ -439,7 +439,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; int n = cast_int(L->top - func) - 1; /* number of real arguments */ int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); @@ -775,7 +775,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->sp->sizeupvalues); + lua_assert(cl->nupvalues == cl->p->sizeupvalues); luaF_initupvals(L, cl); } diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index 9749c9394..f025acac3 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 SharedProto *f, DumpState *D) { +static void DumpCode (const Proto *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->sp->sizek; + int n = f->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->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++) { @@ -144,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); @@ -163,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 @@ -175,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); } @@ -209,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 9c0c911f4..eaf100be6 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -96,59 +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->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; - sp->k = NULL; - sp->sharedk = 0; - } - f->sp = sp; + f->sizep = 0; + f->code = 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; + f->l_G = G(L); return f; } -static void freesharedproto (lua_State *L, Proto *pf) { - SharedProto *f = pf->sp; - if (f == NULL) - return; - if (G(L) == f->l_G) { - 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_freearray(L, f->k, f->sizek); - luaM_free(L, f); - } else if (pf->k != f->k) { - luaM_freearray(L, pf->k, f->sizek); - } -} - void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->p, f->sp->sizep); - freesharedproto(L, 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); } @@ -157,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--; @@ -170,3 +149,21 @@ const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { return NULL; /* not found */ } +#define MAKESHARED(x) if ((x) && (x)->tt == LUA_TLNGSTR) makeshared(x) + +void luaF_shareproto (Proto *f) { + int i; + if (f == NULL) + return; + MAKESHARED(f->source); + for (i = 0; i < f->sizek; i++) { + if (ttype(&f->k[i]) == LUA_TSTRING) + MAKESHARED(tsvalue(&f->k[i])); + } + for (i = 0; i < f->sizeupvalues; i++) + MAKESHARED(f->upvalues[i].name); + for (i = 0; i < f->sizelocvars; i++) + MAKESHARED(f->locvars[i].varname); + for (i = 0; i < f->sizep; i++) + luaF_shareproto(f->p[i]); +} diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 71795b41a..e9512514d 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, 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); @@ -56,6 +56,7 @@ LUAI_FUNC void luaF_close (lua_State *L, StkId level); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); +LUAI_FUNC void luaF_shareproto (Proto *func); #endif diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index a56771300..e56f29228 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -193,6 +193,10 @@ void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); + if (o->tt == LUA_TSHRSTR) { + luaS_fix(g, gco2ts(o)); + return; + } 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 */ @@ -235,20 +239,22 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { */ static void reallymarkobject (global_State *g, GCObject *o) { reentry: - white2gray(o); switch (o->tt) { case LUA_TSHRSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); + luaS_mark(g, gco2ts(o)); break; } case LUA_TLNGSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); + if (!isshared(o)) { + white2gray(o); + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); + } break; } case LUA_TUSERDATA: { TValue uvalue; + white2gray(o); markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ gray2black(o); g->GCmemtrav += sizeudata(gco2u(o)); @@ -260,23 +266,31 @@ static void reallymarkobject (global_State *g, GCObject *o) { break; } case LUA_TLCL: { + white2gray(o); linkgclist(gco2lcl(o), g->gray); break; } case LUA_TCCL: { + white2gray(o); linkgclist(gco2ccl(o), g->gray); break; } case LUA_TTABLE: { - linkgclist(gco2t(o), g->gray); + if (!isshared(o)) + white2gray(o); + linkgclist(gco2t(o), g->gray); break; } case LUA_TTHREAD: { + white2gray(o); linkgclist(gco2th(o), g->gray); break; } case LUA_TPROTO: { - linkgclist(gco2p(o), g->gray); + if (!isshared(o)) { + white2gray(o); + linkgclist(gco2p(o), g->gray); + } break; } default: lua_assert(0); break; @@ -471,20 +485,6 @@ static lu_mem traversetable (global_State *g, Table *h) { sizeof(Node) * cast(size_t, allocsizenode(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 @@ -492,20 +492,24 @@ static int marksharedproto (global_State *g, SharedProto *f) { ** NULL, so the use of 'markobjectN') */ static int traverseproto (global_State *g, Proto *f) { - int i,nk,np; - if (f->cache && iswhite(f->cache)) - f->cache = NULL; /* allow cache to be collected */ - if (f->sp == NULL) - return sizeof(Proto); - nk = (f->k == f->sp->k && g != f->sp->l_G) ? 0 : f->sp->sizek; - np = (f->p == NULL) ? 0 : f->sp->sizep; - for (i = 0; i < nk; i++) /* mark literals */ + int i; + if (g != f->l_G) + return 0; + markobjectN(g, f->source); + for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < np; i++) /* mark nested protos */ + 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]); - return sizeof(Proto) + sizeof(Proto *) * np + - sizeof(TValue) * nk + - marksharedproto(g, f->sp); + 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; } @@ -719,10 +723,6 @@ static void freeobj (lua_State *L, GCObject *o) { 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: - luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); - break; case LUA_TLNGSTR: { luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; @@ -745,7 +745,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); */ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { global_State *g = G(L); - int ow = otherwhite(g); + int ow = otherwhite(g) | bitmask(SHAREBIT); /* shared object never dead */ int white = luaC_white(g); /* current white */ while (*p != NULL && count-- > 0) { GCObject *curr = *p; @@ -783,19 +783,6 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { ** ======================================================= */ -/* -** If possible, shrink string table -*/ -static void checkSizes (lua_State *L, global_State *g) { - if (g->gckind != KGC_EMERGENCY) { - l_mem olddebt = g->GCdebt; - 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(tofinalize(o)); @@ -986,7 +973,6 @@ void luaC_freeallobjects (lua_State *L) { sweepwholelist(L, &g->finobj); sweepwholelist(L, &g->allgc); sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ - lua_assert(g->strt.nuse == 0); } @@ -1057,7 +1043,7 @@ static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - g->GCmemtrav = g->strt.size * sizeof(GCObject*); + g->GCmemtrav = 0; restartcollection(g); g->gcstate = GCSpropagate; return g->GCmemtrav; @@ -1089,7 +1075,6 @@ static lu_mem singlestep (lua_State *L) { } case GCSswpend: { /* finish sweeps */ makewhite(g, g->mainthread); /* sweep main thread */ - checkSizes(L, g); g->gcstate = GCScallfin; return 0; } @@ -1099,6 +1084,7 @@ static lu_mem singlestep (lua_State *L) { return (n * GCFINALIZECOST); } else { /* emergency mode or no more finalizers */ + luaS_collect(g, 0); /* send short strings set to gc thread */ g->gcstate = GCSpause; /* finish collection */ return 0; } diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 425cd7cef..6977ff222 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -79,11 +79,13 @@ #define WHITE1BIT 1 /* object is white (type 1) */ #define BLACKBIT 2 /* object is black */ #define FINALIZEDBIT 3 /* object has been marked for finalization */ +#define SHAREBIT 4 /* object shared from other state */ /* bit 7 is currently used by tests (luaL_checkmemory) */ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) +/* short string is always white */ #define iswhite(x) testbits((x)->marked, WHITEBITS) #define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ @@ -91,9 +93,11 @@ #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) +#define isshared(x) testbit((x)->marked, SHAREBIT) +#define makeshared(x) l_setbit((x)->marked, SHAREBIT) #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) +#define isdead(g,v) isdeadm(otherwhite(g) | bitmask(SHAREBIT), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) #define gray2black(x) l_setbit((x)->marked, BLACKBIT) diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index b537d3923..c99e0f63a 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -307,7 +307,7 @@ typedef struct TString { unsigned int hash; union { size_t lnglen; /* length for long strings */ - struct TString *hnext; /* linked list for hash table */ + size_t ref; /* reference count for short strings */ } u; } TString; @@ -400,11 +400,15 @@ typedef struct LocVar { int endpc; /* first point where variable is dead */ } LocVar; -typedef struct SharedProto { + +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ - lu_byte sharedk; /* constants can be shared directly */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; @@ -413,25 +417,15 @@ typedef struct SharedProto { int sizelocvars; int linedefined; /* debug information */ int lastlinedefined; /* debug information */ - void *l_G; /* global state belongs to */ + 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 */ TString *source; /* used for debug information */ - TValue *k; /* Shared constants */ -} 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; + void *l_G; /* global state belongs to */ } Proto; diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 9e0af8bce..cc54de43c 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,15 +160,14 @@ 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++; } @@ -196,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->sp->locvars[idx]; + return &fs->f->locvars[idx]; } @@ -218,7 +217,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; } @@ -227,8 +226,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, @@ -238,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, fp, name); + luaC_objbarrier(fs->ls->L, f, name); return fs->nups++; } @@ -503,13 +501,13 @@ 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) + 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; } @@ -529,7 +527,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; @@ -544,42 +542,31 @@ 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); } -static lu_byte check_shortstring(const TValue *k, int sizek) { - int i; - for (i=0;iL; 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->sharedk = check_shortstring(f->k, fs->nk); - sp->k = f->k; - 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); @@ -755,8 +742,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 */ } /* }====================================================================== */ @@ -766,7 +753,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? */ @@ -797,7 +784,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) { @@ -807,7 +794,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); @@ -973,7 +960,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; @@ -1609,7 +1596,7 @@ static void statement (LexState *ls) { break; } } - lua_assert(ls->fs->f->sp->maxstacksize >= ls->fs->freereg && + lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && ls->fs->freereg >= ls->fs->nactvar); ls->fs->freereg = ls->fs->nactvar; /* free registers */ leavelevel(ls); @@ -1626,7 +1613,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 declared vararg */ + fs->f->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 */ @@ -1646,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 */ luaD_inctop(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 c1a76643c..d0cd5e75a 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -70,27 +70,6 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) -/* -** 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); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int makeseed (lua_State *L) { - char buff[4 * sizeof(size_t)]; - unsigned int h = luai_makeseed(); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, luaO_nilobject); /* global variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - - /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** invariant (and avoiding underflows in 'totalbytes') @@ -245,7 +224,7 @@ static void close_state (lua_State *L) { luaC_freeallobjects(L); /* collect all objects */ if (g->version) /* closing a fully built state? */ luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); + luaS_collect(g, 1); /* clear short strings */ freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ @@ -308,11 +287,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->frealloc = f; g->ud = ud; g->mainthread = L; - g->seed = makeseed(L); g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; - g->strt.size = g->strt.nuse = 0; - g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; g->version = NULL; @@ -328,6 +304,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; + g->strsave = NULL; + g->strmark = NULL; + g->strfix = NULL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 56b374100..5ccf7bacf 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -130,6 +130,8 @@ typedef struct CallInfo { #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) #define getoah(st) ((st) & CIST_OAH) +/* SSM (short string map) See lstring.c */ +struct ssm_ref; /* ** 'global state', shared by all threads of this state @@ -141,9 +143,7 @@ typedef struct global_State { 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 */ - stringtable strt; /* hash table for strings */ TValue l_registry; - unsigned int seed; /* randomized seed for hashes */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ @@ -169,6 +169,9 @@ typedef struct global_State { TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ + struct ssm_ref *strsave; + struct ssm_ref *strmark; + struct ssm_ref *strfix; } global_State; diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 095771e1e..5368bde06 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -9,7 +9,6 @@ #include "lprefix.h" - #include #include #include "lua.h" @@ -21,6 +20,9 @@ #include "lstate.h" #include "lstring.h" +static unsigned int STRSEED; + +#define STRFIXSIZE 64 #define MEMERRMSG "not enough memory" @@ -65,37 +67,6 @@ unsigned int luaS_hashlongstr (TString *ts) { } -/* -** resizes the string table -*/ -void luaS_resize (lua_State *L, int newsize) { - int i; - stringtable *tb = &G(L)->strt; - 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; - } - 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 */ - TString *hnext = p->u.hnext; /* save next */ - unsigned int h = lmod(p->hash, newsize); /* new position */ - p->u.hnext = tb->hash[h]; /* chain it */ - tb->hash[h] = p; - p = hnext; - } - } - 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, TString *); - } - tb->size = newsize; -} - - /* ** Clear API string cache. (Entries cannot be empty, so fill them with ** a non-collectable string.) @@ -104,11 +75,12 @@ void luaS_clearcache (global_State *g) { 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? */ + if (!isshared(g->strcache[i][j]) && iswhite(g->strcache[i][j])) /* will entry be collected? */ g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } +static struct ssm_ref * newref(int size); /* ** Initialize the string table and the string cache @@ -116,7 +88,8 @@ void luaS_clearcache (global_State *g) { void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + g->strsave = newref(MINSTRTABSIZE); + g->strmark = newref(MINSTRTABSIZE); /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ @@ -143,71 +116,12 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { return ts; } -static unsigned int LNGSTR_SEED; -static unsigned int make_lstr_seed() { - size_t buff[4]; - unsigned int h = time(NULL); - buff[0] = cast(size_t, h); - buff[1] = cast(size_t, &LNGSTR_SEED); - buff[2] = cast(size_t, &make_lstr_seed); - buff[3] = cast(size_t, &luaS_createlngstrobj); - return luaS_hash((const char*)buff, sizeof(buff), h); -} - TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, LNGSTR_SEED); + TString *ts = createstrobj(L, l, LUA_TLNGSTR, STRSEED); ts->u.lnglen = l; return ts; } - -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)->u.hnext; - *p = (*p)->u.hnext; /* remove element from its list */ - tb->nuse--; -} - - -/* -** checks whether short string exists and reuses it or creates a new one -*/ -static TString *queryshrstr (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)]; - 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)) { - /* found! */ - if (isdead(g, ts)) /* dead (but not collected yet)? */ - changewhite(ts); /* resurrect it */ - 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 */ - } - 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; - g->strt.nuse++; - return ts; -} - static TString *internshrstr (lua_State *L, const char *str, size_t l); /* @@ -268,29 +182,453 @@ Udata *luaS_newudata (lua_State *L, size_t s) { */ #include "rwlock.h" +#include "spinlock.h" #include "atomic.h" #include +#include -#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) #define SHRSTR_INITSIZE 0x10000 +/* prime is better for hash */ +#define VMHASHSLOTS 4093 + struct shrmap_slot { struct rwlock lock; TString *str; }; +struct ssm_ref { + TString **hash; + TString **array; + int nuse; /* number of elements */ + int hsize; + int asize; + int acap; +}; + +struct collect_queue { + struct collect_queue *next; + void * key; + struct ssm_ref *strsave; + struct ssm_ref *strmark; + struct ssm_ref *strfix; +}; + struct shrmap { struct rwlock lock; - int n; - int mask; + int rwslots; int total; int roslots; struct shrmap_slot * readwrite; struct shrmap_slot * readonly; + struct spinlock qlock; + struct collect_queue * head; + struct collect_queue * tail; + struct collect_queue * vm[VMHASHSLOTS]; }; static struct shrmap SSM; +#define ADD_SREF(ts) ATOM_INC(&((ts)->u.ref)) +#define DEC_SREF(ts) ATOM_DEC(&((ts)->u.ref)) +#define ZERO_SREF(ts) ((ts)->u.ref == 0) + +static struct ssm_ref * +newref(int size) { + /* size must be must be power of 2 */ + lua_assert( (size&(size-1))==0 ); + struct ssm_ref *r = (struct ssm_ref *)malloc(sizeof(*r)); + if (r == NULL) + return NULL; + TString **hash = (TString **)malloc(sizeof(TString *) * size); + if (hash == NULL) { + free(r); + return NULL; + } + memset(r, 0, sizeof(*r)); + memset(hash, 0, sizeof(TString *) * size); + r->hsize = size; + r->hash = hash; + return r; +} + +static void +expand_ref(struct ssm_ref *r, int changeref) { + int hsize = r->hsize * 2; + TString ** hash = (TString **)malloc(sizeof(TString *) * hsize); + if (hash == NULL) + return; + memset(hash, 0, sizeof(TString *) * hsize); + int i; + for (i=0;ihsize;i++) { + TString *s = r->hash[i]; + if (s) { + hash[lmod(s->hash, hsize)] = s; + } + } + free(r->hash); + r->hash = hash; + r->hsize = hsize; + + for (i=0;iasize;) { + TString *s = r->array[i]; + int slot = lmod(s->hash, hsize); + TString *hs = hash[slot]; + if (hs == s || hs == NULL) { + if (hs == NULL) + hash[slot] = s; + else { + --r->nuse; + if (changeref) + DEC_SREF(s); + } + --r->asize; + r->array[i] = r->array[r->asize]; + } else { + ++i; + } + } +} + +static void +insert_ref(struct ssm_ref *r, TString *s) { + if (r->asize >= r->acap) { + r->acap = r->asize * 2; + if (r->acap == 0) { + r->acap = r->hsize / 2; + } + TString ** array = (TString **)realloc(r->array, r->acap * sizeof(TString *)); + lua_assert(array != NULL); + r->array = array; + } + r->array[r->asize++] = s; +} + +static void +shrink_ref(struct ssm_ref *r) { + int hsize = r->hsize / 2; + if (hsize < MINSTRTABSIZE) + return; + TString ** hash = (TString **)malloc(sizeof(TString *) * hsize); + if (hash == NULL) + return; + memset(hash, 0, sizeof(TString *) * hsize); + int i; + for (i=0;ihsize;i++) { + TString *s = r->hash[i]; + if (s) { + int h = lmod(s->hash, hsize); + if (hash[h] == NULL) + hash[h] = s; + else + insert_ref(r, s); + } + } + free(r->hash); + r->hash = hash; + r->hsize = hsize; +} + +static void +markref(struct ssm_ref *r, TString *s, int changeref) { + unsigned int h = s->hash; + int slot = lmod(h, r->hsize); + TString * hs = r->hash[slot]; + if (hs == s) + return; + ++r->nuse; + if (r->nuse >= r->hsize && r->hsize <= MAX_INT/2) { + expand_ref(r, changeref); + slot = lmod(h, r->hsize); + hs = r->hash[slot]; + } + if (hs != NULL) { + if (hs == s) { + --r->nuse; + return; + } + insert_ref(r, hs); + } + r->hash[slot] = s; +} + +void +luaS_mark(global_State *g, TString *s) { + markref(g->strmark, s, 0); +} + +void +luaS_fix(global_State *g, TString *s) { + if (g->strfix == NULL) + g->strfix = newref(STRFIXSIZE); + markref(g->strfix, s, 0); +} + +static void +delete_ref(struct ssm_ref *r) { + if (r == NULL) + return; + free(r->hash); + free(r->array); + free(r); +} + +static void +delete_cqueue(struct collect_queue *cqueue) { + delete_ref(cqueue->strsave); + delete_ref(cqueue->strmark); + delete_ref(cqueue->strfix); + free(cqueue); +} + +static void +free_cqueue(struct collect_queue *cqueue) { + while (cqueue) { + struct collect_queue * next = cqueue->next; + delete_cqueue(cqueue); + cqueue = next; + } +} + +static void +remove_duplicate(struct ssm_ref *r, int decref) { + int i = 0; + while (i < r->asize) { + TString *s = r->array[i]; + if (r->hash[lmod(s->hash, r->hsize)] == s) { + --r->nuse; + --r->asize; + r->array[i] = r->array[r->asize]; + if (decref) { + DEC_SREF(s); + } + } else { + ++i; + } + } +} + +static struct ssm_ref * +mergeset(struct ssm_ref *set, struct ssm_ref * rset, int changeref) { + if (set == NULL) + return rset; + else if (rset == NULL) + return set; + int total = set->nuse + rset->nuse; + if (total * 2 <= set->hsize) { + shrink_ref(set); + } else if (total > set->hsize) { + expand_ref(set, changeref); + } + int i; + for (i=0;ihsize;i++) { + TString * s = rset->hash[i]; + if (s) { + insert_ref(set, s); + } + } + for (i=0;iasize;i++) { + TString * s = rset->array[i]; + insert_ref(set, s); + } + delete_ref(rset); + remove_duplicate(set, changeref); + return set; +} + +static void +merge_last(struct collect_queue * c) { + void *key = c->key; + int hash = (int)((uintptr_t)key % VMHASHSLOTS); + struct shrmap * s = &SSM; + struct collect_queue * slot = s->vm[hash]; + if (slot == NULL) { + s->vm[hash] = c; + c->next = NULL; + return; + } + + if (slot->key == key) { + // remove head + s->vm[hash] = slot->next; + } else { + for (;;) { + struct collect_queue * next = slot->next; + if (next == NULL) { + // not found, insert head + c->next = s->vm[hash]; + s->vm[hash] = c; + return; + } else if (next->key == key) { + // remove next + slot->next = next->next; + slot = next; + break; + } + slot = next; + } + } + // merge slot (last) into c + c->strsave = mergeset(slot->strsave, c->strsave, 1); + c->strfix = mergeset(slot->strfix, c->strfix, 0); + c->next = s->vm[hash]; + s->vm[hash] = c; + free(slot); +} + +static void +clear_vm(struct collect_queue * c) { + void *key = c->key; + int hash = (int)((uintptr_t)key % VMHASHSLOTS); + struct shrmap * s = &SSM; + struct collect_queue * slot = s->vm[hash]; + lua_assert(slot == c); + s->vm[hash] = slot->next; + delete_cqueue(slot); +} + +static int +compar_tstring(const void *a, const void *b) { + return memcmp(a,b, sizeof(TString *)); +} + +static void +sortset(struct ssm_ref *set) { + qsort(set->array, set->asize,sizeof(TString *),compar_tstring); +} + +static int +exist(struct ssm_ref *r, TString *s) { + int slot = lmod(s->hash, r->hsize); + TString *hs = r->hash[slot]; + if (hs == s) + return 1; + int begin = 0, end = r->asize-1; + while (begin <= end) { + int mid = (begin + end) / 2; + TString *t = r->array[mid]; + if (t == s) + return 1; + if (s > t) + begin = mid + 1; + else + end = mid - 1; + } + return 0; +} + +static int +collectref(struct collect_queue * c) { + int i; + int total = 0; + merge_last(c); + struct ssm_ref *mark = c->strmark; + struct ssm_ref * save = c->strsave; + c->strmark = NULL; + if (mark) { + struct ssm_ref * fix = c->strfix; + sortset(mark); + sortset(fix); + + for (i=0;ihsize;i++) { + TString * s = save->hash[i]; + if (s) { + if (!exist(mark, s) && !exist(fix, s)) { + save->hash[i] = NULL; + DEC_SREF(s); + ++total; + } + } + } + + for (i=0;iasize;) { + TString * s = save->array[i]; + if (!exist(mark, s) && !exist(fix, s)) { + --save->asize; + save->array[i] = save->array[save->asize]; + DEC_SREF(s); + ++total; + } else { + ++i; + } + } + delete_ref(mark); + } else { + for (i=0;ihsize;i++) { + TString * s = save->hash[i]; + if (s) { + DEC_SREF(s); + ++total; + } + } + for (i=0;iasize;i++) { + TString * s = save->array[i]; + DEC_SREF(s); + ++total; + } + clear_vm(c); + } + return total; +} + +static int +pow2size(struct ssm_ref *r) { + if (r->nuse <= MINSTRTABSIZE) + return MINSTRTABSIZE; + int hsize = r->hsize; + while (hsize / 2 > r->nuse) { + hsize /= 2; + } + return hsize; +} + +void +luaS_collect(global_State *g, int closed) { + if (closed) { + delete_ref(g->strmark); + g->strmark = NULL; + } + struct shrmap * s = &SSM; + struct collect_queue *cqueue = (struct collect_queue *)malloc(sizeof(*cqueue)); + if (cqueue == NULL) { + /* OOM, give up */ + return; + } + cqueue->key = g; + cqueue->strsave = g->strsave; + cqueue->strmark = g->strmark; + cqueue->strfix = g->strfix; + cqueue->next = NULL; + + g->strfix = NULL; + if (closed) { + g->strsave = NULL; + g->strmark = NULL; + } else { + g->strsave = newref(pow2size(g->strsave)); + g->strmark = newref(pow2size(g->strmark)); + } + + spinlock_lock(&s->qlock); + if (s->head) { + s->tail->next = cqueue; + s->tail = cqueue; + } else { + s->head = s->tail = cqueue; + } + spinlock_unlock(&s->qlock); +} + +static unsigned int make_str_seed() { + size_t buff[4]; + unsigned int h = time(NULL); + buff[0] = cast(size_t, h); + buff[1] = cast(size_t, &STRSEED); + buff[2] = cast(size_t, &make_str_seed); + buff[3] = cast(size_t, SHRSTR_INITSIZE); + return luaS_hash((const char*)buff, sizeof(buff), h); +} + static struct shrmap_slot * shrstr_newpage(int sz) { int i; @@ -311,7 +649,7 @@ shrstr_deletepage(struct shrmap_slot *s, int sz) { for (i=0;iu.hnext; + TString * next = (TString *)str->next; free(str); str = next; } @@ -324,12 +662,12 @@ static int shrstr_allocpage(struct shrmap * s, int osz, int sz, struct shrmap_slot * newpage) { if (s->readonly != NULL) return 0; - if ((s->mask + 1) != osz) + if (s->rwslots != osz) return 0; s->readonly = s->readwrite; s->readwrite = newpage; - s->roslots = s->mask + 1; - s->mask = sz - 1; + s->roslots = s->rwslots; + s->rwslots = sz; return 1; } @@ -340,13 +678,18 @@ shrstr_rehash(struct shrmap *s, int slotid) { rwlock_wlock(&slot->lock); TString *str = slot->str; while (str) { - TString * next = str->u.hnext; - int newslotid = str->hash & s->mask; - struct shrmap_slot *newslot = &s->readwrite[newslotid]; - rwlock_wlock(&newslot->lock); - str->u.hnext = newslot->str; - newslot->str = str; - rwlock_wunlock(&newslot->lock); + TString * next = (TString *)str->next; + if (ZERO_SREF(str)) { + free(str); + ATOM_DEC(&SSM.total); + } else { + int newslotid = lmod(str->hash, s->rwslots); + struct shrmap_slot *newslot = &s->readwrite[newslotid]; + rwlock_wlock(&newslot->lock); + str->next = (GCObject *)newslot->str; + newslot->str = str; + rwlock_wunlock(&newslot->lock); + } str = next; } @@ -363,17 +706,15 @@ shrstr_rehash(struct shrmap *s, int slotid) { 6. remove temporary readonly (writelock SSM) */ static void -shrstr_expandpage(int cap) { +expandssm() { struct shrmap * s = &SSM; if (s->readonly) return; - int osz = s->mask + 1; + int osz = s->rwslots; int sz = osz * 2; - while (sz < cap) { + if (sz < osz) { // overflow check - if (sz <= 0) - return; - sz = sz * 2; + return; } struct shrmap_slot * newpage = shrstr_newpage(sz); if (newpage == NULL) @@ -396,50 +737,105 @@ shrstr_expandpage(int cap) { shrstr_deletepage(oldpage, osz); } +/* call it in a separate thread */ +LUA_API int +luaS_collectssm(struct ssm_collect *info) { + struct shrmap * s = &SSM; + if (s->total * 5 / 4 > s->rwslots) { + expandssm(); + } + if (s->head) { + struct collect_queue * cqueue; + spinlock_lock(&s->qlock); + cqueue = s->head; + s->head = cqueue->next; + spinlock_unlock(&s->qlock); + if (cqueue) { + if (info) { + info->key = cqueue->key; + } + int n = collectref(cqueue); + if (info) { + info->n = n; + } + } + return 1; + } + return 0; +} + LUA_API void -luaS_initshr() { +luaS_initssm() { struct shrmap * s = &SSM; rwlock_init(&s->lock); - s->n = 0; - s->mask = SHRSTR_INITSIZE - 1; + s->rwslots = SHRSTR_INITSIZE; s->readwrite = shrstr_newpage(SHRSTR_INITSIZE); s->readonly = NULL; - LNGSTR_SEED = make_lstr_seed(); + s->head = NULL; + s->tail = NULL; + spinlock_init(&s->qlock); + STRSEED = make_str_seed(); } LUA_API void -luaS_exitshr() { +luaS_exitssm() { struct shrmap * s = &SSM; rwlock_wlock(&s->lock); - int sz = s->mask + 1; + int sz = s->rwslots; shrstr_deletepage(s->readwrite, sz); shrstr_deletepage(s->readonly, s->roslots); s->readwrite = NULL; s->readonly = NULL; + free_cqueue(s->head); + s->head = NULL; + s->tail = NULL; + int i; + for (i=0;ivm[i]); + s->vm[i] = NULL; + } } static TString * -find_string(TString *t, struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { +find_string(struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { TString *ts = slot->str; - if (t) { - while (ts) { - if (ts == t) - break; - ts = ts->u.hnext; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + ADD_SREF(ts); + break; } - } else { - while (ts) { - if (ts->hash == h && - ts->shrlen == l && - memcmp(str, ts+1, l) == 0) { - break; - } - ts = ts->u.hnext; + ts = (TString *)ts->next; + } + return ts; +} + +static TString * +find_and_collect(struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { + TString **ref = &slot->str; + TString *ts = *ref; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + ADD_SREF(ts); + break; + } + if (ZERO_SREF(ts)) { + *ref = (TString *)ts->next; + free(ts); + ts = *ref; + ATOM_DEC(&SSM.total); + } else { + ref = (TString **)&(ts->next); + ts = *ref; } } return ts; } + /* 1. readlock SSM 2. find string in readwrite page @@ -447,19 +843,19 @@ find_string(TString *t, struct shrmap_slot * slot, unsigned int h, const char *s 4. unlock SSM */ static TString * -query_string(TString *t, unsigned int h, const char *str, lu_byte l) { +query_string(unsigned int h, const char *str, lu_byte l) { struct shrmap * s = &SSM; TString *ts = NULL; rwlock_rlock(&s->lock); - struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; rwlock_rlock(&slot->lock); - ts = find_string(t, slot, h, str, l); + ts = find_string(slot, h, str, l); rwlock_runlock(&slot->lock); if (ts == NULL && s->readonly != NULL) { int mask = s->roslots - 1; slot = &s->readonly[h & mask]; rwlock_rlock(&slot->lock); - ts = find_string(t, slot, h, str, l); + ts = find_string(slot, h, str, l); rwlock_runlock(&slot->lock); } rwlock_runlock(&s->lock); @@ -471,9 +867,13 @@ new_string(unsigned int h, const char *str, lu_byte l) { size_t sz = sizelstring(l); TString *ts = malloc(sz); memset(ts, 0, sz); + setbits(ts->marked, WHITEBITS); + gray2black(ts); + makeshared(ts); ts->tt = LUA_TSHRSTR; ts->hash = h; ts->shrlen = l; + ts->u.ref = 1; memcpy(ts+1, str, l); return ts; } @@ -485,14 +885,19 @@ shrstr_exist(struct shrmap * s, unsigned int h, const char *str, lu_byte l) { unsigned int mask = s->roslots - 1; struct shrmap_slot *slot = &s->readonly[h & mask]; rwlock_rlock(&slot->lock); - found = find_string(NULL, slot, h, str, l); + found = find_string(slot, h, str, l); rwlock_runlock(&slot->lock); if (found) return found; } - struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; rwlock_wlock(&slot->lock); - found = find_string(NULL, slot, h, str, l); + if (s->readonly) { + // Don't collect during expanding + found = find_string(slot, h, str, l); + } else { + found = find_and_collect(slot, h, str, l); + } if (found) { rwlock_wunlock(&slot->lock); return found; @@ -511,9 +916,9 @@ add_string(unsigned int h, const char *str, lu_byte l) { // string is create by other thread, so free tmp free(tmp); } else { - struct shrmap_slot *slot = &s->readwrite[h & s->mask]; + struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; ts = tmp; - ts->u.hnext = slot->str; + ts->next = (GCObject *)slot->str; slot->str = ts; rwlock_wunlock(&slot->lock); ATOM_INC(&SSM.total); @@ -523,76 +928,20 @@ add_string(unsigned int h, const char *str, lu_byte l) { } 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(NULL, 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) { - struct shrmap * s = &SSM; - if (n < 0) { - if (-n > s->n) { - n = -s->n; - } +internshrstr(lua_State *L, const char *str, size_t l) { + TString *ts; + unsigned int h = luaS_hash(str, l, STRSEED); + ts = query_string(h, str, l); + if (ts == NULL) { + ts = add_string(h, str, l); } - ATOM_ADD(&s->n, n); - if (n > 0) { - int t = (s->total + s->n) * 5 / 4; - if (t > s->mask) { - shrstr_expandpage(t); - } - } -} - -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_string(ts, ts->hash, NULL, 0); - if (result) - return result; - h = luaS_hash(str, l, 0); - result = query_string(NULL, 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); + markref(G(L)->strsave, ts, 1); + return ts; } struct slotinfo { int len; - int size; + size_t size; }; static void @@ -603,12 +952,11 @@ getslot(struct shrmap_slot *s, struct slotinfo *info) { while (ts) { ++info->len; info->size += sizelstring(ts->shrlen); - ts = ts->u.hnext; + ts = (TString *)ts->next; } rwlock_runlock(&s->lock); } - struct variance { int count; double mean; @@ -625,8 +973,8 @@ variance_update(struct variance *v, int newValue_) { v->m2 += delta * delta2; } -LUA_API int -luaS_shrinfo(lua_State *L) { +LUA_API void +luaS_infossm(struct ssm_info *info) { struct slotinfo total; struct slotinfo tmp; memset(&total, 0, sizeof(total)); @@ -635,7 +983,7 @@ luaS_shrinfo(lua_State *L) { int slots = 0; rwlock_rlock(&s->lock); int i; - int sz = s->mask + 1; + int sz = s->rwslots; for (i=0;ireadwrite[i]; getslot(slot, &tmp); @@ -663,15 +1011,13 @@ luaS_shrinfo(lua_State *L) { } } rwlock_runlock(&s->lock); - lua_pushinteger(L, SSM.total); // count - lua_pushinteger(L, total.size); // total size - lua_pushinteger(L, total.len); // longest - lua_pushinteger(L, SSM.n); // space - lua_pushinteger(L, slots); // slots + info->total = SSM.total; + info->size = total.size; + info->longest = total.len; + info->slots = slots; if (v.count > 1) { - lua_pushnumber(L, v.m2 / v.count); // variance + info->variance = v.m2 / v.count; } else { - lua_pushnumber(L, 0); // variance + info->variance = 0; } - return 6; } diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index e6a38b885..22525adf9 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -36,10 +36,8 @@ 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); 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); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); @@ -47,10 +45,26 @@ LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); #define ENABLE_SHORT_STRING_TABLE -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); +struct ssm_info { + int total; + int longest; + int slots; + size_t size; + double variance; +}; + +struct ssm_collect { + void *key; + int n; +}; + +LUA_API void luaS_initssm(); +LUA_API void luaS_exitssm(); +LUA_API void luaS_infossm(struct ssm_info *info); +LUA_API int luaS_collectssm(struct ssm_collect *info); + +LUAI_FUNC void luaS_mark(global_State *g, TString *s); +LUAI_FUNC void luaS_fix(global_State *g, TString *s); +LUAI_FUNC void luaS_collect(global_State *g, int closed); #endif diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 232179674..cad0d9f4d 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -597,7 +597,7 @@ static int pmain (lua_State *L) { int main (int argc, char **argv) { int status, result; lua_State *L; - luaS_initshr(); /* init global short string table */ + luaS_initssm(); L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); @@ -610,7 +610,7 @@ int main (int argc, char **argv) { result = lua_toboolean(L, -1); /* get result */ report(L, status); lua_close(L); - luaS_exitshr(); + luaS_exitssm(); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 7bf639f1f..a944404bf 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -233,7 +233,8 @@ LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); LUA_API void (lua_pushboolean) (lua_State *L, int b); LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L); - +LUA_API void (lua_clonefunction) (lua_State *L, const void * fp); +LUA_API void (lua_sharefunction) (lua_State *L, int index); /* ** get functions (Lua -> stack) @@ -282,8 +283,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 @@ -460,11 +459,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-2018 Lua.org, PUC-Rio. diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 46dda035c..62a0509ba 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -150,9 +150,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; } } @@ -193,10 +193,10 @@ static int pmain(lua_State* L) int main(int argc, char* argv[]) { lua_State* L; + luaS_initssm(); 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); @@ -204,6 +204,7 @@ int main(int argc, char* argv[]) lua_pushlightuserdata(L,argv); if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); lua_close(L); + luaS_exitssm(); return EXIT_SUCCESS; } @@ -284,13 +285,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 Instruction* code=f->sp->code; - int pc,n=f->sp->sizecode; + const Instruction* code=f->code; + int pc,n=f->sizecode; for (pc=0; pcsource ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') @@ -417,9 +418,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/lualib.h b/3rd/lua/lualib.h index 422b9ea55..f5304aa0d 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -47,8 +47,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 860570371..7a67d75aa 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -104,7 +104,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; @@ -119,9 +119,7 @@ static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->k = luaM_newvector(S->L, n, TValue); - f->sp->k = f->k; - f->sp->sizek = n; - f->sp->sharedk = 1; + f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -141,8 +139,6 @@ static void LoadConstants (LoadState *S, Proto *f) { setivalue(o, LoadInteger(S)); break; case LUA_TSHRSTR: - f->sp->sharedk = 0; - //fall-through case LUA_TLNGSTR: setsvalue2n(S->L, o, LoadString(S)); break; @@ -157,17 +153,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); @@ -181,7 +177,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); @@ -203,8 +199,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 */ @@ -214,9 +209,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); } @@ -275,9 +270,9 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { cl = luaF_newLclosure(L, LoadByte(&S)); setclLvalue(L, L->top, cl); luaD_inctop(L); - cl->p = luaF_newproto(L, NULL); + cl->p = luaF_newproto(L); LoadFunction(&S, cl->p, NULL); - lua_assert(cl->nupvalues == cl->p->sp->sizeupvalues); + lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); return cl; } diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index f968ba019..a28888485 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -63,17 +63,7 @@ #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 @@ -612,27 +602,6 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { } -/* -** check whether cached closure in prototype 'p' may be reused, that is, -** whether there is a cached closure with the same upvalues needed by -** new closure to be created. -*/ -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 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->upvals[i]->v != v) - return NULL; /* wrong upvalue; cannot reuse closure */ - } - } - return c; /* return cached closure (or NULL if no cached closure) */ -} - - /* ** create a new Lua closure, push it in the stack, and initialize ** its upvalues. Note that the closure is not cached if prototype is @@ -641,8 +610,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; @@ -655,8 +624,6 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i]->refcount++; /* new closure is white, so we do not need a barrier here */ } - if (!isblack(p)) /* cache will not break GC invariant? */ - p->cache = ncl; /* save it on cache for reuse */ } @@ -1088,7 +1055,6 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { - lua_checksig(L); dojump(ci, i, 0); vmbreak; } @@ -1141,7 +1107,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) @@ -1156,7 +1121,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? */ @@ -1169,10 +1133,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); @@ -1181,14 +1145,14 @@ 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)->sp->maxstacksize); + lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; } vmcase(OP_RETURN) { int b = GETARG_B(i); - if (cl->p->sp->sizep > 0) luaF_close(L, base); + if (cl->p->sizep > 0) luaF_close(L, base); 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 */ @@ -1201,7 +1165,6 @@ 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 */ @@ -1268,7 +1231,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 */ @@ -1299,18 +1261,14 @@ void luaV_execute (lua_State *L) { } vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; - 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 */ + pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; /* required results */ 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 (n < 0) /* less arguments than parameters? */ n = 0; /* no vararg arguments */ if (b < 0) { /* B == 0? */ diff --git a/Makefile b/Makefile index 004aabb97..f2b7df0d3 100644 --- a/Makefile +++ b/Makefile @@ -69,6 +69,7 @@ LUA_CLIB_SKYNET = \ lua-stm.c \ lua-debugchannel.c \ lua-datasheet.c \ + lua-ssm.c \ \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 426acf18d..b9b07a611 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -4,7 +4,6 @@ #include #include "malloc_hook.h" -#include "luashrtbl.h" static int ltotal(lua_State *L) { @@ -36,13 +35,6 @@ ldump(lua_State *L) { return 0; } -static int -lexpandshrtbl(lua_State *L) { - int n = luaL_checkinteger(L, 1); - luaS_expandshr(n); - return 0; -} - static int lcurrent(lua_State *L) { lua_pushinteger(L, malloc_current_memory()); @@ -79,8 +71,6 @@ luaopen_skynet_memory(lua_State *L) { { "dumpinfo", ldumpinfo }, { "dump", ldump }, { "info", dump_mem_lua }, - { "ssinfo", luaS_shrinfo }, - { "ssexpand", lexpandshrtbl }, { "current", lcurrent }, { "dumpheap", ldumpheap }, { "profactive", lprofactive }, diff --git a/lualib-src/lua-ssm.c b/lualib-src/lua-ssm.c new file mode 100644 index 000000000..4988057dc --- /dev/null +++ b/lualib-src/lua-ssm.c @@ -0,0 +1,72 @@ +#define LUA_LIB + +#include +#include +#include + +#include "lstring.h" + +static int +linfo(lua_State *L) { + struct ssm_info info; + memset(&info, 0, sizeof(info)); + luaS_infossm(&info); + lua_createtable(L, 0, 5); + lua_pushinteger(L, info.total); + lua_setfield(L, -2, "n"); + lua_pushinteger(L, info.longest); + lua_setfield(L, -2, "longest"); + lua_pushinteger(L, info.slots); + lua_setfield(L, -2, "slots"); + lua_pushinteger(L, info.size); + lua_setfield(L, -2, "size"); + lua_pushnumber(L, info.variance); + lua_setfield(L, -2, "variance"); + + return 1; +} + +static int +lcollect(lua_State *L) { + int loop = lua_toboolean(L, 1); + if (loop) { + int n = 0; + struct ssm_collect info; + while (luaS_collectssm(&info)) { + n+=info.n; + } + lua_pushinteger(L, n); + return 1; + } else { + struct ssm_collect info; + int again = luaS_collectssm(&info); + if (again && lua_istable(L, 2)) { + lua_pushinteger(L, info.n); + lua_setfield(L, 2, "n"); + lua_pushlightuserdata(L, info.key); + lua_setfield(L, 2, "key"); + } + lua_pushboolean(L, again); + return 1; + } +} + +LUAMOD_API int +luaopen_skynet_ssm(lua_State *L) { + luaL_checkversion(L); + + luaL_Reg l[] = { + { "info", linfo }, + { "collect", lcollect }, + { NULL, NULL }, + }; + + luaL_newlib(L,l); + +#ifndef ENABLE_SHORT_STRING_TABLE + lua_pushboolean(L, 1); + lua_setfield(L, -2, "disable"); +#endif + return 1; +} + diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 3dabfd8a5..195544b7e 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,17 +1,15 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" require "skynet.manager" -- import skynet.launch, ... -local memory = require "skynet.memory" skynet.start(function() - 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) + skynet.newservice "garbagecollect" + local harbor_id = tonumber(skynet.getenv "harbor" or 0) if harbor_id == 0 then assert(standalone == nil) diff --git a/service/debug_console.lua b/service/debug_console.lua index 0323c2152..2ed44f79c 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -155,7 +155,6 @@ function COMMAND.help() debug = "debug address : debug a lua service", signal = "signal address sig", cmem = "Show C memory info", - shrtbl = "Show shared short string table info", ping = "ping address", call = "call address ...", trace = "trace address [proto] [on|off]", @@ -337,11 +336,6 @@ function COMMAND.cmem() return tmp end -function COMMAND.shrtbl() - local n, total, longest, space, slots, variance = memory.ssinfo() - return { n = n, total = total, longest = longest, space = space, slots = slots, average = n / slots, variace = variance } -end - function COMMAND.ping(address) address = adjust_address(address) local ti = skynet.now() diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h index a9485ccd6..0dcb27997 100644 --- a/skynet-src/luashrtbl.h +++ b/skynet-src/luashrtbl.h @@ -6,10 +6,23 @@ // 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() {} -static inline void luaS_exitshr() {} -static inline void luaS_expandshr(int n) {} +struct ssm_info { + int total; + int longest; + int slots; + size_t size; + double variance; +}; + +struct ssm_collect { + void *key; + int n; +}; + +static inline void luaS_initssm(); +static inline void luaS_exitssm(); +static inline void luaS_infossm(struct ssm_info *info) {} +static inline int luaS_collectssm(struct ssm_collect *info) { return 0; } #endif diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 3e394d11a..d9aef78a3 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -126,7 +126,7 @@ main(int argc, char *argv[]) { return 1; } - luaS_initshr(); + luaS_initssm(); skynet_globalinit(); skynet_env_init(); @@ -162,7 +162,7 @@ main(int argc, char *argv[]) { skynet_start(&config); skynet_globalexit(); - luaS_exitshr(); + luaS_exitssm(); return 0; } From 76b166f04af1b228d0a2a0c217f124fd0727b865 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Apr 2019 21:30:47 +0800 Subject: [PATCH 116/565] Add new sharetable --- 3rd/lua/lgc.c | 2 +- 3rd/lua/lstring.c | 2 +- 3rd/lua/lvm.c | 2 + 3rd/lua/lvm.h | 2 +- Makefile | 1 + lualib-src/lua-sharetable.c | 203 +++++++++++++++++++++++++++++++++++ lualib/skynet/sharetable.lua | 173 +++++++++++++++++++++++++++++ test/testsharetable.lua | 10 ++ 8 files changed, 392 insertions(+), 3 deletions(-) create mode 100644 lualib-src/lua-sharetable.c create mode 100644 lualib/skynet/sharetable.lua create mode 100644 test/testsharetable.lua diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index e56f29228..92d38f548 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1018,6 +1018,7 @@ static l_mem atomic (lua_State *L) { clearvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ + luaS_collect(g, 0); /* send short strings set to gc thread */ work += g->GCmemtrav; /* complete counting */ return work; /* estimate of memory marked by 'atomic' */ } @@ -1084,7 +1085,6 @@ static lu_mem singlestep (lua_State *L) { return (n * GCFINALIZECOST); } else { /* emergency mode or no more finalizers */ - luaS_collect(g, 0); /* send short strings set to gc thread */ g->gcstate = GCSpause; /* finish collection */ return 0; } diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 5368bde06..c9f30fde1 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -509,7 +509,7 @@ exist(struct ssm_ref *r, TString *s) { TString *t = r->array[mid]; if (t == s) return 1; - if (s > t) + if (memcmp(&s,&t,sizeof(TString *)) > 0) begin = mid + 1; else end = mid - 1; diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index a28888485..334cc9d8e 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -207,6 +207,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, const TValue *tm; /* '__newindex' metamethod */ if (slot != NULL) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ + if (isshared(h)) + luaG_typeerror(L, t, "change"); lua_assert(ttisnil(slot)); /* old value must be nil */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index a8f954f04..94b2a62d8 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -81,7 +81,7 @@ (!ttistable(t) \ ? (slot = NULL, 0) \ : (slot = f(hvalue(t), k), \ - ttisnil(slot) ? 0 \ + (ttisnil(slot) || isshared(hvalue(t))) ? 0 \ : (luaC_barrierback(L, hvalue(t), v), \ setobj2t(L, cast(TValue *,slot), v), \ 1))) diff --git a/Makefile b/Makefile index f2b7df0d3..10076babe 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ LUA_CLIB_SKYNET = \ lua-debugchannel.c \ lua-datasheet.c \ lua-ssm.c \ + lua-sharetable.c \ \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c new file mode 100644 index 000000000..720274aef --- /dev/null +++ b/lualib-src/lua-sharetable.c @@ -0,0 +1,203 @@ +#define LUA_LIB + +#include +#include +#include + +#include "lstring.h" +#include "lobject.h" +#include "ltable.h" +#include "lstate.h" +#include "lapi.h" +#include "lgc.h" + +#ifdef ENABLE_SHORT_STRING_TABLE + +static void +mark_shared(lua_State *L) { + if (lua_type(L, -1) != LUA_TTABLE) { + luaL_error(L, "Not a table, it's a %s.", lua_typename(L, lua_type(L, -1))); + } + Table * t = (Table *)lua_topointer(L, -1); + if (isshared(t)) + return; + makeshared(t); + luaL_checkstack(L, 4, NULL); + if (lua_getmetatable(L, -1)) { + luaL_error(L, "Can't share metatable"); + } + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + int i; + for (i=0;i<2;i++) { + int idx = -i-1; + int t = lua_type(L, idx); + switch (t) { + case LUA_TTABLE: + mark_shared(L); + break; + case LUA_TNUMBER: + case LUA_TBOOLEAN: + case LUA_TLIGHTUSERDATA: + break; + case LUA_TFUNCTION: + if (!lua_iscfunction(L, idx) || lua_getupvalue(L, idx, 1) != NULL) + luaL_error(L, "Invalid function"); + break; + case LUA_TSTRING: { + const char *str = lua_tostring(L, idx); + TString *ts = (TString *)(str - sizeof(UTString)); + makeshared(ts); + break; + } + default: + luaL_error(L, "Invalid type [%s]", lua_typename(L, t)); + break; + } + } + lua_pop(L, 1); + } +} + +static int +make_matrix(lua_State *L) { + // turn off gc , because marking shared will prevent gc mark. + lua_gc(L, LUA_GCSTOP, 0); + mark_shared(L); + Table * t = (Table *)lua_topointer(L, -1); + lua_pushlightuserdata(L, t); + return 1; +} + +static int +clone_table(lua_State *L) { + Table * t = (Table *)lua_touserdata(L, 1); + if (!isshared(t)) + return luaL_error(L, "Not a shared table"); + + sethvalue(L, L->top, t); + api_incr_top(L); + + return 1; +} + +struct state_ud { + lua_State *L; +}; + +static int +close_state(lua_State *L) { + struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); + if (ud->L) { + lua_close(ud->L); + ud->L = NULL; + } + return 0; +} + +static int +get_matrix(lua_State *L) { + struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); + if (ud->L) { + const void * v = lua_topointer(ud->L, 1); + lua_pushlightuserdata(L, (void *)v); + return 1; + } + return 0; +} + +static int +get_size(lua_State *L) { + struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); + if (ud->L) { + lua_Integer sz = lua_gc(ud->L, LUA_GCCOUNT, 0); + sz *= 1024; + sz += lua_gc(ud->L, LUA_GCCOUNTB, 0); + lua_pushinteger(L, sz); + } else { + lua_pushinteger(L, 0); + } + return 1; +} + + +static int +box_state(lua_State *L, lua_State *mL) { + struct state_ud *ud = (struct state_ud *)lua_newuserdata(L, sizeof(*ud)); + ud->L = mL; + if (luaL_newmetatable(L, "BOXMATRIXSTATE")) { + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, close_state); + lua_setfield(L, -2, "close"); + lua_pushcfunction(L, get_matrix); + lua_setfield(L, -2, "getptr"); + lua_pushcfunction(L, get_size); + lua_setfield(L, -2, "size"); + } + lua_setmetatable(L, -2); + + return 1; +} + +static int +load_matrixfile(lua_State *L) { + luaL_openlibs(L); + const char * source = (const char *)lua_touserdata(L, 1); + if (source[0] == '@') { + if (luaL_loadfilex_(L, source+1, NULL) || lua_pcall(L, 0, LUA_MULTRET, 0)) { + lua_error(L); + } + } else { + if (luaL_dostring(L, source) != LUA_OK) { + lua_error(L); + } + } + if (lua_gettop(L) == 0) { + luaL_error(L, "No table returns"); + } + lua_gc(L, LUA_GCCOLLECT, 0); + lua_pushcfunction(L, make_matrix); + lua_insert(L, -2); + lua_call(L, 1, 1); + return 1; +} + +static int +matrix_from_file(lua_State *L) { + lua_State *mL = luaL_newstate(); + if (mL == NULL) { + return luaL_error(L, "luaL_newstate failed"); + } + const char * source = luaL_checkstring(L, 1); + lua_pushcfunction(mL, load_matrixfile); + lua_pushlightuserdata(mL, (void *)source); + int ok = lua_pcall(mL, 1, 1, 0); + if (ok != LUA_OK) { + lua_pushstring(L, lua_tostring(mL, -1)); + lua_close(mL); + lua_error(L); + } + return box_state(L, mL); +} + +LUAMOD_API int +luaopen_skynet_sharetable_core(lua_State *L) { + luaL_checkversion(L); + luaL_Reg l[] = { + { "clone", clone_table }, + { "matrix", matrix_from_file }, + { NULL, NULL }, + }; + luaL_newlib(L, l); + return 1; +} + +#else + +LUAMOD_API int +luaopen_skynet_sharetable_core(lua_State *L) { + return luaL_error(L, "No share string table support"); +} + +#endif \ No newline at end of file diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua new file mode 100644 index 000000000..58aa56e52 --- /dev/null +++ b/lualib/skynet/sharetable.lua @@ -0,0 +1,173 @@ +local skynet = require "skynet" +local service = require "skynet.service" +local core = require "skynet.sharetable.core" + +local function sharetable_service() + local skynet = require "skynet" + local core = require "skynet.sharetable.core" + + local matrix = {} -- all the matrix + local files = {} -- filename : matrix + local clients = {} + + local sharetable = {} + + local function close_matrix(m) + if m == nil then + return + end + local ptr = m:getptr() + local ref = matrix[ptr] + if ref == nil or ref.count == 0 then + matrix[ptr] = nil + m:close() + end + end + + function sharetable.load(source, filename, datasource) + close_matrix(files[filename]) + if datasource == nil then + skynet.error("Load file : " .. filename) + datasource = "@" .. filename + else + skynet.error("Load chunk with name : " .. filename) + end + + local m = core.matrix(datasource) + files[filename] = m + skynet.ret() + end + + local function query_file(source, filename) + local m = files[filename] + local ptr = m:getptr() + local ref = matrix[ptr] + if ref == nil then + ref = { + filename = filename, + count = 0, + matrix = m, + refs = {}, + } + matrix[ptr] = ref + end + if ref.refs[source] == nil then + ref.refs[source] = true + local list = clients[source] + if not list then + clients[source] = { ptr } + else + table.insert(list, ptr) + end + ref.count = ref.count + 1 + end + return ptr + end + + function sharetable.query(source, filename) + local m = files[filename] + if m == nil then + skynet.ret() + end + local ptr = query_file(source, filename) + skynet.ret(skynet.pack(ptr)) + end + + function sharetable.close(source) + local list = clients[source] + if list then + for _, ptr in ipairs(list) do + local ref = matrix[ptr] + if ref and ref.refs[source] then + ref.refs[source] = nil + ref.count = ref.count - 1 + if ref.count == 0 then + if files[ref.filename] ~= ref.matrix then + -- It's a history version + skynet.error(string.format("Delete a version (%s) of %s", ptr, ref.filename)) + ref.matrix:close() + matrix[ptr] = nil + end + end + end + end + clients[source] = nil + end + -- no return + end + + skynet.dispatch("lua", function(_,source,cmd,...) + sharetable[cmd](source,...) + end) + + skynet.info_func(function() + local info = {} + + for filename, m in pairs(files) do + info[filename] = { + current = m:getptr(), + size = m:size(), + } + end + + local function address(refs) + local keys = {} + for addr in pairs(refs) do + table.insert(keys, skynet.address(addr)) + end + table.sort(keys) + return table.concat(keys, ",") + end + + for ptr, copy in pairs(matrix) do + local v = info[copy.filename] + local h = v.history + if h == nil then + h = {} + v.history = h + end + table.insert(h, string.format("%s [%d]: (%s)", copy.matrix:getptr(), copy.matrix:size(), address(copy.refs))) + end + for _, v in pairs(info) do + if v.history then + v.history = table.concat(v.history, "\n\t") + end + end + + return info + end) + +end + +local function load_service(t, key) + if key == "address" then + t.address = service.new("sharetable", sharetable_service) + return t.address + else + return nil + end +end + +local function report_close(t) + local addr = rawget(t, "address") + if addr then + skynet.send(addr, "lua", "close") + end +end + +local sharetable = setmetatable ( {} , { + __index = load_service, + __gc = report_close, +}) + +function sharetable.load(filename, source) + skynet.call(sharetable.address, "lua", "load", filename, source) +end + +function sharetable.query(filename) + local newptr = skynet.call(sharetable.address, "lua", "query", filename) + return core.clone(newptr) +end + +return sharetable + diff --git a/test/testsharetable.lua b/test/testsharetable.lua new file mode 100644 index 000000000..61f0ee37c --- /dev/null +++ b/test/testsharetable.lua @@ -0,0 +1,10 @@ +local skynet = require "skynet" +local sharetable = require "skynet.sharetable" + +skynet.start(function() + sharetable.load("test", "return { x=1,y={ 'hello world' },['hello world'] = true }") + local t = sharetable.query("test") + for k,v in pairs(t) do + print(k,v) + end +end) From 2bcc691c4d50b668bc1ee261d0ce5cb63327847b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 10:51:48 +0800 Subject: [PATCH 117/565] add gc service --- service/garbagecollect.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 service/garbagecollect.lua diff --git a/service/garbagecollect.lua b/service/garbagecollect.lua new file mode 100644 index 000000000..ab19aedc6 --- /dev/null +++ b/service/garbagecollect.lua @@ -0,0 +1,26 @@ +local skynet = require "skynet" +local ssm = require "skynet.ssm" + +local function ssm_info() + return ssm.info() +end + +local function collect() + local info = {} + while true do + while ssm.collect(false, info) do + skynet.error(string.format("Collect %d strings from %s", info.n, info.key)) + end +-- ssm.collect(true) + skynet.sleep(50) + end +end + +skynet.start(function() + if ssm.disable then + skynet.error "Short String Map (SSM) Disabled" + skynet.exit() + end + skynet.info_func(ssm_info) + skynet.fork(collect) +end) From 47d1047004a2e27b6b8a92eadbbc08e9d03a1a22 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 15:36:14 +0800 Subject: [PATCH 118/565] sweep garbage strings --- 3rd/lua/lstring.c | 100 +++++++++++++++++++++++++++++++++++-- 3rd/lua/lstring.h | 3 ++ lualib-src/lua-ssm.c | 6 +++ service/garbagecollect.lua | 8 +-- 4 files changed, 108 insertions(+), 9 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index c9f30fde1..48102b853 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -218,6 +218,7 @@ struct shrmap { struct rwlock lock; int rwslots; int total; + int garbage; int roslots; struct shrmap_slot * readwrite; struct shrmap_slot * readonly; @@ -517,6 +518,14 @@ exist(struct ssm_ref *r, TString *s) { return 0; } +static void +release_tstring(TString *s) { + DEC_SREF(s); + if (ZERO_SREF(s)) { + ATOM_INC(&SSM.garbage); + } +} + static int collectref(struct collect_queue * c) { int i; @@ -535,7 +544,7 @@ collectref(struct collect_queue * c) { if (s) { if (!exist(mark, s) && !exist(fix, s)) { save->hash[i] = NULL; - DEC_SREF(s); + release_tstring(s); ++total; } } @@ -546,7 +555,7 @@ collectref(struct collect_queue * c) { if (!exist(mark, s) && !exist(fix, s)) { --save->asize; save->array[i] = save->array[save->asize]; - DEC_SREF(s); + release_tstring(s); ++total; } else { ++i; @@ -557,13 +566,13 @@ collectref(struct collect_queue * c) { for (i=0;ihsize;i++) { TString * s = save->hash[i]; if (s) { - DEC_SREF(s); + release_tstring(s); ++total; } } for (i=0;iasize;i++) { TString * s = save->array[i]; - DEC_SREF(s); + release_tstring(s); ++total; } clear_vm(c); @@ -682,6 +691,7 @@ shrstr_rehash(struct shrmap *s, int slotid) { if (ZERO_SREF(str)) { free(str); ATOM_DEC(&SSM.total); + ATOM_DEC(&SSM.garbage); } else { int newslotid = lmod(str->hash, s->rwslots); struct shrmap_slot *newslot = &s->readwrite[newslotid]; @@ -737,6 +747,63 @@ expandssm() { shrstr_deletepage(oldpage, osz); } +static int +sweep_slot(struct shrmap *s, int i) { + struct shrmap_slot *slot = &s->readwrite[i]; + int n = 0; + TString *ts; + rwlock_rlock(&slot->lock); + ts = slot->str; + while (ts) { + if (ZERO_SREF(ts)) { + n = 1; + break; + } + ts = (TString *)ts->next; + } + rwlock_runlock(&slot->lock); + if (n == 0) + return 0; + + n = 0; + rwlock_wlock(&slot->lock); + TString **ref = &slot->str; + ts = *ref; + while (ts) { + if (ZERO_SREF(ts)) { + *ref = (TString *)ts->next; + free(ts); + ts = *ref; + ATOM_DEC(&SSM.total); + ATOM_DEC(&SSM.garbage); + ++n; + } else { + ref = (TString **)&(ts->next); + ts = *ref; + } + } + rwlock_wunlock(&slot->lock); + return n; +} + +static int +sweepssm() { + struct shrmap * s = &SSM; + rwlock_rlock(&s->lock); + if (s->readonly) { + rwlock_runlock(&s->lock); + return 0; + } + int sz = s->rwslots; + int i; + int n = 0; + for (i=0;ilock); + return n; +} + /* call it in a separate thread */ LUA_API int luaS_collectssm(struct ssm_collect *info) { @@ -744,6 +811,11 @@ luaS_collectssm(struct ssm_collect *info) { if (s->total * 5 / 4 > s->rwslots) { expandssm(); } + if (s->garbage > s->total / 8) { + info->sweep = sweepssm(); + } else { + info->sweep = 0; + } if (s->head) { struct collect_queue * cqueue; spinlock_lock(&s->qlock); @@ -819,6 +891,9 @@ find_and_collect(struct shrmap_slot * slot, unsigned int h, const char *str, lu_ if (ts->hash == h && ts->shrlen == l && memcmp(str, ts+1, l) == 0) { + if (ZERO_SREF(ts)) { + ATOM_INC(&SSM.garbage); + } ADD_SREF(ts); break; } @@ -827,6 +902,7 @@ find_and_collect(struct shrmap_slot * slot, unsigned int h, const char *str, lu_ free(ts); ts = *ref; ATOM_DEC(&SSM.total); + ATOM_DEC(&SSM.garbage); } else { ref = (TString **)&(ts->next); ts = *ref; @@ -941,7 +1017,9 @@ internshrstr(lua_State *L, const char *str, size_t l) { struct slotinfo { int len; + int garbage; size_t size; + size_t garbage_size; }; static void @@ -951,7 +1029,12 @@ getslot(struct shrmap_slot *s, struct slotinfo *info) { TString *ts = s->str; while (ts) { ++info->len; - info->size += sizelstring(ts->shrlen); + size_t sz = sizelstring(ts->shrlen); + if (ZERO_SREF(ts)) { + ++info->garbage; + info->garbage_size += sz; + } + info->size += sz; ts = (TString *)ts->next; } rwlock_runlock(&s->lock); @@ -992,6 +1075,8 @@ luaS_infossm(struct ssm_info *info) { total.len = tmp.len; } total.size += tmp.size; + total.garbage_size += tmp.garbage_size; + total.garbage += tmp.garbage; variance_update(&v, tmp.len); ++slots; } @@ -1005,7 +1090,10 @@ luaS_infossm(struct ssm_info *info) { if (tmp.len > total.len) { total.len = tmp.len; } + // may double counting, but it's only an info total.size += tmp.size; + total.garbage_size += tmp.garbage_size; + total.garbage += tmp.garbage; variance_update(&v, tmp.len); } } @@ -1015,6 +1103,8 @@ luaS_infossm(struct ssm_info *info) { info->size = total.size; info->longest = total.len; info->slots = slots; + info->garbage = SSM.garbage; + info->garbage_size = total.garbage_size; if (v.count > 1) { info->variance = v.m2 / v.count; } else { diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 22525adf9..72ee14a00 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -49,13 +49,16 @@ struct ssm_info { int total; int longest; int slots; + int garbage; size_t size; + size_t garbage_size; double variance; }; struct ssm_collect { void *key; int n; + int sweep; }; LUA_API void luaS_initssm(); diff --git a/lualib-src/lua-ssm.c b/lualib-src/lua-ssm.c index 4988057dc..fac37850c 100644 --- a/lualib-src/lua-ssm.c +++ b/lualib-src/lua-ssm.c @@ -20,6 +20,10 @@ linfo(lua_State *L) { lua_setfield(L, -2, "slots"); lua_pushinteger(L, info.size); lua_setfield(L, -2, "size"); + lua_pushinteger(L, info.garbage); + lua_setfield(L, -2, "garbage"); + lua_pushinteger(L, info.garbage_size); + lua_setfield(L, -2, "garbage_size"); lua_pushnumber(L, info.variance); lua_setfield(L, -2, "variance"); @@ -43,6 +47,8 @@ lcollect(lua_State *L) { if (again && lua_istable(L, 2)) { lua_pushinteger(L, info.n); lua_setfield(L, 2, "n"); + lua_pushinteger(L, info.sweep); + lua_setfield(L, 2, "sweep"); lua_pushlightuserdata(L, info.key); lua_setfield(L, 2, "key"); } diff --git a/service/garbagecollect.lua b/service/garbagecollect.lua index ab19aedc6..f2a00f060 100644 --- a/service/garbagecollect.lua +++ b/service/garbagecollect.lua @@ -8,10 +8,10 @@ end local function collect() local info = {} while true do - while ssm.collect(false, info) do - skynet.error(string.format("Collect %d strings from %s", info.n, info.key)) - end --- ssm.collect(true) +-- while ssm.collect(false, info) do +-- skynet.error(string.format("Collect %d strings from %s, sweep %d", info.n, info.key, info.sweep)) +-- end + ssm.collect(true) skynet.sleep(50) end end From 3a0981661c1fb0ec9d4c15ac730afd53511423ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 15:59:21 +0800 Subject: [PATCH 119/565] don't use SSM.garbage --- 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 48102b853..7ad062f95 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1103,7 +1103,7 @@ luaS_infossm(struct ssm_info *info) { info->size = total.size; info->longest = total.len; info->slots = slots; - info->garbage = SSM.garbage; + info->garbage = total.garbage; info->garbage_size = total.garbage_size; if (v.count > 1) { info->variance = v.m2 / v.count; From e3c5d63a97b9124567975fd84b34630ea2867272 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Apr 2019 19:17:49 +0800 Subject: [PATCH 120/565] setmetatable raise error --- 3rd/lua/lapi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index ffea5e531..0422cbc32 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -858,6 +858,8 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { } switch (ttnov(obj)) { case LUA_TTABLE: { + if (isshared(hvalue(obj))) + luaG_runerror(L, "can't setmetatable to shared table"); hvalue(obj)->metatable = mt; if (mt) { luaC_objbarrier(L, gcvalue(obj), mt); @@ -1041,10 +1043,8 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { } LUA_API void lua_sharefunction (lua_State *L, int index) { - if (!lua_isfunction(L,index) || lua_iscfunction(L,index)) { - lua_pushstring(L, "Only Lua function can share"); - lua_error(L); - } + if (!lua_isfunction(L,index) || lua_iscfunction(L,index)) + luaG_runerror(L, "Only Lua function can share"); LClosure *f = cast(LClosure *, lua_topointer(L, index)); luaF_shareproto(f->p); } From 68e94b2792f8bb19aa521b5acd6b987aef4bf824 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 23 Apr 2019 19:58:42 +0800 Subject: [PATCH 121/565] add sharetable.loadtable --- lualib-src/lua-sharetable.c | 46 +++++++++++++++++++++++++++++------- lualib/skynet/sharetable.lua | 45 +++++++++++++++++++++++++++-------- test/testsharetable.lua | 8 ++++++- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 720274aef..7b6c2671b 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -145,17 +145,15 @@ load_matrixfile(lua_State *L) { luaL_openlibs(L); const char * source = (const char *)lua_touserdata(L, 1); if (source[0] == '@') { - if (luaL_loadfilex_(L, source+1, NULL) || lua_pcall(L, 0, LUA_MULTRET, 0)) { + if (luaL_loadfilex_(L, source+1, NULL) != LUA_OK) lua_error(L); - } } else { - if (luaL_dostring(L, source) != LUA_OK) { + if (luaL_loadstring(L, source) != LUA_OK) lua_error(L); - } - } - if (lua_gettop(L) == 0) { - luaL_error(L, "No table returns"); } + lua_replace(L, 1); + if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK) + lua_error(L); lua_gc(L, LUA_GCCOLLECT, 0); lua_pushcfunction(L, make_matrix); lua_insert(L, -2); @@ -170,9 +168,41 @@ matrix_from_file(lua_State *L) { return luaL_error(L, "luaL_newstate failed"); } const char * source = luaL_checkstring(L, 1); + int top = lua_gettop(L); lua_pushcfunction(mL, load_matrixfile); lua_pushlightuserdata(mL, (void *)source); - int ok = lua_pcall(mL, 1, 1, 0); + if (top > 1) { + if (!lua_checkstack(mL, top + 1)) { + return luaL_error(L, "Too many argument %d", top); + } + int i; + for (i=2;i<=top;i++) { + switch(lua_type(L, i)) { + case LUA_TBOOLEAN: + lua_pushboolean(mL, lua_toboolean(L, i)); + break; + case LUA_TNUMBER: + if (lua_isinteger(L, i)) { + lua_pushinteger(mL, lua_tointeger(L, i)); + } else { + lua_pushnumber(mL, lua_tonumber(L, i)); + } + break; + case LUA_TLIGHTUSERDATA: + lua_pushlightuserdata(mL, lua_touserdata(L, i)); + break; + case LUA_TFUNCTION: + if (lua_iscfunction(L, i) && lua_getupvalue(L, i, 1) == NULL) { + lua_pushcfunction(mL, lua_tocfunction(L, i)); + break; + } + return luaL_argerror(L, i, "Only support light C function"); + default: + return luaL_argerror(L, i, "Type invalid"); + } + } + } + int ok = lua_pcall(mL, top, 1, 0); if (ok != LUA_OK) { lua_pushstring(L, lua_tostring(mL, -1)); lua_close(mL); diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 58aa56e52..a86533bcf 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -24,17 +24,33 @@ local function sharetable_service() end end - function sharetable.load(source, filename, datasource) + function sharetable.loadfile(source, filename, ...) close_matrix(files[filename]) - if datasource == nil then - skynet.error("Load file : " .. filename) - datasource = "@" .. filename - else - skynet.error("Load chunk with name : " .. filename) - end + local m = core.matrix("@" .. filename, ...) + files[filename] = m + skynet.ret() + end + + function sharetable.loadstring(source, filename, datasource, ...) + close_matrix(files[filename]) + local m = core.matrix(datasource, ...) + files[filename] = m + skynet.ret() + end - local m = core.matrix(datasource) + local function loadtable(filename, ptr, len) + close_matrix(files[filename]) + local m = core.matrix([[ + local unpack, ptr, len = ... + return unpack(ptr, len) + ]], skynet.unpack, ptr, len) files[filename] = m + end + + function sharetable.loadtable(source, filename, ptr, len) + local ok, err = pcall(loadtable, filename, ptr, len) + skynet.trash(ptr, len) + assert(ok, err) skynet.ret() end @@ -160,8 +176,17 @@ local sharetable = setmetatable ( {} , { __gc = report_close, }) -function sharetable.load(filename, source) - skynet.call(sharetable.address, "lua", "load", filename, source) +function sharetable.loadfile(filename, ...) + skynet.call(sharetable.address, "lua", "loadfile", filename, ...) +end + +function sharetable.loadstring(filename, source, ...) + skynet.call(sharetable.address, "lua", "loadstring", filename, source, ...) +end + +function sharetable.loadtable(filename, tbl) + assert(type(tbl) == "table") + skynet.call(sharetable.address, "lua", "loadtable", filename, skynet.pack(tbl)) end function sharetable.query(filename) diff --git a/test/testsharetable.lua b/test/testsharetable.lua index 61f0ee37c..016a286be 100644 --- a/test/testsharetable.lua +++ b/test/testsharetable.lua @@ -2,7 +2,13 @@ local skynet = require "skynet" local sharetable = require "skynet.sharetable" skynet.start(function() - sharetable.load("test", "return { x=1,y={ 'hello world' },['hello world'] = true }") + -- You can also use sharetable.loadfile / sharetable.loadstring + sharetable.loadtable ("test", { x=1,y={ 'hello world' },['hello world'] = true }) + local t = sharetable.query("test") + for k,v in pairs(t) do + print(k,v) + end + sharetable.loadstring ("test", "return { ... }", 1,2,3) local t = sharetable.query("test") for k,v in pairs(t) do print(k,v) From 505da68cb64452a3f4b1a24153f5b9b20ca86c71 Mon Sep 17 00:00:00 2001 From: hong Date: Sat, 27 Apr 2019 16:33:41 +0800 Subject: [PATCH 122/565] Should not link shared table to gray --- 3rd/lua/lgc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 92d38f548..235ee896e 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -276,9 +276,10 @@ static void reallymarkobject (global_State *g, GCObject *o) { break; } case LUA_TTABLE: { - if (!isshared(o)) + if (!isshared(o)) { white2gray(o); linkgclist(gco2t(o), g->gray); + } break; } case LUA_TTHREAD: { From bed5f878dc55c01a671fe7b457c0b677c369ce02 Mon Sep 17 00:00:00 2001 From: hong Date: Sun, 5 May 2019 10:05:23 +0800 Subject: [PATCH 123/565] use ttnov in shareproto --- 3rd/lua/lfunc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index eaf100be6..20dfc1516 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -157,7 +157,7 @@ void luaF_shareproto (Proto *f) { return; MAKESHARED(f->source); for (i = 0; i < f->sizek; i++) { - if (ttype(&f->k[i]) == LUA_TSTRING) + if (ttnov(&f->k[i]) == LUA_TSTRING) MAKESHARED(tsvalue(&f->k[i])); } for (i = 0; i < f->sizeupvalues; i++) From ff9b0538a42e613b0fcddc4ce03076f7ad0bb14a Mon Sep 17 00:00:00 2001 From: hong Date: Sun, 5 May 2019 10:36:01 +0800 Subject: [PATCH 124/565] Reserve SHAREDBIT in GC --- 3rd/lua/lgc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 235ee896e..6370db748 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -65,7 +65,7 @@ */ #define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) #define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & maskcolors) | (x->marked & bitmask(SHAREBIT)) | luaC_white(g))) #define white2gray(x) resetbits(x->marked, WHITEBITS) #define black2gray(x) resetbit(x->marked, BLACKBIT) @@ -756,7 +756,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | white); + curr->marked = cast_byte((marked & maskcolors) | (marked & bitmask(SHAREBIT)) |white); p = &curr->next; /* go to next element */ } } From a8c18287863826467f2fdc6666f89b63b7b9e391 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 11 May 2019 22:11:27 +0800 Subject: [PATCH 125/565] fix #1011 --- lualib/skynet/sharetable.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index a86533bcf..14f013e61 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -84,6 +84,7 @@ local function sharetable_service() local m = files[filename] if m == nil then skynet.ret() + return end local ptr = query_file(source, filename) skynet.ret(skynet.pack(ptr)) @@ -191,7 +192,9 @@ end function sharetable.query(filename) local newptr = skynet.call(sharetable.address, "lua", "query", filename) - return core.clone(newptr) + if newptr then + return core.clone(newptr) + end end return sharetable From 1b60c522b02c07bfdd6e283979acff5c784d1b15 Mon Sep 17 00:00:00 2001 From: hong Date: Sat, 25 May 2019 15:32:51 +0800 Subject: [PATCH 126/565] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E8=AE=A1=E6=95=B0BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lstring.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 7ad062f95..1fcf8ca3a 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -335,8 +335,11 @@ markref(struct ssm_ref *r, TString *s, int changeref) { unsigned int h = s->hash; int slot = lmod(h, r->hsize); TString * hs = r->hash[slot]; - if (hs == s) + if (hs == s){ + if (changeref) + DEC_SREF(s); return; + } ++r->nuse; if (r->nuse >= r->hsize && r->hsize <= MAX_INT/2) { expand_ref(r, changeref); @@ -346,6 +349,8 @@ markref(struct ssm_ref *r, TString *s, int changeref) { if (hs != NULL) { if (hs == s) { --r->nuse; + if (changeref) + DEC_SREF(s); return; } insert_ref(r, hs); From fafc4cad34c45c99cd494608b00339af9bdbc67c Mon Sep 17 00:00:00 2001 From: hong Date: Mon, 27 May 2019 16:34:27 +0800 Subject: [PATCH 127/565] do not makeshared to LUA_TSHRSTR --- 3rd/lua/lstring.c | 1 - lualib-src/lua-sharetable.c | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 1fcf8ca3a..d4add290f 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -950,7 +950,6 @@ new_string(unsigned int h, const char *str, lu_byte l) { memset(ts, 0, sz); setbits(ts->marked, WHITEBITS); gray2black(ts); - makeshared(ts); ts->tt = LUA_TSHRSTR; ts->hash = h; ts->shrlen = l; diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 7b6c2671b..68a224d48 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -47,7 +47,8 @@ mark_shared(lua_State *L) { case LUA_TSTRING: { const char *str = lua_tostring(L, idx); TString *ts = (TString *)(str - sizeof(UTString)); - makeshared(ts); + if(ts->tt==LUA_TLNGSTR) + makeshared(ts); break; } default: From da87df6b86461417d769cd16bccf0a0a7756eda5 Mon Sep 17 00:00:00 2001 From: hong Date: Mon, 27 May 2019 16:18:18 +0800 Subject: [PATCH 128/565] =?UTF-8?q?=E6=9C=89=E6=A6=82=E7=8E=87=E5=87=BA?= =?UTF-8?q?=E7=8E=B0total=E4=B8=BA0=E7=9A=84=E6=97=B6=E5=80=99garbage?= =?UTF-8?q?=E4=B8=8D=E4=B8=BA0=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lstring.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index d4add290f..91db72ea4 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -230,9 +230,10 @@ struct shrmap { static struct shrmap SSM; -#define ADD_SREF(ts) ATOM_INC(&((ts)->u.ref)) -#define DEC_SREF(ts) ATOM_DEC(&((ts)->u.ref)) +#define ADD_SREF(ts) do {if(ATOM_INC(&((ts)->u.ref))==1) ATOM_DEC(&SSM.garbage);}while(0) +#define DEC_SREF(ts) do {if(ATOM_DEC(&((ts)->u.ref))==0) ATOM_INC(&SSM.garbage);}while(0) #define ZERO_SREF(ts) ((ts)->u.ref == 0) +#define FREE_SREF(ts) do {free(ts);ATOM_DEC(&SSM.total);ATOM_DEC(&SSM.garbage);}while(0) static struct ssm_ref * newref(int size) { @@ -526,9 +527,6 @@ exist(struct ssm_ref *r, TString *s) { static void release_tstring(TString *s) { DEC_SREF(s); - if (ZERO_SREF(s)) { - ATOM_INC(&SSM.garbage); - } } static int @@ -664,7 +662,7 @@ shrstr_deletepage(struct shrmap_slot *s, int sz) { TString *str = s[i].str; while (str) { TString * next = (TString *)str->next; - free(str); + FREE_SREF(str); str = next; } } @@ -694,9 +692,7 @@ shrstr_rehash(struct shrmap *s, int slotid) { while (str) { TString * next = (TString *)str->next; if (ZERO_SREF(str)) { - free(str); - ATOM_DEC(&SSM.total); - ATOM_DEC(&SSM.garbage); + FREE_SREF(str); } else { int newslotid = lmod(str->hash, s->rwslots); struct shrmap_slot *newslot = &s->readwrite[newslotid]; @@ -777,10 +773,8 @@ sweep_slot(struct shrmap *s, int i) { while (ts) { if (ZERO_SREF(ts)) { *ref = (TString *)ts->next; - free(ts); + FREE_SREF(ts); ts = *ref; - ATOM_DEC(&SSM.total); - ATOM_DEC(&SSM.garbage); ++n; } else { ref = (TString **)&(ts->next); @@ -896,18 +890,13 @@ find_and_collect(struct shrmap_slot * slot, unsigned int h, const char *str, lu_ if (ts->hash == h && ts->shrlen == l && memcmp(str, ts+1, l) == 0) { - if (ZERO_SREF(ts)) { - ATOM_INC(&SSM.garbage); - } ADD_SREF(ts); break; } if (ZERO_SREF(ts)) { *ref = (TString *)ts->next; - free(ts); + FREE_SREF(ts); ts = *ref; - ATOM_DEC(&SSM.total); - ATOM_DEC(&SSM.garbage); } else { ref = (TString **)&(ts->next); ts = *ref; From 64d99c1bd553d808ea27b6867a8c4d5d60cb3739 Mon Sep 17 00:00:00 2001 From: hong Date: Tue, 4 Jun 2019 16:07:15 +0800 Subject: [PATCH 129/565] =?UTF-8?q?nuse=E8=AE=A1=E6=95=B0=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lstring.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 91db72ea4..54515610b 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -353,10 +353,12 @@ markref(struct ssm_ref *r, TString *s, int changeref) { if (changeref) DEC_SREF(s); return; + } else { + insert_ref(r, s); } - insert_ref(r, hs); + } else { + r->hash[slot] = s; } - r->hash[slot] = s; } void @@ -431,12 +433,12 @@ mergeset(struct ssm_ref *set, struct ssm_ref * rset, int changeref) { for (i=0;ihsize;i++) { TString * s = rset->hash[i]; if (s) { - insert_ref(set, s); + markref(set, s, changeref); } } for (i=0;iasize;i++) { TString * s = rset->array[i]; - insert_ref(set, s); + markref(set, s, changeref); } delete_ref(rset); remove_duplicate(set, changeref); From 9ffa0c6e30752f10fc58b0a02ec54019d2aadbf7 Mon Sep 17 00:00:00 2001 From: hong Date: Tue, 4 Jun 2019 18:53:23 +0800 Subject: [PATCH 130/565] =?UTF-8?q?markref=E5=86=B2=E7=AA=81=E6=97=B6?= =?UTF-8?q?=E5=80=99=E5=88=B7=E6=96=B0=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lstring.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 54515610b..92ecf5842 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -353,12 +353,10 @@ markref(struct ssm_ref *r, TString *s, int changeref) { if (changeref) DEC_SREF(s); return; - } else { - insert_ref(r, s); } - } else { - r->hash[slot] = s; + insert_ref(r, hs); } + r->hash[slot] = s; } void From 82f3e80e0622f67400a797322b57ba5956a473dc Mon Sep 17 00:00:00 2001 From: hong Date: Tue, 4 Jun 2019 19:42:01 +0800 Subject: [PATCH 131/565] =?UTF-8?q?collectref=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=87=8F=E5=B0=91nuse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lstring.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 92ecf5842..3a646d04c 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -524,11 +524,6 @@ exist(struct ssm_ref *r, TString *s) { return 0; } -static void -release_tstring(TString *s) { - DEC_SREF(s); -} - static int collectref(struct collect_queue * c) { int i; @@ -547,7 +542,8 @@ collectref(struct collect_queue * c) { if (s) { if (!exist(mark, s) && !exist(fix, s)) { save->hash[i] = NULL; - release_tstring(s); + --save->nuse; + DEC_SREF(s); ++total; } } @@ -557,8 +553,9 @@ collectref(struct collect_queue * c) { TString * s = save->array[i]; if (!exist(mark, s) && !exist(fix, s)) { --save->asize; + --save->nuse; save->array[i] = save->array[save->asize]; - release_tstring(s); + DEC_SREF(s); ++total; } else { ++i; @@ -569,13 +566,13 @@ collectref(struct collect_queue * c) { for (i=0;ihsize;i++) { TString * s = save->hash[i]; if (s) { - release_tstring(s); + DEC_SREF(s); ++total; } } for (i=0;iasize;i++) { TString * s = save->array[i]; - release_tstring(s); + DEC_SREF(s); ++total; } clear_vm(c); From a04e6b5b2923a657669ca23ef51c470dd3b5f6d7 Mon Sep 17 00:00:00 2001 From: hong Date: Thu, 6 Jun 2019 11:13:19 +0800 Subject: [PATCH 132/565] lua_checksig LUA_CACHELIB --- 3rd/lua/lua.h | 5 +++++ 3rd/lua/lualib.h | 2 ++ 3rd/lua/lvm.c | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index a944404bf..675bc0ff0 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -459,6 +459,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-2018 Lua.org, PUC-Rio. diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index f5304aa0d..422b9ea55 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -47,6 +47,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); diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 334cc9d8e..a2393d50b 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 @@ -1057,6 +1067,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { + lua_checksig(L); dojump(ci, i, 0); vmbreak; } @@ -1109,6 +1120,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) @@ -1123,6 +1135,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? */ @@ -1167,6 +1180,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 */ @@ -1233,6 +1247,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 ca4f549f0da2828b5d96fab688da39a32859c58a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Jun 2019 14:55:57 +0800 Subject: [PATCH 133/565] free all objects in matrix --- 3rd/lua/lgc.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 6370db748..dfe120445 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -733,8 +733,13 @@ static void freeobj (lua_State *L, GCObject *o) { } -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); +static void sweepwholelist (lua_State *L, GCObject **p) { + while (*p != NULL) { + GCObject *curr = *p; + *p = curr->next; /* remove 'curr' from list */ + freeobj(L, curr); /* erase 'curr' */ + } +} /* From 7fed93859366a27d2eeebedf9c45d5b33ce5f5ec Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Jun 2019 16:58:57 +0800 Subject: [PATCH 134/565] mark all short string shared, see #1027 --- 3rd/lua/lstring.c | 2 +- lualib-src/lua-sharetable.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 3a646d04c..34feb0f67 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -75,7 +75,6 @@ void luaS_clearcache (global_State *g) { int i, j; for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { - if (!isshared(g->strcache[i][j]) && iswhite(g->strcache[i][j])) /* will entry be collected? */ g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } @@ -936,6 +935,7 @@ new_string(unsigned int h, const char *str, lu_byte l) { memset(ts, 0, sz); setbits(ts->marked, WHITEBITS); gray2black(ts); + makeshared(ts); ts->tt = LUA_TSHRSTR; ts->hash = h; ts->shrlen = l; diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 68a224d48..7b6c2671b 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -47,8 +47,7 @@ mark_shared(lua_State *L) { case LUA_TSTRING: { const char *str = lua_tostring(L, idx); TString *ts = (TString *)(str - sizeof(UTString)); - if(ts->tt==LUA_TLNGSTR) - makeshared(ts); + makeshared(ts); break; } default: From b45384cc9f9e78c10cfbc607dcbe11cdcf86dd1c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Jun 2019 17:55:45 +0800 Subject: [PATCH 135/565] short string in matrix should be in fixed set --- 3rd/lua/lstring.c | 1 - lualib-src/lua-sharetable.c | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 34feb0f67..b130682fd 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -935,7 +935,6 @@ new_string(unsigned int h, const char *str, lu_byte l) { memset(ts, 0, sz); setbits(ts->marked, WHITEBITS); gray2black(ts); - makeshared(ts); ts->tt = LUA_TSHRSTR; ts->hash = h; ts->shrlen = l; diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 7b6c2671b..fbb56132b 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -47,7 +47,10 @@ mark_shared(lua_State *L) { case LUA_TSTRING: { const char *str = lua_tostring(L, idx); TString *ts = (TString *)(str - sizeof(UTString)); - makeshared(ts); + if(ts->tt == LUA_TLNGSTR) + makeshared(ts); + else + luaS_fix(G(L), ts); break; } default: From 24b333a5e96aeb1fb3ec6f0f304ac9d514551184 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Jun 2019 20:01:10 +0800 Subject: [PATCH 136/565] add api lua_sharestring --- 3rd/lua/lapi.c | 12 ++++++++++++ 3rd/lua/lua.h | 1 + lualib-src/lua-sharetable.c | 12 ++---------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 0422cbc32..e2415d621 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1049,6 +1049,18 @@ LUA_API void lua_sharefunction (lua_State *L, int index) { luaF_shareproto(f->p); } +LUA_API void lua_sharestring (lua_State *L, int index) { + const char *str = lua_tostring(L, index); + if (str == NULL) + luaG_runerror(L, "need a string to share"); + + TString *ts = (TString *)(str - sizeof(UTString)); + if(ts->tt == LUA_TLNGSTR) + makeshared(ts); + else + luaS_fix(G(L), ts); +} + LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 675bc0ff0..0595e75ec 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -235,6 +235,7 @@ LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L); LUA_API void (lua_clonefunction) (lua_State *L, const void * fp); LUA_API void (lua_sharefunction) (lua_State *L, int index); +LUA_API void (lua_sharestring) (lua_State *L, int index); /* ** get functions (Lua -> stack) diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index fbb56132b..0dbb52560 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -6,8 +6,6 @@ #include "lstring.h" #include "lobject.h" -#include "ltable.h" -#include "lstate.h" #include "lapi.h" #include "lgc.h" @@ -44,15 +42,9 @@ mark_shared(lua_State *L) { if (!lua_iscfunction(L, idx) || lua_getupvalue(L, idx, 1) != NULL) luaL_error(L, "Invalid function"); break; - case LUA_TSTRING: { - const char *str = lua_tostring(L, idx); - TString *ts = (TString *)(str - sizeof(UTString)); - if(ts->tt == LUA_TLNGSTR) - makeshared(ts); - else - luaS_fix(G(L), ts); + case LUA_TSTRING: + lua_sharestring(L, idx); break; - } default: luaL_error(L, "Invalid type [%s]", lua_typename(L, t)); break; From 5a0f68999ee6972f535fd18cf2492c1fea478c15 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Jun 2019 11:04:20 +0800 Subject: [PATCH 137/565] fix #1032 --- 3rd/lua/lstring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index b130682fd..20bc9f5da 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -445,7 +445,7 @@ mergeset(struct ssm_ref *set, struct ssm_ref * rset, int changeref) { static void merge_last(struct collect_queue * c) { void *key = c->key; - int hash = (int)((uintptr_t)key % VMHASHSLOTS); + int hash = (int)((size_t)key % VMHASHSLOTS); struct shrmap * s = &SSM; struct collect_queue * slot = s->vm[hash]; if (slot == NULL) { @@ -485,7 +485,7 @@ merge_last(struct collect_queue * c) { static void clear_vm(struct collect_queue * c) { void *key = c->key; - int hash = (int)((uintptr_t)key % VMHASHSLOTS); + int hash = (int)((size_t)key % VMHASHSLOTS); struct shrmap * s = &SSM; struct collect_queue * slot = s->vm[hash]; lua_assert(slot == c); From 6f0e88dc59d93fa20f255f8715a1f6bf180e6ffa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 15 Jun 2019 00:29:05 +0800 Subject: [PATCH 138/565] bugfix, See issue #1027 --- 3rd/lua/lfunc.c | 1 + 3rd/lua/lgc.c | 6 ++++-- 3rd/lua/lstring.c | 1 + lualib-src/lua-sharetable.c | 8 ++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 20dfc1516..0141f98a6 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -155,6 +155,7 @@ void luaF_shareproto (Proto *f) { int i; if (f == NULL) return; + makeshared(f); MAKESHARED(f->source); for (i = 0; i < f->sizek; i++) { if (ttnov(&f->k[i]) == LUA_TSTRING) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index dfe120445..a3cd66540 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -266,8 +266,10 @@ static void reallymarkobject (global_State *g, GCObject *o) { break; } case LUA_TLCL: { - white2gray(o); - linkgclist(gco2lcl(o), g->gray); + if (!isshared(o)) { + white2gray(o); + linkgclist(gco2lcl(o), g->gray); + } break; } case LUA_TCCL: { diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 20bc9f5da..4bec4a7c2 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -935,6 +935,7 @@ new_string(unsigned int h, const char *str, lu_byte l) { memset(ts, 0, sz); setbits(ts->marked, WHITEBITS); gray2black(ts); + makeshared(ts); ts->tt = LUA_TSHRSTR; ts->hash = h; ts->shrlen = l; diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 0dbb52560..18ec781bf 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -39,8 +39,12 @@ mark_shared(lua_State *L) { case LUA_TLIGHTUSERDATA: break; case LUA_TFUNCTION: - if (!lua_iscfunction(L, idx) || lua_getupvalue(L, idx, 1) != NULL) - luaL_error(L, "Invalid function"); + if (lua_getupvalue(L, idx, 1) != NULL) { + luaL_error(L, "Invalid function with upvalue"); + } else if (!lua_iscfunction(L, idx)) { + LClosure *f = (LClosure *)lua_topointer(L, idx); + makeshared(f); + } break; case LUA_TSTRING: lua_sharestring(L, idx); From 8e2facbfb8cb7ded4e916786d087c455ddabfc96 Mon Sep 17 00:00:00 2001 From: lwkienun Date: Sat, 15 Jun 2019 12:11:37 +0800 Subject: [PATCH 139/565] fix crash of sharedata by incref before sharedated.monitor return cobj to client. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crash occr analise: 1.sharedated.update call many times in a short time(eg:3 times) to change a same name conf value to a->b->c->d. 2.sharedated sending 3 response to monitoring clients。a->b and b->c and c->d. 3.if b is destroyed and it's memory is reallocated and rewrited when client process the first message a->b, client's call lbox will crash. solution: conf b must be keeped before message(a->b) is processed by inc it's ref. Any return value from host to client must use incref before send to client. --- lualib/skynet/sharedata.lua | 1 + service/sharedatad.lua | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lualib/skynet/sharedata.lua b/lualib/skynet/sharedata.lua index b955bd90a..be62f67de 100644 --- a/lualib/skynet/sharedata.lua +++ b/lualib/skynet/sharedata.lua @@ -18,6 +18,7 @@ local function monitor(name, obj, cobj) break end sd.update(obj, newobj) + skynet.send(service, "lua", "confirm" , newobj) end if cache[name] == obj then cache[name] = nil diff --git a/service/sharedatad.lua b/service/sharedatad.lua index d74394937..622180f17 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -118,6 +118,7 @@ function CMD.update(name, t, ...) if watch then sharedata.host.markdirty(oldcobj) for _,response in pairs(watch) do + sharedata.host.incref(newobj) response(true, newobj) end end @@ -138,6 +139,7 @@ end function CMD.monitor(name, obj) local v = assert(pool[name]) if obj ~= v.obj then + sharedata.host.incref(v.obj) return v.obj end From 6803f45aa1b09608666889f55392139a87d401c9 Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 14 Jun 2019 22:22:21 +0800 Subject: [PATCH 140/565] add sharetable update --- lualib/skynet/sharetable.lua | 192 ++++++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 14f013e61..d5db143d0 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -190,12 +190,202 @@ function sharetable.loadtable(filename, tbl) skynet.call(sharetable.address, "lua", "loadtable", filename, skynet.pack(tbl)) end + +local RECORD = {} function sharetable.query(filename) local newptr = skynet.call(sharetable.address, "lua", "query", filename) if newptr then - return core.clone(newptr) + local t = core.clone(newptr) + local map = RECORD[filename] + if not map then + map = {} + RECORD[filename] = map + end + map[t] = true + return t end end + +local pairs = pairs +local type = type +local assert = assert +local next = next +local rawset = rawset +local getuservalue = debug.getuservalue +local setuservalue = debug.setuservalue +local getupvalue = debug.getupvalue +local setupvalue = debug.setupvalue +local getlocal = debug.getlocal +local setlocal = debug.setlocal + +local NILOBJ = {} +local function insert_replace(old_t, new_t, replace_map) + for k, ov in pairs(old_t) do + if type(ov) == "table" then + local nv = new_t[k] + if nv == nil then + nv = NILOBJ + end + assert(replace_map[ov] == nil) + replace_map[ov] = nv + nv = type(nv) == "table" and nv or NILOBJ + insert_replace(ov, nv, replace_map) + end + end + replace_map[old_t] = new_t + return replace_map +end + + +local function resolve_replace(replace_map) + local match = {} + local record_map = {} + + local function getnv(v) + local nv = replace_map[v] + if nv then + if nv == NILOBJ then + return nil + end + return nv + end + assert(false) + end + + local function match_value(v) + assert(v ~= nil) + local tv = type(v) + local f = match[tv] + if record_map[v] then + return + end + + if f then + record_map[v] = true + f(v) + end + end + + local function match_mt(v) + local mt = getmetatable(v) + if mt then + local nv = replace_map[mt] + if nv then + nv = getnv(mt) + setmetatable(t, nv) + else + match_value(mt) + end + end + end + + local function match_table(t) + for k,v in next, t do + local nv = replace_map[v] + if nv then + nv = getnv(v) + rawset(t, k, nv) + else + match_value(v) + end + end + match_mt(t) + end + + local function match_userdata(u) + local uv = getuservalue(u) + local nv = replace_map[uv] + if nv then + nv = getnv(uv) + setuservalue(u, nv) + end + match_mt(u) + end + + local function match_funcinfo(info) + local func = info.func + local nups = info.nups + for i=1,nups do + local name, upv = getupvalue(func, i) + local nv = replace_map[upv] + if nv then + nv = getnv(upv) + setupvalue(func, i, nv) + elseif upv then + match_value(upv) + end + end + + local level = info.level + local curco = info.curco or coroutine.running() + if not level then + return + end + local i = 1 + while true do + local name, v = getlocal(curco, level, i) + if name == nil then + break + end + if replace_map[v] then + local nv = getnv(v) + setlocal(curco, level, i, nv) + elseif v then + match_value(v) + end + i = i + 1 + end + end + + local function match_function(f) + local info = debug.getinfo(f) + match_funcinfo(info) + end + + local function match_thread(co) + local level = 1 + while true do + local info = debug.getinfo(co, level) + if not info then + break + end + info.level = level + info.curco = co + match_funcinfo(info) + level = level + 1 + end + end + + match["table"] = match_table + match["function"] = match_function + match["userdata"] = match_userdata + match["thread"] = match_thread + + local root = debug.getregistry() + assert(replace_map[root] == nil) + match_table(root) +end + + +function sharetable.update(...) + local names = {...} + local replace_map = {} + for _, name in ipairs(names) do + local map = RECORD[name] + if map then + local new_t = sharetable.query(name) + for old_t,_ in pairs(map) do + if old_t ~= new_t then + insert_replace(old_t, new_t, replace_map) + end + end + RECORD[name] = nil + end + end + + resolve_replace(replace_map) +end + return sharetable From e9581adfc4c51b6533ffff0ccd2eb4d1c360bf22 Mon Sep 17 00:00:00 2001 From: zixun Date: Sat, 15 Jun 2019 11:26:40 +0800 Subject: [PATCH 141/565] pass sharedtable match --- lualib-src/lua-sharetable.c | 12 ++++++++++++ lualib/skynet/sharetable.lua | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 0dbb52560..f45086d97 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -54,6 +54,17 @@ mark_shared(lua_State *L) { } } +static int +lis_sharedtable(lua_State* L) { + int b = 0; + if(lua_type(L, 1) == LUA_TTABLE) { + Table * t = (Table *)lua_topointer(L, 1); + b = isshared(t); + } + lua_pushboolean(L, b); + return 1; +} + static int make_matrix(lua_State *L) { // turn off gc , because marking shared will prevent gc mark. @@ -212,6 +223,7 @@ luaopen_skynet_sharetable_core(lua_State *L) { luaL_Reg l[] = { { "clone", clone_table }, { "matrix", matrix_from_file }, + { "is_sharedtable", lis_sharedtable }, { NULL, NULL }, }; luaL_newlib(L, l); diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index d5db143d0..208e035df 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local service = require "skynet.service" local core = require "skynet.sharetable.core" +local is_sharedtable = core.is_sharedtable local function sharetable_service() local skynet = require "skynet" @@ -218,6 +219,7 @@ local getupvalue = debug.getupvalue local setupvalue = debug.setupvalue local getlocal = debug.getlocal local setlocal = debug.setlocal +local getinfo = debug.getinfo local NILOBJ = {} local function insert_replace(old_t, new_t, replace_map) @@ -257,7 +259,7 @@ local function resolve_replace(replace_map) assert(v ~= nil) local tv = type(v) local f = match[tv] - if record_map[v] then + if record_map[v] or is_sharedtable(v) then return end @@ -339,14 +341,14 @@ local function resolve_replace(replace_map) end local function match_function(f) - local info = debug.getinfo(f) + local info = getinfo(f, "uf") match_funcinfo(info) end local function match_thread(co) local level = 1 while true do - local info = debug.getinfo(co, level) + local info = getinfo(co, level, "uf") if not info then break end From 744fbea7ee9da868b2551d25ffc1d8a623fb1fc5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Jun 2019 10:19:17 +0800 Subject: [PATCH 142/565] update jemalloc 5.2.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 896ed3a8b..b0b3e49a5 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 896ed3a8b3f41998d4fb4d625d30ac63ef2d51fb +Subproject commit b0b3e49a54ec29e32636f4577d9d5a896d67fd20 From c80b46241270fcf495b677b6665e72485be8a66a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Jun 2019 11:21:01 +0800 Subject: [PATCH 143/565] remove l_G from Proto --- 3rd/lua/lapi.c | 7 +------ 3rd/lua/lfunc.c | 1 - 3rd/lua/lgc.c | 2 -- 3rd/lua/lobject.h | 1 - 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index e2415d621..7500bc793 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1018,13 +1018,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LUA_API void lua_clonefunction (lua_State *L, const void * fp) { LClosure *cl; LClosure *f = cast(LClosure *, fp); + api_check(L, isshared(gcvalue(f->p)), "Not a shared proto"); lua_lock(L); - if (f->p->l_G == G(L)) { - setclLvalue(L,L->top,f); - api_incr_top(L); - lua_unlock(L); - return; - } cl = luaF_newLclosure(L,f->nupvalues); setclLvalue(L,L->top,cl); api_incr_top(L); diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 0141f98a6..2cc9a6ce2 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -117,7 +117,6 @@ Proto *luaF_newproto (lua_State *L) { f->linedefined = 0; f->lastlinedefined = 0; f->source = NULL; - f->l_G = G(L); return f; } diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index a3cd66540..757dbd534 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -496,8 +496,6 @@ static lu_mem traversetable (global_State *g, Table *h) { */ static int traverseproto (global_State *g, Proto *f) { int i; - if (g != f->l_G) - return 0; markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index c99e0f63a..71f55b260 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -425,7 +425,6 @@ typedef struct Proto { Upvaldesc *upvalues; /* upvalue information */ TString *source; /* used for debug information */ GCObject *gclist; - void *l_G; /* global state belongs to */ } Proto; From 7032f21d18542877e44b8e5d67f7a05c86078b9f Mon Sep 17 00:00:00 2001 From: hong Date: Mon, 17 Jun 2019 21:14:26 +0800 Subject: [PATCH 144/565] bugfix Proto is gcobject --- 3rd/lua/lapi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 7500bc793..c319f6127 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1018,7 +1018,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LUA_API void lua_clonefunction (lua_State *L, const void * fp) { LClosure *cl; LClosure *f = cast(LClosure *, fp); - api_check(L, isshared(gcvalue(f->p)), "Not a shared proto"); + api_check(L, isshared(f->p), "Not a shared proto"); lua_lock(L); cl = luaF_newLclosure(L,f->nupvalues); setclLvalue(L,L->top,cl); From 5d26fb3f18487805b61ec65538241e32bf870950 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 18 Jun 2019 14:09:36 +0800 Subject: [PATCH 145/565] remove ssm and add string id --- 3rd/lua/lapi.c | 17 +- 3rd/lua/lauxlib.c | 2 +- 3rd/lua/lfunc.c | 11 +- 3rd/lua/lgc.c | 60 +- 3rd/lua/lgc.h | 7 +- 3rd/lua/lobject.h | 5 +- 3rd/lua/lstate.c | 7 +- 3rd/lua/lstate.h | 6 +- 3rd/lua/lstring.c | 1042 ++++------------------------------- 3rd/lua/lstring.h | 36 +- 3rd/lua/lua.c | 6 +- 3rd/lua/lua.h | 2 + 3rd/lua/luac.c | 3 - Makefile | 1 - lualib-src/lua-sharetable.c | 12 +- lualib-src/lua-ssm.c | 78 --- service/bootstrap.lua | 2 - service/garbagecollect.lua | 26 - skynet-src/luashrtbl.h | 29 - skynet-src/skynet_main.c | 3 - 20 files changed, 183 insertions(+), 1172 deletions(-) delete mode 100644 lualib-src/lua-ssm.c delete mode 100644 service/garbagecollect.lua delete mode 100644 skynet-src/luashrtbl.h diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index c319f6127..a4282db60 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1050,10 +1050,19 @@ LUA_API void lua_sharestring (lua_State *L, int index) { luaG_runerror(L, "need a string to share"); TString *ts = (TString *)(str - sizeof(UTString)); - if(ts->tt == LUA_TLNGSTR) - makeshared(ts); - else - luaS_fix(G(L), ts); + luaS_share(ts); +} + +LUA_API void lua_clonetable(lua_State *L, const void * tp) { + Table *t = cast(Table *, tp); + + if (!isshared(t)) + luaG_runerror(L, "Not a shared table"); + + lua_lock(L); + sethvalue(L, L->top, t); + api_incr_top(L); + lua_unlock(L); } LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 58c6d996b..6b2f98f2d 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1103,7 +1103,7 @@ save(const char *key, const void * proto) { } else { lua_pop(L,2); } - + SPIN_UNLOCK(&CC) return result; } diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 2cc9a6ce2..260f64e93 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -19,6 +19,7 @@ #include "lmem.h" #include "lobject.h" #include "lstate.h" +#include "lstring.h" @@ -148,22 +149,20 @@ const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { return NULL; /* not found */ } -#define MAKESHARED(x) if ((x) && (x)->tt == LUA_TLNGSTR) makeshared(x) - void luaF_shareproto (Proto *f) { int i; if (f == NULL) return; makeshared(f); - MAKESHARED(f->source); + luaS_share(f->source); for (i = 0; i < f->sizek; i++) { if (ttnov(&f->k[i]) == LUA_TSTRING) - MAKESHARED(tsvalue(&f->k[i])); + luaS_share(tsvalue(&f->k[i])); } for (i = 0; i < f->sizeupvalues; i++) - MAKESHARED(f->upvalues[i].name); + luaS_share(f->upvalues[i].name); for (i = 0; i < f->sizelocvars; i++) - MAKESHARED(f->locvars[i].varname); + luaS_share(f->locvars[i].varname); for (i = 0; i < f->sizep; i++) luaF_shareproto(f->p[i]); } diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 757dbd534..08395dece 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -193,12 +193,7 @@ void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); - if (o->tt == LUA_TSHRSTR) { - luaS_fix(g, gco2ts(o)); - return; - } - if (g->allgc != o) - return; /* if object is not 1st in 'allgc' list, it is in global short string table */ + 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 */ @@ -239,22 +234,22 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { */ static void reallymarkobject (global_State *g, GCObject *o) { reentry: + if (isshared(o)) + return; + white2gray(o); switch (o->tt) { case LUA_TSHRSTR: { - luaS_mark(g, gco2ts(o)); + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); break; } case LUA_TLNGSTR: { - if (!isshared(o)) { - white2gray(o); - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); - } + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); break; } case LUA_TUSERDATA: { TValue uvalue; - white2gray(o); markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ gray2black(o); g->GCmemtrav += sizeudata(gco2u(o)); @@ -266,34 +261,23 @@ static void reallymarkobject (global_State *g, GCObject *o) { break; } case LUA_TLCL: { - if (!isshared(o)) { - white2gray(o); - linkgclist(gco2lcl(o), g->gray); - } + linkgclist(gco2lcl(o), g->gray); break; } case LUA_TCCL: { - white2gray(o); linkgclist(gco2ccl(o), g->gray); break; } case LUA_TTABLE: { - if (!isshared(o)) { - white2gray(o); - linkgclist(gco2t(o), g->gray); - } + linkgclist(gco2t(o), g->gray); break; } case LUA_TTHREAD: { - white2gray(o); linkgclist(gco2th(o), g->gray); break; } case LUA_TPROTO: { - if (!isshared(o)) { - white2gray(o); - linkgclist(gco2p(o), g->gray); - } + linkgclist(gco2p(o), g->gray); break; } default: lua_assert(0); break; @@ -724,6 +708,10 @@ static void freeobj (lua_State *L, GCObject *o) { 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: + luaS_remove(L, gco2ts(o)); /* remove it from hash table */ + luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); + break; case LUA_TLNGSTR: { luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; @@ -789,6 +777,19 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { ** ======================================================= */ +/* +** If possible, shrink string table +*/ +static void checkSizes (lua_State *L, global_State *g) { + if (g->gckind != KGC_EMERGENCY) { + l_mem olddebt = g->GCdebt; + 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(tofinalize(o)); @@ -979,6 +980,7 @@ void luaC_freeallobjects (lua_State *L) { sweepwholelist(L, &g->finobj); sweepwholelist(L, &g->allgc); sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ + lua_assert(g->strt.nuse == 0); } @@ -1024,7 +1026,6 @@ static l_mem atomic (lua_State *L) { clearvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ - luaS_collect(g, 0); /* send short strings set to gc thread */ work += g->GCmemtrav; /* complete counting */ return work; /* estimate of memory marked by 'atomic' */ } @@ -1050,7 +1051,7 @@ static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - g->GCmemtrav = 0; + g->GCmemtrav = g->strt.size * sizeof(GCObject*); restartcollection(g); g->gcstate = GCSpropagate; return g->GCmemtrav; @@ -1082,6 +1083,7 @@ static lu_mem singlestep (lua_State *L) { } case GCSswpend: { /* finish sweeps */ makewhite(g, g->mainthread); /* sweep main thread */ + checkSizes(L, g); g->gcstate = GCScallfin; return 0; } diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 6977ff222..55467ef38 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -85,7 +85,6 @@ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) -/* short string is always white */ #define iswhite(x) testbits((x)->marked, WHITEBITS) #define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ @@ -120,15 +119,15 @@ #define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v)) && !isshared(gcvalue(v))) ? \ luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) #define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v)) && !isshared(gcvalue(v))) ? \ luaC_barrierback_(L,p) : cast_void(0)) #define luaC_objbarrier(L,p,o) ( \ - (isblack(p) && iswhite(o)) ? \ + (isblack(p) && iswhite(o) && !isshared(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) #define luaC_upvalbarrier(L,uv) ( \ diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 71f55b260..948e97040 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -189,7 +189,7 @@ typedef struct lua_TValue { #define checkliveness(L,obj) \ lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)) || isshared(gcvalue(obj))))) /* Macros to set values */ @@ -305,9 +305,10 @@ typedef struct TString { lu_byte extra; /* reserved words for short strings; "has hash" for longs */ lu_byte shrlen; /* length for short strings */ unsigned int hash; + size_t id; /* id for short strings */ union { size_t lnglen; /* length for long strings */ - size_t ref; /* reference count for short strings */ + struct TString *hnext; /* linked list for hash table */ } u; } TString; diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index d0cd5e75a..4e3029b32 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -224,7 +224,7 @@ static void close_state (lua_State *L) { luaC_freeallobjects(L); /* collect all objects */ if (g->version) /* closing a fully built state? */ luai_userstateclose(L); - luaS_collect(g, 1); /* clear short strings */ + luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ @@ -289,6 +289,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->mainthread = L; g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; + g->strt.size = g->strt.nuse = 0; + g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; g->version = NULL; @@ -304,9 +306,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; - g->strsave = NULL; - g->strmark = NULL; - g->strfix = NULL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 5ccf7bacf..3f029fe3c 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -130,8 +130,6 @@ typedef struct CallInfo { #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) #define getoah(st) ((st) & CIST_OAH) -/* SSM (short string map) See lstring.c */ -struct ssm_ref; /* ** 'global state', shared by all threads of this state @@ -143,6 +141,7 @@ typedef struct global_State { 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 */ + stringtable strt; /* hash table for strings */ TValue l_registry; lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ @@ -169,9 +168,6 @@ typedef struct global_State { TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ - struct ssm_ref *strsave; - struct ssm_ref *strmark; - struct ssm_ref *strfix; } global_State; diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 4bec4a7c2..a9ff3e159 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -9,8 +9,10 @@ #include "lprefix.h" + #include #include + #include "lua.h" #include "ldebug.h" @@ -19,10 +21,10 @@ #include "lobject.h" #include "lstate.h" #include "lstring.h" +#include "atomic.h" static unsigned int STRSEED; - -#define STRFIXSIZE 64 +static size_t STRID = 0; #define MEMERRMSG "not enough memory" @@ -47,6 +49,24 @@ int luaS_eqlngstr (TString *a, TString *b) { (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ } +int luaS_eqshrstr (TString *a, TString *b) { + lu_byte len = a->shrlen; + lua_assert(b->tt == LUA_TSHRSTR); + int r = len == b->shrlen && (memcmp(getstr(a), getstr(b), len) == 0); + if (r) { + if (a->id < b->id) { + a->id = b->id; + } else { + b->id = a->id; + } + } + return r; +} + +void luaS_share (TString *ts) { + makeshared(ts); + ts->id = ATOM_INC(&STRID); +} unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast(unsigned int, l); @@ -67,6 +87,37 @@ unsigned int luaS_hashlongstr (TString *ts) { } +/* +** resizes the string table +*/ +void luaS_resize (lua_State *L, int newsize) { + int i; + stringtable *tb = &G(L)->strt; + 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; + } + 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 */ + TString *hnext = p->u.hnext; /* save next */ + unsigned int h = lmod(p->hash, newsize); /* new position */ + p->u.hnext = tb->hash[h]; /* chain it */ + tb->hash[h] = p; + p = hnext; + } + } + 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, TString *); + } + tb->size = newsize; +} + + /* ** Clear API string cache. (Entries cannot be empty, so fill them with ** a non-collectable string.) @@ -75,11 +126,20 @@ void luaS_clearcache (global_State *g) { 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 */ } } -static struct ssm_ref * newref(int size); +static unsigned int make_str_seed(lua_State *L) { + size_t buff[4]; + unsigned int h = time(NULL); + buff[0] = cast(size_t, h); + buff[1] = cast(size_t, &STRSEED); + buff[2] = cast(size_t, &make_str_seed); + buff[3] = cast(size_t, L); + return luaS_hash((const char*)buff, sizeof(buff), h); +} /* ** Initialize the string table and the string cache @@ -87,8 +147,10 @@ static struct ssm_ref * newref(int size); void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; - g->strsave = newref(MINSTRTABSIZE); - g->strmark = newref(MINSTRTABSIZE); + if (STRSEED == 0) { + STRSEED = make_str_seed(L); + } + 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 */ @@ -111,17 +173,60 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { ts = gco2ts(o); ts->hash = h; ts->extra = 0; + ts->id = 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, STRSEED); ts->u.lnglen = l; return ts; } -static TString *internshrstr (lua_State *L, const char *str, size_t l); + +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)->u.hnext; + *p = (*p)->u.hnext; /* remove element from its list */ + tb->nuse--; +} + + +/* +** 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) { + TString *ts; + global_State *g = G(L); + unsigned int h = luaS_hash(str, l, STRSEED); + 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)) { + /* found! */ + if (isdead(g, ts)) /* dead (but not collected yet)? */ + changewhite(ts); /* resurrect it */ + return ts; + } + } + 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, l, LUA_TSHRSTR, h); + memcpy(getstr(ts), str, l * sizeof(char)); + ts->shrlen = cast_byte(l); + ts->u.hnext = *list; + *list = ts; + g->strt.nuse++; + return ts; +} + /* ** new string (with explicit length) @@ -176,928 +281,3 @@ Udata *luaS_newudata (lua_State *L, size_t s) { return u; } -/* - * global shared table - */ - -#include "rwlock.h" -#include "spinlock.h" -#include "atomic.h" -#include -#include - -#define SHRSTR_INITSIZE 0x10000 - -/* prime is better for hash */ -#define VMHASHSLOTS 4093 - -struct shrmap_slot { - struct rwlock lock; - TString *str; -}; - -struct ssm_ref { - TString **hash; - TString **array; - int nuse; /* number of elements */ - int hsize; - int asize; - int acap; -}; - -struct collect_queue { - struct collect_queue *next; - void * key; - struct ssm_ref *strsave; - struct ssm_ref *strmark; - struct ssm_ref *strfix; -}; - -struct shrmap { - struct rwlock lock; - int rwslots; - int total; - int garbage; - int roslots; - struct shrmap_slot * readwrite; - struct shrmap_slot * readonly; - struct spinlock qlock; - struct collect_queue * head; - struct collect_queue * tail; - struct collect_queue * vm[VMHASHSLOTS]; -}; - -static struct shrmap SSM; - -#define ADD_SREF(ts) do {if(ATOM_INC(&((ts)->u.ref))==1) ATOM_DEC(&SSM.garbage);}while(0) -#define DEC_SREF(ts) do {if(ATOM_DEC(&((ts)->u.ref))==0) ATOM_INC(&SSM.garbage);}while(0) -#define ZERO_SREF(ts) ((ts)->u.ref == 0) -#define FREE_SREF(ts) do {free(ts);ATOM_DEC(&SSM.total);ATOM_DEC(&SSM.garbage);}while(0) - -static struct ssm_ref * -newref(int size) { - /* size must be must be power of 2 */ - lua_assert( (size&(size-1))==0 ); - struct ssm_ref *r = (struct ssm_ref *)malloc(sizeof(*r)); - if (r == NULL) - return NULL; - TString **hash = (TString **)malloc(sizeof(TString *) * size); - if (hash == NULL) { - free(r); - return NULL; - } - memset(r, 0, sizeof(*r)); - memset(hash, 0, sizeof(TString *) * size); - r->hsize = size; - r->hash = hash; - return r; -} - -static void -expand_ref(struct ssm_ref *r, int changeref) { - int hsize = r->hsize * 2; - TString ** hash = (TString **)malloc(sizeof(TString *) * hsize); - if (hash == NULL) - return; - memset(hash, 0, sizeof(TString *) * hsize); - int i; - for (i=0;ihsize;i++) { - TString *s = r->hash[i]; - if (s) { - hash[lmod(s->hash, hsize)] = s; - } - } - free(r->hash); - r->hash = hash; - r->hsize = hsize; - - for (i=0;iasize;) { - TString *s = r->array[i]; - int slot = lmod(s->hash, hsize); - TString *hs = hash[slot]; - if (hs == s || hs == NULL) { - if (hs == NULL) - hash[slot] = s; - else { - --r->nuse; - if (changeref) - DEC_SREF(s); - } - --r->asize; - r->array[i] = r->array[r->asize]; - } else { - ++i; - } - } -} - -static void -insert_ref(struct ssm_ref *r, TString *s) { - if (r->asize >= r->acap) { - r->acap = r->asize * 2; - if (r->acap == 0) { - r->acap = r->hsize / 2; - } - TString ** array = (TString **)realloc(r->array, r->acap * sizeof(TString *)); - lua_assert(array != NULL); - r->array = array; - } - r->array[r->asize++] = s; -} - -static void -shrink_ref(struct ssm_ref *r) { - int hsize = r->hsize / 2; - if (hsize < MINSTRTABSIZE) - return; - TString ** hash = (TString **)malloc(sizeof(TString *) * hsize); - if (hash == NULL) - return; - memset(hash, 0, sizeof(TString *) * hsize); - int i; - for (i=0;ihsize;i++) { - TString *s = r->hash[i]; - if (s) { - int h = lmod(s->hash, hsize); - if (hash[h] == NULL) - hash[h] = s; - else - insert_ref(r, s); - } - } - free(r->hash); - r->hash = hash; - r->hsize = hsize; -} - -static void -markref(struct ssm_ref *r, TString *s, int changeref) { - unsigned int h = s->hash; - int slot = lmod(h, r->hsize); - TString * hs = r->hash[slot]; - if (hs == s){ - if (changeref) - DEC_SREF(s); - return; - } - ++r->nuse; - if (r->nuse >= r->hsize && r->hsize <= MAX_INT/2) { - expand_ref(r, changeref); - slot = lmod(h, r->hsize); - hs = r->hash[slot]; - } - if (hs != NULL) { - if (hs == s) { - --r->nuse; - if (changeref) - DEC_SREF(s); - return; - } - insert_ref(r, hs); - } - r->hash[slot] = s; -} - -void -luaS_mark(global_State *g, TString *s) { - markref(g->strmark, s, 0); -} - -void -luaS_fix(global_State *g, TString *s) { - if (g->strfix == NULL) - g->strfix = newref(STRFIXSIZE); - markref(g->strfix, s, 0); -} - -static void -delete_ref(struct ssm_ref *r) { - if (r == NULL) - return; - free(r->hash); - free(r->array); - free(r); -} - -static void -delete_cqueue(struct collect_queue *cqueue) { - delete_ref(cqueue->strsave); - delete_ref(cqueue->strmark); - delete_ref(cqueue->strfix); - free(cqueue); -} - -static void -free_cqueue(struct collect_queue *cqueue) { - while (cqueue) { - struct collect_queue * next = cqueue->next; - delete_cqueue(cqueue); - cqueue = next; - } -} - -static void -remove_duplicate(struct ssm_ref *r, int decref) { - int i = 0; - while (i < r->asize) { - TString *s = r->array[i]; - if (r->hash[lmod(s->hash, r->hsize)] == s) { - --r->nuse; - --r->asize; - r->array[i] = r->array[r->asize]; - if (decref) { - DEC_SREF(s); - } - } else { - ++i; - } - } -} - -static struct ssm_ref * -mergeset(struct ssm_ref *set, struct ssm_ref * rset, int changeref) { - if (set == NULL) - return rset; - else if (rset == NULL) - return set; - int total = set->nuse + rset->nuse; - if (total * 2 <= set->hsize) { - shrink_ref(set); - } else if (total > set->hsize) { - expand_ref(set, changeref); - } - int i; - for (i=0;ihsize;i++) { - TString * s = rset->hash[i]; - if (s) { - markref(set, s, changeref); - } - } - for (i=0;iasize;i++) { - TString * s = rset->array[i]; - markref(set, s, changeref); - } - delete_ref(rset); - remove_duplicate(set, changeref); - return set; -} - -static void -merge_last(struct collect_queue * c) { - void *key = c->key; - int hash = (int)((size_t)key % VMHASHSLOTS); - struct shrmap * s = &SSM; - struct collect_queue * slot = s->vm[hash]; - if (slot == NULL) { - s->vm[hash] = c; - c->next = NULL; - return; - } - - if (slot->key == key) { - // remove head - s->vm[hash] = slot->next; - } else { - for (;;) { - struct collect_queue * next = slot->next; - if (next == NULL) { - // not found, insert head - c->next = s->vm[hash]; - s->vm[hash] = c; - return; - } else if (next->key == key) { - // remove next - slot->next = next->next; - slot = next; - break; - } - slot = next; - } - } - // merge slot (last) into c - c->strsave = mergeset(slot->strsave, c->strsave, 1); - c->strfix = mergeset(slot->strfix, c->strfix, 0); - c->next = s->vm[hash]; - s->vm[hash] = c; - free(slot); -} - -static void -clear_vm(struct collect_queue * c) { - void *key = c->key; - int hash = (int)((size_t)key % VMHASHSLOTS); - struct shrmap * s = &SSM; - struct collect_queue * slot = s->vm[hash]; - lua_assert(slot == c); - s->vm[hash] = slot->next; - delete_cqueue(slot); -} - -static int -compar_tstring(const void *a, const void *b) { - return memcmp(a,b, sizeof(TString *)); -} - -static void -sortset(struct ssm_ref *set) { - qsort(set->array, set->asize,sizeof(TString *),compar_tstring); -} - -static int -exist(struct ssm_ref *r, TString *s) { - int slot = lmod(s->hash, r->hsize); - TString *hs = r->hash[slot]; - if (hs == s) - return 1; - int begin = 0, end = r->asize-1; - while (begin <= end) { - int mid = (begin + end) / 2; - TString *t = r->array[mid]; - if (t == s) - return 1; - if (memcmp(&s,&t,sizeof(TString *)) > 0) - begin = mid + 1; - else - end = mid - 1; - } - return 0; -} - -static int -collectref(struct collect_queue * c) { - int i; - int total = 0; - merge_last(c); - struct ssm_ref *mark = c->strmark; - struct ssm_ref * save = c->strsave; - c->strmark = NULL; - if (mark) { - struct ssm_ref * fix = c->strfix; - sortset(mark); - sortset(fix); - - for (i=0;ihsize;i++) { - TString * s = save->hash[i]; - if (s) { - if (!exist(mark, s) && !exist(fix, s)) { - save->hash[i] = NULL; - --save->nuse; - DEC_SREF(s); - ++total; - } - } - } - - for (i=0;iasize;) { - TString * s = save->array[i]; - if (!exist(mark, s) && !exist(fix, s)) { - --save->asize; - --save->nuse; - save->array[i] = save->array[save->asize]; - DEC_SREF(s); - ++total; - } else { - ++i; - } - } - delete_ref(mark); - } else { - for (i=0;ihsize;i++) { - TString * s = save->hash[i]; - if (s) { - DEC_SREF(s); - ++total; - } - } - for (i=0;iasize;i++) { - TString * s = save->array[i]; - DEC_SREF(s); - ++total; - } - clear_vm(c); - } - return total; -} - -static int -pow2size(struct ssm_ref *r) { - if (r->nuse <= MINSTRTABSIZE) - return MINSTRTABSIZE; - int hsize = r->hsize; - while (hsize / 2 > r->nuse) { - hsize /= 2; - } - return hsize; -} - -void -luaS_collect(global_State *g, int closed) { - if (closed) { - delete_ref(g->strmark); - g->strmark = NULL; - } - struct shrmap * s = &SSM; - struct collect_queue *cqueue = (struct collect_queue *)malloc(sizeof(*cqueue)); - if (cqueue == NULL) { - /* OOM, give up */ - return; - } - cqueue->key = g; - cqueue->strsave = g->strsave; - cqueue->strmark = g->strmark; - cqueue->strfix = g->strfix; - cqueue->next = NULL; - - g->strfix = NULL; - if (closed) { - g->strsave = NULL; - g->strmark = NULL; - } else { - g->strsave = newref(pow2size(g->strsave)); - g->strmark = newref(pow2size(g->strmark)); - } - - spinlock_lock(&s->qlock); - if (s->head) { - s->tail->next = cqueue; - s->tail = cqueue; - } else { - s->head = s->tail = cqueue; - } - spinlock_unlock(&s->qlock); -} - -static unsigned int make_str_seed() { - size_t buff[4]; - unsigned int h = time(NULL); - buff[0] = cast(size_t, h); - buff[1] = cast(size_t, &STRSEED); - buff[2] = cast(size_t, &make_str_seed); - buff[3] = cast(size_t, SHRSTR_INITSIZE); - return luaS_hash((const char*)buff, sizeof(buff), h); -} - -static struct shrmap_slot * -shrstr_newpage(int sz) { - int i; - struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s)); - if (s == NULL) - return NULL; - for (i=0;inext; - FREE_SREF(str); - str = next; - } - } - free(s); - } -} - -static int -shrstr_allocpage(struct shrmap * s, int osz, int sz, struct shrmap_slot * newpage) { - if (s->readonly != NULL) - return 0; - if (s->rwslots != osz) - return 0; - s->readonly = s->readwrite; - s->readwrite = newpage; - s->roslots = s->rwslots; - s->rwslots = sz; - - return 1; -} - -static void -shrstr_rehash(struct shrmap *s, int slotid) { - struct shrmap_slot *slot = &s->readonly[slotid]; - rwlock_wlock(&slot->lock); - TString *str = slot->str; - while (str) { - TString * next = (TString *)str->next; - if (ZERO_SREF(str)) { - FREE_SREF(str); - } else { - int newslotid = lmod(str->hash, s->rwslots); - struct shrmap_slot *newslot = &s->readwrite[newslotid]; - rwlock_wlock(&newslot->lock); - str->next = (GCObject *)newslot->str; - newslot->str = str; - rwlock_wunlock(&newslot->lock); - } - str = next; - } - - slot->str = NULL; - rwlock_wunlock(&slot->lock); -} - -/* - 1. writelock SSM if readonly == NULL, (Only one thread can expand) - 2. move old page (readwrite) to readonly - 3. new (empty) page with double size to readwrite - 4. unlock SSM - 5. rehash every slots - 6. remove temporary readonly (writelock SSM) - */ -static void -expandssm() { - struct shrmap * s = &SSM; - if (s->readonly) - return; - int osz = s->rwslots; - int sz = osz * 2; - if (sz < osz) { - // overflow check - return; - } - struct shrmap_slot * newpage = shrstr_newpage(sz); - if (newpage == NULL) - return; - rwlock_wlock(&s->lock); - int succ = shrstr_allocpage(s, osz, sz, newpage); - rwlock_wunlock(&s->lock); - if (!succ) { - shrstr_deletepage(newpage, sz); - return; - } - int i; - for (i=0;ilock); - struct shrmap_slot * oldpage = s->readonly; - s->readonly = NULL; - rwlock_wunlock(&s->lock); - shrstr_deletepage(oldpage, osz); -} - -static int -sweep_slot(struct shrmap *s, int i) { - struct shrmap_slot *slot = &s->readwrite[i]; - int n = 0; - TString *ts; - rwlock_rlock(&slot->lock); - ts = slot->str; - while (ts) { - if (ZERO_SREF(ts)) { - n = 1; - break; - } - ts = (TString *)ts->next; - } - rwlock_runlock(&slot->lock); - if (n == 0) - return 0; - - n = 0; - rwlock_wlock(&slot->lock); - TString **ref = &slot->str; - ts = *ref; - while (ts) { - if (ZERO_SREF(ts)) { - *ref = (TString *)ts->next; - FREE_SREF(ts); - ts = *ref; - ++n; - } else { - ref = (TString **)&(ts->next); - ts = *ref; - } - } - rwlock_wunlock(&slot->lock); - return n; -} - -static int -sweepssm() { - struct shrmap * s = &SSM; - rwlock_rlock(&s->lock); - if (s->readonly) { - rwlock_runlock(&s->lock); - return 0; - } - int sz = s->rwslots; - int i; - int n = 0; - for (i=0;ilock); - return n; -} - -/* call it in a separate thread */ -LUA_API int -luaS_collectssm(struct ssm_collect *info) { - struct shrmap * s = &SSM; - if (s->total * 5 / 4 > s->rwslots) { - expandssm(); - } - if (s->garbage > s->total / 8) { - info->sweep = sweepssm(); - } else { - info->sweep = 0; - } - if (s->head) { - struct collect_queue * cqueue; - spinlock_lock(&s->qlock); - cqueue = s->head; - s->head = cqueue->next; - spinlock_unlock(&s->qlock); - if (cqueue) { - if (info) { - info->key = cqueue->key; - } - int n = collectref(cqueue); - if (info) { - info->n = n; - } - } - return 1; - } - return 0; -} - -LUA_API void -luaS_initssm() { - struct shrmap * s = &SSM; - rwlock_init(&s->lock); - s->rwslots = SHRSTR_INITSIZE; - s->readwrite = shrstr_newpage(SHRSTR_INITSIZE); - s->readonly = NULL; - s->head = NULL; - s->tail = NULL; - spinlock_init(&s->qlock); - STRSEED = make_str_seed(); -} - -LUA_API void -luaS_exitssm() { - struct shrmap * s = &SSM; - rwlock_wlock(&s->lock); - int sz = s->rwslots; - shrstr_deletepage(s->readwrite, sz); - shrstr_deletepage(s->readonly, s->roslots); - s->readwrite = NULL; - s->readonly = NULL; - free_cqueue(s->head); - s->head = NULL; - s->tail = NULL; - int i; - for (i=0;ivm[i]); - s->vm[i] = NULL; - } -} - -static TString * -find_string(struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { - TString *ts = slot->str; - while (ts) { - if (ts->hash == h && - ts->shrlen == l && - memcmp(str, ts+1, l) == 0) { - ADD_SREF(ts); - break; - } - ts = (TString *)ts->next; - } - return ts; -} - -static TString * -find_and_collect(struct shrmap_slot * slot, unsigned int h, const char *str, lu_byte l) { - TString **ref = &slot->str; - TString *ts = *ref; - while (ts) { - if (ts->hash == h && - ts->shrlen == l && - memcmp(str, ts+1, l) == 0) { - ADD_SREF(ts); - break; - } - if (ZERO_SREF(ts)) { - *ref = (TString *)ts->next; - FREE_SREF(ts); - ts = *ref; - } else { - ref = (TString **)&(ts->next); - ts = *ref; - } - } - return ts; -} - - -/* - 1. readlock SSM - 2. find string in readwrite page - 3. find string in readonly (if exist, during exapnding) - 4. unlock SSM - */ -static TString * -query_string(unsigned int h, const char *str, lu_byte l) { - struct shrmap * s = &SSM; - TString *ts = NULL; - rwlock_rlock(&s->lock); - struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; - rwlock_rlock(&slot->lock); - ts = find_string(slot, h, str, l); - rwlock_runlock(&slot->lock); - if (ts == NULL && s->readonly != NULL) { - int mask = s->roslots - 1; - slot = &s->readonly[h & mask]; - rwlock_rlock(&slot->lock); - ts = find_string(slot, h, str, l); - rwlock_runlock(&slot->lock); - } - 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); - setbits(ts->marked, WHITEBITS); - gray2black(ts); - makeshared(ts); - ts->tt = LUA_TSHRSTR; - ts->hash = h; - ts->shrlen = l; - ts->u.ref = 1; - memcpy(ts+1, str, l); - return ts; -} - -static TString * -shrstr_exist(struct shrmap * s, unsigned int h, const char *str, lu_byte l) { - TString *found; - if (s->readonly) { - unsigned int mask = s->roslots - 1; - struct shrmap_slot *slot = &s->readonly[h & mask]; - rwlock_rlock(&slot->lock); - found = find_string(slot, h, str, l); - rwlock_runlock(&slot->lock); - if (found) - return found; - } - struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; - rwlock_wlock(&slot->lock); - if (s->readonly) { - // Don't collect during expanding - found = find_string(slot, h, str, l); - } else { - found = find_and_collect(slot, h, str, l); - } - if (found) { - rwlock_wunlock(&slot->lock); - return found; - } - // not found, lock slot and return. - return NULL; -} - -static TString * -add_string(unsigned int h, const char *str, lu_byte l) { - struct shrmap * s = &SSM; - TString * tmp = new_string(h, str, l); - rwlock_rlock(&s->lock); - struct TString *ts = shrstr_exist(s, h, str, l); - if (ts) { - // string is create by other thread, so free tmp - free(tmp); - } else { - struct shrmap_slot *slot = &s->readwrite[lmod(h, s->rwslots)]; - ts = tmp; - ts->next = (GCObject *)slot->str; - slot->str = ts; - rwlock_wunlock(&slot->lock); - ATOM_INC(&SSM.total); - } - rwlock_runlock(&s->lock); - return ts; -} - -static TString * -internshrstr(lua_State *L, const char *str, size_t l) { - TString *ts; - unsigned int h = luaS_hash(str, l, STRSEED); - ts = query_string(h, str, l); - if (ts == NULL) { - ts = add_string(h, str, l); - } - markref(G(L)->strsave, ts, 1); - return ts; -} - -struct slotinfo { - int len; - int garbage; - size_t size; - size_t garbage_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; - size_t sz = sizelstring(ts->shrlen); - if (ZERO_SREF(ts)) { - ++info->garbage; - info->garbage_size += sz; - } - info->size += sz; - ts = (TString *)ts->next; - } - rwlock_runlock(&s->lock); -} - -struct variance { - int count; - double mean; - double m2; -}; - -static void -variance_update(struct variance *v, int newValue_) { - double newValue = (double)newValue_; - ++v->count; - double delta = newValue - v->mean; - v->mean += delta / v->count; - double delta2 = newValue - v->mean; - v->m2 += delta * delta2; -} - -LUA_API void -luaS_infossm(struct ssm_info *info) { - struct slotinfo total; - struct slotinfo tmp; - memset(&total, 0, sizeof(total)); - struct shrmap * s = &SSM; - struct variance v = { 0,0,0 }; - int slots = 0; - rwlock_rlock(&s->lock); - int i; - int sz = s->rwslots; - for (i=0;ireadwrite[i]; - getslot(slot, &tmp); - if (tmp.len > 0) { - if (tmp.len > total.len) { - total.len = tmp.len; - } - total.size += tmp.size; - total.garbage_size += tmp.garbage_size; - total.garbage += tmp.garbage; - variance_update(&v, tmp.len); - ++slots; - } - } - if (s->readonly) { - sz = s->roslots; - for (i=0;ireadonly[i]; - getslot(slot, &tmp); - if (tmp.len > 0) { - if (tmp.len > total.len) { - total.len = tmp.len; - } - // may double counting, but it's only an info - total.size += tmp.size; - total.garbage_size += tmp.garbage_size; - total.garbage += tmp.garbage; - variance_update(&v, tmp.len); - } - } - } - rwlock_runlock(&s->lock); - info->total = SSM.total; - info->size = total.size; - info->longest = total.len; - info->slots = slots; - info->garbage = total.garbage; - info->garbage_size = total.garbage_size; - if (v.count > 1) { - info->variance = v.m2 / v.count; - } else { - info->variance = 0; - } -} diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 72ee14a00..4cb59297f 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -28,46 +28,24 @@ /* -** equality for short strings, which are always internalized +** equality for short strings, compare id first */ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) - +#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b) || \ + ( ((a)->id == (b)->id) ? ((a)->id != 0) : ((a)->hash == (b)->hash && luaS_eqshrstr(a,b)) ) ) 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 int luaS_eqshrstr (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); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); +LUAI_FUNC void luaS_share(TString *ts); -#define ENABLE_SHORT_STRING_TABLE - -struct ssm_info { - int total; - int longest; - int slots; - int garbage; - size_t size; - size_t garbage_size; - double variance; -}; - -struct ssm_collect { - void *key; - int n; - int sweep; -}; - -LUA_API void luaS_initssm(); -LUA_API void luaS_exitssm(); -LUA_API void luaS_infossm(struct ssm_info *info); -LUA_API int luaS_collectssm(struct ssm_collect *info); - -LUAI_FUNC void luaS_mark(global_State *g, TString *s); -LUAI_FUNC void luaS_fix(global_State *g, TString *s); -LUAI_FUNC void luaS_collect(global_State *g, int closed); #endif diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index cad0d9f4d..ca5b29852 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -18,7 +18,6 @@ #include "lauxlib.h" #include "lualib.h" -#include "lstring.h" @@ -596,9 +595,7 @@ static int pmain (lua_State *L) { int main (int argc, char **argv) { int status, result; - lua_State *L; - luaS_initssm(); - L = luaL_newstate(); /* create state */ + lua_State *L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; @@ -610,7 +607,6 @@ int main (int argc, char **argv) { result = lua_toboolean(L, -1); /* get result */ report(L, status); lua_close(L); - luaS_exitssm(); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 0595e75ec..8a59176a4 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -233,9 +233,11 @@ LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); LUA_API void (lua_pushboolean) (lua_State *L, int b); LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L); + LUA_API void (lua_clonefunction) (lua_State *L, const void * fp); LUA_API void (lua_sharefunction) (lua_State *L, int index); LUA_API void (lua_sharestring) (lua_State *L, int index); +LUA_API void (lua_clonetable) (lua_State *L, const void * t); /* ** get functions (Lua -> stack) diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 62a0509ba..549ad3950 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -21,7 +21,6 @@ #include "lobject.h" #include "lstate.h" #include "lundump.h" -#include "lstring.h" static void PrintFunction(const Proto* f, int full); #define luaU_print PrintFunction @@ -193,7 +192,6 @@ static int pmain(lua_State* L) int main(int argc, char* argv[]) { lua_State* L; - luaS_initssm(); int i=doargs(argc,argv); argc-=i; argv+=i; if (argc<=0) usage("no input files given"); @@ -204,7 +202,6 @@ int main(int argc, char* argv[]) lua_pushlightuserdata(L,argv); if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); lua_close(L); - luaS_exitssm(); return EXIT_SUCCESS; } diff --git a/Makefile b/Makefile index 10076babe..36cb83bc3 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,6 @@ LUA_CLIB_SKYNET = \ lua-stm.c \ lua-debugchannel.c \ lua-datasheet.c \ - lua-ssm.c \ lua-sharetable.c \ \ diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index dce345c47..660d27ec1 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -4,12 +4,9 @@ #include #include -#include "lstring.h" -#include "lobject.h" -#include "lapi.h" #include "lgc.h" -#ifdef ENABLE_SHORT_STRING_TABLE +#ifdef makeshared static void mark_shared(lua_State *L) { @@ -81,12 +78,7 @@ make_matrix(lua_State *L) { static int clone_table(lua_State *L) { - Table * t = (Table *)lua_touserdata(L, 1); - if (!isshared(t)) - return luaL_error(L, "Not a shared table"); - - sethvalue(L, L->top, t); - api_incr_top(L); + lua_clonetable(L, lua_touserdata(L, 1)); return 1; } diff --git a/lualib-src/lua-ssm.c b/lualib-src/lua-ssm.c deleted file mode 100644 index fac37850c..000000000 --- a/lualib-src/lua-ssm.c +++ /dev/null @@ -1,78 +0,0 @@ -#define LUA_LIB - -#include -#include -#include - -#include "lstring.h" - -static int -linfo(lua_State *L) { - struct ssm_info info; - memset(&info, 0, sizeof(info)); - luaS_infossm(&info); - lua_createtable(L, 0, 5); - lua_pushinteger(L, info.total); - lua_setfield(L, -2, "n"); - lua_pushinteger(L, info.longest); - lua_setfield(L, -2, "longest"); - lua_pushinteger(L, info.slots); - lua_setfield(L, -2, "slots"); - lua_pushinteger(L, info.size); - lua_setfield(L, -2, "size"); - lua_pushinteger(L, info.garbage); - lua_setfield(L, -2, "garbage"); - lua_pushinteger(L, info.garbage_size); - lua_setfield(L, -2, "garbage_size"); - lua_pushnumber(L, info.variance); - lua_setfield(L, -2, "variance"); - - return 1; -} - -static int -lcollect(lua_State *L) { - int loop = lua_toboolean(L, 1); - if (loop) { - int n = 0; - struct ssm_collect info; - while (luaS_collectssm(&info)) { - n+=info.n; - } - lua_pushinteger(L, n); - return 1; - } else { - struct ssm_collect info; - int again = luaS_collectssm(&info); - if (again && lua_istable(L, 2)) { - lua_pushinteger(L, info.n); - lua_setfield(L, 2, "n"); - lua_pushinteger(L, info.sweep); - lua_setfield(L, 2, "sweep"); - lua_pushlightuserdata(L, info.key); - lua_setfield(L, 2, "key"); - } - lua_pushboolean(L, again); - return 1; - } -} - -LUAMOD_API int -luaopen_skynet_ssm(lua_State *L) { - luaL_checkversion(L); - - luaL_Reg l[] = { - { "info", linfo }, - { "collect", lcollect }, - { NULL, NULL }, - }; - - luaL_newlib(L,l); - -#ifndef ENABLE_SHORT_STRING_TABLE - lua_pushboolean(L, 1); - lua_setfield(L, -2, "disable"); -#endif - return 1; -} - diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 195544b7e..39f0c49ca 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -8,8 +8,6 @@ skynet.start(function() local launcher = assert(skynet.launch("snlua","launcher")) skynet.name(".launcher", launcher) - skynet.newservice "garbagecollect" - local harbor_id = tonumber(skynet.getenv "harbor" or 0) if harbor_id == 0 then assert(standalone == nil) diff --git a/service/garbagecollect.lua b/service/garbagecollect.lua deleted file mode 100644 index f2a00f060..000000000 --- a/service/garbagecollect.lua +++ /dev/null @@ -1,26 +0,0 @@ -local skynet = require "skynet" -local ssm = require "skynet.ssm" - -local function ssm_info() - return ssm.info() -end - -local function collect() - local info = {} - while true do --- while ssm.collect(false, info) do --- skynet.error(string.format("Collect %d strings from %s, sweep %d", info.n, info.key, info.sweep)) --- end - ssm.collect(true) - skynet.sleep(50) - end -end - -skynet.start(function() - if ssm.disable then - skynet.error "Short String Map (SSM) Disabled" - skynet.exit() - end - skynet.info_func(ssm_info) - skynet.fork(collect) -end) diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h deleted file mode 100644 index 0dcb27997..000000000 --- a/skynet-src/luashrtbl.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef LUA_SHORT_STRING_TABLE_H -#define LUA_SHORT_STRING_TABLE_H - -#include "lstring.h" - -// If you use modified lua, this macro would be defined in lstring.h -#ifndef ENABLE_SHORT_STRING_TABLE - -struct ssm_info { - int total; - int longest; - int slots; - size_t size; - double variance; -}; - -struct ssm_collect { - void *key; - int n; -}; - -static inline void luaS_initssm(); -static inline void luaS_exitssm(); -static inline void luaS_infossm(struct ssm_info *info) {} -static inline int luaS_collectssm(struct ssm_collect *info) { return 0; } - -#endif - -#endif diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index d9aef78a3..ba7bb847d 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -3,7 +3,6 @@ #include "skynet_imp.h" #include "skynet_env.h" #include "skynet_server.h" -#include "luashrtbl.h" #include #include @@ -126,7 +125,6 @@ main(int argc, char *argv[]) { return 1; } - luaS_initssm(); skynet_globalinit(); skynet_env_init(); @@ -162,7 +160,6 @@ main(int argc, char *argv[]) { skynet_start(&config); skynet_globalexit(); - luaS_exitssm(); return 0; } From 10fd5791f76f928b9b9e6f794e60d4a4ab0cb405 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 18 Jun 2019 14:40:29 +0800 Subject: [PATCH 146/565] dec id would be better --- 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 a9ff3e159..893ed8a88 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -65,7 +65,7 @@ int luaS_eqshrstr (TString *a, TString *b) { void luaS_share (TString *ts) { makeshared(ts); - ts->id = ATOM_INC(&STRID); + ts->id = ATOM_DEC(&STRID); } unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { From 4fa1ba57cb4970e889ba977db7079bd16a3c1c03 Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 27 Jun 2019 15:16:15 +0800 Subject: [PATCH 147/565] add SSL_CTX_new error string --- lualib-src/ltls.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 00716b29d..f9701a49a 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -320,7 +320,10 @@ lnew_ctx(lua_State* L) { struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdata(L, sizeof(*ctx_p)); ctx_p->ctx = SSL_CTX_new(SSLv23_method()); if(!ctx_p->ctx) { - luaL_error(L, "SSL_CTX_new client faild."); + unsigned int err = ERR_get_error(); + char buf[256]; + ERR_error_string_n(err, buf, sizeof(buf)); + luaL_error(L, "SSL_CTX_new client faild. %s\n", buf); } if(luaL_newmetatable(L, "_TLS_SSLCTX_METATABLE_")) { From 9193035767597475133a639c2f5145eee828867f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Jul 2019 20:26:53 +0800 Subject: [PATCH 148/565] Improve error message , see issue #1050 --- lualib-src/lua-cluster.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 47c6592dd..a050f3c02 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -111,7 +111,11 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz, int is_push) 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); + if (name == NULL) { + luaL_error(L, "name is not a string, it's a %s", lua_typename(L, lua_type(L, 1))); + } else { + luaL_error(L, "name is too long %s", name); + } } uint8_t buf[TEMP_LENGTH]; From 89f487e0a4413c91d48af27bf6bed4898977a0e0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Jul 2019 23:08:54 +0800 Subject: [PATCH 149/565] bugfix: init the lock of code cache at start --- 3rd/lua/lauxlib.c | 6 +++++- 3rd/lua/lualib.h | 1 + skynet-src/skynet_main.c | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 6b2f98f2d..6da4db78c 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1063,10 +1063,14 @@ clearcache() { static void init() { - SPIN_INIT(&CC); CC.L = luaL_newstate(); } +LUALIB_API void +luaL_initcodecache(void) { + SPIN_INIT(&CC); +} + static const void * load(const char *key) { if (CC.L == NULL) diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 422b9ea55..1bd3fb512 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -49,6 +49,7 @@ LUAMOD_API int (luaopen_package) (lua_State *L); #define LUA_CACHELIB LUAMOD_API int (luaopen_cache) (lua_State *L); +LUALIB_API void (luaL_initcodecache) (void); /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index ba7bb847d..a96c312d9 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -132,6 +132,11 @@ main(int argc, char *argv[]) { struct skynet_config config; +#ifdef LUA_CACHELIB + // init the lock of code cache + luaL_initcodecache(); +#endif + struct lua_State *L = luaL_newstate(); luaL_openlibs(L); // link lua lib From 7e42653f80dfeb8d8aef044cb78e85cced125eb5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Jul 2019 16:53:11 +0800 Subject: [PATCH 150/565] fix #1057 --- lualib/http/httpc.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 4f5880bf8..a26a15055 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -72,6 +72,9 @@ local function request(interface, method, host, url, recvheader, header, content local padding = read(length - #body) body = body .. padding end + elseif code == 204 or code == 304 or code < 200 then + body = "" + -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response else -- no content-length, read all body = body .. interface.readall() From baf5987b0bd9223f239faec5b1614fb792520aee Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Aug 2019 10:20:38 +0800 Subject: [PATCH 151/565] fix #1062 --- service/clusterd.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/service/clusterd.lua b/service/clusterd.lua index f891df2a1..5a151f938 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -50,6 +50,8 @@ local function open_channel(t, key) if succ then t[key] = c ct.channel = c + else + err = string.format("changenode [%s] (%s:%s) failed", key, host, port) end else err = string.format("cluster node [%s] is %s.", key, address == false and "down" or "absent") From 2e8659d5fccc63894f49fe089b689a855b3e51bd Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 15 Aug 2019 18:54:12 +0800 Subject: [PATCH 152/565] dns: remove unused variable --- lualib/skynet/dns.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index ae75cef2c..22b3860fb 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -516,7 +516,7 @@ function dns.resolve(name, ipv6) return answer, answers end - return remote_resolve(name, ipv6, timeout) + return remote_resolve(name, ipv6) end return dns From 65eb351a3b797985ab3f18c3aff6d08971b340a4 Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 15 Aug 2019 18:59:11 +0800 Subject: [PATCH 153/565] fix replace mt when sharetable update --- lualib/skynet/sharetable.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 208e035df..5fff58c3a 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -275,7 +275,7 @@ local function resolve_replace(replace_map) local nv = replace_map[mt] if nv then nv = getnv(mt) - setmetatable(t, nv) + setmetatable(v, nv) else match_value(mt) end From 478575436bd2e00b8923ef235f34c888905f5ede Mon Sep 17 00:00:00 2001 From: wudeng Date: Tue, 27 Aug 2019 19:41:30 +0800 Subject: [PATCH 154/565] uniqtask for gm --- lualib/skynet.lua | 27 ++++++++++++++++++++++++++- lualib/skynet/debug.lua | 4 ++++ service/debug_console.lua | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a49f08134..3f2749b63 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -8,6 +8,7 @@ local pcall = pcall local table = table local tremove = table.remove local tinsert = table.insert +local traceback = debug.traceback local profile = require "skynet.profile" @@ -773,7 +774,7 @@ function skynet.task(ret) local tt = type(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do - ret[session] = debug.traceback(co) + ret[session] = traceback(co) end return elseif tt == "number" then @@ -793,6 +794,30 @@ function skynet.task(ret) end end +function skynet.uniqtask() + local stacks = {} + for session, co in pairs(session_id_coroutine) do + local stack = traceback(co) + local info = stacks[stack] or {count = 0, sessions = {}} + info.count = info.count + 1 + if info.count < 10 then + info.sessions[#info.sessions+1] = session + end + stacks[stack] = info + end + local ret = {} + for stack, info in pairs(stacks) do + local count = info.count + local sessions = table.concat(info.sessions, ",") + if count > 10 then + sessions = sessions .. "..." + end + local head_line = string.format("%d\tsessions:[%s]\n", count, sessions) + ret[head_line] = stack + end + return ret +end + function skynet.term(service) return _error_dispatch(0, service) end diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index e20279508..78fd1a166 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -42,6 +42,10 @@ local function init(skynet, export) end end + function dbgcmd.UNIQTASK() + skynet.ret(skynet.pack(skynet.uniqtask())) + 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 2ed44f79c..07572bbbf 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -148,6 +148,7 @@ function COMMAND.help() clearcache = "clear lua code cache", service = "List unique service", task = "task address : show service task detail", + uniqtask = "task address : show service unique task detail", inject = "inject address luascript.lua", logon = "logon address", logoff = "logoff address", @@ -262,6 +263,11 @@ function COMMAND.task(address) return skynet.call(address,"debug","TASK") end +function COMMAND.uniqtask(address) + address = adjust_address(address) + return skynet.call(address,"debug","UNIQTASK") +end + function COMMAND.info(address, ...) address = adjust_address(address) return skynet.call(address,"debug","INFO", ...) From e4224137f1972ce46dcb6db6686afa4e870c3d41 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 28 Aug 2019 20:19:15 +0800 Subject: [PATCH 155/565] fix #1078 --- 3rd/lua/lstring.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 893ed8a88..cfff68ebc 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -64,6 +64,8 @@ int luaS_eqshrstr (TString *a, TString *b) { } void luaS_share (TString *ts) { + if (ts == NULL) + return; makeshared(ts); ts->id = ATOM_DEC(&STRID); } From 5025e6887f9ff3c611e38908197a603d438c3320 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 29 Aug 2019 16:21:01 +0800 Subject: [PATCH 156/565] fix #1079 --- lualib/skynet/db/mysql.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index d336e957d..a205b7466 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -129,10 +129,6 @@ local function _recv_packet(self,sock) 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) self.packet_no = num From e485aae55bddf605f9984757c734e1b9ea4ac21c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 30 Aug 2019 10:11:51 +0800 Subject: [PATCH 157/565] This may fix #1080 --- service/clusterd.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/service/clusterd.lua b/service/clusterd.lua index 5a151f938..274e9a9ec 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -61,6 +61,9 @@ local function open_channel(t, key) skynet.wakeup(co) end assert(succ, err) + if node_address[key] ~= address then + return open_channel(t,key) + end return c end @@ -76,6 +79,7 @@ local function loadconfig(tmp) assert(load(source, "@"..config_name, "t", tmp))() end end + local reload = {} for name,address in pairs(tmp) do if name:sub(1,2) == "__" then name = name:sub(3) @@ -87,6 +91,7 @@ local function loadconfig(tmp) -- address changed if rawget(node_channel, name) then node_channel[name] = nil -- reset connection + table.insert(reload, name) end node_address[name] = address end @@ -105,6 +110,10 @@ local function loadconfig(tmp) end end end + for _, name in ipairs(reload) do + -- open_channel would block + skynet.fork(open_channel, node_channel, name) + end end function command.reload(source, config) From 21b1c449de2940a9890b53f4898ec8832566e9b4 Mon Sep 17 00:00:00 2001 From: hong Date: Sat, 21 Sep 2019 14:51:49 +0800 Subject: [PATCH 158/565] luaH_set check shared table --- 3rd/lua/ltable.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index ea4fe7fcb..7c909bda2 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -608,7 +608,10 @@ const TValue *luaH_get (Table *t, const TValue *key) { ** barrier and invalidate the TM cache. */ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); + const TValue *p; + if(isshared(t)) + luaG_runerror(L,"attempt to change a shared table"); + p = luaH_get(t, key); if (p != luaO_nilobject) return cast(TValue *, p); else return luaH_newkey(L, t, key); From 9c4e0d5bb784f0cfc3e692e113286743eb4593c0 Mon Sep 17 00:00:00 2001 From: Sean Feng Date: Sat, 28 Sep 2019 19:26:33 +0800 Subject: [PATCH 159/565] add senders command in clusterd service --- service/clusterd.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/service/clusterd.lua b/service/clusterd.lua index 274e9a9ec..658f39263 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -135,6 +135,10 @@ function command.sender(source, node) skynet.ret(skynet.pack(node_channel[node])) end +function command.senders(source) + skynet.retpack(node_sender) +end + local proxy = {} function command.proxy(source, node, name) From 4ddc1577efc2e57f904476e72df7e4d07d16dd5d Mon Sep 17 00:00:00 2001 From: wudeng Date: Thu, 10 Oct 2019 10:59:00 +0800 Subject: [PATCH 160/565] fix: coroutine leak when dispatch failed --- lualib/skynet.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 3f2749b63..7a99ca541 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -177,9 +177,9 @@ function suspend(co, result, command) c.send(addr, skynet.PTYPE_ERROR, session, "") end session_coroutine_id[co] = nil - session_coroutine_address[co] = nil - session_coroutine_tracetag[co] = nil end + session_coroutine_address[co] = nil + session_coroutine_tracetag[co] = nil skynet.fork(function() end) -- trigger command "SUSPEND" error(debug.traceback(co,tostring(command))) end From 8a1d7006eb91ad555630846178e66682a0cef4af Mon Sep 17 00:00:00 2001 From: zixun Date: Sat, 19 Oct 2019 11:25:42 +0800 Subject: [PATCH 161/565] fix match thread, table key --- lualib-src/lua-sharetable.c | 29 +++++++++++++++++++ lualib/skynet/sharetable.lua | 56 ++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 660d27ec1..dcfd460d9 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -83,6 +83,34 @@ clone_table(lua_State *L) { return 1; } +static int +lco_stackvalues(lua_State* L) { + lua_State *cL = lua_tothread(L, 1); + luaL_argcheck(L, cL, 1, "thread expected"); + int n = 0; + if(cL != L) { + luaL_checktype(L, 2, LUA_TTABLE); + n = lua_gettop(cL); + if(n > 0) { + lua_checkstack(L, n+1); + int top = lua_gettop(L); + lua_xmove(cL, L, n); + int i=0; + for(i=0; i Date: Mon, 21 Oct 2019 11:38:08 +0800 Subject: [PATCH 162/565] fix match intern mt and stackvalues --- lualib-src/lua-sharetable.c | 10 ++++---- lualib/skynet/sharetable.lua | 45 ++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index dcfd460d9..d74d1e3aa 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -92,17 +92,15 @@ lco_stackvalues(lua_State* L) { luaL_checktype(L, 2, LUA_TTABLE); n = lua_gettop(cL); if(n > 0) { - lua_checkstack(L, n+1); + luaL_checkstack(L, n+1, NULL); int top = lua_gettop(L); lua_xmove(cL, L, n); int i=0; - for(i=0; i Date: Tue, 22 Oct 2019 15:25:39 +0800 Subject: [PATCH 163/565] use traceback --- lualib/skynet.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 7a99ca541..3252f2aef 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -181,7 +181,7 @@ function suspend(co, result, command) session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil skynet.fork(function() end) -- trigger command "SUSPEND" - error(debug.traceback(co,tostring(command))) + error(traceback(co,tostring(command))) end if command == "SUSPEND" then dispatch_wakeup() @@ -191,12 +191,12 @@ function suspend(co, result, command) return elseif command == "USER" then -- See skynet.coutine for detail - error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. debug.traceback(co)) + error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. traceback(co)) elseif command == nil then -- debug trace return else - error("Unknown command : " .. command .. "\n" .. debug.traceback(co)) + error("Unknown command : " .. command .. "\n" .. traceback(co)) end end @@ -722,7 +722,7 @@ local function init_template(start, ...) end function skynet.pcall(start, ...) - return xpcall(init_template, debug.traceback, start, ...) + return xpcall(init_template, traceback, start, ...) end function skynet.init_service(start) @@ -766,7 +766,7 @@ function skynet.task(ret) end if ret == "init" then if init_thread then - return debug.traceback(init_thread) + return traceback(init_thread) else return end @@ -780,7 +780,7 @@ function skynet.task(ret) elseif tt == "number" then local co = session_id_coroutine[ret] if co then - return debug.traceback(co) + return traceback(co) else return "No session" end From 41c66d1b20cc51bcf3a75431bc84dad66333f6bf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 22 Oct 2019 16:10:57 +0800 Subject: [PATCH 164/565] add skynet.trace_timeout(on), see #1098 --- lualib/skynet.lua | 33 +++++++++++++++++++++++++++++++-- test/testtimer.lua | 6 ++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 3252f2aef..47d13398e 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -200,10 +200,35 @@ function suspend(co, result, command) end end +local co_create_for_timeout +local timeout_traceback + +function skynet.trace_timeout(on) + local function trace_coroutine(func, ti) + local co + co = co_create(function() + timeout_traceback[co] = nil + func() + end) + local info = string.format("TIMER %d+%d : ", skynet.now(), ti) + timeout_traceback[co] = traceback(info, 3) + return co + end + if on then + timeout_traceback = timeout_traceback or {} + co_create_for_timeout = trace_coroutine + else + timeout_traceback = nil + co_create_for_timeout = co_create + end +end + +skynet.trace_timeout(false) -- turn off by default + function skynet.timeout(ti, func) local session = c.intcommand("TIMEOUT",ti) assert(session) - local co = co_create(func) + local co = co_create_for_timeout(func, ti) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co return co -- for debug @@ -774,7 +799,11 @@ function skynet.task(ret) local tt = type(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do - ret[session] = traceback(co) + if timeout_traceback and timeout_traceback[co] then + ret[session] = timeout_traceback[co] + else + ret[session] = traceback(co) + end end return elseif tt == "number" then diff --git a/test/testtimer.lua b/test/testtimer.lua index 776aa0dec..2ca2cbe0a 100644 --- a/test/testtimer.lua +++ b/test/testtimer.lua @@ -13,6 +13,11 @@ end local function test() skynet.timeout(10, function() print("test timeout 10") end) + local taskinfo = {} + skynet.task(taskinfo) + for session, info in pairs(taskinfo) do + print("session = ", session, "trace = ", info) + end for i=1,10 do print("test sleep",i,skynet.now()) skynet.sleep(1) @@ -20,6 +25,7 @@ local function test() end skynet.start(function() + skynet.trace_timeout(true) -- trun on trace for timeout, skynet.task will returns more info. test() skynet.fork(wakeup, coroutine.running()) From 066c2c3a545e51a2787a2f18f5ab6e434a859fad Mon Sep 17 00:00:00 2001 From: yxt945 Date: Wed, 23 Oct 2019 15:31:23 +0800 Subject: [PATCH 165/565] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mysql.ping=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=BD=91=E7=BB=9C=E7=8A=B6=E6=80=81=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0blob=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20=E5=A2=9E=E5=8A=A0mysql.execute=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E8=AF=AD=E5=8F=A5=E5=92=8C=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E8=BF=87=E7=A8=8B=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 1847 +++++++++++++++++++++++------------- 1 file changed, 1194 insertions(+), 653 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index a205b7466..440c2ebca 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -1,653 +1,1194 @@ --- Copyright (C) 2012 Yichun Zhang (agentzh) --- 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 "skynet.socketchannel" -local crypt = require "skynet.crypt" - - -local sub = string.sub -local strgsub = string.gsub -local strformat = string.format -local strbyte = string.byte -local strchar = string.char -local strrep = string.rep -local strunpack = string.unpack -local strpack = string.pack -local sha1= crypt.sha1 -local setmetatable = setmetatable -local error = error -local tonumber = tonumber - -local _M = { _VERSION = '0.14' } --- constants - -local COM_QUERY = 0x03 -local SERVER_MORE_RESULTS_EXISTS = 8 - -local mt = { __index = _M } - --- mysql field value type converters -local converters = {} - -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) - return strunpack("= 0 and first <= 250 then - return first, pos + 1 - end - - if first == 251 then - return nil, 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 then - return nil, pos - end - - return sub(data, pos, pos + len - 1), pos + len -end - - -local function _parse_ok_packet(packet) - local res = {} - local pos - - res.affected_rows, pos = _from_length_coded_bin(packet, 2) - - res.insert_id, pos = _from_length_coded_bin(packet, pos) - - res.server_status, pos = _get_byte2(packet, pos) - - res.warning_count, pos = _get_byte2(packet, pos) - - - local message = sub(packet, pos) - if message and message ~= "" then - res.message = message - end - - - 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 = {} - local catalog, db, table, orig_table, orig_name, charsetnr, length - local pos - catalog, pos = _from_length_coded_str(data, 1) - - - 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 = {} - 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 - - if value ~= nil 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) - local packet, typ, err = _recv_packet(self,sock) - if not packet then - return false, "failed to receive the result packet"..err - end - - if typ == 'ERR' then - local errno, msg, sqlstate = _parse_err_packet(packet) - return false, strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate) - end - - if typ == 'EOF' then - return false, "old pre-4.1 authentication protocol not supported" - end - - return true, packet - end -end - - -local function _mysql_login(self,user,password,database,on_connect) - - return function(sockchannel) - local dispatch_resp = _recv_decode_packet_resp(self) - local packet = sockchannel:response( dispatch_resp ) - - self.protocol_ver = strbyte(packet) - - local server_ver, pos = _from_cstring(packet, 2) - if not server_ver then - error "bad handshake initialization packet: bad server version" - end - - self._server_ver = server_ver - - - local thread_id, pos = _get_byte4(packet, pos) - - local scramble1 = sub(packet, pos, pos + 8 - 1) - 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) - - self._server_lang = strbyte(packet, pos) - pos = pos + 1 - - self._server_status, pos = _get_byte2(packet, pos) - - local more_capabilities - more_capabilities, pos = _get_byte2(packet, pos) - - self._server_capabilities = self._server_capabilities|more_capabilities<<16 - - local len = 21 - 8 - 1 - - 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 - local token = _compute_token(password, scramble) - - local client_flags = 260047; - - local req = strpack("= 0 and first <= 250 then + return first, pos + 1 + end + + if first == 251 then + return nil, 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 _set_length_coded_bin(n) + if n<251 then + return strchar(n) + end + + if n0 then + local null_count=mathfloor((arg_num+7)/8) + + local f,ts,vs + local types_buf="" + local values_buf="" + + for _,v in pairs(args) do + f= store_types[type(v)] + if not f then + error("invalid parameter type",type(v)) + end + + ts,vs = f(v) + + types_buf=types_buf..ts + values_buf=values_buf..vs + end + + cmd_packet = strchar(COM_STMT_EXECUTE) + .. _set_byte4(stmt.prepare_id) + .. strchar(cursor_type) + .. _set_byte4(0x01) + ..strrep("\0",null_count) + ..strchar(0x01) + ..types_buf + ..values_buf + else + cmd_packet = strchar(COM_STMT_EXECUTE) + .. _set_byte4(stmt.prepare_id) + .. strchar(cursor_type) + .. _set_byte4(0x01) + end + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_send_long_data(self, prepare_id,arg_id,arg_data) + local cmd_packet = strchar(COM_STMT_SEND_LONG_DATA) + .. _set_byte4(prepare_id) + .. _set_byte2(arg_id) + .. arg_data + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_close(self, prepare_id) + local cmd_packet = strchar(COM_STMT_CLOSE) + .. _set_byte4(prepare_id) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_reset(self, prepare_id) + local cmd_packet = strchar(COM_STMT_RESET) + .. _set_byte4(prepare_id) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_set_option(self,option) + local cmd_packet = strchar(COM_SET_OPTION) + .. _set_byte2(option) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_fetch(self,prepare_id,line_num) + local cmd_packet = strchar(COM_STMT_FETCH) + .. _set_byte4(prepare_id) + .. _set_byte4(line_num) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function read_result(self, sock) + local packet, typ, err = _recv_packet(self, sock) + if not packet then + return nil, err + --error( err ) + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + return nil, msg, errno, sqlstate + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + if typ == 'OK' then + local res = _parse_ok_packet(packet) + if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then + return res, "again" + end + return res + end + + if typ ~= 'DATA' then + return nil, "packet type " .. typ .. " not supported" + --error( "packet type " .. typ .. " not supported" ) + end + + -- typ == 'DATA' + + local field_count, extra = _parse_result_set_header_packet(packet) + + local cols = {} + 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( strformat("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) + return nil, err + end + + if typ ~= 'EOF' then + --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) + return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" + end + + -- typ == 'EOF' + + local compact = self.compact + + local rows = {} + local i = 0 + while true do + packet, typ, err = _recv_packet(self, sock) + if not packet then + --error (err) + return nil, err + end + + if typ == 'EOF' then + local warning_count, status_flags = _parse_eof_packet(packet) + + if 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) + 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 multiresultset = {res} + multiresultset.multiresultset = true + local i =2 + while err =="again" do + res, err, errno, sqlstate = read_result(self,sock) + if not res then + multiresultset.badresult = true + multiresultset.err = err + multiresultset.errno = errno + multiresultset.sqlstate = sqlstate + return true, multiresultset + end + multiresultset[i]=res + i=i+1 + end + return true, multiresultset + end +end + +function _M.connect(opts) + local self = setmetatable( {stmts = {}}, 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,opts.on_connect), + overload = opts.overload, + } + self.sockchannel = channel + -- try connect first only once + channel:connect(true) + + + 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 + if not self.query_resp then + self.query_resp = _query_resp(self) + end + return sockchannel:request( querypacket, self.query_resp ) +end + +local function read_prepare_result(self, sock) + local resp = {} + local packet, typ, err = _recv_packet(self, sock) + if not packet then + resp.badresult = true + resp.errno = 300101 + resp.err = err + return false, resp + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + resp.badresult = true + resp.errno = errno + resp.err = msg + resp.sqlstate = sqlstate + return true, resp + end + + --第一节只能是OK + if typ ~= "OK" then + resp.badresult = true + resp.errno = 300201 + resp.err = "first typ must be OK,now"..typ + return false,resp + end + local pos + resp.prepare_id,pos = _get_byte4(packet,2) + resp.field_count,pos = _get_byte2(packet,pos) + resp.param_count,pos = _get_byte2(packet,pos) + resp.warning_count = _get_byte2(packet,pos+1) + + resp.params = {} + resp.fields = {} + + if resp.param_count >0 then + local param = _recv_field_packet(self,sock) + while param do + table.insert(resp.params,param) + param = _recv_field_packet(self,sock) + end + end + if resp.field_count>0 then + local field = _recv_field_packet(self,sock) + while field do + table.insert(resp.fields,field) + field = _recv_field_packet(self,sock) + end + end + + return true,resp +end + +local function _prepare_resp(self,sql) + return function(sock) + return read_prepare_result(self,sock,sql) + end +end + +-- 注册预处理语句 +function _prepare(self,query) + local stmt = self.stmts[query] + if stmt then + return stmt + end + + local querypacket = _compose_stmt_prepare(self, query) + local sockchannel = self.sockchannel + if not self.prepare_resp then + self.prepare_resp = _prepare_resp(self) + end + stmt = sockchannel:request( querypacket, self.prepare_resp ) + if stmt then + self.stmts[query] = stmt + end + + return stmt +end + +local function _get_datetime(data,pos) + local len,year,month,day,hour,minute,second + local value + len,pos = _from_length_coded_bin(data,pos) + if len==7 then + year,pos=_get_byte2(data,pos) + month,pos=_get_byte1(data,pos) + day,pos=_get_byte1(data,pos) + hour,pos=_get_byte1(data,pos) + minute,pos=_get_byte1(data,pos) + second,pos=_get_byte1(data,pos) + value = strformat("%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second) + else + value = "2017-09-09 20:08:09" + --unsupported format + pos=pos+len + end + return value,pos +end + +local _binary_parser = { + [0x01] = _get_byte1, + [0x02] = _get_byte2, + [0x03] = _get_byte4, + [0x04] = _get_float, + [0x05] = _get_double, + [0x07] = _get_datetime, + [0x08] = _get_byte8, + [0x0c] = _get_datetime, + [0x0f] = _from_length_coded_str, + [0x10] = _from_length_coded_str, + [0xf9] = _from_length_coded_str, + [0xfa] = _from_length_coded_str, + [0xfb] = _from_length_coded_str, + [0xfc] = _from_length_coded_str, + [0xfd] = _from_length_coded_str, + [0xfe] = _from_length_coded_str, +} + +local function _parse_row_data_binary(data, cols, compact) + local ncols = #cols + local null_count=mathfloor((ncols+7+2)/8) + local pos = 2+null_count + local value + + --空字段表 + local null_fields= {} + local field_index=1 + local byte + for i=2,pos-1 do + byte = strbyte(data,i) + for j=0,7 do + if field_index>2 then + if byte&(1< Date: Wed, 23 Oct 2019 16:43:48 +0800 Subject: [PATCH 166/565] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mysql.ping=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=BD=91=E7=BB=9C=E7=8A=B6=E6=80=81=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0blob=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20=E5=A2=9E=E5=8A=A0mysql.execute=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E8=AF=AD=E5=8F=A5=E5=92=8C=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E8=BF=87=E7=A8=8B=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 440c2ebca..c082a36cb 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -881,7 +881,7 @@ local function _prepare_resp(self,sql) end -- 注册预处理语句 -function _prepare(self,query) +local function _prepare(self,query) local stmt = self.stmts[query] if stmt then return stmt From 29e7256948b1abba3ec94ff22f5b7a7a7190d17f Mon Sep 17 00:00:00 2001 From: yxt945 Date: Thu, 24 Oct 2019 11:01:49 +0800 Subject: [PATCH 167/565] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E7=AC=A6=20\r=20=E4=B8=BAunix=E9=A3=8E=E6=A0=BC=20\n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 2388 ++++++++++++++++++------------------ 1 file changed, 1194 insertions(+), 1194 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index c082a36cb..5da1cde46 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -1,1194 +1,1194 @@ --- Copyright (C) 2012 Yichun Zhang (agentzh) --- 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 "skynet.socketchannel" -local crypt = require "skynet.crypt" - - -local mathfloor = math.floor -local sub = string.sub -local strgsub = string.gsub -local strformat = string.format -local strbyte = string.byte -local strchar = string.char -local strrep = string.rep -local strunpack = string.unpack -local strpack = string.pack -local sha1= crypt.sha1 -local setmetatable = setmetatable -local error = error -local tonumber = tonumber - -local _M = { _VERSION = '0.14' } --- constants - -local pow_2_16 = 2^16 -local pow_2_24 = 2^24 - -local STATE_CONNECTED = 1 -local STATE_COMMAND_SENT = 2 - -local COM_QUERY = 0x03 -local COM_PING = 0x0e -local COM_STMT_PREPARE = 0x16 -local COM_STMT_EXECUTE = 0x17 -local COM_STMT_SEND_LONG_DATA = 0x18 -local COM_STMT_CLOSE = 0x19 -local COM_STMT_RESET = 0x1a -local COM_SET_OPTION = 0x1b -local COM_STMT_FETCH = 0x1c - -local CURSOR_TYPE_NO_CURSOR = 0x00 -local CURSOR_TYPE_READ_ONLY = 0x01 -local CURSOR_TYPE_FOR_UPDATE = 0x02 -local CURSOR_TYPE_SCROLLABLE = 0x04 - -local SERVER_MORE_RESULTS_EXISTS = 8 - -local mt = { __index = _M } - --- mysql field value type converters -local converters = {} - -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_byte1(data, i) - return strbyte(data,i),i+1 -end - -local function _get_byte2(data, i) - return strunpack("= 0 and first <= 250 then - return first, pos + 1 - end - - if first == 251 then - return nil, 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 _set_length_coded_bin(n) - if n<251 then - return strchar(n) - end - - if n0 then - local null_count=mathfloor((arg_num+7)/8) - - local f,ts,vs - local types_buf="" - local values_buf="" - - for _,v in pairs(args) do - f= store_types[type(v)] - if not f then - error("invalid parameter type",type(v)) - end - - ts,vs = f(v) - - types_buf=types_buf..ts - values_buf=values_buf..vs - end - - cmd_packet = strchar(COM_STMT_EXECUTE) - .. _set_byte4(stmt.prepare_id) - .. strchar(cursor_type) - .. _set_byte4(0x01) - ..strrep("\0",null_count) - ..strchar(0x01) - ..types_buf - ..values_buf - else - cmd_packet = strchar(COM_STMT_EXECUTE) - .. _set_byte4(stmt.prepare_id) - .. strchar(cursor_type) - .. _set_byte4(0x01) - end - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_send_long_data(self, prepare_id,arg_id,arg_data) - local cmd_packet = strchar(COM_STMT_SEND_LONG_DATA) - .. _set_byte4(prepare_id) - .. _set_byte2(arg_id) - .. arg_data - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_close(self, prepare_id) - local cmd_packet = strchar(COM_STMT_CLOSE) - .. _set_byte4(prepare_id) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_reset(self, prepare_id) - local cmd_packet = strchar(COM_STMT_RESET) - .. _set_byte4(prepare_id) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_set_option(self,option) - local cmd_packet = strchar(COM_SET_OPTION) - .. _set_byte2(option) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_fetch(self,prepare_id,line_num) - local cmd_packet = strchar(COM_STMT_FETCH) - .. _set_byte4(prepare_id) - .. _set_byte4(line_num) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function read_result(self, sock) - local packet, typ, err = _recv_packet(self, sock) - if not packet then - return nil, err - --error( err ) - end - - if typ == "ERR" then - local errno, msg, sqlstate = _parse_err_packet(packet) - return nil, msg, errno, sqlstate - --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) - end - - if typ == 'OK' then - local res = _parse_ok_packet(packet) - if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then - return res, "again" - end - return res - end - - if typ ~= 'DATA' then - return nil, "packet type " .. typ .. " not supported" - --error( "packet type " .. typ .. " not supported" ) - end - - -- typ == 'DATA' - - local field_count, extra = _parse_result_set_header_packet(packet) - - local cols = {} - 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( strformat("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) - return nil, err - end - - if typ ~= 'EOF' then - --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) - return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" - end - - -- typ == 'EOF' - - local compact = self.compact - - local rows = {} - local i = 0 - while true do - packet, typ, err = _recv_packet(self, sock) - if not packet then - --error (err) - return nil, err - end - - if typ == 'EOF' then - local warning_count, status_flags = _parse_eof_packet(packet) - - if 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) - 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 multiresultset = {res} - multiresultset.multiresultset = true - local i =2 - while err =="again" do - res, err, errno, sqlstate = read_result(self,sock) - if not res then - multiresultset.badresult = true - multiresultset.err = err - multiresultset.errno = errno - multiresultset.sqlstate = sqlstate - return true, multiresultset - end - multiresultset[i]=res - i=i+1 - end - return true, multiresultset - end -end - -function _M.connect(opts) - local self = setmetatable( {stmts = {}}, 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,opts.on_connect), - overload = opts.overload, - } - self.sockchannel = channel - -- try connect first only once - channel:connect(true) - - - 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 - if not self.query_resp then - self.query_resp = _query_resp(self) - end - return sockchannel:request( querypacket, self.query_resp ) -end - -local function read_prepare_result(self, sock) - local resp = {} - local packet, typ, err = _recv_packet(self, sock) - if not packet then - resp.badresult = true - resp.errno = 300101 - resp.err = err - return false, resp - end - - if typ == "ERR" then - local errno, msg, sqlstate = _parse_err_packet(packet) - resp.badresult = true - resp.errno = errno - resp.err = msg - resp.sqlstate = sqlstate - return true, resp - end - - --第一节只能是OK - if typ ~= "OK" then - resp.badresult = true - resp.errno = 300201 - resp.err = "first typ must be OK,now"..typ - return false,resp - end - local pos - resp.prepare_id,pos = _get_byte4(packet,2) - resp.field_count,pos = _get_byte2(packet,pos) - resp.param_count,pos = _get_byte2(packet,pos) - resp.warning_count = _get_byte2(packet,pos+1) - - resp.params = {} - resp.fields = {} - - if resp.param_count >0 then - local param = _recv_field_packet(self,sock) - while param do - table.insert(resp.params,param) - param = _recv_field_packet(self,sock) - end - end - if resp.field_count>0 then - local field = _recv_field_packet(self,sock) - while field do - table.insert(resp.fields,field) - field = _recv_field_packet(self,sock) - end - end - - return true,resp -end - -local function _prepare_resp(self,sql) - return function(sock) - return read_prepare_result(self,sock,sql) - end -end - --- 注册预处理语句 -local function _prepare(self,query) - local stmt = self.stmts[query] - if stmt then - return stmt - end - - local querypacket = _compose_stmt_prepare(self, query) - local sockchannel = self.sockchannel - if not self.prepare_resp then - self.prepare_resp = _prepare_resp(self) - end - stmt = sockchannel:request( querypacket, self.prepare_resp ) - if stmt then - self.stmts[query] = stmt - end - - return stmt -end - -local function _get_datetime(data,pos) - local len,year,month,day,hour,minute,second - local value - len,pos = _from_length_coded_bin(data,pos) - if len==7 then - year,pos=_get_byte2(data,pos) - month,pos=_get_byte1(data,pos) - day,pos=_get_byte1(data,pos) - hour,pos=_get_byte1(data,pos) - minute,pos=_get_byte1(data,pos) - second,pos=_get_byte1(data,pos) - value = strformat("%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second) - else - value = "2017-09-09 20:08:09" - --unsupported format - pos=pos+len - end - return value,pos -end - -local _binary_parser = { - [0x01] = _get_byte1, - [0x02] = _get_byte2, - [0x03] = _get_byte4, - [0x04] = _get_float, - [0x05] = _get_double, - [0x07] = _get_datetime, - [0x08] = _get_byte8, - [0x0c] = _get_datetime, - [0x0f] = _from_length_coded_str, - [0x10] = _from_length_coded_str, - [0xf9] = _from_length_coded_str, - [0xfa] = _from_length_coded_str, - [0xfb] = _from_length_coded_str, - [0xfc] = _from_length_coded_str, - [0xfd] = _from_length_coded_str, - [0xfe] = _from_length_coded_str, -} - -local function _parse_row_data_binary(data, cols, compact) - local ncols = #cols - local null_count=mathfloor((ncols+7+2)/8) - local pos = 2+null_count - local value - - --空字段表 - local null_fields= {} - local field_index=1 - local byte - for i=2,pos-1 do - byte = strbyte(data,i) - for j=0,7 do - if field_index>2 then - if byte&(1<= 0 and first <= 250 then + return first, pos + 1 + end + + if first == 251 then + return nil, 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 _set_length_coded_bin(n) + if n<251 then + return strchar(n) + end + + if n0 then + local null_count=mathfloor((arg_num+7)/8) + + local f,ts,vs + local types_buf="" + local values_buf="" + + for _,v in pairs(args) do + f= store_types[type(v)] + if not f then + error("invalid parameter type",type(v)) + end + + ts,vs = f(v) + + types_buf=types_buf..ts + values_buf=values_buf..vs + end + + cmd_packet = strchar(COM_STMT_EXECUTE) + .. _set_byte4(stmt.prepare_id) + .. strchar(cursor_type) + .. _set_byte4(0x01) + ..strrep("\0",null_count) + ..strchar(0x01) + ..types_buf + ..values_buf + else + cmd_packet = strchar(COM_STMT_EXECUTE) + .. _set_byte4(stmt.prepare_id) + .. strchar(cursor_type) + .. _set_byte4(0x01) + end + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_send_long_data(self, prepare_id,arg_id,arg_data) + local cmd_packet = strchar(COM_STMT_SEND_LONG_DATA) + .. _set_byte4(prepare_id) + .. _set_byte2(arg_id) + .. arg_data + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_close(self, prepare_id) + local cmd_packet = strchar(COM_STMT_CLOSE) + .. _set_byte4(prepare_id) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_reset(self, prepare_id) + local cmd_packet = strchar(COM_STMT_RESET) + .. _set_byte4(prepare_id) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_set_option(self,option) + local cmd_packet = strchar(COM_SET_OPTION) + .. _set_byte2(option) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function _compose_stmt_fetch(self,prepare_id,line_num) + local cmd_packet = strchar(COM_STMT_FETCH) + .. _set_byte4(prepare_id) + .. _set_byte4(line_num) + + local packet_len = #cmd_packet + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + return querypacket +end + +local function read_result(self, sock) + local packet, typ, err = _recv_packet(self, sock) + if not packet then + return nil, err + --error( err ) + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + return nil, msg, errno, sqlstate + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + if typ == 'OK' then + local res = _parse_ok_packet(packet) + if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then + return res, "again" + end + return res + end + + if typ ~= 'DATA' then + return nil, "packet type " .. typ .. " not supported" + --error( "packet type " .. typ .. " not supported" ) + end + + -- typ == 'DATA' + + local field_count, extra = _parse_result_set_header_packet(packet) + + local cols = {} + 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( strformat("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) + return nil, err + end + + if typ ~= 'EOF' then + --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) + return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" + end + + -- typ == 'EOF' + + local compact = self.compact + + local rows = {} + local i = 0 + while true do + packet, typ, err = _recv_packet(self, sock) + if not packet then + --error (err) + return nil, err + end + + if typ == 'EOF' then + local warning_count, status_flags = _parse_eof_packet(packet) + + if 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) + 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 multiresultset = {res} + multiresultset.multiresultset = true + local i =2 + while err =="again" do + res, err, errno, sqlstate = read_result(self,sock) + if not res then + multiresultset.badresult = true + multiresultset.err = err + multiresultset.errno = errno + multiresultset.sqlstate = sqlstate + return true, multiresultset + end + multiresultset[i]=res + i=i+1 + end + return true, multiresultset + end +end + +function _M.connect(opts) + local self = setmetatable( {stmts = {}}, 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,opts.on_connect), + overload = opts.overload, + } + self.sockchannel = channel + -- try connect first only once + channel:connect(true) + + + 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 + if not self.query_resp then + self.query_resp = _query_resp(self) + end + return sockchannel:request( querypacket, self.query_resp ) +end + +local function read_prepare_result(self, sock) + local resp = {} + local packet, typ, err = _recv_packet(self, sock) + if not packet then + resp.badresult = true + resp.errno = 300101 + resp.err = err + return false, resp + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + resp.badresult = true + resp.errno = errno + resp.err = msg + resp.sqlstate = sqlstate + return true, resp + end + + --第一节只能是OK + if typ ~= "OK" then + resp.badresult = true + resp.errno = 300201 + resp.err = "first typ must be OK,now"..typ + return false,resp + end + local pos + resp.prepare_id,pos = _get_byte4(packet,2) + resp.field_count,pos = _get_byte2(packet,pos) + resp.param_count,pos = _get_byte2(packet,pos) + resp.warning_count = _get_byte2(packet,pos+1) + + resp.params = {} + resp.fields = {} + + if resp.param_count >0 then + local param = _recv_field_packet(self,sock) + while param do + table.insert(resp.params,param) + param = _recv_field_packet(self,sock) + end + end + if resp.field_count>0 then + local field = _recv_field_packet(self,sock) + while field do + table.insert(resp.fields,field) + field = _recv_field_packet(self,sock) + end + end + + return true,resp +end + +local function _prepare_resp(self,sql) + return function(sock) + return read_prepare_result(self,sock,sql) + end +end + +-- 注册预处理语句 +local function _prepare(self,query) + local stmt = self.stmts[query] + if stmt then + return stmt + end + + local querypacket = _compose_stmt_prepare(self, query) + local sockchannel = self.sockchannel + if not self.prepare_resp then + self.prepare_resp = _prepare_resp(self) + end + stmt = sockchannel:request( querypacket, self.prepare_resp ) + if stmt then + self.stmts[query] = stmt + end + + return stmt +end + +local function _get_datetime(data,pos) + local len,year,month,day,hour,minute,second + local value + len,pos = _from_length_coded_bin(data,pos) + if len==7 then + year,pos=_get_byte2(data,pos) + month,pos=_get_byte1(data,pos) + day,pos=_get_byte1(data,pos) + hour,pos=_get_byte1(data,pos) + minute,pos=_get_byte1(data,pos) + second,pos=_get_byte1(data,pos) + value = strformat("%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second) + else + value = "2017-09-09 20:08:09" + --unsupported format + pos=pos+len + end + return value,pos +end + +local _binary_parser = { + [0x01] = _get_byte1, + [0x02] = _get_byte2, + [0x03] = _get_byte4, + [0x04] = _get_float, + [0x05] = _get_double, + [0x07] = _get_datetime, + [0x08] = _get_byte8, + [0x0c] = _get_datetime, + [0x0f] = _from_length_coded_str, + [0x10] = _from_length_coded_str, + [0xf9] = _from_length_coded_str, + [0xfa] = _from_length_coded_str, + [0xfb] = _from_length_coded_str, + [0xfc] = _from_length_coded_str, + [0xfd] = _from_length_coded_str, + [0xfe] = _from_length_coded_str, +} + +local function _parse_row_data_binary(data, cols, compact) + local ncols = #cols + local null_count=mathfloor((ncols+7+2)/8) + local pos = 2+null_count + local value + + --空字段表 + local null_fields= {} + local field_index=1 + local byte + for i=2,pos-1 do + byte = strbyte(data,i) + for j=0,7 do + if field_index>2 then + if byte&(1< Date: Mon, 28 Oct 2019 14:44:30 +0800 Subject: [PATCH 168/565] =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=8F=98=E9=87=8F=20?= =?UTF-8?q?pow=5F2=5F16,pow=5F2=5F24=EF=BC=8C=E6=94=B9=E4=B8=BA=E7=AB=8B?= =?UTF-8?q?=E5=8D=B3=E6=95=B0=201<<16,1<<24=20=E5=8F=96=E6=B6=88=E9=A2=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=AF=AD=E5=8F=A5=E7=BC=93=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E7=94=B1=E7=94=A8=E6=88=B7=E8=87=AA=E8=A1=8C=E7=AE=A1=E7=90=86?= =?UTF-8?q?=20COM=5FQUERY,COM=5FPING,COM=5FSTMT=5FPREPARE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=AD=97=E7=AC=A6=E7=B1=BB=E5=9E=8B=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=BD=BF=E7=94=A8=E6=97=B6=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 144 ++++++++----------------------------- 1 file changed, 28 insertions(+), 116 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 5da1cde46..02a1e1e4b 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -25,26 +25,13 @@ local tonumber = tonumber local _M = { _VERSION = '0.14' } -- constants -local pow_2_16 = 2^16 -local pow_2_24 = 2^24 - -local STATE_CONNECTED = 1 -local STATE_COMMAND_SENT = 2 - -local COM_QUERY = 0x03 -local COM_PING = 0x0e -local COM_STMT_PREPARE = 0x16 +local COM_QUERY = '\x03' +local COM_PING = '\x0e' +local COM_STMT_PREPARE = '\x16' local COM_STMT_EXECUTE = 0x17 local COM_STMT_SEND_LONG_DATA = 0x18 -local COM_STMT_CLOSE = 0x19 -local COM_STMT_RESET = 0x1a -local COM_SET_OPTION = 0x1b -local COM_STMT_FETCH = 0x1c local CURSOR_TYPE_NO_CURSOR = 0x00 -local CURSOR_TYPE_READ_ONLY = 0x01 -local CURSOR_TYPE_FOR_UPDATE = 0x02 -local CURSOR_TYPE_SCROLLABLE = 0x04 local SERVER_MORE_RESULTS_EXISTS = 8 @@ -233,15 +220,15 @@ local function _set_length_coded_bin(n) return strchar(n) end - if n0 then local null_count=mathfloor((arg_num+7)/8) @@ -579,19 +565,7 @@ local function _compose_stmt_execute(self,stmt,cursor_type,args) values_buf=values_buf..vs end - cmd_packet = strchar(COM_STMT_EXECUTE) - .. _set_byte4(stmt.prepare_id) - .. strchar(cursor_type) - .. _set_byte4(0x01) - ..strrep("\0",null_count) - ..strchar(0x01) - ..types_buf - ..values_buf - else - cmd_packet = strchar(COM_STMT_EXECUTE) - .. _set_byte4(stmt.prepare_id) - .. strchar(cursor_type) - .. _set_byte4(0x01) + cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf end local packet_len = #cmd_packet @@ -612,47 +586,6 @@ local function _compose_stmt_send_long_data(self, prepare_id,arg_id,arg_data) return querypacket end -local function _compose_stmt_close(self, prepare_id) - local cmd_packet = strchar(COM_STMT_CLOSE) - .. _set_byte4(prepare_id) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_reset(self, prepare_id) - local cmd_packet = strchar(COM_STMT_RESET) - .. _set_byte4(prepare_id) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_set_option(self,option) - local cmd_packet = strchar(COM_SET_OPTION) - .. _set_byte2(option) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_fetch(self,prepare_id,line_num) - local cmd_packet = strchar(COM_STMT_FETCH) - .. _set_byte4(prepare_id) - .. _set_byte4(line_num) - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - local function read_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then @@ -776,7 +709,8 @@ local function _query_resp(self) end function _M.connect(opts) - local self = setmetatable( {stmts = {}}, mt) + + local self = setmetatable( {}, mt) local max_packet_size = opts.max_packet_size if not max_packet_size then @@ -881,23 +815,13 @@ local function _prepare_resp(self,sql) end -- 注册预处理语句 -local function _prepare(self,query) - local stmt = self.stmts[query] - if stmt then - return stmt - end - - local querypacket = _compose_stmt_prepare(self, query) +function _M.prepare(self,sql) + local querypacket = _compose_stmt_prepare(self, sql) local sockchannel = self.sockchannel if not self.prepare_resp then self.prepare_resp = _prepare_resp(self) end - stmt = sockchannel:request( querypacket, self.prepare_resp ) - if stmt then - self.stmts[query] = stmt - end - - return stmt + return sockchannel:request( querypacket, self.prepare_resp ) end local function _get_datetime(data,pos) @@ -941,7 +865,8 @@ local _binary_parser = { local function _parse_row_data_binary(data, cols, compact) local ncols = #cols - local null_count=mathfloor((ncols+7+2)/8) + -- 协议空位图 (列数量 + 7 + 2) / 8 + local null_count=mathfloor((ncols+9)/8) local pos = 2+null_count local value @@ -1100,14 +1025,15 @@ local function _execute_resp(self) end --[[ - 执行sql语句 + 执行预处理语句 失败返回字段 errno badresult sqlstate err ]] -function _M.execute(self, sql,...) +function _M.execute(self, stmt,...) + -- 检查参数,不能为nil local p_n = select('#',...) local p_v for i=1,p_n do @@ -1116,26 +1042,12 @@ function _M.execute(self, sql,...) return { badresult=true, errno=30902, - err="prepare sql fail : "..sql, + err="parameter "..i.." is nil", } end end - local args = {...} - - local stmt=_prepare(self,sql) - if not stmt then - return { - badresult=true, - errno=30901, - err="prepare sql fail : "..sql, - } - end - if stmt.badresult then - return stmt - end - - local querypacket,er = _compose_stmt_execute(self,stmt,CURSOR_TYPE_NO_CURSOR,args) + local querypacket,er = _compose_stmt_execute(self,stmt,CURSOR_TYPE_NO_CURSOR,{...}) if not querypacket then return { badresult=true, From d3c4bee0c9b55abd981c6d6cd77d7028272da5b3 Mon Sep 17 00:00:00 2001 From: yxt945 Date: Mon, 28 Oct 2019 17:27:35 +0800 Subject: [PATCH 169/565] =?UTF-8?q?=5Fcompose=5Fpacket=E5=8F=96=E6=B6=88si?= =?UTF-8?q?ze=E5=8F=82=E6=95=B0=EF=BC=8C=E7=9B=B4=E6=8E=A5=E4=BB=8E#req?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=E5=A4=A7=E5=B0=8F=20read?= =?UTF-8?q?=5Fprepare=5Fresult=E4=BD=BF=E7=94=A8string.unpack=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=80=A7=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=20=5Fget?= =?UTF-8?q?=5Fdatetime=E4=BD=BF=E7=94=A8string.unpack=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=80=A7=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=20=5Fparse=5Frow?= =?UTF-8?q?=5Fdata=5Fbinary=E8=AE=A1=E7=AE=97null=5Fcount=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20mathfloor((ncols+9)/8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 50 ++++++++------------------------------ 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 02a1e1e4b..706dac5a4 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -29,7 +29,6 @@ local COM_QUERY = '\x03' local COM_PING = '\x0e' local COM_STMT_PREPARE = '\x16' local COM_STMT_EXECUTE = 0x17 -local COM_STMT_SEND_LONG_DATA = 0x18 local CURSOR_TYPE_NO_CURSOR = 0x00 @@ -132,8 +131,9 @@ local function _compute_token(password, scramble) end) end -local function _compose_packet(self, req, size) +local function _compose_packet(self, req) self.packet_no = self.packet_no + 1 + local size = #req local packet = _set_byte3(size) .. strchar(self.packet_no) .. req return packet @@ -473,9 +473,7 @@ local function _mysql_login(self,user,password,database,on_connect) token, database) - local packet_len = #req - - local authpacket=_compose_packet(self,req,packet_len) + local authpacket=_compose_packet(self,req) sockchannel:request(authpacket, dispatch_resp) if on_connect then on_connect(self) @@ -487,10 +485,7 @@ end local function _compose_ping(self) self.packet_no = -1 - local cmd_packet = COM_PING - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) + local querypacket = _compose_packet(self, COM_PING) return querypacket end @@ -499,9 +494,8 @@ local function _compose_query(self, query) self.packet_no = -1 local cmd_packet = COM_QUERY..query - local packet_len = 1 + #query - local querypacket = _compose_packet(self, cmd_packet, packet_len) + local querypacket = _compose_packet(self, cmd_packet) return querypacket end @@ -509,9 +503,8 @@ local function _compose_stmt_prepare(self, query) self.packet_no = -1 local cmd_packet = COM_STMT_PREPARE .. query - local packet_len = #cmd_packet - local querypacket = _compose_packet(self, cmd_packet, packet_len) + local querypacket = _compose_packet(self, cmd_packet) return querypacket end @@ -568,21 +561,7 @@ local function _compose_stmt_execute(self,stmt,cursor_type,args) cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf end - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) - return querypacket -end - -local function _compose_stmt_send_long_data(self, prepare_id,arg_id,arg_data) - local cmd_packet = strchar(COM_STMT_SEND_LONG_DATA) - .. _set_byte4(prepare_id) - .. _set_byte2(arg_id) - .. arg_data - - local packet_len = #cmd_packet - - local querypacket = _compose_packet(self, cmd_packet, packet_len) + local querypacket = _compose_packet(self, cmd_packet) return querypacket end @@ -781,11 +760,7 @@ local function read_prepare_result(self, sock) resp.err = "first typ must be OK,now"..typ return false,resp end - local pos - resp.prepare_id,pos = _get_byte4(packet,2) - resp.field_count,pos = _get_byte2(packet,pos) - resp.param_count,pos = _get_byte2(packet,pos) - resp.warning_count = _get_byte2(packet,pos+1) + resp.prepare_id,resp.field_count,resp.param_count,resp.warning_count = string.unpack(" Date: Tue, 29 Oct 2019 10:36:26 +0800 Subject: [PATCH 170/565] =?UTF-8?q?COM=5FSTMT=5FEXECUTE=E6=94=B9=E4=B8=BA'?= =?UTF-8?q?\x17'=EF=BC=8C=E4=BF=9D=E6=8C=81COM=5FXXX=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=BB=9F=E4=B8=80=20=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E9=99=A4=E6=B3=95=E6=94=B9=E4=B8=BA//=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E4=BD=BF=E7=94=A8math.floor=20=5Fcompose=5Fpacket?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8string.pack=E7=BC=96?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 706dac5a4..0d5cd4a16 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -28,7 +28,7 @@ local _M = { _VERSION = '0.14' } local COM_QUERY = '\x03' local COM_PING = '\x0e' local COM_STMT_PREPARE = '\x16' -local COM_STMT_EXECUTE = 0x17 +local COM_STMT_EXECUTE = '\x17' local CURSOR_TYPE_NO_CURSOR = 0x00 @@ -135,8 +135,8 @@ local function _compose_packet(self, req) self.packet_no = self.packet_no + 1 local size = #req - local packet = _set_byte3(size) .. strchar(self.packet_no) .. req - return packet + local packet = string.pack("0 then - local null_count=mathfloor((arg_num+7)/8) + local null_count=(arg_num+7)//8 local f,ts,vs local types_buf="" @@ -836,7 +836,7 @@ local _binary_parser = { local function _parse_row_data_binary(data, cols, compact) local ncols = #cols -- 空位图,前两个bit系统保留 (列数量 + 7 + 2) / 8 - local null_count=mathfloor((ncols+9)/8) + local null_count=(ncols+9)//8 local pos = 2+null_count local value From 082430147f1f4177a29c2eed290a5ec90815e9d5 Mon Sep 17 00:00:00 2001 From: yxt945 Date: Tue, 29 Oct 2019 11:44:34 +0800 Subject: [PATCH 171/565] =?UTF-8?q?=E4=BD=BF=E7=94=A8toingeger=E4=BB=A3?= =?UTF-8?q?=E6=9B=BFmath.floor=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 0d5cd4a16..a47f6bbe2 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -8,7 +8,6 @@ local socketchannel = require "skynet.socketchannel" local crypt = require "skynet.crypt" -local mathfloor = math.floor local sub = string.sub local strgsub = string.gsub local strformat = string.format @@ -21,6 +20,7 @@ local sha1= crypt.sha1 local setmetatable = setmetatable local error = error local tonumber = tonumber +local tointeger = math.tointeger local _M = { _VERSION = '0.14' } -- constants @@ -135,7 +135,7 @@ local function _compose_packet(self, req) self.packet_no = self.packet_no + 1 local size = #req - local packet = string.pack("0 then local null_count=(arg_num+7)//8 @@ -760,7 +760,7 @@ local function read_prepare_result(self, sock) resp.err = "first typ must be OK,now"..typ return false,resp end - resp.prepare_id,resp.field_count,resp.param_count,resp.warning_count = string.unpack(" Date: Tue, 29 Oct 2019 14:28:32 +0800 Subject: [PATCH 172/565] remove useless Intermediate variables --- lualib/skynet/db/mysql.lua | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index a47f6bbe2..dd6313b50 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -134,9 +134,7 @@ end local function _compose_packet(self, req) self.packet_no = self.packet_no + 1 local size = #req - - local packet = strpack(" Date: Tue, 29 Oct 2019 14:48:38 +0800 Subject: [PATCH 173/565] may improve Intermediate variables. --- lualib/skynet/db/mysql.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index dd6313b50..f2e3db1c4 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -334,18 +334,17 @@ end local function _parse_row_data_packet(data, cols, compact) + local value, col, conv local pos = 1 local ncols = #cols local row = {} + 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 + col = cols[i] if value ~= nil then - local conv = converters[typ] + conv = converters[col.type] if conv then value = conv(value) end @@ -353,9 +352,8 @@ local function _parse_row_data_packet(data, cols, compact) if compact then row[i] = value - else - row[name] = value + row[col.name] = value end end From 6b4050bf2c77c3cb53704171950eb1ef2587fda9 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Tue, 29 Oct 2019 15:01:29 +0800 Subject: [PATCH 174/565] format up : 0. remove some useless multiple blank lines. 1. uniform indent as white-space. 2. add some expression white-space pad. --- lualib/skynet/db/mysql.lua | 154 ++++++++++--------------------------- 1 file changed, 40 insertions(+), 114 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index f2e3db1c4..73df35a66 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -23,15 +23,13 @@ local tonumber = tonumber local tointeger = math.tointeger local _M = { _VERSION = '0.14' } --- constants +-- constants local COM_QUERY = '\x03' local COM_PING = '\x0e' local COM_STMT_PREPARE = '\x16' local COM_STMT_EXECUTE = '\x17' - local CURSOR_TYPE_NO_CURSOR = 0x00 - local SERVER_MORE_RESULTS_EXISTS = 8 local mt = { __index = _M } @@ -49,43 +47,41 @@ converters[0x0d] = tonumber -- year converters[0xf6] = tonumber -- newdecimal local function _get_byte1(data, i) - return strbyte(data,i),i+1 + return strbyte(data,i),i+1 end local function _get_byte2(data, i) - return strunpack("0 then + if arg_num > 0 then local null_count=(arg_num+7)//8 - local f,ts,vs local types_buf="" local values_buf="" - for _,v in pairs(args) do f= store_types[type(v)] if not f then error("invalid parameter type",type(v)) end - ts,vs = f(v) - types_buf=types_buf..ts values_buf=values_buf..vs end - cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf end @@ -574,7 +519,6 @@ local function read_result(self, sock) -- typ == 'DATA' local field_count, extra = _parse_result_set_header_packet(packet) - local cols = {} for i = 1, field_count do local col, err, errno, sqlstate = _recv_field_packet(self, sock) @@ -582,7 +526,6 @@ local function read_result(self, sock) return nil, err, errno, sqlstate --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - cols[i] = col end @@ -600,7 +543,6 @@ local function read_result(self, sock) -- typ == 'EOF' local compact = self.compact - local rows = {} local i = 0 while true do @@ -612,11 +554,9 @@ local function read_result(self, sock) if typ == 'EOF' then local warning_count, status_flags = _parse_eof_packet(packet) - if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end - break end @@ -667,7 +607,6 @@ local function _query_resp(self) end function _M.connect(opts) - local self = setmetatable( {}, mt) local max_packet_size = opts.max_packet_size @@ -677,7 +616,6 @@ function _M.connect(opts) 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 "" @@ -686,24 +624,20 @@ function _M.connect(opts) host = opts.host, port = opts.port or 3306, auth = _mysql_login(self,user,password,database,opts.on_connect), - overload = opts.overload, + overload = opts.overload, } self.sockchannel = channel -- try connect first only once channel:connect(true) - 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 @@ -843,14 +777,12 @@ local function _parse_row_data_binary(data, cols, compact) local col = cols[i] local typ = col.type local name = col.name - if not null_fields[i] then parser = _binary_parser[typ] if not parser then error("_parse_row_data_binary()error,unsupported field type "..typ) end value,pos = parser(data,pos) - if compact then row[i] = value else @@ -896,17 +828,15 @@ local function read_execute_result(self,sock) local col while true do packet, typ, err = _recv_packet(self, sock) - if typ=="EOF" then + if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) break end - col = _parse_field_packet(packet) if not col then break --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - table.insert(cols,col) end @@ -920,20 +850,17 @@ local function read_execute_result(self,sock) local row while true do packet, typ, err = _recv_packet(self, sock) - - if typ=="EOF" then + if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end break end - row = _parse_row_data_binary(packet,cols,compact) if not col then break end - table.insert(rows,row) end @@ -1032,24 +959,23 @@ function _M.server_ver(self) end local escape_map = { - ['\0'] = "\\0", - ['\b'] = "\\b", - ['\n'] = "\\n", - ['\r'] = "\\r", - ['\t'] = "\\t", - ['\26'] = "\\Z", - ['\\'] = "\\\\", - ["'"] = "\\'", - ['"'] = '\\"', + ['\0'] = "\\0", + ['\b'] = "\\b", + ['\n'] = "\\n", + ['\r'] = "\\r", + ['\t'] = "\\t", + ['\26'] = "\\Z", + ['\\'] = "\\\\", + ["'"] = "\\'", + ['"'] = '\\"', } function _M.quote_sql_str( str) - return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) + return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) end function _M.set_compact_arrays(self, value) self.compact = value end - return _M From 49c21916a231420bb6c4a4fde6d6a609837a1bbb Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Tue, 29 Oct 2019 15:05:00 +0800 Subject: [PATCH 175/565] format up by vscode plugin --- lualib/skynet/db/mysql.lua | 395 +++++++++++++++++++------------------ 1 file changed, 203 insertions(+), 192 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 73df35a66..1a7240b68 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -7,7 +7,6 @@ local socketchannel = require "skynet.socketchannel" local crypt = require "skynet.crypt" - local sub = string.sub local strgsub = string.gsub local strformat = string.format @@ -16,23 +15,23 @@ local strchar = string.char local strrep = string.rep local strunpack = string.unpack local strpack = string.pack -local sha1= crypt.sha1 +local sha1 = crypt.sha1 local setmetatable = setmetatable local error = error local tonumber = tonumber local tointeger = math.tointeger -local _M = { _VERSION = '0.14' } +local _M = {_VERSION = "0.14"} -- constants -local COM_QUERY = '\x03' -local COM_PING = '\x0e' -local COM_STMT_PREPARE = '\x16' -local COM_STMT_EXECUTE = '\x17' +local COM_QUERY = "\x03" +local COM_PING = "\x0e" +local COM_STMT_PREPARE = "\x16" +local COM_STMT_EXECUTE = "\x17" local CURSOR_TYPE_NO_CURSOR = 0x00 local SERVER_MORE_RESULTS_EXISTS = 8 -local mt = { __index = _M } +local mt = {__index = _M} -- mysql field value type converters local converters = {} @@ -41,37 +40,37 @@ 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 +converters[0x08] = tonumber -- long long +converters[0x09] = tonumber -- int24 +converters[0x0d] = tonumber -- year +converters[0xf6] = tonumber -- newdecimal local function _get_byte1(data, i) - return strbyte(data,i),i+1 + return strbyte(data, i), i + 1 end local function _get_byte2(data, i) - return strunpack(" 0 then - local null_count=(arg_num+7)//8 - local f,ts,vs - local types_buf="" - local values_buf="" - for _,v in pairs(args) do - f= store_types[type(v)] + local null_count = (arg_num + 7) // 8 + local f, ts, vs + local types_buf = "" + local values_buf = "" + for _, v in pairs(args) do + f = store_types[type(v)] if not f then - error("invalid parameter type",type(v)) + error("invalid parameter type", type(v)) end - ts,vs = f(v) - types_buf=types_buf..ts - values_buf=values_buf..vs + ts, vs = f(v) + types_buf = types_buf .. ts + values_buf = values_buf .. vs end - cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf + cmd_packet = cmd_packet .. strrep("\0", null_count) .. strchar(0x01) .. types_buf .. values_buf end return _compose_packet(self, cmd_packet) @@ -494,26 +504,26 @@ local function read_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then return nil, err - --error( err ) + --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) return nil, msg, errno, sqlstate - --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - if typ == 'OK' then + if typ == "OK" then local res = _parse_ok_packet(packet) - if res and 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 end - if typ ~= 'DATA' then + if typ ~= "DATA" then return nil, "packet type " .. typ .. " not supported" - --error( "packet type " .. typ .. " not supported" ) + --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' @@ -524,7 +534,7 @@ local function read_result(self, sock) local col, err, errno, sqlstate = _recv_field_packet(self, sock) if not col then return nil, err, errno, sqlstate - --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end cols[i] = col end @@ -535,9 +545,9 @@ local function read_result(self, sock) return nil, err end - if typ ~= 'EOF' then + if typ ~= "EOF" then --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) - return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" + return nil, "unexpected packet type " .. typ .. " while eof packet is " .. "expected" end -- typ == 'EOF' @@ -552,16 +562,16 @@ local function read_result(self, sock) return nil, err end - if typ == 'EOF' then + if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) - if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then + if 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 + -- return nil, 'bad row packet type: ' .. typ -- end -- typ == 'DATA' @@ -574,24 +584,24 @@ local function read_result(self, sock) end local function _query_resp(self) - return function(sock) - local res, err, errno, sqlstate = read_result(self,sock) + return function(sock) + local res, err, errno, sqlstate = read_result(self, sock) if not res then - local badresult ={} + local badresult = {} badresult.badresult = true badresult.err = err badresult.errno = errno badresult.sqlstate = sqlstate - return true , badresult + return true, badresult end if err ~= "again" then return true, res end local multiresultset = {res} multiresultset.multiresultset = true - local i =2 - while err =="again" do - res, err, errno, sqlstate = read_result(self,sock) + local i = 2 + while err == "again" do + res, err, errno, sqlstate = read_result(self, sock) if not res then multiresultset.badresult = true multiresultset.err = err @@ -599,15 +609,15 @@ local function _query_resp(self) multiresultset.sqlstate = sqlstate return true, multiresultset end - multiresultset[i]=res - i=i+1 + multiresultset[i] = res + i = i + 1 end return true, multiresultset end end function _M.connect(opts) - local self = setmetatable( {}, mt) + local self = setmetatable({}, mt) local max_packet_size = opts.max_packet_size if not max_packet_size then @@ -620,11 +630,12 @@ function _M.connect(opts) local user = opts.user or "" local password = opts.password or "" - local channel = socketchannel.channel { + local channel = + socketchannel.channel { host = opts.host, port = opts.port or 3306, - auth = _mysql_login(self,user,password,database,opts.on_connect), - overload = opts.overload, + auth = _mysql_login(self, user, password, database, opts.on_connect), + overload = opts.overload } self.sockchannel = channel -- try connect first only once @@ -644,7 +655,7 @@ function _M.query(self, query) if not self.query_resp then self.query_resp = _query_resp(self) end - return sockchannel:request( querypacket, self.query_resp ) + return sockchannel:request(querypacket, self.query_resp) end local function read_prepare_result(self, sock) @@ -670,61 +681,61 @@ local function read_prepare_result(self, sock) if typ ~= "OK" then resp.badresult = true resp.errno = 300201 - resp.err = "first typ must be OK,now"..typ - return false,resp + resp.err = "first typ must be OK,now" .. typ + return false, resp end - resp.prepare_id,resp.field_count,resp.param_count,resp.warning_count = strunpack("0 then - local param = _recv_field_packet(self,sock) + if resp.param_count > 0 then + local param = _recv_field_packet(self, sock) while param do - table.insert(resp.params,param) - param = _recv_field_packet(self,sock) + table.insert(resp.params, param) + param = _recv_field_packet(self, sock) end end - if resp.field_count>0 then - local field = _recv_field_packet(self,sock) + if resp.field_count > 0 then + local field = _recv_field_packet(self, sock) while field do - table.insert(resp.fields,field) - field = _recv_field_packet(self,sock) + table.insert(resp.fields, field) + field = _recv_field_packet(self, sock) end end - return true,resp + return true, resp end -local function _prepare_resp(self,sql) - return function(sock) - return read_prepare_result(self,sock,sql) +local function _prepare_resp(self, sql) + return function(sock) + return read_prepare_result(self, sock, sql) end end -- 注册预处理语句 -function _M.prepare(self,sql) +function _M.prepare(self, sql) local querypacket = _compose_stmt_prepare(self, sql) local sockchannel = self.sockchannel if not self.prepare_resp then self.prepare_resp = _prepare_resp(self) end - return sockchannel:request( querypacket, self.prepare_resp ) + return sockchannel:request(querypacket, self.prepare_resp) end -local function _get_datetime(data,pos) - local len,year,month,day,hour,minute,second +local function _get_datetime(data, pos) + local len, year, month, day, hour, minute, second local value - len,pos = _from_length_coded_bin(data,pos) - if len==7 then - year,month,day,hour,minute,second,pos=string.unpack("2 then - if byte&(1< 2 then + if byte & (1 << j) == 0 then + null_fields[field_index - 2] = false else - null_fields[field_index-2]=true + null_fields[field_index - 2] = true end end - field_index=field_index+1 + field_index = field_index + 1 end end @@ -780,9 +791,9 @@ local function _parse_row_data_binary(data, cols, compact) if not null_fields[i] then parser = _binary_parser[typ] if not parser then - error("_parse_row_data_binary()error,unsupported field type "..typ) + error("_parse_row_data_binary()error,unsupported field type " .. typ) end - value,pos = parser(data,pos) + value, pos = parser(data, pos) if compact then row[i] = value else @@ -794,30 +805,30 @@ local function _parse_row_data_binary(data, cols, compact) return row end -local function read_execute_result(self,sock) +local function read_execute_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then return nil, err - --error( err ) + --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) return nil, msg, errno, sqlstate - --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - if typ == 'OK' then + if typ == "OK" then local res = _parse_ok_packet(packet) - if res and 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 end - if typ ~= 'DATA' then + if typ ~= "DATA" then return nil, "packet type " .. typ .. " not supported" - --error( "packet type " .. typ .. " not supported" ) + --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' @@ -835,13 +846,13 @@ local function read_execute_result(self,sock) col = _parse_field_packet(packet) if not col then break - --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - table.insert(cols,col) + table.insert(cols, col) end --没有记录集返回 - if #cols<1 then + if #cols < 1 then return {} end @@ -852,40 +863,40 @@ local function read_execute_result(self,sock) packet, typ, err = _recv_packet(self, sock) if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) - if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then + if status_flags & SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end break end - row = _parse_row_data_binary(packet,cols,compact) + row = _parse_row_data_binary(packet, cols, compact) if not col then break end - table.insert(rows,row) + table.insert(rows, row) end return rows end local function _execute_resp(self) - return function(sock) - local res, err, errno, sqlstate = read_execute_result(self,sock) + return function(sock) + local res, err, errno, sqlstate = read_execute_result(self, sock) if not res then - local badresult ={} + local badresult = {} badresult.badresult = true badresult.err = err badresult.errno = errno badresult.sqlstate = sqlstate - return true , badresult + return true, badresult end if err ~= "again" then return true, res end local mulitresultset = {res} mulitresultset.mulitresultset = true - local i =2 - while err =="again" do - res, err, errno, sqlstate = read_execute_result(self,sock) + local i = 2 + while err == "again" do + res, err, errno, sqlstate = read_execute_result(self, sock) if not res then mulitresultset.badresult = true mulitresultset.err = err @@ -893,11 +904,11 @@ local function _execute_resp(self) mulitresultset.sqlstate = sqlstate return true, mulitresultset end - mulitresultset[i]=res - i=i+1 + mulitresultset[i] = res + i = i + 1 end return true, mulitresultset - end + end end --[[ @@ -908,50 +919,50 @@ end sqlstate err ]] -function _M.execute(self, stmt,...) +function _M.execute(self, stmt, ...) -- 检查参数,不能为nil - local p_n = select('#',...) + local p_n = select("#", ...) local p_v - for i=1,p_n do - p_v = select(i,...) - if p_v==nil then + for i = 1, p_n do + p_v = select(i, ...) + if p_v == nil then return { - badresult=true, - errno=30902, - err="parameter "..i.." is nil", + badresult = true, + errno = 30902, + err = "parameter " .. i .. " is nil" } end end - local querypacket,er = _compose_stmt_execute(self,stmt,CURSOR_TYPE_NO_CURSOR,{...}) + local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, {...}) if not querypacket then return { - badresult=true, - errno=30902, - err=er, + badresult = true, + errno = 30902, + err = er } end local sockchannel = self.sockchannel if not self.execute_resp then self.execute_resp = _execute_resp(self) end - return sockchannel:request( querypacket, self.execute_resp ) + return sockchannel:request(querypacket, self.execute_resp) end function _M.ping(self) - local querypacket,er = _compose_ping(self) + local querypacket, er = _compose_ping(self) if not querypacket then return { - badresult=true, - errno=30902, - err=er, + badresult = true, + errno = 30902, + err = er } end local sockchannel = self.sockchannel if not self.query_resp then self.query_resp = _query_resp(self) end - return sockchannel:request( querypacket, self.query_resp ) + return sockchannel:request(querypacket, self.query_resp) end function _M.server_ver(self) @@ -959,19 +970,19 @@ function _M.server_ver(self) end local escape_map = { - ['\0'] = "\\0", - ['\b'] = "\\b", - ['\n'] = "\\n", - ['\r'] = "\\r", - ['\t'] = "\\t", - ['\26'] = "\\Z", - ['\\'] = "\\\\", + ["\0"] = "\\0", + ["\b"] = "\\b", + ["\n"] = "\\n", + ["\r"] = "\\r", + ["\t"] = "\\t", + ["\26"] = "\\Z", + ["\\"] = "\\\\", ["'"] = "\\'", - ['"'] = '\\"', + ['"'] = '\\"' } -function _M.quote_sql_str( str) - return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) +function _M.quote_sql_str(str) + return strformat("'%s'", strgsub(str, '[\0\b\n\r\t\26\\\'"]', escape_map)) end function _M.set_compact_arrays(self, value) From 58291a2a489e77f69e1234ea9a6217244ae0de5f Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Tue, 29 Oct 2019 15:29:28 +0800 Subject: [PATCH 176/565] restore some styles as before. --- lualib/skynet/db/mysql.lua | 57 +++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 1a7240b68..f63963eab 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -102,13 +102,10 @@ local function _from_cstring(data, i) end local function _dumphex(bytes) - return strgsub( - bytes, - ".", + return strgsub(bytes, ".", function(x) return strformat("%02x ", strbyte(x)) - end - ) + end) end local function _compute_token(password, scramble) @@ -123,9 +120,7 @@ local function _compute_token(password, scramble) local stage3 = sha1(scramble .. stage2) local i = 0 - return strgsub( - stage3, - ".", + return strgsub(stage3, ".", function(x) i = i + 1 -- ~ is xor in lua 5.3 @@ -258,7 +253,7 @@ local function _parse_err_packet(packet) local errno, pos = _get_byte2(packet, 2) local marker = sub(packet, pos, pos) local sqlstate - if marker == "#" then + if marker == '#' then -- with sqlstate pos = pos + 1 sqlstate = sub(packet, pos, pos + 5 - 1) @@ -413,9 +408,7 @@ local function _mysql_login(self, user, password, database, on_connect) local scramble = scramble1 .. scramble_part2 local token = _compute_token(password, scramble) local client_flags = 260047 - local req = - strpack( - " Date: Tue, 5 Nov 2019 16:23:11 +0800 Subject: [PATCH 177/565] Add socket_sendbuffer --- lualib-src/lua-socket.c | 62 ++++++++++----- service-src/service_harbor.c | 20 +++-- skynet-src/skynet_socket.c | 12 +-- skynet-src/skynet_socket.h | 38 ++++++++- skynet-src/socket_buffer.h | 17 +++++ skynet-src/socket_server.c | 144 +++++++++++++++++++++++++---------- skynet-src/socket_server.h | 13 ++-- 7 files changed, 225 insertions(+), 81 deletions(-) create mode 100644 skynet-src/socket_buffer.h diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 1cb8dcc26..38f96a5c7 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -525,41 +525,59 @@ concat_table(lua_State *L, int index, void *buffer, size_t tlen) { lua_pop(L,1); } -static void * -get_buffer(lua_State *L, int index, int *sz) { +static void +get_buffer(lua_State *L, int index, struct socket_sendbuffer *buf) { void *buffer; switch(lua_type(L, index)) { - const char * str; size_t len; case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - buffer = lua_touserdata(L,index); - *sz = luaL_checkinteger(L,index+1); + // lua full useobject must be a raw pointer, it can't be a socket object or a memory object. + buf->type = SOCKET_BUFFER_RAWPOINTER; + buf->buffer = lua_touserdata(L, index); + if (lua_isinteger(L, index+1)) { + buf->sz = lua_tointeger(L, index+1); + } else { + buf->sz = lua_rawlen(L, index+1); + } break; + case LUA_TLIGHTUSERDATA: { + int sz = -1; + if (lua_isinteger(L, index+1)) { + sz = lua_tointeger(L,index+1); + } + if (sz < 0) { + buf->type = SOCKET_BUFFER_OBJECT; + } else { + buf->type = SOCKET_BUFFER_MEMORY; + } + buf->buffer = lua_touserdata(L,index); + buf->sz = (size_t)sz; + 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; + buf->type = SOCKET_BUFFER_MEMORY; + buf->buffer = buffer; + buf->sz = len; break; default: - str = luaL_checklstring(L, index, &len); - buffer = skynet_malloc(len); - memcpy(buffer, str, len); - *sz = (int)len; + buf->type = SOCKET_BUFFER_RAWPOINTER; + buf->buffer = luaL_checklstring(L, index, &buf->sz); break; } - return buffer; } static int 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, 2, &sz); - int err = skynet_socket_send(ctx, id, buffer, sz); + struct socket_sendbuffer buf; + buf.id = id; + get_buffer(L, 2, &buf); + int err = skynet_socket_sendbuffer(ctx, &buf); lua_pushboolean(L, !err); return 1; } @@ -568,9 +586,10 @@ static int 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, 2, &sz); - int err = skynet_socket_send_lowpriority(ctx, id, buffer, sz); + struct socket_sendbuffer buf; + buf.id = id; + get_buffer(L, 2, &buf); + int err = skynet_socket_sendbuffer_lowpriority(ctx, &buf); lua_pushboolean(L, !err); return 1; } @@ -645,9 +664,10 @@ 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); + struct socket_sendbuffer buf; + buf.id = id; + get_buffer(L, 3, &buf); + int err = skynet_socket_udp_sendbuffer(ctx, address, &buf); lua_pushboolean(L, !err); diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index acd82235d..4e337344e 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -332,13 +332,19 @@ send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, 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); + uint8_t sendbuf[sz_header+4]; to_bigendian(sendbuf, (uint32_t)sz_header); memcpy(sendbuf+4, buffer, sz); header_to_message(cookie, sendbuf+4+sz); + struct socket_sendbuffer tmp; + tmp.id = fd; + tmp.type = SOCKET_BUFFER_RAWPOINTER; + tmp.buffer = sendbuf; + tmp.sz = sz_header+4; + // ignore send error, because if the connection is broken, the mainloop will recv a message. - skynet_socket_send(ctx, fd, sendbuf, sz_header+4); + skynet_socket_sendbuffer(ctx, &tmp); } static void @@ -580,9 +586,13 @@ remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_L static void 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); + uint8_t handshake[1] = { (uint8_t)h->id }; + struct socket_sendbuffer tmp; + tmp.id = s->fd; + tmp.type = SOCKET_BUFFER_RAWPOINTER; + tmp.buffer = handshake; + tmp.sz = 1; + skynet_socket_sendbuffer(h->ctx, &tmp); } static void diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 1b56024cb..25044b6a6 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -117,13 +117,13 @@ skynet_socket_poll() { } int -skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { - return socket_server_send(SOCKET_SERVER, id, buffer, sz); +skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer) { + return socket_server_send(SOCKET_SERVER, buffer); } int -skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { - return socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); +skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer) { + return socket_server_send_lowpriority(SOCKET_SERVER, buffer); } int @@ -179,8 +179,8 @@ 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) { - return socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz); +skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer) { + return socket_server_udp_send(SOCKET_SERVER, (const struct socket_udp_address *)address, buffer); } const char * diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 2fd38eb31..ff7e23fbb 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -2,6 +2,7 @@ #define skynet_socket_h #include "socket_info.h" +#include "socket_buffer.h" struct skynet_context; @@ -26,8 +27,8 @@ void skynet_socket_free(); int skynet_socket_poll(); void skynet_socket_updatetime(); -int skynet_socket_send(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_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer); +int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer); 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); @@ -38,9 +39,40 @@ 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); +int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); struct socket_info * skynet_socket_info(); +// legacy APIs + +static inline void sendbuffer_init_(struct socket_sendbuffer *buf, int id, const void *buffer, int sz) { + buf->id = id; + buf->buffer = buffer; + if (sz < 0) { + buf->type = SOCKET_BUFFER_OBJECT; + } else { + buf->type = SOCKET_BUFFER_MEMORY; + } + buf->sz = (size_t)sz; +} + +static inline int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { + struct socket_sendbuffer tmp; + sendbuffer_init_(&tmp, id, buffer, sz); + return skynet_socket_sendbuffer(ctx, &tmp); +} + +static inline int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { + struct socket_sendbuffer tmp; + sendbuffer_init_(&tmp, id, buffer, sz); + return skynet_socket_sendbuffer_lowpriority(ctx, &tmp); +} + +static inline int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) { + struct socket_sendbuffer tmp; + sendbuffer_init_(&tmp, id, buffer, sz); + return skynet_socket_udp_sendbuffer(ctx, address, &tmp); +} + #endif diff --git a/skynet-src/socket_buffer.h b/skynet-src/socket_buffer.h new file mode 100644 index 000000000..dbe813564 --- /dev/null +++ b/skynet-src/socket_buffer.h @@ -0,0 +1,17 @@ +#ifndef socket_buffer_h +#define socket_buffer_h + +#include + +#define SOCKET_BUFFER_MEMORY 0 +#define SOCKET_BUFFER_OBJECT 1 +#define SOCKET_BUFFER_RAWPOINTER 2 + +struct socket_sendbuffer { + int id; + int type; + const void *buffer; + size_t sz; +}; + +#endif diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index ddee29431..1ebaa0bde 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -58,11 +58,13 @@ #define WARNING_SIZE (1024*1024) +#define USEROBJECT ((size_t)(~0)) + struct write_buffer { struct write_buffer * next; - void *buffer; + const void *buffer; char *ptr; - int sz; + size_t sz; bool userobject; uint8_t udp_address[UDP_ADDRESS_SIZE]; }; @@ -131,8 +133,8 @@ struct request_open { struct request_send { int id; - int sz; - char * buffer; + size_t sz; + const void * buffer; }; struct request_send_udp { @@ -225,8 +227,8 @@ union sockaddr_all { }; struct send_object { - void * buffer; - int sz; + const void * buffer; + size_t sz; void (*free_func)(void *); }; @@ -272,8 +274,8 @@ socket_unlock(struct socket_lock *sl) { } static inline bool -send_object_init(struct socket_server *ss, struct send_object *so, void *object, int sz) { - if (sz < 0) { +send_object_init(struct socket_server *ss, struct send_object *so, const void *object, size_t sz) { + if (sz == USEROBJECT) { so->buffer = ss->soi.buffer(object); so->sz = ss->soi.size(object); so->free_func = ss->soi.free; @@ -286,12 +288,40 @@ send_object_init(struct socket_server *ss, struct send_object *so, void *object, } } +static void +dummy_free(void *ptr) { + (void)ptr; +} + +static inline void +send_object_init_from_sendbuffer(struct socket_server *ss, struct send_object *so, struct socket_sendbuffer *buf) { + switch (buf->type) { + case SOCKET_BUFFER_MEMORY: + send_object_init(ss, so, (void *)buf->buffer, buf->sz); + break; + case SOCKET_BUFFER_OBJECT: + send_object_init(ss, so, (void *)buf->buffer, USEROBJECT); + break; + case SOCKET_BUFFER_RAWPOINTER: + so->buffer = (void *)buf->buffer; + so->sz = buf->sz; + so->free_func = dummy_free; + break; + default: + // never get here + so->buffer = NULL; + so->sz = 0; + so->free_func = NULL; + break; + } +} + static inline void write_buffer_free(struct socket_server *ss, struct write_buffer *wb) { if (wb->userobject) { - ss->soi.free(wb->buffer); + ss->soi.free((void *)wb->buffer); } else { - FREE(wb->buffer); + FREE((void *)wb->buffer); } FREE(wb); } @@ -400,10 +430,39 @@ free_wb_list(struct socket_server *ss, struct wb_list *list) { } 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); +free_buffer(struct socket_server *ss, struct socket_sendbuffer *buf) { + void *buffer = (void *)buf->buffer; + switch (buf->type) { + case SOCKET_BUFFER_MEMORY: + FREE((void *)buffer); + break; + case SOCKET_BUFFER_OBJECT: + ss->soi.free(buffer); + break; + case SOCKET_BUFFER_RAWPOINTER: + break; + } +} + +static const void * +clone_buffer(struct socket_sendbuffer *buf, size_t *sz) { + switch (buf->type) { + case SOCKET_BUFFER_MEMORY: + *sz = buf->sz; + return buf->buffer; + case SOCKET_BUFFER_OBJECT: + *sz = USEROBJECT; + return buf->buffer; + case SOCKET_BUFFER_RAWPOINTER: + // It's a raw pointer, we need make a copy + *sz = buf->sz; + void * tmp = MALLOC(*sz); + memcpy(tmp, buf->buffer, *sz); + return tmp; + } + // never get here + *sz = 0; + return NULL; } static void @@ -429,7 +488,12 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_lock *l, s } s->type = SOCKET_TYPE_INVALID; if (s->dw_buffer) { - free_buffer(ss, s->dw_buffer, s->dw_size); + struct socket_sendbuffer tmp; + tmp.buffer = s->dw_buffer; + tmp.sz = s->dw_size; + tmp.id = s->id; + tmp.type = (tmp.sz == USEROBJECT) ? SOCKET_BUFFER_OBJECT : SOCKET_BUFFER_MEMORY; + free_buffer(ss, &tmp); s->dw_buffer = NULL; } socket_unlock(l); @@ -849,12 +913,12 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock if (s->type == SOCKET_TYPE_INVALID || s->id != id || s->type == SOCKET_TYPE_HALFCLOSE || s->type == SOCKET_TYPE_PACCEPT) { - so.free_func(request->buffer); + so.free_func((void *)request->buffer); return -1; } 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); + so.free_func((void *)request->buffer); return -1; } if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { @@ -870,7 +934,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock if (sasz == 0) { // udp type mismatch, just drop it. fprintf(stderr, "socket-server: udp socket (%d) type mistach.\n", id); - so.free_func(request->buffer); + so.free_func((void *)request->buffer); return -1; } int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); @@ -878,7 +942,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock append_sendbuffer_udp(ss,s,priority,request,udp_address); } else { stat_write(ss,s,n); - so.free_func(request->buffer); + so.free_func((void *)request->buffer); return -1; } } @@ -1572,10 +1636,11 @@ can_direct_write(struct socket *s, int id) { // return -1 when error, 0 when success int -socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { +socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { + int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { - free_buffer(ss, buffer, sz); + free_buffer(ss, buf); return -1; } @@ -1587,7 +1652,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz if (can_direct_write(s,id)) { // send directly struct send_object so; - send_object_init(ss, &so, (void *)buffer, sz); + send_object_init_from_sendbuffer(ss, &so, buf); ssize_t n; if (s->protocol == PROTOCOL_TCP) { n = write(s->fd, so.buffer, so.sz); @@ -1597,7 +1662,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz if (sasz == 0) { fprintf(stderr, "socket-server : set udp (%d) address first.\n", id); socket_unlock(&l); - so.free_func((void *)buffer); + so.free_func((void *)buf->buffer); return -1; } n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); @@ -1610,12 +1675,11 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz if (n == so.sz) { // write done socket_unlock(&l); - so.free_func((void *)buffer); + so.free_func((void *)buf->buffer); return 0; } // write failed, put buffer into s->dw_* , and let socket thread send it. see send_buffer() - s->dw_buffer = buffer; - s->dw_size = sz; + s->dw_buffer = clone_buffer(buf, &s->dw_size); s->dw_offset = n; sp_write(ss->event_fd, s->fd, s, true); @@ -1630,8 +1694,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz struct request_package request; request.u.send.id = id; - request.u.send.sz = sz; - request.u.send.buffer = (char *)buffer; + request.u.send.buffer = clone_buffer(buf, &request.u.send.sz); send_request(ss, &request, 'D', sizeof(request.u.send)); return 0; @@ -1639,10 +1702,12 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz // return -1 when error, 0 when success int -socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { +socket_server_send_lowpriority(struct socket_server *ss, struct socket_sendbuffer *buf) { + int id = buf->id; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { - free_buffer(ss, buffer, sz); + free_buffer(ss, buf); return -1; } @@ -1650,8 +1715,7 @@ socket_server_send_lowpriority(struct socket_server *ss, int id, const void * bu struct request_package request; request.u.send.id = id; - request.u.send.sz = sz; - request.u.send.buffer = (char *)buffer; + request.u.send.buffer = clone_buffer(buf, &request.u.send.sz); send_request(ss, &request, 'P', sizeof(request.u.send)); return 0; @@ -1836,10 +1900,11 @@ socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, } int -socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) { +socket_server_udp_send(struct socket_server *ss, const struct socket_udp_address *addr, struct socket_sendbuffer *buf) { + int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { - free_buffer(ss, buffer, sz); + free_buffer(ss, buf); return -1; } @@ -1853,7 +1918,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); + free_buffer(ss, buf); return -1; } @@ -1865,12 +1930,12 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp if (can_direct_write(s,id)) { // send directly struct send_object so; - send_object_init(ss, &so, (void *)buffer, sz); + send_object_init_from_sendbuffer(ss, &so, buf); union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { socket_unlock(&l); - so.free_func((void *)buffer); + so.free_func((void *)buf->buffer); return -1; } int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); @@ -1878,7 +1943,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp // sendto succ stat_write(ss,s,n); socket_unlock(&l); - so.free_func((void *)buffer); + so.free_func((void *)buf->buffer); return 0; } } @@ -1888,8 +1953,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp 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; + request.u.send_udp.send.buffer = clone_buffer(buf, &request.u.send_udp.send.sz); memcpy(request.u.send_udp.address, udp_address, addrsz); diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 965685027..cbfe98897 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -3,6 +3,7 @@ #include #include "socket_info.h" +#include "socket_buffer.h" #define SOCKET_DATA 0 #define SOCKET_CLOSE 1 @@ -33,8 +34,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 -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); +int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer); +int socket_server_send_lowpriority(struct socket_server *, struct socket_sendbuffer *buffer); // ctrl command below returns id int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); @@ -53,17 +54,17 @@ 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 -int 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 *, const struct socket_udp_address *, struct socket_sendbuffer *buffer); // 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 *); + const void * (*buffer)(const void *); + size_t (*size)(const void *); void (*free)(void *); }; -// if you send package sz == -1, use soi. +// if you send package with type SOCKET_BUFFER_OBJECT, use soi. void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi); struct socket_info * socket_server_info(struct socket_server *); From 80d1082b428ed64ef28476e3ef798f29a3c5f73f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Nov 2019 17:16:06 +0800 Subject: [PATCH 178/565] bugfix --- lualib-src/lua-socket.c | 2 +- skynet-src/socket_server.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 38f96a5c7..b7fc69ec6 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -537,7 +537,7 @@ get_buffer(lua_State *L, int index, struct socket_sendbuffer *buf) { if (lua_isinteger(L, index+1)) { buf->sz = lua_tointeger(L, index+1); } else { - buf->sz = lua_rawlen(L, index+1); + buf->sz = lua_rawlen(L, index); } break; case LUA_TLIGHTUSERDATA: { diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 1ebaa0bde..0e1fbd13f 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -58,7 +58,7 @@ #define WARNING_SIZE (1024*1024) -#define USEROBJECT ((size_t)(~0)) +#define USEROBJECT ((size_t)(-1)) struct write_buffer { struct write_buffer * next; From 81a7c44a05f6a31a044f4415e26e45699e8a2e55 Mon Sep 17 00:00:00 2001 From: zixun Date: Sat, 20 Jul 2019 06:02:36 -0700 Subject: [PATCH 179/565] add ws/wss server and client support --- examples/simplewebsocket.lua | 90 +++++++ lualib/http/httpc.lua | 77 +----- lualib/http/internal.lua | 81 ++++++ lualib/http/websocket.lua | 465 +++++++++++++++++++++++++++++++++++ 4 files changed, 637 insertions(+), 76 deletions(-) create mode 100755 examples/simplewebsocket.lua create mode 100755 lualib/http/websocket.lua diff --git a/examples/simplewebsocket.lua b/examples/simplewebsocket.lua new file mode 100755 index 000000000..1d798d958 --- /dev/null +++ b/examples/simplewebsocket.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local socket = require "skynet.socket" +local service = require "skynet.service" +local websocket = require "http.websocket" + +local handle = {} +local MODE = ... + +if MODE == "agent" then + function handle.connect(id) + print("ws connect from: " .. tostring(id)) + end + + function handle.handshake(id, header) + print("ws handshake from: " .. tostring(id)) + print("----header-----") + for k,v in pairs(header) do + print(k,v) + end + print("--------------") + end + + function handle.message(id, msg) + websocket.write(id, msg) + end + + function handle.ping(id) + print("ws ping from: " .. tostring(id) .. "\n") + end + + function handle.pong(id) + print("ws pong from: " .. tostring(id)) + end + + function handle.close(id, code, reason) + print("ws close from: " .. tostring(id), code, reason) + end + + function handle.error(id) + print("ws error from: " .. tostring(id)) + end + + skynet.start(function () + skynet.dispatch("lua", function (_,_, id, protocol) + websocket.accept(id, handle, protocol) + end) + end) + +else + local function simple_echo_client_service(protocol) + local skynet = require "skynet" + local websocket = require "http.websocket" + local url = string.format("%s://127.0.0.1:9948/", protocol) + local ws_id = websocket.connect(url) + while true do + local msg = "hello world!" + websocket.write(ws_id, msg) + print(">: " .. msg) + local resp, close_reason = websocket.read(ws_id) + print("<: " .. (resp and resp or "[Close] " .. close_reason)) + if not resp then + print("echo server close.") + break + end + websocket.ping(ws_id) + skynet.sleep(100) + end + end + + skynet.start(function () + local agent = {} + for i= 1, 20 do + agent[i] = skynet.newservice(SERVICE_NAME, "agent") + end + local balance = 1 + local protocol = "ws" + local id = socket.listen("0.0.0.0", 9948) + skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol)) + socket.start(id, function(id, addr) + print(string.format("accept client socket_id: %s addr:%s", id, addr)) + skynet.send(agent[balance], "lua", id, protocol) + balance = balance + 1 + if balance > #agent then + balance = 1 + end + end) + -- test echo client + service.new("websocket_echo_client", simple_echo_client_service, protocol) + end) +end \ No newline at end of file diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index a26a15055..da4e8b024 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -8,81 +8,6 @@ local table = table local httpc = {} -local function request(interface, method, host, url, recvheader, header, content) - local read = interface.read - local write = interface.write - 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 - else - 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\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) - 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 - elseif code == 204 or code == 304 or code < 200 then - body = "" - -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response - else - -- no content-length, read all - body = body .. interface.readall() - end - end - - return code, body -end local async_dns @@ -172,7 +97,7 @@ function httpc.request(method, host, url, recvheader, header, content) if interface.init then interface.init() end - local ok , statuscode, body = pcall(request, interface, method, host, url, recvheader, header, content) + local ok , statuscode, body = pcall(internal.request, interface, method, host, url, recvheader, header, content) finish = true socket.close(fd) if interface.close then diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 6f76b86dc..ec45ff50e 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -141,4 +141,85 @@ function M.recvchunkedbody(readbytes, bodylimit, header, body) return result, header end + +function M.request(interface, method, host, url, recvheader, header, content) + local is_ws = interface.websocket + local read = interface.read + local write = interface.write + 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 + else + 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\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) + end + + local tmpline = {} + local body = M.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 = M.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 = M.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 + elseif code == 204 or code == 304 or code < 200 then + body = "" + -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response + elseif is_ws and code == 101 then + -- if websocket handshake success + return code, body + else + -- no content-length, read all + body = body .. interface.readall() + end + end + + return code, body +end + return M diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua new file mode 100755 index 000000000..81f8d96a3 --- /dev/null +++ b/lualib/http/websocket.lua @@ -0,0 +1,465 @@ +local internal = require "http.internal" +local socket = require "skynet.socket" +local crypt = require "skynet.crypt" +local httpd = require "http.httpd" +local skynet = require "skynet" +local sockethelper = require "http.sockethelper" +local socket_error = sockethelper.socket_error + +local GLOBAL_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" + +local M = {} +local ws_pool = {} + + +local function write_handshake(self, host, url, header) + local key = crypt.base64encode(crypt.randomkey()..crypt.randomkey()) + local request_header = { + ["Upgrade"] = "websocket", + ["Connection"] = "Upgrade", + ["Sec-WebSocket-Version"] = "13", + ["Sec-WebSocket-Key"] = key + } + if header then + for k,v in pairs(header) do + assert(request_header[k] == nil, k) + request_header[k] = v + end + end + + local recvheader = {} + local code, body = internal.request(self, "GET", host, url, recvheader, request_header) + if code ~= 101 then + error(string.format("websocket handshake error: code[%s] info:%s", code, body)) + end + + if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then + error("websocket handshake upgrade must websocket") + end + + if not recvheader["connection"] or recvheader["connection"]:lower() ~= "upgrade" then + error("websocket handshake connection must upgrade") + end + + local sw_key = recvheader["sec-websocket-accept"] + if not sw_key then + error("websocket handshake need Sec-WebSocket-Accept") + end + + local guid = self.guid + sw_key = crypt.base64decode(sw_key) + if sw_key ~= crypt.sha1(key .. guid) then + error("websocket handshake invalid Sec-WebSocket-Accept") + end +end + + +local function read_handshake(self) + local tmpline = {} + local header_body = internal.recvheader(self.read, tmpline, "") + if not header_body then + return 413 + end + + local request = assert(tmpline[1]) + local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" + assert(method and url and httpver) + if method:lower() ~= "get" then + return 400, "need GET method" + end + + httpver = assert(tonumber(httpver)) + if httpver < 1.0 or httpver > 1.1 then + return 505 -- HTTP Version not supported + end + + local header = internal.parseheader(tmpline, 2, {}) + if not header then + return 400 -- Bad request + end + if not header["upgrade"] or header["upgrade"]:lower() ~= "websocket" then + return 426, "Upgrade Required" + end + + if not header["host"] then + return 400, "host Required" + end + + if not header["connection"] or header["connection"]:lower() ~= "upgrade" then + return 400, "Connection must Upgrade" + end + + local sw_key = header["sec-websocket-key"] + if not sw_key then + return 400, "Sec-WebSocket-Key Required" + else + local raw_key = crypt.base64decode(sw_key) + if #raw_key ~= 16 then + return 400, "Sec-WebSocket-Key invalid" + end + end + + if not header["sec-websocket-version"] or header["sec-websocket-version"] ~= "13" then + return 400, "Sec-WebSocket-Version must 13" + end + + local sw_protocol = header["sec-websocket-protocol"] + local sub_pro = "" + if sw_protocol then + for sub_protocol in string.gmatch(sw_protocol, "[^%s,]+") do + if sub_protocol == "chat" then + sub_pro = "Sec-WebSocket-Protocol: chat\r\n" + has_chat = true + break + end + end + if not has_chat then + return 400, "Sec-WebSocket-Protocol need include chat" + end + end + + -- response handshake + local accept = crypt.base64encode(crypt.sha1(sw_key .. self.guid)) + local resp = "HTTP/1.1 101 Switching Protocols\r\n".. + "Upgrade: websocket\r\n".. + "Connection: Upgrade\r\n".. + string.format("Sec-WebSocket-Accept: %s\r\n", accept).. + sub_pro .. + "\r\n" + self.write(resp) + return nil, header +end + +local function try_handle(self, method, ...) + local handle = self.handle + local f = handle and handle[method] + if f then + f(self.id, ...) + end +end + +local op_code = { + ["frame"] = 0x00, + ["text"] = 0x01, + ["binary"] = 0x02, + ["close"] = 0x08, + ["ping"] = 0x09, + ["pong"] = 0x0A, + [0x00] = "frame", + [0x01] = "text", + [0x02] = "binary", + [0x08] = "close", + [0x09] = "ping", + [0x0A] = "pong", +} + +local function write_frame(self, op, payload_data) + payload_data = payload_data or "" + local payload_len = #payload_data + local send_buf = {} + local op_v = assert(op_code[op]) + local v1 = 0x80 | op_v -- fin is 1 with opcode + local s + -- mask set to 0 + if payload_len < 126 then + s = string.pack("I1I1", v1, payload_len) + elseif payload_len < 0xffff then + s = string.pack("I1I1>I2", v1, 126, payload_len) + else + s = string.pack("I1I1>I8", v1, 127, payload_len) + end + + self.write(s) + if payload_len > 0 then + self.write(payload_data) + end +end + + +local function read_frame(self) + local s = self.read(2) + local v1, v2 = string.unpack("I1I1", s) + local fin = (v1 & 0x80) ~= 0 + local rsv1 = (v1 & 0x40) ~= 0 + local rsv2 = (v1 & 0x20) ~= 0 + local rsv3 = (v1 & 0x10) ~= 0 + local op = v1 & 0x0f + local mask = (v2 & 0x80) ~= 0 + local payload_len = (v2 & 0x7f) + if payload_len == 126 then + s = self.read(2) + payload_len = string.unpack(">I2", s) + elseif payload_len == 127 then + s = self.read(8) + payload_len = string.unpack(">I8", s) + end + + -- print("fin, rsv1, rsv2, rsv3, op, mask, payload_len", + -- fin, rsv1, rsv2, rsv3, op, mask, payload_len) + local masking_key + if mask then + s = self.read(4) + local k1, k2, k3, k4 = string.unpack("I1I1I1I1", s) + masking_key = {k1, k2, k3, k4} + end + + local payload_data = payload_len>0 and self.read(payload_len) or "" + if masking_key then + local t = {} + local len = #payload_data + for i=1, len do + local c = string.byte(payload_data, i) + local m = masking_key[(i-1) % 4 + 1] + local v = c ~ m + t[i] = string.char(v) + end + payload_data = table.concat(t) + end + return fin, assert(op_code[op]), payload_data +end + + +local function resolve_accept(self) + try_handle(self, "connect") + local code, err = read_handshake(self) + if code then + local ok, s = httpd.write_response(self.write, code, err) + if not ok then + error(s) + end + end + + local header = err + try_handle(self, "handshake", header) + local recv_buf = {} + while true do + local fin, op, payload_data = read_frame(self) + if op == "close" then + local code, reason + local payload_len = #payload_data + if payload_len > 2 then + local fmt = string.format(">I2s[%d]", payload_len - 2) + code, reason = string.unpack(fmt, payload_data) + end + try_handle(self, "close", code, reason) + break + elseif op == "ping" then + write_frame(self, "pong") + try_handle(self, "ping") + elseif op == "pong" then + try_handle(self, "pong") + else + if fin and #recv_buf == 0 then + try_handle(self, "message", payload_data) + else + recv_buf[#recv_buf+1] = payload_data + if fin then + local s = table.concat(recv_buf) + try_handle(self, "message", s) + recv_buf = {} -- clear recv_buf + end + end + end + end +end + + +local SSLCTX_CLIENT = nil +local function _new_client_ws(socket_id, protocol) + local obj + if protocol == "ws" then + obj = { + websocket = true, + close = function () + socket.close(socket_id) + end, + read = sockethelper.readfunc(socket_id), + write = sockethelper.writefunc(socket_id), + readall = function () + return socket.readall(socket_id) + end, + } + elseif protocol == "wss" then + local tls = require "http.tlshelper" + SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() + local tls_ctx = tls.newtls("client", SSLCTX_CLIENT) + local init = tls.init_requestfunc(socket_id, tls_ctx) + init() + obj = { + websocket = true, + close = function () + socket.close(socket_id) + tls.closefunc(tls_ctx) + end, + read = tls.readfunc(socket_id, tls_ctx), + write = tls.writefunc(socket_id, tls_ctx), + readall = tls.readallfunc(socket_id, tls_ctx), + } + else + error(string.format("invalid websocket protocol:%s", tostring(protocol))) + end + obj.id = assert(socket_id) + obj.guid = GLOBAL_GUID + ws_pool[socket_id] = obj + return obj +end + + +local SSLCTX_SERVER = nil +local function _new_server_ws(socket_id, handle, protocol) + local obj + if protocol == "ws" then + obj = { + close = function () + socket.close(socket_id) + end, + read = sockethelper.readfunc(socket_id), + write = sockethelper.writefunc(socket_id), + } + + elseif protocol == "wss" then + local tls = require "http.tlshelper" + if not SSLCTX_SERVER then + SSLCTX_SERVER = tls.newctx() + -- gen cert and key + -- openssl req -x509 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem -out server-cert.pem + local certfile = skynet.getenv("certfile") or "./server-cert.pem" + local keyfile = skynet.getenv("keyfile") or "./server-key.pem" + SSLCTX_SERVER:set_cert(certfile, keyfile) + end + local tls_ctx = tls.newtls("server", SSLCTX_SERVER) + local init = tls.init_responsefunc(socket_id, tls_ctx) + init() + obj = { + close = function () + socket.close(socket_id) + tls.closefunc(tls_ctx) + end, + read = tls.readfunc(socket_id, tls_ctx), + write = tls.writefunc(socket_id, tls_ctx), + } + + else + error(string.format("invalid websocket protocol:%s", tostring(protocol))) + end + + obj.id = assert(socket_id) + obj.handle = handle + obj.guid = GLOBAL_GUID + ws_pool[socket_id] = obj + return obj +end + + +local function _close_websocket(ws_obj) + local id = ws_obj.id + assert(ws_pool[id] == ws_obj) + ws_pool[id] = nil + ws_obj.close() +end + + +-- handle interface +-- connect / handshake / message / ping / pong / close / error +function M.accept(socket_id, handle, protocol) + socket.start(socket_id) + protocol = protocol or "ws" + local ws_obj = _new_server_ws(socket_id, handle, protocol) + local on_warning = handle and handle["warning"] + if on_warning then + socket.warning(socket_id, function (id, sz) + on_warning(ws_obj, sz) + end) + end + + local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj) + _close_websocket(ws_obj) + if not ok then + if err == socket_error then + try_handle(ws_obj, "error", ws_obj) + else + error(err) + end + end +end + + +function M.connect(url, header) + local protocol, host, uri = string.match(url, "^(wss?)://([^/]+)(.*)$") + if protocol ~= "wss" and protocol ~= "ws" then + error(string.format("invalid protocol: %s", protocol)) + end + + assert(host) + local host_name, host_port = string.match(host, "^([^:]+):?(%d*)$") + assert(host_name and host_port) + if host_port == "" then + host_port = protocol == "ws" and 80 or 443 + end + + uri = uri == "" and "/" or uri + local socket_id = socket.open(host_name, host_port) + assert(socket_id) + local ws_obj = _new_client_ws(socket_id, protocol) + write_handshake(ws_obj, host_name, uri, header) + return socket_id +end + + +function M.read(id) + local ws_obj = assert(ws_pool[id]) + local recv_buf + while true do + local fin, op, payload_data = read_frame(ws_obj) + if op == "close" then + _close_websocket(ws_obj) + return false, payload_data + elseif op == "ping" then + write_frame(ws_obj, "pong") + elseif op ~= "pong" then -- op is frame, text binary + if fin and not recv_buf then + return payload_data + else + recv_buf = recv_buf or {} + recv_buf[#recv_buf+1] = payload_data + if fin then + local s = table.concat(recv_buf) + return s + end + end + end + end + assert(false) +end + + +function M.write(id, data, fmt) + local ws_obj = assert(ws_pool[id]) + fmt = fmt or "text" + assert(fmt == "text" or fmt == "binary") + write_frame(ws_obj, fmt, data) +end + + +function M.ping(id) + local ws_obj = assert(ws_pool[id]) + write_frame(ws_obj, "ping") +end + + +function M.close(id, code ,reason) + local ws_obj = ws_pool[id] + if not ws_obj then + return + end + + pcall(function () + reason = reason or "" + local payload_data = code and string.pack(">I2s", code, reason) or nil + write_frame(ws_obj, "close", payload_data) + end) + _close_websocket(ws_obj) +end + + +return M \ No newline at end of file From 4341106364b8ef68681761ced3e01e6fc42b3eab Mon Sep 17 00:00:00 2001 From: zixun Date: Sun, 21 Jul 2019 16:32:58 +0800 Subject: [PATCH 180/565] fix mask and close --- lualib/http/websocket.lua | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 81f8d96a3..0da38c3d3 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -180,9 +180,10 @@ local function read_frame(self) local s = self.read(2) local v1, v2 = string.unpack("I1I1", s) local fin = (v1 & 0x80) ~= 0 - local rsv1 = (v1 & 0x40) ~= 0 - local rsv2 = (v1 & 0x20) ~= 0 - local rsv3 = (v1 & 0x10) ~= 0 + -- unused flag + -- local rsv1 = (v1 & 0x40) ~= 0 + -- local rsv2 = (v1 & 0x20) ~= 0 + -- local rsv3 = (v1 & 0x10) ~= 0 local op = v1 & 0x0f local mask = (v2 & 0x80) ~= 0 local payload_len = (v2 & 0x7f) @@ -194,27 +195,10 @@ local function read_frame(self) payload_len = string.unpack(">I8", s) end - -- print("fin, rsv1, rsv2, rsv3, op, mask, payload_len", - -- fin, rsv1, rsv2, rsv3, op, mask, payload_len) - local masking_key - if mask then - s = self.read(4) - local k1, k2, k3, k4 = string.unpack("I1I1I1I1", s) - masking_key = {k1, k2, k3, k4} - end - + -- print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) + local masking_key = mask and self.read(4) or false local payload_data = payload_len>0 and self.read(payload_len) or "" - if masking_key then - local t = {} - local len = #payload_data - for i=1, len do - local c = string.byte(payload_data, i) - local m = masking_key[(i-1) % 4 + 1] - local v = c ~ m - t[i] = string.char(v) - end - payload_data = table.concat(t) - end + payload_data = masking_key and crypt.xor_str(payload_data, masking_key) or payload_data return fin, assert(op_code[op]), payload_data end @@ -238,7 +222,7 @@ local function resolve_accept(self) local code, reason local payload_len = #payload_data if payload_len > 2 then - local fmt = string.format(">I2s[%d]", payload_len - 2) + local fmt = string.format(">I2c%d", payload_len - 2) code, reason = string.unpack(fmt, payload_data) end try_handle(self, "close", code, reason) @@ -455,7 +439,11 @@ function M.close(id, code ,reason) pcall(function () reason = reason or "" - local payload_data = code and string.pack(">I2s", code, reason) or nil + local payload_data + if code then + local fmt =string.format(">I2c%d", #reason) + payload_data = string.pack(fmt, code, reason) + end write_frame(ws_obj, "close", payload_data) end) _close_websocket(ws_obj) From 7240de08a9ffc161480daf17b37e654face25ee5 Mon Sep 17 00:00:00 2001 From: zixun Date: Sun, 21 Jul 2019 16:57:50 +0800 Subject: [PATCH 181/565] use sockethelper connect --- lualib/http/websocket.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 0da38c3d3..e62ee9fd0 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -368,7 +368,7 @@ function M.accept(socket_id, handle, protocol) end -function M.connect(url, header) +function M.connect(url, header, timeout) local protocol, host, uri = string.match(url, "^(wss?)://([^/]+)(.*)$") if protocol ~= "wss" and protocol ~= "ws" then error(string.format("invalid protocol: %s", protocol)) @@ -382,8 +382,7 @@ function M.connect(url, header) end uri = uri == "" and "/" or uri - local socket_id = socket.open(host_name, host_port) - assert(socket_id) + local socket_id = sockethelper.connect(host_name, host_port, timeout) local ws_obj = _new_client_ws(socket_id, protocol) write_handshake(ws_obj, host_name, uri, header) return socket_id From 60f4f26b85a8ef49dea23ec07bd7427205185f84 Mon Sep 17 00:00:00 2001 From: zixun Date: Wed, 24 Jul 2019 21:33:05 +0800 Subject: [PATCH 182/565] fix closed and delete send_buf --- lualib/http/websocket.lua | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index e62ee9fd0..828d1831f 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -9,7 +9,19 @@ local socket_error = sockethelper.socket_error local GLOBAL_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" local M = {} + + local ws_pool = {} +local function _close_websocket(ws_obj) + local id = ws_obj.id + assert(ws_pool[id] == ws_obj) + ws_pool[id] = nil + ws_obj.close() +end + +local function _isws_closed(id) + return not ws_pool[id] +end local function write_handshake(self, host, url, header) @@ -156,7 +168,6 @@ local op_code = { local function write_frame(self, op, payload_data) payload_data = payload_data or "" local payload_len = #payload_data - local send_buf = {} local op_v = assert(op_code[op]) local v1 = 0x80 | op_v -- fin is 1 with opcode local s @@ -217,6 +228,9 @@ local function resolve_accept(self) try_handle(self, "handshake", header) local recv_buf = {} while true do + if _isws_closed(self.id) then + return + end local fin, op, payload_data = read_frame(self) if op == "close" then local code, reason @@ -335,14 +349,6 @@ local function _new_server_ws(socket_id, handle, protocol) end -local function _close_websocket(ws_obj) - local id = ws_obj.id - assert(ws_pool[id] == ws_obj) - ws_pool[id] = nil - ws_obj.close() -end - - -- handle interface -- connect / handshake / message / ping / pong / close / error function M.accept(socket_id, handle, protocol) @@ -357,10 +363,15 @@ function M.accept(socket_id, handle, protocol) end local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj) - _close_websocket(ws_obj) + local closed = _isws_closed(socket_id) + if not closed then + _close_websocket(ws_obj) + end if not ok then if err == socket_error then - try_handle(ws_obj, "error", ws_obj) + if not closed then + try_handle(ws_obj, "error", ws_obj) + end else error(err) end From 0e96c761f82b74b9b22d4a6d261c08c5bd8c1855 Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 26 Jul 2019 22:20:03 +0800 Subject: [PATCH 183/565] add write with masked and limit payload --- lualib/http/websocket.lua | 62 +++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 828d1831f..95983f469 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -7,6 +7,7 @@ local sockethelper = require "http.sockethelper" local socket_error = sockethelper.socket_error local GLOBAL_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" +local MAX_FRAME_SIZE = 256 * 1024 -- max frame is 256K local M = {} @@ -76,7 +77,7 @@ local function read_handshake(self) local request = assert(tmpline[1]) local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" assert(method and url and httpver) - if method:lower() ~= "get" then + if method ~= "GET" then return 400, "need GET method" end @@ -165,28 +166,47 @@ local op_code = { [0x0A] = "pong", } -local function write_frame(self, op, payload_data) +local function write_frame(self, op, payload_data, masking_key) payload_data = payload_data or "" local payload_len = #payload_data local op_v = assert(op_code[op]) local v1 = 0x80 | op_v -- fin is 1 with opcode local s + local mask = masking_key and 0x80 or 0x00 -- mask set to 0 if payload_len < 126 then - s = string.pack("I1I1", v1, payload_len) + s = string.pack("I1I1", v1, mask | payload_len) elseif payload_len < 0xffff then - s = string.pack("I1I1>I2", v1, 126, payload_len) + s = string.pack("I1I1>I2", v1, mask | 126, payload_len) else - s = string.pack("I1I1>I8", v1, 127, payload_len) + s = string.pack("I1I1>I8", v1, mask | 127, payload_len) end - self.write(s) + + -- write masking_key + if masking_key then + s = string.pack(">I4", masking_key) + self.write(s) + payload_data = crypt.xor_str(payload_data, s) + end + if payload_len > 0 then self.write(payload_data) end end +local function read_close(payload_data) + local code, reason + local payload_len = #payload_data + if payload_len > 2 then + local fmt = string.format(">I2c%d", payload_len - 2) + code, reason = string.unpack(fmt, payload_data) + end + return code, reason +end + + local function read_frame(self) local s = self.read(2) local v1, v2 = string.unpack("I1I1", s) @@ -206,7 +226,11 @@ local function read_frame(self) payload_len = string.unpack(">I8", s) end - -- print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) + if payload_len > MAX_FRAME_SIZE then + error("payload_len is too large") + end + + print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) local masking_key = mask and self.read(4) or false local payload_data = payload_len>0 and self.read(payload_len) or "" payload_data = masking_key and crypt.xor_str(payload_data, masking_key) or payload_data @@ -233,12 +257,8 @@ local function resolve_accept(self) end local fin, op, payload_data = read_frame(self) if op == "close" then - local code, reason - local payload_len = #payload_data - if payload_len > 2 then - local fmt = string.format(">I2c%d", payload_len - 2) - code, reason = string.unpack(fmt, payload_data) - end + local code, reason = read_close(payload_data) + write_frame(self, "close") try_handle(self, "close", code, reason) break elseif op == "ping" then @@ -370,7 +390,7 @@ function M.accept(socket_id, handle, protocol) if not ok then if err == socket_error then if not closed then - try_handle(ws_obj, "error", ws_obj) + try_handle(ws_obj, "error") end else error(err) @@ -427,11 +447,11 @@ function M.read(id) end -function M.write(id, data, fmt) +function M.write(id, data, fmt, masking_key) local ws_obj = assert(ws_pool[id]) fmt = fmt or "text" assert(fmt == "text" or fmt == "binary") - write_frame(ws_obj, fmt, data) + write_frame(ws_obj, fmt, data, masking_key) end @@ -447,7 +467,7 @@ function M.close(id, code ,reason) return end - pcall(function () + local ok, err = xpcall(function () reason = reason or "" local payload_data if code then @@ -455,8 +475,14 @@ function M.close(id, code ,reason) payload_data = string.pack(fmt, code, reason) end write_frame(ws_obj, "close", payload_data) - end) + -- local fin, op, payload_data = read_frame(ws_obj) + -- assert(fin and op == "close") + -- local code, reason = read_close(payload_data) + end, debug.traceback) _close_websocket(ws_obj) + if not ok then + skynet.error(err) + end end From 8aac2114e37d24acce581e57f7fe05ecee16d50a Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 26 Jul 2019 22:29:33 +0800 Subject: [PATCH 184/565] delete debug print --- lualib/http/websocket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 95983f469..c48f20646 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -230,7 +230,7 @@ local function read_frame(self) error("payload_len is too large") end - print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) + -- print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) local masking_key = mask and self.read(4) or false local payload_data = payload_len>0 and self.read(payload_len) or "" payload_data = masking_key and crypt.xor_str(payload_data, masking_key) or payload_data From b5ea18f44b2a5306850b64a9a30e7cbed446ebe4 Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 26 Jul 2019 22:45:52 +0800 Subject: [PATCH 185/565] check full frame size --- lualib/http/websocket.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index c48f20646..580ac6032 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -250,6 +250,7 @@ local function resolve_accept(self) local header = err try_handle(self, "handshake", header) + local recv_count = 0 local recv_buf = {} while true do if _isws_closed(self.id) then @@ -271,10 +272,15 @@ local function resolve_accept(self) try_handle(self, "message", payload_data) else recv_buf[#recv_buf+1] = payload_data + recv_count = recv_count + #payload_data + if recv_count > MAX_FRAME_SIZE then + error("payload_len is too large") + end if fin then local s = table.concat(recv_buf) try_handle(self, "message", s) recv_buf = {} -- clear recv_buf + recv_count = 0 end end end From 082fb732e57e3ba9843d0a25c342dac9e0e47123 Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 26 Jul 2019 22:49:41 +0800 Subject: [PATCH 186/565] fix httpver --- lualib/http/websocket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 580ac6032..e148d6492 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -82,7 +82,7 @@ local function read_handshake(self) end httpver = assert(tonumber(httpver)) - if httpver < 1.0 or httpver > 1.1 then + if httpver < 1.1 then return 505 -- HTTP Version not supported end From ec8e518abc8762395eef407cf35cc37284ce256c Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 26 Jul 2019 23:01:42 +0800 Subject: [PATCH 187/565] only server websocket check frame size --- lualib/http/websocket.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index e148d6492..0aef92cc0 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -226,7 +226,7 @@ local function read_frame(self) payload_len = string.unpack(">I8", s) end - if payload_len > MAX_FRAME_SIZE then + if self.mode == "server" and payload_len > MAX_FRAME_SIZE then error("payload_len is too large") end @@ -322,6 +322,8 @@ local function _new_client_ws(socket_id, protocol) else error(string.format("invalid websocket protocol:%s", tostring(protocol))) end + + obj.mode = "client" obj.id = assert(socket_id) obj.guid = GLOBAL_GUID ws_pool[socket_id] = obj @@ -367,6 +369,7 @@ local function _new_server_ws(socket_id, handle, protocol) error(string.format("invalid websocket protocol:%s", tostring(protocol))) end + obj.mode = "server" obj.id = assert(socket_id) obj.handle = handle obj.guid = GLOBAL_GUID @@ -481,9 +484,6 @@ function M.close(id, code ,reason) payload_data = string.pack(fmt, code, reason) end write_frame(ws_obj, "close", payload_data) - -- local fin, op, payload_data = read_frame(ws_obj) - -- assert(fin and op == "close") - -- local code, reason = read_close(payload_data) end, debug.traceback) _close_websocket(ws_obj) if not ok then From 21d680d5b776464f1426ae3333bfbc2870a7c397 Mon Sep 17 00:00:00 2001 From: zixun Date: Wed, 31 Jul 2019 14:23:23 +0800 Subject: [PATCH 188/565] triger handle close when force close --- lualib/http/websocket.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 0aef92cc0..50dd6c68c 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -254,6 +254,7 @@ local function resolve_accept(self) local recv_buf = {} while true do if _isws_closed(self.id) then + try_handle(self, "close") return end local fin, op, payload_data = read_frame(self) @@ -398,7 +399,9 @@ function M.accept(socket_id, handle, protocol) end if not ok then if err == socket_error then - if not closed then + if closed then + try_handle(ws_obj, "close") + else try_handle(ws_obj, "error") end else From 38c47b8a5dd6f2f418b3f7cc1c6b7142694cf6df Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 16 Aug 2019 20:07:24 +0800 Subject: [PATCH 189/565] add websocket address and url param --- examples/simplewebsocket.lua | 13 +++++++------ lualib/http/websocket.lua | 14 ++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/simplewebsocket.lua b/examples/simplewebsocket.lua index 1d798d958..fe46d5bfb 100755 --- a/examples/simplewebsocket.lua +++ b/examples/simplewebsocket.lua @@ -11,8 +11,9 @@ if MODE == "agent" then print("ws connect from: " .. tostring(id)) end - function handle.handshake(id, header) - print("ws handshake from: " .. tostring(id)) + function handle.handshake(id, header, url) + local addr = websocket.addrinfo(id) + print("ws handshake from: " .. tostring(id), "url", url, "addr:", addr) print("----header-----") for k,v in pairs(header) do print(k,v) @@ -41,8 +42,8 @@ if MODE == "agent" then end skynet.start(function () - skynet.dispatch("lua", function (_,_, id, protocol) - websocket.accept(id, handle, protocol) + skynet.dispatch("lua", function (_,_, id, protocol, addr) + websocket.accept(id, handle, protocol, addr) end) end) @@ -50,7 +51,7 @@ else local function simple_echo_client_service(protocol) local skynet = require "skynet" local websocket = require "http.websocket" - local url = string.format("%s://127.0.0.1:9948/", protocol) + local url = string.format("%s://127.0.0.1:9948/test_websocket", protocol) local ws_id = websocket.connect(url) while true do local msg = "hello world!" @@ -78,7 +79,7 @@ else skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol)) socket.start(id, function(id, addr) print(string.format("accept client socket_id: %s addr:%s", id, addr)) - skynet.send(agent[balance], "lua", id, protocol) + skynet.send(agent[balance], "lua", id, protocol, addr) balance = balance + 1 if balance > #agent then balance = 1 diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 50dd6c68c..b2bd6c89d 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -140,7 +140,7 @@ local function read_handshake(self) sub_pro .. "\r\n" self.write(resp) - return nil, header + return nil, header, url end local function try_handle(self, method, ...) @@ -240,7 +240,7 @@ end local function resolve_accept(self) try_handle(self, "connect") - local code, err = read_handshake(self) + local code, err, url = read_handshake(self) if code then local ok, s = httpd.write_response(self.write, code, err) if not ok then @@ -249,7 +249,7 @@ local function resolve_accept(self) end local header = err - try_handle(self, "handshake", header) + try_handle(self, "handshake", header, url) local recv_count = 0 local recv_buf = {} while true do @@ -381,10 +381,11 @@ end -- handle interface -- connect / handshake / message / ping / pong / close / error -function M.accept(socket_id, handle, protocol) +function M.accept(socket_id, handle, protocol, addr) socket.start(socket_id) protocol = protocol or "ws" local ws_obj = _new_server_ws(socket_id, handle, protocol) + ws_obj.addr = addr local on_warning = handle and handle["warning"] if on_warning then socket.warning(socket_id, function (id, sz) @@ -427,6 +428,7 @@ function M.connect(url, header, timeout) uri = uri == "" and "/" or uri local socket_id = sockethelper.connect(host_name, host_port, timeout) local ws_obj = _new_client_ws(socket_id, protocol) + ws_obj.addr = host write_handshake(ws_obj, host_name, uri, header) return socket_id end @@ -472,6 +474,10 @@ function M.ping(id) write_frame(ws_obj, "ping") end +function M.addrinfo(id) + local ws_obj = assert(ws_pool[id]) + return ws_obj.addr +end function M.close(id, code ,reason) local ws_obj = ws_pool[id] From 4585da9cbd1daab379b282c952a6a6c5f7231920 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Mon, 30 Sep 2019 11:49:23 +0800 Subject: [PATCH 190/565] bug fixed --- lualib/http/websocket.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index b2bd6c89d..49947109e 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -119,6 +119,7 @@ local function read_handshake(self) local sw_protocol = header["sec-websocket-protocol"] local sub_pro = "" if sw_protocol then + local has_chat = false for sub_protocol in string.gmatch(sw_protocol, "[^%s,]+") do if sub_protocol == "chat" then sub_pro = "Sec-WebSocket-Protocol: chat\r\n" From 8450e7f366ed748f2a758e3bf5ebff6cd9fa8cbd Mon Sep 17 00:00:00 2001 From: zixun Date: Wed, 30 Oct 2019 14:14:28 +0800 Subject: [PATCH 191/565] add result of accept --- examples/simplewebsocket.lua | 5 ++++- lualib/http/websocket.lua | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/simplewebsocket.lua b/examples/simplewebsocket.lua index fe46d5bfb..998482097 100755 --- a/examples/simplewebsocket.lua +++ b/examples/simplewebsocket.lua @@ -43,7 +43,10 @@ if MODE == "agent" then skynet.start(function () skynet.dispatch("lua", function (_,_, id, protocol, addr) - websocket.accept(id, handle, protocol, addr) + local ok, err = websocket.accept(id, handle, protocol, addr) + if not ok then + print(err) + end end) end) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 49947109e..bfecdbe62 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -407,9 +407,11 @@ function M.accept(socket_id, handle, protocol, addr) try_handle(ws_obj, "error") end else - error(err) + -- error(err) + return false, err end end + return true end From a6293f27cd08cbe33b5dc99b561e54659d544d54 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Nov 2019 16:42:03 +0800 Subject: [PATCH 192/565] 1.3.0 rc --- 3rd/jemalloc | 2 +- HISTORY.md | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index b0b3e49a5..ea6b3e973 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit b0b3e49a54ec29e32636f4577d9d5a896d67fd20 +Subproject commit ea6b3e973b477b8061e0076bb257dbd7f3faa756 diff --git a/HISTORY.md b/HISTORY.md index 4b7341bc6..195f0e96a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,16 @@ +v1.3.0 (2019-11-19) +----------- +* Improve mysql driver (@yxt945) +* Improve cluster +* Improve lua shared proto (@hongling0) +* Improve socket.write +* Add lua sharetable +* Add https support (@lvzixun) +* Add websocket support (@lvzixun) +* Fix bug in dns +* Fix some memory leaks +* jemalloc update to 5.2.1 + v1.2.0 (2018-11-6) ----------- * Improve cluster support From 0e3f37f38b148f9dc05f42b203244d85f62d3a60 Mon Sep 17 00:00:00 2001 From: Jay Li Date: Fri, 22 Nov 2019 11:50:38 +0800 Subject: [PATCH 193/565] Call tls.closefunc --- lualib/http/websocket.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index bfecdbe62..58b0c1d8a 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -315,7 +315,7 @@ local function _new_client_ws(socket_id, protocol) websocket = true, close = function () socket.close(socket_id) - tls.closefunc(tls_ctx) + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -361,7 +361,7 @@ local function _new_server_ws(socket_id, handle, protocol) obj = { close = function () socket.close(socket_id) - tls.closefunc(tls_ctx) + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -504,4 +504,4 @@ function M.close(id, code ,reason) end -return M \ No newline at end of file +return M From b7b595e53abf89dc790df27057f03b5592e69057 Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 28 Nov 2019 10:08:59 +0800 Subject: [PATCH 194/565] fix kqueue read with eof --- skynet-src/socket_kqueue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index d154bcdd7..cf5a59ede 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -75,7 +75,7 @@ sp_wait(int kfd, struct event *e, int max) { unsigned filter = ev[i].filter; bool eof = (ev[i].flags & EV_EOF) != 0; e[i].write = (filter == EVFILT_WRITE) && (!eof); - e[i].read = (filter == EVFILT_READ) && (!eof); + e[i].read = (filter == EVFILT_READ); e[i].error = (ev[i].flags & EV_ERROR) != 0; e[i].eof = eof; } From a15c64a73b6c4d1173d033e7f5a2aa57cb5d3632 Mon Sep 17 00:00:00 2001 From: zixun Date: Thu, 28 Nov 2019 21:42:28 +0800 Subject: [PATCH 195/565] fix sharetable update when match self coroutine --- lualib/skynet/sharetable.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 1faf4118e..eb2449a0d 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -421,7 +421,7 @@ local function resolve_replace(replace_map) if not info then break end - info.level = level + info.level = is_self and level + 1 or level info.curco = co match_funcinfo(info) level = level + 1 From b295b875c86020f339b89c7fe3de9ffe754686c1 Mon Sep 17 00:00:00 2001 From: wudeng Date: Fri, 29 Nov 2019 14:47:34 +0800 Subject: [PATCH 196/565] fix sharedata emptyslot --- lualib-src/lua-sharedata.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 16b20872e..7f8cdead7 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -253,6 +253,7 @@ fillcolliding(lua_State *L, struct context *ctx) { for (i=emptyslot;ihash[i].valuetype == VALUETYPE_NIL) { n = &tbl->hash[i]; + emptyslot = i + 1; break; } } From 99a7d745fbcc1f76a2c00beac8355834b0a89a87 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Dec 2019 15:33:01 +0800 Subject: [PATCH 197/565] use socket_channle:changehost, this may also fix #1130 --- service/clusterd.lua | 2 +- service/clustersender.lua | 46 +++++++++------------------------------ 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 658f39263..5bf36e7a4 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -35,7 +35,7 @@ local function open_channel(t, key) local host, port = string.match(address, "([^:]+):(.*)$") c = node_sender[key] if c == nil then - c = skynet.newservice("clustersender", key, nodename) + c = skynet.newservice("clustersender", key, nodename, host, port) if node_sender[key] then -- double check skynet.kill(c) diff --git a/service/clustersender.lua b/service/clustersender.lua index 992373ef6..fe4bde952 100644 --- a/service/clustersender.lua +++ b/service/clustersender.lua @@ -5,11 +5,9 @@ local cluster = require "skynet.cluster.core" local channel local session = 1 -local node, nodename = ... +local node, nodename, init_host, init_port = ... local command = {} -local waiting = {} - local function send_request(addr, msg, sz) -- msg is a local pointer, cluster.packrequest will free it @@ -31,16 +29,7 @@ local function send_request(addr, msg, sz) return channel:request(request, current_session, padding) end -local function wait() - local co = coroutine.running() - table.insert(waiting, co) - skynet.wait(co) -end - function command.req(...) - if channel == nil then - wait() - end local ok, msg = pcall(send_request, ...) if ok then if type(msg) == "table" then @@ -55,9 +44,6 @@ function command.req(...) end function command.push(addr, msg, sz) - if channel == nil then - wait() - end local request, new_session, padding = cluster.packpush(addr, session, msg, sz) if padding then -- is multi push session = new_session @@ -73,30 +59,18 @@ local function read_response(sock) end function command.changenode(host, port) - local c = sc.channel { - host = host, - port = tonumber(port), - response = read_response, - nodelay = true, - } - local succ, err = pcall(c.connect, c, true) - if channel then - channel:close() - end - if succ then - channel = c - for k, co in ipairs(waiting) do - waiting[k] = nil - skynet.wakeup(co) - end - skynet.ret(skynet.pack(nil)) - else - channel = nil -- reset channel - skynet.response()(false) - end + channel:changehost(host, tonumber(port)) + channel:connect(true) + skynet.ret(skynet.pack(nil)) end skynet.start(function() + channel = sc.channel { + host = init_host, + port = tonumber(init_port), + response = read_response, + nodelay = true, + } skynet.dispatch("lua", function(session , source, cmd, ...) local f = assert(command[cmd]) f(...) From b2e50c3bfae594ddaff39b6780a732b96259a4a4 Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Tue, 10 Dec 2019 11:17:42 +0800 Subject: [PATCH 198/565] fixed --- test/testmongodb.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index cda41ec4a..c5175264f 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -25,7 +25,7 @@ function test_auth() host = host, port = port, } ) - db = c[db_name] + local db = c[db_name] db:auth(username, password) db.testdb:dropIndex("*") @@ -86,8 +86,8 @@ function test_find_and_remove() assert(ret and ret.test_key2 == 1, err) 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) + assert(ret:count() == 3) + assert(ret:count(true) == 1) if ret:hasNext() then ret = ret:next() end From 3c7279a6b7b1f1822baad1c6d51efdaac6015f0e Mon Sep 17 00:00:00 2001 From: Hanlei Qin Date: Tue, 10 Dec 2019 11:38:18 +0800 Subject: [PATCH 199/565] 1. disambiguation: testdb --> testcoll (test collection) 2. let `c` as cllient, `db` as `c[db_name]` 3. clear up as lua-check --- test/testmongodb.lua | 85 ++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index c5175264f..c588f9eb1 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -20,6 +20,7 @@ local function _create_client() end function test_auth() + local ok, err, ret local c = mongo.client( { host = host, port = port, @@ -28,64 +29,71 @@ function test_auth() local db = c[db_name] db:auth(username, password) - db.testdb:dropIndex("*") - db.testdb:drop() + db.testcoll:dropIndex("*") + db.testcoll:drop() - local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) - local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) end function test_insert_without_index() - local db = _create_client() - db[db_name].testdb:dropIndex("*") - db[db_name].testdb:drop() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:dropIndex("*") + db.testcoll:drop() - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}); + ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}); + ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) end function test_insert_with_index() - local db = _create_client() + local ok, err, ret + local c = _create_client() + local db = c[db_name] - db[db_name].testdb:dropIndex("*") - db[db_name].testdb:drop() + db.testcoll:dropIndex("*") + db.testcoll:drop() - db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}) - assert(ok and ret and ret.n == 1) + ok, err, ret = db.testcoll:safe_insert({test_key = 1}) + assert(ok and ret and ret.n == 1, err) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1}) - assert(ok == false and string.find(err, "duplicate key error")) + ok, err, ret = db.testcoll:safe_insert({test_key = 1}) + assert(ok == false and string.find(err, "duplicate key error")) end function test_find_and_remove() - local db = _create_client() + local ok, err, ret + local c = _create_client() + local db = c[db_name] - db[db_name].testdb:dropIndex("*") - db[db_name].testdb:drop() + db.testcoll:dropIndex("*") + db.testcoll:drop() - db[db_name].testdb:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) + db.testcoll:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1}) + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) assert(ok and ret and ret.n == 1, err) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2}) + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) assert(ok and ret and ret.n == 1, err) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3}) + ok, err, ret = db.testcoll:safe_insert({test_key = 2, test_key2 = 3}) assert(ok and ret and ret.n == 1, err) - local ret = db[db_name].testdb:findOne({test_key2 = 1}) + ret = db.testcoll:findOne({test_key2 = 1}) assert(ret and ret.test_key2 == 1, err) - local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1) + ret = db.testcoll: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 @@ -93,33 +101,34 @@ function test_find_and_remove() end assert(ret and ret.test_key2 == 1) - db[db_name].testdb:delete({test_key = 1}) - db[db_name].testdb:delete({test_key = 2}) + db.testcoll:delete({test_key = 1}) + db.testcoll:delete({test_key = 2}) - local ret = db[db_name].testdb:findOne({test_key = 1}) + ret = db.testcoll:findOne({test_key = 1}) assert(ret == nil) end - function test_expire_index() - local db = _create_client() + local ok, err, ret + local c = _create_client() + local db = c[db_name] - db[db_name].testdb:dropIndex("*") - db[db_name].testdb:drop() + db.testcoll:dropIndex("*") + db.testcoll: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, }) + db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) + db.testcoll:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) - local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_date = bson.date(os.time())}) assert(ok and ret and ret.n == 1, err) - local ret = db[db_name].testdb:findOne({test_key = 1}) + ret = db.testcoll:findOne({test_key = 1}) assert(ret and ret.test_key == 1) for i = 1, 60 do skynet.sleep(100); print("check expire", i) - local ret = db[db_name].testdb:findOne({test_key = 1}) + ret = db.testcoll:findOne({test_key = 1}) if ret == nil then return end From 5527e9e32ef21c83c8e05ae04f917848ab5f9036 Mon Sep 17 00:00:00 2001 From: fisherman <996442717qqcom@gmail.com> Date: Fri, 13 Dec 2019 10:33:19 +0800 Subject: [PATCH 200/565] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/sharedatad.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 622180f17..014723e28 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -89,7 +89,7 @@ function CMD.delete(name) end function CMD.query(name) - local v = assert(pool[name]) + local v = assert(pool[name], name) local obj = v.obj sharedata.host.incref(obj) return v.obj From ed6dd3967015f143ae3d40b1619f543a37b7edee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=89=E6=9C=8B?= Date: Tue, 17 Dec 2019 18:31:12 +0800 Subject: [PATCH 201/565] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=A2=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=8F=A5=E6=9F=84=E9=87=8D=E7=BD=AE=E5=92=8C=E9=87=8A?= =?UTF-8?q?=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index f63963eab..fa73ab869 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -28,6 +28,8 @@ local COM_QUERY = "\x03" local COM_PING = "\x0e" local COM_STMT_PREPARE = "\x16" local COM_STMT_EXECUTE = "\x17" +local COM_STMT_CLOSE = "\x19" +local COM_STMT_RESET = "\x1a" local CURSOR_TYPE_NO_CURSOR = 0x00 local SERVER_MORE_RESULTS_EXISTS = 8 @@ -942,6 +944,38 @@ function _M.execute(self, stmt, ...) return sockchannel:request(querypacket, self.execute_resp) end +local function _compose_stmt_reset(self, stmt) + self.packet_no = -1 + + local cmd_packet = strpack("c1 Date: Tue, 17 Dec 2019 18:55:47 +0800 Subject: [PATCH 202/565] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=B6=E8=A1=A8?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index fa73ab869..6d92712f5 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -958,7 +958,6 @@ function _M.stmt_reset(self, stmt) if not self.query_resp then self.query_resp = _query_resp(self) end - return sockchannel:request(querypacket, self.query_resp) end local function _compose_stmt_close(self, stmt) From 386c709f425d090f5443b1cf2d0c875c93d35c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=89=E6=9C=8B?= Date: Tue, 17 Dec 2019 18:58:04 +0800 Subject: [PATCH 203/565] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=B6=E8=A1=A8?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 6d92712f5..e701a0aa8 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -945,33 +945,34 @@ function _M.execute(self, stmt, ...) end local function _compose_stmt_reset(self, stmt) - self.packet_no = -1 + self.packet_no = -1 - local cmd_packet = strpack("c1 Date: Tue, 17 Dec 2019 19:10:38 +0800 Subject: [PATCH 204/565] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=8D=95=E5=85=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/testmysql.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/testmysql.lua b/test/testmysql.lua index 0b11aeb70..9649b6607 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -68,6 +68,13 @@ local function test3( db) i=i+1 end end +local function test4( db) + local stmt = db:prepare("SELECT * FROM cats WHERE name=?") + print ( "test4 prepare result=",dump( stmt ) ) + local res = db:execute(stmt,'Bob') + print ( "test4 query result=",dump( res ) ) + db:stmt_close(stmt) +end skynet.start(function() local function on_connect(db) @@ -102,6 +109,7 @@ skynet.start(function() -- test in another coroutine skynet.fork( test2, db) skynet.fork( test3, db) + skynet.fork( test4, db) -- multiresultset test res = db:query("select * from cats order by id asc ; select * from cats") print ("multiresultset test result=", dump( res ) ) From c075de74252b14f4d5f4a19f107573aef9198a16 Mon Sep 17 00:00:00 2001 From: zixun Date: Mon, 23 Dec 2019 14:45:29 +0800 Subject: [PATCH 205/565] use SSL_CTX_use_certificate_chain_file instead of SSL_CTX_use_certificate_file --- lualib-src/ltls.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index f9701a49a..9d352de77 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -284,9 +284,9 @@ _lctx_cert(lua_State* L) { luaL_error(L, "need private key"); } - int ret = SSL_CTX_use_certificate_file(ctx_p->ctx, certfile, SSL_FILETYPE_PEM); + int ret = SSL_CTX_use_certificate_chain_file(ctx_p->ctx, certfile); if(ret != 1) { - luaL_error(L, "SSL_CTX_use_certificate_file error:%d", ret); + luaL_error(L, "SSL_CTX_use_certificate_chain_file error:%d", ret); } ret = SSL_CTX_use_PrivateKey_file(ctx_p->ctx, key, SSL_FILETYPE_PEM); From d232af70f2ef150ea41537de50a0ade92ecd7e3a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 25 Dec 2019 17:58:03 +0800 Subject: [PATCH 206/565] fix #1139 --- lualib-src/lua-multicast.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 4588bd10f..dab90ee2a 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -141,7 +141,8 @@ static int mc_nextid(lua_State *L) { uint32_t id = (uint32_t)luaL_checkinteger(L, 1); id += 256; - lua_pushinteger(L, (uint32_t)id); + // remove the highest bit, see #1139 + lua_pushinteger(L, id & 0x7fffffffu); return 1; } From 672bdcf39f6c2137164a7c7d555db26d8de82e72 Mon Sep 17 00:00:00 2001 From: yxt945 Date: Tue, 17 Dec 2019 21:21:35 +0800 Subject: [PATCH 207/565] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mysql=E5=AD=98?= =?UTF-8?q?=E8=B4=AE=E8=BF=87=E7=A8=8B=E5=92=8Cblob=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/testmysql.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/testmysql.lua b/test/testmysql.lua index 9649b6607..1b3e34854 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -75,6 +75,48 @@ local function test4( db) print ( "test4 query result=",dump( res ) ) db:stmt_close(stmt) end + +-- 测试存储过程和blob读写 +local function test_sp_blob(db) + print("test stored procedure") + -- 创建测试表 + db:query "DROP TABLE IF EXISTS `test`" + db:query [[ + CREATE TABLE `test` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `str` varchar(45) COLLATE utf8mb4_bin DEFAULT NULL, + `dt` timestamp NULL DEFAULT NULL, + `flt` double DEFAULT NULL, + `blb` mediumblob, + `num` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id_UNIQUE` (`id`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; + ]] + -- 创建测试存储过程 + db:query "DROP PROCEDURE IF EXISTS `get_test`" + db:query [[ + CREATE PROCEDURE `get_test`(IN p_id int) + BEGIN + select * from test where id=p_id; + END + ]] + local stmt_insert = db:prepare("INSERT test (str,dt,flt,num,blb) VALUES (?,?,?,?,?)") + local stmt_csp = db:prepare("call get_test(?)") + local test_blob = string.char(0x01,0x02,0x03,0x04,0x0a,0x0b,0x0d,0x0e,0x10,0x20,0x30,0x40) + + local r = db:execute(stmt_insert,'test_str','2020-3-20 15:30:40',3.1415,89,test_blob) + print("insert result : insert_id",r.insert_id,"affected_rows",r.affected_rows + ,"server_status",r.server_status,"warning_count",r.warning_count) + + r = db:execute(stmt_csp,1) + local rs = r[1][1] + print("call get_test() result : str",rs.str,"dt",rs.dt,"flt",rs.flt,"num",rs.num + ,"blb len",#rs.blb,"equal",test_blob==rs.blb) + + print("test stored procedure ok") +end + skynet.start(function() local function on_connect(db) @@ -106,6 +148,9 @@ skynet.start(function() res = db:query("select * from cats order by id asc") print ( dump( res ) ) + -- 测试存储过程和二进制blob + test_sp_blob(db) + -- test in another coroutine skynet.fork( test2, db) skynet.fork( test3, db) From d3a6b8d80bb9ac98154bd582d7dbcdc1ceef5d74 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Dec 2019 10:57:44 +0800 Subject: [PATCH 208/565] fix #1141 --- lualib/skynet/multicast.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/multicast.lua b/lualib/skynet/multicast.lua index d122fead6..07e74df6b 100644 --- a/lualib/skynet/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -71,7 +71,8 @@ local function dispatch_subscribe(channel, source, pack, msg, sz) local self = dispatch[channel] if not self then mc.close(pack) - error ("Unknown channel " .. channel) + -- This channel may unsubscribe first, see #1141 + return end if self.__subscribe then From da4360787f0d8a71f06dd85a35cbf615455c71c3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Jan 2020 09:38:07 +0800 Subject: [PATCH 209/565] Improve socketchannel, try the next host in backup list when auth failed. See issue #1145 --- lualib/skynet/db/mongo.lua | 37 ++----- lualib/skynet/socketchannel.lua | 185 +++++++++++++++++++------------- 2 files changed, 116 insertions(+), 106 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 441b74bf9..42dd9eeed 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -112,38 +112,15 @@ 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 + elseif rs_data.primary then + local host, port = __parse_addr(rs_data.primary) + mongoc.host = host + mongoc.port = port + mongoc.__sock:changehost(host, port) else - 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 + -- socketchannel would try the next host in backup list + error ("No primary return : " .. tostring(rs_data.me)) end end end diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 2fbf08afb..147fe81ba 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -201,27 +201,6 @@ local function dispatch_function(self) end end -local function connect_backup(self) - if self.__backup then - for _, addr in ipairs(self.__backup) do - local host, port - if type(addr) == "table" then - host, port = addr.host, addr.port - else - host = addr - port = self.__port - 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 - end -end - local function term_dispatch_thread(self) if not self.__response and self.__dispatch_thread then -- dispatch by order, send close signal to dispatch thread @@ -233,78 +212,132 @@ local function connect_once(self) if self.__closed then return false end - assert(not self.__sock and not self.__authcoroutine) - -- term current dispatch thread (send a signal) - term_dispatch_thread(self) - local fd,err = socket.open(self.__host, self.__port) - if not fd then - fd = connect_backup(self) - if not fd then - return false, err + local addr_list = {} + local addr_set = {} + + local function _add_backup() + if self.__backup then + for _, addr in ipairs(self.__backup) do + local host, port + if type(addr) == "table" then + host,port = addr.host, addr.port + else + host = addr + port = self.__port + end + + -- don't add the same host + local hostkey = host..":"..port + if not addr_set[hostkey] then + addr_set[hostkey] = true + table.insert(addr_list, { host = host, port = port }) + end + end end end - if self.__nodelay then - socketdriver.nodelay(fd) + + local function _next_addr() + local addr = table.remove(addr_list,1) + if addr then + skynet.error("socket: connect to backup host", addr.host, addr.port) + end + return addr end - -- register overload warning + local function _connect_once(self, addr) + local fd,err = socket.open(addr.host, addr.port) + if not fd then + -- try next one + addr = _next_addr() + if addr == nil then + return false, err + end + return _connect_once(self, addr) + end - local overload = self.__overload_notify - if overload then - local function overload_trigger(id, size) - if id == self.__sock[1] then - if size == 0 then - if self.__overload then - self.__overload = false - overload(false) - end - else - if not self.__overload then - self.__overload = true - overload(true) + self.__host = addr.host + self.__port = addr.port + + assert(not self.__sock and not self.__authcoroutine) + -- term current dispatch thread (send a signal) + term_dispatch_thread(self) + + if self.__nodelay then + socketdriver.nodelay(fd) + end + + -- register overload warning + + local overload = self.__overload_notify + if overload then + local function overload_trigger(id, size) + if id == self.__sock[1] then + if size == 0 then + if self.__overload then + self.__overload = false + overload(false) + end else - skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port)) + if not self.__overload then + self.__overload = true + overload(true) + else + skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port)) + end end end end - end - skynet.fork(overload_trigger, fd, 0) - socket.warning(fd, overload_trigger) - end + skynet.fork(overload_trigger, fd, 0) + socket.warning(fd, overload_trigger) + end - while self.__dispatch_thread do - -- wait for dispatch thread exit - skynet.yield() - end + while self.__dispatch_thread do + -- wait for dispatch thread exit + skynet.yield() + end - self.__sock = setmetatable( {fd} , channel_socket_meta ) - self.__dispatch_thread = skynet.fork(function() - pcall(dispatch_function(self), self) - -- clear dispatch_thread - self.__dispatch_thread = nil - end) - - if self.__auth then - self.__authcoroutine = coroutine.running() - local ok , message = pcall(self.__auth, self) - if not ok then - close_channel_socket(self) - if message ~= socket_error then - self.__authcoroutine = false - skynet.error("socket: auth failed", message) + self.__sock = setmetatable( {fd} , channel_socket_meta ) + self.__dispatch_thread = skynet.fork(function() + pcall(dispatch_function(self), self) + -- clear dispatch_thread + self.__dispatch_thread = nil + end) + + if self.__auth then + self.__authcoroutine = coroutine.running() + local ok , message = pcall(self.__auth, 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 + self.__authcoroutine = false + if ok then + if not self.__sock then + -- auth may change host, so connect again + return connect_once(self) + end + -- auth succ, go through + else + -- auth failed, try next addr + _add_backup() -- auth may add new backup hosts + addr = _next_addr() + if addr == nil then + return false, "no more backup host" + end + return _connect_once(self, addr) end end - self.__authcoroutine = false - if ok and not self.__sock then - -- auth may change host, so connect again - return connect_once(self) - end - return ok + + return true end - return true + _add_backup() + return _connect_once(self, { host = self.__host, port = self.__port }) end local function try_connect(self , once) From 409646a2401908d0c3c99aca1c4da218380dd67c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 19 Jan 2020 20:23:26 +0800 Subject: [PATCH 210/565] happy new year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 5770e4241..146f0a9b8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2017 codingnow.com +Copyright (c) 2012-2020 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 ac38fd620b64c67dc342e23d754b95eae0139592 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Feb 2020 16:08:51 +0800 Subject: [PATCH 211/565] see #1150 --- service/clusteragent.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 0f0d3ec43..3797c04f9 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -81,11 +81,12 @@ local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push) local addr = register_name["@" .. name] if addr then ok = true - msg, sz = skynet.pack(addr) + msg = skynet.packstring(addr) else ok = false msg = "name not found" end + sz = nil else if cluster.isname(addr) then addr = register_name[addr] From 99d43388ab7e0976cba5ba486c9c997c29b1bc7f Mon Sep 17 00:00:00 2001 From: zixun Date: Fri, 14 Feb 2020 18:02:31 +0800 Subject: [PATCH 212/565] fix handshake error --- lualib/http/websocket.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 58b0c1d8a..4f901ceec 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -247,6 +247,8 @@ local function resolve_accept(self) if not ok then error(s) end + try_handle(self, "close") + return end local header = err From 6de4a012a751d1f3b26f1c78e6072f7c99bf6805 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 18 Feb 2020 16:05:47 +0800 Subject: [PATCH 213/565] fix #1154 --- lualib/skynet.lua | 1 + lualib/skynet/remotedebug.lua | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 47d13398e..d1e08b212 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -861,6 +861,7 @@ local debug = require "skynet.debug" debug.init(skynet, { dispatch = skynet.dispatch_message, suspend = suspend, + resume = coroutine_resume, }) return skynet diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index d0c99e274..4cb269ef1 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -14,6 +14,7 @@ local HOOK_FUNC = "raw_dispatch_message" local raw_dispatcher local print = _G.print local skynet_suspend +local skynet_resume local prompt local newline @@ -169,20 +170,20 @@ local dbgcmd = {} function dbgcmd.s(co) local ctx = ctx_active[co] ctx.next_mode = false - skynet_suspend(co, coroutine.resume(co)) + skynet_suspend(co, skynet_resume(co)) end function dbgcmd.n(co) local ctx = ctx_active[co] ctx.next_mode = true - skynet_suspend(co, coroutine.resume(co)) + skynet_suspend(co, skynet_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)) + skynet_suspend(co, skynet_resume(co)) end local function hook_dispatch(dispatcher, resp, fd, channel) @@ -259,6 +260,7 @@ end function M.start(import, fd, handle) local dispatcher = import.dispatch skynet_suspend = import.suspend + skynet_resume = import.resume assert(raw_dispatcher == nil, "Already in debug mode") skynet.error "Enter debug mode" local channel = debugchannel.connect(handle) From 3c425b76bc33d5b1e7e21205ae98ee7e87ec1c4e Mon Sep 17 00:00:00 2001 From: Jay Li Date: Fri, 28 Feb 2020 10:06:50 +0800 Subject: [PATCH 214/565] ignore comment line in resolv.conf --- lualib/skynet/dns.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua index 22b3860fb..3e1129641 100644 --- a/lualib/skynet/dns.lua +++ b/lualib/skynet/dns.lua @@ -161,7 +161,7 @@ local function parse_resolv_conf() local server for line in f:lines() do - server = line:match("%s*nameserver%s+([^#;%s]+)") + server = line:match("^%s*nameserver%s+([^#;%s]+)") if server then break end From f6b8eba01e4e8f66ebab64b6bf8566ce0c5585e0 Mon Sep 17 00:00:00 2001 From: shuax Date: Tue, 3 Mar 2020 14:06:22 +0800 Subject: [PATCH 215/565] https://tools.ietf.org/html/rfc6455#section-5.5.3 A Pong frame sent in response to a Ping frame must have identical "Application data" as found in the message body of the Ping frame being replied to. --- lualib/http/websocket.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 4f901ceec..6b038744c 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -267,7 +267,7 @@ local function resolve_accept(self) try_handle(self, "close", code, reason) break elseif op == "ping" then - write_frame(self, "pong") + write_frame(self, "pong", payload_data) try_handle(self, "ping") elseif op == "pong" then try_handle(self, "pong") @@ -448,7 +448,7 @@ function M.read(id) _close_websocket(ws_obj) return false, payload_data elseif op == "ping" then - write_frame(ws_obj, "pong") + write_frame(ws_obj, "pong", payload_data) elseif op ~= "pong" then -- op is frame, text binary if fin and not recv_buf then return payload_data From 892189ff90912f33f1e30e3f4fbf82bc02c0d720 Mon Sep 17 00:00:00 2001 From: Fanrncho <206867549@qq.com> Date: Fri, 27 Mar 2020 11:35:07 +0800 Subject: [PATCH 216/565] Update websocket.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据Issue(https://github.com/cloudwu/skynet/issues/1163#issue-587403602)提出修改建议。 --- lualib/http/websocket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 6b038744c..c9b8ecd9f 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -98,7 +98,7 @@ local function read_handshake(self) return 400, "host Required" end - if not header["connection"] or header["connection"]:lower() ~= "upgrade" then + if not header["connection"] or not header["connection"]:lower():find("upgrade", 1,true) then return 400, "Connection must Upgrade" end From 7efa79906dd6c935f62cad460faac85761532028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1=28fisherman=29?= <996442717qqcom@gmail.com> Date: Sun, 29 Mar 2020 03:28:18 +0800 Subject: [PATCH 217/565] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20mysql=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/cloudwu/skynet/issues/1165 --- test/testmysql.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index 1b3e34854..0301ae364 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -103,7 +103,7 @@ local function test_sp_blob(db) ]] local stmt_insert = db:prepare("INSERT test (str,dt,flt,num,blb) VALUES (?,?,?,?,?)") local stmt_csp = db:prepare("call get_test(?)") - local test_blob = string.char(0x01,0x02,0x03,0x04,0x0a,0x0b,0x0d,0x0e,0x10,0x20,0x30,0x40) + local test_blob = string.char(0xFF,0x8F,0x03,0x04,0x0a,0x0b,0x0d,0x0e,0x10,0x20,0x30,0x40) local r = db:execute(stmt_insert,'test_str','2020-3-20 15:30:40',3.1415,89,test_blob) print("insert result : insert_id",r.insert_id,"affected_rows",r.affected_rows @@ -120,14 +120,14 @@ end skynet.start(function() local function on_connect(db) - db:query("set charset utf8"); + db:query("set charset utf8mb4"); end local db=mysql.connect({ host="127.0.0.1", port=3306, database="skynet", user="root", - password="1", + password="123456", max_packet_size = 1024 * 1024, on_connect = on_connect }) From fd27676869dbc4ae15d08ea93542d39353d67064 Mon Sep 17 00:00:00 2001 From: zixun Date: Mon, 30 Mar 2020 17:26:06 +0800 Subject: [PATCH 218/565] add message type --- examples/simplewebsocket.lua | 3 ++- lualib/http/websocket.lua | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/simplewebsocket.lua b/examples/simplewebsocket.lua index 998482097..dde6fda54 100755 --- a/examples/simplewebsocket.lua +++ b/examples/simplewebsocket.lua @@ -21,7 +21,8 @@ if MODE == "agent" then print("--------------") end - function handle.message(id, msg) + function handle.message(id, msg, msg_type) + assert(msg_type == "binary" or msg_type == "text") websocket.write(id, msg) end diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index c9b8ecd9f..a2220a493 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -273,7 +273,7 @@ local function resolve_accept(self) try_handle(self, "pong") else if fin and #recv_buf == 0 then - try_handle(self, "message", payload_data) + try_handle(self, "message", payload_data, op) else recv_buf[#recv_buf+1] = payload_data recv_count = recv_count + #payload_data @@ -282,7 +282,7 @@ local function resolve_accept(self) end if fin then local s = table.concat(recv_buf) - try_handle(self, "message", s) + try_handle(self, "message", s, op) recv_buf = {} -- clear recv_buf recv_count = 0 end From 98434708c47e9916306dfe192c8699ba81bb0510 Mon Sep 17 00:00:00 2001 From: xiaojin Date: Wed, 8 Apr 2020 11:07:36 +0800 Subject: [PATCH 219/565] add support for charset encoding --- lualib/skynet/db/mysql.lua | 92 +++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index e701a0aa8..1f04ece56 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -23,6 +23,55 @@ local tointeger = math.tointeger local _M = {_VERSION = "0.14"} +-- the following charset map is generated from the following mysql query: +-- SELECT CHARACTER_SET_NAME, ID +-- FROM information_schema.collations +-- WHERE IS_DEFAULT = 'Yes' ORDER BY id; +local CHARSET_MAP = { + _default = 0, + big5 = 1, + dec8 = 3, + cp850 = 4, + hp8 = 6, + koi8r = 7, + latin1 = 8, + latin2 = 9, + swe7 = 10, + ascii = 11, + ujis = 12, + sjis = 13, + hebrew = 16, + tis620 = 18, + euckr = 19, + koi8u = 22, + gb2312 = 24, + greek = 25, + cp1250 = 26, + gbk = 28, + latin5 = 30, + armscii8 = 32, + utf8 = 33, + ucs2 = 35, + cp866 = 36, + keybcs2 = 37, + macce = 38, + macroman = 39, + cp852 = 40, + latin7 = 41, + utf8mb4 = 45, + cp1251 = 51, + utf16 = 54, + utf16le = 56, + cp1256 = 57, + cp1257 = 59, + utf32 = 60, + binary = 63, + geostd8 = 92, + cp932 = 95, + eucjpms = 97, + gb18030 = 248 +} + -- constants local COM_QUERY = "\x03" local COM_PING = "\x0e" @@ -366,7 +415,7 @@ local function _recv_decode_packet_resp(self) end end -local function _mysql_login(self, user, password, database, on_connect) +local function _mysql_login(self, user, password, charset, database, on_connect) return function(sockchannel) local dispatch_resp = _recv_decode_packet_resp(self) local packet = sockchannel:response(dispatch_resp) @@ -410,10 +459,11 @@ local function _mysql_login(self, user, password, database, on_connect) local scramble = scramble1 .. scramble_part2 local token = _compute_token(password, scramble) local client_flags = 260047 - local req = strpack(" Date: Thu, 9 Apr 2020 09:24:34 +0800 Subject: [PATCH 220/565] change mysql default charset --- lualib/skynet/db/mysql.lua | 2 +- test/testmysql.lua | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 1f04ece56..cb67f7dce 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -674,7 +674,7 @@ function _M.connect(opts) local database = opts.database or "" local user = opts.user or "" local password = opts.password or "" - local charset = CHARSET_MAP[opts.charset or "utf8mb4"] + local charset = CHARSET_MAP[opts.charset or "_default"] local channel = socketchannel.channel { host = opts.host, diff --git a/test/testmysql.lua b/test/testmysql.lua index 0301ae364..e0c95e203 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -128,6 +128,7 @@ skynet.start(function() database="skynet", user="root", password="123456", + charset="utf8mb4", max_packet_size = 1024 * 1024, on_connect = on_connect }) From 9222ff8d7af8bb24649e5bbc36d3bb467d7a7fe7 Mon Sep 17 00:00:00 2001 From: xiaojin Date: Thu, 9 Apr 2020 09:38:11 +0800 Subject: [PATCH 221/565] del charset todo --- lualib/skynet/db/mysql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index cb67f7dce..6ac2797d5 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -463,7 +463,7 @@ local function _mysql_login(self, user, password, charset, database, on_connect) client_flags, self._max_packet_size, strchar(charset), - strrep("\0", 23), -- TODO: add support for charset encoding + strrep("\0", 23), user, token, database From 22f25bad556fb34975c828b2646e6bb6013217b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1?= <996442717qqcom@gmail.com> Date: Thu, 9 Apr 2020 10:04:59 +0800 Subject: [PATCH 222/565] format. --- test/testmysql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index e0c95e203..edd457bb8 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -128,7 +128,7 @@ skynet.start(function() database="skynet", user="root", password="123456", - charset="utf8mb4", + charset="utf8mb4", max_packet_size = 1024 * 1024, on_connect = on_connect }) From 2389772c73b5cabf01e338ad56fa2288ea69a05b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 23 Apr 2020 11:45:35 +0800 Subject: [PATCH 223/565] add padding mode for DES, see #1179 --- lualib-src/lua-crypt.c | 128 ++++++++++++++++++++++++++++++++++------- test/testcrypt.lua | 24 ++++++++ 2 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 test/testcrypt.lua diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 6f99d4f67..77ba7c40f 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -9,6 +9,10 @@ #include #include +#define PADDING_MODE_ISO7816_4 0 +#define PADDING_MODE_PKCS7 1 +#define PADDING_MODE_COUNT 2 + #define SMALL_CHUNK 256 /* the eight DES S-boxes */ @@ -353,6 +357,97 @@ lrandomkey(lua_State *L) { return 1; } +static void +padding_mode_table(lua_State *L) { + // see macros PADDING_MODE_ISO7816_4, etc. + const char * mode[] = { + "iso7816_4", + "pkcs7", + }; + int n = sizeof(mode) / sizeof(mode[0]); + int i; + lua_createtable(L,0,n); + for (i=0;i= PADDING_MODE_COUNT) + luaL_error(L, "Invalid padding mode %d", mode); +} + +static void +add_padding(lua_State *L, uint8_t buf[8], const uint8_t *src, int offset, int mode) { + check_padding_mode(L, mode); + if (offset >= 8) + luaL_error(L, "Invalid padding"); + memcpy(buf, src, offset); + padding_add_func[mode](buf, offset); +} + +static int +remove_padding(lua_State *L, const uint8_t *last, int mode) { + check_padding_mode(L, mode); + return padding_remove_func[mode](last); +} + static void des_key(lua_State *L, uint32_t SK[32]) { size_t keysz = 0; @@ -371,6 +466,7 @@ ldesencode(lua_State *L) { size_t textsz = 0; const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); size_t chunksz = (textsz + 8) & ~7; + int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4); uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (chunksz > SMALL_CHUNK) { @@ -380,18 +476,8 @@ ldesencode(lua_State *L) { 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; - } - } + add_padding(L, tail, text+i, textsz - i, padding_mode); des_crypt(SK, tail, buffer+i); lua_pushlstring(L, (const char *)buffer, chunksz); @@ -413,6 +499,7 @@ ldesdecode(lua_State *L) { if ((textsz & 7) || textsz == 0) { return luaL_error(L, "Invalid des crypt text length %d", (int)textsz); } + int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4); uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (textsz > SMALL_CHUNK) { @@ -421,17 +508,8 @@ ldesdecode(lua_State *L) { 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) { + int padding = remove_padding(L, buffer + textsz - 1, padding_mode); + if (padding <= 0 || padding > 8) { return luaL_error(L, "Invalid des crypt text"); } lua_pushlstring(L, (const char *)buffer, textsz - padding); @@ -954,6 +1032,7 @@ lxor_str(lua_State *L) { int lsha1(lua_State *L); int lhmac_sha1(lua_State *L); + LUAMOD_API int luaopen_skynet_crypt(lua_State *L) { luaL_checkversion(L); @@ -980,9 +1059,14 @@ luaopen_skynet_crypt(lua_State *L) { { "hmac_sha1", lhmac_sha1 }, { "hmac_hash", lhmac_hash }, { "xor_str", lxor_str }, + { "padding", NULL }, { NULL, NULL }, }; luaL_newlib(L,l); + + padding_mode_table(L); + lua_setfield(L, -2, "padding"); + return 1; } diff --git a/test/testcrypt.lua b/test/testcrypt.lua new file mode 100644 index 000000000..01038b7c4 --- /dev/null +++ b/test/testcrypt.lua @@ -0,0 +1,24 @@ +local skynet = require "skynet" +local crypt = require "skynet.crypt" + +local text = "hello world" +local key = "12345678" + +local function desencode(key, text, padding) + local c = crypt.desencode(key, text, crypt.padding[padding or "iso7816_4"]) + return crypt.base64encode(c) +end + +local function desdecode(key, text, padding) + text = crypt.base64decode(text) + return crypt.desdecode(key, text, crypt.padding[padding or "iso7816_4"]) +end + +local etext = desencode(key, text) +assert( etext == "KNugLrX23UcGtcVlk9y+LA==") +assert(desdecode(key, etext) == text) + +local etext = desencode(key, text, "pkcs7") +assert(desdecode(key, etext, "pkcs7") == text) + +skynet.start(skynet.exit) \ No newline at end of file From 949d943c3270a9bfd765c935fee6c148000eaa97 Mon Sep 17 00:00:00 2001 From: Bruce Date: Thu, 23 Apr 2020 15:19:57 +0800 Subject: [PATCH 224/565] DES crypt use standard padding mode PKCS7 (#1179) * DES crypt use standard padding PKCS7 * add more desencode(pkcs7 padding mode) test case --- test/testcrypt.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/testcrypt.lua b/test/testcrypt.lua index 01038b7c4..acb979f0e 100644 --- a/test/testcrypt.lua +++ b/test/testcrypt.lua @@ -21,4 +21,14 @@ assert(desdecode(key, etext) == text) local etext = desencode(key, text, "pkcs7") assert(desdecode(key, etext, "pkcs7") == text) +assert(desencode(key, "","pkcs7")=="/rlZt9RkL8s=") +assert(desencode(key, "1","pkcs7")=="g6AtgJul6q0=") +assert(desencode(key, "12","pkcs7")=="NefFpG+m1O4=") +assert(desencode(key, "123","pkcs7")=="LDiFUdf0iew=") +assert(desencode(key, "1234","pkcs7")=="T9u7dzBdi+w=") +assert(desencode(key, "12345","pkcs7")=="AGgKdx/Qic8=") +assert(desencode(key, "123456","pkcs7")=="ED5wLgc3Mnw=") +assert(desencode(key, "1234567","pkcs7")=="mYo+BYIT41M=") +assert(desencode(key, "12345678","pkcs7")=="ltACiHjVjIn+uVm31GQvyw==") + skynet.start(skynet.exit) \ No newline at end of file From 87cde31c28c16b622c3a66ed086b95476a89b480 Mon Sep 17 00:00:00 2001 From: yxt945 Date: Thu, 23 Apr 2020 17:56:35 +0800 Subject: [PATCH 225/565] =?UTF-8?q?mysql=20:=20=E4=BF=AE=E5=A4=8D=E8=AF=BB?= =?UTF-8?q?=E5=86=99=E8=B4=9F=E6=95=B0=E5=AF=BC=E8=87=B4=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E5=A4=84=E9=94=99=E8=AF=AF=20(#1182)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mysql : 修复读写负数导致益处错误 * 注释增加字段类型 _binary_parser 参考 enum_field_types --- lualib/skynet/db/mysql.lua | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 6ac2797d5..fc8ffcfd5 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -100,10 +100,18 @@ local function _get_byte1(data, i) return strbyte(data, i), i + 1 end +local function _get_int1(data, i) + return strunpack(" Date: Sat, 25 Apr 2020 19:26:27 +0800 Subject: [PATCH 226/565] =?UTF-8?q?mysql=20:=20=E4=BF=AE=E5=A4=8D=E8=AF=BB?= =?UTF-8?q?=E5=86=99=E8=B4=9F=E6=95=B0=E5=AF=BC=E8=87=B4=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E5=A4=84=E9=94=99=E8=AF=AF=20(#1182)=20(#1183)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mysql : 修复读写负数导致溢出处错误 #1182 * modify mysql test cases * 对于db内部整型值的处理使用独立函数 --- lualib/skynet/db/mysql.lua | 39 +++++++++++++++++++++++++++++++------- test/testmysql.lua | 17 +++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index fc8ffcfd5..711ba39de 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -4,6 +4,8 @@ -- The license is under the BSD license. -- Modified by Cloud Wu (remove bit32 for lua 5.3) +-- protocol detail: https://mariadb.com/kb/en/clientserver-protocol/ + local socketchannel = require "skynet.socketchannel" local crypt = require "skynet.crypt" @@ -100,7 +102,10 @@ local function _get_byte1(data, i) return strbyte(data, i), i + 1 end -local function _get_int1(data, i) +local function _get_int1(data, i, is_signed) + if not is_signed then + return strunpack(" Date: Tue, 12 May 2020 11:58:34 +0800 Subject: [PATCH 227/565] fix #1188 --- lualib/snax/gateserver.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 61e7c97ec..b5165e2b9 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -112,8 +112,7 @@ function gateserver.start(handler) function MSG.error(fd, msg) if fd == socket then - socketdriver.close(fd) - skynet.error("gateserver close listen socket, accpet error:",msg) + skynet.error("gateserver accpet error:",msg) else if handler.error then handler.error(fd, msg) From 9f86498872699a869f5e17b90d8b3c24030f551d Mon Sep 17 00:00:00 2001 From: Fanrncho <206867549@qq.com> Date: Wed, 3 Jun 2020 20:44:22 +0800 Subject: [PATCH 228/565] Update socket.lua (#1197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果监听套接字在accept时异常(例如:errno == EMFILE),那么不应该关闭监听套接字本身,输出异常信息即可。 --- lualib/skynet/socket.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index fe7e108bc..e075be782 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -112,6 +112,10 @@ socket_message[5] = function(id, _, err) skynet.error("socket: error on unknown", id, err) return end + if s.callback then + skynet.error("socket: accpet error:", err) + return + end if s.connected then skynet.error("socket: error on", id, err) elseif s.connecting then From 435aa384c81ed7db88b1c4ebb7f70e37617fb076 Mon Sep 17 00:00:00 2001 From: wudeng Date: Fri, 19 Jun 2020 16:19:49 +0800 Subject: [PATCH 229/565] fix sproto write_ff buffer overflow (#1205) * bugfix , see #1205 * fix bug Co-authored-by: Cloud Wu --- lualib-src/sproto/sproto.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 36b209ceb..2c36ad9f2 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -1262,15 +1262,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); - +write_ff(const uint8_t * src, const uint8_t * src_end, uint8_t * des, int n) { des[0] = 0xff; - des[1] = align8_n/8 - 1; - memcpy(des+2, src, n); - for(i=0; i< align8_n-n; i++){ - des[n+2+i] = 0; + des[1] = n - 1; + if (src + n * 8 <= src_end) { + memcpy(des+2, src, n*8); + } else { + int sz = (int)(src_end - src); + memcpy(des+2, src, sz); + memset(des+2+sz, 0, n*8-sz); } } @@ -1283,6 +1283,7 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { int ff_n = 0; int size = 0; const uint8_t * src = srcv; + const uint8_t * src_end = (uint8_t *)srcv + srcsz; uint8_t * buffer = bufferv; for (i=0;i= 0) { - write_ff(ff_srcstart, ff_desstart, 256*8); + write_ff(ff_srcstart, src_end, ff_desstart, 256); } ff_n = 0; } } else { if (ff_n > 0) { if (bufsz >= 0) { - write_ff(ff_srcstart, ff_desstart, ff_n*8); + write_ff(ff_srcstart, src_end, ff_desstart, ff_n); } ff_n = 0; } @@ -1322,11 +1323,8 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { buffer += n; size += 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)); + if(bufsz >= 0 && ff_n > 0) { + write_ff(ff_srcstart, src_end, ff_desstart, ff_n); } return size; } From 693176efc8ac28a40da7793805fd91f5e593a9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Wed, 24 Jun 2020 16:25:50 +0800 Subject: [PATCH 230/565] =?UTF-8?q?=E4=BC=98=E5=8C=96sharetable=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20(#1208)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zixun --- lualib/skynet/sharetable.lua | 43 +++++++++++++++--------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index eb2449a0d..6bec2d268 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -258,16 +258,12 @@ local function resolve_replace(replace_map) local function match_value(v) assert(v ~= nil) - if v == RECORD then + if record_map[v] or is_sharedtable(v) then return end local tv = type(v) local f = match[tv] - if record_map[v] or is_sharedtable(v) then - return - end - if f then record_map[v] = true f(v) @@ -364,7 +360,7 @@ local function resolve_replace(replace_map) end local level = info.level - local curco = info.curco or coroutine.running() + local curco = info.curco if not level then return end @@ -390,7 +386,7 @@ local function resolve_replace(replace_map) end local stack_values_tmp = {} - local function match_thread(co) + local function match_thread(co, level) -- match stackvalues local n = stackvalues(co, stack_values_tmp) for i=1,n do @@ -399,40 +395,37 @@ local function resolve_replace(replace_map) match_value(v) end - -- match callinfo - local level = 1 - -- jump the fucntion from sharetable.update to top - local is_self = coroutine.running() == co - if is_self then - while true do - local info = getinfo(co, level, "uf") - level = level + 1 - if not info then - level = 1 - break - elseif info.func == sharetable.update then - break - end - end - end - + level = level or 1 while true do local info = getinfo(co, level, "uf") if not info then break end - info.level = is_self and level + 1 or level + info.level = level info.curco = co match_funcinfo(info) level = level + 1 end end + local function prepare_match() + local co = coroutine.running() + record_map[co] = true + record_map[match] = true + record_map[RECORD] = true + record_map[record_map] = true + record_map[insert_replace] = true + record_map[resolve_replace] = true + assert(getinfo(co, 3, "f").func == sharetable.update) + match_thread(co, 5) -- ignore match_thread and match_funcinfo frame + end + match["table"] = match_table match["function"] = match_function match["userdata"] = match_userdata match["thread"] = match_thread + prepare_match() match_internmt() local root = debug.getregistry() From f2b1bd73198ee68352d9acaebdcebef7e39bb3b2 Mon Sep 17 00:00:00 2001 From: fanlix Date: Wed, 22 Jul 2020 19:26:39 +0800 Subject: [PATCH 231/565] stat: add mem stat for jemalloc (#1218) 1, add opts arg for mallctl,dumpinfo 2, collect jemalloc.mem info 3, add debug_console cmd: jmem Co-authored-by: fx --- lualib-src/lua-memory.c | 35 ++++++++++++++++++++++++++++++++++- service/debug_console.lua | 10 ++++++++++ skynet-src/malloc_hook.c | 6 +++--- skynet-src/malloc_hook.h | 2 +- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index b9b07a611..2f77a97d7 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -23,11 +23,42 @@ lblock(lua_State *L) { static int ldumpinfo(lua_State *L) { - memory_info_dump(); + const char *opts = NULL; + if (lua_isstring(L, 1)) { + opts = luaL_checkstring(L,1); + } + memory_info_dump(opts); return 0; } +static int +ljestat(lua_State *L) { + static const char* names[] = { + "stats.allocated", + "stats.resident", + "stats.retained", + "stats.mapped", + "stats.active" }; + static size_t flush = 1; + mallctl_int64("epoch", &flush); // refresh je.stats.cache + lua_newtable(L); + int i; + for (i = 0; i < (sizeof(names)/sizeof(names[0])); i++) { + lua_pushstring(L, names[i]); + lua_pushinteger(L, (lua_Integer) mallctl_int64(names[i], NULL)); + lua_settable(L, -3); + } + return 1; +} + +static int +lmallctl(lua_State *L) { + const char *name = luaL_checkstring(L,1); + lua_pushinteger(L, (lua_Integer) mallctl_int64(name, NULL)); + return 1; +} + static int ldump(lua_State *L) { dump_c_mem(); @@ -69,6 +100,8 @@ luaopen_skynet_memory(lua_State *L) { { "total", ltotal }, { "block", lblock }, { "dumpinfo", ldumpinfo }, + { "jestat", ljestat }, + { "mallctl", lmallctl }, { "dump", ldump }, { "info", dump_mem_lua }, { "current", lcurrent }, diff --git a/service/debug_console.lua b/service/debug_console.lua index 07572bbbf..35c4f1d08 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -156,6 +156,7 @@ function COMMAND.help() debug = "debug address : debug a lua service", signal = "signal address sig", cmem = "Show C memory info", + jmem = "Show jemalloc mem stats", ping = "ping address", call = "call address ...", trace = "trace address [proto] [on|off]", @@ -342,6 +343,15 @@ function COMMAND.cmem() return tmp end +function COMMAND.jmem() + local info = memory.jestat() + local tmp = {} + for k,v in pairs(info) do + tmp[k] = string.format("%11d %8.2f Mb", v, v/1048576) + end + return tmp +end + function COMMAND.ping(address) address = adjust_address(address) local ti = skynet.now() diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 150c2cc61..a3fed30ec 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -127,8 +127,8 @@ static void malloc_oom(size_t size) { } void -memory_info_dump(void) { - je_malloc_stats_print(0,0,0); +memory_info_dump(const char* opts) { + je_malloc_stats_print(0,0, opts); } bool @@ -241,7 +241,7 @@ skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { #define raw_free free void -memory_info_dump(void) { +memory_info_dump(const char* opts) { skynet_error(NULL, "No jemalloc"); } diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index 04f522ac7..ae95b9a5e 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -7,7 +7,7 @@ extern size_t malloc_used_memory(void); extern size_t malloc_memory_block(void); -extern void memory_info_dump(void); +extern void memory_info_dump(const char *opts); extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern bool mallctl_bool(const char* name, bool* newval); From bfc19ee7b95160c3ea0badcf24f92f08e7e71e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 27 Jul 2020 17:57:55 +0800 Subject: [PATCH 232/565] add sproto double type (#1221) Co-authored-by: zixun --- lualib-src/sproto/lsproto.c | 20 ++++++++++++++++++++ lualib-src/sproto/sproto.c | 2 ++ lualib-src/sproto/sproto.h | 3 ++- lualib/sprotoparser.lua | 6 +++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index cb3320270..16d36554f 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -55,6 +55,13 @@ static int64_t lua_tointegerx(lua_State *L, int idx, int *isnum) { return 0; } } + +static int lua_absindex (lua_State *L, int idx) { + if (idx > 0 || idx <= LUA_REGISTRYINDEX) + return idx; + return lua_gettop(L) + idx + 1; +} + #endif static void @@ -235,6 +242,11 @@ encode(const struct sproto_arg *args) { return 8; } } + case SPROTO_DOUBLE: { + lua_Number v = lua_tonumber(L, -1); + *(double*)args->value = (double)v; + return 8; + } case SPROTO_TBOOLEAN: { int v = lua_toboolean(L, -1); if (!lua_isboolean(L,-1)) { @@ -395,6 +407,11 @@ decode(const struct sproto_arg *args) { } break; } + case SPROTO_DOUBLE: { + double v = *(double*)args->value; + lua_pushnumber(L, v); + break; + } case SPROTO_TBOOLEAN: { int v = *(uint64_t*)args->value; lua_pushboolean(L,v); @@ -671,6 +688,9 @@ push_default(const struct sproto_arg *args, int array) { else lua_pushinteger(L, 0); break; + case SPROTO_DOUBLE: + lua_pushnumber(L, 0.0); + break; case SPROTO_TBOOLEAN: lua_pushboolean(L, 0); break; diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 2c36ad9f2..4d38d51cc 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -944,6 +944,7 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.type = type; args.index = 0; switch(type) { + case SPROTO_DOUBLE: case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { union { @@ -1177,6 +1178,7 @@ sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_ } } else { switch (f->type) { + case SPROTO_DOUBLE: case SPROTO_TINTEGER: { uint32_t sz = todword(currentdata); if (sz == SIZEOF_INT32) { diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index f70309ec0..1fb9252ea 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -13,7 +13,8 @@ struct sproto_type; #define SPROTO_TINTEGER 0 #define SPROTO_TBOOLEAN 1 #define SPROTO_TSTRING 2 -#define SPROTO_TSTRUCT 3 +#define SPROTO_DOUBLE 3 +#define SPROTO_TSTRUCT 4 // sub type of string (sproto_arg.extra) #define SPROTO_TSTRING_STRING 0 diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 4c9a7d07a..afac92f73 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -97,7 +97,10 @@ 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 pt = p[1] + if result[pt] ~= nil then + error(string.format("redefine %s in protocol %s", pt, obj[1])) + end local typename = p[2] if type(typename) == "table" then local struct = typename @@ -179,6 +182,7 @@ local buildin_types = { boolean = 1, string = 2, binary = 2, -- binary is a sub type of string + double = 3, } local function checktype(types, ptype, t) From 875b7683c608c8b33bc2307185358de85870a242 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Jul 2020 15:59:00 +0800 Subject: [PATCH 233/565] srv may be a local name, see #1220 --- lualib/skynet.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d1e08b212..1ef8ad46a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -91,7 +91,8 @@ local function _error_dispatch(error_session, error_source) end end for session, srv in pairs(watching_session) do - if srv == error_source then + if srv == error_source or + (type(srv) == "string" and skynet.localname(srv) == error_source) then tinsert(error_queue, session) end end From c749b130105a75c1072c8b353747db577135b848 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Jul 2020 16:13:05 +0800 Subject: [PATCH 234/565] Revert "srv may be a local name, see #1220" This reverts commit 875b7683c608c8b33bc2307185358de85870a242. --- lualib/skynet.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 1ef8ad46a..d1e08b212 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -91,8 +91,7 @@ local function _error_dispatch(error_session, error_source) end end for session, srv in pairs(watching_session) do - if srv == error_source or - (type(srv) == "string" and skynet.localname(srv) == error_source) then + if srv == error_source then tinsert(error_queue, session) end end From 950389599d29873cf598a30386d6d7ab78024ff2 Mon Sep 17 00:00:00 2001 From: hong Date: Wed, 29 Jul 2020 07:13:57 +0800 Subject: [PATCH 235/565] =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9C=89=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=AD=BB=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=20(#1225)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/lua-mongo.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index a8ca0b0cf..4ceffa066 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -246,6 +246,9 @@ op_reply(lua_State *L) { lua_rawseti(L, 2, i); int32_t doc_len = get_length((document)doc); + if (doc_len <= 0) { + return luaL_error(L, "Invalid result bson document"); + } doc += doc_len; sz -= doc_len; From 44485b195c28ddf15baec967b1e9e71b3df4b753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 3 Aug 2020 22:54:04 +0800 Subject: [PATCH 236/565] update sproto double array (#1227) Co-authored-by: zixun --- lualib-src/sproto/lsproto.c | 6 +++--- lualib-src/sproto/sproto.c | 6 ++++-- lualib-src/sproto/sproto.h | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 16d36554f..214ec8df5 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -242,7 +242,7 @@ encode(const struct sproto_arg *args) { return 8; } } - case SPROTO_DOUBLE: { + case SPROTO_TDOUBLE: { lua_Number v = lua_tonumber(L, -1); *(double*)args->value = (double)v; return 8; @@ -407,7 +407,7 @@ decode(const struct sproto_arg *args) { } break; } - case SPROTO_DOUBLE: { + case SPROTO_TDOUBLE: { double v = *(double*)args->value; lua_pushnumber(L, v); break; @@ -688,7 +688,7 @@ push_default(const struct sproto_arg *args, int array) { else lua_pushinteger(L, 0); break; - case SPROTO_DOUBLE: + case SPROTO_TDOUBLE: lua_pushnumber(L, 0.0); break; case SPROTO_TBOOLEAN: diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 4d38d51cc..62c336e97 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -849,6 +849,7 @@ 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_TDOUBLE: case SPROTO_TINTEGER: { int noarray; buffer = encode_integer_array(cb,args,buffer,size, &noarray); @@ -944,7 +945,7 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.type = type; args.index = 0; switch(type) { - case SPROTO_DOUBLE: + case SPROTO_TDOUBLE: case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { union { @@ -1067,6 +1068,7 @@ decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { } stream += SIZEOF_LENGTH; switch (type) { + case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { int len = *stream; ++stream; @@ -1178,7 +1180,7 @@ sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_ } } else { switch (f->type) { - case SPROTO_DOUBLE: + case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { uint32_t sz = todword(currentdata); if (sz == SIZEOF_INT32) { diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index 1fb9252ea..0ee5255b3 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -13,7 +13,7 @@ struct sproto_type; #define SPROTO_TINTEGER 0 #define SPROTO_TBOOLEAN 1 #define SPROTO_TSTRING 2 -#define SPROTO_DOUBLE 3 +#define SPROTO_TDOUBLE 3 #define SPROTO_TSTRUCT 4 // sub type of string (sproto_arg.extra) From ddedffc75386003f0e3f48aa8ffa4839f430d97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=89=E6=9C=8B?= <22249030@qq.com> Date: Thu, 6 Aug 2020 08:42:03 +0800 Subject: [PATCH 237/565] =?UTF-8?q?mysql=E9=A2=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=94=AF=E6=8C=81nil=E5=8F=82=E6=95=B0,nil?= =?UTF-8?q?=3D=3DNULL=20(#1228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/db/mysql.lua | 42 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 711ba39de..67fd39500 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -560,6 +560,10 @@ local store_types = { end } +store_types["nil"] = function(v) + return _set_byte2(0x06), "" +end + local function _compose_stmt_execute(self, stmt, cursor_type, args) local arg_num = #args if arg_num ~= stmt.param_count then @@ -570,11 +574,29 @@ local function _compose_stmt_execute(self, stmt, cursor_type, args) local cmd_packet = strpack(" 0 then - local null_count = (arg_num + 7) // 8 local f, ts, vs local types_buf = "" local values_buf = "" - for _, v in pairs(args) do + --生成NULL位图 + local null_count = (arg_num + 7) // 8 + local null_map = "" + local field_index = 1 + for i = 1, null_count do + local byte = 0 + for j = 0, 7 do + if field_index < arg_num then + if args[field_index] == nil then + byte = byte | (1 << j) + else + byte = byte | (0 << j) + end + end + field_index = field_index + 1 + end + null_map = null_map .. strchar(byte) + end + for i = 1, arg_num do + local v = args[i] f = store_types[type(v)] if not f then error("invalid parameter type", type(v)) @@ -583,7 +605,7 @@ local function _compose_stmt_execute(self, stmt, cursor_type, args) types_buf = types_buf .. ts values_buf = values_buf .. vs end - cmd_packet = cmd_packet .. strrep("\0", null_count) .. strchar(0x01) .. types_buf .. values_buf + cmd_packet = cmd_packet .. null_map .. strchar(0x01) .. types_buf .. values_buf end return _compose_packet(self, cmd_packet) @@ -1011,20 +1033,6 @@ end err ]] function _M.execute(self, stmt, ...) - -- 检查参数,不能为nil - local p_n = select('#', ...) - local p_v - for i = 1, p_n do - p_v = select(i, ...) - if p_v == nil then - return { - badresult = true, - errno = 30902, - err = "parameter " .. i .. " is nil" - } - end - end - local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, {...}) if not querypacket then return { From c35c8f173bcea717f9dda90ff2c2afbee1ca4b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Fri, 7 Aug 2020 16:46:49 +0800 Subject: [PATCH 238/565] #1229 fix sharetable update (#1230) Co-authored-by: zixun --- lualib/skynet/sharetable.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 6bec2d268..e80d39290 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -257,8 +257,7 @@ local function resolve_replace(replace_map) end local function match_value(v) - assert(v ~= nil) - if record_map[v] or is_sharedtable(v) then + if v == nil or record_map[v] or is_sharedtable(v) then return end @@ -385,13 +384,12 @@ local function resolve_replace(replace_map) match_funcinfo(info) end - local stack_values_tmp = {} local function match_thread(co, level) -- match stackvalues - local n = stackvalues(co, stack_values_tmp) + local values = {} + local n = stackvalues(co, values) for i=1,n do - local v = stack_values_tmp[i] - stack_values_tmp[i] = nil + local v = values[i] match_value(v) end From a4dc6b909478a7397eb205f3ca5a2800e9391d65 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2020 10:57:59 +0800 Subject: [PATCH 239/565] add api skynet_socket_pause --- lualib-src/lua-socket.c | 9 ++++++ lualib/skynet/socket.lua | 2 +- skynet-src/skynet_socket.c | 7 +++++ skynet-src/skynet_socket.h | 1 + skynet-src/socket_server.c | 56 ++++++++++++++++++++++++++++++-------- skynet-src/socket_server.h | 1 + 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index b7fc69ec6..3d527473a 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -611,6 +611,14 @@ lstart(lua_State *L) { return 0; } +static int +lpause(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + skynet_socket_pause(ctx,id); + return 0; +} + static int lnodelay(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); @@ -794,6 +802,7 @@ luaopen_skynet_socketdriver(lua_State *L) { { "lsend", lsendlow }, { "bind", lbind }, { "start", lstart }, + { "pause", lpause }, { "nodelay", lnodelay }, { "udp", ludp }, { "udp_connect", ludp_connect }, diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e075be782..74b3ff3ec 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -113,7 +113,7 @@ socket_message[5] = function(id, _, err) return end if s.callback then - skynet.error("socket: accpet error:", err) + skynet.error("socket: accpet error:", err) return end if s.connected then diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 25044b6a6..7ba7a6837 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -162,6 +162,13 @@ skynet_socket_start(struct skynet_context *ctx, int id) { socket_server_start(SOCKET_SERVER, source, id); } +void +skynet_socket_pause(struct skynet_context *ctx, int id) { + uint32_t source = skynet_context_handle(ctx); + socket_server_pause(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 ff7e23fbb..dd66f834c 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -35,6 +35,7 @@ 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_pause(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); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 0e1fbd13f..b6e9b4f47 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -37,6 +37,10 @@ #define PRIORITY_HIGH 0 #define PRIORITY_LOW 1 +#define READING_PAUSE 0 +#define READING_RESUME 1 +#define READING_CLOSE 2 + #define HASH_ID(id) (((unsigned)id) % MAX_SOCKET) #define ID_TAG16(id) ((id>>MAX_SOCKET_P) & 0xffff) @@ -95,7 +99,8 @@ struct socket { int id; uint8_t protocol; uint8_t type; - uint16_t udpconnecting; + uint8_t reading; + int udpconnecting; int64_t warn_size; union { int size; @@ -166,7 +171,7 @@ struct request_bind { uintptr_t opaque; }; -struct request_start { +struct request_resumepause { int id; uintptr_t opaque; }; @@ -212,7 +217,7 @@ struct request_package { struct request_close close; struct request_listen listen; struct request_bind bind; - struct request_start start; + struct request_resumepause resumepause; struct request_setopt setopt; struct request_udp udp; struct request_setudp set_udp; @@ -477,9 +482,10 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_lock *l, s assert(s->type != SOCKET_TYPE_RESERVE); free_wb_list(ss,&s->high); free_wb_list(ss,&s->low); - if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { + if (s->reading == READING_RESUME) { sp_del(ss->event_fd, s->fd); } + s->reading = READING_CLOSE; socket_lock(l); if (s->type != SOCKET_TYPE_BIND) { if (close(s->fd) < 0) { @@ -538,6 +544,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->id = id; s->fd = fd; + s->reading = READING_PAUSE; s->sending = ID_TAG16(id) << 16 | 0; s->protocol = protocol; s->p.size = MIN_READ_BUFFER; @@ -1046,7 +1053,7 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke } static int -start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) { +resume_socket(struct socket_server *ss, struct request_resumepause *request, struct socket_message *result) { int id = request->id; result->id = id; result->opaque = request->opaque; @@ -1059,12 +1066,15 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc } struct socket_lock l; socket_lock_init(s, &l); - if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { + if (s->reading == READING_PAUSE) { if (sp_add(ss->event_fd, s->fd, s)) { + s->reading = READING_RESUME; force_close(ss, s, &l, result); result->data = strerror(errno); return SOCKET_ERR; } + } + if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; s->opaque = request->opaque; result->data = "start"; @@ -1079,6 +1089,19 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc return -1; } +static void +pause_socket(struct socket_server *ss, struct request_resumepause *request) { + int id = request->id; + struct socket *s = &ss->slot[HASH_ID(id)]; + if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + return; + } + if (s->reading == READING_RESUME) { + sp_del(ss->event_fd, s->fd); + s->reading = READING_PAUSE; + } +} + static void setopt_socket(struct socket_server *ss, struct request_setopt *request) { int id = request->id; @@ -1210,8 +1233,11 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { block_readpipe(fd, buffer, len); // ctrl command only exist in local fd, so don't worry about endian. switch (type) { + case 'R': + return resume_socket(ss,(struct request_resumepause *)buffer, result); case 'S': - return start_socket(ss,(struct request_start *)buffer, result); + pause_socket(ss,(struct request_resumepause *)buffer); + return -1; case 'B': return bind_socket(ss,(struct request_bind *)buffer, result); case 'L': @@ -1841,12 +1867,20 @@ socket_server_bind(struct socket_server *ss, uintptr_t opaque, int fd) { return id; } -void +void socket_server_start(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; - request.u.start.id = id; - request.u.start.opaque = opaque; - send_request(ss, &request, 'S', sizeof(request.u.start)); + request.u.resumepause.id = id; + request.u.resumepause.opaque = opaque; + send_request(ss, &request, 'R', sizeof(request.u.resumepause)); +} + +void +socket_server_pause(struct socket_server *ss, uintptr_t opaque, int id) { + struct request_package request; + request.u.resumepause.id = id; + request.u.resumepause.opaque = opaque; + send_request(ss, &request, 'S', sizeof(request.u.resumepause)); } void diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index cbfe98897..05ea12898 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -32,6 +32,7 @@ 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); +void socket_server_pause(struct socket_server *, uintptr_t opaque, int id); // return -1 when error int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer); From 51fd63213e1eb94233eed83fe04d74bca1a13a9f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2020 16:35:55 +0800 Subject: [PATCH 240/565] bugfix: switch reading status --- 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 b6e9b4f47..645c9b2df 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -544,7 +544,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->id = id; s->fd = fd; - s->reading = READING_PAUSE; + s->reading = add ? READING_RESUME : READING_PAUSE; s->sending = ID_TAG16(id) << 16 | 0; s->protocol = protocol; s->p.size = MIN_READ_BUFFER; @@ -1068,11 +1068,11 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str socket_lock_init(s, &l); if (s->reading == READING_PAUSE) { if (sp_add(ss->event_fd, s->fd, s)) { - s->reading = READING_RESUME; force_close(ss, s, &l, result); result->data = strerror(errno); return SOCKET_ERR; } + s->reading = READING_RESUME; } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; From c8d0b1014d1b8a1dcec542c45b0246200a57582a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2020 16:54:15 +0800 Subject: [PATCH 241/565] add a warning message --- lualib-src/lua-socket.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 3d527473a..fec74260d 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -14,11 +14,13 @@ #include #include +#include "skynet.h" #include "skynet_socket.h" #define BACKLOG 32 // 2 ** 12 == 4096 #define LARGE_PAGE_NODE 12 +#define POOL_SIZE_WARNING 32 #define BUFFER_LIMIT (256 * 1024) struct buffer_node { @@ -124,6 +126,9 @@ lpushbuffer(lua_State *L) { lnewpool(L, size); free_node = lua_touserdata(L,-1); lua_rawseti(L, pool_index, tsz+1); + if (tsz > POOL_SIZE_WARNING) { + skynet_error(NULL, "Too many socket pool (%d)", tsz); + } } lua_pushlightuserdata(L, free_node->next); lua_rawseti(L, pool_index, 1); // sb poolt msg size From d80ed7483d7a3dd966619b665dbf3bdeb1ef95ec Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 10:32:46 +0800 Subject: [PATCH 242/565] add timestamp for logging to file --- service-src/service_logger.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index eadf2f280..b3f06bf7a 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -4,6 +4,7 @@ #include #include #include +#include struct logger { FILE * handle; @@ -30,6 +31,17 @@ logger_release(struct logger * inst) { skynet_free(inst); } +#define SIZETIMEFMT 250 + +static const char * +timestring(char tmp[SIZETIMEFMT]) { + time_t ti; + time(&ti); + struct tm *info = localtime(&ti); + strftime(tmp, SIZETIMEFMT, "%D %T ", info); + return tmp; +} + static int 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; @@ -40,7 +52,11 @@ logger_cb(struct skynet_context * context, void *ud, int type, int session, uint } break; case PTYPE_TEXT: - fprintf(inst->handle, "[:%08x] ",source); + if (inst->filename) { + char tmp[SIZETIMEFMT]; + fprintf(inst->handle, "%s", timestring(tmp)); + } + fprintf(inst->handle, "[:%08x] ", source); fwrite(msg, sz , 1, inst->handle); fprintf(inst->handle, "\n"); fflush(inst->handle); From c7386fe768008ddefed67e56aa595a7656c3c015 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 10:54:40 +0800 Subject: [PATCH 243/565] open log file in append mode --- service-src/service_logger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index b3f06bf7a..b8d9a0cc2 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -69,7 +69,7 @@ logger_cb(struct skynet_context * context, void *ud, int type, int session, uint int logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) { if (parm) { - inst->handle = fopen(parm,"w"); + inst->handle = fopen(parm,"a"); if (inst->handle == NULL) { return 1; } From 446dafdc0bbb9bda2554fb64f06704214d77c878 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 11:47:33 +0800 Subject: [PATCH 244/565] use skynet_now for timestamp --- service-src/service_logger.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index b8d9a0cc2..6c7204c51 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -9,6 +9,7 @@ struct logger { FILE * handle; char * filename; + uint32_t starttime; int close; }; @@ -33,13 +34,14 @@ logger_release(struct logger * inst) { #define SIZETIMEFMT 250 -static const char * -timestring(char tmp[SIZETIMEFMT]) { - time_t ti; +static int +timestring(struct logger *inst, char tmp[SIZETIMEFMT]) { + uint64_t now = skynet_now(); + time_t ti = now/100 + inst->starttime; time(&ti); struct tm *info = localtime(&ti); - strftime(tmp, SIZETIMEFMT, "%D %T ", info); - return tmp; + strftime(tmp, SIZETIMEFMT, "%D %T", info); + return now % 100; } static int @@ -54,7 +56,8 @@ logger_cb(struct skynet_context * context, void *ud, int type, int session, uint case PTYPE_TEXT: if (inst->filename) { char tmp[SIZETIMEFMT]; - fprintf(inst->handle, "%s", timestring(tmp)); + int msec = timestring(ud, tmp); + fprintf(inst->handle, "%s.%02d ", tmp, msec); } fprintf(inst->handle, "[:%08x] ", source); fwrite(msg, sz , 1, inst->handle); @@ -68,6 +71,8 @@ logger_cb(struct skynet_context * context, void *ud, int type, int session, uint int logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) { + const char * r = skynet_command(ctx, "STARTTIME", NULL); + inst->starttime = strtoul(r, NULL, 10); if (parm) { inst->handle = fopen(parm,"a"); if (inst->handle == NULL) { From 3025e4765ac4dd54dec0e7337d857a650d0a142b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 11:50:45 +0800 Subject: [PATCH 245/565] csec --- service-src/service_logger.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index 6c7204c51..4d24438a4 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -56,8 +56,8 @@ logger_cb(struct skynet_context * context, void *ud, int type, int session, uint case PTYPE_TEXT: if (inst->filename) { char tmp[SIZETIMEFMT]; - int msec = timestring(ud, tmp); - fprintf(inst->handle, "%s.%02d ", tmp, msec); + int csec = timestring(ud, tmp); + fprintf(inst->handle, "%s.%02d ", tmp, csec); } fprintf(inst->handle, "[:%08x] ", source); fwrite(msg, sz , 1, inst->handle); From d3d5e3063aca741d90ec471aa3e96868e1a8dace Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 14:08:25 +0800 Subject: [PATCH 246/565] use luaL_buffinitsize --- 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 fec74260d..ed9a226b0 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -184,7 +184,7 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { } luaL_Buffer b; - luaL_buffinit(L, &b); + luaL_buffinitsize(L, &b, sz); for (;;) { int bytes = current->sz - sb->offset; if (bytes >= sz) { From ce52533c7463f339db956a819b8ef0cb7431aa9e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2020 16:32:22 +0800 Subject: [PATCH 247/565] remove time() --- service-src/service_logger.c | 1 - 1 file changed, 1 deletion(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index 4d24438a4..7d58f4a3f 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -38,7 +38,6 @@ static int timestring(struct logger *inst, char tmp[SIZETIMEFMT]) { uint64_t now = skynet_now(); time_t ti = now/100 + inst->starttime; - time(&ti); struct tm *info = localtime(&ti); strftime(tmp, SIZETIMEFMT, "%D %T", info); return now % 100; From 55efa75fdff6a092be14f75bc28648d4071cb4dc Mon Sep 17 00:00:00 2001 From: Dirk Chang Date: Mon, 31 Aug 2020 16:48:50 +0800 Subject: [PATCH 248/565] Invalid addrinfo pointer casues skynet asserted in freeaddrinfo on OpenWRT (arm cpu) (#1236) --- skynet-src/socket_server.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 645c9b2df..7d2aa738a 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -594,7 +594,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; + goto _failed_getaddrinfo; } int sock= -1; for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { @@ -643,6 +643,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return -1; _failed: freeaddrinfo( ai_list ); +_failed_getaddrinfo: ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return SOCKET_ERR; } From 280f0251dae5da56d868ae1be166416127cc003b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 4 Sep 2020 12:26:13 +0800 Subject: [PATCH 249/565] Add traffic control for socket --- lualib/skynet/socket.lua | 22 ++++++++++++++++++++-- test/testsocket.lua | 6 +----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 74b3ff3ec..dd2db507a 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -3,6 +3,7 @@ local skynet = require "skynet" local skynet_core = require "skynet.core" local assert = assert +local BUFFER_LIMIT = 128 * 1024 local socket = {} -- api local buffer_pool = {} -- store all message buffer object local socket_pool = setmetatable( -- store all socket object @@ -30,6 +31,10 @@ end local function suspend(s) assert(not s.co) s.co = coroutine.running() + if s.pause then + driver.start(s.id) + s.pause = nil + end skynet.wait(s.co) -- wakeup closing corouting every time suspend, -- because socket.close() will wait last socket buffer operation before clear the buffer. @@ -56,6 +61,10 @@ socket_message[1] = function(id, size, data) if sz >= rr then s.read_required = nil wakeup(s) + if sz > BUFFER_LIMIT then + driver.pause(id) + s.pause = true + end end else if s.buffer_limit and sz > s.buffer_limit then @@ -69,7 +78,14 @@ socket_message[1] = function(id, size, data) if driver.readline(s.buffer,nil,rr) then s.read_required = nil wakeup(s) + if sz > BUFFER_LIMIT then + driver.pause(id) + s.pause = true + end end + elseif sz > BUFFER_LIMIT and not s.pause then + driver.pause(id) + s.pause = true end end end @@ -81,8 +97,10 @@ socket_message[2] = function(id, _ , addr) return end -- log remote addr - s.connected = true - wakeup(s) + if not s.connected then -- resume may also post connect message + s.connected = true + wakeup(s) + end end -- SKYNET_SOCKET_TYPE_CLOSE = 3 diff --git a/test/testsocket.lua b/test/testsocket.lua index 179df1a80..715a31e1f 100644 --- a/test/testsocket.lua +++ b/test/testsocket.lua @@ -5,6 +5,7 @@ local mode , id = ... local function echo(id) socket.start(id) + socket.write(id, "Hello Skynet\n") while true do local str = socket.read(id) @@ -28,12 +29,7 @@ if mode == "agent" then end) else local function accept(id) - socket.start(id) - socket.write(id, "Hello Skynet\n") 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) end skynet.start(function() From a4c31ac64e40f11c5a97bb6c6f532f7ed2d60d7d Mon Sep 17 00:00:00 2001 From: Fanrncho <206867549@qq.com> Date: Mon, 7 Sep 2020 13:13:48 +0800 Subject: [PATCH 250/565] Update internal.lua (#1237) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正小错误。 --- lualib/http/internal.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index ec45ff50e..8c26519ab 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,5 +1,6 @@ local table = table local type = type +local sockethelper = require "http.sockethelper" local M = {} @@ -170,7 +171,7 @@ function M.request(interface, method, host, url, recvheader, header, content) local tmpline = {} local body = M.recvheader(read, tmpline, "") if not body then - error(socket.socket_error) + error(sockethelper.socket_error) end local statusline = tmpline[1] From eef2b1fc628c8ec428cee31a9be61dfeaf51ee69 Mon Sep 17 00:00:00 2001 From: Whislly Date: Tue, 15 Sep 2020 15:12:44 +0800 Subject: [PATCH 251/565] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E4=B8=BA?= =?UTF-8?q?int64=E7=9A=84=E6=96=B9=E6=B3=95=20(#1241)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update lua-bson.c * 增加一个指定数据类型为int64的方法 * 修改一些类型错误 * Invalid date -> Invalid int64 --- lualib-src/lua-bson.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index eacecf3c7..6fca56236 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -395,6 +395,14 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) case BSON_MAXKEY: case BSON_NULL: break; + case BSON_INT64: { + if (len != 2 + 8) { + luaL_error(L, "Invalid int64"); + } + const int64_t * v = (const int64_t *)(str + 2); + write_int64(bs, *v); + break; + } default: luaL_error(L,"Invalid subtype %d", subt); } @@ -1015,6 +1023,19 @@ ldate(lua_State *L) { return 1; } +static int +lint64(lua_State *L) { + int64_t d = luaL_checkinteger(L, 1); + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addchar(&b, 0); + luaL_addchar(&b, BSON_INT64); + luaL_addlstring(&b, (const char *)&d, sizeof(d)); + luaL_pushresult(&b); + + return 1; +} + static int ltimestamp(lua_State *L) { int d = luaL_checkinteger(L,1); @@ -1145,9 +1166,18 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { case BSON_DBPOINTER: case BSON_SYMBOL: case BSON_CODEWS: - lua_pushvalue(L, lua_upvalueindex(13)); + lua_pushvalue(L, lua_upvalueindex(14)); lua_pushlstring(L, (const char *)buf, sz); return 2; + case BSON_INT64: { + if (sz != 8) { + return luaL_error(L, "Invalid int64"); + } + int64_t d = *(const int64_t *)buf; + lua_pushvalue(L, lua_upvalueindex(13)); + lua_pushinteger(L, d); + return 2; + } default: return luaL_error(L, "Invalid subtype %d", subtype); } @@ -1203,7 +1233,8 @@ typeclosure(lua_State *L) { "regex", // 10 "minkey", // 11 "maxkey", // 12 - "unsupported", // 13 + "int64", // 13 + "unsupported", // 14 }; int i; int n = sizeof(typename)/sizeof(typename[0]); @@ -1305,6 +1336,7 @@ luaopen_bson(lua_State *L) { { "regex", lregex }, { "binary", lbinary }, { "objectid", lobjectid }, + { "int64", lint64 }, { "decode", ldecode }, { NULL, NULL }, }; From eaa60ca8ddad83c46a560daabdcb196c8e747c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Sat, 10 Oct 2020 13:36:59 +0800 Subject: [PATCH 252/565] Update to lua 5.4 (#1174) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update to lua 5.4.0-rc1 * remove LUA_ERRGCMM * use age bits for shared * remove unused lbitlib.c * use luaV_fastget * fix #1174 * suspend函数tailcall优化 * 防止没有session导致未调用suspend * luaH_setint check shared (#1184) * set LUA_GCGEN by default * fix #1174 (#1186) checkshared in sweeplist * update to lua 5.4 rc2 * update to lua 5.4 rc4 * move skynet.profile into snlua * Use lua_sethook to interrupt tight loops * remove lua_checksig * critical condition for signals * update to lua 5.4 released * lua 5.4 bugfix * update lua bugfix * fix lua_sharestring (#1224) * update lua * update lua 5.4 * update README * add skynet.select * add test * test error & discard * yield session * request error has no error message * add timeout to skynet.select * bugfix * for lua 5.4 * new version * bugfix * bugfix * use if instead of while * make local * yield in select for * update lua 5.4.1 * change lua version (#1245) Co-authored-by: xiaojin * update lua version number Co-authored-by: hong Co-authored-by: hong Co-authored-by: 风---自由 <996442717qqcom@gmail.com> Co-authored-by: xiaojin --- 3rd/lua/Makefile | 81 +- 3rd/lua/README | 2 +- 3rd/lua/lapi.c | 713 +++++++++------ 3rd/lua/lapi.h | 27 +- 3rd/lua/lauxlib.c | 351 +++---- 3rd/lua/lauxlib.h | 54 +- 3rd/lua/lbaselib.c | 139 +-- 3rd/lua/lbitlib.c | 233 ----- 3rd/lua/lcode.c | 1131 +++++++++++++++++------ 3rd/lua/lcode.h | 34 +- 3rd/lua/lcorolib.c | 89 +- 3rd/lua/lctype.c | 43 +- 3rd/lua/lctype.h | 18 +- 3rd/lua/ldblib.c | 73 +- 3rd/lua/ldebug.c | 463 ++++++---- 3rd/lua/ldebug.h | 21 +- 3rd/lua/ldo.c | 518 ++++++----- 3rd/lua/ldo.h | 37 +- 3rd/lua/ldump.c | 197 ++-- 3rd/lua/lfunc.c | 224 ++++- 3rd/lua/lfunc.h | 51 +- 3rd/lua/lgc.c | 1388 +++++++++++++++++++--------- 3rd/lua/lgc.h | 137 ++- 3rd/lua/linit.c | 7 +- 3rd/lua/liolib.c | 119 ++- 3rd/lua/ljumptab.h | 112 +++ 3rd/lua/llex.c | 76 +- 3rd/lua/llex.h | 12 +- 3rd/lua/llimits.h | 118 ++- 3rd/lua/lmathlib.c | 433 ++++++++- 3rd/lua/lmem.c | 178 +++- 3rd/lua/lmem.h | 72 +- 3rd/lua/loadlib.c | 261 +++--- 3rd/lua/lobject.c | 340 ++++--- 3rd/lua/lobject.h | 734 ++++++++++----- 3rd/lua/lopcodes.c | 190 ++-- 3rd/lua/lopcodes.h | 401 ++++---- 3rd/lua/lopnames.h | 103 +++ 3rd/lua/loslib.c | 85 +- 3rd/lua/lparser.c | 1000 +++++++++++++------- 3rd/lua/lparser.h | 77 +- 3rd/lua/lprefix.h | 4 +- 3rd/lua/lstate.c | 252 +++-- 3rd/lua/lstate.h | 235 ++++- 3rd/lua/lstring.c | 158 ++-- 3rd/lua/lstring.h | 28 +- 3rd/lua/lstrlib.c | 483 +++++++--- 3rd/lua/ltable.c | 681 +++++++++----- 3rd/lua/ltable.h | 22 +- 3rd/lua/ltablib.c | 52 +- 3rd/lua/ltm.c | 173 +++- 3rd/lua/ltm.h | 37 +- 3rd/lua/lua.c | 423 +++++---- 3rd/lua/lua.h | 70 +- 3rd/lua/luac.c | 470 ++++++++-- 3rd/lua/luaconf.h | 260 +++--- 3rd/lua/lualib.h | 5 +- 3rd/lua/lundump.c | 276 +++--- 3rd/lua/lundump.h | 8 +- 3rd/lua/lutf8lib.c | 117 ++- 3rd/lua/lvm.c | 1728 ++++++++++++++++++++++------------- 3rd/lua/lvm.h | 93 +- 3rd/lua/lzio.c | 2 +- 3rd/lua/lzio.h | 2 +- Makefile | 1 - README.md | 4 +- examples/client.lua | 4 +- examples/login/client.lua | 4 +- lualib-src/lua-profile.c | 269 ------ lualib-src/lua-skynet.c | 3 - lualib/skynet.lua | 178 +++- lualib/skynet/coroutine.lua | 63 +- lualib/sprotoparser.lua | 2 +- service-src/service_snlua.c | 326 ++++++- service/debug_console.lua | 25 +- service/launcher.lua | 49 +- test/testselect.lua | 94 ++ 77 files changed, 11129 insertions(+), 5814 deletions(-) delete mode 100644 3rd/lua/lbitlib.c create mode 100644 3rd/lua/ljumptab.h create mode 100644 3rd/lua/lopnames.h delete mode 100644 lualib-src/lua-profile.c create mode 100644 test/testselect.lua diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 0b7c87818..47dd795aa 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -4,7 +4,7 @@ # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= # Your platform. See PLATS for possible values. -PLAT= none +PLAT= guess CC= gcc -std=gnu99 CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) @@ -14,6 +14,7 @@ LIBS= -lm $(SYSLIBS) $(MYLIBS) AR= ar rcu RANLIB= ranlib RM= rm -f +UNAME= uname SYSCFLAGS= SYSLDFLAGS= @@ -24,16 +25,16 @@ MYLDFLAGS= MYLIBS= MYOBJS= +# Special flags for compiler modules; -Os reduces code size. +CMCFLAGS= -Os + # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris +PLATS= guess aix bsd c89 freebsd generic linux linux-readline 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 lutf8lib.o loadlib.o linit.o +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 lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) LUA_T= lua @@ -65,6 +66,9 @@ $(LUA_T): $(LUA_O) $(LUA_A) $(LUAC_T): $(LUAC_O) $(LUA_A) $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) +test: + ./lua -v + clean: $(RM) $(ALL_T) $(ALL_O) @@ -80,15 +84,21 @@ echo: @echo "AR= $(AR)" @echo "RANLIB= $(RANLIB)" @echo "RM= $(RM)" + @echo "UNAME= $(UNAME)" -# Convenience targets for popular platforms +# Convenience targets for popular platforms. ALL= all -none: - @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" +help: + @echo "Do 'make PLATFORM' where PLATFORM is one of these:" @echo " $(PLATS)" + @echo "See doc/readme.html for complete instructions." + +guess: + @echo Guessing `$(UNAME)` + @$(MAKE) `$(UNAME)` -aix: +AIX aix: $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" bsd: @@ -100,20 +110,24 @@ c89: @echo '*** C89 does not guarantee 64-bit integers for Lua.' @echo '' - -freebsd: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" +FreeBSD NetBSD OpenBSD freebsd: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" generic: $(ALL) -linux: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline" +Linux linux: linux-noreadline + +linux-noreadline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" -macosx: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc +linux-readline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" + +Darwin macos macosx: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" mingw: - $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \ + $(MAKE) "LUA_A=lua54.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 @@ -121,11 +135,21 @@ mingw: posix: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" -solaris: +SunOS solaris: $(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 +# Targets that do not create files (not all makes understand .PHONY). +.PHONY: all $(PLATS) help test clean default o a depend echo + +# Compiler modules may use special flags. +llex.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c + +lparser.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c + +lcode.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c # DO NOT DELETE @@ -134,7 +158,6 @@ lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.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 @@ -149,8 +172,8 @@ ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.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 +lfunc.o: lfunc.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 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 @@ -180,17 +203,17 @@ 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 + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.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 +luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.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 + ltable.h lvm.h ljumptab.h lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ lobject.h ltm.h lzio.h diff --git a/3rd/lua/README b/3rd/lua/README index de361c844..4a68ad51d 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,4 +1,4 @@ -This is a modify version of lua 5.3.2 (http://www.lua.org/ftp/lua-5.3.2.tar.gz) . +This is a modify version of lua 5.4.0-rc1 (http://www.lua.org/work/lua-5.4.0-rc1.tar.gz) . For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index a4282db60..13835180b 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $ +** $Id: lapi.c $ ** Lua API ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -28,7 +29,6 @@ #include "ltm.h" #include "lundump.h" #include "lvm.h" -#include "lfunc.h" @@ -37,11 +37,14 @@ const char lua_ident[] = "$LuaAuthors: " LUA_AUTHORS " $"; -/* value at a non-valid index */ -#define NONVALIDVALUE cast(TValue *, luaO_nilobject) -/* corresponding test */ -#define isvalid(o) ((o) != luaO_nilobject) +/* +** Test for a valid index. +** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. +** However, it covers the most common cases in a faster way. +*/ +#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) + /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) @@ -49,56 +52,54 @@ const char lua_ident[] = /* 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_checkstackindex(l, i, o) \ - api_check(l, isstackindex(i, o), "index not in the stack") - - -static TValue *index2addr (lua_State *L, int idx) { +static TValue *index2value (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"); - if (o >= L->top) return NONVALIDVALUE; - else return o; + StkId o = ci->func + idx; + api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); + if (o >= L->top) return &G(L)->nilvalue; + else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return L->top + idx; + return s2v(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"); - if (ttislcf(ci->func)) /* light C function? */ - return NONVALIDVALUE; /* it has no upvalues */ + if (ttislcf(s2v(ci->func))) /* light C function? */ + return &G(L)->nilvalue; /* it has no upvalues */ else { - CClosure *func = clCvalue(ci->func); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; + CClosure *func = clCvalue(s2v(ci->func)); + return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } } } -/* -** to be called by 'lua_checkstack' in protected mode, to grow stack -** capturing memory errors -*/ -static void growstack (lua_State *L, void *ud) { - int size = *(int *)ud; - luaD_growstack(L, size); +static StkId index2stack (lua_State *L, int idx) { + CallInfo *ci = L->ci; + if (idx > 0) { + StkId o = ci->func + idx; + api_check(L, o < L->top, "unacceptable index"); + return o; + } + else { /* non-positive index */ + api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, !ispseudo(idx), "invalid index"); + return L->top + idx; + } } LUA_API int lua_checkstack (lua_State *L, int n) { int res; - CallInfo *ci = L->ci; + CallInfo *ci; lua_lock(L); + ci = L->ci; api_check(L, n >= 0, "negative 'n'"); if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ @@ -107,7 +108,7 @@ LUA_API int lua_checkstack (lua_State *L, int n) { if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ res = 0; /* no */ else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); + res = luaD_growstack(L, n, 0); } if (res && ci->top < L->top + n) ci->top = L->top + n; /* adjust frame top */ @@ -125,7 +126,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { 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); + setobjs2s(to, to->top, from->top + i); to->top++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); @@ -142,10 +143,9 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { } -LUA_API const lua_Number *lua_version (lua_State *L) { - static const lua_Number version = LUA_VERSION_NUM; - if (L == NULL) return &version; - else return G(L)->version; +LUA_API lua_Number lua_version (lua_State *L) { + UNUSED(L); + return LUA_VERSION_NUM; } @@ -171,18 +171,25 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; + CallInfo *ci; + StkId func; + ptrdiff_t diff; /* difference for new top */ lua_lock(L); + ci = L->ci; + func = ci->func; if (idx >= 0) { - 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; + api_check(L, idx <= ci->top - (func + 1), "new top too large"); + diff = ((func + 1) + idx) - L->top; + for (; diff > 0; diff--) + setnilvalue(s2v(L->top++)); /* clear new slots */ } else { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* 'subtract' index (index is negative) */ + diff = idx + 1; /* will "subtract" index (as it is negative) */ } + if (diff < 0 && hastocloseCfunc(ci->nresults)) + luaF_close(L, L->top + diff, LUA_OK); + L->top += diff; /* correct top only after closing any upvalue */ lua_unlock(L); } @@ -190,11 +197,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { /* ** Reverse the stack segment from 'from' to 'to' ** (auxiliary to 'lua_rotate') +** Note that we move(copy) only the value inside the stack. +** (We do not move additional fields that may exist.) */ static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; - setobj(L, &temp, from); + setobj(L, &temp, s2v(from)); setobjs2s(L, from, to); setobj2s(L, to, &temp); } @@ -209,8 +218,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); t = L->top - 1; /* end of stack segment being rotated */ - p = index2addr(L, idx); /* start of segment */ - api_checkstackindex(L, idx, p); + p = index2stack(L, idx); /* start of segment */ 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' */ @@ -223,12 +231,12 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { TValue *fr, *to; lua_lock(L); - fr = index2addr(L, fromidx); - to = index2addr(L, toidx); - api_checkvalidindex(L, to); + fr = index2value(L, fromidx); + to = index2value(L, toidx); + api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -237,7 +245,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); - setobj2s(L, L->top, index2addr(L, idx)); + setobj2s(L, L->top, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } @@ -250,53 +258,53 @@ 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) ? ttnov(o) : LUA_TNONE); + const TValue *o = index2value(L, idx); + return (isvalid(L, o) ? ttype(o) : LUA_TNONE); } LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); - api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); + api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); return ttypename(t); } LUA_API int lua_iscfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttislcf(o) || (ttisCclosure(o))); } LUA_API int lua_isinteger (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return ttisinteger(o); } LUA_API int lua_isnumber (lua_State *L, int idx) { lua_Number n; - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return tonumber(o, &n); } LUA_API int lua_isstring (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttisstring(o) || cvt2str(o)); } LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttisfulluserdata(o) || ttislightuserdata(o)); } LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - StkId o1 = index2addr(L, index1); - StkId o2 = index2addr(L, index2); - return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; + const TValue *o1 = index2value(L, index1); + const TValue *o2 = index2value(L, index2); + return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; } @@ -310,19 +318,20 @@ LUA_API void lua_arith (lua_State *L, int op) { 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); + luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); L->top--; /* remove second operand */ lua_unlock(L); } LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { - StkId o1, o2; + const TValue *o1; + const TValue *o2; int i = 0; lua_lock(L); /* may call tag method */ - o1 = index2addr(L, index1); - o2 = index2addr(L, index2); - if (isvalid(o1) && isvalid(o2)) { + o1 = index2value(L, index1); + o2 = index2value(L, index2); + if (isvalid(L, o1) && isvalid(L, o2)) { switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; @@ -336,7 +345,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, L->top); + size_t sz = luaO_str2num(s, s2v(L->top)); if (sz != 0) api_incr_top(L); return sz; @@ -344,66 +353,66 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { - lua_Number n; - const TValue *o = index2addr(L, idx); + lua_Number n = 0; + const TValue *o = index2value(L, idx); int isnum = tonumber(o, &n); - if (!isnum) - n = 0; /* call to 'tonumber' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return n; } LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { - lua_Integer res; - const TValue *o = index2addr(L, idx); + lua_Integer res = 0; + const TValue *o = index2value(L, idx); int isnum = tointeger(o, &res); - if (!isnum) - res = 0; /* call to 'tointeger' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return res; } LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return !l_isfalse(o); } LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - StkId o = index2addr(L, idx); + TValue *o; + lua_lock(L); + o = index2value(L, idx); if (!ttisstring(o)) { 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 */ luaO_tostring(L, o); luaC_checkGC(L); - o = index2addr(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); + o = index2value(L, idx); /* previous call may reallocate the stack */ } if (len != NULL) *len = vslen(o); + lua_unlock(L); return svalue(o); } -LUA_API size_t lua_rawlen (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - 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)); +LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { + const TValue *o = index2value(L, idx); + switch (ttypetag(o)) { + case LUA_VSHRSTR: return tsvalue(o)->shrlen; + case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; + case LUA_VUSERDATA: return uvalue(o)->len; + case LUA_VTABLE: return luaH_getn(hvalue(o)); default: return 0; } } LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); if (ttislcf(o)) return fvalue(o); else if (ttisCclosure(o)) return clCvalue(o)->f; @@ -411,9 +420,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 (ttnov(o)) { +static void *touserdata (const TValue *o) { + switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; @@ -421,23 +429,37 @@ LUA_API void *lua_touserdata (lua_State *L, int idx) { } +LUA_API void *lua_touserdata (lua_State *L, int idx) { + const TValue *o = index2value(L, idx); + return touserdata(o); +} + + LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (!ttisthread(o)) ? NULL : thvalue(o); } +/* +** Returns a pointer to the internal representation of an object. +** Note that ANSI C does not allow the conversion of a pointer to +** function to a 'void*', so the conversion here goes through +** a 'size_t'. (As the returned pointer is only informative, this +** conversion should not be a problem.) +*/ LUA_API const void *lua_topointer (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttype(o)) { - case LUA_TTABLE: return hvalue(o); - case LUA_TLCL: return clLvalue(o); - 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: return getudatamem(uvalue(o)); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; + const TValue *o = index2value(L, idx); + switch (ttypetag(o)) { + case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); + case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: + return touserdata(o); + default: { + if (iscollectable(o)) + return gcvalue(o); + else + return NULL; + } } } @@ -450,7 +472,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); - setnilvalue(L->top); + setnilvalue(s2v(L->top)); api_incr_top(L); lua_unlock(L); } @@ -458,7 +480,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setfltvalue(L->top, n); + setfltvalue(s2v(L->top), n); api_incr_top(L); lua_unlock(L); } @@ -466,7 +488,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); - setivalue(L->top, n); + setivalue(s2v(L->top), n); api_incr_top(L); lua_unlock(L); } @@ -492,7 +514,7 @@ 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_lock(L); if (s == NULL) - setnilvalue(L->top); + setnilvalue(s2v(L->top)); else { TString *ts; ts = luaS_new(L, s); @@ -533,7 +555,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { - setfvalue(L->top, fn); + setfvalue(s2v(L->top), fn); api_incr_top(L); } else { @@ -544,10 +566,11 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { cl->f = fn; L->top -= n; while (n--) { - setobj2n(L, &cl->upvalue[n], L->top + n); + setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); /* does not need barrier because closure is white */ + lua_assert(iswhite(cl)); } - setclCvalue(L, L->top, cl); + setclCvalue(L, s2v(L->top), cl); api_incr_top(L); luaC_checkGC(L); } @@ -557,7 +580,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ + if (b) + setbtvalue(s2v(L->top)); + else + setbfvalue(s2v(L->top)); api_incr_top(L); lua_unlock(L); } @@ -565,7 +591,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); - setpvalue(L->top, p); + setpvalue(s2v(L->top), p); api_incr_top(L); lua_unlock(L); } @@ -573,7 +599,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); - setthvalue(L, L->top, L); + setthvalue(L, s2v(L->top), L); api_incr_top(L); lua_unlock(L); return (G(L)->mainthread == L); @@ -596,89 +622,106 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { else { setsvalue2s(L, L->top, str); api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); } lua_unlock(L); - return ttnov(L->top - 1); + return ttype(s2v(L->top - 1)); } LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + Table *reg; lua_lock(L); + reg = hvalue(&G(L)->l_registry); return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } LUA_API int lua_gettable (lua_State *L, int idx) { - StkId t; + const TValue *slot; + TValue *t; lua_lock(L); - t = index2addr(L, idx); - luaV_gettable(L, t, L->top - 1, L->top - 1); + t = index2value(L, idx); + if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { + setobj2s(L, L->top - 1, slot); + } + else + luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); lua_unlock(L); - return ttnov(L->top - 1); + return ttype(s2v(L->top - 1)); } LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { lua_lock(L); - return auxgetstr(L, index2addr(L, idx), k); + return auxgetstr(L, index2value(L, idx), k); } LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { - StkId t; + TValue *t; const TValue *slot; lua_lock(L); - t = index2addr(L, idx); - if (luaV_fastget(L, t, n, slot, luaH_getint)) { + t = index2value(L, idx); + if (luaV_fastgeti(L, t, n, slot)) { 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, slot); + TValue aux; + setivalue(&aux, n); + luaV_finishget(L, t, &aux, L->top, slot); } + api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return ttype(s2v(L->top - 1)); +} + + +static int finishrawget (lua_State *L, const TValue *val) { + if (isempty(val)) /* avoid copying empty items to the stack */ + setnilvalue(s2v(L->top)); + else + setobj2s(L, L->top, val); + api_incr_top(L); + lua_unlock(L); + return ttype(s2v(L->top - 1)); +} + + +static Table *gettable (lua_State *L, int idx) { + TValue *t = index2value(L, idx); + api_check(L, ttistable(t), "table expected"); + return hvalue(t); } LUA_API int lua_rawget (lua_State *L, int idx) { - StkId t; + Table *t; + const TValue *val; lua_lock(L); - t = index2addr(L, idx); - 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); + api_checknelems(L, 1); + t = gettable(L, idx); + val = luaH_get(t, s2v(L->top - 1)); + L->top--; /* remove key */ + return finishrawget(L, val); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { - StkId t; + Table *t; lua_lock(L); - t = index2addr(L, idx); - api_check(L, 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); + t = gettable(L, idx); + return finishrawget(L, luaH_getint(t, n)); } LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { - StkId t; + Table *t; TValue k; lua_lock(L); - t = index2addr(L, idx); - 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); - lua_unlock(L); - return ttnov(L->top - 1); + t = gettable(L, idx); + setpvalue(&k, cast_voidp(p)); + return finishrawget(L, luaH_get(t, &k)); } @@ -686,7 +729,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); - sethvalue(L, L->top, t); + sethvalue2s(L, L->top, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); @@ -700,8 +743,8 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { Table *mt; int res = 0; lua_lock(L); - obj = index2addr(L, objindex); - switch (ttnov(obj)) { + obj = index2value(L, objindex); + switch (ttype(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; @@ -709,11 +752,11 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { mt = uvalue(obj)->metatable; break; default: - mt = G(L)->mt[ttnov(obj)]; + mt = G(L)->mt[ttype(obj)]; break; } if (mt != NULL) { - sethvalue(L, L->top, mt); + sethvalue2s(L, L->top, mt); api_incr_top(L); res = 1; } @@ -722,15 +765,23 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { } -LUA_API int lua_getuservalue (lua_State *L, int idx) { - StkId o; +LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { + TValue *o; + int t; lua_lock(L); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - getuservalue(L, uvalue(o), L->top); + if (n <= 0 || n > uvalue(o)->nuvalue) { + setnilvalue(s2v(L->top)); + t = LUA_TNONE; + } + else { + setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); + t = ttype(s2v(L->top)); + } api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return t; } @@ -745,12 +796,14 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); api_checknelems(L, 1); - if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) + if (luaV_fastset(L, t, str, slot, luaH_getstr)) { + luaV_finishfastset(L, t, slot, s2v(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, slot); + luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); L->top -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ @@ -758,18 +811,24 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + Table *reg; lua_lock(L); /* unlock done in 'auxsetstr' */ + reg = hvalue(&G(L)->l_registry); auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } LUA_API void lua_settable (lua_State *L, int idx) { - StkId t; + TValue *t; + const TValue *slot; lua_lock(L); api_checknelems(L, 2); - t = index2addr(L, idx); - luaV_settable(L, t, L->top - 2, L->top - 1); + t = index2value(L, idx); + if (luaV_fastset(L, t, s2v(L->top - 2), slot, luaH_get)) { + luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + } + else + luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); L->top -= 2; /* pop index and value */ lua_unlock(L); } @@ -777,68 +836,63 @@ LUA_API void lua_settable (lua_State *L, int idx) { LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, index2addr(L, idx), k); + auxsetstr(L, index2value(L, idx), k); } LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { - StkId t; + TValue *t; const TValue *slot; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) - L->top--; /* pop value */ + t = index2value(L, idx); + if (luaV_fastseti(L, t, n, slot)) { + luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + } else { - setivalue(L->top, n); - api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, slot); - L->top -= 2; /* pop value and key */ + TValue aux; + setivalue(&aux, n); + luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); } + L->top--; /* pop value */ lua_unlock(L); } -LUA_API void lua_rawset (lua_State *L, int idx) { - StkId o; +static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { + Table *t; TValue *slot; lua_lock(L); - api_checknelems(L, 2); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - 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; + api_checknelems(L, n); + t = gettable(L, idx); + slot = luaH_set(L, t, key); + setobj2t(L, slot, s2v(L->top - 1)); + invalidateTMcache(t); + luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); + L->top -= n; lua_unlock(L); } -LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - StkId o; - lua_lock(L); - api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - luaH_setint(L, hvalue(o), n, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top-1); - L->top--; - lua_unlock(L); +LUA_API void lua_rawset (lua_State *L, int idx) { + aux_rawset(L, idx, s2v(L->top - 2), 2); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId o; - TValue k, *slot; + TValue k; + setpvalue(&k, cast_voidp(p)); + aux_rawset(L, idx, &k, 1); +} + + +LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { + Table *t; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - setpvalue(&k, cast(void *, p)); - slot = luaH_set(L, hvalue(o), &k); - setobj2t(L, slot, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top - 1); + t = gettable(L, idx); + luaH_setint(L, t, n, s2v(L->top - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top--; lua_unlock(L); } @@ -849,14 +903,14 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { Table *mt; lua_lock(L); api_checknelems(L, 1); - obj = index2addr(L, objindex); - if (ttisnil(L->top - 1)) + obj = index2value(L, objindex); + if (ttisnil(s2v(L->top - 1))) mt = NULL; else { - api_check(L, ttistable(L->top - 1), "table expected"); - mt = hvalue(L->top - 1); + api_check(L, ttistable(s2v(L->top - 1)), "table expected"); + mt = hvalue(s2v(L->top - 1)); } - switch (ttnov(obj)) { + switch (ttype(obj)) { case LUA_TTABLE: { if (isshared(hvalue(obj))) luaG_runerror(L, "can't setmetatable to shared table"); @@ -876,7 +930,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { break; } default: { - G(L)->mt[ttnov(obj)] = mt; + G(L)->mt[ttype(obj)] = mt; break; } } @@ -886,16 +940,23 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { } -LUA_API void lua_setuservalue (lua_State *L, int idx) { - StkId o; +LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { + TValue *o; + int res; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - setuservalue(L, uvalue(o), L->top - 1); - luaC_barrier(L, gcvalue(o), L->top - 1); + if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) + res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ + else { + setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + res = 1; + } L->top--; lua_unlock(L); + return res; } @@ -919,7 +980,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, 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? */ + if (k != NULL && yieldable(L)) { /* 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); /* do the call */ @@ -962,12 +1023,12 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, if (errfunc == 0) func = 0; else { - StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); + StkId o = index2stack(L, errfunc); + api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ - if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ + if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); } @@ -976,7 +1037,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u.c.k = k; /* save continuation */ ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ - ci->extra = savestack(L, c.func); + ci->u2.funcidx = cast_int(savestack(L, c.func)); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ @@ -991,6 +1052,16 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, return status; } +static void set_env (lua_State *L, LClosure *f) { + 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); + } +} LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode) { @@ -1001,15 +1072,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, luaZ_init(L, &z, reader, 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 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]); - } + LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ + set_env(L,f); } lua_unlock(L); return status; @@ -1021,19 +1085,11 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { api_check(L, isshared(f->p), "Not a shared proto"); lua_lock(L); cl = luaF_newLclosure(L,f->nupvalues); - setclLvalue(L,L->top,cl); + setclLvalue2s(L,L->top,cl); api_incr_top(L); cl->p = f->p; 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]); - } + set_env(L,cl); lua_unlock(L); } @@ -1045,11 +1101,9 @@ LUA_API void lua_sharefunction (lua_State *L, int index) { } LUA_API void lua_sharestring (lua_State *L, int index) { - const char *str = lua_tostring(L, index); - if (str == NULL) + if (lua_type(L,index) != LUA_TSTRING) luaG_runerror(L, "need a string to share"); - - TString *ts = (TString *)(str - sizeof(UTString)); + TString *ts = tsvalue(index2value(L,index)); luaS_share(ts); } @@ -1060,7 +1114,7 @@ LUA_API void lua_clonetable(lua_State *L, const void * tp) { luaG_runerror(L, "Not a shared table"); lua_lock(L); - sethvalue(L, L->top, t); + sethvalue2s(L, L->top, t); api_incr_top(L); lua_unlock(L); } @@ -1070,7 +1124,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { TValue *o; lua_lock(L); api_checknelems(L, 1); - o = L->top - 1; + o = s2v(L->top - 1); if (isLfunction(o)) status = luaU_dump(L, getproto(o), writer, data, strip); else @@ -1088,12 +1142,13 @@ LUA_API int lua_status (lua_State *L) { /* ** Garbage-collection function */ - -LUA_API int lua_gc (lua_State *L, int what, int data) { +LUA_API int lua_gc (lua_State *L, int what, ...) { + va_list argp; int res = 0; global_State *g; lua_lock(L); g = G(L); + va_start(argp, what); switch (what) { case LUA_GCSTOP: { g->gcrunning = 0; @@ -1118,11 +1173,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { + int data = va_arg(argp, int); l_mem debt = 1; /* =1 to signal that it did an actual step */ lu_byte oldrunning = g->gcrunning; g->gcrunning = 1; /* allow GC to run */ if (data == 0) { - luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ + luaE_setdebt(g, 0); /* do a basic step */ luaC_step(L); } else { /* add 'data' to total debt */ @@ -1136,22 +1192,49 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSETPAUSE: { - res = g->gcpause; - g->gcpause = data; + int data = va_arg(argp, int); + res = getgcparam(g->gcpause); + setgcparam(g->gcpause, data); break; } case LUA_GCSETSTEPMUL: { - res = g->gcstepmul; - if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ - g->gcstepmul = data; + int data = va_arg(argp, int); + res = getgcparam(g->gcstepmul); + setgcparam(g->gcstepmul, data); break; } case LUA_GCISRUNNING: { res = g->gcrunning; break; } + case LUA_GCGEN: { + int minormul = va_arg(argp, int); + int majormul = va_arg(argp, int); + res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; + if (minormul != 0) + g->genminormul = minormul; + if (majormul != 0) + setgcparam(g->genmajormul, majormul); + luaC_changemode(L, KGC_GEN); + break; + } + case LUA_GCINC: { + int pause = va_arg(argp, int); + int stepmul = va_arg(argp, int); + int stepsize = va_arg(argp, int); + res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; + if (pause != 0) + setgcparam(g->gcpause, pause); + if (stepmul != 0) + setgcparam(g->gcstepmul, stepmul); + if (stepsize != 0) + g->gcstepsize = stepsize; + luaC_changemode(L, KGC_INC); + break; + } default: res = -1; /* invalid option */ } + va_end(argp); lua_unlock(L); return res; } @@ -1164,21 +1247,27 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { LUA_API int lua_error (lua_State *L) { + TValue *errobj; lua_lock(L); + errobj = s2v(L->top - 1); api_checknelems(L, 1); - luaG_errormsg(L); + /* error object is the memory error message? */ + if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) + luaM_error(L); /* raise a memory error */ + else + luaG_errormsg(L); /* raise a regular error */ /* code unreachable; will unlock when control actually leaves the kernel */ return 0; /* to avoid warnings */ } LUA_API int lua_next (lua_State *L, int idx) { - StkId t; + Table *t; int more; lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - more = luaH_next(L, hvalue(t), L->top - 1); + api_checknelems(L, 1); + t = gettable(L, idx); + more = luaH_next(L, t, L->top - 1); if (more) { api_incr_top(L); } @@ -1189,26 +1278,40 @@ LUA_API int lua_next (lua_State *L, int idx) { } +LUA_API void lua_toclose (lua_State *L, int idx) { + int nresults; + StkId o; + lua_lock(L); + o = index2stack(L, idx); + nresults = L->ci->nresults; + api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, + "marked index below or equal new one"); + luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ + if (!hastocloseCfunc(nresults)) /* function not marked yet? */ + L->ci->nresults = codeNresults(nresults); /* mark it */ + lua_assert(hastocloseCfunc(L->ci->nresults)); + lua_unlock(L); +} + + LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); - if (n >= 2) { + if (n > 0) luaV_concat(L, n); - } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); + else { /* nothing to concatenate */ + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } - /* else n == 1; nothing to do */ luaC_checkGC(L); lua_unlock(L); } LUA_API void lua_len (lua_State *L, int idx) { - StkId t; + TValue *t; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); luaV_objlen(L, L->top, t); api_incr_top(L); lua_unlock(L); @@ -1233,11 +1336,28 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { } -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { +void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { + lua_lock(L); + G(L)->ud_warn = ud; + G(L)->warnf = f; + lua_unlock(L); +} + + +void lua_warning (lua_State *L, const char *msg, int tocont) { + lua_lock(L); + luaE_warning(L, msg, tocont); + lua_unlock(L); +} + + + +LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); - u = luaS_newudata(L, size); - setuvalue(L, L->top, u); + api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); + u = luaS_newudata(L, size, nuvalue); + setuvalue(L, s2v(L->top), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -1246,25 +1366,27 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { -static const char *aux_upvalue (StkId fi, int n, TValue **val, - CClosure **owner, UpVal **uv) { - switch (ttype(fi)) { - case LUA_TCCL: { /* C closure */ +static const char *aux_upvalue (TValue *fi, int n, TValue **val, + GCObject **owner) { + switch (ttypetag(fi)) { + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; + if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) + return NULL; /* 'n' not in [1, f->nupvalues] */ *val = &f->upvalue[n-1]; - if (owner) *owner = f; + if (owner) *owner = obj2gco(f); return ""; } - case LUA_TLCL: { /* Lua closure */ + case LUA_VLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; + if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) + return NULL; /* 'n' not in [1, p->sizeupvalues] */ *val = f->upvals[n-1]->v; - if (uv) *uv = f->upvals[n - 1]; + if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; - return (name == NULL) ? "(*no name)" : getstr(name); + return (name == NULL) ? "(no name)" : getstr(name); } default: return NULL; /* not a closure */ } @@ -1275,7 +1397,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, NULL); + name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { setobj2s(L, L->top, val); api_incr_top(L); @@ -1288,18 +1410,16 @@ 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 */ - CClosure *owner = NULL; - UpVal *uv = NULL; - StkId fi; + GCObject *owner = NULL; /* to avoid warnings */ + TValue *fi; lua_lock(L); - fi = index2addr(L, funcindex); + fi = index2value(L, funcindex); api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner, &uv); + name = aux_upvalue(fi, n, &val, &owner); if (name) { L->top--; - setobj(L, val, L->top); - if (owner) { luaC_barrier(L, owner, L->top); } - else if (uv) { luaC_upvalbarrier(L, uv); } + setobj(L, val, s2v(L->top)); + luaC_barrier(L, owner, val); } lua_unlock(L); return name; @@ -1308,7 +1428,7 @@ 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); + TValue *fi = index2value(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"); @@ -1318,12 +1438,12 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { - StkId fi = index2addr(L, fidx); - switch (ttype(fi)) { - case LUA_TLCL: { /* lua closure */ + TValue *fi = index2value(L, fidx); + switch (ttypetag(fi)) { + case LUA_VLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); } - case LUA_TCCL: { /* C closure */ + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); return &f->upvalue[n - 1]; @@ -1341,11 +1461,8 @@ 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; - (*up1)->refcount++; - if (upisopen(*up1)) (*up1)->u.open.touched = 1; - luaC_upvalbarrier(L, *up1); + luaC_objbarrier(L, f1, *up1); } diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index 8e16ad53d..41216b270 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lapi.h $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -11,14 +11,37 @@ #include "llimits.h" #include "lstate.h" + +/* Increments 'L->top', checking for stack overflows */ #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ "stack overflow");} + +/* +** If a call returns too many multiple returns, the callee may not have +** stack space to accommodate all results. In this case, this macro +** increases its stack space ('L->ci->top'). +*/ #define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + +/* Ensure the stack has at least 'n' elements */ #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ "not enough elements in the stack") +/* +** To reduce the overhead of returning from C functions, the presence of +** to-be-closed variables in these functions is coded in the CallInfo's +** field 'nresults', in a way that functions with no to-be-closed variables +** with zero, one, or "all" wanted results have no overhead. Functions +** with other number of wanted results, as well as functions with +** variables to be closed, have an extra check. +*/ + +#define hastocloseCfunc(n) ((n) < LUA_MULTRET) + +#define codeNresults(n) (-(n) - 3) + #endif diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 6da4db78c..16d16cf72 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.289.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lauxlib.c $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -27,6 +27,12 @@ #include "lauxlib.h" +#if !defined(MAX_SIZET) +/* maximum value for size_t */ +#define MAX_SIZET ((size_t)(~(size_t)0)) +#endif + + /* ** {====================================================== ** Traceback @@ -40,8 +46,8 @@ /* -** search for 'objidx' in table at index -1. -** return 1 + string at top if find a good name. +** Search for 'objidx' in table at index -1. ('objidx' must be an +** absolute index.) Return 1 + string at top if it found a good name. */ static int findfield (lua_State *L, int objidx, int level) { if (level == 0 || !lua_istable(L, -1)) @@ -54,10 +60,10 @@ static int findfield (lua_State *L, int objidx, int level) { return 1; } else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); + /* stack: lib_name, lib_table, field_name (top) */ + lua_pushliteral(L, "."); /* place '.' between the two names */ + lua_replace(L, -3); /* (in the slot occupied by table) */ + lua_concat(L, 3); /* lib_name.field_name */ return 1; } } @@ -76,12 +82,12 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { 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.'? */ + if (strncmp(name, LUA_GNAME ".", 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 */ + lua_copy(L, -1, top + 1); /* copy name to proper place */ + lua_settop(L, top + 1); /* remove table "loaded" and name copy */ return 1; } else { @@ -124,32 +130,37 @@ static int lastlevel (lua_State *L) { LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { + luaL_Buffer b; lua_Debug ar; - int top = lua_gettop(L); 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:"); + int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; + luaL_buffinit(L, &b); + if (msg) { + luaL_addstring(&b, msg); + luaL_addchar(&b, '\n'); + } + luaL_addstring(&b, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { - if (n1-- == 0) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = last - LEVELS2 + 1; /* and skip to last ones */ + if (limit2show-- == 0) { /* too many levels? */ + int n = last - level - LEVELS2 + 1; /* number of levels to skip */ + lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); + luaL_addvalue(&b); /* add warning about skip */ + level += n; /* and skip to last levels */ } else { lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); + if (ar.currentline <= 0) + lua_pushfstring(L, "\n\t%s: in ", ar.short_src); + else + lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); + luaL_addvalue(&b); pushfuncname(L, &ar); + luaL_addvalue(&b); if (ar.istailcall) - lua_pushliteral(L, "\n\t(...tail calls...)"); - lua_concat(L, lua_gettop(L) - top); + luaL_addstring(&b, "\n\t(...tail calls...)"); } } - lua_concat(L, lua_gettop(L) - top); + luaL_pushresult(&b); } /* }====================================================== */ @@ -179,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { } -static int typeerror (lua_State *L, int arg, const char *tname) { +int luaL_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) @@ -194,7 +205,7 @@ static int typeerror (lua_State *L, int arg, const char *tname) { static void tag_error (lua_State *L, int arg, int tag) { - typeerror(L, arg, lua_typename(L, tag)); + luaL_typeerror(L, arg, lua_typename(L, tag)); } @@ -238,7 +249,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { return 1; } else { - lua_pushnil(L); + luaL_pushfail(L); if (fname) lua_pushfstring(L, "%s: %s", fname, strerror(en)); else @@ -273,23 +284,24 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { LUALIB_API int luaL_execresult (lua_State *L, int stat) { const char *what = "exit"; /* type of termination */ - if (stat == -1) /* error? */ + if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else - lua_pushnil(L); + luaL_pushfail(L); lua_pushstring(L, what); lua_pushinteger(L, stat); - return 3; /* return true/nil,what,code */ + return 3; /* return true/fail,what,code */ } } /* }====================================================== */ + /* ** {====================================================== ** Userdata's metatable manipulation @@ -332,7 +344,7 @@ LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { void *p = luaL_testudata(L, ud, tname); - if (p == NULL) typeerror(L, ud, tname); + luaL_argexpected(L, p != NULL, ud, tname); return p; } @@ -464,8 +476,8 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { 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"); + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ } box->box = temp; box->bsize = newsize; @@ -479,16 +491,20 @@ static int boxgc (lua_State *L) { } -static void *newbox (lua_State *L, size_t newsize) { - UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); +static const luaL_Reg boxmt[] = { /* box metamethods */ + {"__gc", boxgc}, + {"__close", boxgc}, + {NULL, NULL} +}; + + +static void newbox (lua_State *L) { + UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); 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 */ - } + if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ + luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ lua_setmetatable(L, -2); - return resizebox(L, -1, newsize); } @@ -496,38 +512,64 @@ static void *newbox (lua_State *L, size_t newsize) { ** check whether buffer is using a userdata on the stack as a temporary ** buffer */ -#define buffonstack(B) ((B)->b != (B)->initb) +#define buffonstack(B) ((B)->b != (B)->init.b) /* -** returns a pointer to a free area with at least 'sz' bytes +** Compute new size for buffer 'B', enough to accommodate extra 'sz' +** bytes. */ -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - lua_State *L = B->L; - if (B->size - B->n < sz) { /* not enough space? */ +static size_t newbuffsize (luaL_Buffer *B, size_t sz) { + size_t newsize = B->size * 2; /* double buffer size */ + if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ + return luaL_error(B->L, "buffer too large"); + if (newsize < B->n + sz) /* double is not big enough? */ + newsize = B->n + sz; + return newsize; +} + + +/* +** Returns a pointer to a free area with at least 'sz' bytes in buffer +** 'B'. 'boxidx' is the relative position in the stack where the +** buffer's box is or should be. +*/ +static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { + if (B->size - B->n >= sz) /* enough space? */ + return B->b + B->n; + else { + lua_State *L = B->L; char *newbuff; - size_t newsize = B->size * 2; /* double buffer size */ - if (newsize - B->n < sz) /* not big enough? */ - newsize = B->n + sz; - if (newsize < B->n || newsize - B->n < sz) - luaL_error(L, "buffer too large"); + size_t newsize = newbuffsize(B, sz); /* create larger buffer */ - if (buffonstack(B)) - newbuff = (char *)resizebox(L, -1, newsize); - else { /* no buffer yet */ - newbuff = (char *)newbox(L, newsize); + if (buffonstack(B)) /* buffer already has a box? */ + newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ + else { /* no box yet */ + lua_pushnil(L); /* reserve slot for final result */ + newbox(L); /* create a new box */ + /* move box (and slot) to its intended position */ + lua_rotate(L, boxidx - 1, 2); + lua_toclose(L, boxidx); + newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ } B->b = newbuff; B->size = newsize; + return newbuff + B->n; } - return &B->b[B->n]; +} + +/* +** returns a pointer to a free area with at least 'sz' bytes +*/ +LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { + return prepbuffsize(B, sz, -1); } LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ - char *b = luaL_prepbuffsize(B, l); + char *b = prepbuffsize(B, l, -1); memcpy(b, s, l * sizeof(char)); luaL_addsize(B, l); } @@ -543,8 +585,8 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); if (buffonstack(B)) { - resizebox(L, -2, 0); /* delete old buffer */ - lua_remove(L, -2); /* remove its header from the stack */ + lua_copy(L, -1, -3); /* move string to reserved slot */ + lua_pop(L, 2); /* pop string and box (closing the box) */ } } @@ -555,20 +597,29 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { } +/* +** 'luaL_addvalue' is the only function in the Buffer system where the +** box (if existent) is not on the top of the stack. So, instead of +** calling 'luaL_addlstring', it replicates the code using -2 as the +** last argument to 'prepbuffsize', signaling that the box is (or will +** be) bellow the string being added to the buffer. (Box creation can +** trigger an emergency GC, so we should not remove the string from the +** stack before we have the space guaranteed.) +*/ LUALIB_API void luaL_addvalue (luaL_Buffer *B) { lua_State *L = B->L; - size_t l; - const char *s = lua_tolstring(L, -1, &l); - if (buffonstack(B)) - lua_insert(L, -2); /* put value below buffer */ - luaL_addlstring(B, s, l); - lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */ + size_t len; + const char *s = lua_tolstring(L, -1, &len); + char *b = prepbuffsize(B, len, -2); + memcpy(b, s, len * sizeof(char)); + luaL_addsize(B, len); + lua_pop(L, 1); /* pop string */ } LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->L = L; - B->b = B->initb; + B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; } @@ -576,7 +627,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { luaL_buffinit(L, B); - return luaL_prepbuffsize(B, sz); + return prepbuffsize(B, sz, -1); } /* }====================================================== */ @@ -699,6 +750,7 @@ static int skipcomment (LoadF *lf, int *cp) { else return 0; /* no comment */ } + LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, const char *mode) { LoadF lf; @@ -844,87 +896,6 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { } -/* -** {====================================================== -** Compatibility with 5.1 module functions -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -static const char *luaL_findtable (lua_State *L, int idx, - const char *fname, int szhint) { - const char *e; - if (idx) lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, e - fname); - 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); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - - -/* -** Count number of elements in a luaL_Reg list. -*/ -static int libsize (const luaL_Reg *l) { - int size = 0; - for (; l && l->name; l++) size++; - return size; -} - - -/* -** Find or create a module table with a given name. The function -** 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, 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_remove(L, -2); /* remove LOADED table */ -} - - -LUALIB_API void luaL_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { - luaL_checkversion(L); - if (libname) { - luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ - lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ - } - if (l) - luaL_setfuncs(L, l, nup); - else - lua_pop(L, nup); /* remove upvalues */ -} - -#endif -/* }====================================================== */ - /* ** set functions from list 'l' into table at top - 'nup'; each ** function gets the 'nup' elements at the top as upvalues. @@ -933,10 +904,14 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { 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 */ + if (l->func == NULL) /* place holder? */ + lua_pushboolean(L, 0); + else { + 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 */ @@ -987,18 +962,24 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, } -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r) { const char *wild; size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ + luaL_addlstring(b, s, wild - s); /* push prefix */ + luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after 'p' */ } - luaL_addstring(&b, s); /* push last suffix */ + luaL_addstring(b, s); /* push last suffix */ +} + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, + const char *p, const char *r) { + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addgsub(&b, s, p, r); luaL_pushresult(&b); return lua_tostring(L, -1); } @@ -1016,28 +997,64 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + msg); return 0; /* return to Lua to abort */ } +/* +** Emit a warning. '*warnstate' means: +** 0 - warning system is off; +** 1 - ready to start a new message; +** 2 - previous message is to be continued. +*/ +static void warnf (void *ud, const char *message, int tocont) { + int *warnstate = (int *)ud; + if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ + if (strcmp(message, "@off") == 0) + *warnstate = 0; + else if (strcmp(message, "@on") == 0) + *warnstate = 1; + return; + } + else if (*warnstate == 0) /* warnings off? */ + return; + if (*warnstate == 1) /* previous message was the last? */ + lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ + lua_writestringerror("%s", message); /* write message */ + if (tocont) /* not the last part? */ + *warnstate = 2; /* to be continued */ + else { /* last part */ + lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ + *warnstate = 1; /* ready to start a new message */ + } +} + + LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); - if (L) lua_atpanic(L, &panic); + if (L) { + int *warnstate; /* space for warning state */ + lua_atpanic(L, &panic); + warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); + luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ + *warnstate = 0; /* default is warnings off */ + lua_setwarnf(L, warnf, warnstate); + } return L; } LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { - const lua_Number *v = lua_version(L); + 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) + else if (v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); + (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); } // use clonefunction diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index f0a289dc7..e3348ad67 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lauxlib.h $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -15,6 +15,12 @@ #include "lua.h" +/* global table */ +#define LUA_GNAME "_G" + + +typedef struct luaL_Buffer luaL_Buffer; + /* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) @@ -44,6 +50,7 @@ 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 arg, const char *extramsg); +LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, @@ -73,6 +80,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); + /* predefined references */ #define LUA_NOREF (-2) #define LUA_REFNIL (-1) @@ -95,8 +103,10 @@ LUALIB_API lua_State *(luaL_newstate) (void); 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); +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r); +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, + const char *p, const char *r); LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); @@ -123,6 +133,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_argcheck(L, cond,arg,extramsg) \ ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + +#define luaL_argexpected(L,cond,arg,tname) \ + ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) @@ -141,19 +155,30 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +/* push the value used to represent failure/error */ +#define luaL_pushfail(L) lua_pushnil(L) + + /* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ -typedef struct luaL_Buffer { +struct luaL_Buffer { char *b; /* buffer address */ size_t size; /* buffer size */ size_t n; /* number of characters in buffer */ lua_State *L; - char initb[LUAL_BUFFERSIZE]; /* initial buffer */ -} luaL_Buffer; + union { + LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ + char b[LUAL_BUFFERSIZE]; /* initial buffer */ + } init; +}; + + +#define luaL_bufflen(bf) ((bf)->n) +#define luaL_buffaddr(bf) ((bf)->b) #define luaL_addchar(B,c) \ @@ -162,6 +187,8 @@ typedef struct luaL_Buffer { #define luaL_addsize(B,s) ((B)->n += (s)) +#define luaL_buffsub(B,s) ((B)->n -= (s)) + LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 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); @@ -199,21 +226,6 @@ typedef struct luaL_Stream { /* }====================================================== */ - - -/* compatibility with old module system */ -#if defined(LUA_COMPAT_MODULE) - -LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, - int sizehint); -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); - -#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) - -#endif - - /* ** {================================================================== ** "Abstraction Layer" for basic report of messages and errors diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 6460e4f8d..747fd45a2 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lbaselib.c $ ** Basic library ** See Copyright Notice in lua.h */ @@ -24,18 +24,12 @@ static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; + for (i = 1; i <= n; i++) { /* for each argument */ size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); + const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ + if (i > 1) /* not the first element? */ + lua_writestring("\t", 1); /* add a tab before it */ + lua_writestring(s, l); /* print it */ lua_pop(L, 1); /* pop result */ } lua_writeline(); @@ -43,13 +37,31 @@ static int luaB_print (lua_State *L) { } +/* +** Creates a warning with all given arguments. +** Check first for errors; otherwise an error may interrupt +** the composition of a warning, leaving it unfinished. +*/ +static int luaB_warn (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int i; + luaL_checkstring(L, 1); /* at least one argument */ + for (i = 2; i <= n; i++) + luaL_checkstring(L, i); /* make sure all arguments are strings */ + for (i = 1; i < n; i++) /* compose warning */ + lua_warning(L, lua_tostring(L, i), 1); + lua_warning(L, lua_tostring(L, n), 0); /* close warning */ + 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 */ + if (*s == '-') { s++; neg = 1; } /* handle sign */ else if (*s == '+') s++; if (!isalnum((unsigned char)*s)) /* no digit? */ return NULL; @@ -68,7 +80,6 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) { static int luaB_tonumber (lua_State *L) { 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; @@ -79,6 +90,7 @@ static int luaB_tonumber (lua_State *L) { if (s != NULL && lua_stringtonumber(L, s) == l + 1) return 1; /* successful conversion to number */ /* else not a number */ + luaL_checkany(L, 1); /* (but there must be some parameter) */ } } else { @@ -94,7 +106,7 @@ static int luaB_tonumber (lua_State *L) { return 1; } /* else not a number */ } /* else not a number */ - lua_pushnil(L); /* not a number */ + luaL_pushfail(L); /* not a number */ return 1; } @@ -125,8 +137,7 @@ static int luaB_getmetatable (lua_State *L) { static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); + luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); @@ -145,8 +156,8 @@ static int luaB_rawequal (lua_State *L) { static int luaB_rawlen (lua_State *L) { int t = lua_type(L, 1); - luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, - "table or string expected"); + luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, + "table or string"); lua_pushinteger(L, lua_rawlen(L, 1)); return 1; } @@ -170,27 +181,58 @@ static int luaB_rawset (lua_State *L) { } +static int pushmode (lua_State *L, int oldmode) { + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); + return 1; +} + + static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", - "isrunning", NULL}; + "isrunning", "generational", "incremental", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING}; + LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - 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, (lua_Number)res + ((lua_Number)b/1024)); + int k = lua_gc(L, o); + int b = lua_gc(L, LUA_GCCOUNTB); + lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); + return 1; + } + case LUA_GCSTEP: { + int step = (int)luaL_optinteger(L, 2, 0); + int res = lua_gc(L, o, step); + lua_pushboolean(L, res); + return 1; + } + case LUA_GCSETPAUSE: + case LUA_GCSETSTEPMUL: { + int p = (int)luaL_optinteger(L, 2, 0); + int previous = lua_gc(L, o, p); + lua_pushinteger(L, previous); return 1; } - case LUA_GCSTEP: case LUA_GCISRUNNING: { + case LUA_GCISRUNNING: { + int res = lua_gc(L, o); lua_pushboolean(L, res); return 1; } + case LUA_GCGEN: { + int minormul = (int)luaL_optinteger(L, 2, 0); + int majormul = (int)luaL_optinteger(L, 3, 0); + return pushmode(L, lua_gc(L, o, minormul, majormul)); + } + case LUA_GCINC: { + int pause = (int)luaL_optinteger(L, 2, 0); + int stepmul = (int)luaL_optinteger(L, 3, 0); + int stepsize = (int)luaL_optinteger(L, 4, 0); + return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); + } default: { + int res = lua_gc(L, o); lua_pushinteger(L, res); return 1; } @@ -206,23 +248,6 @@ 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? */ - lua_pushcfunction(L, iter); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - if (iszero) lua_pushinteger(L, 0); /* and initial value */ - else lua_pushnil(L); - } - else { - lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ - } - return 3; -} - - static int luaB_next (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 2); /* create a 2nd argument if there isn't one */ @@ -236,7 +261,17 @@ static int luaB_next (lua_State *L) { static int luaB_pairs (lua_State *L) { - return pairsmeta(L, "__pairs", 0, luaB_next); + luaL_checkany(L, 1); + if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ + lua_pushcfunction(L, luaB_next); /* will return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushnil(L); /* and initial value */ + } + else { + lua_pushvalue(L, 1); /* argument 'self' to metamethod */ + lua_call(L, 1, 3); /* get 3 values from metamethod */ + } + return 3; } @@ -255,15 +290,11 @@ static int ipairsaux (lua_State *L) { ** (The given "table" may not be a table.) */ static int luaB_ipairs (lua_State *L) { -#if defined(LUA_COMPAT_IPAIRS) - return pairsmeta(L, "__ipairs", 1, ipairsaux); -#else luaL_checkany(L, 1); lua_pushcfunction(L, ipairsaux); /* iteration function */ lua_pushvalue(L, 1); /* state */ lua_pushinteger(L, 0); /* initial value */ return 3; -#endif } @@ -277,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) { return 1; } else { /* error (message is on top of the stack) */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ + return 2; /* return fail plus error message */ } } @@ -459,13 +490,11 @@ static const luaL_Reg base_funcs[] = { {"ipairs", luaB_ipairs}, {"loadfile", luaB_loadfile}, {"load", luaB_load}, -#if defined(LUA_COMPAT_LOADSTRING) - {"loadstring", luaB_load}, -#endif {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, {"print", luaB_print}, + {"warn", luaB_warn}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, @@ -477,7 +506,7 @@ static const luaL_Reg base_funcs[] = { {"type", luaB_type}, {"xpcall", luaB_xpcall}, /* placeholders */ - {"_G", NULL}, + {LUA_GNAME, NULL}, {"_VERSION", NULL}, {NULL, NULL} }; @@ -489,7 +518,7 @@ LUAMOD_API int luaopen_base (lua_State *L) { luaL_setfuncs(L, base_funcs, 0); /* set global _G */ lua_pushvalue(L, -1); - lua_setfield(L, -2, "_G"); + lua_setfield(L, -2, LUA_GNAME); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); diff --git a/3rd/lua/lbitlib.c b/3rd/lua/lbitlib.c deleted file mode 100644 index 4786c0d48..000000000 --- a/3rd/lua/lbitlib.c +++ /dev/null @@ -1,233 +0,0 @@ -/* -** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ -** Standard library for bitwise operations -** See Copyright Notice in lua.h -*/ - -#define lbitlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#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 -#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) - - -/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ -#define mask(n) (~((ALLONES << 1) << ((n) - 1))) - - - -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 &= checkunsigned(L, i); - return trim(r); -} - - -static int b_and (lua_State *L) { - lua_Unsigned r = andaux(L); - pushunsigned(L, r); - return 1; -} - - -static int b_test (lua_State *L) { - lua_Unsigned r = andaux(L); - lua_pushboolean(L, r != 0); - return 1; -} - - -static int b_or (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r |= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_xor (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r ^= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_not (lua_State *L) { - lua_Unsigned r = ~checkunsigned(L, 1); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { - if (i < 0) { /* shift right? */ - i = -i; - r = trim(r); - if (i >= LUA_NBITS) r = 0; - else r >>= i; - } - else { /* shift left */ - if (i >= LUA_NBITS) r = 0; - else r <<= i; - r = trim(r); - } - pushunsigned(L, r); - return 1; -} - - -static int b_lshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); -} - - -static int b_rshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); -} - - -static int b_arshift (lua_State *L) { - 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); - else { /* arithmetic shift for 'negative' number */ - if (i >= LUA_NBITS) r = ALLONES; - else - r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ - pushunsigned(L, r); - return 1; - } -} - - -static int b_rot (lua_State *L, lua_Integer d) { - 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)); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkinteger(L, 2)); -} - - -static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkinteger(L, 2)); -} - - -/* -** get field and width arguments for field-manipulation functions, -** checking whether they are valid. -** ('luaL_error' called without 'return' to avoid later warnings about -** 'width' being used uninitialized.) -*/ -static int fieldargs (lua_State *L, int farg, int *width) { - 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 = (int)w; - return (int)f; -} - - -static int b_extract (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - int f = fieldargs(L, 2, &w); - r = (r >> f) & mask(w); - pushunsigned(L, r); - return 1; -} - - -static int b_replace (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - lua_Unsigned v = trim(checkunsigned(L, 2)); - int f = fieldargs(L, 3, &w); - lua_Unsigned m = mask(w); - r = (r & ~(m << f)) | ((v & m) << f); - pushunsigned(L, r); - return 1; -} - - -static const luaL_Reg bitlib[] = { - {"arshift", b_arshift}, - {"band", b_and}, - {"bnot", b_not}, - {"bor", b_or}, - {"bxor", b_xor}, - {"btest", b_test}, - {"extract", b_extract}, - {"lrotate", b_lrot}, - {"lshift", b_lshift}, - {"replace", b_replace}, - {"rrotate", b_rrot}, - {"rshift", b_rshift}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - luaL_newlib(L, bitlib); - 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 12619f54a..6f241c947 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.112.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lcode.c $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -36,11 +37,22 @@ #define hasjumps(e) ((e)->t != (e)->f) +static int codesJ (FuncState *fs, OpCode o, int sj, int k); + + + +/* semantic error */ +l_noret luaK_semerror (LexState *ls, const char *msg) { + ls->t.token = 0; /* remove "near " from final message */ + luaX_syntaxerror(ls, msg); +} + + /* ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ -static int tonumeral(const expdesc *e, TValue *v) { +static int tonumeral (const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { @@ -55,6 +67,60 @@ static int tonumeral(const expdesc *e, TValue *v) { } +/* +** Get the constant value from a constant expression +*/ +static TValue *const2val (FuncState *fs, const expdesc *e) { + lua_assert(e->k == VCONST); + return &fs->ls->dyd->actvar.arr[e->u.info].k; +} + + +/* +** If expression is a constant, fills 'v' with its value +** and returns 1. Otherwise, returns 0. +*/ +int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { + if (hasjumps(e)) + return 0; /* not a constant */ + switch (e->k) { + case VFALSE: + setbfvalue(v); + return 1; + case VTRUE: + setbtvalue(v); + return 1; + case VNIL: + setnilvalue(v); + return 1; + case VKSTR: { + setsvalue(fs->ls->L, v, e->u.strval); + return 1; + } + case VCONST: { + setobj(fs->ls->L, v, const2val(fs, e)); + return 1; + } + default: return tonumeral(e, v); + } +} + + +/* +** Return the previous instruction of the current code. If there +** may be a jump target between the current instruction and the +** previous one, return an invalid instruction (to avoid wrong +** optimizations). +*/ +static Instruction *previousinstruction (FuncState *fs) { + static const Instruction invalidinstruction = ~(Instruction)0; + if (fs->pc > fs->lasttarget) + return &fs->f->code[fs->pc - 1]; /* previous instruction */ + else + return cast(Instruction*, &invalidinstruction); +} + + /* ** Create a OP_LOADNIL instruction, but try to optimize: if the previous ** instruction is also OP_LOADNIL and ranges are compatible, adjust @@ -62,21 +128,18 @@ static int tonumeral(const expdesc *e, TValue *v) { ** 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->code[fs->pc-1]; - 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? */ - if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ - if (pl > l) l = pl; /* l = max(l, pl) */ - SETARG_A(*previous, from); - SETARG_B(*previous, l - from); - return; - } + Instruction *previous = previousinstruction(fs); + 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? */ + if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ + if (pl > l) l = pl; /* l = max(l, pl) */ + SETARG_A(*previous, from); + SETARG_B(*previous, l - from); + return; } /* else go through */ } luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ @@ -88,7 +151,7 @@ void luaK_nil (FuncState *fs, int from, int n) { ** a list of jumps. */ static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); + int offset = GETARG_sJ(fs->f->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -104,9 +167,10 @@ static void fixjump (FuncState *fs, int pc, int dest) { Instruction *jmp = &fs->f->code[pc]; int offset = dest - (pc + 1); lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) + if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ)) luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); + lua_assert(GET_OPCODE(*jmp) == OP_JMP); + SETARG_sJ(*jmp, offset); } @@ -129,17 +193,10 @@ void luaK_concat (FuncState *fs, int *l1, int 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. +** can be fixed later (with 'fixjump'). */ int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - 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; + return codesJ(fs, OP_JMP, NO_JUMP, 0); } @@ -147,7 +204,13 @@ int luaK_jump (FuncState *fs) { ** Code a 'return' instruction */ void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); + OpCode op; + switch (nret) { + case 0: op = OP_RETURN0; break; + case 1: op = OP_RETURN1; break; + default: op = OP_RETURN; break; + } + luaK_codeABC(fs, op, first, nret + 1, 0); } @@ -155,8 +218,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { ** 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); +static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { + luaK_codeABCk(fs, op, A, B, C, k); return luaK_jump(fs); } @@ -201,7 +264,7 @@ static int patchtestreg (FuncState *fs, int node, int reg) { 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)); + *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i)); } return 1; } @@ -235,73 +298,103 @@ 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 +** 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.) */ -static void dischargejpc (FuncState *fs) { - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); - fs->jpc = NO_JUMP; +void luaK_patchlist (FuncState *fs, int list, int target) { + lua_assert(target <= fs->pc); + patchlistaux(fs, list, target, NO_REG, target); } -/* -** 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); + int hr = luaK_getlabel(fs); /* mark "here" as a jump target */ + luaK_patchlist(fs, list, hr); } /* -** 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.) +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. */ -void luaK_patchlist (FuncState *fs, int list, int target) { - 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); +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 120 +#endif + + +/* limit for difference between lines in relative line info. */ +#define LIMLINEDIFF 0x80 + + +/* +** Save line info for a new instruction. If difference from last line +** does not fit in a byte, of after that many instructions, save a new +** absolute line info; (in that case, the special value 'ABSLINEINFO' +** in 'lineinfo' signals the existence of this absolute information.) +** Otherwise, store the difference from last line in 'lineinfo'. +*/ +static void savelineinfo (FuncState *fs, Proto *f, int line) { + int linedif = line - fs->previousline; + int pc = fs->pc - 1; /* last instruction coded */ + if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) { + luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, + f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); + f->abslineinfo[fs->nabslineinfo].pc = pc; + f->abslineinfo[fs->nabslineinfo++].line = line; + linedif = ABSLINEINFO; /* signal that there is absolute information */ + fs->iwthabs = 0; /* restart counter */ } + luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, + MAX_INT, "opcodes"); + f->lineinfo[pc] = linedif; + fs->previousline = line; /* last line saved */ } /* -** 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.) +** Remove line information from the last instruction. +** If line information for that instruction is absolute, set 'iwthabs' +** above its max to force the new (replacing) instruction to have +** absolute line info, too. */ -void luaK_patchclose (FuncState *fs, int list, int level) { - level++; /* argument is +1 to reserve 0 as non-op */ - for (; list != NO_JUMP; list = 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); +static void removelastlineinfo (FuncState *fs) { + Proto *f = fs->f; + int pc = fs->pc - 1; /* last instruction coded */ + if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ + fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ + fs->iwthabs--; /* undo previous increment */ + } + else { /* absolute line information */ + lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); + fs->nabslineinfo--; /* remove it */ + fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ } } +/* +** Remove the last instruction created, correcting line information +** accordingly. +*/ +static void removelastinstruction (FuncState *fs) { + removelastlineinfo(fs); + fs->pc--; +} + + /* ** Emit instruction 'i', checking for array sizes and saving also its ** line information. Return 'i' position. */ -static int luaK_code (FuncState *fs, Instruction i) { +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, MAX_INT, "opcodes"); - f->code[fs->pc] = i; - /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, - MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; - return fs->pc++; + f->code[fs->pc++] = i; + savelineinfo(fs, f, fs->ls->lastline); + return fs->pc - 1; /* index of new instruction */ } @@ -309,12 +402,11 @@ 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) { +int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { lua_assert(getOpMode(o) == iABC); - lua_assert(getBMode(o) != OpArgN || b == 0); - lua_assert(getCMode(o) != OpArgN || c == 0); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); - return luaK_code(fs, CREATE_ABC(o, a, b, c)); + lua_assert(a <= MAXARG_A && b <= MAXARG_B && + c <= MAXARG_C && (k & ~1) == 0); + return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); } @@ -322,13 +414,34 @@ 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); + lua_assert(getOpMode(o) == iABx); lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, a, bc)); } +/* +** Format and emit an 'iAsBx' instruction. +*/ +int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { + unsigned int b = bc + OFFSET_sBx; + lua_assert(getOpMode(o) == iAsBx); + lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, a, b)); +} + + +/* +** Format and emit an 'isJ' instruction. +*/ +static int codesJ (FuncState *fs, OpCode o, int sj, int k) { + unsigned int j = sj + OFFSET_sJ; + lua_assert(getOpMode(o) == isJ); + lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); + return luaK_code(fs, CREATE_sJ(o, j, k)); +} + + /* ** Emit an "extra argument" instruction (format 'iAx') */ @@ -343,7 +456,7 @@ static int codeextraarg (FuncState *fs, int a) { ** (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) { +static int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); else { @@ -384,13 +497,28 @@ void luaK_reserveregs (FuncState *fs, int n) { ) */ static void freereg (FuncState *fs, int reg) { - if (!ISK(reg) && reg >= fs->nactvar) { + if (reg >= luaY_nvarstack(fs)) { fs->freereg--; lua_assert(reg == fs->freereg); } } +/* +** Free two registers in proper order +*/ +static void freeregs (FuncState *fs, int r1, int r2) { + if (r1 > r2) { + freereg(fs, r1); + freereg(fs, r2); + } + else { + freereg(fs, r2); + freereg(fs, r1); + } +} + + /* ** Free register used by expression 'e' (if any) */ @@ -407,14 +535,7 @@ static void freeexp (FuncState *fs, expdesc *e) { 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); - } + freeregs(fs, r1, r2); } @@ -433,7 +554,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { 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) && + if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) return k; /* reuse index */ } @@ -455,7 +576,7 @@ 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) { +static int stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); return addk(fs, &o, &o); /* use string itself as key */ @@ -468,9 +589,9 @@ int luaK_stringK (FuncState *fs, TString *s) { ** same value; conversion to 'void*' is used only for hashing, so there ** are no "precision" problems. */ -int luaK_intK (FuncState *fs, lua_Integer n) { +static int luaK_intK (FuncState *fs, lua_Integer n) { TValue k, o; - setpvalue(&k, cast(void*, cast(size_t, n))); + setpvalue(&k, cast_voidp(cast_sizet(n))); setivalue(&o, n); return addk(fs, &k, &o); } @@ -486,11 +607,21 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { /* -** Add a boolean to list of constants and return its index. +** Add a false to list of constants and return its index. +*/ +static int boolF (FuncState *fs) { + TValue o; + setbfvalue(&o); + return addk(fs, &o, &o); /* use boolean itself as key */ +} + + +/* +** Add a true to list of constants and return its index. */ -static int boolK (FuncState *fs, int b) { +static int boolT (FuncState *fs) { TValue o; - setbvalue(&o, b); + setbtvalue(&o); return addk(fs, &o, &o); /* use boolean itself as key */ } @@ -507,22 +638,93 @@ static int nilK (FuncState *fs) { } +/* +** Check whether 'i' can be stored in an 'sC' operand. Equivalent to +** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of +** overflows in the hidden addition inside 'int2sC'. +*/ +static int fitsC (lua_Integer i) { + return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); +} + + +/* +** Check whether 'i' can be stored in an 'sBx' operand. +*/ +static int fitsBx (lua_Integer i) { + return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); +} + + +void luaK_int (FuncState *fs, int reg, lua_Integer i) { + if (fitsBx(i)) + luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + else + luaK_codek(fs, reg, luaK_intK(fs, i)); +} + + +static void luaK_float (FuncState *fs, int reg, lua_Number f) { + lua_Integer fi; + if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) + luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); + else + luaK_codek(fs, reg, luaK_numberK(fs, f)); +} + + +/* +** Convert a constant in 'v' into an expression description 'e' +*/ +static void const2exp (TValue *v, expdesc *e) { + switch (ttypetag(v)) { + case LUA_VNUMINT: + e->k = VKINT; e->u.ival = ivalue(v); + break; + case LUA_VNUMFLT: + e->k = VKFLT; e->u.nval = fltvalue(v); + break; + case LUA_VFALSE: + e->k = VFALSE; + break; + case LUA_VTRUE: + e->k = VTRUE; + break; + case LUA_VNIL: + e->k = VNIL; + break; + case LUA_VSHRSTR: case LUA_VLNGSTR: + e->k = VKSTR; e->u.strval = tsvalue(v); + break; + default: lua_assert(0); + } +} + + /* ** 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). +** 'e' must be a multi-ret expression (function call or vararg). */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getinstruction(fs, e), nresults + 1); - } - else if (e->k == VVARARG) { - Instruction *pc = &getinstruction(fs, e); - SETARG_B(*pc, nresults + 1); + Instruction *pc = &getinstruction(fs, e); + if (e->k == VCALL) /* expression is an open function call? */ + SETARG_C(*pc, nresults + 1); + else { + lua_assert(e->k == VVARARG); + SETARG_C(*pc, nresults + 1); SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } - else lua_assert(nresults == LUA_MULTRET); +} + + +/* +** Convert a VKSTR to a VK +*/ +static void str2K (FuncState *fs, expdesc *e) { + lua_assert(e->k == VKSTR); + e->u.info = stringK(fs, e->u.strval); + e->k = VK; } @@ -532,7 +734,7 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { ** 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). +** become VRELOC (as OP_VARARG puts its results where it wants). ** (Calls are created returning one result, so that does not need ** to be fixed.) */ @@ -544,39 +746,53 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { e->u.info = GETARG_A(getinstruction(fs, e)); } else if (e->k == VVARARG) { - SETARG_B(getinstruction(fs, e), 2); - e->k = VRELOCABLE; /* can relocate its simple result */ + SETARG_C(getinstruction(fs, e), 2); + e->k = VRELOC; /* can relocate its simple result */ } } /* -** Ensure that expression 'e' is not a variable. +** Ensure that expression 'e' is not a variable (nor a constant). +** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { + case VCONST: { + const2exp(const2val(fs, e), e); + break; + } case VLOCAL: { /* already in a register */ + e->u.info = e->u.var.sidx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); - e->k = VRELOCABLE; + e->k = VRELOC; + break; + } + case VINDEXUP: { + e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; + break; + } + case VINDEXI: { + freereg(fs, e->u.ind.t); + e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; + break; + } + case VINDEXSTR: { + freereg(fs, e->u.ind.t); + e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; break; } case VINDEXED: { - OpCode op; - freereg(fs, e->u.ind.idx); - 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; + freeregs(fs, e->u.ind.t, e->u.ind.idx); + e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; break; } case VVARARG: case VCALL: { @@ -591,6 +807,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { /* ** Ensures expression value is in register 'reg' (and therefore ** 'e' will become a non-relocatable expression). +** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); @@ -599,23 +816,30 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_nil(fs, reg, 1); break; } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); + case VFALSE: { + luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); break; } + case VTRUE: { + luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); + break; + } + case VKSTR: { + str2K(fs, e); + } /* FALLTHROUGH */ case VK: { luaK_codek(fs, reg, e->u.info); break; } case VKFLT: { - luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); + luaK_float(fs, reg, e->u.nval); break; } case VKINT: { - luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + luaK_int(fs, reg, e->u.ival); break; } - case VRELOCABLE: { + case VRELOC: { Instruction *pc = &getinstruction(fs, e); SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ break; @@ -637,6 +861,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { /* ** Ensures expression value is in any register. +** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { if (e->k != VNONRELOC) { /* no fixed register yet? */ @@ -646,9 +871,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { } -static int code_loadbool (FuncState *fs, int A, int b, int jump) { +static int code_loadbool (FuncState *fs, int A, OpCode op) { luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); + return luaK_codeABC(fs, op, A, 0, 0); } @@ -666,8 +891,8 @@ static int need_value (FuncState *fs, int list) { /* -** Ensures final expression result (including results from its jump -** lists) is in register 'reg'. +** Ensures final expression result (which includes 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). @@ -682,8 +907,9 @@ 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_loadbool(fs, reg, 0, 1); - p_t = code_loadbool(fs, reg, 1, 0); + p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ + p_t = code_loadbool(fs, reg, OP_LOADTRUE); + /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); @@ -697,8 +923,7 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { /* -** Ensures final expression result (including results from its jump -** lists) is in next available register. +** Ensures final expression result is in next available register. */ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); @@ -709,15 +934,15 @@ 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. +** Ensures final expression result is in some (any) register +** and return that register. */ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); 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? */ + if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } @@ -728,8 +953,8 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { /* -** Ensures final expression result is either in a register or in an -** upvalue. +** 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)) @@ -738,8 +963,8 @@ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { /* -** Ensures final expression result is either in a register or it is -** a constant. +** Ensures final expression result is either in a register +** or it is a constant. */ void luaK_exp2val (FuncState *fs, expdesc *e) { if (hasjumps(e)) @@ -749,30 +974,54 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { } +/* +** Try to make 'e' a K expression with an index in the range of R/K +** indices. Return true iff succeeded. +*/ +static int luaK_exp2K (FuncState *fs, expdesc *e) { + if (!hasjumps(e)) { + int info; + switch (e->k) { /* move constants to 'k' */ + case VTRUE: info = boolT(fs); break; + case VFALSE: info = boolF(fs); break; + case VNIL: info = nilK(fs); break; + case VKINT: info = luaK_intK(fs, e->u.ival); break; + case VKFLT: info = luaK_numberK(fs, e->u.nval); break; + case VKSTR: info = stringK(fs, e->u.strval); break; + case VK: info = e->u.info; break; + default: return 0; /* not a constant */ + } + if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */ + e->k = VK; /* make expression a 'K' expression */ + e->u.info = info; + return 1; + } + } + /* else, expression doesn't fit; leave it unchanged */ + return 0; +} + + /* ** 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. +** Returns 1 iff expression is K. */ int luaK_exp2RK (FuncState *fs, expdesc *e) { - luaK_exp2val(fs, e); - 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; + if (luaK_exp2K(fs, e)) + return 1; + else { /* not a constant in the right range: put it in a register */ + luaK_exp2anyreg(fs, e); + return 0; } - /* not a constant in the right range: put it in a register */ - return luaK_exp2anyreg(fs, e); +} + + +static void codeABRK (FuncState *fs, OpCode o, int a, int b, + expdesc *ec) { + int k = luaK_exp2RK(fs, ec); + luaK_codeABCk(fs, o, a, b, ec->u.info, k); } @@ -783,7 +1032,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -791,10 +1040,20 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); break; } + case VINDEXUP: { + codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex); + break; + } + case VINDEXI: { + codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex); + break; + } + case VINDEXSTR: { + codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); + break; + } case VINDEXED: { - OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); + codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); break; } default: lua_assert(0); /* invalid var kind to store */ @@ -814,7 +1073,7 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { e->u.info = fs->freereg; /* base register for op_self */ 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)); + codeABRK(fs, OP_SELF, e->u.info, ereg, key); freeexp(fs, key); } @@ -826,7 +1085,7 @@ 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); - SETARG_A(*pc, !(GETARG_A(*pc))); + SETARG_k(*pc, (GETARG_k(*pc) ^ 1)); } @@ -837,17 +1096,17 @@ static void negatecondition (FuncState *fs, expdesc *e) { ** and removing the 'not'. */ static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOCABLE) { + if (e->k == VRELOC) { 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); + removelastinstruction(fs); /* remove previous OP_NOT */ + return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); + return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); } @@ -863,7 +1122,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { pc = e->u.info; /* save jump position */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } @@ -908,13 +1167,12 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { ** 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; /* true == not nil == not false */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } @@ -922,12 +1180,12 @@ static void codenot (FuncState *fs, expdesc *e) { negatecondition(fs, e); break; } - case VRELOCABLE: + case VRELOC: case VNONRELOC: { discharge2anyreg(fs, e); freeexp(fs, e); e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); - e->k = VRELOCABLE; + e->k = VRELOC; break; } default: lua_assert(0); /* cannot happen */ @@ -939,16 +1197,95 @@ static void codenot (FuncState *fs, expdesc *e) { } +/* +** Check whether expression 'e' is a small literal string +*/ +static int isKstr (FuncState *fs, expdesc *e) { + return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && + ttisshrstring(&fs->f->k[e->u.info])); +} + +/* +** Check whether expression 'e' is a literal integer. +*/ +int luaK_isKint (expdesc *e) { + return (e->k == VKINT && !hasjumps(e)); +} + + +/* +** Check whether expression 'e' is a literal integer in +** proper range to fit in register C +*/ +static int isCint (expdesc *e) { + return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); +} + + +/* +** Check whether expression 'e' is a literal integer in +** proper range to fit in register sC +*/ +static int isSCint (expdesc *e) { + return luaK_isKint(e) && fitsC(e->u.ival); +} + + +/* +** Check whether expression 'e' is a literal integer or float in +** proper range to fit in a register (sB or sC). +*/ +static int isSCnumber (expdesc *e, int *pi, int *isfloat) { + lua_Integer i; + if (e->k == VKINT) + i = e->u.ival; + else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) + *isfloat = 1; + else + return 0; /* not a number */ + if (!hasjumps(e) && fitsC(i)) { + *pi = int2sC(cast_int(i)); + return 1; + } + else + return 0; +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a -** register or upvalue. +** register or upvalue. Upvalues can only be indexed by literal strings. +** Keys can be literal strings in the constant table or arbitrary +** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - 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; + if (k->k == VKSTR) + str2K(fs, k); + lua_assert(!hasjumps(t) && + (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); + if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ + luaK_exp2anyreg(fs, t); /* put it in a register */ + if (t->k == VUPVAL) { + t->u.ind.t = t->u.info; /* upvalue index */ + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXUP; + } + else { + /* register index of the table */ + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info; + if (isKstr(fs, k)) { + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXSTR; + } + else if (isCint(k)) { + t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ + t->k = VINDEXI; + } + else { + t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ + t->k = VINDEXED; + } + } } @@ -962,7 +1299,7 @@ static int validop (int op, TValue *v1, TValue *v2) { 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)); + return (tointegerns(v1, &i) && tointegerns(v2, &i)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); @@ -976,11 +1313,11 @@ static int validop (int op, TValue *v1, TValue *v2) { ** (In this case, 'e1' has the final result.) */ static int constfolding (FuncState *fs, int op, expdesc *e1, - const expdesc *e2) { + 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 */ - luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ + luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ if (ttisinteger(&res)) { e1->k = VKINT; e1->u.ival = ivalue(&res); @@ -1005,7 +1342,7 @@ 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 */ + e->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); } @@ -1015,61 +1352,212 @@ 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) { - int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ - int rk1 = luaK_exp2RK(fs, e1); +static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, + OpCode op, int v2, int flip, int line, + OpCode mmop, TMS event) { + int v1 = luaK_exp2anyreg(fs, e1); + int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ + e1->u.info = pc; + e1->k = VRELOC; /* all those operations are relocatable */ + luaK_fixline(fs, line); + luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ luaK_fixline(fs, line); } /* -** Emit code for comparisons. -** 'e1' was already put in R/K form by 'luaK_infix'. +** Emit code for binary expressions that "produce values" over +** two registers. */ -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; +static void codebinexpval (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int line) { + int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ + lua_assert(OP_ADD <= op && op <= OP_SHR); + finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, + cast(TMS, (op - OP_ADD) + TM_ADD)); +} + + +/* +** Code binary operators with immediate operands. +*/ +static void codebini (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int flip, int line, + TMS event) { + int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ + lua_assert(e2->k == VKINT); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); +} + + +/* Try to code a binary operator negating its second operand. +** For the metamethod, 2nd operand must keep its original value. +*/ +static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, + OpCode op, int line, TMS event) { + if (!luaK_isKint(e2)) + return 0; /* not an integer constant */ + else { + lua_Integer i2 = e2->u.ival; + if (!(fitsC(i2) && fitsC(-i2))) + return 0; /* not in the proper range */ + else { /* operating a small integer constant */ + int v2 = cast_int(i2); + finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); + /* correct metamethod argument */ + SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); + return 1; /* successfully coded */ } } +} + + +static void swapexps (expdesc *e1, expdesc *e2) { + expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ +} + + +/* +** Code arithmetic operators ('+', '-', ...). If second operand is a +** constant in the proper range, use variant opcodes with K operands. +*/ +static void codearith (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + TMS event = cast(TMS, opr + TM_ADD); + if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ + int v2 = e2->u.info; /* K index */ + OpCode op = cast(OpCode, opr + OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); + } + else { /* 'e2' is neither an immediate nor a K operand */ + OpCode op = cast(OpCode, opr + OP_ADD); + if (flip) + swapexps(e1, e2); /* back to original order */ + codebinexpval(fs, op, e1, e2, line); /* use standard operators */ + } +} + + +/* +** Code commutative operators ('+', '*'). If first operand is a +** numeric constant, change order of operands to try to use an +** immediate or K operator. +*/ +static void codecommutative (FuncState *fs, BinOpr op, + expdesc *e1, expdesc *e2, int line) { + int flip = 0; + if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ + swapexps(e1, e2); /* change order */ + flip = 1; + } + if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ + codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); + else + codearith(fs, op, e1, e2, flip, line); +} + + +/* +** Code bitwise operations; they are all associative, so the function +** tries to put an integer constant as the 2nd operand (a K operand). +*/ +static void codebitwise (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int line) { + int flip = 0; + int v2; + OpCode op; + if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + swapexps(e1, e2); /* 'e2' will be the constant operand */ + flip = 1; + } + else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ + op = cast(OpCode, opr + OP_ADD); + codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ + return; + } + v2 = e2->u.info; /* index in K array */ + op = cast(OpCode, opr + OP_ADDK); + lua_assert(ttisinteger(&fs->f->k[v2])); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, + cast(TMS, opr + TM_ADD)); +} + + +/* +** Emit code for order comparisons. When using an immediate operand, +** 'isfloat' tells whether the original value was a float. +*/ +static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { + int r1, r2; + int im; + int isfloat = 0; + if (isSCnumber(e2, &im, &isfloat)) { + /* use immediate operand */ + r1 = luaK_exp2anyreg(fs, e1); + r2 = im; + op = cast(OpCode, (op - OP_LT) + OP_LTI); + } + else if (isSCnumber(e1, &im, &isfloat)) { + /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ + r1 = luaK_exp2anyreg(fs, e2); + r2 = im; + op = (op == OP_LT) ? OP_GTI : OP_GEI; + } + else { /* regular case, compare two registers */ + r1 = luaK_exp2anyreg(fs, e1); + r2 = luaK_exp2anyreg(fs, e2); + } + freeexps(fs, e1, e2); + e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); e1->k = VJMP; } /* -** Aplly prefix operation 'op' to expression 'e'. +** Emit code for equality comparisons ('==', '~='). +** 'e1' was already put as RK by 'luaK_infix'. +*/ +static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { + int r1, r2; + int im; + int isfloat = 0; /* not needed here, but kept for symmetry */ + OpCode op; + if (e1->k != VNONRELOC) { + lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); + swapexps(e1, e2); + } + r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ + if (isSCnumber(e2, &im, &isfloat)) { + op = OP_EQI; + r2 = im; /* immediate operand */ + } + else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ + op = OP_EQK; + r2 = e2->u.info; /* constant index */ + } + else { + op = OP_EQ; /* will compare two registers */ + r2 = luaK_exp2anyreg(fs, e2); + } + freeexps(fs, e1, e2); + e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); + e1->k = VJMP; +} + + +/* +** Apply prefix operation 'op' to expression 'e'. */ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; + luaK_dischargevars(fs, e); switch (op) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ if (constfolding(fs, op + LUA_OPUNM, e, &ef)) break; - /* FALLTHROUGH */ + /* else */ /* FALLTHROUGH */ case OPR_LEN: codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); break; @@ -1084,6 +1572,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { ** 2nd operand. */ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { + luaK_dischargevars(fs, v); switch (op) { case OPR_AND: { luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ @@ -1094,7 +1583,7 @@ 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: @@ -1103,67 +1592,126 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); + luaK_exp2anyreg(fs, v); /* else keep numeral, which may be folded with 2nd operand */ break; } - default: { - luaK_exp2RK(fs, v); + case OPR_EQ: case OPR_NE: { + if (!tonumeral(v, NULL)) + luaK_exp2RK(fs, v); + /* else keep numeral, which may be an immediate operand */ + break; + } + case OPR_LT: case OPR_LE: + case OPR_GT: case OPR_GE: { + int dummy, dummy2; + if (!isSCnumber(v, &dummy, &dummy2)) + luaK_exp2anyreg(fs, v); + /* else keep numeral, which may be an immediate operand */ break; } + default: lua_assert(0); + } +} + +/* +** Create code for '(e1 .. e2)'. +** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))', +** because concatenation is right associative), merge both CONCATs. +*/ +static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { + Instruction *ie2 = previousinstruction(fs); + if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */ + int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */ + lua_assert(e1->u.info + 1 == GETARG_A(*ie2)); + freeexp(fs, e2); + SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */ + SETARG_B(*ie2, n + 1); /* will concatenate one more element */ + } + else { /* 'e2' is not a concatenation */ + luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */ + freeexp(fs, e2); + luaK_fixline(fs, line); } } /* ** 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, +void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - switch (op) { + luaK_dischargevars(fs, e2); + if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2)) + return; /* done by folding */ + switch (opr) { case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; break; } - case OPR_CONCAT: { - luaK_exp2val(fs, e2); - 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(getinstruction(fs, e2), e1->u.info); - e1->k = VRELOCABLE; e1->u.info = e2->u.info; + case OPR_CONCAT: { /* e1 .. e2 */ + luaK_exp2nextreg(fs, e2); + codeconcat(fs, e1, e2, line); + break; + } + case OPR_ADD: case OPR_MUL: { + codecommutative(fs, opr, e1, e2, line); + break; + } + case OPR_SUB: { + if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) + break; /* coded as (r1 + -I) */ + /* ELSE */ + } /* FALLTHROUGH */ + case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { + codearith(fs, opr, e1, e2, 0, line); + break; + } + case OPR_BAND: case OPR_BOR: case OPR_BXOR: { + codebitwise(fs, opr, e1, e2, line); + break; + } + case OPR_SHL: { + if (isSCint(e1)) { + swapexps(e1, e2); + codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ } - else { - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codebinexpval(fs, OP_CONCAT, e1, e2, line); + else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { + /* coded as (r1 >> -I) */; } + else /* regular case (two registers) */ + codebinexpval(fs, OP_SHL, e1, e2, line); break; } - 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 (!constfolding(fs, op + LUA_OPADD, e1, e2)) - codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); + case OPR_SHR: { + if (isSCint(e2)) + codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ + else /* regular case (two registers) */ + codebinexpval(fs, OP_SHR, e1, e2, line); break; } - case OPR_EQ: case OPR_LT: case OPR_LE: - case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, op, e1, e2); + case OPR_EQ: case OPR_NE: { + codeeq(fs, opr, e1, e2); + break; + } + case OPR_LT: case OPR_LE: { + OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); + codeorder(fs, op, e1, e2); + break; + } + case OPR_GT: case OPR_GE: { + /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ + OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); + swapexps(e1, e2); + codeorder(fs, op, e1, e2); break; } default: lua_assert(0); @@ -1172,10 +1720,23 @@ void luaK_posfix (FuncState *fs, BinOpr op, /* -** Change line information associated with current position. +** Change line information associated with current position, by removing +** previous info and adding it again with new line. */ void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; + removelastlineinfo(fs); + savelineinfo(fs, fs->f, line); +} + + +void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { + Instruction *inst = &fs->f->code[pc]; + int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ + int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ + int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ + int k = (extra > 0); /* true iff needs extra argument */ + *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); + *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); } @@ -1187,17 +1748,67 @@ void luaK_fixline (FuncState *fs, int line) { ** 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 && tostore <= LFIELDS_PER_FLUSH); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else if (c <= MAXARG_Ax) { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - codeextraarg(fs, c); + if (tostore == LUA_MULTRET) + tostore = 0; + if (nelems <= MAXARG_C) + luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); + else { + int extra = nelems / (MAXARG_C + 1); + nelems %= (MAXARG_C + 1); + luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); + codeextraarg(fs, extra); } - else - luaX_syntaxerror(fs->ls, "constructor too long"); fs->freereg = base + 1; /* free registers with list values */ } + +/* +** return the final target of a jump (skipping jumps to jumps) +*/ +static int finaltarget (Instruction *code, int i) { + int count; + for (count = 0; count < 100; count++) { /* avoid infinite loops */ + Instruction pc = code[i]; + if (GET_OPCODE(pc) != OP_JMP) + break; + else + i += GETARG_sJ(pc) + 1; + } + return i; +} + + +/* +** Do a final pass over the code of a function, doing small peephole +** optimizations and adjustments. +*/ +void luaK_finish (FuncState *fs) { + int i; + Proto *p = fs->f; + for (i = 0; i < fs->pc; i++) { + Instruction *pc = &p->code[i]; + lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); + switch (GET_OPCODE(*pc)) { + case OP_RETURN0: case OP_RETURN1: { + if (!(fs->needclose || p->is_vararg)) + break; /* no extra work */ + /* else use OP_RETURN to do the extra work */ + SET_OPCODE(*pc, OP_RETURN); + } /* FALLTHROUGH */ + case OP_RETURN: case OP_TAILCALL: { + if (fs->needclose) + SETARG_k(*pc, 1); /* signal that it needs to close */ + if (p->is_vararg) + SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ + break; + } + case OP_JMP: { + int target = finaltarget(p->code, i); + fixjump(fs, i, target); + break; + } + default: break; + } + } +} diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 882dc9c15..326582445 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lcode.h $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -24,40 +24,53 @@ ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { + /* arithmetic operators */ OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, - OPR_DIV, - OPR_IDIV, + OPR_DIV, OPR_IDIV, + /* bitwise operators */ OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR, + /* string operator */ OPR_CONCAT, + /* comparison operators */ OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, + /* logical operators */ OPR_AND, OPR_OR, OPR_NOBINOPR } BinOpr; +/* true if operation is foldable (that is, it is arithmetic or bitwise) */ +#define foldbinop(op) ((op) <= OPR_SHR) + + +#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) + + typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; /* get (pointer to) instruction of given 'expdesc' */ #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) +LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); +LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); +LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, + int B, int C, int k); +LUAI_FUNC int luaK_isKint (expdesc *e); +LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 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_intK (FuncState *fs, lua_Integer n); +LUAI_FUNC void luaK_int (FuncState *fs, int reg, 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); @@ -75,14 +88,17 @@ LUAI_FUNC int luaK_jump (FuncState *fs); LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); +LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, + int ra, int asize, int hsize); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); +LUAI_FUNC void luaK_finish (FuncState *fs); +LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); #endif diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index 0b17af9e3..c165031d2 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lcorolib.c $ ** Coroutine Library ** See Copyright Notice in lua.h */ @@ -20,25 +20,24 @@ static lua_State *getco (lua_State *L) { lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "thread expected"); + luaL_argexpected(L, co, 1, "thread"); return co; } +/* +** Resumes a coroutine. Returns the number of results for non-error +** cases or -1 for errors. +*/ static int auxresume (lua_State *L, lua_State *co, int narg) { - int status; + int status, nres; if (!lua_checkstack(co, narg)) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } - if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { - lua_pushliteral(L, "cannot resume dead coroutine"); - return -1; /* error flag */ - } lua_xmove(L, co, narg); - status = lua_resume(co, L, narg); + status = lua_resume(co, L, narg, &nres); if (status == LUA_OK || status == LUA_YIELD) { - int nres = lua_gettop(co); if (!lua_checkstack(L, nres + 1)) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); @@ -74,9 +73,13 @@ static int luaB_coresume (lua_State *L) { 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_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ + if (r < 0) { /* error? */ + int stat = lua_status(co); + if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */ + lua_resetthread(co); /* close its tbc variables */ + if (stat != LUA_ERRMEM && /* not a memory error and ... */ + lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ + luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); } @@ -108,35 +111,48 @@ static int luaB_yield (lua_State *L) { } -static int luaB_costatus (lua_State *L) { - lua_State *co = getco(L); - if (L == co) lua_pushliteral(L, "running"); +#define COS_RUN 0 +#define COS_DEAD 1 +#define COS_YIELD 2 +#define COS_NORM 3 + + +static const char *const statname[] = + {"running", "dead", "suspended", "normal"}; + + +static int auxstatus (lua_State *L, lua_State *co) { + if (L == co) return COS_RUN; else { switch (lua_status(co)) { case LUA_YIELD: - lua_pushliteral(L, "suspended"); - break; + return COS_YIELD; case LUA_OK: { lua_Debug ar; - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ - lua_pushliteral(L, "normal"); /* it is running */ + if (lua_getstack(co, 0, &ar)) /* does it have frames? */ + return COS_NORM; /* it is running */ else if (lua_gettop(co) == 0) - lua_pushliteral(L, "dead"); + return COS_DEAD; else - lua_pushliteral(L, "suspended"); /* initial state */ - break; + return COS_YIELD; /* initial state */ } default: /* some error occurred */ - lua_pushliteral(L, "dead"); - break; + return COS_DEAD; } } +} + + +static int luaB_costatus (lua_State *L) { + lua_State *co = getco(L); + lua_pushstring(L, statname[auxstatus(L, co)]); return 1; } static int luaB_yieldable (lua_State *L) { - lua_pushboolean(L, lua_isyieldable(L)); + lua_State *co = lua_isnone(L, 1) ? L : getco(L); + lua_pushboolean(L, lua_isyieldable(co)); return 1; } @@ -148,6 +164,28 @@ static int luaB_corunning (lua_State *L) { } +static int luaB_close (lua_State *L) { + lua_State *co = getco(L); + int status = auxstatus(L, co); + switch (status) { + case COS_DEAD: case COS_YIELD: { + status = lua_resetthread(co); + if (status == LUA_OK) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushboolean(L, 0); + lua_xmove(co, L, 1); /* copy error message */ + return 2; + } + } + default: /* normal or running coroutine */ + return luaL_error(L, "cannot close a %s coroutine", statname[status]); + } +} + + static const luaL_Reg co_funcs[] = { {"create", luaB_cocreate}, {"resume", luaB_coresume}, @@ -156,6 +194,7 @@ static const luaL_Reg co_funcs[] = { {"wrap", luaB_cowrap}, {"yield", luaB_yield}, {"isyieldable", luaB_yieldable}, + {"close", luaB_close}, {NULL, NULL} }; diff --git a/3rd/lua/lctype.c b/3rd/lua/lctype.c index f8ad7a2ed..954228094 100644 --- a/3rd/lua/lctype.c +++ b/3rd/lua/lctype.c @@ -1,5 +1,5 @@ /* -** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lctype.c $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ @@ -16,6 +16,15 @@ #include + +#if defined (LUA_UCID) /* accept UniCode IDentifiers? */ +/* consider all non-ascii codepoints to be alphabetic */ +#define NONA 0x01 +#else +#define NONA 0x00 /* default */ +#endif + + LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x00, /* EOZ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ @@ -34,22 +43,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #endif /* } */ diff --git a/3rd/lua/lctype.h b/3rd/lua/lctype.h index b09b21a33..864e19018 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 $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ @@ -13,7 +13,7 @@ /* ** WARNING: the functions defined here do not necessarily correspond ** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua +** optimized for the specific needs of Lua. */ #if !defined(LUA_USE_CTYPE) @@ -61,14 +61,20 @@ #define lisprint(c) testprop(c, MASK(PRINTBIT)) #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) + /* -** this 'ltolower' only works for alphabetic characters +** In ASCII, this 'ltolower' is correct for alphabetic characters and +** for '.'. That is enough for Lua needs. ('check_exp' ensures that +** the character either is an upper-case letter or is unchanged by +** the transformation, which holds for lower-case letters and '.'.) */ -#define ltolower(c) ((c) | ('A' ^ 'a')) +#define ltolower(c) \ + check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ + (c) | ('A' ^ 'a')) -/* two more entries for 0 and -1 (EOZ) */ -LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; +/* one entry for each character and for -1 (EOZ) */ +LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) #else /* }{ */ diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 9d29afb0a..59eb8f0ea 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ldblib.c $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -21,10 +21,10 @@ /* -** The hook table at registry[&HOOKKEY] maps threads to their current -** hook function. (We only need the unique address of 'HOOKKEY'.) +** The hook table at registry[HOOKKEY] maps threads to their current +** hook function. */ -static const int HOOKKEY = 0; +static const char *const HOOKKEY = "_HOOKKEY"; /* @@ -55,8 +55,7 @@ static int db_getmetatable (lua_State *L) { static int db_setmetatable (lua_State *L) { int t = lua_type(L, 2); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); + luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); lua_settop(L, 2); lua_setmetatable(L, 1); return 1; /* return 1st argument */ @@ -64,19 +63,24 @@ static int db_setmetatable (lua_State *L) { static int db_getuservalue (lua_State *L) { + int n = (int)luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) - lua_pushnil(L); - else - lua_getuservalue(L, 1); + luaL_pushfail(L); + else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { + lua_pushboolean(L, 1); + return 2; + } return 1; } static int db_setuservalue (lua_State *L) { + int n = (int)luaL_optinteger(L, 3, 1); luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checkany(L, 2); lua_settop(L, 2); - lua_setuservalue(L, 1); + if (!lua_setiuservalue(L, 1, n)) + luaL_pushfail(L); return 1; } @@ -146,7 +150,7 @@ 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"); + const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ @@ -155,7 +159,7 @@ static int db_getinfo (lua_State *L) { } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { - lua_pushnil(L); /* level out of range */ + luaL_pushfail(L); /* level out of range */ return 1; } } @@ -163,7 +167,8 @@ static int db_getinfo (lua_State *L) { return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { - settabss(L, "source", ar.source); + lua_pushlstring(L, ar.source, ar.srclen); + lua_setfield(L, -2, "source"); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); @@ -180,6 +185,10 @@ static int db_getinfo (lua_State *L) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } + if (strchr(options, 'r')) { + settabsi(L, "ftransfer", ar.ftransfer); + settabsi(L, "ntransfer", ar.ntransfer); + } if (strchr(options, 't')) settabsb(L, "istailcall", ar.istailcall); if (strchr(options, 'L')) @@ -193,8 +202,6 @@ static int db_getinfo (lua_State *L) { static int db_getlocal (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; 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 */ @@ -202,6 +209,8 @@ static int db_getlocal (lua_State *L) { return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ + lua_Debug ar; + const char *name; 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"); @@ -214,7 +223,7 @@ static int db_getlocal (lua_State *L) { return 2; } else { - lua_pushnil(L); /* no name (nor value) */ + luaL_pushfail(L); /* no name (nor value) */ return 1; } } @@ -305,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) { 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_getfield(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 */ @@ -358,14 +367,12 @@ static int db_sethook (lua_State *L) { 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 */ + if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { + /* table just created; initialize it */ lua_pushstring(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ } checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ @@ -382,12 +389,14 @@ static int db_gethook (lua_State *L) { char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); - if (hook == NULL) /* no hook? */ - lua_pushnil(L); + if (hook == NULL) { /* no hook? */ + luaL_pushfail(L); + return 1; + } else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); else { /* hook table must exist */ - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); lua_rawget(L, -2); /* 1st result = hooktable[L1] */ @@ -408,7 +417,7 @@ static int db_debug (lua_State *L) { return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) - lua_writestringerror("%s\n", lua_tostring(L, -1)); + lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); lua_settop(L, 0); /* remove eventual returns */ } } @@ -428,6 +437,17 @@ static int db_traceback (lua_State *L) { } +static int db_setcstacklimit (lua_State *L) { + int limit = (int)luaL_checkinteger(L, 1); + int res = lua_setcstacklimit(L, limit); + if (res == 0) + lua_pushboolean(L, 0); + else + lua_pushinteger(L, res); + return 1; +} + + static const luaL_Reg dblib[] = { {"debug", db_debug}, {"getuservalue", db_getuservalue}, @@ -445,6 +465,7 @@ static const luaL_Reg dblib[] = { {"setmetatable", db_setmetatable}, {"setupvalue", db_setupvalue}, {"traceback", db_traceback}, + {"setcstacklimit", db_setcstacklimit}, {NULL, NULL} }; diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index e1389296e..8cb00e51a 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.121.1.2 2017/07/10 17:21:50 roberto Exp $ +** $Id: ldebug.c $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -31,12 +31,10 @@ -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) - - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) +#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) +/* inverse of 'pcRel' */ +#define invpcRel(pc, p) ((p)->code + (pc) + 1) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); @@ -48,47 +46,103 @@ static int currentpc (CallInfo *ci) { } -static int currentline (CallInfo *ci) { - return getfuncline(ci_func(ci)->p, currentpc(ci)); +/* +** Get a "base line" to find the line corresponding to an instruction. +** For that, search the array of absolute line info for the largest saved +** instruction smaller or equal to the wanted instruction. A special +** case is when there is no absolute info or the instruction is before +** the first absolute one. +*/ +static int getbaseline (const Proto *f, int pc, int *basepc) { + if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { + *basepc = -1; /* start from the beginning */ + return f->linedefined; + } + else { + unsigned int i; + if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) + i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ + else { /* binary search */ + unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ + i = 0; /* abslineinfo[i] <= pc */ + while (i < j - 1) { + unsigned int m = (j + i) / 2; + if (pc >= f->abslineinfo[m].pc) + i = m; + else + j = m; + } + } + *basepc = f->abslineinfo[i].pc; + return f->abslineinfo[i].line; + } } /* -** 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. +** Get the line corresponding to instruction 'pc' in function 'f'; +** first gets a base line and from there does the increments until +** the desired instruction. */ -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); +int luaG_getfuncline (const Proto *f, int pc) { + if (f->lineinfo == NULL) /* no debug information? */ + return -1; + else { + int basepc; + int baseline = getbaseline(f, pc, &basepc); + while (basepc++ < pc) { /* walk until given instruction */ + lua_assert(f->lineinfo[basepc] != ABSLINEINFO); + baseline += f->lineinfo[basepc]; /* correct line */ + } + return baseline; } } +static int getcurrentline (CallInfo *ci) { + return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); +} + + +/* +** Set 'trap' for all active Lua frames. +** This function can be called during a signal, under "reasonable" +** assumptions. A new 'ci' is completely linked in the list before it +** becomes part of the "active" list, and we assume that pointers are +** atomic; see comment in next function. +** (A compiler doing interprocedural optimizations could, theoretically, +** reorder memory writes in such a way that the list could be +** temporarily broken while inserting a new element. We simply assume it +** has no good reasons to do that.) +*/ +static void settraps (CallInfo *ci) { + for (; ci != NULL; ci = ci->previous) + if (isLua(ci)) + ci->u.l.trap = 1; +} + + /* -** 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'). +** This function can be called during a signal, under "reasonable" +** assumptions. +** Fields '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? */ mask = 0; func = NULL; } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); + if (mask) + settraps(L->ci); /* to trace inside 'luaV_execute' */ } @@ -124,7 +178,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { } -static const char *upvalname (Proto *p, int uv) { +static const char *upvalname (const Proto *p, int uv) { TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); @@ -132,38 +186,37 @@ static const char *upvalname (Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - 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; - return "(*vararg)"; /* generic name for any vararg */ + if (clLvalue(s2v(ci->func))->p->is_vararg) { + int nextra = ci->u.l.nextraargs; + if (n >= -nextra) { /* 'n' is negative */ + *pos = ci->func - nextra - (n + 1); + return "(vararg)"; /* generic name for any vararg */ + } } + return NULL; /* no such vararg */ } -static const char *findlocal (lua_State *L, CallInfo *ci, int n, - StkId *pos) { +const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { + StkId base = ci->func + 1; const char *name = NULL; - StkId base; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); - else { - base = ci->u.l.base; + return findvararg(ci, n, pos); + else name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } } - else - base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ 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 */ + if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ + /* generic name for any valid slot */ + name = isLua(ci) ? "(temporary)" : "(C temporary)"; + } else return NULL; /* no name */ } - *pos = base + (n - 1); + if (pos) + *pos = base + (n - 1); return name; } @@ -171,22 +224,20 @@ 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? */ + if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); + name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar->i_ci, n, &pos); + name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobj2s(L, L->top, pos); + setobjs2s(L, L->top, pos); api_incr_top(L); } } - swapextra(L); lua_unlock(L); return name; } @@ -196,13 +247,11 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - swapextra(L); - name = findlocal(L, ar->i_ci, n, &pos); + name = luaG_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; } @@ -211,36 +260,55 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { static void funcinfo (lua_Debug *ar, Closure *cl) { if (noLuaClosure(cl)) { ar->source = "=[C]"; + ar->srclen = LL("=[C]"); ar->linedefined = -1; ar->lastlinedefined = -1; ar->what = "C"; } else { - Proto *p = cl->l.p; - ar->source = p->source ? getstr(p->source) : "=?"; + const Proto *p = cl->l.p; + if (p->source) { + ar->source = getstr(p->source); + ar->srclen = tsslen(p->source); + } + else { + ar->source = "=?"; + ar->srclen = LL("=?"); + } ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; ar->what = (ar->linedefined == 0) ? "main" : "Lua"; } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); + luaO_chunkid(ar->short_src, ar->source, ar->srclen); +} + + +static int nextline (const Proto *p, int currentline, int pc) { + if (p->lineinfo[pc] != ABSLINEINFO) + return currentline + p->lineinfo[pc]; + else + return luaG_getfuncline(p, pc); } static void collectvalidlines (lua_State *L, Closure *f) { if (noLuaClosure(f)) { - setnilvalue(L->top); + setnilvalue(s2v(L->top)); api_incr_top(L); } else { int i; TValue v; - int *lineinfo = f->l.p->lineinfo; + const Proto *p = f->l.p; + int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue(L, L->top, t); /* push it on stack */ + sethvalue2s(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 */ - luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ + setbtvalue(&v); /* boolean 'true' to be the value of all indices */ + for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ + currentline = nextline(p, currentline, i); + luaH_setint(L, t, currentline, &v); /* table[line] = true */ + } } } @@ -269,7 +337,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; + ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; break; } case 'u': { @@ -296,6 +364,15 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } break; } + case 'r': { + if (ci == NULL || !(ci->callstatus & CIST_TRAN)) + ar->ftransfer = ar->ntransfer = 0; + else { + ar->ftransfer = ci->u2.transferinfo.ftransfer; + ar->ntransfer = ci->u2.transferinfo.ntransfer; + } + break; + } case 'L': case 'f': /* handled by lua_getinfo */ break; @@ -310,28 +387,26 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *cl; CallInfo *ci; - StkId func; + TValue *func; lua_lock(L); - swapextra(L); if (*what == '>') { ci = NULL; - func = L->top - 1; + func = s2v(L->top - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } else { ci = ar->i_ci; - func = ci->func; - lua_assert(ttisfunction(ci->func)); + func = s2v(ci->func); + lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { - setobjs2s(L, L->top, func); + setobj2s(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); @@ -345,30 +420,38 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ -static const char *getobjname (Proto *p, int lastpc, int reg, +static const char *getobjname (const Proto *p, int lastpc, int reg, const char **name); /* -** find a "name" for the RK value 'c' +** Find a "name" for the constant 'c'. */ -static void kname (Proto *p, int pc, int c, const char **name) { - if (ISK(c)) { /* is 'c' a constant? */ - TValue *kvalue = &p->k[INDEXK(c)]; - if (ttisstring(kvalue)) { /* literal constant? */ - *name = svalue(kvalue); /* it is its own name */ - return; - } - /* else no reasonable name found */ - } - else { /* 'c' is a register */ - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (what && *what == 'c') { /* found a constant name? */ - return; /* 'name' already filled */ - } - /* else no reasonable name found */ - } - *name = "?"; /* no reasonable name found */ +static void kname (const Proto *p, int c, const char **name) { + TValue *kvalue = &p->k[c]; + *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; +} + + +/* +** Find a "name" for the register 'c'. +*/ +static void rname (const Proto *p, int pc, int c, const char **name) { + const char *what = getobjname(p, pc, c, name); /* search for 'c' */ + if (!(what && *what == 'c')) /* did not find a constant name? */ + *name = "?"; +} + + +/* +** Find a "name" for a 'C' value in an RK instruction. +*/ +static void rkname (const Proto *p, int pc, Instruction i, const char **name) { + int c = GETARG_C(i); /* key index */ + if (GETARG_k(i)) /* is 'c' a constant? */ + kname(p, c, name); + else /* 'c' is a register */ + rname(p, pc, c, name); } @@ -380,55 +463,70 @@ static int filterpc (int pc, int jmptarget) { /* -** try to find last instruction before 'lastpc' that modified register 'reg' +** Try to find last instruction before 'lastpc' that modified register 'reg'. */ -static int findsetreg (Proto *p, int lastpc, int reg) { +static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ + if (testMMMode(GET_OPCODE(p->code[lastpc]))) + lastpc--; /* previous instruction was not actually executed */ for (pc = 0; pc < lastpc; pc++) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); + int change; /* true if current instruction changed 'reg' */ switch (op) { - case OP_LOADNIL: { + case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */ int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ - setreg = filterpc(pc, jmptarget); + change = (a <= reg && reg <= a + b); break; } - case OP_TFORCALL: { - if (reg >= a + 2) /* affect all regs above its base */ - setreg = filterpc(pc, jmptarget); + case OP_TFORCALL: { /* affect all regs above its base */ + change = (reg >= a + 2); break; } case OP_CALL: - case OP_TAILCALL: { - if (reg >= a) /* affect all registers above base */ - setreg = filterpc(pc, jmptarget); + case OP_TAILCALL: { /* affect all registers above base */ + change = (reg >= a); break; } - case OP_JMP: { - int b = GETARG_sBx(i); + case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ + int b = GETARG_sJ(i); int dest = pc + 1 + b; - /* jump is forward and do not skip 'lastpc'? */ - if (pc < dest && dest <= lastpc) { - if (dest > jmptarget) - jmptarget = dest; /* update 'jmptarget' */ - } + /* jump does not skip 'lastpc' and is larger than current one? */ + if (dest <= lastpc && dest > jmptarget) + jmptarget = dest; /* update 'jmptarget' */ + change = 0; break; } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ - setreg = filterpc(pc, jmptarget); + default: /* any instruction that sets A */ + change = (testAMode(op) && reg == a); break; } + if (change) + setreg = filterpc(pc, jmptarget); } return setreg; } -static const char *getobjname (Proto *p, int lastpc, int reg, +/* +** Check whether table being indexed by instruction 'i' is the +** environment '_ENV' +*/ +static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { + int t = GETARG_B(i); /* table index */ + const char *name; /* name of indexed variable */ + if (isup) /* is an upvalue? */ + name = upvalname(p, t); + else + getobjname(p, pc, t, &name); + return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +} + + +static const char *getobjname (const Proto *p, int lastpc, int reg, const char **name) { int pc; *name = luaF_getlocalname(p, reg + 1, lastpc); @@ -446,15 +544,24 @@ static const char *getobjname (Proto *p, int lastpc, int reg, return getobjname(p, pc, b, name); /* get name for 'b' */ break; } - case OP_GETTABUP: + case OP_GETTABUP: { + int k = GETARG_C(i); /* key index */ + kname(p, k, name); + return gxf(p, pc, i, 1); + } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ - int t = GETARG_B(i); /* table index */ - const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ - ? luaF_getlocalname(p, t + 1, pc) - : upvalname(p, t); - kname(p, pc, k, name); - return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; + rname(p, pc, k, name); + return gxf(p, pc, i, 0); + } + case OP_GETI: { + *name = "integer index"; + return "field"; + } + case OP_GETFIELD: { + int k = GETARG_C(i); /* key index */ + kname(p, k, name); + return gxf(p, pc, i, 0); } case OP_GETUPVAL: { *name = upvalname(p, GETARG_B(i)); @@ -471,8 +578,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, break; } case OP_SELF: { - int k = GETARG_C(i); /* key index */ - kname(p, pc, k, name); + rkname(p, pc, i, name); return "method"; } default: break; /* go through to return NULL */ @@ -491,7 +597,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, 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 */ + const Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ @@ -508,16 +614,14 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, } /* other instructions can do calls through metamethods */ case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: + case OP_GETI: case OP_GETFIELD: tm = TM_INDEX; break; - case OP_SETTABUP: case OP_SETTABLE: + case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: 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 */ + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + tm = cast(TMS, GETARG_C(i)); break; } case OP_UNM: tm = TM_UNM; break; @@ -525,12 +629,16 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 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_LT: case OP_LE: case OP_LTI: case OP_LEI: + *name = "order"; /* '<=' can call '__lt', etc. */ + return "metamethod"; + case OP_CLOSE: case OP_RETURN: + *name = "close"; + return "metamethod"; default: return NULL; /* cannot find a reasonable name */ } - *name = getstr(G(L)->tmname[tm]); + *name = getstr(G(L)->tmname[tm]) + 2; return "metamethod"; } @@ -544,8 +652,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, ** checks are ISO C and ensure a correct result. */ static int isinstack (CallInfo *ci, const TValue *o) { - ptrdiff_t i = o - ci->u.l.base; - return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); + StkId base = ci->func + 1; + ptrdiff_t i = cast(StkId, o) - base; + return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); } @@ -576,7 +685,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(ci, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(o - ci->u.l.base), &name); + cast_int(cast(StkId, o) - (ci->func + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } @@ -588,6 +697,12 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { } +l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { + luaG_runerror(L, "bad 'for' %s (number expected, got %s)", + what, luaT_objtypename(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"); @@ -596,8 +711,7 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { 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? */ + if (!ttisnumber(p1)) /* first operand is wrong? */ p2 = p1; /* now second is wrong */ luaG_typeerror(L, p2, msg); } @@ -608,7 +722,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1, */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; - if (!tointeger(p1, &temp)) + if (!tointegerns(p1, &temp)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } @@ -629,7 +743,7 @@ 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); + luaO_chunkid(buff, getstr(src), tsslen(src)); else { /* no source available; use "?" instead */ buff[0] = '?'; buff[1] = '\0'; } @@ -640,6 +754,7 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); + lua_assert(ttisfunction(s2v(errfunc))); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ L->top++; /* assume EXTRA_STACK */ @@ -658,42 +773,80 @@ 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->source, getcurrentline(ci)); luaG_errormsg(L); } -void luaG_traceexec (lua_State *L) { +/* +** Check whether new instruction 'newpc' is in a different line from +** previous instruction 'oldpc'. +*/ +static int changedline (const Proto *p, int oldpc, int newpc) { + if (p->lineinfo == NULL) /* no debug information? */ + return 0; + while (oldpc++ < newpc) { + if (p->lineinfo[oldpc] != 0) + return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); + } + return 0; /* no line changes between positions */ +} + + +/* +** Traces the execution of a Lua function. Called before the execution +** of each opcode, when debug is on. 'L->oldpc' stores the last +** instruction traced, to detect line changes. When entering a new +** function, 'npci' will be zero and will test as a new line without +** the need for 'oldpc'; so, 'oldpc' does not need to be initialized +** before. Some exceptional conditions may return to a function without +** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is +** reset to zero. (A wrong but valid 'oldpc' at most causes an extra +** call to a line hook.) +*/ +int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; - int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + const Proto *p = ci_func(ci)->p; + int counthook; + /* 'L->oldpc' may be invalid; reset it in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; + if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ + ci->u.l.trap = 0; /* don't need to stop again */ + return 0; /* turn off 'trap' */ + } + pc++; /* reference is always next instruction */ + ci->u.l.savedpc = pc; /* save 'pc' */ + 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 */ + return 1; /* no line hook and count != 0; nothing to be done now */ 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) */ + return 1; /* do not call hook again (VM yielded, so it did not move) */ } + if (!isIT(*(ci->u.l.savedpc - 1))) + L->top = ci->top; /* prepare top */ if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ + luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* 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; + int npci = pcRel(pc, p); + if (npci == 0 || /* call linehook when enter a new function, */ + pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ + changedline(p, oldpc, npci)) { /* enter new line */ + int newline = luaG_getfuncline(p, npci); + luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ + } + L->oldpc = npci; /* 'pc' of last call to line hook */ + } 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); } + return 1; /* keep 'trap' on */ } diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 8cea0ee0a..a0a584862 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ldebug.h $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -11,15 +11,28 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) +#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) + + +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue(s2v((ci)->func))) -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) +/* +** mark for entries in 'lineinfo' array that has absolute information in +** 'abslineinfo' array +*/ +#define ABSLINEINFO (-0x80) +LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); +LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, + StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); +LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, + const char *what); 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, @@ -33,7 +46,7 @@ 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); +LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); #endif diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 316e45c8f..5473815a1 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.157.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ldo.c $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -88,7 +88,7 @@ struct lua_longjmp { }; -static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { +void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { switch (errcode) { case LUA_ERRMEM: { /* memory error? */ setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ @@ -98,6 +98,10 @@ static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } + case CLOSEPROTECT: { + setnilvalue(s2v(oldtop)); /* no error message */ + break; + } default: { setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; @@ -114,6 +118,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); + errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ @@ -121,7 +126,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ - seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ + luaD_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); @@ -134,7 +139,8 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - unsigned short oldnCcalls = L->nCcalls; + global_State *g = G(L); + l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci); struct lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ @@ -143,7 +149,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls; + L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci; return lj.status; } @@ -155,17 +161,19 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, TValue *oldstack) { +static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { CallInfo *ci; UpVal *up; - L->top = (L->top - oldstack) + L->stack; + if (oldstack == newstack) + return; /* stack address did not change */ + L->top = (L->top - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = (up->v - oldstack) + L->stack; + up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + L->stack; - ci->func = (ci->func - oldstack) + L->stack; + ci->top = (ci->top - oldstack) + newstack; + ci->func = (ci->func - oldstack) + newstack; if (isLua(ci)) - ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; + ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } @@ -174,36 +182,53 @@ static void correctstack (lua_State *L, TValue *oldstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) -void luaD_reallocstack (lua_State *L, int newsize) { - TValue *oldstack = L->stack; +int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int lim = L->stacksize; + StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); + if (unlikely(newstack == NULL)) { /* reallocation failed? */ + if (raiseerror) + luaM_error(L); + else return 0; /* do not raise an error */ + } for (; lim < newsize; lim++) - setnilvalue(L->stack + lim); /* erase new segment */ + setnilvalue(s2v(newstack + lim)); /* erase new segment */ + correctstack(L, L->stack, newstack); + L->stack = newstack; L->stacksize = newsize; L->stack_last = L->stack + newsize - EXTRA_STACK; - correctstack(L, oldstack); + return 1; } -void luaD_growstack (lua_State *L, int n) { +/* +** Try to grow the stack by at least 'n' elements. when 'raiseerror' +** is true, raises any error; otherwise, return 0 in case of errors. +*/ +int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = L->stacksize; - if (size > LUAI_MAXSTACK) /* error after extra size? */ - luaD_throw(L, LUA_ERRERR); + int newsize = 2 * size; /* tentative new size */ + if (unlikely(size > LUAI_MAXSTACK)) { /* need more space after extra size? */ + if (raiseerror) + luaD_throw(L, LUA_ERRERR); /* error inside message handler */ + else return 0; + } else { int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - int newsize = 2 * size; - if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; - if (newsize < needed) newsize = needed; - if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ - luaD_reallocstack(L, ERRORSTACKSIZE); - luaG_runerror(L, "stack overflow"); + if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ + newsize = LUAI_MAXSTACK; + if (newsize < needed) /* but must respect what was asked for */ + newsize = needed; + if (unlikely(newsize > LUAI_MAXSTACK)) { /* stack overflow? */ + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + else return 0; } - else - luaD_reallocstack(L, newsize); - } + } /* else no errors */ + return luaD_reallocstack(L, newsize, raiseerror); } @@ -220,20 +245,16 @@ static int stackinuse (lua_State *L) { void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; + int goodsize = inuse + BASIC_STACK_SIZE; 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 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); + if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && goodsize < L->stacksize) + luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ + luaE_shrinkCI(L); /* shrink CI list */ } @@ -247,12 +268,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 +** called. (Both 'L->hook' and 'L->hookmask', which trigger this ** function, can be changed asynchronously by signals.) */ -void luaD_hook (lua_State *L, int event, int line) { +void luaD_hook (lua_State *L, int event, int line, + int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ + int mask = CIST_HOOKED; CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); ptrdiff_t ci_top = savestack(L, ci->top); @@ -260,11 +283,16 @@ void luaD_hook (lua_State *L, int event, int line) { ar.event = event; ar.currentline = line; ar.i_ci = ci; + if (ntransfer != 0) { + mask |= CIST_TRAN; /* 'ci' has transfer information */ + ci->u2.transferinfo.ftransfer = ftransfer; + ci->u2.transferinfo.ntransfer = ntransfer; + } luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); + if (L->top + LUA_MINSTACK > ci->top) + ci->top = L->top + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= CIST_HOOKED; + ci->callstatus |= mask; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); @@ -272,56 +300,66 @@ void luaD_hook (lua_State *L, int event, int line) { L->allowhook = 1; ci->top = restorestack(L, ci_top); L->top = restorestack(L, top); - ci->callstatus &= ~CIST_HOOKED; + ci->callstatus &= ~mask; } } -static void callhook (lua_State *L, CallInfo *ci) { - int hook = LUA_HOOKCALL; +/* +** Executes a call hook for Lua functions. This function is called +** whenever 'hookmask' is not zero, so it checks whether call hooks are +** active. +*/ +void luaD_hookcall (lua_State *L, CallInfo *ci) { + int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; + Proto *p; + if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ + return; /* don't call hook */ + p = clLvalue(s2v(ci->func))->p; + L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(ci->previous) && - GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { - ci->callstatus |= CIST_TAIL; - hook = LUA_HOOKTAILCALL; - } - luaD_hook(L, hook, -1); + luaD_hook(L, hook, -1, 1, p->numparams); ci->u.l.savedpc--; /* correct 'pc' */ } -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { - int i; - int nfixargs = p->numparams; - StkId base, fixed; - /* move fixed parameters to final position */ - fixed = L->top - actual; /* first fixed argument */ - base = L->top; /* final position of first argument */ - for (i = 0; i < nfixargs && i < actual; i++) { - setobjs2s(L, L->top++, fixed + i); - setnilvalue(fixed + i); /* erase original copy (for GC) */ +static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { + ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ + int delta = 0; + if (isLuacode(ci)) { + Proto *p = ci_func(ci)->p; + if (p->is_vararg) + delta = ci->u.l.nextraargs + p->numparams + 1; + if (L->top < ci->top) + L->top = ci->top; /* correct top to run hook */ } - for (; i < nfixargs; i++) - setnilvalue(L->top++); /* complete missing arguments */ - return base; + if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + int ftransfer; + ci->func += delta; /* if vararg, back to virtual 'func' */ + ftransfer = cast(unsigned short, firstres - ci->func); + luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ + ci->func -= delta; + } + if (isLua(ci = ci->previous)) + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ + return restorestack(L, oldtop); } /* -** 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. +** Check whether 'func' has a '__call' metafield. If so, put it in the +** stack, below original 'func', so that 'luaD_call' can call it. Raise +** an error if there is no '__call' metafield. */ -static void tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); +void luaD_tryfuncTM (lua_State *L, StkId func) { + const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; - if (!ttisfunction(tm)) - luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at 'func' */ - for (p = L->top; p > func; p--) + if (unlikely(ttisnil(tm))) + luaG_typeerror(L, s2v(func), "call"); /* nothing to call */ + for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); - L->top++; /* slot ensured by caller */ - setobj2s(L, func, tm); /* tag method is the new function to be called */ + L->top++; /* stack space pre-allocated by the caller */ + setobj2s(L, func, tm); /* metamethod is the new function to be called */ } @@ -331,183 +369,161 @@ static void tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static int moveresults (lua_State *L, const TValue *firstResult, StkId res, - int nres, int wanted) { +static void moveresults (lua_State *L, StkId res, int nres, int wanted) { + StkId firstresult; + int i; switch (wanted) { /* handle typical cases separately */ - case 0: break; /* nothing to move */ - case 1: { /* one result needed */ + case 0: /* no values needed */ + L->top = res; + return; + case 1: /* one value needed */ if (nres == 0) /* no results? */ - firstResult = luaO_nilobject; /* adjust with nil */ - setobjs2s(L, res, firstResult); /* move it to proper place */ + setnilvalue(s2v(res)); /* adjust with nil */ + else + setobjs2s(L, res, L->top - nres); /* move it to proper place */ + L->top = res + 1; + return; + case LUA_MULTRET: + wanted = nres; /* we want all results */ 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); + default: /* multiple results (or to-be-closed variables) */ + if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ + ptrdiff_t savedres = savestack(L, res); + luaF_close(L, res, LUA_OK); /* may change the stack */ + res = restorestack(L, savedres); + wanted = codeNresults(wanted); /* correct value */ + if (wanted == LUA_MULTRET) + wanted = nres; } break; - } } + firstresult = L->top - nres; /* index of first result */ + /* move all results to correct place */ + for (i = 0; i < nres && i < wanted; i++) + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); 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. +** moves current number of results to proper place. */ -int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { - StkId res; - 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 */ - luaD_hook(L, LUA_HOOKRET, -1); - firstResult = restorestack(L, fr); - } - L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ - } - res = ci->func; /* res == final position of 1st result */ +void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { + if (L->hookmask) + L->top = rethook(L, ci, L->top - nres, nres); L->ci = ci->previous; /* back to caller */ /* move results to proper place */ - return moveresults(L, firstResult, res, nres, wanted); + moveresults(L, ci->func, nres, ci->nresults); } -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +#define next_ci(L) (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' */ +/* +** Prepare a function for a tail call, building its call info on top +** of the current call info. 'narg1' is the number of arguments plus 1 +** (so that it includes the function itself). +*/ +void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; + int i; + for (i = 0; i < narg1; i++) /* move down function and arguments */ + setobjs2s(L, ci->func + i, func + i); + checkstackGC(L, fsize); + func = ci->func; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + L->top = func + narg1; /* set top */ +} /* -** 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). +** Call a function (C or Lua). The function to be called is at *func. +** The arguments are on the stack, right after the function. +** When returns, all the results are on the stack, starting at the original +** function position. */ -int luaD_precall (lua_State *L, StkId func, int nresults) { +void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; - CallInfo *ci; - switch (ttype(func)) { - case LUA_TCCL: /* C closure */ - f = clCvalue(func)->f; + retry: + switch (ttypetag(s2v(func))) { + case LUA_VCCL: /* C closure */ + f = clCvalue(s2v(func))->f; goto Cfunc; - case LUA_TLCF: /* light C function */ - f = fvalue(func); + case LUA_VLCF: /* light C function */ + f = fvalue(s2v(func)); Cfunc: { int n; /* number of returns */ - checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ + CallInfo *ci; + checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + L->ci = ci = next_ci(L); ci->nresults = nresults; - ci->func = func; + ci->callstatus = CIST_C; ci->top = L->top + LUA_MINSTACK; + ci->func = func; lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); + if (L->hookmask & LUA_MASKCALL) { + int narg = cast_int(L->top - func) - 1; + luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); + } 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; + luaD_poscall(L, ci, n); + break; } - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - Proto *p = clLvalue(func)->p; - int n = cast_int(L->top - func) - 1; /* number of real arguments */ + case LUA_VLCL: { /* Lua function */ + CallInfo *ci; + Proto *p = clLvalue(s2v(func))->p; + int narg = cast_int(L->top - func) - 1; /* number of real arguments */ + int nfixparams = p->numparams; 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 */ + checkstackGCp(L, fsize, func); + L->ci = ci = next_ci(L); ci->nresults = nresults; + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus = 0; + ci->top = func + 1 + fsize; ci->func = func; - ci->u.l.base = base; - L->top = ci->top = base + fsize; + L->ci = ci; + for (; narg < nfixparams; narg++) + setnilvalue(s2v(L->top++)); /* complete missing arguments */ 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; + luaV_execute(L, ci); /* run the function */ + break; } 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 */ + checkstackGCp(L, 1, func); /* space for metamethod */ + luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + goto retry; /* try again with metamethod */ } } } /* -** 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 */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** 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) { - if (++L->nCcalls >= LUAI_MAXCCALLS) - stackerror(L); - if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ - luaV_execute(L); /* call it */ - L->nCcalls--; -} - - -/* -** Similar to 'luaD_call', but does not allow yields during the call +** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - L->nny++; + incXCcalls(L); + if (getCcalls(L) <= CSTACKERR) { /* possible C stack overflow? */ + luaE_exitCcall(L); /* to compensate decrement in next call */ + luaE_enterCcall(L); /* check properly */ + } luaD_call(L, func, nResults); - L->nny--; + decXCcalls(L); } @@ -519,7 +535,7 @@ static void finishCcall (lua_State *L, int status) { CallInfo *ci = L->ci; int n; /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && L->nny == 0); + lua_assert(ci->u.c.k != NULL && yieldable(L)); /* 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? */ @@ -533,7 +549,7 @@ static void finishCcall (lua_State *L, int status) { n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } @@ -546,14 +562,15 @@ static void finishCcall (lua_State *L, int status) { ** status is LUA_YIELD). */ static void unroll (lua_State *L, void *ud) { + CallInfo *ci; 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? */ + while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ + if (!isLua(ci)) /* C function? */ 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' */ + luaV_execute(L, ci); /* execute down to higher C 'boundary' */ } } } @@ -583,12 +600,12 @@ static int recover (lua_State *L, int status) { CallInfo *ci = findpcall(L); if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->extra); - luaF_close(L, oldtop); - seterrorobj(L, status, oldtop); + oldtop = restorestack(L, ci->u2.funcidx); + luaF_close(L, oldtop, status); /* may change the stack */ + oldtop = restorestack(L, ci->u2.funcidx); + luaD_seterrorobj(L, status, oldtop); L->ci = ci; 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; return 1; /* continue running the coroutine */ @@ -621,95 +638,94 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) { /* starting a coroutine? */ - if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ - luaV_execute(L); /* call it */ + luaD_call(L, firstArg - 1, LUA_MULTRET); } 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? */ - luaV_execute(L); /* just continue running Lua code */ + luaV_execute(L, ci); /* just continue running Lua code */ else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ 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, ci, firstArg, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } unroll(L, NULL); /* run continuation */ } } - -LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { +LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, + int *nresults) { int status; - unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); 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->top - (L->ci->func + 1) == nargs) /* no function? */ + return resume_error(L, "cannot resume dead coroutine", nargs); } - else if (L->status != LUA_YIELD) + else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); - L->nCcalls = (from) ? from->nCcalls + 1 : 1; - if (L->nCcalls >= LUAI_MAXCCALLS) + if (from == NULL) + L->nCcalls = CSTACKTHREAD; + else /* correct 'nCcalls' for this thread */ + L->nCcalls = getCcalls(from) - L->nci - CSTACKCF; + if (L->nCcalls <= CSTACKERR) 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); - if (status == -1) /* error calling 'lua_resume'? */ - status = LUA_ERRRUN; - else { /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &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 */ + /* continue running after recoverable errors */ + while (errorstatus(status) && recover(L, status)) { + /* unroll continuation */ + status = luaD_rawrunprotected(L, unroll, &status); + } + if (likely(!errorstatus(status))) + lua_assert(status == L->status); /* normal end or yield */ + else { /* unrecoverable error */ + L->status = cast_byte(status); /* mark thread as 'dead' */ + luaD_seterrorobj(L, status, L->top); /* push error message */ + L->ci->top = L->top; } - L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; - lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); + *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield + : cast_int(L->top - (L->ci->func + 1)); lua_unlock(L); return status; } LUA_API int lua_isyieldable (lua_State *L) { - return (L->nny == 0); + return yieldable(L); } LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { - CallInfo *ci = L->ci; + CallInfo *ci; luai_userstateyield(L, nresults); lua_lock(L); + ci = L->ci; api_checknelems(L, nresults); - if (L->nny > 0) { + if (unlikely(!yieldable(L))) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); else luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; - ci->extra = savestack(L, ci->func); /* save current 'func' */ if (isLua(ci)) { /* inside a hook? */ + lua_assert(!isLuacode(ci)); api_check(L, k == NULL, "hooks cannot continue after yielding"); + ci->u2.nyield = 0; /* no results */ } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - ci->func = L->top - nresults - 1; /* protect stack below results */ + ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ @@ -718,22 +734,26 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } +/* +** Call the C function 'func' in protected mode, restoring basic +** thread information ('allowhook', etc.) and in particular +** its stack level in case of errors. +*/ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { int status; CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; - unsigned short old_nny = L->nny; ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); - if (status != LUA_OK) { /* an error occurred? */ + if (unlikely(status != LUA_OK)) { /* an error occurred? */ StkId oldtop = restorestack(L, old_top); - luaF_close(L, oldtop); /* close possible pending closures */ - seterrorobj(L, status, oldtop); L->ci = old_ci; L->allowhook = old_allowhooks; - L->nny = old_nny; + status = luaF_close(L, oldtop, status); + oldtop = restorestack(L, old_top); /* previous call may change stack */ + luaD_seterrorobj(L, status, oldtop); luaD_shrinkstack(L); } L->errfunc = old_errfunc; @@ -784,7 +804,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode) { struct SParser p; int status; - L->nny++; /* cannot yield during parsing */ + incnny(L); /* cannot yield during parsing */ p.z = z; p.name = name; p.mode = mode; p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; @@ -795,7 +815,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); - L->nny--; + decnny(L); return status; } diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 3b2983a38..6c6cb2855 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ldo.h $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -17,12 +17,15 @@ ** 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. +** It also allows the running of one GC step when the stack is +** reallocated. ** '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); } + { pre; luaD_growstack(L, n, 1); pos; } \ + else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) @@ -30,24 +33,40 @@ #define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) +#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) + + +/* macro to check stack size, preserving 'p' */ +#define checkstackGCp(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' */ + + +/* macro to check stack size and GC */ +#define checkstackGC(L,fsize) \ + luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); +LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 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_hook (lua_State *L, int event, int line, + int fTransfer, int nTransfer); +LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); +LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); 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 void luaD_tryfuncTM (lua_State *L, StkId func); 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, 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_poscall (lua_State *L, CallInfo *ci, int nres); +LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); +LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index f025acac3..f848b669c 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ldump.c $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -29,15 +29,15 @@ typedef struct { /* -** All high-level dumps go through DumpVector; you can change it to +** 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) +#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) -#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) +#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) -static void DumpBlock (const void *b, size_t size, DumpState *D) { +static void dumpBlock (DumpState *D, const void *b, size_t size) { if (D->status == 0 && size > 0) { lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); @@ -46,153 +46,164 @@ static void DumpBlock (const void *b, size_t size, DumpState *D) { } -#define DumpVar(x,D) DumpVector(&x,1,D) +#define dumpVar(D,x) dumpVector(D,&x,1) -static void DumpByte (int y, DumpState *D) { +static void dumpByte (DumpState *D, int y) { lu_byte x = (lu_byte)y; - DumpVar(x, D); + dumpVar(D, x); } -static void DumpInt (int x, DumpState *D) { - DumpVar(x, D); +/* dumpInt Buff Size */ +#define DIBS ((sizeof(size_t) * 8 / 7) + 1) + +static void dumpSize (DumpState *D, size_t x) { + lu_byte buff[DIBS]; + int n = 0; + do { + buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ + x >>= 7; + } while (x != 0); + buff[DIBS - 1] |= 0x80; /* mark last byte */ + dumpVector(D, buff + DIBS - n, n); +} + + +static void dumpInt (DumpState *D, int x) { + dumpSize(D, x); } -static void DumpNumber (lua_Number x, DumpState *D) { - DumpVar(x, D); +static void dumpNumber (DumpState *D, lua_Number x) { + dumpVar(D, x); } -static void DumpInteger (lua_Integer x, DumpState *D) { - DumpVar(x, D); +static void dumpInteger (DumpState *D, lua_Integer x) { + dumpVar(D, x); } -static void DumpString (const TString *s, DumpState *D) { +static void dumpString (DumpState *D, const TString *s) { if (s == NULL) - DumpByte(0, D); + dumpSize(D, 0); else { - size_t size = tsslen(s) + 1; /* include trailing '\0' */ + size_t size = tsslen(s); const char *str = getstr(s); - if (size < 0xFF) - DumpByte(cast_int(size), D); - else { - DumpByte(0xFF, D); - DumpVar(size, D); - } - DumpVector(str, size - 1, D); /* no need to save '\0' */ + dumpSize(D, size + 1); + dumpVector(D, str, size); } } -static void DumpCode (const Proto *f, DumpState *D) { - DumpInt(f->sizecode, D); - DumpVector(f->code, f->sizecode, D); +static void dumpCode (DumpState *D, const Proto *f) { + dumpInt(D, f->sizecode); + dumpVector(D, f->code, f->sizecode); } -static void DumpFunction(const Proto *f, TString *psource, DumpState *D); +static void dumpFunction(DumpState *D, const Proto *f, TString *psource); -static void DumpConstants (const Proto *f, DumpState *D) { +static void dumpConstants (DumpState *D, const Proto *f) { int i; int n = f->sizek; - DumpInt(n, D); + dumpInt(D, n); 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); + int tt = ttypetag(o); + dumpByte(D, tt); + switch (tt) { + case LUA_VNUMFLT: + dumpNumber(D, fltvalue(o)); + break; + case LUA_VNUMINT: + dumpInteger(D, ivalue(o)); + break; + case LUA_VSHRSTR: + case LUA_VLNGSTR: + dumpString(D, tsvalue(o)); + break; + default: + lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); } } } -static void DumpProtos (const Proto *f, DumpState *D) { +static void dumpProtos (DumpState *D, const Proto *f) { int i; int n = f->sizep; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); + dumpFunction(D, f->p[i], f->source); } -static void DumpUpvalues (const Proto *f, DumpState *D) { +static void dumpUpvalues (DumpState *D, const Proto *f) { int i, n = f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpByte(f->upvalues[i].instack, D); - DumpByte(f->upvalues[i].idx, D); + dumpByte(D, f->upvalues[i].instack); + dumpByte(D, f->upvalues[i].idx); + dumpByte(D, f->upvalues[i].kind); } } -static void DumpDebug (const Proto *f, DumpState *D) { +static void dumpDebug (DumpState *D, const Proto *f) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; - DumpInt(n, D); - DumpVector(f->lineinfo, n, D); + dumpInt(D, n); + dumpVector(D, f->lineinfo, n); + n = (D->strip) ? 0 : f->sizeabslineinfo; + dumpInt(D, n); + for (i = 0; i < n; i++) { + dumpInt(D, f->abslineinfo[i].pc); + dumpInt(D, f->abslineinfo[i].line); + } n = (D->strip) ? 0 : f->sizelocvars; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpString(f->locvars[i].varname, D); - DumpInt(f->locvars[i].startpc, D); - DumpInt(f->locvars[i].endpc, D); + dumpString(D, f->locvars[i].varname); + dumpInt(D, f->locvars[i].startpc); + dumpInt(D, f->locvars[i].endpc); } n = (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpString(f->upvalues[i].name, D); + dumpString(D, f->upvalues[i].name); } -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { +static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { if (D->strip || f->source == psource) - DumpString(NULL, D); /* no debug info or same source as its parent */ + dumpString(D, NULL); /* 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); + dumpString(D, f->source); + dumpInt(D, f->linedefined); + dumpInt(D, f->lastlinedefined); + dumpByte(D, f->numparams); + dumpByte(D, f->is_vararg); + dumpByte(D, f->maxstacksize); + dumpCode(D, f); + dumpConstants(D, f); + dumpUpvalues(D, f); + dumpProtos(D, f); + dumpDebug(D, f); } -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); +static void dumpHeader (DumpState *D) { + dumpLiteral(D, LUA_SIGNATURE); + dumpByte(D, LUAC_VERSION); + dumpByte(D, LUAC_FORMAT); + dumpLiteral(D, LUAC_DATA); + dumpByte(D, sizeof(Instruction)); + dumpByte(D, sizeof(lua_Integer)); + dumpByte(D, sizeof(lua_Number)); + dumpInteger(D, LUAC_INT); + dumpNumber(D, LUAC_NUM); } @@ -207,9 +218,9 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.data = data; D.strip = strip; D.status = 0; - DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); - DumpFunction(f, NULL, &D); + dumpHeader(&D); + dumpByte(&D, f->sizeupvalues); + dumpFunction(&D, f, NULL); return D.status; } diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 260f64e93..f2d60d2e1 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lfunc.c $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -14,6 +14,8 @@ #include "lua.h" +#include "ldebug.h" +#include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" @@ -23,82 +25,227 @@ -CClosure *luaF_newCclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); +CClosure *luaF_newCclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); CClosure *c = gco2ccl(o); - c->nupvalues = cast_byte(n); + c->nupvalues = cast_byte(nupvals); return c; } -LClosure *luaF_newLclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); +LClosure *luaF_newLclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); LClosure *c = gco2lcl(o); c->p = NULL; - c->nupvalues = cast_byte(n); - while (n--) c->upvals[n] = NULL; + c->nupvalues = cast_byte(nupvals); + while (nupvals--) c->upvals[nupvals] = NULL; return c; } + /* ** 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; + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); + UpVal *uv = gco2upv(o); uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); cl->upvals[i] = uv; + luaC_objbarrier(L, cl, o); + } +} + + +/* +** Create a new upvalue at the given level, and link it to the list of +** open upvalues of 'L' after entry 'prev'. +**/ +static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); + UpVal *uv = gco2upv(o); + UpVal *next = *prev; + uv->v = s2v(level); /* current value lives in the stack */ + uv->tbc = tbc; + uv->u.open.next = next; /* link it to list of open upvalues */ + uv->u.open.previous = prev; + if (next) + next->u.open.previous = &uv->u.open.next; + *prev = 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; } +/* +** Find and reuse, or create if it does not exist, an upvalue +** at the given level. +*/ UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; UpVal *p; - UpVal *uv; 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? */ + while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ + lua_assert(!isdead(G(L), p)); + if (uplevel(p) == level) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } - /* 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 */ - 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; + /* not found: create a new upvalue after 'pp' */ + return newupval(L, 0, level, pp); +} + + +static void callclose (lua_State *L, void *ud) { + UNUSED(ud); + luaD_callnoyield(L, L->top - 3, 0); +} + + +/* +** Prepare closing method plus its arguments for object 'obj' with +** error message 'err'. (This function assumes EXTRA_STACK.) +*/ +static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { + StkId top = L->top; + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) /* no metamethod? */ + return 0; /* nothing to call */ + setobj2s(L, top, tm); /* will call metamethod... */ + setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ + setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ + L->top = top + 3; /* add function and arguments */ + return 1; +} + + +/* +** Raise an error with message 'msg', inserting the name of the +** local variable at position 'level' in the stack. +*/ +static void varerror (lua_State *L, StkId level, const char *msg) { + int idx = cast_int(level - L->ci->func); + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, msg, vname); +} + + +/* +** Prepare and call a closing method. If status is OK, code is still +** inside the original protected call, and so any error will be handled +** there. Otherwise, a previous error already activated the original +** protected call, and so the call to the closing method must be +** protected here. (A status == CLOSEPROTECT behaves like a previous +** error, to also run the closing method in protected mode). +** If status is OK, the call to the closing method will be pushed +** at the top of the stack. Otherwise, values are pushed after +** the 'level' of the upvalue being closed, as everything after +** that won't be used again. +*/ +static int callclosemth (lua_State *L, StkId level, int status) { + TValue *uv = s2v(level); /* value being closed */ + if (likely(status == LUA_OK)) { + if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ + callclose(L, NULL); /* call closing method */ + else if (!l_isfalse(uv)) /* non-closable non-false value? */ + varerror(L, level, "attempt to close non-closable variable '%s'"); } - return uv; + else { /* must close the object in protected mode */ + ptrdiff_t oldtop; + level++; /* space for error message */ + oldtop = savestack(L, level + 1); /* top will be after that */ + luaD_seterrorobj(L, status, level); /* set error message */ + if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ + int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); + if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ + status = newstatus; /* this will be the new error */ + else { + if (newstatus != LUA_OK) /* suppressed error? */ + luaE_warnerror(L, "__close metamethod"); + /* leave original error (or nil) on top */ + L->top = restorestack(L, oldtop); + } + } + /* else no metamethod; ignore this case and keep original error */ + } + return status; +} + + +/* +** Try to create a to-be-closed upvalue +** (can raise a memory-allocation error) +*/ +static void trynewtbcupval (lua_State *L, void *ud) { + newupval(L, 1, cast(StkId, ud), &L->openupval); } -void luaF_close (lua_State *L, StkId level) { +/* +** Create a to-be-closed upvalue. If there is a memory error +** when creating the upvalue, the closing method must be called here, +** as there is no upvalue to call it later. +*/ +void luaF_newtbcupval (lua_State *L, StkId level) { + TValue *obj = s2v(level); + lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); + if (!l_isfalse(obj)) { /* false doesn't need to be closed */ + int status; + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) /* no metamethod? */ + varerror(L, level, "variable '%s' got a non-closable value"); + status = luaD_rawrunprotected(L, trynewtbcupval, level); + if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ + lua_assert(status == LUA_ERRMEM); + luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ + /* next call must succeed, as object is closable */ + prepclosingmethod(L, s2v(level), s2v(level + 1)); + callclose(L, NULL); /* call closing method */ + luaD_throw(L, LUA_ERRMEM); /* throw memory error */ + } + } +} + + +void luaF_unlinkupval (UpVal *uv) { + lua_assert(upisopen(uv)); + *uv->u.open.previous = uv->u.open.next; + if (uv->u.open.next) + uv->u.open.next->u.open.previous = uv->u.open.previous; +} + + +int luaF_close (lua_State *L, StkId level, int status) { UpVal *uv; - 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 { - setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ - uv->v = &uv->u.value; /* now current value lives here */ - luaC_upvalbarrier(L, uv); + while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { + TValue *slot = &uv->u.value; /* new position for value */ + lua_assert(uplevel(uv) < L->top); + if (uv->tbc && status != NOCLOSINGMETH) { + /* must run closing method, which may change the stack */ + ptrdiff_t levelrel = savestack(L, level); + status = callclosemth(L, uplevel(uv), status); + level = restorestack(L, levelrel); + } + luaF_unlinkupval(uv); + setobj(L, slot, uv->v); /* move value to upvalue slot */ + uv->v = slot; /* now current value lives here */ + if (!iswhite(uv)) { /* neither white nor dead? */ + nw2black(uv); /* closed upvalues cannot be gray */ + luaC_barrier(L, uv, slot); } } + return status; } Proto *luaF_newproto (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); + GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); Proto *f = gco2p(o); f->k = NULL; f->sizek = 0; @@ -108,6 +255,8 @@ Proto *luaF_newproto (lua_State *L) { f->sizecode = 0; f->lineinfo = NULL; f->sizelineinfo = 0; + f->abslineinfo = NULL; + f->sizeabslineinfo = 0; f->upvalues = NULL; f->sizeupvalues = 0; f->numparams = 0; @@ -127,6 +276,7 @@ void luaF_freeproto (lua_State *L, Proto *f) { 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->abslineinfo, f->sizeabslineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); luaM_free(L, f); @@ -156,7 +306,7 @@ void luaF_shareproto (Proto *f) { makeshared(f); luaS_share(f->source); for (i = 0; i < f->sizek; i++) { - if (ttnov(&f->k[i]) == LUA_TSTRING) + if (ttype(&f->k[i]) == LUA_TSTRING) luaS_share(tsvalue(&f->k[i])); } for (i = 0; i < f->sizeupvalues; i++) diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index e9512514d..4025ea802 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lfunc.h $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -11,11 +11,11 @@ #include "lobject.h" -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TValue)*((n)-1))) +#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ + cast_int(sizeof(TValue)) * (n)) -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TValue *)*((n)-1))) +#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ + cast_int(sizeof(TValue *)) * (n)) /* test whether thread is in 'twups' list */ @@ -29,34 +29,41 @@ #define MAXUPVAL 255 +#define upisopen(up) ((up)->v != &(up)->u.value) + + +#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) + + /* -** Upvalues for Lua closures +** maximum number of misses before giving up the cache of closures +** in prototypes */ -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 MAXMISS 10 -#define upisopen(up) ((up)->v != &(up)->u.value) + +/* +** Special "status" for 'luaF_close' +*/ + +/* close upvalues without running their closing methods */ +#define NOCLOSINGMETH (-1) + +/* close upvalues running all closing methods in protected mode */ +#define CLOSEPROTECT (-2) 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 CClosure *luaF_newCclosure (lua_State *L, int nupvals); +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); 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_newtbcupval (lua_State *L, StkId level); +LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); +LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); LUAI_FUNC void luaF_shareproto (Proto *func); - #endif diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 08395dece..c2027fe8a 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $ +** $Id: lgc.c $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -9,9 +9,10 @@ #include "lprefix.h" - +#include #include + #include "lua.h" #include "ldebug.h" @@ -27,29 +28,29 @@ /* -** internal state for collector while inside the atomic phase. The -** collector should never be in this state while running regular code. +** Maximum number of elements to sweep in each single step. +** (Large enough to dissipate fixed overheads but small enough +** to allow small steps for the collector.) */ -#define GCSinsideatomic (GCSpause + 1) +#define GCSWEEPMAX 100 /* -** cost of sweeping one element (the size of a small object divided -** by some adjust for the sweep speed) +** Maximum number of finalizers to call in each single step. */ -#define GCSWEEPCOST ((sizeof(TString) + 4) / 4) +#define GCFINMAX 10 -/* maximum number of elements to sweep in each single step */ -#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) -/* cost of calling one finalizer */ -#define GCFINALIZECOST GCSWEEPCOST +/* +** Cost of calling one finalizer. +*/ +#define GCFINALIZECOST 50 /* -** macro to adjust 'stepmul': 'stepmul' is actually used like -** 'stepmul / STEPMULADJ' (value chosen by tests) +** The equivalent, in bytes, of one unit of "work" (visiting a slot, +** sweeping an object, etc.) */ -#define STEPMULADJ 200 +#define WORK2MEM sizeof(TValue) /* @@ -59,30 +60,42 @@ #define PAUSEADJ 100 -/* -** 'makewhite' erases all color bits then sets only the current white -** bit -*/ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask with all color bits */ +#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) + +/* mask with all GC bits */ +#define maskgcbits (maskcolors | AGEBITS) + + +/* macro to erase all color bits then set only the current white bit */ #define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | (x->marked & bitmask(SHAREBIT)) | luaC_white(g))) + (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g))) + +/* make an object gray (neither white nor black) */ +#define set2gray(x) resetbits(x->marked, maskcolors) -#define white2gray(x) resetbits(x->marked, WHITEBITS) -#define black2gray(x) resetbit(x->marked, BLACKBIT) + +/* make an object black (coming from any color) */ +#define set2black(x) \ + (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) -#define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) +#define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) +/* +** Protected access to objects in values +*/ +#define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) -#define markvalue(g,o) { checkconsistency(o); \ +#define markvalue(g,o) { checkliveness(g->mainthread,o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } +#define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } + #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } /* @@ -92,6 +105,8 @@ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); +static lu_mem atomic (lua_State *L); +static void entersweep (lua_State *L); /* @@ -104,28 +119,60 @@ static void reallymarkobject (global_State *g, GCObject *o); /* ** one after last element in a hash array */ -#define gnodelast(h) gnode(h, cast(size_t, sizenode(h))) +#define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) + + +static GCObject **getgclist (GCObject *o) { + switch (o->tt) { + case LUA_VTABLE: return &gco2t(o)->gclist; + case LUA_VLCL: return &gco2lcl(o)->gclist; + case LUA_VCCL: return &gco2ccl(o)->gclist; + case LUA_VTHREAD: return &gco2th(o)->gclist; + case LUA_VPROTO: return &gco2p(o)->gclist; + case LUA_VUSERDATA: { + Udata *u = gco2u(o); + lua_assert(u->nuvalue > 0); + return &u->gclist; + } + default: lua_assert(0); return 0; + } +} + + +/* +** Link a collectable object 'o' with a known type into the list 'p'. +** (Must be a macro to access the 'gclist' field in different types.) +*/ +#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) + +static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { + lua_assert(!isgray(o)); /* cannot be in a gray list */ + *pnext = *list; + *list = o; + set2gray(o); /* now it is */ +} /* -** link collectable object 'o' into list pointed by 'p' +** Link a generic collectable object 'o' into the list 'p'. */ -#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) +#define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) + /* -** 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. +** Clear keys for empty entries in tables. If entry is empty +** and its key is not marked, mark its entry as dead. This allows the +** collection of the key, but keeps its entry in the table (its removal +** could break a chain). The main feature of a dead key is that it must +** be different from any other value, to do not disturb searches. +** Other places never manipulate dead keys, because its associated empty +** value is enough to signal that the entry is logically empty. */ -static void removeentry (Node *n) { - lua_assert(ttisnil(gval(n))); - if (valiswhite(gkey(n))) - setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */ +static void clearkey (Node *n) { + lua_assert(isempty(gval(n))); + if (keyiswhite(n)) + setdeadkey(n); /* unused and unmarked key; remove it */ } @@ -136,30 +183,43 @@ static void removeentry (Node *n) { ** 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, tsvalue(o)); /* strings are 'values', so are never weak */ +static int iscleared (global_State *g, const GCObject *o) { + if (o == NULL) return 0; /* non-collectable value */ + else if (novariant(o->tt) == LUA_TSTRING) { + markobject(g, o); /* strings are 'values', so are never weak */ return 0; } - else return iswhite(gcvalue(o)); + else return iswhite(o); } /* -** barrier that moves collector forward, that is, mark the white 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.) +** Barrier that moves collector forward, that is, marks the white object +** 'v' being pointed by the black object 'o'. In the generational +** mode, 'v' must also become old, if 'o' is old; however, it cannot +** be changed directly to OLD, because it may still point to non-old +** objects. So, it is marked as OLD0. In the next cycle it will become +** OLD1, and in the next it will finally become OLD (regular old). By +** then, any object it points to will also be old. If called in the +** incremental sweep phase, it clears the black object to white (sweep +** it) to avoid other barrier calls for this same object. (That cannot +** be done is generational mode, as its sweep does not distinguish +** whites from deads.) */ 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)); - if (keepinvariant(g)) /* must keep invariant? */ + lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o) && !isshared(o)); + if (keepinvariant(g)) { /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ + if (isold(o)) { + lua_assert(!isold(v)); /* white object could not be old */ + setage(v, G_OLD0); /* restore generational invariant */ + } + } else { /* sweep phase */ lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ + if (g->gckind == KGC_INC) /* incremental mode? */ + makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -168,33 +228,24 @@ 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. */ -void luaC_barrierback_ (lua_State *L, Table *t) { +void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); - lua_assert(isblack(t) && !isdead(g, t)); - black2gray(t); /* make table gray (again) */ - linkgclist(t, g->grayagain); -} - - -/* -** 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. -*/ -void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { - global_State *g = G(L); - GCObject *o = gcvalue(uv->v); - lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ - if (keepinvariant(g)) - markobject(g, o); + lua_assert(isblack(o) && !isdead(g, o)); + lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); + if (getage(o) == G_TOUCHED2) /* already in gray list? */ + set2gray(o); /* make it gray to become touched1 */ + else /* link it in 'grayagain' and paint it gray */ + linkobjgclist(o, g->grayagain); + if (isold(o)) /* generational mode? */ + setage(o, G_TOUCHED1); /* touched in current cycle */ } 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 */ + set2gray(o); /* they will be gray forever */ + setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ g->fixedgc = o; @@ -227,57 +278,47 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { /* -** mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list.) +** Mark an object. Userdata with no user values, strings, and closed +** upvalues are visited and turned black here. Open upvalues are +** already indirectly linked through their respective threads in the +** 'twups' list, so they don't go to the gray list; nevertheless, they +** are kept gray to avoid barriers, as their values will be revisited +** by the thread or by 'remarkupvals'. Other objects are added to the +** gray list to be visited (and turned black) later. Both userdata and +** upvalues can call this function recursively, but this recursion goes +** for at most two levels: An upvalue cannot refer to another upvalue +** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - reentry: if (isshared(o)) return; - white2gray(o); switch (o->tt) { - case LUA_TSHRSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); + case LUA_VSHRSTR: + case LUA_VLNGSTR: { + set2black(o); /* nothing to visit */ break; } - case LUA_TLNGSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); + case LUA_VUPVAL: { + UpVal *uv = gco2upv(o); + if (upisopen(uv)) + set2gray(uv); /* open upvalues are kept gray */ + else + set2black(o); /* closed upvalues are visited here */ + markvalue(g, uv->v); /* mark its content */ break; } - case LUA_TUSERDATA: { - TValue uvalue; - markobjectN(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; + case LUA_VUSERDATA: { + Udata *u = gco2u(o); + if (u->nuvalue == 0) { /* no user values? */ + markobjectN(g, u->metatable); /* mark its metatable */ + set2black(o); /* nothing else to mark */ + break; } - break; - } - case LUA_TLCL: { - linkgclist(gco2lcl(o), g->gray); - break; - } - case LUA_TCCL: { - linkgclist(gco2ccl(o), g->gray); - break; - } - case LUA_TTABLE: { - linkgclist(gco2t(o), g->gray); - break; - } - case LUA_TTHREAD: { - linkgclist(gco2th(o), g->gray); - break; - } - case LUA_TPROTO: { - linkgclist(gco2p(o), g->gray); + /* else... */ + } /* FALLTHROUGH */ + case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: + case LUA_VTHREAD: case LUA_VPROTO: { + linkobjgclist(o, g->gray); /* to be visited later */ break; } default: lua_assert(0); break; @@ -298,38 +339,58 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static void markbeingfnz (global_State *g) { +static lu_mem markbeingfnz (global_State *g) { GCObject *o; - for (o = g->tobefnz; o != NULL; o = o->next) + lu_mem count = 0; + for (o = g->tobefnz; o != NULL; o = o->next) { + count++; markobject(g, o); + } + return count; } /* -** 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. +** For each non-marked thread, simulates a barrier between each open +** upvalue and its value. (If the thread is collected, the value will be +** assigned to the upvalue, but then it can be too late for the barrier +** to act. The "barrier" does not need to check colors: A non-marked +** thread must be young; upvalues cannot be older than their threads; so +** any visited upvalue must be young too.) Also removes the thread from +** the list, as it was already visited. Removes also threads with no +** upvalues, as they have nothing to be checked. (If the thread gets an +** upvalue later, it will be linked in the list again.) */ -static void remarkupvals (global_State *g) { +static int remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; + int work = 0; /* estimate of how much work was done here */ while ((thread = *p) != NULL) { - lua_assert(!isblack(thread)); /* threads are never black */ - if (isgray(thread) && thread->openupval != NULL) + work++; + if (!iswhite(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; + lua_assert(!isold(thread) || thread->openupval == NULL); *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; + lua_assert(getage(uv) <= getage(thread)); + work++; + if (!iswhite(uv)) { /* upvalue already visited? */ + lua_assert(upisopen(uv) && isgray(uv)); + markvalue(g, uv->v); /* mark its value */ } } } } + return work; +} + + +static void cleargraylists (global_State *g) { + g->gray = g->grayagain = NULL; + g->weak = g->allweak = g->ephemeron = NULL; } @@ -337,8 +398,7 @@ static void remarkupvals (global_State *g) { ** mark root set and reset all gray lists, to start a new collection */ static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; + cleargraylists(g); markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -354,6 +414,26 @@ static void restartcollection (global_State *g) { ** ======================================================= */ + +/* +** Check whether object 'o' should be kept in the 'grayagain' list for +** post-processing by 'correctgraylist'. (It could put all old objects +** in the list and leave all the work to 'correctgraylist', but it is +** more efficient to avoid adding elements that will be removed.) Only +** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go +** back to a gray list, but then it must become OLD. (That is what +** 'correctgraylist' does when it finds a TOUCHED2 object.) +*/ +static void genlink (global_State *g, GCObject *o) { + lua_assert(isblack(o)); + if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ + linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ + } /* everything else do not need to be linked back */ + else if (getage(o) == G_TOUCHED2) + changeage(o, G_TOUCHED2, G_OLD); /* advance age */ +} + + /* ** 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 @@ -364,22 +444,21 @@ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); /* 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); + int hasclears = (h->alimit > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */ + lua_assert(!keyisnil(n)); + markkey(g, n); + if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */ hasclears = 1; /* table will have to be cleared */ } } - if (g->gcstate == GCSpropagate) - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ - else if (hasclears) + if (g->gcstate == GCSatomic && hasclears) linkgclist(h, g->weak); /* has to be cleared later */ + else + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ } @@ -391,27 +470,31 @@ static void traverseweakvalue (global_State *g, Table *h) { ** 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). +** (in the atomic phase). In generational mode, some tables +** must be kept in some gray list for post-processing; this is done +** by 'genlink'. */ -static int traverseephemeron (global_State *g, Table *h) { +static int traverseephemeron (global_State *g, Table *h, int inv) { int marked = 0; /* true if an object is marked in this traversal */ int hasclears = 0; /* true if table has white keys */ int hasww = 0; /* true if table has entry "white-key -> white-value" */ - Node *n, *limit = gnodelast(h); unsigned int i; + unsigned int asize = luaH_realasize(h); + unsigned int nsize = sizenode(h); /* traverse array part */ - for (i = 0; i < h->sizearray; i++) { + for (i = 0; i < asize; i++) { if (valiswhite(&h->array[i])) { marked = 1; reallymarkobject(g, gcvalue(&h->array[i])); } } - /* traverse hash part */ - for (n = gnode(h, 0); n < limit; n++) { - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ + /* traverse hash part; if 'inv', traverse descending + (see 'convergeephemerons') */ + for (i = 0; i < nsize; i++) { + Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i); + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ + else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ hasww = 1; /* white-white entry */ @@ -428,6 +511,8 @@ static int traverseephemeron (global_State *g, Table *h) { 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 */ + else + genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ return marked; } @@ -435,18 +520,19 @@ static int traverseephemeron (global_State *g, Table *h) { static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); unsigned int i; - for (i = 0; i < h->sizearray; i++) /* traverse array part */ + unsigned int asize = luaH_realasize(h); + for (i = 0; i < asize; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - markvalue(g, gval(n)); /* mark value */ + lua_assert(!keyisnil(n)); + markkey(g, n); + markvalue(g, gval(n)); } } + genlink(g, obj2gco(h)); } @@ -455,21 +541,29 @@ static lu_mem traversetable (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); markobjectN(g, h->metatable); if (mode && ttisstring(mode) && /* is there a weak mode? */ - ((weakkey = strchr(svalue(mode), 'k')), - (weakvalue = strchr(svalue(mode), 'v')), + (cast_void(weakkey = strchr(svalue(mode), 'k')), + cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* keep table gray */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h); + traverseephemeron(g, h, 0); else /* all weak */ linkgclist(h, g->allweak); /* nothing to traverse now */ } else /* not weak */ traversestrongtable(g, h); - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, allocsizenode(h)); + return 1 + h->alimit + 2 * allocsizenode(h); +} + + +static int traverseudata (global_State *g, Udata *u) { + int i; + markobjectN(g, u->metatable); /* mark its metatable */ + for (i = 0; i < u->nuvalue; i++) + markvalue(g, &u->uv[i].uv); + genlink(g, obj2gco(u)); + return 1 + u->nuvalue; } @@ -489,137 +583,126 @@ static int traverseproto (global_State *g, Proto *f) { 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 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; } -static lu_mem traverseCclosure (global_State *g, CClosure *cl) { +static int traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); - return sizeCclosure(cl->nupvalues); + return 1 + 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) +** Traverse a Lua closure, marking its prototype and its upvalues. +** (Both can be NULL while closure is being created.) */ -static lu_mem traverseLclosure (global_State *g, LClosure *cl) { +static int traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ + for (i = 0; i < cl->nupvalues; i++) { /* visit 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); - } + markobjectN(g, uv); /* mark upvalue */ } - return sizeLclosure(cl->nupvalues); + return 1 + cl->nupvalues; } -static lu_mem traversethread (global_State *g, lua_State *th) { +/* +** Traverse a thread, marking the elements in the stack up to its top +** and cleaning the rest of the stack in the final traversal. That +** ensures that the entire stack have valid (non-dead) objects. +** Threads have no barriers. In gen. mode, old threads must be visited +** at every cycle, because they might point to young objects. In inc. +** mode, the thread can still be modified before the end of the cycle, +** and therefore it must be visited again in the atomic phase. To ensure +** these visits, threads must return to a gray list if they are not new +** (which can only happen in generational mode) or if the traverse is in +** the propagate phase (which can only happen in incremental mode). +*/ +static int traversethread (global_State *g, lua_State *th) { + UpVal *uv; StkId o = th->stack; + if (isold(th) || g->gcstate == GCSpropagate) + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) return 1; /* stack not completely built yet */ - lua_assert(g->gcstate == GCSinsideatomic || + lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ - markvalue(g, o); - if (g->gcstate == GCSinsideatomic) { /* final traversal? */ + markvalue(g, s2v(o)); + for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) + markobject(g, uv); /* open upvalues cannot be collected */ + if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ - setnilvalue(o); + setnilvalue(s2v(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 if (g->gckind != KGC_EMERGENCY) + else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * th->nci); + return 1 + th->stacksize; } /* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). +** traverse one gray object, turning it to black. */ -static void propagatemark (global_State *g) { - lu_mem size; +static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; - lua_assert(isgray(o)); - gray2black(o); + nw2black(o); + g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_TTABLE: { - Table *h = gco2t(o); - g->gray = h->gclist; /* remove from 'gray' list */ - size = traversetable(g, h); - break; - } - case LUA_TLCL: { - LClosure *cl = gco2lcl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseLclosure(g, cl); - break; - } - case LUA_TCCL: { - CClosure *cl = gco2ccl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseCclosure(g, cl); - break; - } - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - g->gray = th->gclist; /* remove from 'gray' list */ - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(o); - size = traversethread(g, th); - break; - } - case LUA_TPROTO: { - Proto *p = gco2p(o); - g->gray = p->gclist; /* remove from 'gray' list */ - size = traverseproto(g, p); - break; - } - default: lua_assert(0); return; + case LUA_VTABLE: return traversetable(g, gco2t(o)); + case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); + case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); + case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); + case LUA_VPROTO: return traverseproto(g, gco2p(o)); + case LUA_VTHREAD: return traversethread(g, gco2th(o)); + default: lua_assert(0); return 0; } - g->GCmemtrav += size; } -static void propagateall (global_State *g) { - while (g->gray) propagatemark(g); +static lu_mem propagateall (global_State *g) { + lu_mem tot = 0; + while (g->gray) + tot += propagatemark(g); + return tot; } +/* +** Traverse all ephemeron tables propagating marks from keys to values. +** Repeat until it converges, that is, nothing new is marked. 'dir' +** inverts the direction of the traversals, trying to speed up +** convergence on chains in the same table. +** +*/ static void convergeephemerons (global_State *g) { int changed; + int dir = 0; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; - while ((w = next) != NULL) { - next = gco2t(w)->gclist; - if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ + while ((w = next) != NULL) { /* for each ephemeron table */ + Table *h = gco2t(w); + next = h->gclist; /* list is rebuilt during loop */ + nw2black(h); /* out of the list (for now) */ + if (traverseephemeron(g, h, dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } } - } while (changed); + dir = !dir; /* invert direction next time */ + } while (changed); /* repeat until no more changes */ } /* }====================================================== */ @@ -633,19 +716,18 @@ static void convergeephemerons (global_State *g) { /* -** clear entries with unmarked keys from all weaktables in list 'l' up -** to element 'f' +** clear entries with unmarked keys from all weaktables in list 'l' */ -static void clearkeys (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { +static void clearbykeys (global_State *g, GCObject *l) { + for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); + Node *limit = gnodelast(h); + Node *n; for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { - setnilvalue(gval(n)); /* remove value ... */ - } - if (ttisnil(gval(n))) /* is entry empty? */ - removeentry(n); /* remove entry from table */ + if (iscleared(g, gckeyN(n))) /* unmarked key? */ + setempty(gval(n)); /* remove entry */ + if (isempty(gval(n))) /* is entry empty? */ + clearkey(n); /* clear its key */ } } } @@ -655,104 +737,100 @@ static void clearkeys (global_State *g, GCObject *l, GCObject *f) { ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ -static void clearvalues (global_State *g, GCObject *l, GCObject *f) { +static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); unsigned int i; - for (i = 0; i < h->sizearray; i++) { + unsigned int asize = luaH_realasize(h); + for (i = 0; i < asize; i++) { TValue *o = &h->array[i]; - if (iscleared(g, o)) /* value was collected? */ - setnilvalue(o); /* remove value */ + if (iscleared(g, gcvalueN(o))) /* value was collected? */ + setempty(o); /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && iscleared(g, gval(n))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } + if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ + setempty(gval(n)); /* remove entry */ + if (isempty(gval(n))) /* is entry empty? */ + clearkey(n); /* clear its key */ } } } -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 freeupval (lua_State *L, UpVal *uv) { + if (upisopen(uv)) + luaF_unlinkupval(uv); + luaM_free(L, uv); } static void freeobj (lua_State *L, GCObject *o) { switch (o->tt) { - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TLCL: { - freeLclosure(L, gco2lcl(o)); + case LUA_VPROTO: + luaF_freeproto(L, gco2p(o)); break; - } - case LUA_TCCL: { + case LUA_VUPVAL: + freeupval(L, gco2upv(o)); + break; + case LUA_VLCL: + luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + break; + case LUA_VCCL: luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; + case LUA_VTABLE: + luaH_free(L, gco2t(o)); + break; + case LUA_VTHREAD: + luaE_freethread(L, gco2th(o)); + break; + case LUA_VUSERDATA: { + Udata *u = gco2u(o); + luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); + 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: + case LUA_VSHRSTR: luaS_remove(L, gco2ts(o)); /* remove it from hash table */ luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); break; - case LUA_TLNGSTR: { + case LUA_VLNGSTR: luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; - } default: lua_assert(0); } } -static void sweepwholelist (lua_State *L, GCObject **p) { - while (*p != NULL) { - GCObject *curr = *p; - *p = curr->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } -} - - /* -** sweep at most 'count' elements from a list of GCObjects erasing dead +** sweep at most 'countin' elements from a list of GCObjects erasing dead ** 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. +** list is finished. ('*countout' gets the number of elements traversed.) */ -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { +static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, + int *countout) { global_State *g = G(L); - int ow = otherwhite(g) | bitmask(SHAREBIT); /* shared object never dead */ + int ow = otherwhite(g); + int i; int white = luaC_white(g); /* current white */ - while (*p != NULL && count-- > 0) { + for (i = 0; *p != NULL && i < countin; i++) { GCObject *curr = *p; int marked = curr->marked; - if (isdeadm(ow, marked)) { /* is 'curr' dead? */ + if (isshared(curr)) + p = &curr->next; + else if (isdeadm(ow, marked)) { /* is 'curr' dead? */ *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | (marked & bitmask(SHAREBIT)) |white); + curr->marked = cast_byte((marked & ~maskgcbits) | white); p = &curr->next; /* go to next element */ } } + if (countout) + *countout = i; /* number of elements traversed */ return (*p == NULL) ? NULL : p; } @@ -763,7 +841,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; do { - p = sweeplist(L, p, 1); + p = sweeplist(L, p, 1, NULL); } while (p == old); return p; } @@ -778,18 +856,23 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ /* -** If possible, 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; - 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 */ + if (!g->gcemergency) { + if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */ + l_mem olddebt = g->GCdebt; + luaS_resize(L, g->strt.size / 2); + g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ + } } } +/* +** Get the next udata to be finalized from the 'tobefnz' list, and +** link it back into the 'allgc' list. +*/ static GCObject *udata2finalize (global_State *g) { GCObject *o = g->tobefnz; /* get first element */ lua_assert(tofinalize(o)); @@ -799,6 +882,8 @@ static GCObject *udata2finalize (global_State *g) { resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ + else if (getage(o) == G_OLD1) + g->firstold1 = o; /* it is the first OLD1 object in the list */ return o; } @@ -809,51 +894,42 @@ static void dothecall (lua_State *L, void *ud) { } -static void GCTM (lua_State *L, int propagateerrors) { +static void GCTM (lua_State *L) { global_State *g = G(L); const TValue *tm; TValue v; + lua_assert(!g->gcemergency); setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); - if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ + if (!notm(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; int running = g->gcrunning; L->allowhook = 0; /* stop debug hooks during GC metamethod */ g->gcrunning = 0; /* avoid GC steps */ - 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 */ + setobj2s(L, L->top++, tm); /* push finalizer... */ + setobj2s(L, L->top++, &v); /* ... and its argument */ 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? */ - if (status == LUA_ERRRUN) { /* is there an error object? */ - const char *msg = (ttisstring(L->top - 1)) - ? svalue(L->top - 1) - : "no message"; - luaO_pushfstring(L, "error in __gc metamethod (%s)", msg); - status = LUA_ERRGCMM; /* error in __gc metamethod */ - } - luaD_throw(L, status); /* re-throw error */ + if (unlikely(status != LUA_OK)) { /* error while running __gc? */ + luaE_warnerror(L, "__gc metamethod"); + L->top--; /* pops error object */ } } } /* -** call a few (up to 'g->gcfinnum') finalizers +** Call a few finalizers */ -static int runafewfinalizers (lua_State *L) { +static int runafewfinalizers (lua_State *L, int n) { 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 */ + int i; + for (i = 0; i < n && g->tobefnz; i++) + GCTM(L); /* call one finalizer */ return i; } @@ -864,7 +940,7 @@ static int runafewfinalizers (lua_State *L) { static void callallpendingfinalizers (lua_State *L) { global_State *g = G(L); while (g->tobefnz) - GCTM(L, 0); + GCTM(L); } @@ -879,18 +955,23 @@ static GCObject **findlast (GCObject **p) { /* -** move all unreachable objects (or 'all' objects) that need -** finalization from list 'finobj' to list 'tobefnz' (to be finalized) +** Move all unreachable objects (or 'all' objects) that need +** finalization from list 'finobj' to list 'tobefnz' (to be finalized). +** (Note that objects after 'finobjold1' cannot be white, so they +** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, +** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != NULL) { /* traverse all finalizable objects */ + while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ else { + if (curr == g->finobjsur) /* removing 'finobjsur'? */ + g->finobjsur = curr->next; /* correct it */ *p = curr->next; /* remove 'curr' from 'finobj' list */ curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; @@ -900,6 +981,27 @@ static void separatetobefnz (global_State *g, int all) { } +/* +** If pointer 'p' points to 'o', move it to the next element. +*/ +static void checkpointer (GCObject **p, GCObject *o) { + if (o == *p) + *p = o->next; +} + + +/* +** Correct pointers to objects inside 'allgc' list when +** object 'o' is being removed from the list. +*/ +static void correctpointers (global_State *g, GCObject *o) { + checkpointer(&g->survival, o); + checkpointer(&g->old1, o); + checkpointer(&g->reallyold, o); + checkpointer(&g->firstold1, o); +} + + /* ** if object 'o' has a finalizer, remove it from 'allgc' list (must ** search the list to find it) and link it in 'finobj' list. @@ -916,6 +1018,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } + else + correctpointers(g, o); /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ @@ -928,6 +1032,416 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* }====================================================== */ +/* +** {====================================================== +** Generational Collector +** ======================================================= +*/ + +static void setpause (global_State *g); + + +/* +** Sweep a list of objects to enter generational mode. Deletes dead +** objects and turns the non dead to old. All non-dead threads---which +** are now old---must be in a gray list. Everything else is not in a +** gray list. Open upvalues are also kept gray. +*/ +static void sweep2old (lua_State *L, GCObject **p) { + GCObject *curr; + global_State *g = G(L); + while ((curr = *p) != NULL) { + if (isshared(curr)) + p = &curr->next; /* go to next element */ + else if (iswhite(curr)) { /* is 'curr' dead? */ + lua_assert(isdead(g, curr)); + *p = curr->next; /* remove 'curr' from list */ + freeobj(L, curr); /* erase 'curr' */ + } + else { /* all surviving objects become old */ + setage(curr, G_OLD); + if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ + lua_State *th = gco2th(curr); + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + } + else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) + set2gray(curr); /* open upvalues are always gray */ + else /* everything else is black */ + nw2black(curr); + p = &curr->next; /* go to next element */ + } + } +} + + +/* +** Sweep for generational mode. Delete dead objects. (Because the +** collection is not incremental, there are no "new white" objects +** during the sweep. So, any white object must be dead.) For +** non-dead objects, advance their ages and clear the color of +** new objects. (Old objects keep their colors.) +** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced +** here, because these old-generation objects are usually not swept +** here. They will all be advanced in 'correctgraylist'. That function +** will also remove objects turned white here from any gray list. +*/ +static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, + GCObject *limit, GCObject **pfirstold1) { + static const lu_byte nextage[] = { + G_SURVIVAL, /* from G_NEW */ + G_OLD1, /* from G_SURVIVAL */ + G_OLD1, /* from G_OLD0 */ + G_OLD, /* from G_OLD1 */ + G_OLD, /* from G_OLD (do not change) */ + G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ + G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ + }; + int white = luaC_white(g); + GCObject *curr; + while ((curr = *p) != limit) { + if (isshared(curr)) + p = &curr->next; /* go to next element */ + else if (iswhite(curr)) { /* is 'curr' dead? */ + lua_assert(!isold(curr) && isdead(g, curr)); + *p = curr->next; /* remove 'curr' from list */ + freeobj(L, curr); /* erase 'curr' */ + } + else { /* correct mark and age */ + if (getage(curr) == G_NEW) { /* new objects go back to white */ + int marked = curr->marked & ~maskgcbits; /* erase GC bits */ + curr->marked = cast_byte(marked | G_SURVIVAL | white); + } + else { /* all other objects will be old, and so keep their color */ + setage(curr, nextage[getage(curr)]); + if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) + *pfirstold1 = curr; /* first OLD1 object in the list */ + } + p = &curr->next; /* go to next element */ + } + } + return p; +} + + +/* +** Traverse a list making all its elements white and clearing their +** age. In incremental mode, all objects are 'new' all the time, +** except for fixed strings (which are always old). +*/ +static void whitelist (global_State *g, GCObject *p) { + int white = luaC_white(g); + for (; p != NULL; p = p->next) + p->marked = cast_byte((p->marked & ~maskgcbits) | white); +} + + +/* +** Correct a list of gray objects. Return pointer to where rest of the +** list should be linked. +** Because this correction is done after sweeping, young objects might +** be turned white and still be in the list. They are only removed. +** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; +** Non-white threads also remain on the list; 'TOUCHED2' objects become +** regular old; they and anything else are removed from the list. +*/ +static GCObject **correctgraylist (GCObject **p) { + GCObject *curr; + while ((curr = *p) != NULL) { + GCObject **next = getgclist(curr); + if (iswhite(curr)) + goto remove; /* remove all white objects */ + else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(curr)); + nw2black(curr); /* make it black, for next barrier */ + changeage(curr, G_TOUCHED1, G_TOUCHED2); + goto remain; /* keep it in the list and go to next element */ + } + else if (curr->tt == LUA_VTHREAD) { + lua_assert(isgray(curr)); + goto remain; /* keep non-white threads on the list */ + } + else { /* everything else is removed */ + lua_assert(isold(curr)); /* young objects should be white here */ + if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ + changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ + nw2black(curr); /* make object black (to be removed) */ + goto remove; + } + remove: *p = *next; continue; + remain: p = next; continue; + } + return p; +} + + +/* +** Correct all gray lists, coalescing them into 'grayagain'. +*/ +static void correctgraylists (global_State *g) { + GCObject **list = correctgraylist(&g->grayagain); + *list = g->weak; g->weak = NULL; + list = correctgraylist(list); + *list = g->allweak; g->allweak = NULL; + list = correctgraylist(list); + *list = g->ephemeron; g->ephemeron = NULL; + correctgraylist(list); +} + + +/* +** Mark black 'OLD1' objects when starting a new young collection. +** Gray objects are already in some gray list, and so will be visited +** in the atomic step. +*/ +static void markold (global_State *g, GCObject *from, GCObject *to) { + GCObject *p; + for (p = from; p != to; p = p->next) { + if (getage(p) == G_OLD1) { + lua_assert(!iswhite(p)); + changeage(p, G_OLD1, G_OLD); /* now they are old */ + if (isblack(p)) + reallymarkobject(g, p); + } + } +} + + +/* +** Finish a young-generation collection. +*/ +static void finishgencycle (lua_State *L, global_State *g) { + correctgraylists(g); + checkSizes(L, g); + g->gcstate = GCSpropagate; /* skip restart */ + if (!g->gcemergency) + callallpendingfinalizers(L); +} + + +/* +** Does a young collection. First, mark 'OLD1' objects. Then does the +** atomic step. Then, sweep all lists and advance pointers. Finally, +** finish the collection. +*/ +static void youngcollection (lua_State *L, global_State *g) { + GCObject **psurvival; /* to point to first non-dead survival object */ + GCObject *dummy; /* dummy out parameter to 'sweepgen' */ + lua_assert(g->gcstate == GCSpropagate); + if (g->firstold1) { /* are there regular OLD1 objects? */ + markold(g, g->firstold1, g->reallyold); /* mark them */ + g->firstold1 = NULL; /* no more OLD1 objects (for now) */ + } + markold(g, g->finobj, g->finobjrold); + markold(g, g->tobefnz, NULL); + atomic(L); + + /* sweep nursery and get a pointer to its last live element */ + g->gcstate = GCSswpallgc; + psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->old1, &g->firstold1); + g->reallyold = g->old1; + g->old1 = *psurvival; /* 'survival' survivals are old now */ + g->survival = g->allgc; /* all news are survivals */ + + /* repeat for 'finobj' lists */ + dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->finobjold1, &dummy); + g->finobjrold = g->finobjold1; + g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ + g->finobjsur = g->finobj; /* all news are survivals */ + + sweepgen(L, g, &g->tobefnz, NULL, &dummy); + finishgencycle(L, g); +} + + +/* +** Clears all gray lists, sweeps objects, and prepare sublists to enter +** generational mode. The sweeps remove dead objects and turn all +** surviving objects to old. Threads go back to 'grayagain'; everything +** else is turned black (not in any gray list). +*/ +static void atomic2gen (lua_State *L, global_State *g) { + cleargraylists(g); + /* sweep all elements making them old */ + g->gcstate = GCSswpallgc; + sweep2old(L, &g->allgc); + /* everything alive now is old */ + g->reallyold = g->old1 = g->survival = g->allgc; + g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ + + /* repeat for 'finobj' lists */ + sweep2old(L, &g->finobj); + g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; + + sweep2old(L, &g->tobefnz); + + g->gckind = KGC_GEN; + g->lastatomic = 0; + g->GCestimate = gettotalbytes(g); /* base for memory control */ + finishgencycle(L, g); +} + + +/* +** Enter generational mode. Must go until the end of an atomic cycle +** to ensure that all objects are correctly marked and weak tables +** are cleared. Then, turn all objects into old and finishes the +** collection. +*/ +static lu_mem entergen (lua_State *L, global_State *g) { + lu_mem numobjs; + luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + numobjs = atomic(L); /* propagates all and then do the atomic stuff */ + atomic2gen(L, g); + return numobjs; +} + + +/* +** Enter incremental mode. Turn all objects white, make all +** intermediate lists point to NULL (to avoid invalid pointers), +** and go to the pause state. +*/ +static void enterinc (global_State *g) { + whitelist(g, g->allgc); + g->reallyold = g->old1 = g->survival = NULL; + whitelist(g, g->finobj); + whitelist(g, g->tobefnz); + g->finobjrold = g->finobjold1 = g->finobjsur = NULL; + g->gcstate = GCSpause; + g->gckind = KGC_INC; + g->lastatomic = 0; +} + + +/* +** Change collector mode to 'newmode'. +*/ +void luaC_changemode (lua_State *L, int newmode) { + global_State *g = G(L); + if (newmode != g->gckind) { + if (newmode == KGC_GEN) /* entering generational mode? */ + entergen(L, g); + else + enterinc(g); /* entering incremental mode */ + } + g->lastatomic = 0; +} + + +/* +** Does a full collection in generational mode. +*/ +static lu_mem fullgen (lua_State *L, global_State *g) { + enterinc(g); + return entergen(L, g); +} + + +/* +** Set debt for the next minor collection, which will happen when +** memory grows 'genminormul'%. +*/ +static void setminordebt (global_State *g) { + luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); +} + + +/* +** Does a major collection after last collection was a "bad collection". +** +** When the program is building a big structure, it allocates lots of +** memory but generates very little garbage. In those scenarios, +** the generational mode just wastes time doing small collections, and +** major collections are frequently what we call a "bad collection", a +** collection that frees too few objects. To avoid the cost of switching +** between generational mode and the incremental mode needed for full +** (major) collections, the collector tries to stay in incremental mode +** after a bad collection, and to switch back to generational mode only +** after a "good" collection (one that traverses less than 9/8 objects +** of the previous one). +** The collector must choose whether to stay in incremental mode or to +** switch back to generational mode before sweeping. At this point, it +** does not know the real memory in use, so it cannot use memory to +** decide whether to return to generational mode. Instead, it uses the +** number of objects traversed (returned by 'atomic') as a proxy. The +** field 'g->lastatomic' keeps this count from the last collection. +** ('g->lastatomic != 0' also means that the last collection was bad.) +*/ +static void stepgenfull (lua_State *L, global_State *g) { + lu_mem newatomic; /* count of traversed objects */ + lu_mem lastatomic = g->lastatomic; /* count from last collection */ + if (g->gckind == KGC_GEN) /* still in generational mode? */ + enterinc(g); /* enter incremental mode */ + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + newatomic = atomic(L); /* mark everybody */ + if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ + atomic2gen(L, g); /* return to generational mode */ + setminordebt(g); + } + else { /* another bad collection; stay in incremental mode */ + g->GCestimate = gettotalbytes(g); /* first estimate */; + entersweep(L); + luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + setpause(g); + g->lastatomic = newatomic; + } +} + + +/* +** Does a generational "step". +** Usually, this means doing a minor collection and setting the debt to +** make another collection when memory grows 'genminormul'% larger. +** +** However, there are exceptions. If memory grows 'genmajormul'% +** larger than it was at the end of the last major collection (kept +** in 'g->GCestimate'), the function does a major collection. At the +** end, it checks whether the major collection was able to free a +** decent amount of memory (at least half the growth in memory since +** previous major collection). If so, the collector keeps its state, +** and the next collection will probably be minor again. Otherwise, +** we have what we call a "bad collection". In that case, set the field +** 'g->lastatomic' to signal that fact, so that the next collection will +** go to 'stepgenfull'. +** +** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; +** in that case, do a minor collection. +*/ +static void genstep (lua_State *L, global_State *g) { + if (g->lastatomic != 0) /* last collection was a bad one? */ + stepgenfull(L, g); /* do a full step */ + else { + lu_mem majorbase = g->GCestimate; /* memory after last major collection */ + lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { + lu_mem numobjs = fullgen(L, g); /* do a major collection */ + if (gettotalbytes(g) < majorbase + (majorinc / 2)) { + /* collected at least half of memory growth since last major + collection; keep doing minor collections */ + setminordebt(g); + } + else { /* bad collection */ + g->lastatomic = numobjs; /* signal that last collection was bad */ + setpause(g); /* do a long wait for next (major) collection */ + } + } + else { /* regular case; do a minor collection */ + youngcollection(L, g); + setminordebt(g); + g->GCestimate = majorbase; /* preserve base value */ + } + } + lua_assert(isdecGCmodegen(g)); +} + +/* }====================================================== */ + /* ** {====================================================== @@ -937,26 +1451,28 @@ 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. (Division by 'estimate' -** should be OK: it cannot be zero (because Lua cannot even start with -** less than PAUSEADJ bytes). +** Set the "time" to wait before starting a new GC cycle; cycle will +** start when memory use hits the threshold of ('estimate' * pause / +** PAUSEADJ). (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 threshold, debt; + int pause = getgcparam(g->gcpause); l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ lua_assert(estimate > 0); - threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * g->gcpause /* no overflow */ + threshold = (pause < MAX_LMEM / estimate) /* overflow? */ + ? estimate * pause /* no overflow */ : MAX_LMEM; /* overflow; truncate to maximum */ debt = gettotalbytes(g) - threshold; + if (debt > 0) debt = 0; luaE_setdebt(g, debt); } /* ** Enter first sweep phase. -** The call to 'sweeplist' tries to make pointer point to an object +** The call to 'sweeptolive' makes the 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. @@ -965,85 +1481,97 @@ static void entersweep (lua_State *L) { global_State *g = G(L); g->gcstate = GCSswpallgc; lua_assert(g->sweepgc == NULL); - g->sweepgc = sweeplist(L, &g->allgc, 1); + g->sweepgc = sweeptolive(L, &g->allgc); +} + + +/* +** Delete all objects in list 'p' until (but not including) object +** 'limit'. +*/ +static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { + while (p != limit) { + GCObject *next = p->next; + freeobj(L, p); + p = next; + } } +/* +** Call all finalizers of the objects in the given Lua state, and +** then free all objects, except for the main thread. +*/ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); + luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - lua_assert(g->tobefnz == NULL); - g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ - g->gckind = KGC_NORMAL; - sweepwholelist(L, &g->finobj); - sweepwholelist(L, &g->allgc); - sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ + deletelist(L, g->allgc, obj2gco(g->mainthread)); + deletelist(L, g->finobj, NULL); + deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } -static l_mem atomic (lua_State *L) { +static lu_mem atomic (lua_State *L) { global_State *g = G(L); - l_mem work; + lu_mem work = 0; GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ + g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); lua_assert(!iswhite(g->mainthread)); - g->gcstate = GCSinsideatomic; - g->GCmemtrav = 0; /* start counting work */ + g->gcstate = GCSatomic; markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ + work += propagateall(g); /* empties 'gray' list */ /* remark occasional upvalues of (maybe) dead threads */ - remarkupvals(g); - propagateall(g); /* propagate changes */ - work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ + work += remarkupvals(g); + work += propagateall(g); /* propagate changes */ g->gray = grayagain; - propagateall(g); /* traverse 'grayagain' list */ - g->GCmemtrav = 0; /* restart counting */ + work += propagateall(g); /* traverse 'grayagain' list */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ - clearvalues(g, g->weak, NULL); - clearvalues(g, g->allweak, NULL); + clearbyvalues(g, g->weak, NULL); + clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; - work += g->GCmemtrav; /* stop counting (objects being 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 'resurrection' */ - g->GCmemtrav = 0; /* restart counting */ + work += markbeingfnz(g); /* mark objects that will be finalized */ + work += propagateall(g); /* remark, to propagate 'resurrection' */ 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 */ + clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ + clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ /* clear values from resurrected weak tables */ - clearvalues(g, g->weak, origweak); - clearvalues(g, g->allweak, origall); + clearbyvalues(g, g->weak, origweak); + clearbyvalues(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' */ + lua_assert(g->gray == NULL); + return work; /* estimate of slots marked by 'atomic' */ } -static lu_mem sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { +static int 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); + int count; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count); g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - if (g->sweepgc) /* is there still something to sweep? */ - return (GCSWEEPMAX * GCSWEEPCOST); + return count; + } + else { /* enter next state */ + g->gcstate = nextstate; + g->sweepgc = nextlist; + return 0; /* no work done */ } - /* else enter next state */ - g->gcstate = nextstate; - g->sweepgc = nextlist; - return 0; } @@ -1051,23 +1579,20 @@ static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - g->GCmemtrav = g->strt.size * sizeof(GCObject*); restartcollection(g); g->gcstate = GCSpropagate; - return g->GCmemtrav; + return 1; } case GCSpropagate: { - 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 */ + if (g->gray == NULL) { /* no more gray objects? */ + g->gcstate = GCSenteratomic; /* finish propagate phase */ + return 0; + } + else + return propagatemark(g); /* traverse one gray object */ } - case GCSatomic: { - lu_mem work; - propagateall(g); /* make sure gray list is empty */ - work = atomic(L); /* work is what was traversed by 'atomic' */ + case GCSenteratomic: { + lu_mem work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; return work; @@ -1082,15 +1607,14 @@ static lu_mem singlestep (lua_State *L) { 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); + if (g->tobefnz && !g->gcemergency) { + int n = runafewfinalizers(L, GCFINMAX); + return n * GCFINALIZECOST; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ @@ -1114,71 +1638,81 @@ void luaC_runtilstate (lua_State *L, int statesmask) { /* -** get GC debt and convert it from Kb to 'work units' (avoid zero debt -** and overflows) +** Performs a basic incremental step. The debt and step size are +** converted from bytes to "units of work"; then the function loops +** running single steps until adding that many units of work or +** finishing a cycle (pause state). Finally, it sets the debt that +** controls when next step will be performed. */ -static l_mem getdebt (global_State *g) { - l_mem debt = g->GCdebt; - int stepmul = g->gcstepmul; - if (debt <= 0) return 0; /* minimal debt */ +static void incstep (lua_State *L, global_State *g) { + int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ + l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; + l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) + ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul + : MAX_LMEM; /* overflow; keep maximum value */ + do { /* repeat until pause or enough "credit" (negative debt) */ + lu_mem work = singlestep(L); /* perform one single step */ + debt -= work; + } while (debt > -stepsize && g->gcstate != GCSpause); + if (g->gcstate == GCSpause) + setpause(g); /* pause until next cycle */ else { - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - return debt; + debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ + luaE_setdebt(g, debt); } } /* -** performs a basic GC step when collector is running +** performs a basic GC step if collector is running */ void luaC_step (lua_State *L) { global_State *g = G(L); - 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); + lua_assert(!g->gcemergency); + if (g->gcrunning) { /* running? */ + if(isdecGCmodegen(g)) + genstep(L, g); + else + incstep(L, g); } } /* -** 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). +** Perform a full collection in incremental mode. ** 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); - lua_assert(g->gckind == KGC_NORMAL); - if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ - if (keepinvariant(g)) { /* black objects? */ +static void fullinc (lua_State *L, global_State *g) { + 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(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); } + +/* +** 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). +*/ +void luaC_fullgc (lua_State *L, int isemergency) { + global_State *g = G(L); + lua_assert(!g->gcemergency); + g->gcemergency = isemergency; /* set flag */ + if (g->gckind == KGC_INC) + fullinc(L, g); + else + fullgen(L, g); + g->gcemergency = 0; +} + /* }====================================================== */ diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 55467ef38..eb00bcf8f 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lgc.h $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -12,38 +12,31 @@ #include "lstate.h" /* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). +** Collectable objects may have one of three colors: white, which means +** the object is not marked; gray, which means the object is marked, but +** its references may be not marked; and black, which means that the +** object and all its references are marked. The main invariant of the +** garbage collector, while marking objects, is that a black object can +** never point to a white one. Moreover, any gray object must be in a +** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it +** can be visited again before finishing the collection cycle. (Open +** upvalues are an exception to this rule.) These lists have no meaning +** when the invariant is not being enforced (e.g., sweep phase). */ - -/* how much to allocate before next GC step */ -#if !defined(GCSTEPSIZE) -/* ~100 small strings */ -#define GCSTEPSIZE (cast_int(100 * sizeof(TString))) -#endif - - /* ** Possible states of the Garbage Collector */ #define GCSpropagate 0 -#define GCSatomic 1 -#define GCSswpallgc 2 -#define GCSswpfinobj 3 -#define GCSswptobefnz 4 -#define GCSswpend 5 -#define GCScallfin 6 -#define GCSpause 7 +#define GCSenteratomic 1 +#define GCSatomic 2 +#define GCSswpallgc 3 +#define GCSswpfinobj 4 +#define GCSswptobefnz 5 +#define GCSswpend 6 +#define GCScallfin 7 +#define GCSpause 8 #define issweepphase(g) \ @@ -64,7 +57,7 @@ /* ** some useful bit tricks */ -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) +#define resetbits(x,m) ((x) &= cast_byte(~(m))) #define setbits(x,m) ((x) |= (m)) #define testbits(x,m) ((x) & (m)) #define bitmask(b) (1<<(b)) @@ -74,13 +67,19 @@ #define testbit(x,b) testbits(x, bitmask(b)) -/* 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 marked for finalization */ -#define SHAREBIT 4 /* object shared from other state */ -/* bit 7 is currently used by tests (luaL_checkmemory) */ +/* +** Layout for bit use in 'marked' field. First three bits are +** used for object "age" in generational mode. Last bit is used +** by tests. +*/ +#define WHITE0BIT 3 /* object is white (type 0) */ +#define WHITE1BIT 4 /* object is white (type 1) */ +#define BLACKBIT 5 /* object is black */ +#define FINALIZEDBIT 6 /* object has been marked for finalization */ + +#define TESTBIT 7 + + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) @@ -92,17 +91,66 @@ #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) -#define isshared(x) testbit((x)->marked, SHAREBIT) -#define makeshared(x) l_setbit((x)->marked, SHAREBIT) #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) -#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g) | bitmask(SHAREBIT), (v)->marked) +#define isdeadm(ow,m) ((m) & (ow)) +#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->marked, BLACKBIT) +#define nw2black(x) \ + check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) + +#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) + + +/* object age in generational mode */ +#define G_NEW 0 /* created in current cycle */ +#define G_SURVIVAL 1 /* created in previous cycle */ +#define G_OLD0 2 /* marked old by frw. barrier in this cycle */ +#define G_OLD1 3 /* first full cycle as old */ +#define G_OLD 4 /* really old object (not to be visited) */ +#define G_TOUCHED1 5 /* old object touched this cycle */ +#define G_TOUCHED2 6 /* old object touched in previous cycle */ +#define G_SHARED 7 /* object is shared */ + +#define AGEBITS 7 /* all age bits (111) */ + +#define getage(o) ((o)->marked & AGEBITS) +#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) +#define isold(o) (getage(o) > G_SURVIVAL) -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) +#define isshared(x) (getage(x) == G_SHARED) +#define makeshared(x) setage(x, G_SHARED) +#define changeage(o,f,t) \ + check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) + + +/* Default Values for GC parameters */ +#define LUAI_GENMAJORMUL 100 +#define LUAI_GENMINORMUL 20 + +/* wait memory to double before starting new cycle */ +#define LUAI_GCPAUSE 200 + +/* +** some gc parameters are stored divided by 4 to allow a maximum value +** up to 1023 in a 'lu_byte'. +*/ +#define getgcparam(p) ((p) * 4) +#define setgcparam(p,v) ((p) = (v) / 4) + +#define LUAI_GCMUL 100 + +/* how much to allocate before next GC step (log2) */ +#define LUAI_GCSTEPSIZE 13 /* 8 KB */ + + +/* +** Check whether the declared GC mode is generational. While in +** generational mode, the collector can go temporarily to incremental +** mode to improve performance. This is signaled by 'g->lastatomic != 0'. +*/ +#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' @@ -130,10 +178,6 @@ (isblack(p) && iswhite(o) && !isshared(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#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); LUAI_FUNC void luaC_step (lua_State *L); @@ -141,10 +185,9 @@ 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); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); -LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); #endif diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index 480da52c7..69808f84f 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: linit.c $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ @@ -40,7 +40,7 @@ ** program */ static const luaL_Reg loadedlibs[] = { - {"_G", luaopen_base}, + {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, @@ -50,9 +50,6 @@ static const luaL_Reg loadedlibs[] = { {LUA_MATHLIBNAME, luaopen_math}, {LUA_UTF8LIBNAME, luaopen_utf8}, {LUA_DBLIBNAME, luaopen_debug}, -#if defined(LUA_COMPAT_BITLIB) - {LUA_BITLIBNAME, luaopen_bit32}, -#endif {NULL, NULL} }; diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 8a9e75cd0..60ab1bfab 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: liolib.c $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -39,7 +39,7 @@ /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ static int l_checkmode (const char *mode) { return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && - (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ + (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ } @@ -52,6 +52,12 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ +#if !defined(l_checkmodep) +/* By default, Lua accepts only "r" or "w" as mode */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') +#endif + + #if !defined(l_popen) /* { */ #if defined(LUA_USE_POSIX) /* { */ @@ -68,7 +74,7 @@ static int l_checkmode (const char *mode) { /* ISO C definitions */ #define l_popen(L,c,m) \ - ((void)((void)c, m), \ + ((void)c, (void)m, \ luaL_error(L, "'popen' not supported"), \ (FILE*)0) #define l_pclose(L,file) ((void)L, (void)file, -1) @@ -133,6 +139,7 @@ static int l_checkmode (const char *mode) { /* }====================================================== */ + #define IO_PREFIX "_IO_" #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) #define IO_INPUT (IO_PREFIX "input") @@ -152,7 +159,7 @@ static int io_type (lua_State *L) { luaL_checkany(L, 1); p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); if (p == NULL) - lua_pushnil(L); /* not a file */ + luaL_pushfail(L); /* not a file */ else if (isclosed(p)) lua_pushliteral(L, "closed file"); else @@ -186,7 +193,7 @@ static FILE *tofile (lua_State *L) { ** handle is in a consistent state. */ static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); + LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); p->closef = NULL; /* mark file handle as 'closed' */ luaL_setmetatable(L, LUA_FILEHANDLE); return p; @@ -214,7 +221,7 @@ static int f_close (lua_State *L) { static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ + lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ return f_close(L); } @@ -269,6 +276,7 @@ static int io_open (lua_State *L) { */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); + errno = 0; return luaL_execresult(L, l_pclose(L, p->f)); } @@ -277,6 +285,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); + luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; @@ -295,7 +304,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 + IOPREF_LEN); + luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -336,12 +345,22 @@ static int io_readline (lua_State *L); */ #define MAXARGLINE 250 +/* +** Auxiliary function to create the iteration function for 'lines'. +** The iteration function is a closure over 'io_readline', with +** the following upvalues: +** 1) The file being read (first value in the stack) +** 2) the number of arguments to read +** 3) a boolean, true iff file has to be closed when finished ('toclose') +** *) a variable number of format arguments (rest of the stack) +*/ 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_pushvalue(L, 1); /* file */ 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 */ + lua_rotate(L, 2, 3); /* move the three values to their positions */ lua_pushcclosure(L, io_readline, 3 + n); } @@ -353,6 +372,11 @@ static int f_lines (lua_State *L) { } +/* +** Return an iteration function for 'io.lines'. If file has to be +** closed, also returns the file itself as a second result (to be +** closed as the state at the exit of a generic for). +*/ static int io_lines (lua_State *L) { int toclose; if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ @@ -368,8 +392,15 @@ static int io_lines (lua_State *L) { lua_replace(L, 1); /* put file at index 1 */ toclose = 1; /* close it after iteration */ } - aux_lines(L, toclose); - return 1; + aux_lines(L, toclose); /* push iteration function */ + if (toclose) { + lua_pushnil(L); /* state */ + lua_pushnil(L); /* control */ + lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ + return 4; + } + else + return 1; } @@ -435,7 +466,7 @@ static int readdigits (RN *rn, int hex) { /* ** 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 +** correct and to convert it to a Lua number. */ static int read_number (lua_State *L, FILE *f) { RN rn; @@ -447,7 +478,7 @@ static int read_number (lua_State *L, FILE *f) { 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 */ + test2(&rn, "-+"); /* optional sign */ if (test2(&rn, "00")) { if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ else count = 1; /* count initial '0' as a valid digit */ @@ -456,7 +487,7 @@ static int read_number (lua_State *L, FILE *f) { 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 */ + test2(&rn, "-+"); /* exponent sign */ readdigits(&rn, 0); /* exponent digits */ } ungetc(rn.c, rn.f); /* unread look-ahead char */ @@ -481,17 +512,17 @@ 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'; + int c; luaL_buffinit(L, &b); - while (c != EOF && c != '\n') { /* repeat until end of line */ - char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ + do { /* may need to read several chunks to get whole line */ + char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ 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; + buff[i++] = c; /* read up to end of line or buffer limit */ l_unlockfile(f); luaL_addsize(&b, i); - } + } while (c != EOF && c != '\n'); /* repeat until end of line */ if (!chop && c == '\n') /* want a newline and have one? */ luaL_addchar(&b, c); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ @@ -528,14 +559,14 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; - int success; - int n; + int n, success; clearerr(f); if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); - n = first+1; /* to return 1 result */ + n = first + 1; /* to return 1 result */ } - else { /* ensure stack space for all results and for auxlib's buffer */ + else { + /* ensure stack space for all results and for auxlib's buffer */ luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); success = 1; for (n = first; nargs-- && success; n++) { @@ -570,7 +601,7 @@ static int g_read (lua_State *L, FILE *f, int first) { return luaL_fileresult(L, 0, NULL); if (!success) { lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ + luaL_pushfail(L); /* push nil instead */ } return n - first; } @@ -586,6 +617,9 @@ static int f_read (lua_State *L) { } +/* +** Iteration function for 'lines'. +*/ static int io_readline (lua_State *L) { LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); int i; @@ -600,14 +634,14 @@ static int io_readline (lua_State *L) { lua_assert(n > 0); /* should return at least a nil */ if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ - else { /* first result is nil: EOF or error */ + else { /* first result is false: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(L, "%s", lua_tostring(L, -n + 1)); } if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); + lua_settop(L, 0); /* clear stack */ + lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ aux_close(L); /* close it */ } return 0; @@ -716,26 +750,37 @@ static const luaL_Reg iolib[] = { /* ** methods for file handles */ -static const luaL_Reg flib[] = { - {"close", f_close}, - {"flush", f_flush}, - {"lines", f_lines}, +static const luaL_Reg meth[] = { {"read", f_read}, + {"write", f_write}, + {"lines", f_lines}, + {"flush", f_flush}, {"seek", f_seek}, + {"close", f_close}, {"setvbuf", f_setvbuf}, - {"write", f_write}, + {NULL, NULL} +}; + + +/* +** metamethods for file handles +*/ +static const luaL_Reg metameth[] = { + {"__index", NULL}, /* place holder */ {"__gc", f_gc}, + {"__close", f_gc}, {"__tostring", f_tostring}, {NULL, NULL} }; static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ + luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ + luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ + luaL_newlibtable(L, meth); /* create method table */ + luaL_setfuncs(L, meth, 0); /* add file methods to method table */ + lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ + lua_pop(L, 1); /* pop metatable */ } @@ -745,7 +790,7 @@ static void createmeta (lua_State *L) { static int io_noclose (lua_State *L) { LStream *p = tolstream(L); p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); + luaL_pushfail(L); lua_pushliteral(L, "cannot close standard file"); return 2; } diff --git a/3rd/lua/ljumptab.h b/3rd/lua/ljumptab.h new file mode 100644 index 000000000..8306f250c --- /dev/null +++ b/3rd/lua/ljumptab.h @@ -0,0 +1,112 @@ +/* +** $Id: ljumptab.h $ +** Jump Table for the Lua interpreter +** See Copyright Notice in lua.h +*/ + + +#undef vmdispatch +#undef vmcase +#undef vmbreak + +#define vmdispatch(x) goto *disptab[x]; + +#define vmcase(l) L_##l: + +#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); + + +static const void *const disptab[NUM_OPCODES] = { + +#if 0 +** you can update the following list with this command: +** +** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h +** +#endif + +&&L_OP_MOVE, +&&L_OP_LOADI, +&&L_OP_LOADF, +&&L_OP_LOADK, +&&L_OP_LOADKX, +&&L_OP_LOADFALSE, +&&L_OP_LFALSESKIP, +&&L_OP_LOADTRUE, +&&L_OP_LOADNIL, +&&L_OP_GETUPVAL, +&&L_OP_SETUPVAL, +&&L_OP_GETTABUP, +&&L_OP_GETTABLE, +&&L_OP_GETI, +&&L_OP_GETFIELD, +&&L_OP_SETTABUP, +&&L_OP_SETTABLE, +&&L_OP_SETI, +&&L_OP_SETFIELD, +&&L_OP_NEWTABLE, +&&L_OP_SELF, +&&L_OP_ADDI, +&&L_OP_ADDK, +&&L_OP_SUBK, +&&L_OP_MULK, +&&L_OP_MODK, +&&L_OP_POWK, +&&L_OP_DIVK, +&&L_OP_IDIVK, +&&L_OP_BANDK, +&&L_OP_BORK, +&&L_OP_BXORK, +&&L_OP_SHRI, +&&L_OP_SHLI, +&&L_OP_ADD, +&&L_OP_SUB, +&&L_OP_MUL, +&&L_OP_MOD, +&&L_OP_POW, +&&L_OP_DIV, +&&L_OP_IDIV, +&&L_OP_BAND, +&&L_OP_BOR, +&&L_OP_BXOR, +&&L_OP_SHL, +&&L_OP_SHR, +&&L_OP_MMBIN, +&&L_OP_MMBINI, +&&L_OP_MMBINK, +&&L_OP_UNM, +&&L_OP_BNOT, +&&L_OP_NOT, +&&L_OP_LEN, +&&L_OP_CONCAT, +&&L_OP_CLOSE, +&&L_OP_TBC, +&&L_OP_JMP, +&&L_OP_EQ, +&&L_OP_LT, +&&L_OP_LE, +&&L_OP_EQK, +&&L_OP_EQI, +&&L_OP_LTI, +&&L_OP_LEI, +&&L_OP_GTI, +&&L_OP_GEI, +&&L_OP_TEST, +&&L_OP_TESTSET, +&&L_OP_CALL, +&&L_OP_TAILCALL, +&&L_OP_RETURN, +&&L_OP_RETURN0, +&&L_OP_RETURN1, +&&L_OP_FORLOOP, +&&L_OP_FORPREP, +&&L_OP_TFORPREP, +&&L_OP_TFORCALL, +&&L_OP_TFORLOOP, +&&L_OP_SETLIST, +&&L_OP_CLOSURE, +&&L_OP_VARARG, +&&L_OP_VARARGPREP, +&&L_OP_EXTRAARG + +}; diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 66fd411ba..3d6b2b97a 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.96.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: llex.c $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -29,7 +29,7 @@ -#define next(ls) (ls->current = zgetc(ls->z)) +#define next(ls) (ls->current = zgetc(ls->z)) @@ -63,7 +63,7 @@ static void save (LexState *ls, int c) { newsize = luaZ_sizebuffer(b) * 2; luaZ_resizebuffer(ls->L, b, newsize); } - b->buffer[luaZ_bufflen(b)++] = cast(char, c); + b->buffer[luaZ_bufflen(b)++] = cast_char(c); } @@ -81,8 +81,10 @@ void luaX_init (lua_State *L) { const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast_uchar(token)); - return luaO_pushfstring(ls->L, "'%c'", token); + if (lisprint(token)) + return luaO_pushfstring(ls->L, "'%c'", token); + else /* control character */ + return luaO_pushfstring(ls->L, "'<\\%d>'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; @@ -129,15 +131,15 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 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->h, L->top - 1); - if (ttisnil(o)) { /* not in use yet? */ + o = luaH_set(L, ls->h, s2v(L->top - 1)); + if (isempty(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 */ + table is not a metatable, so it does not need to invalidate cache */ + setbtvalue(o); /* t[string] = true */ luaC_checkGC(L); } else { /* string already present */ - ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ + ts = keystrval(nodefromval(o)); /* re-use value previously stored */ } L->top--; /* remove string from stack */ return ts; @@ -208,8 +210,16 @@ static int check_next2 (LexState *ls, const char *set) { /* LUA_NUMBER */ /* -** this function is quite liberal in what it accepts, as 'luaO_str2num' -** will reject ill-formed numerals. +** This function is quite liberal in what it accepts, as 'luaO_str2num' +** will reject ill-formed numerals. Roughly, it accepts the following +** pattern: +** +** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))* +** +** The only tricky part is to accept [+-] only after a valid exponent +** mark, to avoid reading '3-4' or '0xe+1' as a single number. +** +** The caller might have already read an initial dot. */ static int read_numeral (LexState *ls, SemInfo *seminfo) { TValue obj; @@ -220,14 +230,14 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next2(ls, expo)) /* exponent part? */ + if (check_next2(ls, expo)) /* exponent mark? */ check_next2(ls, "-+"); /* optional exponent sign */ - if (lisxdigit(ls->current)) - save_and_next(ls); - else if (ls->current == '.') + else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ save_and_next(ls); else break; } + if (lislalpha(ls->current)) /* is numeral touching a letter? */ + save_and_next(ls); /* force an error */ save(ls, '\0'); if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ lexerror(ls, "malformed number", TK_FLT); @@ -244,12 +254,12 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** 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) +** reads a sequence '[=*[' or ']=*]', leaving the last bracket. +** If sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). */ -static int skip_sep (LexState *ls) { - int count = 0; +static size_t skip_sep (LexState *ls) { + size_t count = 0; int s = ls->current; lua_assert(s == '[' || s == ']'); save_and_next(ls); @@ -257,11 +267,13 @@ static int skip_sep (LexState *ls) { save_and_next(ls); count++; } - return (ls->current == s) ? count : (-count) - 1; + return (ls->current == s) ? count + 2 + : (count == 0) ? 1 + : 0; } -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { +static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { int line = ls->linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ @@ -295,8 +307,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { } } endloop: if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), - luaZ_bufflen(ls->buff) - 2*(2 + sep)); + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, + luaZ_bufflen(ls->buff) - 2 * sep); } @@ -330,10 +342,10 @@ static unsigned long readutf8esc (LexState *ls) { 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))) { + while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; + esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); r = (r << 4) + luaO_hexavalue(ls->current); - esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large"); } esccheck(ls, ls->current == '}', "missing '}'"); next(ls); /* skip '}' */ @@ -444,9 +456,9 @@ static int llex (LexState *ls, SemInfo *seminfo) { /* else is a comment */ next(ls); if (ls->current == '[') { /* long comment? */ - int sep = skip_sep(ls); + size_t sep = skip_sep(ls); luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ - if (sep >= 0) { + if (sep >= 2) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ break; @@ -458,12 +470,12 @@ static int llex (LexState *ls, SemInfo *seminfo) { break; } case '[': { /* long string or simply '[' */ - int sep = skip_sep(ls); - if (sep >= 0) { + size_t sep = skip_sep(ls); + if (sep >= 2) { read_long_string(ls, seminfo, sep); return TK_STRING; } - else if (sep != -1) /* '[=...' missing second bracket */ + else if (sep == 0) /* '[=...' missing second bracket? */ lexerror(ls, "invalid long string delimiter", TK_STRING); return '['; } diff --git a/3rd/lua/llex.h b/3rd/lua/llex.h index 2ed0af66a..389d2f863 100644 --- a/3rd/lua/llex.h +++ b/3rd/lua/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: llex.h $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -7,11 +7,17 @@ #ifndef llex_h #define llex_h +#include + #include "lobject.h" #include "lzio.h" -#define FIRST_RESERVED 257 +/* +** Single-char tokens (terminal symbols) are represented by their own +** numeric code. Other tokens start at the following value. +*/ +#define FIRST_RESERVED (UCHAR_MAX + 1) #if !defined(LUA_ENV) @@ -37,7 +43,7 @@ enum RESERVED { }; /* number of reserved words */ -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) +#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) typedef union { diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index d1036f6bc..48c97f959 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: llimits.h $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -14,6 +14,7 @@ #include "lua.h" + /* ** '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 @@ -22,7 +23,7 @@ #if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_UMEM lu_mem; typedef LUAI_MEM l_mem; -#elif LUAI_BITSINT >= 32 /* }{ */ +#elif LUAI_IS32INT /* }{ */ typedef size_t lu_mem; typedef ptrdiff_t l_mem; #else /* 16-bit ints */ /* }{ */ @@ -33,12 +34,13 @@ typedef long l_mem; /* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; +typedef signed char ls_byte; /* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)) -/* maximum size visible for Lua (must be representable in a lua_Integer */ +/* 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)) @@ -51,6 +53,23 @@ typedef unsigned char lu_byte; #define MAX_INT INT_MAX /* maximum value of an int */ +/* +** floor of the log2 of the maximum signed value for integral type 't'. +** (That is, maximum 'n' such that '2^n' fits in the given signed type.) +*/ +#define log2maxs(t) (sizeof(t) * 8 - 2) + + +/* +** test whether an unsigned value is a power of 2 (or zero) +*/ +#define ispow2(x) (((x) & ((x) - 1)) == 0) + + +/* number of chars of a literal string without the ending \0 */ +#define LL(x) (sizeof(x)/sizeof(char) - 1) + + /* ** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer @@ -60,27 +79,20 @@ typedef unsigned char lu_byte; -/* type to ensure maximum alignment */ -#if defined(LUAI_USER_ALIGNMENT_T) -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; -#else -typedef union { - lua_Number n; - double u; - void *s; - lua_Integer i; - long l; -} L_Umaxalign; -#endif - - - /* 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 */ +/* +** Internal assertions for in-house debugging +*/ +#if defined LUAI_ASSERT +#undef NDEBUG +#include +#define lua_assert(c) assert(c) +#endif + #if defined(lua_assert) #define check_exp(c,e) (lua_assert(c), (e)) /* to avoid problems with conditions too long */ @@ -95,7 +107,7 @@ typedef LUAI_UACINT l_uacInt; ** assertion for checking API calls */ #if !defined(luai_apicheck) -#define luai_apicheck(l,e) lua_assert(e) +#define luai_apicheck(l,e) ((void)l, lua_assert(e)) #endif #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) @@ -111,10 +123,15 @@ typedef LUAI_UACINT l_uacInt; #define cast(t, exp) ((t)(exp)) #define cast_void(i) cast(void, (i)) -#define cast_byte(i) cast(lu_byte, (i)) +#define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) +#define cast_uint(i) cast(unsigned int, (i)) +#define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) +#define cast_char(i) cast(char, (i)) +#define cast_charp(i) cast(char *, (i)) +#define cast_sizet(i) cast(size_t, (i)) /* cast a signed lua_Integer to lua_Unsigned */ @@ -133,38 +150,49 @@ typedef LUAI_UACINT l_uacInt; /* -** non-return type +** macros to improve jump prediction (used mainly for error handling) */ +#if !defined(likely) + #if defined(__GNUC__) -#define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) && _MSC_VER >= 1200 -#define l_noret void __declspec(noreturn) +#define likely(x) (__builtin_expect(((x) != 0), 1)) +#define unlikely(x) (__builtin_expect(((x) != 0), 0)) #else -#define l_noret void +#define likely(x) (x) +#define unlikely(x) (x) #endif +#endif /* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int.) +** non-return type */ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 +#if !defined(l_noret) + +#if defined(__GNUC__) +#define l_noret void __attribute__((noreturn)) +#elif defined(_MSC_VER) && _MSC_VER >= 1200 +#define l_noret void __declspec(noreturn) +#else +#define l_noret void #endif +#endif /* ** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) */ -#if LUAI_BITSINT >= 32 -typedef unsigned int Instruction; +#if LUAI_IS32INT +typedef unsigned int l_uint32; #else -typedef unsigned long Instruction; +typedef unsigned long l_uint32; #endif +typedef l_uint32 Instruction; + /* @@ -225,8 +253,7 @@ typedef unsigned long Instruction; /* -** these macros allow user-specific actions on threads when you defined -** LUAI_EXTRASPACE and need to do something extra when a thread is +** these macros allow user-specific actions when a thread is ** created/deleted/resumed/yielded. */ #if !defined(luai_userstateopen) @@ -270,15 +297,20 @@ typedef unsigned long Instruction; #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. +** modulo: defined as 'a - floor(a/b)*b'; the direct computation +** using this definition has several problems with rounding errors, +** so it is better to use 'fmod'. '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: non-integer result is equivalent to +** a non-zero remainder 'm'; negative result is equivalent to 'a' and +** 'b' with different signs, or 'm' and 'b' with different signs +** (as the result 'm' of 'fmod' has the same sign of 'a'). */ #if !defined(luai_nummod) #define luai_nummod(L,a,b,m) \ - { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } + { (void)L; (m) = l_mathop(fmod)(a,b); \ + if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } #endif /* exponentiation */ @@ -295,6 +327,8 @@ typedef unsigned long Instruction; #define luai_numeq(a,b) ((a)==(b)) #define luai_numlt(a,b) ((a)<(b)) #define luai_numle(a,b) ((a)<=(b)) +#define luai_numgt(a,b) ((a)>(b)) +#define luai_numge(a,b) ((a)>=(b)) #define luai_numisnan(a) (!luai_numeq((a), (a))) #endif @@ -310,7 +344,7 @@ typedef unsigned long Instruction; #else /* realloc stack keeping its size */ #define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } + { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } #endif #if !defined(HARDMEMTESTS) diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 7ef7e593f..86def470c 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.119.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lmathlib.c $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -10,8 +10,11 @@ #include "lprefix.h" -#include +#include +#include #include +#include +#include #include "lua.h" @@ -23,19 +26,6 @@ #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) { if (lua_isinteger(L, 1)) { lua_Integer n = lua_tointeger(L, 1); @@ -87,7 +77,7 @@ static int math_toint (lua_State *L) { lua_pushinteger(L, n); else { luaL_checkany(L, 1); - lua_pushnil(L); /* value is not convertible to integer */ + luaL_pushfail(L); /* value is not convertible to integer */ } return 1; } @@ -239,22 +229,347 @@ static int math_max (lua_State *L) { return 1; } + +static int math_type (lua_State *L) { + if (lua_type(L, 1) == LUA_TNUMBER) + lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); + else { + luaL_checkany(L, 1); + luaL_pushfail(L); + } + return 1; +} + + + +/* +** {================================================================== +** Pseudo-Random Number Generator based on 'xoshiro256**'. +** =================================================================== +*/ + +/* number of binary digits in the mantissa of a float */ +#define FIGS l_floatatt(MANT_DIG) + +#if FIGS > 64 +/* there are only 64 random bits; use them all */ +#undef FIGS +#define FIGS 64 +#endif + + +/* +** LUA_RAND32 forces the use of 32-bit integers in the implementation +** of the PRN generator (mainly for testing). +*/ +#if !defined(LUA_RAND32) && !defined(Rand64) + +/* try to find an integer type with at least 64 bits */ + +#if (ULONG_MAX >> 31 >> 31) >= 3 + +/* 'long' has at least 64 bits */ +#define Rand64 unsigned long + +#elif !defined(LUA_USE_C89) && defined(LLONG_MAX) + +/* there is a 'long long' type (which must have at least 64 bits) */ +#define Rand64 unsigned long long + +#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3 + +/* 'lua_Integer' has at least 64 bits */ +#define Rand64 lua_Unsigned + +#endif + +#endif + + +#if defined(Rand64) /* { */ + +/* +** Standard implementation, using 64-bit integers. +** If 'Rand64' has more than 64 bits, the extra bits do not interfere +** with the 64 initial bits, except in a right shift. Moreover, the +** final result has to discard the extra bits. +*/ + +/* avoid using extra bits when needed */ +#define trim64(x) ((x) & 0xffffffffffffffffu) + + +/* rotate left 'x' by 'n' bits */ +static Rand64 rotl (Rand64 x, int n) { + return (x << n) | (trim64(x) >> (64 - n)); +} + +static Rand64 nextrand (Rand64 *state) { + Rand64 state0 = state[0]; + Rand64 state1 = state[1]; + Rand64 state2 = state[2] ^ state0; + Rand64 state3 = state[3] ^ state1; + Rand64 res = rotl(state1 * 5, 7) * 9; + state[0] = state0 ^ state3; + state[1] = state1 ^ state2; + state[2] = state2 ^ (state1 << 17); + state[3] = rotl(state3, 45); + return res; +} + + +/* must take care to not shift stuff by more than 63 slots */ + + +/* +** Convert bits from a random integer into a float in the +** interval [0,1), getting the higher FIG bits from the +** random unsigned integer and converting that to a float. +*/ + +/* must throw out the extra (64 - FIGS) bits */ +#define shift64_FIG (64 - FIGS) + +/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ +#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) + +static lua_Number I2d (Rand64 x) { + return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; +} + +/* convert a 'Rand64' to a 'lua_Unsigned' */ +#define I2UInt(x) ((lua_Unsigned)trim64(x)) + +/* convert a 'lua_Unsigned' to a 'Rand64' */ +#define Int2I(x) ((Rand64)(x)) + + +#else /* no 'Rand64' }{ */ + +/* get an integer with at least 32 bits */ +#if LUAI_IS32INT +typedef unsigned int lu_int32; +#else +typedef unsigned long lu_int32; +#endif + + /* -** 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.) +** Use two 32-bit integers to represent a 64-bit quantity. */ +typedef struct Rand64 { + lu_int32 h; /* higher half */ + lu_int32 l; /* lower half */ +} Rand64; + + +/* +** If 'lu_int32' has more than 32 bits, the extra bits do not interfere +** with the 32 initial bits, except in a right shift and comparisons. +** Moreover, the final result has to discard the extra bits. +*/ + +/* avoid using extra bits when needed */ +#define trim32(x) ((x) & 0xffffffffu) + + +/* +** basic operations on 'Rand64' values +*/ + +/* build a new Rand64 value */ +static Rand64 packI (lu_int32 h, lu_int32 l) { + Rand64 result; + result.h = h; + result.l = l; + return result; +} + +/* return i << n */ +static Rand64 Ishl (Rand64 i, int n) { + lua_assert(n > 0 && n < 32); + return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n); +} + +/* i1 ^= i2 */ +static void Ixor (Rand64 *i1, Rand64 i2) { + i1->h ^= i2.h; + i1->l ^= i2.l; +} + +/* return i1 + i2 */ +static Rand64 Iadd (Rand64 i1, Rand64 i2) { + Rand64 result = packI(i1.h + i2.h, i1.l + i2.l); + if (trim32(result.l) < trim32(i1.l)) /* carry? */ + result.h++; + return result; +} + +/* return i * 5 */ +static Rand64 times5 (Rand64 i) { + return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */ +} + +/* return i * 9 */ +static Rand64 times9 (Rand64 i) { + return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */ +} + +/* return 'i' rotated left 'n' bits */ +static Rand64 rotl (Rand64 i, int n) { + lua_assert(n > 0 && n < 32); + return packI((i.h << n) | (trim32(i.l) >> (32 - n)), + (trim32(i.h) >> (32 - n)) | (i.l << n)); +} + +/* for offsets larger than 32, rotate right by 64 - offset */ +static Rand64 rotl1 (Rand64 i, int n) { + lua_assert(n > 32 && n < 64); + n = 64 - n; + return packI((trim32(i.h) >> n) | (i.l << (32 - n)), + (i.h << (32 - n)) | (trim32(i.l) >> n)); +} + +/* +** implementation of 'xoshiro256**' algorithm on 'Rand64' values +*/ +static Rand64 nextrand (Rand64 *state) { + Rand64 res = times9(rotl(times5(state[1]), 7)); + Rand64 t = Ishl(state[1], 17); + Ixor(&state[2], state[0]); + Ixor(&state[3], state[1]); + Ixor(&state[1], state[2]); + Ixor(&state[0], state[3]); + Ixor(&state[2], t); + state[3] = rotl1(state[3], 45); + return res; +} + + +/* +** Converts a 'Rand64' into a float. +*/ + +/* an unsigned 1 with proper type */ +#define UONE ((lu_int32)1) + + +#if FIGS <= 32 + +/* 2^(-FIGS) */ +#define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) + +/* +** get up to 32 bits from higher half, shifting right to +** throw out the extra bits. +*/ +static lua_Number I2d (Rand64 x) { + lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS)); + return h * scaleFIG; +} + +#else /* 32 < FIGS <= 64 */ + +/* must take care to not shift stuff by more than 31 slots */ + +/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ +#define scaleFIG \ + ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33))) + +/* +** use FIGS - 32 bits from lower half, throwing out the other +** (32 - (FIGS - 32)) = (64 - FIGS) bits +*/ +#define shiftLOW (64 - FIGS) + +/* +** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) +*/ +#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0) + + +static lua_Number I2d (Rand64 x) { + lua_Number h = (lua_Number)trim32(x.h) * shiftHI; + lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW); + return (h + l) * scaleFIG; +} + +#endif + + +/* convert a 'Rand64' to a 'lua_Unsigned' */ +static lua_Unsigned I2UInt (Rand64 x) { + return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); +} + +/* convert a 'lua_Unsigned' to a 'Rand64' */ +static Rand64 Int2I (lua_Unsigned n) { + return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); +} + +#endif /* } */ + + +/* +** A state uses four 'Rand64' values. +*/ +typedef struct { + Rand64 s[4]; +} RanState; + + +/* +** Project the random integer 'ran' into the interval [0, n]. +** Because 'ran' has 2^B possible values, the projection can only be +** uniform when the size of the interval is a power of 2 (exact +** division). Otherwise, to get a uniform projection into [0, n], we +** first compute 'lim', the smallest Mersenne number not smaller than +** 'n'. We then project 'ran' into the interval [0, lim]. If the result +** is inside [0, n], we are done. Otherwise, we try with another 'ran', +** until we have a result inside the interval. +*/ +static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, + RanState *state) { + if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */ + return ran & n; /* no bias */ + else { + lua_Unsigned lim = n; + /* compute the smallest (2^b - 1) not smaller than 'n' */ + lim |= (lim >> 1); + lim |= (lim >> 2); + lim |= (lim >> 4); + lim |= (lim >> 8); + lim |= (lim >> 16); +#if (LUA_MAXUNSIGNED >> 31) >= 3 + lim |= (lim >> 32); /* integer type has more than 32 bits */ +#endif + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ + && lim >= n /* not smaller than 'n', */ + && (lim >> 1) < n); /* and it is the smallest one */ + while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ + ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ + return ran; + } +} + + static int math_random (lua_State *L) { lua_Integer low, up; - double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0)); + lua_Unsigned p; + RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); + Rand64 rv = nextrand(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ - lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */ + lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ return 1; } case 1: { /* only upper limit */ low = 1; up = luaL_checkinteger(L, 1); + if (up == 0) { /* single 0 as argument? */ + lua_pushinteger(L, I2UInt(rv)); /* full random integer */ + return 1; + } break; } case 2: { /* lower and upper limits */ @@ -266,35 +581,72 @@ static int math_random (lua_State *L) { } /* 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); + /* project random integer into the interval [0, up - low] */ + p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); + lua_pushinteger(L, p + (lua_Unsigned)low); return 1; } -static int math_randomseed (lua_State *L) { - l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); - (void)l_rand(); /* discard first value to avoid undesirable correlations */ - return 0; +static void setseed (lua_State *L, Rand64 *state, + lua_Unsigned n1, lua_Unsigned n2) { + int i; + state[0] = Int2I(n1); + state[1] = Int2I(0xff); /* avoid a zero state */ + state[2] = Int2I(n2); + state[3] = Int2I(0); + for (i = 0; i < 16; i++) + nextrand(state); /* discard initial values to "spread" seed */ + lua_pushinteger(L, n1); + lua_pushinteger(L, n2); } -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"); +/* +** Set a "random" seed. To get some randomness, use the current time +** and the address of 'L' (in case the machine does address space layout +** randomization). +*/ +static void randseed (lua_State *L, RanState *state) { + lua_Unsigned seed1 = (lua_Unsigned)time(NULL); + lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; + setseed(L, state->s, seed1, seed2); +} + + +static int math_randomseed (lua_State *L) { + RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); + if (lua_isnone(L, 1)) { + randseed(L, state); } else { - luaL_checkany(L, 1); - lua_pushnil(L); + lua_Integer n1 = luaL_checkinteger(L, 1); + lua_Integer n2 = luaL_optinteger(L, 2, 0); + setseed(L, state->s, n1, n2); } - return 1; + return 2; /* return seeds */ +} + + +static const luaL_Reg randfuncs[] = { + {"random", math_random}, + {"randomseed", math_randomseed}, + {NULL, NULL} +}; + + +/* +** Register the random functions and initialize their state. +*/ +static void setrandfunc (lua_State *L) { + RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); + randseed(L, state); /* initialize with a "random" seed */ + lua_pop(L, 2); /* remove pushed seeds */ + luaL_setfuncs(L, randfuncs, 1); } +/* }================================================================== */ + /* ** {================================================================== @@ -367,8 +719,6 @@ static const luaL_Reg mathlib[] = { {"min", math_min}, {"modf", math_modf}, {"rad", math_rad}, - {"random", math_random}, - {"randomseed", math_randomseed}, {"sin", math_sin}, {"sqrt", math_sqrt}, {"tan", math_tan}, @@ -384,6 +734,8 @@ static const luaL_Reg mathlib[] = { {"log10", math_log10}, #endif /* placeholders */ + {"random", NULL}, + {"randomseed", NULL}, {"pi", NULL}, {"huge", NULL}, {"maxinteger", NULL}, @@ -405,6 +757,7 @@ LUAMOD_API int luaopen_math (lua_State *L) { lua_setfield(L, -2, "maxinteger"); lua_pushinteger(L, LUA_MININTEGER); lua_setfield(L, -2, "mininteger"); + setrandfunc(L); return 1; } diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index 0241cc3ba..43739bffd 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lmem.c $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -22,79 +22,181 @@ #include "lstate.h" +#if defined(EMERGENCYGCTESTS) +/* +** First allocation will fail whenever not building initial state +** and not shrinking a block. (This fail will trigger 'tryagain' and +** a full GC cycle at every allocation.) +*/ +static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { + if (ttisnil(&g->nilvalue) && ns > os) + return NULL; /* fail */ + else /* normal allocation */ + return (*g->frealloc)(g->ud, block, os, ns); +} +#else +#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) +#endif + + + + /* ** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); +** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); ** ('osize' is the old size, 'nsize' is the new size) ** -** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no -** matter 'x'). +** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. +** Particularly, frealloc(ud, NULL, 0, 0) does nothing, +** which is equivalent to free(NULL) in ISO C. ** -** * 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 ISO C) +** - frealloc(ud, NULL, x, s) creates a new block of size 's' +** (no matter 'x'). Returns NULL if it cannot create the new block. ** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) +** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from +** size 'x' to size 'y'. Returns NULL if it cannot reallocate the +** block to the new size. */ + +/* +** {================================================================== +** Functions to allocate/deallocate arrays for the Parser +** =================================================================== +*/ + +/* +** Minimum size for arrays during parsing, to avoid overhead of +** reallocating to size 1, then 2, and then 4. All these arrays +** will be reallocated to exact sizes or erased when parsing ends. +*/ #define MINSIZEARRAY 4 -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, - int limit, const char *what) { +void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, + int size_elems, int limit, const char *what) { void *newblock; - int newsize; - if (*size >= limit/2) { /* cannot double it? */ - if (*size >= limit) /* cannot grow even a little? */ + int size = *psize; + if (nelems + 1 <= size) /* does one extra element still fit? */ + return block; /* nothing to be done */ + if (size >= limit / 2) { /* cannot double it? */ + if (unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); - newsize = limit; /* still have at least one free place */ + size = limit; /* still have at least one free place */ } else { - newsize = (*size)*2; - if (newsize < MINSIZEARRAY) - newsize = MINSIZEARRAY; /* minimum size */ + size *= 2; + if (size < MINSIZEARRAY) + size = MINSIZEARRAY; /* minimum size */ } - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); - *size = newsize; /* update only when everything else is OK */ + lua_assert(nelems + 1 <= size && size <= limit); + /* 'limit' ensures that multiplication will not overflow */ + newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, + cast_sizet(size) * size_elems); + *psize = size; /* update only when everything else is OK */ return newblock; } +/* +** In prototypes, the size of the array is also its number of +** elements (to save memory). So, if it cannot shrink an array +** to its number of elements, the only option is to raise an +** error. +*/ +void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, + int final_n, int size_elem) { + void *newblock; + size_t oldsize = cast_sizet((*size) * size_elem); + size_t newsize = cast_sizet(final_n * size_elem); + lua_assert(newsize <= oldsize); + newblock = luaM_saferealloc_(L, block, oldsize, newsize); + *size = final_n; + return newblock; +} + +/* }================================================================== */ + + l_noret luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); } +/* +** Free memory +*/ +void luaM_free_ (lua_State *L, void *block, size_t osize) { + global_State *g = G(L); + lua_assert((osize == 0) == (block == NULL)); + (*g->frealloc)(g->ud, block, osize, 0); + g->GCdebt -= osize; +} + + +/* +** In case of allocation fail, this function will call the GC to try +** to free some memory and then try the allocation again. +** (It should not be called when shrinking a block, because then the +** interpreter may be in the middle of a collection step.) +*/ +static void *tryagain (lua_State *L, void *block, + size_t osize, size_t nsize) { + global_State *g = G(L); + if (ttisnil(&g->nilvalue)) { /* is state fully build? */ + luaC_fullgc(L, 1); /* try to free some memory... */ + return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + } + else return NULL; /* cannot free any memory without a full state */ +} + /* -** generic allocation routine. +** Generic allocation routine. +** If allocation fails while shrinking a block, do not try again; the +** GC shrinks some blocks and it is not reentrant. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); - size_t realosize = (block) ? osize : 0; - lua_assert((realosize == 0) == (block == NULL)); -#if defined(HARDMEMTESTS) - if (nsize > realosize && g->gcrunning) - luaC_fullgc(L, 1); /* force a GC whenever possible */ -#endif - newblock = (*g->frealloc)(g->ud, block, osize, nsize); - if (newblock == NULL && nsize > 0) { - 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); + lua_assert((osize == 0) == (block == NULL)); + newblock = firsttry(g, block, osize, nsize); + if (unlikely(newblock == NULL && nsize > 0)) { + if (nsize > osize) /* not shrinking a block? */ + newblock = tryagain(L, block, osize, nsize); + if (newblock == NULL) /* still no memory? */ + return NULL; /* do not update 'GCdebt' */ } lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - realosize; + g->GCdebt = (g->GCdebt + nsize) - osize; + return newblock; +} + + +void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, + size_t nsize) { + void *newblock = luaM_realloc_(L, block, osize, nsize); + if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ + luaM_error(L); return newblock; } + +void *luaM_malloc_ (lua_State *L, size_t size, int tag) { + if (size == 0) + return NULL; /* that's all */ + else { + global_State *g = G(L); + void *newblock = firsttry(g, NULL, tag, size); + if (unlikely(newblock == NULL)) { + newblock = tryagain(L, NULL, tag, size); + if (newblock == NULL) + luaM_error(L); + } + g->GCdebt += size; + return newblock; + } +} diff --git a/3rd/lua/lmem.h b/3rd/lua/lmem.h index 357b1e43e..8c75a44be 100644 --- a/3rd/lua/lmem.h +++ b/3rd/lua/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lmem.h $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -14,12 +14,13 @@ #include "lua.h" +#define luaM_error(L) luaD_throw(L, LUA_ERRMEM) + + /* -** 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). -** +** This macro tests whether it is safe to multiply 'n' by the size of +** type 't' without overflows. 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 @@ -27,43 +28,66 @@ ** 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) \ - (((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))) +#define luaM_testsize(n,e) \ + (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) + +#define luaM_checksize(L,n,e) \ + (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) + + +/* +** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that +** the result is not larger than 'n' and cannot overflow a 'size_t' +** when multiplied by the size of type 't'. (Assumes that 'n' is an +** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) +*/ +#define luaM_limitN(n,t) \ + ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ + cast_uint((MAX_SIZET/sizeof(t)))) + /* ** 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))) + cast_charp(luaM_saferealloc_(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_realloc_(L, (b), (n)*sizeof(*(b)), 0) +#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) +#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) +#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) -#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) -#define luaM_newvector(L,n,t) \ - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) +#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) +#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) +#define luaM_newvectorchecked(L,n,t) \ + (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) -#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) +#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) #define luaM_growvector(L,v,nelems,size,t,limit,e) \ - if ((nelems)+1 > (size)) \ - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) + ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ + luaM_limitN(limit,t),e))) #define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) + (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ + cast_sizet(n) * sizeof(t)))) + +#define luaM_shrinkvector(L,v,size,fs,t) \ + ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) LUAI_FUNC l_noret luaM_toobig (lua_State *L); /* not to be called directly */ LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, size_t size); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, - size_t size_elem, int limit, +LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, + size_t size); +LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); +LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, + int *size, int size_elem, int limit, const char *what); +LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, + int final_n, int size_elem); +LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 45f44d322..c0ec9a131 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: loadlib.c $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -56,10 +56,10 @@ /* -** unique key for table in the registry that keeps handles +** key for table in the registry that keeps handles ** for all loaded C libraries */ -static const int CLIBS = 0; +static const char *const CLIBS = "_CLIBS"; #define LIB_FAIL "open" @@ -67,6 +67,13 @@ static const int CLIBS = 0; #define setprogdir(L) ((void)0) +/* +** Special type equivalent to '(void*)' for functions in gcc +** (to suppress warnings when converting function pointers) +*/ +typedef void (*voidf)(void); + + /* ** system-dependent functions */ @@ -206,7 +213,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)GetProcAddress((HMODULE)lib, sym); + lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); if (f == NULL) pusherror(L); return f; } @@ -269,8 +276,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif -#define AUXMARK "\1" /* auxiliary mark */ - /* ** return registry.LUA_NOENV as a boolean @@ -290,22 +295,33 @@ static int noenv (lua_State *L) { static void setpath (lua_State *L, const char *fieldname, const char *envname, const char *dft) { + const char *dftmark; 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? */ + const char *path = getenv(nver); /* try versioned name */ + if (path == NULL) /* no versioned 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' */ + else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) + lua_pushstring(L, path); /* nothing to change */ + else { /* path contains a ";;": insert default path in its place */ + size_t len = strlen(path); + luaL_Buffer b; + luaL_buffinit(L, &b); + if (path < dftmark) { /* is there a prefix before ';;'? */ + luaL_addlstring(&b, path, dftmark - path); /* add it */ + luaL_addchar(&b, *LUA_PATH_SEP); + } + luaL_addstring(&b, dft); /* add default */ + if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ + luaL_addchar(&b, *LUA_PATH_SEP); + luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark); + } + luaL_pushresult(&b); } setprogdir(L); lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ - lua_pop(L, 1); /* pop versioned variable name */ + lua_pop(L, 1); /* pop versioned variable name ('nver') */ } /* }================================================================== */ @@ -316,7 +332,7 @@ static void setpath (lua_State *L, const char *fieldname, */ static void *checkclib (lua_State *L, const char *path) { void *plib; - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); + lua_getfield(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' */ @@ -329,7 +345,7 @@ static void *checkclib (lua_State *L, const char *path) { ** 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_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_pushlightuserdata(L, plib); lua_pushvalue(L, -1); lua_setfield(L, -3, path); /* CLIBS[path] = plib */ @@ -397,10 +413,10 @@ static int ll_loadlib (lua_State *L) { if (stat == 0) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ + return 3; /* return fail, error message, and where */ } } @@ -421,14 +437,42 @@ static int readable (const char *filename) { } -static const char *pushnexttemplate (lua_State *L, const char *path) { - const char *l; - while (*path == *LUA_PATH_SEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, l - path); /* template */ - return l; +/* +** Get the next name in '*path' = 'name1;name2;name3;...', changing +** the ending ';' to '\0' to create a zero-terminated string. Return +** NULL when list ends. +*/ +static const char *getnextfilename (char **path, char *end) { + char *sep; + char *name = *path; + if (name == end) + return NULL; /* no more names */ + else if (*name == '\0') { /* from previous iteration? */ + *name = *LUA_PATH_SEP; /* restore separator */ + name++; /* skip it */ + } + sep = strchr(name, *LUA_PATH_SEP); /* find next separator */ + if (sep == NULL) /* separator not found? */ + sep = end; /* name goes until the end */ + *sep = '\0'; /* finish file name */ + *path = sep; /* will start next search from here */ + return name; +} + + +/* +** Given a path such as ";blabla.so;blublu.so", pushes the string +** +** no file 'blabla.so' +** no file 'blublu.so' +*/ +static void pusherrornotfound (lua_State *L, const char *path) { + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addstring(&b, "no file '"); + luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); + luaL_addstring(&b, "'"); + luaL_pushresult(&b); } @@ -436,21 +480,25 @@ static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - if (*sep != '\0') /* non-empty separator? */ + luaL_Buffer buff; + char *pathname; /* path with name inserted */ + char *endpathname; /* its end */ + const char *filename; + /* separator is non-empty and appears in 'name'? */ + if (*sep != '\0' && strchr(name, *sep) != NULL) name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ + luaL_buffinit(L, &buff); + /* add path to the buffer, replacing marks ('?') with the file name */ + luaL_addgsub(&buff, path, LUA_PATH_MARK, name); + luaL_addchar(&buff, '\0'); + pathname = luaL_buffaddr(&buff); /* writable list of file names */ + endpathname = pathname + luaL_bufflen(&buff) - 1; + while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file '%s'", filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ + return lua_pushstring(L, filename); /* save and return name */ } - luaL_pushresult(&msg); /* create error message */ + luaL_pushresult(&buff); /* push path to create error message */ + pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ return NULL; /* not found */ } @@ -462,9 +510,9 @@ static int ll_searchpath (lua_State *L) { luaL_optstring(L, 4, LUA_DIRSEP)); if (f != NULL) return 1; else { /* error message is on top of the stack */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); - return 2; /* return nil + error message */ + return 2; /* return fail + error message */ } } @@ -548,7 +596,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 '%s' in file '%s'", name, filename); + lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); return 1; } } @@ -560,23 +608,30 @@ 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, 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; + if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ + lua_pushfstring(L, "no field package.preload['%s']", name); + return 1; + } + else { + lua_pushliteral(L, ":preload:"); + return 2; + } } static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); /* 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"); + luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { + luaL_addstring(&msg, "\n\t"); /* error-message prefix */ if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ + luaL_buffsub(&msg, 2); /* remove prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } @@ -588,8 +643,10 @@ static void findloader (lua_State *L, const char *name) { lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ } - else + else { /* no error message */ lua_pop(L, 2); /* remove both returns */ + luaL_buffsub(&msg, 2); /* remove prefix */ + } } } @@ -604,113 +661,33 @@ static int ll_require (lua_State *L) { /* else must load package */ lua_pop(L, 1); /* remove 'getfield' result */ findloader(L, name); - lua_pushstring(L, name); /* pass name as argument to module loader */ - lua_insert(L, -2); /* name is 1st argument (before search data) */ + lua_rotate(L, -2, 1); /* function <-> loader data */ + lua_pushvalue(L, 1); /* name is 1st argument to module loader */ + lua_pushvalue(L, -3); /* loader data is 2nd argument */ + /* stack: ...; loader data; loader function; mod. name; loader data */ lua_call(L, 2, 1); /* run loader to load module */ + /* stack: ...; loader data; result from loader */ if (!lua_isnil(L, -1)) /* non-nil return? */ lua_setfield(L, 2, name); /* LOADED[name] = returned value */ + else + lua_pop(L, 1); /* pop nil */ 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_copy(L, -1, -2); /* replace loader result */ lua_setfield(L, 2, name); /* LOADED[name] = true */ } - return 1; + lua_rotate(L, -2, 1); /* loader data <-> module result */ + return 2; /* return module result and loader data */ } /* }====================================================== */ -/* -** {====================================================== -** 'module' function -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -/* -** changes the environment variable of calling function -*/ -static void set_env (lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - 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 */ -} - - -static void dooptions (lua_State *L, int n) { - int i; - for (i = 2; i <= n; i++) { - if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */ - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } - } -} - - -static void modinit (lua_State *L, const char *modname) { - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - int lastarg = lua_gettop(L); /* last parameter */ - luaL_pushmodule(L, modname, 1); /* get/create module table */ - /* check whether table already has a _NAME field */ - 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); - } - lua_pushvalue(L, -1); - set_env(L); - dooptions(L, lastarg); - return 1; -} - - -static int ll_seeall (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushglobaltable(L); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -#endif -/* }====================================================== */ - - static const luaL_Reg pk_funcs[] = { {"loadlib", ll_loadlib}, {"searchpath", ll_searchpath}, -#if defined(LUA_COMPAT_MODULE) - {"seeall", ll_seeall}, -#endif /* placeholders */ {"preload", NULL}, {"cpath", NULL}, @@ -722,9 +699,6 @@ static const luaL_Reg pk_funcs[] = { static const luaL_Reg ll_funcs[] = { -#if defined(LUA_COMPAT_MODULE) - {"module", ll_module}, -#endif {"require", ll_require}, {NULL, NULL} }; @@ -742,10 +716,6 @@ 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' */ } @@ -755,12 +725,11 @@ static void createsearcherstable (lua_State *L) { ** setting a finalizer to close all libraries when closing state. */ static void createclibstable (lua_State *L) { - lua_newtable(L); /* create CLIBS table */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* 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 */ } diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 2218c8cdd..f8ea917a8 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: lobject.c $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -29,36 +29,6 @@ #include "lvm.h" - -LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; - - -/* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. -*/ -int luaO_int2fb (unsigned int x) { - int e = 0; /* exponent */ - if (x < 8) return x; - 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); -} - - -/* converts back */ -int luaO_fb2int (int x) { - return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); -} - - /* ** Computes ceil(log2(x)) */ @@ -87,7 +57,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, 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_OPIDIV: return luaV_idiv(L, v1, v2); case LUA_OPBAND: return intop(&, v1, v2); case LUA_OPBOR: return intop(|, v1, v2); case LUA_OPBXOR: return intop(^, v1, v2); @@ -110,53 +80,55 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, 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; - } + case LUA_OPMOD: return luaV_modf(L, v1, v2); default: lua_assert(0); return 0; } } -void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, - TValue *res) { +int luaO_rawarith (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)) { + if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; - if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } default: { /* other operations */ lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); - return; + return 1; } - else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } } - /* 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)); +} + + +void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, + StkId res) { + if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { + /* could not perform raw operation; try metamethod */ + luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); + } } @@ -187,7 +159,7 @@ static int isneg (const char **s) { #define MAXSIGDIG 30 /* -** convert an hexadecimal numeric string to a number, following +** convert a hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { @@ -198,9 +170,9 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { int e = 0; /* exponent correction */ int neg; /* 1 if number is negative */ int hasdot = 0; /* true after seen a dot */ - *endptr = cast(char *, s); /* nothing is valid yet */ + *endptr = cast_charp(s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); /* check signal */ + neg = isneg(&s); /* check sign */ 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 */ @@ -220,20 +192,20 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { } if (nosigdig + sigdig == 0) /* no digits? */ return 0.0; /* invalid format */ - *endptr = cast(char *, s); /* valid up to here */ + *endptr = cast_charp(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; /* exponent value */ - int neg1; /* exponent signal */ + int neg1; /* exponent sign */ s++; /* skip 'p' */ - neg1 = isneg(&s); /* signal */ + neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) 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_charp(s); /* valid up to here */ } if (neg) r = -r; return l_mathop(ldexp)(r, e); @@ -243,37 +215,42 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { /* }====================================================== */ -/* maximum length of a numeral */ +/* maximum length of a numeral to be converted to a number */ #if !defined (L_MAXLENNUM) #define L_MAXLENNUM 200 #endif +/* +** Convert string 's' to a Lua number (put in 'result'). Return NULL on +** fail or the address of the ending '\0' on success. ('mode' == 'x') +** means a hexadecimal numeral. +*/ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; *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 */ + return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ } /* -** 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) +** Convert string 's' to a Lua number (put in 'result') handling the +** current locale. ** 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 +** mark. If the conversion 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. +** The variable 'mode' checks for special characters in the string: +** - 'n' means 'inf' or 'nan' (which should be rejected) +** - 'x' means a hexadecimal numeral +** - '.' just optimizes the search for the common case (no special chars) */ static const char *l_str2d (const char *s, lua_Number *result) { const char *endptr; - const char *pmode = strpbrk(s, ".xXnN"); + const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; if (mode == 'n') /* reject 'inf' and 'nan' */ return NULL; @@ -345,109 +322,204 @@ size_t luaO_str2num (const char *s, TValue *o) { int luaO_utf8esc (char *buff, unsigned long x) { int n = 1; /* number of bytes put in buffer (backwards) */ - lua_assert(x <= 0x10FFFF); + lua_assert(x <= 0x7FFFFFFFu); if (x < 0x80) /* ascii? */ - buff[UTF8BUFFSZ - 1] = cast(char, x); + 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)); + 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 */ + 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 +/* +** Maximum length of the conversion of a number to a string. Must be +** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. +** (For a long long int, this is 19 digits plus a sign and a final '\0', +** adding to 21. For a long double, it can go to a sign, 33 digits, +** the dot, an exponent letter, an exponent sign, 5 exponent digits, +** and a final '\0', adding to 43.) +*/ +#define MAXNUMBER2STR 44 /* -** Convert a number object to a string +** Convert a number object to a string, adding it to a buffer */ -void luaO_tostring (lua_State *L, StkId obj) { - char buff[MAXNUMBER2STR]; - size_t len; +static int tostringbuff (TValue *obj, char *buff) { + int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) - len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); + len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); else { - len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); -#if !defined(LUA_COMPAT_FLOATSTRING) + len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj)); if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ buff[len++] = lua_getlocaledecpoint(); buff[len++] = '0'; /* adds '.0' to result */ } -#endif } - setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); + return len; } -static void pushstr (lua_State *L, const char *str, size_t l) { +/* +** Convert a number object to a Lua string, replacing the value at 'obj' +*/ +void luaO_tostring (lua_State *L, TValue *obj) { + char buff[MAXNUMBER2STR]; + int len = tostringbuff(obj, buff); + setsvalue(L, obj, luaS_newlstr(L, buff, len)); +} + + + + +/* +** {================================================================== +** 'luaO_pushvfstring' +** =================================================================== +*/ + +/* size for buffer space used by 'luaO_pushvfstring' */ +#define BUFVFS 200 + +/* buffer used by 'luaO_pushvfstring' */ +typedef struct BuffFS { + lua_State *L; + int pushed; /* number of string pieces already on the stack */ + int blen; /* length of partial string in 'space' */ + char space[BUFVFS]; /* holds last part of the result */ +} BuffFS; + + +/* +** Push given string to the stack, as part of the buffer, and +** join the partial strings in the stack into one. +*/ +static void pushstr (BuffFS *buff, const char *str, size_t l) { + lua_State *L = buff->L; setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - luaD_inctop(L); + L->top++; /* may use one extra slot */ + buff->pushed++; + luaV_concat(L, buff->pushed); /* join partial results into one */ + buff->pushed = 1; } /* -** this function handles only '%d', '%c', '%f', '%p', and '%s' +** empty the buffer space into the stack +*/ +static void clearbuff (BuffFS *buff) { + pushstr(buff, buff->space, buff->blen); /* push buffer contents */ + buff->blen = 0; /* space now is empty */ +} + + +/* +** Get a space of size 'sz' in the buffer. If buffer has not enough +** space, empty it. 'sz' must fit in an empty buffer. +*/ +static char *getbuff (BuffFS *buff, int sz) { + lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); + if (sz > BUFVFS - buff->blen) /* not enough space? */ + clearbuff(buff); + return buff->space + buff->blen; +} + + +#define addsize(b,sz) ((b)->blen += (sz)) + + +/* +** Add 'str' to the buffer. If string is larger than the buffer space, +** push the string directly to the stack. +*/ +static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { + if (slen <= BUFVFS) { /* does string fit into buffer? */ + char *bf = getbuff(buff, cast_int(slen)); + memcpy(bf, str, slen); /* add string to buffer */ + addsize(buff, cast_int(slen)); + } + else { /* string larger than buffer */ + clearbuff(buff); /* string comes after buffer's content */ + pushstr(buff, str, slen); /* push string */ + } +} + + +/* +** Add a number to the buffer. +*/ +static void addnum2buff (BuffFS *buff, TValue *num) { + char *numbuff = getbuff(buff, MAXNUMBER2STR); + int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ + addsize(buff, len); +} + + +/* +** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' 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 (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - pushstr(L, fmt, e - fmt); - switch (*(e+1)) { + BuffFS buff; /* holds last part of the result */ + const char *e; /* points to next '%' */ + buff.pushed = buff.blen = 0; + buff.L = L; + while ((e = strchr(fmt, '%')) != NULL) { + addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ + switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; - pushstr(L, s, strlen(s)); + addstr2buff(&buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ - 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)); + char c = cast_uchar(va_arg(argp, int)); + addstr2buff(&buff, &c, sizeof(char)); break; } case 'd': { /* an 'int' */ - setivalue(L->top, va_arg(argp, int)); - goto top2str; + TValue num; + setivalue(&num, va_arg(argp, int)); + addnum2buff(&buff, &num); + break; } case 'I': { /* a 'lua_Integer' */ - setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); - goto top2str; + TValue num; + setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); + addnum2buff(&buff, &num); + break; } case 'f': { /* a 'lua_Number' */ - setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); - top2str: /* convert the top element to a string */ - luaD_inctop(L); - luaO_tostring(L, L->top - 1); + TValue num; + setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); + addnum2buff(&buff, &num); break; } case 'p': { /* a pointer */ - char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ + const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ + char *bf = getbuff(&buff, sz); void *p = va_arg(argp, void *); - int l = lua_pointer2str(buff, sizeof(buff), p); - pushstr(L, buff, l); + int len = lua_pointer2str(bf, sz, p); + addsize(&buff, len); break; } - 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); + case 'U': { /* a 'long' as a UTF-8 sequence */ + char bf[UTF8BUFFSZ]; + int len = luaO_utf8esc(bf, va_arg(argp, long)); + addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); break; } case '%': { - pushstr(L, "%", 1); + addstr2buff(&buff, "%", 1); break; } default: { @@ -455,13 +527,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { *(e + 1)); } } - n += 2; - fmt = e+2; + fmt = e + 2; /* skip '%' and the specifier */ } - luaD_checkstack(L, 1); - pushstr(L, fmt, strlen(fmt)); - if (n > 0) luaV_concat(L, n + 1); - return svalue(L->top - 1); + addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ + clearbuff(&buff); /* empty buffer into the stack */ + lua_assert(buff.pushed == 1); + return svalue(s2v(L->top - 1)); } @@ -474,9 +545,8 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { return msg; } +/* }================================================================== */ -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) #define RETS "..." #define PRE "[string \"" @@ -484,36 +554,36 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - size_t l = strlen(source); +void luaO_chunkid (char *out, const char *source, size_t srclen) { + size_t bufflen = LUA_IDSIZE; /* free space in buffer */ if (*source == '=') { /* 'literal' source */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* truncate it */ addstr(out, source + 1, bufflen - 1); *out = '\0'; } } else if (*source == '@') { /* file name */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* add '...' before rest of name */ addstr(out, RETS, LL(RETS)); bufflen -= LL(RETS); - memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); + memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); } } else { /* string; format as [string "source"] */ const char *nl = strchr(source, '\n'); /* find first new line (if any) */ addstr(out, PRE, LL(PRE)); /* add prefix */ bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (l < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, l); /* keep it */ + if (srclen < bufflen && nl == NULL) { /* small one-line source? */ + addstr(out, source, srclen); /* keep it */ } else { - if (nl != NULL) l = nl - source; /* stop at first newline */ - if (l > bufflen) l = bufflen; - addstr(out, source, l); + if (nl != NULL) srclen = nl - source; /* stop at first newline */ + if (srclen > bufflen) srclen = bufflen; + addstr(out, source, srclen); addstr(out, RETS, LL(RETS)); } memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 948e97040..08b6bd216 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lobject.h $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -17,288 +17,347 @@ /* -** Extra tags for non-values +** Extra types for collectable non-values */ -#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */ -#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */ +#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ +#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ + /* -** number of all possible tags (including LUA_TNONE but excluding DEADKEY) +** number of all possible types (including LUA_TNONE) */ -#define LUA_TOTALTAGS (LUA_TPROTO + 2) +#define LUA_TOTALTYPES (LUA_TPROTO + 2) /* ** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* value) +** bits 0-3: actual tag (a LUA_T* constant) ** bits 4-5: variant bits ** bit 6: whether value is collectable */ +/* add variant bits to a type */ +#define makevariant(t,v) ((t) | ((v) << 4)) + + /* -** LUA_TFUNCTION variants: -** 0 - Lua function -** 1 - light C function -** 2 - regular C function (closure) +** Union of all Lua values */ +typedef union Value { + struct GCObject *gc; /* collectable objects */ + void *p; /* light userdata */ + lua_CFunction f; /* light C functions */ + lua_Integer i; /* integer numbers */ + lua_Number n; /* float numbers */ +} Value; -/* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ +/* +** Tagged Values. This is the basic representation of values in Lua: +** an actual value plus a tag with its type. +*/ -/* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ +#define TValuefields Value value_; lu_byte tt_ +typedef struct TValue { + TValuefields; +} TValue; -/* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ + +#define val_(o) ((o)->value_) +#define valraw(o) (&val_(o)) -/* Bit mark for collectable types */ -#define BIT_ISCOLLECTABLE (1 << 6) +/* raw type tag of a TValue */ +#define rawtt(o) ((o)->tt_) -/* mark a tag as collectable */ -#define ctb(t) ((t) | BIT_ISCOLLECTABLE) +/* tag with no variants (bits 0-3) */ +#define novariant(t) ((t) & 0x0F) + +/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ +#define withvariant(t) ((t) & 0x3F) +#define ttypetag(o) withvariant(rawtt(o)) + +/* type of a TValue */ +#define ttype(o) (novariant(rawtt(o))) +/* Macros to test type */ +#define checktag(o,t) (rawtt(o) == (t)) +#define checktype(o,t) (ttype(o) == (t)) + + +/* Macros for internal tests */ + +/* collectable object has the same tag as the original value */ +#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) + /* -** Common type for all collectable objects +** Any value being manipulated by the program either is non +** collectable, or the collectable object has the right tag +** and it is not dead. The option 'L == NULL' allows other +** macros using this one to be used where L is not available. */ -typedef struct GCObject GCObject; +#define checkliveness(L,obj) \ + ((void)L, lua_longassert(!iscollectable(obj) || \ + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)) || isshared(gcvalue(obj)))))) +/* Macros to set values */ + +/* set a value's tag */ +#define settt_(o,t) ((o)->tt_=(t)) + + +/* main macro to copy values (from 'obj1' to 'obj2') */ +#define setobj(L,obj1,obj2) \ + { TValue *io1=(obj1); const TValue *io2=(obj2); \ + io1->value_ = io2->value_; settt_(io1, io2->tt_); \ + checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } + /* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) +** Different types of assignments, according to source and destination. +** (They are mostly equal now, but may be different in the future.) */ -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked + +/* from stack to stack */ +#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) +/* to stack (not from same stack) */ +#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) +/* from table to same table */ +#define setobjt2t setobj +/* to new object */ +#define setobj2n setobj +/* to table */ +#define setobj2t setobj /* -** Common type has only the common header +** Entries in the Lua stack */ -struct GCObject { - CommonHeader; -}; +typedef union StackValue { + TValue val; +} StackValue; +/* index to stack elements */ +typedef StackValue *StkId; + +/* convert a 'StackValue' to a 'TValue' */ +#define s2v(o) (&(o)->val) -/* -** 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 +** {================================================================== +** Nil +** =================================================================== */ -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; +/* Standard nil */ +#define LUA_VNIL makevariant(LUA_TNIL, 0) -#define TValuefields Value value_; int tt_ +/* Empty slot (which might be different from a slot containing nil) */ +#define LUA_VEMPTY makevariant(LUA_TNIL, 1) +/* Value returned for a key not found in a table (absent key) */ +#define LUA_VABSTKEY makevariant(LUA_TNIL, 2) -typedef struct lua_TValue { - TValuefields; -} TValue; +/* macro to test for (any kind of) nil */ +#define ttisnil(v) checktype((v), LUA_TNIL) -/* macro defining a nil value */ -#define NILCONSTANT {NULL}, LUA_TNIL +/* macro to test for a standard nil */ +#define ttisstrictnil(o) checktag((o), LUA_VNIL) -#define val_(o) ((o)->value_) +#define setnilvalue(obj) settt_(obj, LUA_VNIL) -/* raw type tag of a TValue */ -#define rttype(o) ((o)->tt_) +#define isabstkey(v) checktag((v), LUA_VABSTKEY) -/* tag with no variants (bits 0-3) */ -#define novariant(x) ((x) & 0x0F) -/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define ttype(o) (rttype(o) & 0x3F) +/* +** macro to detect non-standard nils (used only in assertions) +*/ +#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) -/* type tag of a TValue with no variants (bits 0-3) */ -#define ttnov(o) (novariant(rttype(o))) +/* +** By default, entries with any kind of nil are considered empty. +** (In any definition, values associated with absent keys must also +** be accepted as empty.) +*/ +#define isempty(v) ttisnil(v) -/* Macros to test type */ -#define checktag(o,t) (rttype(o) == (t)) -#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) -#define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) -#define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) -#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) +/* macro defining a value corresponding to an absent key */ +#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY -/* Macros to access values */ -#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 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), gco2t(val_(o).gc)) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#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)) -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) +/* mark an entry as empty */ +#define setempty(v) settt_(v, LUA_VEMPTY) -#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) +/* }================================================================== */ -/* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) -#define checkliveness(L,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)) || isshared(gcvalue(obj))))) +/* +** {================================================================== +** Booleans +** =================================================================== +*/ -/* Macros to set values */ -#define settt_(o,t) ((o)->tt_=(t)) +#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) +#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) -#define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } +#define ttisboolean(o) checktype((o), LUA_TBOOLEAN) +#define ttisfalse(o) checktag((o), LUA_VFALSE) +#define ttistrue(o) checktag((o), LUA_VTRUE) -#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 l_isfalse(o) (ttisfalse(o) || ttisnil(o)) -#define chgivalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } -#define setnilvalue(obj) settt_(obj, LUA_TNIL) +#define setbfvalue(obj) settt_(obj, LUA_VFALSE) +#define setbtvalue(obj) settt_(obj, LUA_VTRUE) -#define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } +/* }================================================================== */ -#define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } -#define setbvalue(obj,x) \ - { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } +/* +** {================================================================== +** Threads +** =================================================================== +*/ -#define setgcovalue(L,obj,x) \ - { TValue *io = (obj); GCObject *i_g=(x); \ - val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } +#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) -#define setsvalue(L,obj,x) \ - { TValue *io = (obj); TString *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ - checkliveness(L,io); } +#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) -#define setuvalue(L,obj,x) \ - { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(L,io); } +#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ 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(L,io); } +#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) -#define setclCvalue(L,obj,x) \ - { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ - 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(L,io); } -#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) +/* +** {================================================================== +** Collectable Objects +** =================================================================== +*/ +/* +** Common Header for all collectable objects (in macro form, to be +** included in other objects) +*/ +#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked -#define setobj(L,obj1,obj2) \ - { TValue *io1=(obj1); *io1 = *(obj2); \ - (void)L; checkliveness(L,io1); } +/* Common type for all collectable objects */ +typedef struct GCObject { + CommonHeader; +} GCObject; + + +/* Bit mark for collectable types */ +#define BIT_ISCOLLECTABLE (1 << 6) + +#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) + +/* mark a tag as collectable */ +#define ctb(t) ((t) | BIT_ISCOLLECTABLE) + +#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) + +#define gcvalueraw(v) ((v).gc) + +#define setgcovalue(L,obj,x) \ + { TValue *io = (obj); GCObject *i_g=(x); \ + val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } + +/* }================================================================== */ /* -** different types of assignments, according to destination +** {================================================================== +** Numbers +** =================================================================== */ -/* from stack to (same) stack */ -#define setobjs2s setobj -/* to stack (not from same stack) */ -#define setobj2s setobj -#define setsvalue2s setsvalue -#define sethvalue2s sethvalue -#define setptvalue2s setptvalue -/* from table to same table */ -#define setobjt2t setobj -/* to new object */ -#define setobj2n setobj -#define setsvalue2n setsvalue +/* Variant tags for numbers */ +#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ +#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ + +#define ttisnumber(o) checktype((o), LUA_TNUMBER) +#define ttisfloat(o) checktag((o), LUA_VNUMFLT) +#define ttisinteger(o) checktag((o), LUA_VNUMINT) + +#define nvalue(o) check_exp(ttisnumber(o), \ + (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) +#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) +#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) + +#define fltvalueraw(v) ((v).n) +#define ivalueraw(v) ((v).i) + +#define setfltvalue(obj,x) \ + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } -/* to table (define it as an expression to be used in macros) */ -#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1))) +#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_VNUMINT); } + +#define chgivalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } +/* }================================================================== */ /* -** {====================================================== -** types and prototypes -** ======================================================= +** {================================================================== +** Strings +** =================================================================== */ +/* Variant tags for strings */ +#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ +#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ -typedef TValue *StkId; /* index to stack elements */ +#define ttisstring(o) checktype((o), LUA_TSTRING) +#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) +#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) +#define tsvalueraw(v) (gco2ts((v).gc)) +#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) + +#define setsvalue(L,obj,x) \ + { TValue *io = (obj); TString *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ + checkliveness(L,io); } + +/* set a string to the stack */ +#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) + +/* set a string to a new object */ +#define setsvalue2n setsvalue /* -** Header for string value; string bytes follow the end of this structure -** (aligned according to 'UTString'; see next). +** Header for a string value. */ typedef struct TString { CommonHeader; @@ -310,75 +369,121 @@ typedef struct TString { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; + char contents[1]; } TString; -/* -** 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 getstr(ts) \ - check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString)) +#define getstr(ts) ((ts)->contents) /* 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) +#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) /* get string length from 'TValue *o' */ #define vslen(o) tsslen(tsvalue(o)) +/* }================================================================== */ + + +/* +** {================================================================== +** Userdata +** =================================================================== +*/ + /* -** Header for userdata; memory area follows the end of this structure -** (aligned according to 'UUdata'; see next). +** Light userdata should be a variant of userdata, but for compatibility +** reasons they are also different types. +*/ +#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) + +#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) + +#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) + +#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) +#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) + +#define pvalueraw(v) ((v).p) + +#define setpvalue(obj,x) \ + { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } + +#define setuvalue(L,obj,x) \ + { TValue *io = (obj); Udata *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ + checkliveness(L,io); } + + +/* Ensures that addresses after this type are always fully aligned. */ +typedef union UValue { + TValue uv; + LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ +} UValue; + + +/* +** Header for userdata with user values; +** memory area follows the end of this structure. */ typedef struct Udata { CommonHeader; - lu_byte ttuv_; /* user value's tag */ - struct Table *metatable; + unsigned short nuvalue; /* number of user values */ size_t len; /* number of bytes */ - union Value user_; /* user value */ + struct Table *metatable; + GCObject *gclist; + UValue uv[1]; /* user values */ } Udata; /* -** Ensures that address after this type is always fully aligned. +** Header for userdata with no user values. These userdata do not need +** to be gray during GC, and therefore do not need a 'gclist' field. +** To simplify, the code always use 'Udata' for both kinds of userdata, +** making sure it never accesses 'gclist' on userdata with no user values. +** This structure here is used only to compute the correct size for +** this representation. (The 'bindata' field in its end ensures correct +** alignment for binary data following this header.) */ -typedef union UUdata { - L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */ - Udata uv; -} UUdata; +typedef struct Udata0 { + CommonHeader; + unsigned short nuvalue; /* number of user values */ + size_t len; /* number of bytes */ + struct Table *metatable; + union {LUAI_MAXALIGN;} bindata; +} Udata0; -/* -** 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))) +/* compute the offset of the memory area of a userdata */ +#define udatamemoffset(nuv) \ + ((nuv) == 0 ? offsetof(Udata0, bindata) \ + : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) + +/* get the address of the memory block inside 'Udata' */ +#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) -#define setuservalue(L,u,o) \ - { const TValue *io=(o); Udata *iu = (u); \ - iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ - checkliveness(L,io); } +/* compute the size of a userdata */ +#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) +/* }================================================================== */ + + +/* +** {================================================================== +** Prototypes +** =================================================================== +*/ -#define getuservalue(L,u,o) \ - { TValue *io=(o); const Udata *iu = (u); \ - io->value_ = iu->user_; settt_(io, iu->ttuv_); \ - checkliveness(L,io); } +#define LUA_VPROTO makevariant(LUA_TPROTO, 0) /* @@ -388,6 +493,7 @@ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ + lu_byte kind; /* kind of corresponding variable */ } Upvaldesc; @@ -402,12 +508,27 @@ typedef struct LocVar { } LocVar; +/* +** Associates the absolute line source for a given instruction ('pc'). +** The array 'lineinfo' gives, for each instruction, the difference in +** lines from the previous instruction. When that difference does not +** fit into a byte, Lua saves the absolute line for that instruction. +** (Lua also saves the absolute line periodically, to speed up the +** computation of a line number: we can use binary search in the +** absolute-line array, but we must traverse the 'lineinfo' array +** linearly to compute a line.) +*/ +typedef struct AbsLineInfo { + int pc; + int line; +} AbsLineInfo; + /* ** Function Prototypes */ typedef struct Proto { CommonHeader; - lu_byte numparams; /* number of fixed parameters */ + lu_byte numparams; /* number of fixed (named) parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ @@ -416,29 +537,85 @@ typedef struct Proto { int sizelineinfo; int sizep; /* size of 'p' */ int sizelocvars; + int sizeabslineinfo; /* size of 'abslineinfo' */ int linedefined; /* debug information */ int lastlinedefined; /* debug information */ 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 */ + ls_byte *lineinfo; /* information about source lines (debug information) */ + AbsLineInfo *abslineinfo; /* idem */ + LocVar *locvars; /* information about local variables (debug information) */ TString *source; /* used for debug information */ GCObject *gclist; } Proto; +/* }================================================================== */ /* -** Lua Upvalues +** {================================================================== +** Closures +** =================================================================== */ -typedef struct UpVal UpVal; + +#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) + + +/* Variant tags for functions */ +#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ +#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ +#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ + +#define ttisfunction(o) checktype(o, LUA_TFUNCTION) +#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL) +#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) +#define ttislcf(o) checktag((o), LUA_VLCF) +#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) + +#define isLfunction(o) ttisLclosure(o) + +#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) +#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) +#define fvalue(o) check_exp(ttislcf(o), val_(o).f) +#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) + +#define fvalueraw(v) ((v).f) + +#define setclLvalue(L,obj,x) \ + { TValue *io = (obj); LClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ + checkliveness(L,io); } + +#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) + +#define setfvalue(obj,x) \ + { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } + +#define setclCvalue(L,obj,x) \ + { TValue *io = (obj); CClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ + checkliveness(L,io); } /* -** Closures +** Upvalues for Lua closures */ +typedef struct UpVal { + CommonHeader; + lu_byte tbc; /* true if it represents a to-be-closed variable */ + TValue *v; /* points to stack or to its own value */ + union { + struct { /* (when open) */ + struct UpVal *next; /* linked list */ + struct UpVal **previous; + } open; + TValue value; /* the value (when closed) */ + } u; +} UpVal; + + #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist @@ -463,42 +640,81 @@ typedef union Closure { } Closure; -#define isLfunction(o) ttisLclosure(o) - #define getproto(o) (clLvalue(o)->p) +/* }================================================================== */ + /* +** {================================================================== ** Tables +** =================================================================== */ -typedef union TKey { - struct { - TValuefields; - int next; /* for chaining (offset for next node) */ - } nk; - TValue tvk; -} TKey; +#define LUA_VTABLE makevariant(LUA_TTABLE, 0) + +#define ttistable(o) checktag((o), ctb(LUA_VTABLE)) + +#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) +#define sethvalue(L,obj,x) \ + { TValue *io = (obj); Table *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ + checkliveness(L,io); } -/* copy a value into a key without messing up field 'next' */ -#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(L,io_); } +#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) -typedef struct Node { - TValue i_val; - TKey i_key; +/* +** Nodes for Hash tables: A pack of two TValue's (key-value pairs) +** plus a 'next' field to link colliding entries. The distribution +** of the key's fields ('key_tt' and 'key_val') not forming a proper +** 'TValue' allows for a smaller size for 'Node' both in 4-byte +** and 8-byte alignments. +*/ +typedef union Node { + struct NodeKey { + TValuefields; /* fields for value */ + lu_byte key_tt; /* key type */ + int next; /* for chaining */ + Value key_val; /* key value */ + } u; + TValue i_val; /* direct access to node's value as a proper 'TValue' */ } Node; +/* copy a value into a key */ +#define setnodekey(L,node,obj) \ + { Node *n_=(node); const TValue *io_=(obj); \ + n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ + checkliveness(L,io_); } + + +/* copy a value from a key */ +#define getnodekey(L,obj,node) \ + { TValue *io_=(obj); const Node *n_=(node); \ + io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ + checkliveness(L,io_); } + + +/* +** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the +** real size of 'array'. Otherwise, the real size of 'array' is the +** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' +** is zero); 'alimit' is then used as a hint for #t. +*/ + +#define BITRAS (1 << 7) +#define isrealasize(t) (!((t)->flags & BITRAS)) +#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) +#define setnorealasize(t) ((t)->flags |= BITRAS) + + typedef struct Table { CommonHeader; lu_byte flags; /* 1<

u.key_tt) +#define keyval(node) ((node)->u.key_val) + +#define keyisnil(node) (keytt(node) == LUA_TNIL) +#define keyisinteger(node) (keytt(node) == LUA_VNUMINT) +#define keyival(node) (keyval(node).i) +#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) +#define keystrval(node) (gco2ts(keyval(node).gc)) + +#define setnilkey(node) (keytt(node) = LUA_TNIL) + +#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) + +#define gckey(n) (keyval(n).gc) +#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) + /* -** 'module' operation for hashing (size is always a power of 2) +** Use a "nil table" to mark dead keys in a table. Those keys serve +** to keep space for removed entries, which may still be part of +** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE +** set, so these values are considered not collectable and are different +** from any valid value. */ -#define lmod(s,size) \ - (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) +#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) +/* }================================================================== */ -#define twoto(x) (1<<(x)) -#define sizenode(t) (twoto((t)->lsizenode)) /* -** (address of) a fixed nil value +** 'module' operation for hashing (size is always a power of 2) */ -#define luaO_nilobject (&luaO_nilobject_) +#define lmod(s,size) \ + (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) + +#define twoto(x) (1<<(x)) +#define sizenode(t) (twoto((t)->lsizenode)) -LUAI_DDEC const TValue luaO_nilobject_; /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC int luaO_ceillog2 (unsigned int x); +LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, + const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, - const TValue *p2, TValue *res); + const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); LUAI_FUNC int luaO_hexavalue (int c); -LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj); +LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); #endif diff --git a/3rd/lua/lopcodes.c b/3rd/lua/lopcodes.c index 5ca3eb261..c67aa227c 100644 --- a/3rd/lua/lopcodes.c +++ b/3rd/lua/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lopcodes.c $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -10,115 +10,95 @@ #include "lprefix.h" -#include - #include "lopcodes.h" /* ORDER OP */ -LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { - "MOVE", - "LOADK", - "LOADKX", - "LOADBOOL", - "LOADNIL", - "GETUPVAL", - "GETTABUP", - "GETTABLE", - "SETTABUP", - "SETUPVAL", - "SETTABLE", - "NEWTABLE", - "SELF", - "ADD", - "SUB", - "MUL", - "MOD", - "POW", - "DIV", - "IDIV", - "BAND", - "BOR", - "BXOR", - "SHL", - "SHR", - "UNM", - "BNOT", - "NOT", - "LEN", - "CONCAT", - "JMP", - "EQ", - "LT", - "LE", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "FORLOOP", - "FORPREP", - "TFORCALL", - "TFORLOOP", - "SETLIST", - "CLOSURE", - "VARARG", - "EXTRAARG", - NULL -}; - - -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) - LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A B C mode opcode */ - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ - ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ - ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ - ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ - ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ +/* MM OT IT T A mode opcode */ + opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ + ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ + ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ + ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 6feaa1cd0..122e5d21f 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.149.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lopcodes.h $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -11,69 +11,94 @@ /*=========================================================================== - We assume that instructions are unsigned numbers. - All instructions have an opcode in the first 6 bits. - Instructions can have the following fields: - 'A' : 8 bits - 'B' : 9 bits - 'C' : 9 bits - 'Ax' : 26 bits ('A', 'B', and 'C' together) - 'Bx' : 18 bits ('B' and 'C' together) - 'sBx' : signed Bx - - A signed argument is represented in excess K; that is, the number - value is the unsigned value minus K. K is exactly the maximum value - for that argument (so that -max is represented by 0, and +max is - represented by 2*max), which is half the maximum for the corresponding - unsigned argument. + We assume that instructions are unsigned 32-bit integers. + All instructions have an opcode in the first 7 bits. + Instructions can have the following formats: + + 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 + 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 +iABC C(8) | B(8) |k| A(8) | Op(7) | +iABx Bx(17) | A(8) | Op(7) | +iAsBx sBx (signed)(17) | A(8) | Op(7) | +iAx Ax(25) | Op(7) | +isJ sJ(25) | Op(7) | + + A signed argument is represented in excess K: the represented value is + the written unsigned value minus K, where K is half the maximum for the + corresponding unsigned argument. ===========================================================================*/ -enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ +enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* ** size and position of opcode arguments. */ -#define SIZE_C 9 -#define SIZE_B 9 -#define SIZE_Bx (SIZE_C + SIZE_B) +#define SIZE_C 8 +#define SIZE_B 8 +#define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 -#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) +#define SIZE_Ax (SIZE_Bx + SIZE_A) +#define SIZE_sJ (SIZE_Bx + SIZE_A) -#define SIZE_OP 6 +#define SIZE_OP 7 #define POS_OP 0 + #define POS_A (POS_OP + SIZE_OP) -#define POS_C (POS_A + SIZE_A) -#define POS_B (POS_C + SIZE_C) -#define POS_Bx POS_C +#define POS_k (POS_A + SIZE_A) +#define POS_B (POS_k + 1) +#define POS_C (POS_B + SIZE_B) + +#define POS_Bx POS_k + #define POS_Ax POS_A +#define POS_sJ POS_A + /* ** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) +** we use (signed) 'int' to manipulate most arguments, +** so they must fit in ints. */ -#if SIZE_Bx < LUAI_BITSINT-1 -#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ + +/* Check whether type 'int' has at least 'b' bits ('b' < 32) */ +#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1) + + +#if L_INTHASBITS(SIZE_Bx) +#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ + + +#if L_INTHASBITS(SIZE_Ax) #define MAXARG_Ax ((1<> 1) + + +#define MAXARG_A ((1<> 1) + +#define int2sC(i) ((i) + OFFSET_sC) +#define sC2int(i) ((i) - OFFSET_sC) /* creates a mask with 'n' 1 bits at position 'p' */ @@ -90,33 +115,49 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ ((cast(Instruction, o)<>pos) & MASK1(size,0))) +#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m) + + +#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ ((cast(Instruction, v)<> 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_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++ */ - -OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ +OP_MOVE,/* A B R[A] := R[B] */ +OP_LOADI,/* A sBx R[A] := sBx */ +OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ +OP_LOADK,/* A Bx R[A] := K[Bx] */ +OP_LOADKX,/* A R[A] := K[extra arg] */ +OP_LOADFALSE,/* A R[A] := false */ +OP_LFALSESKIP,/*A R[A] := false; pc++ */ +OP_LOADTRUE,/* A R[A] := true */ +OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ +OP_GETUPVAL,/* A B R[A] := UpValue[B] */ +OP_SETUPVAL,/* A B UpValue[B] := R[A] */ + +OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */ +OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */ +OP_GETI,/* A B C R[A] := R[B][C] */ +OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */ + +OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */ +OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ +OP_SETI,/* A B C R[A][B] := RK(C) */ +OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */ + +OP_NEWTABLE,/* A B C k R[A] := {} */ + +OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ + +OP_ADDI,/* A B sC R[A] := R[B] + sC */ + +OP_ADDK,/* A B C R[A] := R[B] + K[C] */ +OP_SUBK,/* A B C R[A] := R[B] - K[C] */ +OP_MULK,/* A B C R[A] := R[B] * K[C] */ +OP_MODK,/* A B C R[A] := R[B] % K[C] */ +OP_POWK,/* A B C R[A] := R[B] ^ K[C] */ +OP_DIVK,/* A B C R[A] := R[B] / K[C] */ +OP_IDIVK,/* A B C R[A] := R[B] // K[C] */ + +OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ +OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ +OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */ + +OP_SHRI,/* A B sC R[A] := R[B] >> sC */ +OP_SHLI,/* A B sC R[A] := sC << R[B] */ + +OP_ADD,/* A B C R[A] := R[B] + R[C] */ +OP_SUB,/* A B C R[A] := R[B] - R[C] */ +OP_MUL,/* A B C R[A] := R[B] * R[C] */ +OP_MOD,/* A B C R[A] := R[B] % R[C] */ +OP_POW,/* A B C R[A] := R[B] ^ R[C] */ +OP_DIV,/* A B C R[A] := R[B] / R[C] */ +OP_IDIV,/* A B C R[A] := R[B] // R[C] */ + +OP_BAND,/* A B C R[A] := R[B] & R[C] */ +OP_BOR,/* A B C R[A] := R[B] | R[C] */ +OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ +OP_SHL,/* A B C R[A] := R[B] << R[C] */ +OP_SHR,/* A B C R[A] := R[B] >> R[C] */ + +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ +OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ +OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ + +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 R[A] := R[A].. ... ..R[A + B - 1] */ + +OP_CLOSE,/* A close all upvalues >= R[A] */ +OP_TBC,/* A mark variable A "to be closed" */ +OP_JMP,/* sJ pc += sJ */ +OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ +OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ +OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ + +OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ +OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ +OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ +OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ +OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ +OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ + +OP_TEST,/* A k if (not R[A] == k) then pc++ */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ + +OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ +OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ + +OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ +OP_RETURN0,/* return */ +OP_RETURN1,/* A return R[A] */ + +OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ +OP_FORPREP,/* A Bx ; + if not to run then pc+=Bx+1; */ + +OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ +OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ +OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ + +OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ + +OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ + +OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ - -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) 0 means + the function is vararg, so that its 'func' must be corrected before + returning; in this case, (C - 1) is its number of fixed parameters. + + (*) In comparisons with an immediate operand, C signals whether the + original operand was a float. (It must be corrected in case of + metamethods.) + ===========================================================================*/ /* ** masks for instruction properties. The format is: -** bits 0-1: op mode -** bits 2-3: C arg mode -** bits 4-5: B arg mode -** bit 6: instruction set register A -** bit 7: operator is a test (next instruction must be a jump) +** bits 0-2: op mode +** bit 3: instruction set register A +** bit 4: operator is a test (next instruction must be a jump) +** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) +** bit 6: instruction sets 'L->top' for next instruction (when C == 0) +** bit 7: instruction is an MM instruction (call a metamethod) */ -enum OpArgMask { - OpArgN, /* argument is not used */ - OpArgU, /* argument is used */ - OpArgR, /* argument is a register or a jump offset */ - OpArgK /* argument is a constant or register/constant */ -}; +LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) -LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; +#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) +#define testAMode(m) (luaP_opmodes[m] & (1 << 3)) +#define testTMode(m) (luaP_opmodes[m] & (1 << 4)) +#define testITMode(m) (luaP_opmodes[m] & (1 << 5)) +#define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) -#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) -#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) +/* "out top" (set top for next instruction) */ +#define isOT(i) \ + ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ + GET_OPCODE(i) == OP_TAILCALL) +/* "in top" (uses top from previous instruction) */ +#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) -LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ +#define opmode(mm,ot,it,t,a,m) \ + (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) /* number of list items to accumulate before a SETLIST instruction */ #define LFIELDS_PER_FLUSH 50 - #endif diff --git a/3rd/lua/lopnames.h b/3rd/lua/lopnames.h new file mode 100644 index 000000000..965cec9bf --- /dev/null +++ b/3rd/lua/lopnames.h @@ -0,0 +1,103 @@ +/* +** $Id: lopnames.h $ +** Opcode names +** See Copyright Notice in lua.h +*/ + +#if !defined(lopnames_h) +#define lopnames_h + +#include + + +/* ORDER OP */ + +static const char *const opnames[] = { + "MOVE", + "LOADI", + "LOADF", + "LOADK", + "LOADKX", + "LOADFALSE", + "LFALSESKIP", + "LOADTRUE", + "LOADNIL", + "GETUPVAL", + "SETUPVAL", + "GETTABUP", + "GETTABLE", + "GETI", + "GETFIELD", + "SETTABUP", + "SETTABLE", + "SETI", + "SETFIELD", + "NEWTABLE", + "SELF", + "ADDI", + "ADDK", + "SUBK", + "MULK", + "MODK", + "POWK", + "DIVK", + "IDIVK", + "BANDK", + "BORK", + "BXORK", + "SHRI", + "SHLI", + "ADD", + "SUB", + "MUL", + "MOD", + "POW", + "DIV", + "IDIV", + "BAND", + "BOR", + "BXOR", + "SHL", + "SHR", + "MMBIN", + "MMBINI", + "MMBINK", + "UNM", + "BNOT", + "NOT", + "LEN", + "CONCAT", + "CLOSE", + "TBC", + "JMP", + "EQ", + "LT", + "LE", + "EQK", + "EQI", + "LTI", + "LEI", + "GTI", + "GEI", + "TEST", + "TESTSET", + "CALL", + "TAILCALL", + "RETURN", + "RETURN0", + "RETURN1", + "FORLOOP", + "FORPREP", + "TFORPREP", + "TFORCALL", + "TFORLOOP", + "SETLIST", + "CLOSURE", + "VARARG", + "VARARGPREP", + "EXTRAARG", + NULL +}; + +#endif + diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index de590c6b7..e65e188bd 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: loslib.c $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -59,18 +59,20 @@ ** =================================================================== */ -#if !defined(l_time_t) /* { */ /* ** type to represent time_t in Lua */ +#if !defined(LUA_NUMTIME) /* { */ + #define l_timet lua_Integer #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) +#define l_gettime(L,arg) luaL_checkinteger(L, arg) -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; -} +#else /* }{ */ + +#define l_timet lua_Number +#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) +#define l_gettime(L,arg) luaL_checknumber(L, arg) #endif /* } */ @@ -90,7 +92,7 @@ static time_t l_checktime (lua_State *L, int arg) { /* 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)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) #endif /* } */ @@ -137,10 +139,11 @@ static time_t l_checktime (lua_State *L, int arg) { - static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); + int stat; + errno = 0; + stat = system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { @@ -194,11 +197,25 @@ static int os_clock (lua_State *L) { ** ======================================================= */ -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); +/* +** About the overflow check: an overflow cannot occur when time +** is represented by a lua_Integer, because either lua_Integer is +** large enough to represent all int fields or it is not large enough +** to represent a time that cause a field to overflow. However, if +** times are represented as doubles and lua_Integer is int, then the +** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 +** to compute the year. +*/ +static void setfield (lua_State *L, const char *key, int value, int delta) { + #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) + if (value > LUA_MAXINTEGER - delta) + luaL_error(L, "field '%s' is out-of-bound", key); + #endif + lua_pushinteger(L, (lua_Integer)value + delta); lua_setfield(L, -2, key); } + static void setboolfield (lua_State *L, const char *key, int value) { if (value < 0) /* undefined? */ return; /* does not set field */ @@ -211,14 +228,14 @@ static void setboolfield (lua_State *L, const char *key, int value) { ** 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); + setfield(L, "year", stm->tm_year, 1900); + setfield(L, "month", stm->tm_mon, 1); + setfield(L, "day", stm->tm_mday, 0); + setfield(L, "hour", stm->tm_hour, 0); + setfield(L, "min", stm->tm_min, 0); + setfield(L, "sec", stm->tm_sec, 0); + setfield(L, "yday", stm->tm_yday, 1); + setfield(L, "wday", stm->tm_wday, 1); setboolfield(L, "isdst", stm->tm_isdst); } @@ -231,11 +248,6 @@ static int getboolfield (lua_State *L, const char *key) { } -/* 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); /* get field and its type */ @@ -248,7 +260,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { res = d; } else { - if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + /* unsigned avoids overflow when lua_Integer has 32 bits */ + if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta + : (lua_Integer)INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } @@ -276,6 +290,13 @@ static const char *checkoption (lua_State *L, const char *conv, } +static time_t l_checktime (lua_State *L, int arg) { + l_timet t = l_gettime(L, arg); + luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); + return (time_t)t; +} + + /* maximum size for an individual 'strftime' item */ #define SIZETIMEFMT 250 @@ -294,7 +315,7 @@ static int os_date (lua_State *L) { stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, - "time result cannot be represented in this installation"); + "date result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); @@ -330,12 +351,12 @@ 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, 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_mon = getfield(L, "month", -1, 1); + ts.tm_mday = getfield(L, "day", -1, 0); + ts.tm_hour = getfield(L, "hour", 12, 0); + ts.tm_min = getfield(L, "min", 0, 0); + ts.tm_sec = getfield(L, "sec", 0, 0); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); setallfields(L, &ts); /* update fields with normalized values */ diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index cc54de43c..bc7d9a4f2 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $ +** $Id: lparser.c $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -52,6 +53,7 @@ typedef struct BlockCnt { 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 insidetbc; /* true if inside the scope of a to-be-closed var. */ } BlockCnt; @@ -63,13 +65,6 @@ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); -/* semantic error */ -static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove "near " from final message */ - luaX_syntaxerror(ls, msg); -} - - static l_noret error_expected (LexState *ls, int token) { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); @@ -94,6 +89,9 @@ static void checklimit (FuncState *fs, int v, int l, const char *what) { } +/* +** Test whether next token is 'c'; if so, skip it. +*/ static int testnext (LexState *ls, int c) { if (ls->t.token == c) { luaX_next(ls); @@ -103,12 +101,18 @@ static int testnext (LexState *ls, int c) { } +/* +** Check that next token is 'c'. +*/ static void check (LexState *ls, int c) { if (ls->t.token != c) error_expected(ls, c); } +/* +** Check that next token is 'c' and skip it. +*/ static void checknext (LexState *ls, int c) { check(ls, c); luaX_next(ls); @@ -118,11 +122,15 @@ static void checknext (LexState *ls, int c) { #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - +/* +** Check that next token is 'what' and skip it. In case of error, +** raise an error that the expected 'what' should match a 'who' +** in line 'where' (if that is not the current line). +*/ static void check_match (LexState *ls, int what, int who, int where) { - if (!testnext(ls, what)) { - if (where == ls->linenumber) - error_expected(ls, what); + if (unlikely(!testnext(ls, what))) { + if (where == ls->linenumber) /* all in the same line? */ + error_expected(ls, what); /* do not need a complex message */ else { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected (to close %s at line %d)", @@ -148,73 +156,189 @@ static void init_exp (expdesc *e, expkind k, int i) { } -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); +static void codestring (expdesc *e, TString *s) { + e->f = e->t = NO_JUMP; + e->k = VKSTR; + e->u.strval = s; } -static void checkname (LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); +static void codename (LexState *ls, expdesc *e) { + codestring(e, str_checkname(ls)); } -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; +/* +** Register a new local variable in the active 'Proto' (for debug +** information). +*/ +static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, + luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; + f->locvars[fs->ndebugvars].varname = varname; + f->locvars[fs->ndebugvars].startpc = fs->pc; luaC_objbarrier(ls->L, f, varname); - return fs->nlocvars++; + return fs->ndebugvars++; } -static void new_localvar (LexState *ls, TString *name) { +/* +** Create a new local variable with the given 'name'. Return its index +** in the function. +*/ +static int new_localvar (LexState *ls, TString *name) { + lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; - int reg = registerlocalvar(ls, name); + Vardesc *var; checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); - luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, MAX_INT, "local variables"); - dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); + MAXVARS, "local variables"); + luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, + dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); + var = &dyd->actvar.arr[dyd->actvar.n++]; + var->vd.kind = VDKREG; /* default */ + var->vd.name = name; + return dyd->actvar.n - 1 - fs->firstlocal; } +#define new_localvarliteral(ls,v) \ + new_localvar(ls, \ + luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); + -static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { - new_localvar(ls, luaX_newstring(ls, name, sz)); + +/* +** Return the "variable description" (Vardesc) of a given variable. +** (Unless noted otherwise, all variables are referred to by their +** compiler indices.) +*/ +static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { + return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; } -#define new_localvarliteral(ls,v) \ - new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) + +/* +** Convert 'nvar', a compiler index level, to it corresponding +** stack index level. For that, search for the highest variable +** below that level that is in the stack and uses its stack +** index ('sidx'). +*/ +static int stacklevel (FuncState *fs, int nvar) { + while (nvar-- > 0) { + Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ + if (vd->vd.kind != RDKCTC) /* is in the stack? */ + return vd->vd.sidx + 1; + } + return 0; /* no variables in the stack */ +} + + +/* +** Return the number of variables in the stack for function 'fs' +*/ +int luaY_nvarstack (FuncState *fs) { + return stacklevel(fs, fs->nactvar); +} -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]; +/* +** Get the debug-information entry for current variable 'vidx'. +*/ +static LocVar *localdebuginfo (FuncState *fs, int vidx) { + Vardesc *vd = getlocalvardesc(fs, vidx); + if (vd->vd.kind == RDKCTC) + return NULL; /* no debug info. for constants */ + else { + int idx = vd->vd.pidx; + lua_assert(idx < fs->ndebugvars); + return &fs->f->locvars[idx]; + } } +/* +** Create an expression representing variable 'vidx' +*/ +static void init_var (FuncState *fs, expdesc *e, int vidx) { + e->f = e->t = NO_JUMP; + e->k = VLOCAL; + e->u.var.vidx = vidx; + e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; +} + + +/* +** Raises an error if variable described by 'e' is read only +*/ +static void check_readonly (LexState *ls, expdesc *e) { + FuncState *fs = ls->fs; + TString *varname = NULL; /* to be set if variable is const */ + switch (e->k) { + case VCONST: { + varname = ls->dyd->actvar.arr[e->u.info].vd.name; + break; + } + case VLOCAL: { + Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); + if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ + varname = vardesc->vd.name; + break; + } + case VUPVAL: { + Upvaldesc *up = &fs->f->upvalues[e->u.info]; + if (up->kind != VDKREG) + varname = up->name; + break; + } + default: + return; /* other cases cannot be read-only */ + } + if (varname) { + const char *msg = luaO_pushfstring(ls->L, + "attempt to assign to const variable '%s'", getstr(varname)); + luaK_semerror(ls, msg); /* error */ + } +} + + +/* +** Start the scope for the last 'nvars' created variables. +*/ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; + int stklevel = luaY_nvarstack(fs); + int i; + for (i = 0; i < nvars; i++) { + int vidx = fs->nactvar++; + Vardesc *var = getlocalvardesc(fs, vidx); + var->vd.sidx = stklevel++; + var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } } +/* +** Close the scope for all variables up to level 'tolevel'. +** (debug info.) +*/ static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar)->endpc = fs->pc; + while (fs->nactvar > tolevel) { + LocVar *var = localdebuginfo(fs, --fs->nactvar); + if (var) /* does it have debug information? */ + var->endpc = fs->pc; + } } +/* +** Search the upvalues of the function 'fs' for one +** with the given 'name'. +*/ static int searchupvalue (FuncState *fs, TString *name) { int i; Upvaldesc *up = fs->f->upvalues; @@ -225,7 +349,7 @@ static int searchupvalue (FuncState *fs, TString *name) { } -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { +static Upvaldesc *allocupvalue (FuncState *fs) { Proto *f = fs->f; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); @@ -233,58 +357,87 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { Upvaldesc, MAXUPVAL, "upvalues"); 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; - luaC_objbarrier(fs->ls->L, f, name); - return fs->nups++; + return &f->upvalues[fs->nups++]; } -static int searchvar (FuncState *fs, TString *n) { +static int newupvalue (FuncState *fs, TString *name, expdesc *v) { + Upvaldesc *up = allocupvalue(fs); + FuncState *prev = fs->prev; + if (v->k == VLOCAL) { + up->instack = 1; + up->idx = v->u.var.sidx; + up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; + lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); + } + else { + up->instack = 0; + up->idx = cast_byte(v->u.info); + up->kind = prev->f->upvalues[v->u.info].kind; + lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); + } + up->name = name; + luaC_objbarrier(fs->ls->L, fs->f, name); + return fs->nups - 1; +} + + +/* +** Look for an active local variable with the name 'n' in the +** function 'fs'. If found, initialize 'var' with it and return +** its expression kind; otherwise return -1. +*/ +static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, getlocvar(fs, i)->varname)) - return i; + Vardesc *vd = getlocalvardesc(fs, i); + if (eqstr(n, vd->vd.name)) { /* found? */ + if (vd->vd.kind == RDKCTC) /* compile-time constant? */ + init_exp(var, VCONST, fs->firstlocal + i); + else /* real variable */ + init_var(fs, var, i); + return var->k; + } } return -1; /* not found */ } /* - Mark block where variable at given level was defined - (to emit close instructions later). +** Mark block where variable at given level was defined +** (to emit close instructions later). */ static void markupval (FuncState *fs, int level) { BlockCnt *bl = fs->bl; while (bl->nactvar > level) bl = bl->previous; bl->upval = 1; + fs->needclose = 1; } /* - Find variable with given name 'n'. If it is an upvalue, add this - upvalue into all intermediate functions. +** Find a variable with the given name 'n'. If it is an upvalue, add +** this upvalue into all intermediate functions. If it is a global, set +** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ init_exp(var, VVOID, 0); /* default is global */ else { - int v = searchvar(fs, n); /* look up locals at current level */ + int v = searchvar(fs, n, var); /* 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 */ + if (v == VLOCAL && !base) + markupval(fs, var->u.var.vidx); /* local will be used as an upval */ } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ 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 */ + if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ + idx = newupvalue(fs, n, var); /* will be a new upvalue */ + else /* it is a global or a constant */ + return; /* don't need to do anything at this level */ } init_exp(var, VUPVAL, idx); /* new or old upvalue */ } @@ -292,6 +445,10 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { } +/* +** Find a variable with the given name 'n', handling global variables +** too. +*/ static void singlevar (LexState *ls, expdesc *var) { TString *varname = str_checkname(ls); FuncState *fs = ls->fs; @@ -300,88 +457,96 @@ static void singlevar (LexState *ls, expdesc *var) { expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ lua_assert(var->k != VVOID); /* this one must exist */ - codestring(ls, &key, varname); /* key is variable name */ + codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } } +/* +** Adjust the number of results from an expression list 'e' with 'nexps' +** expressions to 'nvars' values. +*/ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; - int extra = nvars - nexps; - if (hasmultret(e->k)) { - extra++; /* includes call itself */ - if (extra < 0) extra = 0; + int needed = nvars - nexps; /* extra values needed */ + if (hasmultret(e->k)) { /* last expression has multiple returns? */ + int extra = needed + 1; /* discount last expression itself */ + if (extra < 0) + extra = 0; luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - if (extra > 1) luaK_reserveregs(fs, extra-1); } else { - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ - if (extra > 0) { - int reg = fs->freereg; - luaK_reserveregs(fs, extra); - luaK_nil(fs, reg, extra); - } + if (e->k != VVOID) /* at least one expression? */ + luaK_exp2nextreg(fs, e); /* close last expression */ + if (needed > 0) /* missing values? */ + luaK_nil(fs, fs->freereg, needed); /* complete with nils */ } - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ + if (needed > 0) + luaK_reserveregs(fs, needed); /* registers for extra values */ + else /* adding 'needed' is actually a subtraction */ + fs->freereg += needed; /* remove extra values */ } -static void enterlevel (LexState *ls) { - lua_State *L = ls->L; - ++L->nCcalls; - checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); -} +/* +** Macros to limit the maximum recursion depth while parsing +*/ +#define enterlevel(ls) luaE_enterCcall((ls)->L) + +#define leavelevel(ls) luaE_exitCcall((ls)->L) -#define leavelevel(ls) ((ls)->L->nCcalls--) +/* +** Generates an error that a goto jumps into the scope of some +** local variable. +*/ +static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { + const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); + const char *msg = " at line %d jumps into the scope of local '%s'"; + msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); + luaK_semerror(ls, msg); /* raise the error */ +} -static void closegoto (LexState *ls, int g, Labeldesc *label) { +/* +** Solves the goto at index 'g' to given 'label' and removes it +** from the list of pending goto's. +** If it jumps into the scope of some variable, raises an error. +*/ +static void solvegoto (LexState *ls, int g, Labeldesc *label) { int i; - FuncState *fs = ls->fs; - Labellist *gl = &ls->dyd->gt; - Labeldesc *gt = &gl->arr[g]; + Labellist *gl = &ls->dyd->gt; /* list of goto's */ + Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ 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 '%s'", - getstr(gt->name), gt->line, getstr(vname)); - semerror(ls, msg); - } - luaK_patchlist(fs, gt->pc, label->pc); - /* remove goto from pending list */ - for (i = g; i < gl->n - 1; i++) + if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ + jumpscopeerror(ls, gt); + luaK_patchlist(ls->fs, gt->pc, label->pc); + for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ gl->arr[i] = gl->arr[i + 1]; gl->n--; } /* -** try to close a goto with existing labels; this solves backward jumps +** Search for an active label with the given name. */ -static int findlabel (LexState *ls, int g) { +static Labeldesc *findlabel (LexState *ls, TString *name) { int i; - BlockCnt *bl = ls->fs->bl; Dyndata *dyd = ls->dyd; - Labeldesc *gt = &dyd->gt.arr[g]; - /* check labels in current block for a match */ - for (i = bl->firstlabel; i < dyd->label.n; i++) { + /* check labels in current function for a match */ + for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { Labeldesc *lb = &dyd->label.arr[i]; - 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); - closegoto(ls, g, lb); /* close it */ - return 1; - } + if (eqstr(lb->name, name)) /* correct label? */ + return lb; } - return 0; /* label not found; cannot close goto */ + return NULL; /* label not found */ } +/* +** Adds a new label/goto in the corresponding list. +*/ static int newlabelentry (LexState *ls, Labellist *l, TString *name, int line, int pc) { int n = l->n; @@ -390,48 +555,76 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, l->arr[n].name = name; l->arr[n].line = line; l->arr[n].nactvar = ls->fs->nactvar; + l->arr[n].close = 0; l->arr[n].pc = pc; l->n = n + 1; return n; } +static int newgotoentry (LexState *ls, TString *name, int line, int pc) { + return newlabelentry(ls, &ls->dyd->gt, name, line, pc); +} + + /* -** check whether new label 'lb' matches any pending gotos in current -** block; solves forward jumps +** Solves forward jumps. Check whether new label 'lb' matches any +** pending gotos in current block and solves them. Return true +** if any of the goto's need to close upvalues. */ -static void findgotos (LexState *ls, Labeldesc *lb) { +static int solvegotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; int i = ls->fs->bl->firstgoto; + int needsclose = 0; while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) - closegoto(ls, i, lb); + if (eqstr(gl->arr[i].name, lb->name)) { + needsclose |= gl->arr[i].close; + solvegoto(ls, i, lb); /* will remove 'i' from the list */ + } else i++; } + return needsclose; } /* -** 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. +** Create a new label with the given 'name' at the given 'line'. +** 'last' tells whether label is the last non-op statement in its +** block. Solves all pending goto's to this new label and adds +** a close instruction if necessary. +** Returns true iff it added a close instruction. +*/ +static int createlabel (LexState *ls, TString *name, int line, + int last) { + FuncState *fs = ls->fs; + Labellist *ll = &ls->dyd->label; + int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); + if (last) { /* label is last no-op statement in the block? */ + /* assume that locals are already out of scope */ + ll->arr[l].nactvar = fs->bl->nactvar; + } + if (solvegotos(ls, &ll->arr[l])) { /* need close? */ + luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); + return 1; + } + return 0; +} + + +/* +** Adjust pending gotos to outer level of a block. */ static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i = bl->firstgoto; + int i; Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block and try to close it - with visible labels */ - while (i < gl->n) { + /* correct pending gotos to current block */ + for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { - if (bl->upval) - luaK_patchclose(fs, gt->pc, bl->nactvar); - gt->nactvar = bl->nactvar; - } - if (!findlabel(fs->ls, i)) - i++; /* move to next one */ + /* leaving a variable scope? */ + if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) + gt->close |= bl->upval; /* jump may need a close */ + gt->nactvar = bl->nactvar; /* update goto level */ } } @@ -442,54 +635,50 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; bl->upval = 0; + bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); bl->previous = fs->bl; fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); + lua_assert(fs->freereg == luaY_nvarstack(fs)); } /* -** create a label named 'break' to resolve break statements -*/ -static void breaklabel (LexState *ls) { - TString *n = luaS_new(ls->L, "break"); - int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); - findgotos(ls, &ls->dyd->label.arr[l]); -} - -/* -** generates an error for an undefined 'goto'; choose appropriate -** message when label name is a reserved word (which can only be 'break') +** generates an error for an undefined 'goto'. */ 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 '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - semerror(ls, msg); + const char *msg; + if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { + msg = "break outside loop at line %d"; + msg = luaO_pushfstring(ls->L, msg, gt->line); + } + else { + msg = "no visible label '%s' for at line %d"; + msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); + } + luaK_semerror(ls, msg); } static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; - if (bl->previous && bl->upval) { - /* create a 'jump to here' to close upvalues */ - int j = luaK_jump(fs); - luaK_patchclose(fs, j, bl->nactvar); - luaK_patchtohere(fs, j); - } - if (bl->isloop) - breaklabel(ls); /* close pending breaks */ + int hasclose = 0; + int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ + if (bl->isloop) /* fix pending breaks? */ + hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); + if (!hasclose && bl->previous && bl->upval) + luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); fs->bl = bl->previous; removevars(fs, bl->nactvar); lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ + fs->freereg = stklevel; /* free registers */ ls->dyd->label.n = bl->firstlabel; /* remove local labels */ if (bl->previous) /* inner block? */ movegotosout(fs, bl); /* update pending gotos to outer block */ - else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ - undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ + else { + if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ + undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ + } } @@ -515,35 +704,40 @@ static Proto *addprototype (LexState *ls) { /* ** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction must use the last available register, +** The OP_CLOSURE instruction uses the last available register, ** so that, if it invokes the GC, the GC knows which registers ** are in use at that time. + */ static void codeclosure (LexState *ls, expdesc *v) { FuncState *fs = ls->fs->prev; - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); + init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); luaK_exp2nextreg(fs, v); /* fix it at the last register */ } static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f; + Proto *f = fs->f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; fs->pc = 0; + fs->previousline = f->linedefined; + fs->iwthabs = 0; fs->lasttarget = 0; - fs->jpc = NO_JUMP; fs->freereg = 0; fs->nk = 0; + fs->nabslineinfo = 0; fs->np = 0; fs->nups = 0; - fs->nlocvars = 0; + fs->ndebugvars = 0; fs->nactvar = 0; + fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; + fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; - f = fs->f; f->source = ls->source; + luaC_objbarrier(ls->L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); } @@ -553,21 +747,18 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - luaK_ret(fs, 0, 0); /* final return */ + luaK_ret(fs, luaY_nvarstack(fs), 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; lua_assert(fs->bl == NULL); + luaK_finish(fs); + luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); + luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); + luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, + fs->nabslineinfo, AbsLineInfo); + luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); + luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); + luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); + luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); ls->fs = fs->prev; luaC_checkGC(L); } @@ -613,7 +804,7 @@ static void fieldsel (LexState *ls, expdesc *v) { expdesc key; luaK_exp2anyregup(fs, v); luaX_next(ls); /* skip the dot or colon */ - checkname(ls, &key); + codename(ls, &key); luaK_indexed(fs, v, &key); } @@ -634,48 +825,49 @@ static void yindex (LexState *ls, expdesc *v) { */ -struct ConsControl { +typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ - int na; /* total number of array elements */ + int na; /* number of array elements already stored */ int tostore; /* number of array elements pending to be stored */ -}; +} ConsControl; -static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | '['exp1']') = exp1 */ +static void recfield (LexState *ls, ConsControl *cc) { + /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; - expdesc key, val; - int rkkey; + expdesc tab, key, val; if (ls->t.token == TK_NAME) { checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - checkname(ls, &key); + codename(ls, &key); } else /* ls->t.token == '[' */ yindex(ls, &key); cc->nh++; checknext(ls, '='); - rkkey = luaK_exp2RK(fs, &key); + tab = *cc->t; + luaK_indexed(fs, &tab, &key); expr(ls, &val); - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); + luaK_storevar(fs, &tab, &val); fs->freereg = reg; /* free registers */ } -static void closelistfield (FuncState *fs, struct ConsControl *cc) { +static void closelistfield (FuncState *fs, ConsControl *cc) { if (cc->v.k == VVOID) return; /* there is no list item */ luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; if (cc->tostore == LFIELDS_PER_FLUSH) { luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ + cc->na += cc->tostore; cc->tostore = 0; /* no more items pending */ } } -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { +static void lastlistfield (FuncState *fs, ConsControl *cc) { if (cc->tostore == 0) return; if (hasmultret(cc->v.k)) { luaK_setmultret(fs, &cc->v); @@ -687,19 +879,18 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { luaK_exp2nextreg(fs, &cc->v); luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); } + cc->na += cc->tostore; } -static void listfield (LexState *ls, struct ConsControl *cc) { +static void listfield (LexState *ls, ConsControl *cc) { /* listfield -> exp */ expr(ls, &cc->v); - checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; cc->tostore++; } -static void field (LexState *ls, struct ConsControl *cc) { +static void field (LexState *ls, ConsControl *cc) { /* field -> listfield | recfield */ switch(ls->t.token) { case TK_NAME: { /* may be 'listfield' or 'recfield' */ @@ -727,12 +918,13 @@ static void constructor (LexState *ls, expdesc *t) { FuncState *fs = ls->fs; int line = ls->linenumber; int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; + ConsControl cc; + luaK_code(fs, 0); /* space for extra arg. */ cc.na = cc.nh = cc.tostore = 0; cc.t = t; - init_exp(t, VRELOCABLE, pc); + init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ + luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ checknext(ls, '{'); do { lua_assert(cc.v.k == VVOID || cc.tostore > 0); @@ -742,20 +934,24 @@ 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 */ + luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); } /* }====================================================================== */ +static void setvararg (FuncState *fs, int nparams) { + fs->f->is_vararg = 1; + luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); +} + static void parlist (LexState *ls) { /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; - f->is_vararg = 0; + int isvararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { @@ -766,16 +962,18 @@ static void parlist (LexState *ls) { } case TK_DOTS: { /* param -> '...' */ luaX_next(ls); - f->is_vararg = 1; /* declared vararg */ + isvararg = 1; break; } default: luaX_syntaxerror(ls, " or '...' expected"); } - } while (!f->is_vararg && testnext(ls, ',')); + } while (!isvararg && testnext(ls, ',')); } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ + if (isvararg) + setvararg(fs, f->numparams); /* declared vararg */ + luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ } @@ -825,7 +1023,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { args.k = VVOID; else { explist(ls, &args); - luaK_setmultret(fs, &args); + if (hasmultret(args.k)) + luaK_setmultret(fs, &args); } check_match(ls, ')', '(', line); break; @@ -835,7 +1034,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { break; } case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); + codestring(&args, ls->t.seminfo.ts); luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } @@ -902,7 +1101,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { fieldsel(ls, v); break; } - case '[': { /* '[' exp1 ']' */ + case '[': { /* '[' exp ']' */ expdesc key; luaK_exp2anyregup(fs, v); yindex(ls, &key); @@ -912,7 +1111,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { case ':': { /* ':' NAME funcargs */ expdesc key; luaX_next(ls); - checkname(ls, &key); + codename(ls, &key); luaK_self(fs, v, &key); funcargs(ls, v, line); break; @@ -943,7 +1142,7 @@ static void simpleexp (LexState *ls, expdesc *v) { break; } case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); + codestring(v, ls->t.seminfo.ts); break; } case TK_NIL: { @@ -962,7 +1161,7 @@ static void simpleexp (LexState *ls, expdesc *v) { FuncState *fs = ls->fs; 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)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); break; } case '{': { /* constructor */ @@ -1022,6 +1221,9 @@ static BinOpr getbinopr (int op) { } +/* +** Priority table for binary operators. +*/ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ @@ -1050,9 +1252,9 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { UnOpr uop; enterlevel(ls); uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { + if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ int line = ls->linenumber; - luaX_next(ls); + luaX_next(ls); /* skip operator */ subexpr(ls, v, UNARY_PRIORITY); luaK_prefix(ls->fs, uop, v, line); } @@ -1063,7 +1265,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { expdesc v2; BinOpr nextop; int line = ls->linenumber; - luaX_next(ls); + luaX_next(ls); /* skip operator */ luaK_infix(ls->fs, op, v); /* read sub-expression with higher priority */ nextop = subexpr(ls, &v2, priority[op].right); @@ -1121,43 +1323,60 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { int extra = fs->freereg; /* eventual position to save local variable */ int conflict = 0; for (; lh; lh = lh->prev) { /* check all previous assignments */ - if (lh->v.k == VINDEXED) { /* assigning to a table? */ - /* table is the upvalue/local being assigned now? */ - if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) { - conflict = 1; - lh->v.u.ind.vt = VLOCAL; - lh->v.u.ind.t = extra; /* previous assignment will use safe copy */ + if (vkisindexed(lh->v.k)) { /* assignment to table field? */ + if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ + if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { + conflict = 1; /* table is the upvalue being assigned now */ + lh->v.k = VINDEXSTR; + lh->v.u.ind.t = extra; /* assignment will use safe copy */ + } } - /* index is the local being assigned? (index cannot be upvalue) */ - if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) { - conflict = 1; - lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ + else { /* table is a register */ + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { + conflict = 1; /* table is the local being assigned now */ + lh->v.u.ind.t = extra; /* assignment will use safe copy */ + } + /* is index the local being assigned? */ + if (lh->v.k == VINDEXED && v->k == VLOCAL && + lh->v.u.ind.idx == v->u.var.sidx) { + conflict = 1; + lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ + } } } } if (conflict) { /* copy upvalue/local value to a temporary (in position 'extra') */ - OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; - luaK_codeABC(fs, op, extra, v->u.info, 0); + if (v->k == VLOCAL) + luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); + else + luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); } } - -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { +/* +** Parse and compile a multiple assignment. The first "variable" +** (a 'suffixedexp') was already read by the caller. +** +** assignment -> suffixedexp restassign +** restassign -> ',' suffixedexp restassign | '=' explist +*/ +static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, vkisvar(lh->v.k), "syntax error"); - if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ + check_readonly(ls, &lh->v); + if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ struct LHS_assign nv; nv.prev = lh; suffixedexp(ls, &nv.v); - if (nv.v.k != VINDEXED) + if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); - checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, - "C levels"); - assignment(ls, &nv, nvars+1); + enterlevel(ls); /* control recursion depth */ + restassign(ls, &nv, nvars+1); + leavelevel(ls); } - else { /* assignment -> '=' explist */ + else { /* restassign -> '=' explist */ int nexps; checknext(ls, '='); nexps = explist(ls, &e); @@ -1184,57 +1403,55 @@ static int cond (LexState *ls) { } -static void gotostat (LexState *ls, int pc) { +static void gotostat (LexState *ls) { + FuncState *fs = ls->fs; int line = ls->linenumber; - TString *label; - int g; - if (testnext(ls, TK_GOTO)) - label = str_checkname(ls); - else { - luaX_next(ls); /* skip break */ - label = luaS_new(ls->L, "break"); + TString *name = str_checkname(ls); /* label's name */ + Labeldesc *lb = findlabel(ls, name); + if (lb == NULL) /* no label? */ + /* forward jump; will be resolved when the label is declared */ + newgotoentry(ls, name, line, luaK_jump(fs)); + else { /* found a label */ + /* backward jump; will be resolved here */ + int lblevel = stacklevel(fs, lb->nactvar); /* label level */ + if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ + luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); + /* create jump and link it to the label */ + luaK_patchlist(fs, luaK_jump(fs), lb->pc); } - g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); - findlabel(ls, g); /* close it if label already defined */ } -/* check for repeated labels on the same block */ -static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { - int i; - for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (eqstr(label, ll->arr[i].name)) { - const char *msg = luaO_pushfstring(fs->ls->L, - "label '%s' already defined on line %d", - getstr(label), ll->arr[i].line); - semerror(fs->ls, msg); - } - } +/* +** Break statement. Semantically equivalent to "goto break". +*/ +static void breakstat (LexState *ls) { + int line = ls->linenumber; + luaX_next(ls); /* skip break */ + newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); } -/* skip no-op statements */ -static void skipnoopstat (LexState *ls) { - while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) - statement(ls); +/* +** Check whether there is already a label with the given 'name'. +*/ +static void checkrepeated (LexState *ls, TString *name) { + Labeldesc *lb = findlabel(ls, name); + if (unlikely(lb != NULL)) { /* already defined? */ + const char *msg = "label '%s' already defined on line %d"; + msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); + luaK_semerror(ls, msg); /* error */ + } } -static void labelstat (LexState *ls, TString *label, int line) { +static void labelstat (LexState *ls, TString *name, int line) { /* label -> '::' NAME '::' */ - FuncState *fs = ls->fs; - Labellist *ll = &ls->dyd->label; - int l; /* index of new label being created */ - 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, 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 */ - ll->arr[l].nactvar = fs->bl->nactvar; - } - findgotos(ls, &ll->arr[l]); + while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) + statement(ls); /* skip other no-op statements */ + checkrepeated(ls, name); /* check for repeated labels */ + createlabel(ls, name, line, block_follow(ls, 0)); } @@ -1269,58 +1486,83 @@ static void repeatstat (LexState *ls, int line) { statlist(ls); check_match(ls, TK_UNTIL, TK_REPEAT, line); condexit = cond(ls); /* read condition (inside scope block) */ - if (bl2.upval) /* upvalues? */ - luaK_patchclose(fs, condexit, bl2.nactvar); leaveblock(fs); /* finish scope */ + if (bl2.upval) { /* upvalues? */ + int exit = luaK_jump(fs); /* normal exit must jump over fix */ + luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ + luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); + condexit = luaK_jump(fs); /* repeat after closing upvalues */ + luaK_patchtohere(fs, exit); /* normal exit comes to here */ + } luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ leaveblock(fs); /* finish loop */ } -static int exp1 (LexState *ls) { +/* +** Read an expression and generate code to put its results in next +** stack slot. +** +*/ +static void exp1 (LexState *ls) { expdesc e; - int reg; expr(ls, &e); luaK_exp2nextreg(ls->fs, &e); lua_assert(e.k == VNONRELOC); - reg = e.u.info; - return reg; } -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { +/* +** Fix for instruction at position 'pc' to jump to 'dest'. +** (Jump addresses are relative in Lua). 'back' true means +** a back jump. +*/ +static void fixforjump (FuncState *fs, int pc, int dest, int back) { + Instruction *jmp = &fs->f->code[pc]; + int offset = dest - (pc + 1); + if (back) + offset = -offset; + if (unlikely(offset > MAXARG_Bx)) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_Bx(*jmp, offset); +} + + +/* +** Generate code for a 'for' loop. +*/ +static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* forbody -> DO block */ + static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; + static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; - adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); + prep = luaK_codeABx(fs, forprep[isgen], base, 0); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - if (isnum) /* numeric for? */ - endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); - else { /* generic for */ + fixforjump(fs, prep, luaK_getlabel(fs), 0); + if (isgen) { /* generic for? */ luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); - endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); } - luaK_patchlist(fs, endfor, prep + 1); + endfor = luaK_codeABx(fs, forloop[isgen], base, 0); + fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ + /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; - new_localvarliteral(ls, "(for index)"); - new_localvarliteral(ls, "(for limit)"); - new_localvarliteral(ls, "(for step)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); new_localvar(ls, varname); checknext(ls, '='); exp1(ls); /* initial value */ @@ -1329,10 +1571,11 @@ 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_intK(fs, 1)); + luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } - forbody(ls, base, line, 1, 1); + adjustlocalvars(ls, 3); /* control variables */ + forbody(ls, base, line, 1, 0); } @@ -1340,13 +1583,14 @@ static void forlist (LexState *ls, TString *indexname) { /* forlist -> NAME {,NAME} IN explist forbody */ FuncState *fs = ls->fs; expdesc e; - int nvars = 4; /* gen, state, control, plus at least one declared var */ + int nvars = 5; /* gen, state, control, toclose, 'indexname' */ int line; int base = fs->freereg; /* create control variables */ - new_localvarliteral(ls, "(for generator)"); new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for control)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); /* create declared variables */ new_localvar(ls, indexname); while (testnext(ls, ',')) { @@ -1355,9 +1599,11 @@ static void forlist (LexState *ls, TString *indexname) { } checknext(ls, TK_IN); line = ls->linenumber; - adjust_assign(ls, 3, explist(ls, &e), &e); + adjust_assign(ls, 4, explist(ls, &e), &e); + adjustlocalvars(ls, 4); /* control variables */ + markupval(fs, fs->nactvar); /* last control var. must be closed */ luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 0); + forbody(ls, base, line, nvars - 4, 1); } @@ -1379,28 +1625,68 @@ static void forstat (LexState *ls, int line) { } +/* +** Check whether next instruction is a single jump (a 'break', a 'goto' +** to a forward label, or a 'goto' to a backward label with no variable +** to close). If so, set the name of the 'label' it is jumping to +** ("break" for a 'break') or to where it is jumping to ('target') and +** return true. If not a single jump, leave input unchanged, to be +** handled as a regular statement. +*/ +static int issinglejump (LexState *ls, TString **label, int *target) { + if (testnext(ls, TK_BREAK)) { /* a break? */ + *label = luaS_newliteral(ls->L, "break"); + return 1; + } + else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) + return 0; /* not a valid goto */ + else { + TString *lname = ls->lookahead.seminfo.ts; /* label's id */ + Labeldesc *lb = findlabel(ls, lname); + if (lb) { /* a backward jump? */ + /* does it need to close variables? */ + if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) + return 0; /* not a single jump; cannot optimize */ + *target = lb->pc; + } + else /* jump forward */ + *label = lname; + luaX_next(ls); /* skip goto */ + luaX_next(ls); /* skip name */ + return 1; + } +} + + static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ BlockCnt bl; + int line; FuncState *fs = ls->fs; + TString *jlb = NULL; + int target = NO_JUMP; expdesc v; int jf; /* instruction to skip 'then' code (if condition is false) */ luaX_next(ls); /* skip IF or ELSEIF */ expr(ls, &v); /* read condition */ checknext(ls, TK_THEN); - if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { + line = ls->linenumber; + if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ 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? */ + if (jlb != NULL) /* forward jump? */ + newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ + else /* backward jump */ + luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ + while (testnext(ls, ';')) {} /* skip semicolons */ + if (block_follow(ls, 0)) { /* jump is the entire block? */ leaveblock(fs); return; /* and that is it */ } else /* must skip over 'then' part if condition is false */ jf = luaK_jump(fs); } - else { /* regular case (not goto/break) */ + else { /* regular case (not a jump) */ luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ enterblock(fs, &bl, 0); jf = v.f; @@ -1431,21 +1717,60 @@ static void ifstat (LexState *ls, int line) { static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; + int fvar = fs->nactvar; /* function's variable index */ new_localvar(ls, str_checkname(ls)); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ - getlocvar(fs, b.u.info)->startpc = fs->pc; + localdebuginfo(fs, fvar)->startpc = fs->pc; +} + + +static int getlocalattribute (LexState *ls) { + /* ATTRIB -> ['<' Name '>'] */ + if (testnext(ls, '<')) { + const char *attr = getstr(str_checkname(ls)); + checknext(ls, '>'); + if (strcmp(attr, "const") == 0) + return RDKCONST; /* read-only variable */ + else if (strcmp(attr, "close") == 0) + return RDKTOCLOSE; /* to-be-closed variable */ + else + luaK_semerror(ls, + luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); + } + return VDKREG; /* regular variable */ +} + + +static void checktoclose (LexState *ls, int level) { + if (level != -1) { /* is there a to-be-closed variable? */ + FuncState *fs = ls->fs; + markupval(fs, level + 1); + fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ + luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); + } } static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ + /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ + FuncState *fs = ls->fs; + int toclose = -1; /* index of to-be-closed variable (if any) */ + Vardesc *var; /* last variable */ + int vidx, kind; /* index and kind of last variable */ int nvars = 0; int nexps; expdesc e; do { - new_localvar(ls, str_checkname(ls)); + vidx = new_localvar(ls, str_checkname(ls)); + kind = getlocalattribute(ls); + getlocalvardesc(fs, vidx)->vd.kind = kind; + if (kind == RDKTOCLOSE) { /* to-be-closed? */ + if (toclose != -1) /* one already present? */ + luaK_semerror(ls, "multiple to-be-closed variables in local list"); + toclose = fs->nactvar + nvars; + } nvars++; } while (testnext(ls, ',')); if (testnext(ls, '=')) @@ -1454,8 +1779,19 @@ static void localstat (LexState *ls) { e.k = VVOID; nexps = 0; } - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); + var = getlocalvardesc(fs, vidx); /* get last variable */ + if (nvars == nexps && /* no adjustments? */ + var->vd.kind == RDKCONST && /* last variable is const? */ + luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ + var->vd.kind = RDKCTC; /* variable is a compile-time constant */ + adjustlocalvars(ls, nvars - 1); /* exclude last variable */ + fs->nactvar++; /* but count it */ + } + else { + adjust_assign(ls, nvars, nexps, &e); + adjustlocalvars(ls, nvars); + } + checktoclose(ls, toclose); } @@ -1492,11 +1828,13 @@ static void exprstat (LexState *ls) { suffixedexp(ls, &v.v); if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ v.prev = NULL; - assignment(ls, &v, 1); + restassign(ls, &v, 1); } else { /* stat -> func */ + Instruction *inst; check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ + inst = &getinstruction(fs, &v.v); + SETARG_C(*inst, 1); /* call statement uses no results */ } } @@ -1505,26 +1843,25 @@ static void retstat (LexState *ls) { /* stat -> RETURN [explist] [';'] */ FuncState *fs = ls->fs; expdesc e; - int first, nret; /* registers with returned values */ + int nret; /* number of values being returned */ + int first = luaY_nvarstack(fs); /* first slot to be returned */ if (block_follow(ls, 1) || ls->t.token == ';') - first = nret = 0; /* return no values */ + nret = 0; /* return no values */ else { nret = explist(ls, &e); /* optional return values */ if (hasmultret(e.k)) { luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1) { /* tail call? */ + if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); + lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); } - first = fs->nactvar; nret = LUA_MULTRET; /* return all values */ } else { 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 */ + first = luaK_exp2anyreg(fs, &e); /* can use original slot */ + else { /* values must go to the top of the stack */ + luaK_exp2nextreg(fs, &e); lua_assert(nret == fs->freereg - first); } } @@ -1586,9 +1923,13 @@ static void statement (LexState *ls) { retstat(ls); break; } - case TK_BREAK: /* stat -> breakstat */ + case TK_BREAK: { /* stat -> breakstat */ + breakstat(ls); + break; + } case TK_GOTO: { /* stat -> 'goto' NAME */ - gotostat(ls, luaK_jump(ls->fs)); + luaX_next(ls); /* skip 'goto' */ + gotostat(ls); break; } default: { /* stat -> func | assignment */ @@ -1597,8 +1938,8 @@ static void statement (LexState *ls) { } } lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ + ls->fs->freereg >= luaY_nvarstack(ls->fs)); + ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ leavelevel(ls); } @@ -1611,11 +1952,15 @@ static void statement (LexState *ls) { */ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; - expdesc v; + Upvaldesc *env; open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always declared vararg */ - init_exp(&v, VLOCAL, 0); /* create and... */ - newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ + setvararg(fs, 0); /* main function is always declared vararg */ + env = allocupvalue(fs); /* ...set environment upvalue */ + env->instack = 1; + env->idx = 0; + env->kind = VDKREG; + env->name = ls->envn; + luaC_objbarrier(ls->L, fs->f, env->name); luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); @@ -1628,14 +1973,15 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ + setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue(L, L->top, lexstate.h); /* anchor it */ + sethvalue2s(L, L->top, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ - lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ + luaC_objbarrier(L, funcstate.f, funcstate.f->source); lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index f45b23cba..618cb0106 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lparser.h $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -30,56 +30,88 @@ typedef enum { 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 */ + VKINT, /* integer constant; ival = numerical integer value */ + VKSTR, /* string constant; strval = TString address; + (string is fixed by the lexer) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; info = local register */ + VLOCAL, /* local variable; var.sidx = stack index (local register); + var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; - ind.vt = whether 't' is register or upvalue; - ind.t = table register or upvalue; - ind.idx = key's R/K index */ + ind.t = table register; + ind.idx = key's R index */ + VINDEXUP, /* indexed upvalue; + ind.t = table upvalue; + ind.idx = key's K index */ + VINDEXI, /* indexed variable with constant integer; + ind.t = table register; + ind.idx = key's value */ + VINDEXSTR, /* indexed variable with literal string; + ind.t = table register; + ind.idx = key's 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 */ + VRELOC, /* 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; -#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) -#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) +#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) +#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) + typedef struct expdesc { expkind k; union { lua_Integer ival; /* for VKINT */ lua_Number nval; /* for VKFLT */ + TString *strval; /* for VKSTR */ int info; /* for generic use */ - struct { /* for indexed variables (VINDEXED) */ - short idx; /* index (R/K) */ + struct { /* for indexed variables */ + short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ - lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; + struct { /* for local variables */ + lu_byte sidx; /* index in the stack */ + unsigned short vidx; /* compiler index (in 'actvar.arr') */ + } var; } u; int t; /* patch list of 'exit when true' */ int f; /* patch list of 'exit when false' */ } expdesc; -/* description of active local variable */ -typedef struct Vardesc { - short idx; /* variable index in stack */ +/* kinds of variables */ +#define VDKREG 0 /* regular */ +#define RDKCONST 1 /* constant */ +#define RDKTOCLOSE 2 /* to-be-closed */ +#define RDKCTC 3 /* compile-time constant */ + +/* description of an active local variable */ +typedef union Vardesc { + struct { + TValuefields; /* constant value (if it is a compile-time constant) */ + lu_byte kind; + lu_byte sidx; /* index of the variable in the stack */ + short pidx; /* index of the variable in the Proto's 'locvars' array */ + TString *name; /* variable name */ + } vd; + TValue k; /* constant value (if any) */ } Vardesc; + /* description of pending goto statements and label statements */ typedef struct Labeldesc { TString *name; /* label identifier */ int pc; /* position in code */ int line; /* line where it appeared */ - lu_byte nactvar; /* local level where it appears in current block */ + lu_byte nactvar; /* number of active variables in that position */ + lu_byte close; /* goto that escapes upvalues */ } Labeldesc; @@ -93,7 +125,7 @@ typedef struct Labellist { /* dynamic structures used by the parser */ typedef struct Dyndata { - struct { /* list of active local variables */ + struct { /* list of all active local variables */ Vardesc *arr; int n; int size; @@ -115,17 +147,22 @@ typedef struct FuncState { struct BlockCnt *bl; /* chain of current blocks */ 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 previousline; /* last line that was saved in 'lineinfo' */ int nk; /* number of elements in 'k' */ int np; /* number of elements in 'p' */ + int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ - short nlocvars; /* number of elements in 'f->locvars' */ + int firstlabel; /* index of first label (in 'dyd->label->arr') */ + short ndebugvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ + lu_byte iwthabs; /* instructions issued since last absolute line info */ + lu_byte needclose; /* function needs to close upvalues when returning */ } FuncState; +LUAI_FUNC int luaY_nvarstack (FuncState *fs); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); diff --git a/3rd/lua/lprefix.h b/3rd/lua/lprefix.h index 9a749a3f3..484f2ad6f 100644 --- a/3rd/lua/lprefix.h +++ b/3rd/lua/lprefix.h @@ -1,5 +1,5 @@ /* -** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lprefix.h $ ** Definitions for Lua code that must come before any other header file ** See Copyright Notice in lua.h */ @@ -33,7 +33,7 @@ /* ** Windows stuff */ -#if defined(_WIN32) /* { */ +#if defined(_WIN32) /* { */ #if !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 4e3029b32..4f6bf8bf7 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lstate.c $ ** Global State ** See Copyright Notice in lua.h */ @@ -28,25 +28,6 @@ #include "ltm.h" -#if !defined(LUAI_GCPAUSE) -#define LUAI_GCPAUSE 200 /* 200% */ -#endif - -#if !defined(LUAI_GCMUL) -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ -#endif - - -/* -** a macro to help the creation of a unique random seed when a state is -** created; the seed is used to randomize hashes. -*/ -#if !defined(luai_makeseed) -#include -#define luai_makeseed() cast(unsigned int, time(NULL)) -#endif - - /* ** thread state + extra space @@ -70,6 +51,37 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) +/* +** A macro to create a "random" seed when a state is created; +** the seed is used to randomize string hashes. +*/ +#if 0 && !defined(luai_makeseed) + +#include + +/* +** Compute an initial seed with some level of randomness. +** Rely on Address Space Layout Randomization (if present) and +** current time. +*/ +#define addbuff(b,p,e) \ + { size_t t = cast_sizet(e); \ + memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } + +static unsigned int luai_makeseed (lua_State *L) { + char buff[3 * sizeof(size_t)]; + unsigned int h = cast_uint(time(NULL)); + int p = 0; + addbuff(buff, p, L); /* heap variable */ + addbuff(buff, p, &h); /* local variable */ + addbuff(buff, p, &lua_newstate); /* public function */ + lua_assert(p == sizeof(buff)); + return luaS_hash(buff, p, h, 1); +} + +#endif + + /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** invariant (and avoiding underflows in 'totalbytes') @@ -84,12 +96,73 @@ void luaE_setdebt (global_State *g, l_mem debt) { } +LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { + global_State *g = G(L); + int ccalls; + luaE_freeCI(L); /* release unused CIs */ + ccalls = getCcalls(L); + if (limit >= 40000) + return 0; /* out of bounds */ + limit += CSTACKERR; + if (L != g-> mainthread) + return 0; /* only main thread can change the C stack */ + else if (ccalls <= CSTACKERR) + return 0; /* handling overflow */ + else { + int diff = limit - g->Cstacklimit; + if (ccalls + diff <= CSTACKERR) + return 0; /* new limit would cause an overflow */ + g->Cstacklimit = limit; /* set new limit */ + L->nCcalls += diff; /* correct 'nCcalls' */ + return limit - diff - CSTACKERR; /* success; return previous limit */ + } +} + + +/* +** Decrement count of "C calls" and check for overflows. In case of +** a stack overflow, check appropriate error ("regular" overflow or +** overflow while handling stack overflow). If 'nCcalls' is smaller +** than CSTACKERR but larger than CSTACKMARK, it means it has just +** entered the "overflow zone", so the function raises an overflow +** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is +** already handling an overflow) but larger than CSTACKERRMARK, does +** not report an error (to allow message handling to work). Otherwise, +** report a stack overflow while handling a stack overflow (probably +** caused by a repeating error in the message handling function). +*/ + +void luaE_enterCcall (lua_State *L) { + int ncalls = getCcalls(L); + L->nCcalls--; + if (ncalls <= CSTACKERR) { /* possible overflow? */ + luaE_freeCI(L); /* release unused CIs */ + ncalls = getCcalls(L); /* update call count */ + if (ncalls <= CSTACKERR) { /* still overflow? */ + if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */ + luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ + else if (ncalls >= CSTACKMARK) { + /* not in error-handling zone; raise the error now */ + L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ + luaG_runerror(L, "C stack overflow"); + } + /* else stack is in the error-handling zone; + allow message handler to work */ + } + } +} + + CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci = luaM_new(L, CallInfo); + CallInfo *ci; + lua_assert(L->ci->next == NULL); + luaE_enterCcall(L); + ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; + ci->u.l.trap = 0; L->nci++; return ci; } @@ -102,46 +175,60 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; + L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } + L->nCcalls -= L->nci; /* adjust result */ } /* -** free half of the CallInfo structures not in use by a thread +** free half of the CallInfo structures not in use by a thread, +** keeping the first one. */ void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci; - 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 */ + CallInfo *ci = L->ci->next; /* first free CallInfo */ + CallInfo *next; + if (ci == NULL) + return; /* no extra elements */ + L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ + while ((next = ci->next) != NULL) { /* two extra elements? */ + CallInfo *next2 = next->next; /* next's next */ + ci->next = next2; /* remove next from the list */ L->nci--; - ci->next = next2; /* remove 'next' from the list */ - next2->previous = ci; - ci = next2; /* keep next's next */ + luaM_free(L, next); /* free next */ + if (next2 == NULL) + break; /* no more elements */ + else { + next2->previous = ci; + ci = next2; /* continue */ + } } + L->nCcalls -= L->nci; /* adjust result */ } static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); L1->stacksize = BASIC_STACK_SIZE; for (i = 0; i < BASIC_STACK_SIZE; i++) - setnilvalue(L1->stack + i); /* erase new stack */ + setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; - ci->callstatus = 0; + ci->callstatus = CIST_C; ci->func = L1->top; - setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ + ci->u.c.k = NULL; + ci->nresults = 0; + setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ + L1->top++; ci->top = L1->top + LUA_MINSTACK; L1->ci = ci; } @@ -177,7 +264,8 @@ static void init_registry (lua_State *L, global_State *g) { /* ** open parts of the state that may cause memory-allocation errors. -** ('g->version' != NULL flags that the state was completely build) +** ('g->nilvalue' being a nil value flags that the state was completely +** build.) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -188,7 +276,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ - g->version = lua_version(NULL); + setnilvalue(&g->nilvalue); luai_userstateopen(L); } @@ -205,24 +293,23 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; - L->nCcalls = 0; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; L->allowhook = 1; resethookcount(L); L->openupval = NULL; - L->nny = 1; L->status = LUA_OK; L->errfunc = 0; + L->oldpc = 0; } static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ - if (g->version) /* closing a fully built state? */ + if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ luai_userstateclose(L); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); @@ -232,21 +319,23 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g = G(L); + global_State *g; lua_State *L1; lua_lock(L); + g = G(L); luaC_checkGC(L); /* create new thread */ L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; L1->marked = luaC_white(g); - L1->tt = LUA_TTHREAD; + L1->tt = LUA_VTHREAD; /* link it on list 'allgc' */ L1->next = g->allgc; g->allgc = obj2gco(L1); /* anchor it on L stack */ - setthvalue(L, L->top, L1); + setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); + L1->nCcalls = getCcalls(L); L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; @@ -263,7 +352,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ + luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -271,6 +360,28 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } +int lua_resetthread (lua_State *L) { + CallInfo *ci; + int status; + lua_lock(L); + L->ci = ci = &L->base_ci; /* unwind CallInfo list */ + setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ + ci->func = L->stack; + ci->callstatus = CIST_C; + status = luaF_close(L, L->stack, CLOSEPROTECT); + if (status != CLOSEPROTECT) /* real errors? */ + luaD_seterrorobj(L, status, L->stack + 1); + else { + status = LUA_OK; + L->top = L->stack + 1; + } + ci->top = L->top + LUA_MINSTACK; + L->status = status; + lua_unlock(L); + return status; +} + + LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; @@ -279,33 +390,43 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { if (l == NULL) return NULL; L = &l->l.l; g = &l->g; - L->next = NULL; - L->tt = LUA_TTHREAD; + L->tt = LUA_VTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); + g->allgc = obj2gco(L); /* by now, only object is the main thread */ + L->next = NULL; + g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; + incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; + g->warnf = NULL; + g->ud_warn = NULL; g->mainthread = L; g->gcrunning = 0; /* no GC while building state */ - g->GCestimate = 0; g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; - g->version = NULL; g->gcstate = GCSpause; - g->gckind = KGC_NORMAL; - g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; + g->gckind = KGC_INC; + g->gcemergency = 0; + g->finobj = g->tobefnz = g->fixedgc = NULL; + g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; + g->finobjsur = g->finobjold1 = g->finobjrold = 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->gcstepmul = LUAI_GCMUL; + g->lastatomic = 0; + setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ + setgcparam(g->gcpause, LUAI_GCPAUSE); + setgcparam(g->gcstepmul, LUAI_GCMUL); + g->gcstepsize = LUAI_GCSTEPSIZE; + setgcparam(g->genmajormul, LUAI_GENMAJORMUL); + g->genminormul = LUAI_GENMINORMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ @@ -317,9 +438,32 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ lua_lock(L); + L = G(L)->mainthread; /* only the main thread can be closed */ close_state(L); } +void luaE_warning (lua_State *L, const char *msg, int tocont) { + lua_WarnFunction wf = G(L)->warnf; + if (wf != NULL) + wf(G(L)->ud_warn, msg, tocont); +} + + +/* +** Generate a warning from an error message +*/ +void luaE_warnerror (lua_State *L, const char *where) { + TValue *errobj = s2v(L->top - 1); /* error object */ + const char *msg = (ttisstring(errobj)) + ? svalue(errobj) + : "error object is not a string"; + /* produce warning "error in %s (%s)" (where, msg) */ + luaE_warning(L, "error in ", 1); + luaE_warning(L, where, 1); + luaE_warning(L, " (", 1); + luaE_warning(L, msg, 1); + luaE_warning(L, ")", 0); +} + diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 3f029fe3c..89d43409b 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lstate.h $ ** Global State ** See Copyright Notice in lua.h */ @@ -15,7 +15,6 @@ /* - ** 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 @@ -27,12 +26,44 @@ ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). ** +** For the generational collector, some of these lists have marks for +** generations. Each mark points to the first element in the list for +** that particular generation; that generation goes until the next mark. +** +** 'allgc' -> 'survival': new objects; +** 'survival' -> 'old': objects that survived one collection; +** 'old1' -> 'reallyold': objects that became old in last collection; +** 'reallyold' -> NULL: objects old for more than one cycle. +** +** 'finobj' -> 'finobjsur': new objects marked for finalization; +** 'finobjsur' -> 'finobjold1': survived """"; +** 'finobjold1' -> 'finobjrold': just old """"; +** 'finobjrold' -> NULL: really old """". +** +** All lists can contain elements older than their main ages, due +** to 'luaC_checkfinalizer' and 'udata2finalize', which move +** objects between the normal lists and the "marked for finalization" +** lists. Moreover, barriers can age young objects in young lists as +** OLD0, which then become OLD1. However, a list never contains +** elements younger than their main ages. +** +** The generational collector also uses a pointer 'firstold1', which +** points to the first OLD1 object in the list. It is used to optimize +** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' +** and 'reallyold', but often the list has no OLD1 objects or they are +** after 'old1'.) Note the difference between it and 'old1': +** 'firstold1': no OLD1 objects before this point; there can be all +** ages after it. +** 'old1': no objects younger than OLD1 after this point. +*/ + +/* ** Moreover, there is another set of lists that control gray objects. ** These lists are linked by fields 'gclist'. (All objects that ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists -** must be gray: +** must be gray (with two exceptions explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -43,9 +74,85 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. -** The last three lists are used only during the atomic phase. +** +** The exceptions to that "gray rule" are: +** - TOUCHED2 objects in generational mode stay in a gray list (because +** they must be visited again at the end of the cycle), but they are +** marked black because assignments to them must activate barriers (to +** move them back to TOUCHED1). +** - Open upvales are kept gray to avoid barriers, but they stay out +** of gray lists. (They don't even have a 'gclist' field.) +*/ + + +/* +** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of +** how many "C calls" it still can do in the C stack, to avoid C-stack +** overflow. This count is very rough approximation; it considers only +** recursive functions inside the interpreter, as non-recursive calls +** can be considered using a fixed (although unknown) amount of stack +** space. +** +** The count has two parts: the lower part is the count itself; the +** higher part counts the number of non-yieldable calls in the stack. +** (They are together so that we can change both with one instruction.) +** +** Because calls to external C functions can use an unknown amount +** of space (e.g., functions using an auxiliary buffer), calls +** to these functions add more than one to the count (see CSTACKCF). +** +** The proper count excludes the number of CallInfo structures allocated +** by Lua, as a kind of "potential" calls. So, when Lua calls a function +** (and "consumes" one CallInfo), it needs neither to decrement nor to +** check 'nCcalls', as its use of C stack is already accounted for. +*/ + +/* number of "C stack slots" used by an external C function */ +#define CSTACKCF 10 + + +/* +** The C-stack size is sliced in the following zones: +** - larger than CSTACKERR: normal stack; +** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; +** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; +** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; +** (Because the counter can be decremented CSTACKCF at once, we need +** the so called "buffer zones", with at least that size, to properly +** detect a change from one zone to the next.) */ +#define CSTACKERR (8 * CSTACKCF) +#define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) +#define CSTACKERRMARK (CSTACKCF + 2) + + +/* initial limit for the C-stack of threads */ +#define CSTACKTHREAD (2 * CSTACKERR) + + +/* true if this thread does not have non-yieldable calls in the stack */ +#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) + +/* real number of C calls */ +#define getCcalls(L) ((L)->nCcalls & 0xffff) + + +/* Increment the number of non-yieldable calls */ +#define incnny(L) ((L)->nCcalls += 0x10000) + +/* Decrement the number of non-yieldable calls */ +#define decnny(L) ((L)->nCcalls -= 0x10000) + +/* Increment the number of non-yieldable calls and decrement nCcalls */ +#define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) + +/* Decrement the number of non-yieldable calls and increment nCcalls */ +#define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) + + + + struct lua_longjmp; /* defined in ldo.c */ @@ -69,8 +176,8 @@ 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_INC 0 /* incremental gc */ +#define KGC_GEN 1 /* generational gc */ typedef struct stringtable { @@ -82,12 +189,6 @@ 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'. -** 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 */ @@ -95,8 +196,9 @@ typedef struct CallInfo { struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ - StkId base; /* base for this function */ const Instruction *savedpc; + volatile l_signalT trap; + int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ lua_KFunction k; /* continuation in case of yields */ @@ -104,7 +206,14 @@ typedef struct CallInfo { lua_KContext ctx; /* context info. in case of yields */ } c; } u; - ptrdiff_t extra; + union { + int funcidx; /* called-function index */ + int nyield; /* number of values yielded */ + struct { /* info about transferred values (for call/return hooks) */ + unsigned short ftransfer; /* offset of first value transferred */ + unsigned short ntransfer; /* number of values transferred */ + } transferinfo; + } u2; short nresults; /* expected number of results from this function */ unsigned short callstatus; } CallInfo; @@ -114,17 +223,22 @@ typedef struct CallInfo { ** Bits in CallInfo status */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_C (1<<1) /* call is running a C function */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#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 */ -#define CIST_LEQ (1<<7) /* using __lt for __le */ -#define CIST_FIN (1<<8) /* call is running a finalizer */ +#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ +#define CIST_TAIL (1<<4) /* call was tail called */ +#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ +#define CIST_FIN (1<<6) /* call is running a finalizer */ +#define CIST_TRAN (1<<7) /* 'ci' has transfer information */ +#if defined(LUA_COMPAT_LT_LE) +#define CIST_LEQ (1<<8) /* using __lt for __le */ +#endif + +/* active function is a Lua function */ +#define isLua(ci) (!((ci)->callstatus & CIST_C)) -#define isLua(ci) ((ci)->callstatus & CIST_LUA) +/* call is running Lua code (not a hook) */ +#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) @@ -139,14 +253,21 @@ typedef struct global_State { void *ud; /* auxiliary data to 'frealloc' */ 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 */ + lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ stringtable strt; /* hash table for strings */ TValue l_registry; + TValue nilvalue; /* a nil value */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte genminormul; /* control for minor generational collections */ + lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ + lu_byte gcemergency; /* true if this is an emergency collection */ + lu_byte gcpause; /* size of pause between successive GCs */ + lu_byte gcstepmul; /* GC "speed" */ + lu_byte gcstepsize; /* (log2 of) GC granularity */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ @@ -157,17 +278,24 @@ typedef struct global_State { GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ + /* fields for generational collector */ + GCObject *survival; /* start of objects that survived one GC cycle */ + GCObject *old1; /* start of old1 objects */ + GCObject *reallyold; /* objects more than one cycle old ("really old") */ + GCObject *firstold1; /* first OLD1 object in the list (if any) */ + GCObject *finobjsur; /* list of survival objects with finalizers */ + GCObject *finobjold1; /* list of old1 objects with finalizers */ + GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ - unsigned int gcfinnum; /* number of finalizers to call in each GC step */ - int gcpause; /* size of pause between successive GCs */ - 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 */ - TString *memerrmsg; /* memory-error message */ + TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ + lua_WarnFunction warnf; /* warning function */ + void *ud_warn; /* auxiliary data to 'warnf' */ + unsigned int Cstacklimit; /* current limit for the C stack */ } global_State; @@ -176,12 +304,12 @@ typedef struct global_State { */ struct lua_State { CommonHeader; - unsigned short nci; /* number of items in 'ci' list */ lu_byte status; + lu_byte allowhook; + unsigned short nci; /* number of items in 'ci' list */ StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - 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 */ @@ -191,13 +319,12 @@ struct lua_State { CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ + l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ + int oldpc; /* last pc traced */ int stacksize; int basehookcount; int hookcount; - unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls */ - l_signalT hookmask; - lu_byte allowhook; + volatile l_signalT hookmask; }; @@ -206,6 +333,12 @@ struct lua_State { /* ** Union of all collectable objects (only for conversions) +** ISO C99, 6.5.2.3 p.5: +** "if a union contains several structures that share a common initial +** sequence [...], and if the union object currently contains one +** of these structures, it is permitted to inspect the common initial +** part of any of them anywhere that a declaration of the complete type +** of the union is visible." */ union GCUnion { GCObject gc; /* common header */ @@ -215,27 +348,36 @@ union GCUnion { struct Table h; struct Proto p; struct lua_State th; /* thread */ + struct UpVal upv; }; +/* +** ISO C99, 6.7.2.1 p.14: +** "A pointer to a union object, suitably converted, points to each of +** its members [...], and vice versa." +*/ #define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ #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 gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) +#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ 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)) +#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) +#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) +#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) +#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) -/* macro to convert a Lua object into a GCObject */ -#define obj2gco(v) \ - check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) +/* +** macro to convert a Lua object into a GCObject +** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) +*/ +#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) /* actual number of total bytes allocated */ @@ -246,7 +388,12 @@ 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); +LUAI_FUNC void luaE_enterCcall (lua_State *L); +LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); +LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); + +#define luaE_exitCcall(L) ((L)->nCcalls++) #endif diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index cfff68ebc..48ef659d3 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lstring.c $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -11,7 +11,6 @@ #include -#include #include "lua.h" @@ -26,11 +25,8 @@ static unsigned int STRSEED; static size_t STRID = 0; -#define MEMERRMSG "not enough memory" - - /* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to +** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to ** compute its hash */ #if !defined(LUAI_HASHLIMIT) @@ -38,12 +34,19 @@ static size_t STRID = 0; #endif + +/* +** Maximum size for string table. +*/ +#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) + + /* ** equality for long strings */ int luaS_eqlngstr (TString *a, TString *b) { size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); + lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); return (a == b) || /* same instance or... */ ((len == b->u.lnglen) && /* equal length and ... */ (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ @@ -51,7 +54,7 @@ int luaS_eqlngstr (TString *a, TString *b) { int luaS_eqshrstr (TString *a, TString *b) { lu_byte len = a->shrlen; - lua_assert(b->tt == LUA_TSHRSTR); + lua_assert(b->tt == LUA_VSHRSTR); int r = len == b->shrlen && (memcmp(getstr(a), getstr(b), len) == 0); if (r) { if (a->id < b->id) { @@ -70,9 +73,9 @@ void luaS_share (TString *ts) { ts->id = ATOM_DEC(&STRID); } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { - unsigned int h = seed ^ cast(unsigned int, l); - size_t step = (l >> LUAI_HASHLIMIT) + 1; +unsigned int luaS_hash (const char *str, size_t l, unsigned int seed, + size_t step) { + unsigned int h = seed ^ cast_uint(l); for (; l >= step; l -= step) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; @@ -80,43 +83,58 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int luaS_hashlongstr (TString *ts) { - lua_assert(ts->tt == LUA_TLNGSTR); + lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ - ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); + size_t len = ts->u.lnglen; + size_t step = (len >> LUAI_HASHLIMIT) + 1; + ts->hash = luaS_hash(getstr(ts), len, ts->hash, step); ts->extra = 1; /* now it has its hash */ } return ts->hash; } -/* -** resizes the string table -*/ -void luaS_resize (lua_State *L, int newsize) { +static void tablerehash (TString **vect, int osize, int nsize) { int i; - stringtable *tb = &G(L)->strt; - 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; - } - 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 */ + for (i = osize; i < nsize; i++) /* clear new elements */ + vect[i] = NULL; + for (i = 0; i < osize; i++) { /* rehash old part of the array */ + TString *p = vect[i]; + vect[i] = NULL; + while (p) { /* for each string in the list */ TString *hnext = p->u.hnext; /* save next */ - unsigned int h = lmod(p->hash, newsize); /* new position */ - p->u.hnext = tb->hash[h]; /* chain it */ - tb->hash[h] = p; + unsigned int h = lmod(p->hash, nsize); /* new position */ + p->u.hnext = vect[h]; /* chain it into array */ + vect[h] = p; p = hnext; } } - 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, TString *); +} + + +/* +** Resize the string table. If allocation fails, keep the current size. +** (This can degrade performance, but any non-zero size should work +** correctly.) +*/ +void luaS_resize (lua_State *L, int nsize) { + stringtable *tb = &G(L)->strt; + int osize = tb->size; + TString **newvect; + if (nsize < osize) /* shrinking table? */ + tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ + newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); + if (unlikely(newvect == NULL)) { /* reallocation failed? */ + if (nsize < osize) /* was it shrinking table? */ + tablerehash(tb->hash, nsize, osize); /* restore to original size */ + /* leave table as it was */ + } + else { /* allocation succeeded */ + tb->hash = newvect; + tb->size = nsize; + if (nsize > osize) + tablerehash(newvect, osize, nsize); /* rehash for new size */ } - tb->size = newsize; } @@ -128,21 +146,27 @@ void luaS_clearcache (global_State *g) { 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 */ + if (iswhite(g->strcache[i][j])) /* will entry be collected? */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } -static unsigned int make_str_seed(lua_State *L) { +#if !defined(luai_makeseed) + +#include + +static unsigned int luai_makeseed(lua_State *L) { size_t buff[4]; unsigned int h = time(NULL); buff[0] = cast(size_t, h); buff[1] = cast(size_t, &STRSEED); - buff[2] = cast(size_t, &make_str_seed); + buff[2] = cast(size_t, &luai_makeseed); buff[3] = cast(size_t, L); - return luaS_hash((const char*)buff, sizeof(buff), h); + return luaS_hash((const char*)buff, sizeof(buff), h, 1); } +#endif + /* ** Initialize the string table and the string cache */ @@ -150,9 +174,12 @@ void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; if (STRSEED == 0) { - STRSEED = make_str_seed(L); + STRSEED = luai_makeseed(L); } - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + stringtable *tb = &G(L)->strt; + tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); + tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ + tb->size = MINSTRTABSIZE; /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ @@ -182,7 +209,7 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, STRSEED); + TString *ts = createstrobj(L, l, LUA_VLNGSTR, STRSEED); ts->u.lnglen = l; return ts; } @@ -198,34 +225,46 @@ void luaS_remove (lua_State *L, TString *ts) { } +static void growstrtab (lua_State *L, stringtable *tb) { + if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ + luaC_fullgc(L, 1); /* try to free some... */ + if (tb->nuse == MAX_INT) /* still too many? */ + luaM_error(L); /* cannot even create a message... */ + } + if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ + luaS_resize(L, tb->size * 2); +} + + /* -** checks whether short string exists and reuses it or creates a new one +** 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) { TString *ts; global_State *g = G(L); - unsigned int h = luaS_hash(str, l, STRSEED); - TString **list = &g->strt.hash[lmod(h, g->strt.size)]; + stringtable *tb = &g->strt; + unsigned int h = luaS_hash(str, l, STRSEED, 1); + TString **list = &tb->hash[lmod(h, tb->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)) { + if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ return ts; } } - 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 */ + /* else must create a new string */ + if (tb->nuse >= tb->size) { /* need to grow string table? */ + growstrtab(L, tb); + list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } - ts = createstrobj(L, l, LUA_TSHRSTR, h); + ts = createstrobj(L, l, LUA_VSHRSTR, h); memcpy(getstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); ts->u.hnext = *list; *list = ts; - g->strt.nuse++; + tb->nuse++; return ts; } @@ -238,7 +277,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) + if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getstr(ts), str, l * sizeof(char)); @@ -270,16 +309,19 @@ TString *luaS_new (lua_State *L, const char *str) { } -Udata *luaS_newudata (lua_State *L, size_t s) { +Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { Udata *u; + int i; GCObject *o; - if (s > MAX_SIZE - sizeof(Udata)) + if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); - o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); + o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); u->len = s; + u->nuvalue = nuvalue; u->metatable = NULL; - setuservalue(L, u, luaO_nilobject); + for (i = 0; i < nuvalue; i++) + setnilvalue(&u->uv[i].uv); return u; } diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 4cb59297f..600e53eb1 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lstring.h $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -12,10 +12,18 @@ #include "lstate.h" -#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) +/* +** Memory-allocation error message must be preallocated (it cannot +** be created after memory is exhausted) +*/ +#define MEMERRMSG "not enough memory" + -#define sizeludata(l) (sizeof(union UUdata) + (l)) -#define sizeudata(u) sizeludata((u)->len) +/* +** Size of a TString: Size of the header plus space for the string +** itself (including final '\0'). +*/ +#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) @@ -24,16 +32,17 @@ /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) +#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) /* ** equality for short strings, compare id first */ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b) || \ - ( ((a)->id == (b)->id) ? ((a)->id != 0) : ((a)->hash == (b)->hash && luaS_eqshrstr(a,b)) ) ) +#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b) || \ + ( ((a)->id == (b)->id) ? ((a)->id != 0) : ((a)->hash == (b)->hash && luaS_eqshrstr(a,b)) ) ) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); +LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, + unsigned int seed, size_t step); LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC int luaS_eqshrstr (TString *a, TString *b); @@ -41,11 +50,10 @@ 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 Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); 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); LUAI_FUNC void luaS_share(TString *ts); - #endif diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index b4bed7e93..2ba8bde47 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.254.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: lstrlib.c $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -59,23 +60,50 @@ static int str_len (lua_State *L) { } -/* translate a relative string position: negative means back from end */ -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 (lua_Integer)len + pos + 1; +/* +** translate a relative initial string position +** (negative means back from end): clip result to [1, inf). +** The length of any string in Lua must fit in a lua_Integer, +** so there are no overflows in the casts. +** The inverted comparison avoids a possible overflow +** computing '-pos'. +*/ +static size_t posrelatI (lua_Integer pos, size_t len) { + if (pos > 0) + return (size_t)pos; + else if (pos == 0) + return 1; + else if (pos < -(lua_Integer)len) /* inverted comparison */ + return 1; /* clip to 1 */ + else return len + (size_t)pos + 1; +} + + +/* +** Gets an optional ending string position from argument 'arg', +** with default value 'def'. +** Negative means back from end: clip result to [0, len] +*/ +static size_t getendpos (lua_State *L, int arg, lua_Integer def, + size_t len) { + lua_Integer pos = luaL_optinteger(L, arg, def); + if (pos > (lua_Integer)len) + return len; + else if (pos >= 0) + return (size_t)pos; + else if (pos < -(lua_Integer)len) + return 0; + else return len + (size_t)pos + 1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 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 > (lua_Integer)l) end = l; + size_t start = posrelatI(luaL_checkinteger(L, 2), l); + size_t end = getendpos(L, 3, -1, l); if (start <= end) - lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1); + lua_pushlstring(L, s + start - 1, (end - start) + 1); else lua_pushliteral(L, ""); return 1; } @@ -148,13 +176,12 @@ 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); - lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); - lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); + lua_Integer pi = luaL_optinteger(L, 2, 1); + size_t posi = posrelatI(pi, l); + size_t pose = getendpos(L, 3, pi, l); int n, i; - if (posi < 1) posi = 1; - if (pose > (lua_Integer)l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* arithmetic overflow? */ + if (pose - posi >= (size_t)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"); @@ -170,8 +197,8 @@ static int str_char (lua_State *L) { luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, n); for (i=1; i<=n; i++) { - lua_Integer c = luaL_checkinteger(L, i); - luaL_argcheck(L, uchar(c) == c, i, "value out of range"); + lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i); + luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range"); p[i - 1] = uchar(c); } luaL_pushresultsize(&b, n); @@ -179,27 +206,142 @@ static int str_char (lua_State *L) { } -static int writer (lua_State *L, const void *b, size_t size, void *B) { - (void)L; - luaL_addlstring((luaL_Buffer *) B, (const char *)b, size); +/* +** Buffer to store the result of 'string.dump'. It must be initialized +** after the call to 'lua_dump', to ensure that the function is on the +** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might +** push stuff.) +*/ +struct str_Writer { + int init; /* true iff buffer has been initialized */ + luaL_Buffer B; +}; + + +static int writer (lua_State *L, const void *b, size_t size, void *ud) { + struct str_Writer *state = (struct str_Writer *)ud; + if (!state->init) { + state->init = 1; + luaL_buffinit(L, &state->B); + } + luaL_addlstring(&state->B, (const char *)b, size); return 0; } static int str_dump (lua_State *L) { - luaL_Buffer b; + struct str_Writer state; int strip = lua_toboolean(L, 2); luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); - luaL_buffinit(L,&b); - if (lua_dump(L, writer, &b, strip) != 0) + lua_settop(L, 1); /* ensure function is on the top of the stack */ + state.init = 0; + if (lua_dump(L, writer, &state, strip) != 0) return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&b); + luaL_pushresult(&state.B); return 1; } +/* +** {====================================================== +** METAMETHODS +** ======================================================= +*/ + +#if defined(LUA_NOCVTS2N) /* { */ + +/* no coercion from strings to numbers */ + +static const luaL_Reg stringmetamethods[] = { + {"__index", NULL}, /* placeholder */ + {NULL, NULL} +}; + +#else /* }{ */ + +static int tonum (lua_State *L, int arg) { + if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ + lua_pushvalue(L, arg); + return 1; + } + else { /* check whether it is a numerical string */ + size_t len; + const char *s = lua_tolstring(L, arg, &len); + return (s != NULL && lua_stringtonumber(L, s) == len + 1); + } +} + + +static void trymt (lua_State *L, const char *mtname) { + lua_settop(L, 2); /* back to the original arguments */ + if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) + luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, + luaL_typename(L, -2), luaL_typename(L, -1)); + lua_insert(L, -3); /* put metamethod before arguments */ + lua_call(L, 2, 1); /* call metamethod */ +} + + +static int arith (lua_State *L, int op, const char *mtname) { + if (tonum(L, 1) && tonum(L, 2)) + lua_arith(L, op); /* result will be on the top */ + else + trymt(L, mtname); + return 1; +} + + +static int arith_add (lua_State *L) { + return arith(L, LUA_OPADD, "__add"); +} + +static int arith_sub (lua_State *L) { + return arith(L, LUA_OPSUB, "__sub"); +} + +static int arith_mul (lua_State *L) { + return arith(L, LUA_OPMUL, "__mul"); +} + +static int arith_mod (lua_State *L) { + return arith(L, LUA_OPMOD, "__mod"); +} + +static int arith_pow (lua_State *L) { + return arith(L, LUA_OPPOW, "__pow"); +} + +static int arith_div (lua_State *L) { + return arith(L, LUA_OPDIV, "__div"); +} + +static int arith_idiv (lua_State *L) { + return arith(L, LUA_OPIDIV, "__idiv"); +} + +static int arith_unm (lua_State *L) { + return arith(L, LUA_OPUNM, "__unm"); +} + + +static const luaL_Reg stringmetamethods[] = { + {"__add", arith_add}, + {"__sub", arith_sub}, + {"__mul", arith_mul}, + {"__mod", arith_mod}, + {"__pow", arith_pow}, + {"__div", arith_div}, + {"__idiv", arith_idiv}, + {"__unm", arith_unm}, + {"__index", NULL}, /* placeholder */ + {NULL, NULL} +}; + +#endif /* } */ + +/* }====================================================== */ + /* ** {====================================================== ** PATTERN MATCHING @@ -547,25 +689,46 @@ static const char *lmemfind (const char *s1, size_t l1, } -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { +/* +** get information about the i-th capture. If there are no captures +** and 'i==0', return information about the whole match, which +** is the range 's'..'e'. If the capture is a string, return +** its length and put its address in '*cap'. If it is an integer +** (a position), push it on the stack and return CAP_POSITION. +*/ +static size_t get_onecapture (MatchState *ms, int i, const char *s, + const char *e, const char **cap) { if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else + if (i != 0) luaL_error(ms->L, "invalid capture index %%%d", i + 1); + *cap = s; + return e - s; } else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) + ptrdiff_t capl = ms->capture[i].len; + *cap = ms->capture[i].init; + if (capl == CAP_UNFINISHED) + luaL_error(ms->L, "unfinished capture"); + else if (capl == CAP_POSITION) lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); + return capl; } } +/* +** Push the i-th capture on the stack. +*/ +static void push_onecapture (MatchState *ms, int i, const char *s, + const char *e) { + const char *cap; + ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); + if (l != CAP_POSITION) + lua_pushlstring(ms->L, cap, l); + /* else position was already pushed */ +} + + static int push_captures (MatchState *ms, const char *s, const char *e) { int i; int nlevels = (ms->level == 0 && s) ? 1 : ms->level; @@ -608,16 +771,15 @@ 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); - lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); - if (init < 1) init = 1; - else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ - lua_pushnil(L); /* cannot find anything */ + size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; + if (init > ls) { /* start after string's end? */ + luaL_pushfail(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 - (size_t)init + 1, p, lp); + const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { lua_pushinteger(L, (s2 - s) + 1); lua_pushinteger(L, (s2 - s) + lp); @@ -626,7 +788,7 @@ static int str_find_aux (lua_State *L, int find) { } else { MatchState ms; - const char *s1 = s + init - 1; + const char *s1 = s + init; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ @@ -646,7 +808,7 @@ static int str_find_aux (lua_State *L, int find) { } } while (s1++ < ms.src_end && !anchor); } - lua_pushnil(L); /* not found */ + luaL_pushfail(L); /* not found */ return 1; } @@ -690,11 +852,14 @@ static int gmatch (lua_State *L) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); + size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; GMatchState *gm; - lua_settop(L, 2); /* keep them on closure to avoid being collected */ - gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); + lua_settop(L, 2); /* keep strings on closure to avoid being collected */ + gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0); + if (init > ls) /* start after string's end? */ + init = ls + 1; /* avoid overflows in 's + init' */ prepstate(&gm->ms, L, s, ls, p, lp); - gm->src = s; gm->p = p; gm->lastmatch = NULL; + gm->src = s + init; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); return 1; } @@ -702,60 +867,72 @@ 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; + size_t 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]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) { - if (news[i] != 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 */ - } + const char *p; + while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { + luaL_addlstring(b, news, p - news); + p++; /* skip ESC */ + if (*p == L_ESC) /* '%%' */ + luaL_addchar(b, *p); + else if (*p == '0') /* '%0' */ + luaL_addlstring(b, s, e - s); + else if (isdigit(uchar(*p))) { /* '%n' */ + const char *cap; + ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); + if (resl == CAP_POSITION) + luaL_addvalue(b); /* add position to accumulated result */ + else + luaL_addlstring(b, cap, resl); } + else + luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); + l -= p + 1 - news; + news = p + 1; } + luaL_addlstring(b, news, l); } -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { +/* +** Add the replacement value to the string buffer 'b'. +** Return true if the original string was changed. (Function calls and +** table indexing resulting in nil or false do not change the subject.) +*/ +static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e, int tr) { lua_State *L = ms->L; switch (tr) { - case LUA_TFUNCTION: { + case LUA_TFUNCTION: { /* call the function */ int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); + lua_pushvalue(L, 3); /* push the function */ + n = push_captures(ms, s, e); /* all captures as arguments */ + lua_call(L, n, 1); /* call it */ break; } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); + case LUA_TTABLE: { /* index the table */ + push_onecapture(ms, 0, s, e); /* first capture is the index */ lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); - return; + add_s(ms, b, s, e); /* add value to the buffer */ + return 1; /* something changed */ } } if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ + lua_pop(L, 1); /* remove value */ + luaL_addlstring(b, s, e - s); /* keep original text */ + return 0; /* no changes */ } else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ + return luaL_error(L, "invalid replacement value (a %s)", + luaL_typename(L, -1)); + else { + luaL_addvalue(b); /* add result to accumulator */ + return 1; /* something changed */ + } } @@ -768,11 +945,12 @@ static int str_gsub (lua_State *L) { lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ int anchor = (*p == '^'); lua_Integer n = 0; /* replacement count */ + int changed = 0; /* change flag */ MatchState ms; luaL_Buffer b; - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || + luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table expected"); + "string/function/table"); luaL_buffinit(L, &b); if (anchor) { p++; lp--; /* skip anchor character */ @@ -783,7 +961,7 @@ static int str_gsub (lua_State *L) { 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 replacement to buffer */ + changed = add_value(&ms, &b, src, e, tr) | changed; src = lastmatch = e; } else if (src < ms.src_end) /* otherwise, skip one character */ @@ -791,8 +969,12 @@ static int str_gsub (lua_State *L) { else break; /* end of subject */ if (anchor) break; } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); + if (!changed) /* no changes? */ + lua_pushvalue(L, 1); /* return original string */ + else { /* something changed */ + luaL_addlstring(&b, src, ms.src_end-src); + luaL_pushresult(&b); /* create and return new string */ + } lua_pushinteger(L, n); /* number of substitutions */ return 2; } @@ -813,8 +995,6 @@ static int str_gsub (lua_State *L) { ** Hexadecimal floating-point formatter */ -#include - #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) @@ -824,7 +1004,7 @@ static int str_gsub (lua_State *L) { ** 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) +#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) /* @@ -851,7 +1031,7 @@ static int num2straux (char *buff, int sz, lua_Number x) { 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 */ + buff[n++] = '-'; /* add sign */ m = -m; /* make it positive */ } buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ @@ -887,17 +1067,30 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* -** Maximum size of each formatted item. This maximum size is produced +** Maximum size for items formatted with '%f'. This size is produced ** 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) +** is maximum exponent + 1). (99+3+1, adding some extra, 110) */ -#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) +#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) + + +/* +** All formats except '%f' do not need that large limit. The other +** float formats use exponents, so that they fit in the 99 limit for +** significant digits; 's' for large strings and 'q' add items directly +** to the buffer; all integer formats also fit in the 99 limit. The +** worst case are floats: they may need 99 significant digits, plus +** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120. +*/ +#define MAX_ITEM 120 /* valid flags in a format specification */ -#define FLAGS "-+ #0" +#if !defined(L_FMTFLAGS) +#define L_FMTFLAGS "-+ #0" +#endif + /* ** maximum size of each format specification (such as "%-099.99d") @@ -929,14 +1122,32 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { /* -** Ensures the 'buff' string uses a dot as the radix character. +** Serialize a floating-point number in such a way that it can be +** scanned back by Lua. Use hexadecimal format for "common" numbers +** (to preserve precision); inf, -inf, and NaN are handled separately. +** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) */ -static void checkdp (char *buff, int nb) { - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ - char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); - if (ppoint) *ppoint = '.'; /* change it to a dot */ +static int quotefloat (lua_State *L, char *buff, lua_Number n) { + const char *s; /* for the fixed representations */ + if (n == (lua_Number)HUGE_VAL) /* inf? */ + s = "1e9999"; + else if (n == -(lua_Number)HUGE_VAL) /* -inf? */ + s = "-1e9999"; + else if (n != n) /* NaN? */ + s = "(0/0)"; + else { /* format number as hexadecimal */ + int nb = lua_number2strx(L, buff, MAX_ITEM, + "%" LUA_NUMBER_FRMLEN "a", n); + /* ensures that 'buff' string uses a dot as the radix character */ + if (memchr(buff, '.', nb) == NULL) { /* no dot? */ + char point = lua_getlocaledecpoint(); /* try locale point */ + char *ppoint = (char *)memchr(buff, point, nb); + if (ppoint) *ppoint = '.'; /* change it to a dot */ + } + return nb; } + /* for the fixed representations */ + return l_sprintf(buff, MAX_ITEM, "%s", s); } @@ -951,15 +1162,12 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { case LUA_TNUMBER: { 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 */ - } + if (!lua_isinteger(L, arg)) /* float? */ + nb = quotefloat(L, buff, lua_tonumber(L, arg)); else { /* integers */ lua_Integer n = lua_tointeger(L, arg); const char *format = (n == LUA_MININTEGER) /* corner case? */ - ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ + ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */ : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } @@ -980,8 +1188,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { 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 */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) + while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char)) luaL_error(L, "invalid format (repeated flags)"); if (isdigit(uchar(*p))) p++; /* skip width */ if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ @@ -1028,36 +1236,51 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ + int maxitem = MAX_ITEM; + char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) - luaL_argerror(L, arg, "no value"); + return luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); + nb = l_sprintf(buff, maxitem, 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 = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, MAX_ITEM, form, + nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); break; - case 'e': case 'E': case 'f': - case 'g': case 'G': { + case 'f': + maxitem = MAX_ITEMF; /* extra space for '%f' */ + buff = luaL_prepbuffsize(&b, maxitem); + /* FALLTHROUGH */ + case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); + break; + } + case 'p': { + const void *p = lua_topointer(L, arg); + if (p == NULL) { /* avoid calling 'printf' with argument NULL */ + p = "(null)"; /* result */ + form[strlen(form) - 1] = 's'; /* format it as a string */ + } + nb = l_sprintf(buff, maxitem, form, p); break; } case 'q': { + if (form[2] != '\0') /* modifiers? */ + return luaL_error(L, "specifier '%%q' cannot have modifiers"); addliteral(L, &b, arg); break; } @@ -1073,18 +1296,17 @@ static int str_format (lua_State *L) { luaL_addvalue(&b); /* keep entire string */ } else { /* format the string into 'buff' */ - nb = l_sprintf(buff, MAX_ITEM, form, s); + nb = l_sprintf(buff, maxitem, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } } break; } default: { /* also treat cases 'pnLlh' */ - return luaL_error(L, "invalid option '%%%c' to 'format'", - *(strfrmt - 1)); + return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } - lua_assert(nb < MAX_ITEM); + lua_assert(nb < maxitem); luaL_addsize(&b, nb); } } @@ -1422,17 +1644,12 @@ static int str_packsize (lua_State *L) { while (*fmt != '\0') { int size, ntoalign; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, + "variable-length format"); 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"); - /* call never return, but to avoid warnings: *//* FALLTHROUGH */ - default: break; - } } lua_pushinteger(L, (lua_Integer)totalsize); return 1; @@ -1478,15 +1695,15 @@ static int str_unpack (lua_State *L) { 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; + size_t pos = posrelatI(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"); + luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2, + "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ luaL_checkstack(L, 2, "too many results"); @@ -1515,13 +1732,15 @@ static int str_unpack (lua_State *L) { } 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"); + luaL_argcheck(L, len <= ld - pos - size, 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); + luaL_argcheck(L, pos + len < ld, 2, + "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); pos += len + 1; /* skip string plus final '\0' */ break; @@ -1562,7 +1781,9 @@ static const luaL_Reg strlib[] = { static void createmetatable (lua_State *L) { - lua_createtable(L, 0, 1); /* table to be metatable for strings */ + /* table to be metatable for strings */ + luaL_newlibtable(L, stringmetamethods); + luaL_setfuncs(L, stringmetamethods, 0); lua_pushliteral(L, ""); /* dummy string */ lua_pushvalue(L, -2); /* copy table */ lua_setmetatable(L, -2); /* set table as metatable for strings */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 7c909bda2..45692f4ac 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.118.1.4 2018/06/08 16:22:51 roberto Exp $ +** $Id: ltable.c $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -40,21 +40,34 @@ /* -** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is -** the largest integer such that MAXASIZE fits in an unsigned int. +** MAXABITS is the largest integer such that MAXASIZE fits in an +** unsigned int. */ #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) -#define MAXASIZE (1u << MAXABITS) + /* -** 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.) +** MAXASIZE is the maximum size of the array part. It is the minimum +** between 2^MAXABITS and the maximum size that, measured in bytes, +** fits in a 'size_t'. +*/ +#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) + +/* +** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a +** signed int. */ #define MAXHBITS (MAXABITS - 1) +/* +** MAXHSIZE is the maximum size of the hash part. It is the minimum +** between 2^MAXHBITS and the maximum size such that, measured in bytes, +** it fits in a 'size_t'. +*/ +#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) + + #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) #define hashstr(t,str) hashpow2(t, (str)->hash) @@ -75,11 +88,15 @@ #define dummynode (&dummynode_) static const Node dummynode_ = { - {NILCONSTANT}, /* value */ - {{NILCONSTANT, 0}} /* key */ + {{NULL}, LUA_VEMPTY, /* value's value and type */ + LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ }; +static const TValue absentkey = {ABSTKEYCONSTANT}; + + + /* ** Hash for floating-point numbers. ** The main computation should be just @@ -103,51 +120,162 @@ static int l_hashfloat (lua_Number n) { 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); + unsigned int u = cast_uint(i) + cast_uint(ni); + return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); } } #endif /* -** 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_TNUMINT: - return hashint(t, ivalue(key)); - case LUA_TNUMFLT: - return hashmod(t, l_hashfloat(fltvalue(key))); - case LUA_TSHRSTR: - 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: - return hashpointer(t, pvalue(key)); - case LUA_TLCF: - return hashpointer(t, fvalue(key)); +** returns the 'main' position of an element in a table (that is, +** the index of its hash value). The key comes broken (tag in 'ktt' +** and value in 'vkl') so that we can call it on keys inserted into +** nodes. +*/ +static Node *mainposition (const Table *t, int ktt, const Value *kvl) { + switch (withvariant(ktt)) { + case LUA_VNUMINT: + return hashint(t, ivalueraw(*kvl)); + case LUA_VNUMFLT: + return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); + case LUA_VSHRSTR: + return hashstr(t, tsvalueraw(*kvl)); + case LUA_VLNGSTR: + return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); + case LUA_VFALSE: + return hashboolean(t, 0); + case LUA_VTRUE: + return hashboolean(t, 1); + case LUA_VLIGHTUSERDATA: + return hashpointer(t, pvalueraw(*kvl)); + case LUA_VLCF: + return hashpointer(t, fvalueraw(*kvl)); + default: + return hashpointer(t, gcvalueraw(*kvl)); + } +} + + +/* +** Returns the main position of an element given as a 'TValue' +*/ +static Node *mainpositionTV (const Table *t, const TValue *key) { + return mainposition(t, rawtt(key), valraw(key)); +} + + +/* +** Check whether key 'k1' is equal to the key in node 'n2'. +** This equality is raw, so there are no metamethods. Floats +** with integer values have been normalized, so integers cannot +** be equal to floats. It is assumed that 'eqshrstr' is simply +** pointer equality, so that short strings are handled in the +** default case. +*/ +static int equalkey (const TValue *k1, const Node *n2) { + if (rawtt(k1) != keytt(n2)) /* not the same variants? */ + return 0; /* cannot be same key */ + switch (ttypetag(k1)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: + return 1; + case LUA_VNUMINT: + return (ivalue(k1) == keyival(n2)); + case LUA_VNUMFLT: + return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); + case LUA_VLIGHTUSERDATA: + return pvalue(k1) == pvalueraw(keyval(n2)); + case LUA_VLCF: + return fvalue(k1) == fvalueraw(keyval(n2)); + case LUA_VLNGSTR: + return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); default: - lua_assert(!ttisdeadkey(key)); - return hashpointer(t, gcvalue(key)); + return gcvalue(k1) == gcvalueraw(keyval(n2)); } } /* -** returns the index for 'key' if 'key' is an appropriate key to live in -** the array part of the table, 0 otherwise. +** True if value of 'alimit' is equal to the real size of the array +** part of table 't'. (Otherwise, the array part must be larger than +** 'alimit'.) */ -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 */ +#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) + + +/* +** Returns the real size of the 'array' array +*/ +LUAI_FUNC unsigned int luaH_realasize (const Table *t) { + if (limitequalsasize(t)) + return t->alimit; /* this is the size */ + else { + unsigned int size = t->alimit; + /* compute the smallest power of 2 not smaller than 'n' */ + size |= (size >> 1); + size |= (size >> 2); + size |= (size >> 4); + size |= (size >> 8); + size |= (size >> 16); +#if (UINT_MAX >> 30) > 3 + size |= (size >> 32); /* unsigned int has more than 32 bits */ +#endif + size++; + lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); + return size; } - return 0; /* 'key' did not match some condition */ +} + + +/* +** Check whether real size of the array is a power of 2. +** (If it is not, 'alimit' cannot be changed to any other value +** without changing the real size.) +*/ +static int ispow2realasize (const Table *t) { + return (!isrealasize(t) || ispow2(t->alimit)); +} + + +static unsigned int setlimittosize (Table *t) { + t->alimit = luaH_realasize(t); + setrealasize(t); + return t->alimit; +} + + +#define limitasasize(t) check_exp(isrealasize(t), t->alimit) + + + +/* +** "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 = mainpositionTV(t, key); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (equalkey(key, n)) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) + return &absentkey; /* not found */ + n += nx; + } + } +} + + +/* +** returns the index for 'k' if 'k' is an appropriate key to live in +** the array part of a table, 0 otherwise. +*/ +static unsigned int arrayindex (lua_Integer k) { + if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ + return cast_uint(k); /* 'key' is an appropriate array index */ + else + return 0; } @@ -156,46 +284,39 @@ static unsigned int arrayindex (const TValue *key) { ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ -static unsigned int findindex (lua_State *L, Table *t, StkId key) { +static unsigned int findindex (lua_State *L, Table *t, TValue *key, + unsigned int asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ - i = arrayindex(key); - if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ + i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; + if (i - 1u < asize) /* 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' */ - 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 + 1) + t->sizearray; - } - nx = gnext(n); - if (nx == 0) - luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - else n += nx; - } + const TValue *n = getgeneric(t, key); + if (unlikely(isabstkey(n))) + luaG_runerror(L, "invalid key to 'next'"); /* key not found */ + i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ + /* hash elements are numbered after array ones */ + return (i + 1) + asize; } } int luaH_next (lua_State *L, Table *t, StkId key) { - 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? */ - setivalue(key, i + 1); - setobj2s(L, key+1, &t->array[i]); + unsigned int asize = luaH_realasize(t); + unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ + for (; i < asize; i++) { /* try first array part */ + if (!isempty(&t->array[i])) { /* a non-empty entry? */ + setivalue(s2v(key), i + 1); + setobj2s(L, key + 1, &t->array[i]); return 1; } } - 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))); + for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ + if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ + Node *n = gnode(t, i); + getnodekey(L, s2v(key), n); + setobj2s(L, key + 1, gval(n)); return 1; } } @@ -203,6 +324,12 @@ int luaH_next (lua_State *L, Table *t, StkId key) { } +static void freehash (lua_State *L, Table *t) { + if (!isdummy(t)) + luaM_freearray(L, t->node, cast_sizet(sizenode(t))); +} + + /* ** {============================================================= ** Rehash @@ -214,7 +341,8 @@ int luaH_next (lua_State *L, Table *t, StkId key) { ** "count array" where 'nums[i]' is the number of integers in the table ** 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. +** will go to the array part; return the optimal size. (The condition +** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) */ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { int i; @@ -226,12 +354,10 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { for (i = 0, twotoi = 1; twotoi > 0 && *pna > twotoi / 2; i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - optimal = twotoi; /* optimal size (till now) */ - na = a; /* all elements up to 'optimal' will go to array part */ - } + a += nums[i]; + if (a > twotoi/2) { /* more than half elements present? */ + optimal = twotoi; /* optimal size (till now) */ + na = a; /* all elements up to 'optimal' will go to array part */ } } lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); @@ -240,7 +366,7 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { } -static int countint (const TValue *key, unsigned int *nums) { +static int countint (lua_Integer 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 */ @@ -261,18 +387,19 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ unsigned int i = 1; /* count to traverse all array keys */ + unsigned int asize = limitasasize(t); /* real array size */ /* 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 (lim > asize) { + lim = asize; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { - if (!ttisnil(&t->array[i-1])) + if (!isempty(&t->array[i-1])) lc++; } nums[lg] += lc; @@ -288,8 +415,9 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { int i = sizenode(t); while (i--) { Node *n = &t->node[i]; - if (!ttisnil(gval(n))) { - ause += countint(gkey(n), nums); + if (!isempty(gval(n))) { + if (keyisinteger(n)) + ause += countint(keyival(n), nums); totaluse++; } } @@ -298,15 +426,13 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { } -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]); - t->sizearray = size; -} - - +/* +** Creates an array for the hash part of a table with the given +** size, or reuses the dummy node if size is zero. +** The computation for size overflow is in two steps: the first +** comparison ensures that the shift in the second one does not +** overflow. +*/ static void setnodevector (lua_State *L, Table *t, unsigned int size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ @@ -316,15 +442,15 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { else { int i; int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS) + if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); for (i = 0; i < (int)size; i++) { Node *n = gnode(t, i); gnext(n) = 0; - setnilvalue(wgkey(n)); - setnilvalue(gval(n)); + setnilkey(n); + setempty(gval(n)); } t->lsizenode = cast_byte(lsize); t->lastfree = gnode(t, size); /* all positions are free */ @@ -332,55 +458,88 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { } -typedef struct { - Table *t; - unsigned int nhsize; -} AuxsetnodeT; +/* +** (Re)insert all elements from the hash part of 'ot' into table 't'. +*/ +static void reinsert (lua_State *L, Table *ot, Table *t) { + int j; + int size = sizenode(ot); + for (j = 0; j < size; j++) { + Node *old = gnode(ot, j); + if (!isempty(gval(old))) { + /* doesn't need barrier/invalidate cache, as entry was + already present in the table */ + TValue k; + getnodekey(L, &k, old); + setobjt2t(L, luaH_set(L, t, &k), gval(old)); + } + } +} -static void auxsetnode (lua_State *L, void *ud) { - AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); - setnodevector(L, asn->t, asn->nhsize); +/* +** Exchange the hash part of 't1' and 't2'. +*/ +static void exchangehashpart (Table *t1, Table *t2) { + lu_byte lsizenode = t1->lsizenode; + Node *node = t1->node; + Node *lastfree = t1->lastfree; + t1->lsizenode = t2->lsizenode; + t1->node = t2->node; + t1->lastfree = t2->lastfree; + t2->lsizenode = lsizenode; + t2->node = node; + t2->lastfree = lastfree; } -void luaH_resize (lua_State *L, Table *t, unsigned int nasize, +/* +** Resize table 't' for the new given sizes. Both allocations (for +** the hash part and for the array part) can fail, which creates some +** subtleties. If the first allocation, for the hash part, fails, an +** error is raised and that is it. Otherwise, it copies the elements from +** the shrinking part of the array (if it is shrinking) into the new +** hash. Then it reallocates the array part. If that fails, the table +** is in its original state; the function frees the new hash part and then +** raises the allocation error. Otherwise, it sets the new hash part +** into the table, initializes the new part of the array (if any) with +** nils and reinserts the elements of the old hash back into the new +** parts of the table. +*/ +void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize) { unsigned int i; - int j; - AuxsetnodeT asn; - unsigned int oldasize = t->sizearray; - int oldhsize = allocsizenode(t); - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ - asn.t = t; asn.nhsize = nhsize; - if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ - setarrayvector(L, t, oldasize); /* array back to its original size */ - luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ - } - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ - for (i=nasize; iarray[i])) + Table newt; /* to keep the new hash part */ + unsigned int oldasize = setlimittosize(t); + TValue *newarray; + /* create new hash part with appropriate size into 'newt' */ + setnodevector(L, &newt, nhsize); + if (newasize < oldasize) { /* will array shrink? */ + t->alimit = newasize; /* pretend array has new size... */ + exchangehashpart(t, &newt); /* and new hash */ + /* re-insert into the new hash the elements from vanishing slice */ + for (i = newasize; i < oldasize; i++) { + if (!isempty(&t->array[i])) luaH_setint(L, t, i + 1, &t->array[i]); } - /* shrink array */ - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); + t->alimit = oldasize; /* restore current size... */ + exchangehashpart(t, &newt); /* and hash (in case of errors) */ } - /* re-insert elements from hash part */ - for (j = 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 */ - setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); - } + /* allocate new array */ + newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); + if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ + freehash(L, &newt); /* release new hash part */ + luaM_error(L); /* raise error (with array unchanged) */ } - if (oldhsize > 0) /* not the dummy node? */ - luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ + /* allocation ok; initialize new part of the array */ + exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ + t->array = newarray; /* set new array part */ + t->alimit = newasize; + for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ + setempty(&t->array[i]); + /* re-insert elements from old hash part into new parts */ + reinsert(L, &newt, t); /* 'newt' now has the old hash */ + freehash(L, &newt); /* free old hash part */ } @@ -399,11 +558,13 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { int i; int totaluse; for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ + setlimittosize(t); 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 */ - na += countint(ek, nums); + if (ttisinteger(ek)) + na += countint(ivalue(ek), nums); totaluse++; /* compute new size for array part */ asize = computesizes(nums, &na); @@ -419,21 +580,20 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { Table *luaH_new (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); + GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; - t->flags = cast_byte(~0); + t->flags = cast_byte(maskflags); /* table has no metamethod fields */ t->array = NULL; - t->sizearray = 0; + t->alimit = 0; setnodevector(L, t, 0); return t; } void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast(size_t, sizenode(t))); - luaM_freearray(L, t->array, t->sizearray); + freehash(L, t); + luaM_freearray(L, t->array, luaH_realasize(t)); luaM_free(L, t); } @@ -442,7 +602,7 @@ static Node *getfreepos (Table *t) { if (!isdummy(t)) { while (t->lastfree > t->node) { t->lastfree--; - if (ttisnil(gkey(t->lastfree))) + if (keyisnil(t->lastfree)) return t->lastfree; } } @@ -461,18 +621,20 @@ 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"); + if (unlikely(ttisnil(key))) + luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { + lua_Number f = fltvalue(key); lua_Integer k; - if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */ + if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } - else if (luai_numisnan(fltvalue(key))) + else if (unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } - mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ + mp = mainpositionTV(t, key); + if (!isempty(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? */ @@ -481,7 +643,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { return luaH_set(L, t, key); /* insert key into grown table */ } lua_assert(!isdummy(t)); - othern = mainposition(t, gkey(mp)); + othern = mainposition(t, keytt(mp), &keyval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ @@ -492,7 +654,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { gnext(f) += cast_int(mp - f); /* correct 'next' */ gnext(mp) = 0; /* now 'mp' is free */ } - setnilvalue(gval(mp)); + setempty(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ @@ -503,24 +665,34 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { mp = f; } } - setnodekey(L, &mp->i_key, key); - luaC_barrierback(L, t, key); - lua_assert(ttisnil(gval(mp))); + setnodekey(L, mp, key); + luaC_barrierback(L, obj2gco(t), key); + lua_assert(isempty(gval(mp))); return gval(mp); } /* -** search function for integers +** Search function for integers. If integer is inside 'alimit', get it +** directly from the array part. Otherwise, if 'alimit' is not equal to +** the real size of the array, key still can be in the array part. In +** this case, try to avoid a call to 'luaH_realasize' when key is just +** one more than the limit (so that it can be incremented without +** changing the real size of the array). */ 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) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ + return &t->array[key - 1]; + else if (!limitequalsasize(t) && /* key still may be in the array part? */ + (l_castS2U(key) == t->alimit + 1 || + l_castS2U(key) - 1u < luaH_realasize(t))) { + t->alimit = cast_uint(key); /* probably '#t' is here now */ return &t->array[key - 1]; + } else { Node *n = hashint(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) + if (keyisinteger(n) && keyival(n) == key) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -528,7 +700,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { n += nx; } } - return luaO_nilobject; + return &absentkey; } } @@ -538,34 +710,14 @@ const TValue *luaH_getint (Table *t, lua_Integer 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 */ - const TValue *k = gkey(n); - if (ttisshrstring(k) && eqshrstr(tsvalue(k), key)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return luaO_nilobject; /* not found */ - n += nx; - } - } -} - - -/* -** "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); + lua_assert(key->tt == LUA_VSHRSTR); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) + if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ else { int nx = gnext(n); if (nx == 0) - return luaO_nilobject; /* not found */ + return &absentkey; /* not found */ n += nx; } } @@ -573,7 +725,7 @@ static const TValue *getgeneric (Table *t, const TValue *key) { const TValue *luaH_getstr (Table *t, TString *key) { - if (key->tt == LUA_TSHRSTR) + if (key->tt == LUA_VSHRSTR) return luaH_getshortstr(t, key); else { /* for long strings, use generic case */ TValue ko; @@ -587,13 +739,13 @@ const TValue *luaH_getstr (Table *t, TString *key) { ** main search function */ const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttype(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: { + switch (ttypetag(key)) { + case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key)); + case LUA_VNUMINT: return luaH_getint(t, ivalue(key)); + case LUA_VNIL: return &absentkey; + case LUA_VNUMFLT: { lua_Integer k; - if (luaV_tointeger(key, &k, 0)) /* index is int? */ + if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ @@ -610,18 +762,21 @@ const TValue *luaH_get (Table *t, const TValue *key) { TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { const TValue *p; if(isshared(t)) - luaG_runerror(L,"attempt to change a shared table"); + luaG_runerror(L, "attempt to change a shared table"); p = luaH_get(t, key); - if (p != luaO_nilobject) + if (!isabstkey(p)) return cast(TValue *, p); else return luaH_newkey(L, t, key); } void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p = luaH_getint(t, key); + const TValue *p; TValue *cell; - if (p != luaO_nilobject) + if(isshared(t)) + luaG_runerror(L, "attempt to change a shared table"); + p = luaH_getint(t, key); + if (!isabstkey(p)) cell = cast(TValue *, p); else { TValue k; @@ -632,24 +787,49 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { } -static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { - lua_Unsigned i = j; /* i is zero or a present index */ - j++; - /* find 'i' and 'j' such that i is present and j is not */ - while (!ttisnil(luaH_getint(t, j))) { - i = j; - if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while (!ttisnil(luaH_getint(t, i))) i++; - return i - 1; +/* +** Try to find a boundary in the hash part of table 't'. From the +** caller, we know that 'j' is zero or present and that 'j + 1' is +** present. We want to find a larger key that is absent from the +** table, so that we can do a binary search between the two keys to +** find a boundary. We keep doubling 'j' until we get an absent index. +** If the doubling would overflow, we try LUA_MAXINTEGER. If it is +** absent, we are ready for the binary search. ('j', being max integer, +** is larger or equal to 'i', but it cannot be equal because it is +** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a +** boundary. ('j + 1' cannot be a present integer key because it is +** not a valid integer in Lua.) +*/ +static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { + lua_Unsigned i; + if (j == 0) j++; /* the caller ensures 'j + 1' is present */ + do { + i = j; /* 'i' is a present index */ + if (j <= l_castS2U(LUA_MAXINTEGER) / 2) + j *= 2; + else { + j = LUA_MAXINTEGER; + if (isempty(luaH_getint(t, j))) /* t[j] not present? */ + break; /* 'j' now is an absent index */ + else /* weird case */ + return j; /* well, max integer is a boundary... */ } - j *= 2; + } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ + /* i < j && t[i] present && t[j] absent */ + while (j - i > 1u) { /* do a binary search between them */ + lua_Unsigned m = (i + j) / 2; + if (isempty(luaH_getint(t, m))) j = m; + else i = m; } - /* now do a binary search between them */ - while (j - i > 1) { - lua_Unsigned m = (i+j)/2; - if (ttisnil(luaH_getint(t, m))) j = m; + return i; +} + + +static unsigned int binsearch (const TValue *array, unsigned int i, + unsigned int j) { + while (j - i > 1u) { /* binary search */ + unsigned int m = (i + j) / 2; + if (isempty(&array[m - 1])) j = m; else i = m; } return i; @@ -657,33 +837,92 @@ static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { /* -** 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). +** Try to find a boundary in table 't'. (A 'boundary' is an integer index +** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent +** and 'maxinteger' if t[maxinteger] is present.) +** (In the next explanation, we use Lua indices, that is, with base 1. +** The code itself uses base 0 when indexing the array part of the table.) +** The code starts with 'limit = t->alimit', a position in the array +** part that may be a boundary. +** +** (1) If 't[limit]' is empty, there must be a boundary before it. +** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' +** is present. If so, it is a boundary. Otherwise, do a binary search +** between 0 and limit to find a boundary. In both cases, try to +** use this boundary as the new 'alimit', as a hint for the next call. +** +** (2) If 't[limit]' is not empty and the array has more elements +** after 'limit', try to find a boundary there. Again, try first +** the special case (which should be quite frequent) where 'limit+1' +** is empty, so that 'limit' is a boundary. Otherwise, check the +** last element of the array part. If it is empty, there must be a +** boundary between the old limit (present) and the last element +** (absent), which is found with a binary search. (This boundary always +** can be a new limit.) +** +** (3) The last case is when there are no elements in the array part +** (limit == 0) or its last element (the new limit) is present. +** In this case, must check the hash part. If there is no hash part +** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call +** 'hash_search' to find a boundary in the hash part of the table. +** (In those cases, the boundary is not inside the array part, and +** therefore cannot be used as a new limit.) */ lua_Unsigned luaH_getn (Table *t) { - unsigned int j = t->sizearray; - if (j > 0 && ttisnil(&t->array[j - 1])) { - /* there is a boundary in the array part: (binary) search for it */ - unsigned int i = 0; - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(&t->array[m - 1])) j = m; - else i = m; + unsigned int limit = t->alimit; + if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ + /* there must be a boundary before 'limit' */ + if (limit >= 2 && !isempty(&t->array[limit - 2])) { + /* 'limit - 1' is a boundary; can it be a new limit? */ + if (ispow2realasize(t) && !ispow2(limit - 1)) { + t->alimit = limit - 1; + setnorealasize(t); /* now 'alimit' is not the real size */ + } + return limit - 1; + } + else { /* must search for a boundary in [0, limit] */ + unsigned int boundary = binsearch(t->array, 0, limit); + /* can this boundary represent the real size of the array? */ + if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { + t->alimit = boundary; /* use it as the new limit */ + setnorealasize(t); + } + return boundary; + } + } + /* 'limit' is zero or present in table */ + if (!limitequalsasize(t)) { /* (2)? */ + /* 'limit' > 0 and array has more elements after 'limit' */ + if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ + return limit; /* this is the boundary */ + /* else, try last element in the array */ + limit = luaH_realasize(t); + if (isempty(&t->array[limit - 1])) { /* empty? */ + /* there must be a boundary in the array after old limit, + and it must be a valid new limit */ + unsigned int boundary = binsearch(t->array, t->alimit, limit); + t->alimit = boundary; + return boundary; } - return i; + /* else, new limit is present in the table; check the hash part */ } - /* else must find a boundary in hash part */ - else if (isdummy(t)) /* hash part is empty? */ - return j; /* that is easy... */ - else return unbound_search(t, j); + /* (3) 'limit' is the last element and either is zero or present in table */ + lua_assert(limit == luaH_realasize(t) && + (limit == 0 || !isempty(&t->array[limit - 1]))); + if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) + return limit; /* 'limit + 1' is absent */ + else /* 'limit + 1' is also present */ + return hash_search(t, limit); } #if defined(LUA_DEBUG) +/* export these functions for the test library */ + Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainposition(t, key); + return mainpositionTV(t, key); } int luaH_isdummy (const Table *t) { return isdummy(t); } diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 92db0ac7b..c0060f4b6 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ +** $Id: ltable.h $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -12,19 +12,15 @@ #define gnode(t,i) (&(t)->node[i]) #define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->i_key.nk.next) +#define gnext(n) ((n)->u.next) -/* '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) +** Clear all bits of fast-access metamethods, which means that the table +** may have any of these metamethods. (First access that fails after the +** clearing will set the bit again.) */ -#define wgkey(n) (&(n)->i_key.nk) - -#define invalidateTMcache(t) ((t)->flags = 0) +#define invalidateTMcache(t) ((t)->flags &= ~maskflags) /* true when 't' is using 'dummynode' as its hash part */ @@ -35,9 +31,8 @@ #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)))) +/* returns the Node, given the value of a table entry */ +#define nodefromval(v) cast(Node *, (v)) LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); @@ -55,6 +50,7 @@ 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 lua_Unsigned luaH_getn (Table *t); +LUAI_FUNC unsigned int luaH_realasize (const Table *t); #if defined(LUA_DEBUG) diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index c5349578e..d344a47e9 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ltablib.c $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -58,24 +58,6 @@ static void checktab (lua_State *L, int arg, int what) { } -#if defined(LUA_COMPAT_MAXN) -static int maxn (lua_State *L) { - lua_Number max = 0; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pop(L, 1); /* remove value */ - if (lua_type(L, -1) == LUA_TNUMBER) { - lua_Number v = lua_tonumber(L, -1); - if (v > max) max = v; - } - } - lua_pushnumber(L, max); - return 1; -} -#endif - - static int tinsert (lua_State *L) { lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ @@ -87,7 +69,9 @@ static int tinsert (lua_State *L) { case 3: { 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"); + /* check whether 'pos' is in [1, e] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, + "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ lua_geti(L, 1, i - 1); lua_seti(L, 1, i); /* t[i] = t[i - 1] */ @@ -107,14 +91,16 @@ static int tremove (lua_State *L) { 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"); + /* check whether 'pos' is in [1, size + 1] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, + "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { lua_geti(L, 1, pos + 1); lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } lua_pushnil(L); - lua_seti(L, 1, pos); /* t[pos] = nil */ + lua_seti(L, 1, pos); /* remove entry t[pos] */ return 1; } @@ -191,7 +177,7 @@ static int tconcat (lua_State *L) { ** ======================================================= */ -static int pack (lua_State *L) { +static int tpack (lua_State *L) { int i; int n = lua_gettop(L); /* number of elements to pack */ lua_createtable(L, n, 1); /* create result table */ @@ -204,7 +190,7 @@ static int pack (lua_State *L) { } -static int unpack (lua_State *L) { +static int tunpack (lua_State *L) { lua_Unsigned n; lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); @@ -313,14 +299,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { /* 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)) { + while ((void)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)) { + while ((void)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] */ @@ -352,7 +338,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { /* -** QuickSort algorithm (recursive function) +** Quicksort algorithm (recursive function) */ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned int rnd) { @@ -425,12 +411,9 @@ static int sort (lua_State *L) { static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, -#if defined(LUA_COMPAT_MAXN) - {"maxn", maxn}, -#endif {"insert", tinsert}, - {"pack", pack}, - {"unpack", unpack}, + {"pack", tpack}, + {"unpack", tunpack}, {"remove", tremove}, {"move", tmove}, {"sort", sort}, @@ -440,11 +423,6 @@ static const luaL_Reg tab_funcs[] = { LUAMOD_API int luaopen_table (lua_State *L) { luaL_newlib(L, tab_funcs); -#if defined(LUA_COMPAT_UNPACK) - /* _G.unpack = table.unpack */ - lua_getfield(L, -1, "unpack"); - lua_setglobal(L, "unpack"); -#endif return 1; } diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 0e7c71321..4770f96bb 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: ltm.c $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -16,6 +16,7 @@ #include "ldebug.h" #include "ldo.h" +#include "lgc.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" @@ -26,11 +27,11 @@ static const char udatatypename[] = "userdata"; -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { +LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", - "proto" /* this last case is used for tests only */ + "upvalue", "proto" /* these last cases are used for tests only */ }; @@ -42,7 +43,7 @@ void luaT_init (lua_State *L) { "__div", "__idiv", "__band", "__bor", "__bxor", "__shl", "__shr", "__unm", "__bnot", "__lt", "__le", - "__concat", "__call" + "__concat", "__call", "__close" }; int i; for (i=0; iflags |= cast_byte(1u<metatable; break; @@ -77,9 +78,9 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { mt = uvalue(o)->metatable; break; default: - mt = G(L)->mt[ttnov(o)]; + mt = G(L)->mt[ttype(o)]; } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); + return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); } @@ -95,54 +96,62 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { 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 */ + return ttypename(ttype(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); + const TValue *p2, const TValue *p3) { + 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 */ + setobj2s(L, func + 3, p3); /* 3rd argument */ + L->top = func + 4; + /* metamethod may yield only when called from Lua code */ + if (isLuacode(L->ci)) + luaD_call(L, func, 0); + else + luaD_callnoyield(L, func, 0); +} + + +void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { + ptrdiff_t result = savestack(L, res); 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 */ - if (isLua(L->ci)) - luaD_call(L, func, hasres); + if (isLuacode(L->ci)) + luaD_call(L, func, 1); 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); - } + luaD_callnoyield(L, func, 1); + res = restorestack(L, result); + setobjs2s(L, res, --L->top); /* move result to its place */ } -int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { +static int 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)) + if (notm(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - luaT_callTM(L, tm, p1, p2, res, 1); + if (notm(tm)) return 0; + luaT_callTMres(L, tm, p1, p2, res); 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)) { + if (!callbinTM(L, p1, p2, res, event)) { 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; - if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) + if (ttisnumber(p1) && ttisnumber(p2)) luaG_tointerror(L, p1, p2); else luaG_opinterror(L, p1, p2, "perform bitwise operation on"); @@ -155,11 +164,107 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +void luaT_tryconcatTM (lua_State *L) { + StkId top = L->top; + if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT)) + luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); +} + + +void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, + int flip, StkId res, TMS event) { + if (flip) + luaT_trybinTM(L, p2, p1, res, event); + else + luaT_trybinTM(L, p1, p2, res, event); +} + + +void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, + int flip, StkId res, TMS event) { + TValue aux; + setivalue(&aux, i2); + luaT_trybinassocTM(L, p1, &aux, flip, res, event); +} + + +/* +** Calls an order tag method. +** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old +** behavior: if 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 rtop, event)) - return -1; /* no metamethod */ + if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ + return !l_isfalse(s2v(L->top)); +#if defined(LUA_COMPAT_LT_LE) + else if (event == TM_LE) { + /* try '!(p2 < p1)' for '(p1 <= p2)' */ + L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + if (callbinTM(L, p2, p1, L->top, TM_LT)) { + L->ci->callstatus ^= CIST_LEQ; /* clear mark */ + return l_isfalse(s2v(L->top)); + } + /* else error will remove this 'ci'; no need to clear mark */ + } +#endif + luaG_ordererror(L, p1, p2); /* no metamethod found */ + return 0; /* to avoid warnings */ +} + + +int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int flip, int isfloat, TMS event) { + TValue aux; const TValue *p2; + if (isfloat) { + setfltvalue(&aux, cast_num(v2)); + } + else + setivalue(&aux, v2); + if (flip) { /* arguments were exchanged? */ + p2 = p1; p1 = &aux; /* correct them */ + } else - return !l_isfalse(L->top); + p2 = &aux; + return luaT_callorderTM(L, p1, p2, event); +} + + +void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, + const Proto *p) { + int i; + int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ + int nextra = actual - nfixparams; /* number of extra arguments */ + ci->u.l.nextraargs = nextra; + luaD_checkstack(L, p->maxstacksize + 1); + /* copy function to the top of the stack */ + setobjs2s(L, L->top++, ci->func); + /* move fixed parameters to the top of the stack */ + for (i = 1; i <= nfixparams; i++) { + setobjs2s(L, L->top++, ci->func + i); + setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ + } + ci->func += actual + 1; + ci->top += actual + 1; + lua_assert(L->top <= ci->top && ci->top <= L->stack_last); +} + + +void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { + int i; + int nextra = ci->u.l.nextraargs; + if (wanted < 0) { + wanted = nextra; /* get all extra arguments available */ + checkstackGCp(L, nextra, where); /* ensure stack space */ + L->top = where + nextra; /* next instruction will need top */ + } + for (i = 0; i < wanted && i < nextra; i++) + setobjs2s(L, where + i, ci->func - nextra + i); + for (; i < wanted; i++) /* complete required results with nil */ + setnilvalue(s2v(where + i)); } diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 8170688da..73b833c60 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: ltm.h $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -40,10 +40,26 @@ typedef enum { TM_LE, TM_CONCAT, TM_CALL, + TM_CLOSE, TM_N /* number of elements in the enum */ } TMS; +/* +** Mask with 1 in all fast-access methods. A 1 in any of these bits +** in the flag of a (meta)table means the metatable does not have the +** corresponding metamethod field. (Bit 7 of the flag is used for +** 'isrealasize'.) +*/ +#define maskflags (~(~0u << (TM_EQ + 1))) + + +/* +** Test whether there is no tagmethod. +** (Because tagmethods use raw accesses, the result may be an "empty" nil.) +*/ +#define notm(tm) ttisnil(tm) + #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) @@ -52,7 +68,7 @@ typedef enum { #define ttypename(x) luaT_typenames_[(x) + 1] -LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; +LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); @@ -63,14 +79,25 @@ LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 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); + const TValue *p2, const TValue *p3); +LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, + const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_tryconcatTM (lua_State *L); +LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, + const TValue *p2, int inv, StkId res, TMS event); +LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, + int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); +LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int inv, int isfloat, TMS event); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, + struct CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, + StkId where, int wanted); #endif diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index ca5b29852..454ce12f3 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.230.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: lua.c $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -9,31 +9,22 @@ #include "lprefix.h" -#include #include #include #include +#include + #include "lua.h" #include "lauxlib.h" #include "lualib.h" - -#if !defined(LUA_PROMPT) -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " -#endif - #if !defined(LUA_PROGNAME) #define LUA_PROGNAME "lua" #endif -#if !defined(LUA_MAXINPUT) -#define LUA_MAXINPUT 512 -#endif - #if !defined(LUA_INIT_VAR) #define LUA_INIT_VAR "LUA_INIT" #endif @@ -41,65 +32,6 @@ #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX -/* -** lua_stdin_is_tty detects whether the standard input is a 'tty' (that -** is, whether we're running lua interactively). -*/ -#if !defined(lua_stdin_is_tty) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include -#define lua_stdin_is_tty() isatty(0) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#include -#include - -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) - -#else /* }{ */ - -/* ISO C definition */ -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ - -#endif /* } */ - -#endif /* } */ - - -/* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** 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_readline) /* { */ - -#if defined(LUA_USE_READLINE) /* { */ - -#include -#include -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) - -#else /* }{ */ - -#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,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } - -#endif /* } */ - -#endif /* } */ - - - - static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; @@ -122,8 +54,9 @@ static void lstop (lua_State *L, lua_Debug *ar) { ** interpreter. */ static void laction (int i) { + int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); + lua_sethook(globalL, lstop, flag, 1); } @@ -141,6 +74,7 @@ static void print_usage (const char *badoption) { " -l name require library 'name' into global 'name'\n" " -v show version information\n" " -E ignore environment variables\n" + " -W turn warnings on\n" " -- stop handling options\n" " - stop handling options and execute stdin\n" , @@ -267,6 +201,220 @@ static int dolibrary (lua_State *L, const char *name) { } +/* +** 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 = argv[0]; + if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) + fname = NULL; /* stdin */ + status = luaL_loadfile(L, fname); + if (status == LUA_OK) { + int n = pushargs(L); /* push arguments to script */ + status = docall(L, n, LUA_MULTRET); + } + return report(L, status); +} + + +/* 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 */ + + +/* +** 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 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': + if (argv[i][2] != '\0') /* extra characters? */ + return has_error; /* invalid option */ + args |= has_E; + break; + case 'W': + if (argv[i][2] != '\0') /* extra characters? */ + return has_error; /* invalid option */ + break; + case 'i': + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ + case 'v': + if (argv[i][2] != '\0') /* extra characters? */ + return has_error; /* invalid option */ + args |= has_v; + break; + case 'e': + args |= has_e; /* FALLTHROUGH */ + 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 has_error; /* no next argument or it is another option */ + } + break; + default: /* invalid option */ + return has_error; + } + } + *first = i; /* no script name */ + return args; +} + + +/* +** Processes options 'e' and 'l', which involve running Lua code, and +** 'W', which also affects the state. +** 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++) { + int option = argv[i][1]; + lua_assert(argv[i][0] == '-'); /* already checked */ + switch (option) { + case 'e': case 'l': { + int status; + const char *extra = argv[i] + 2; /* both options need an argument */ + if (*extra == '\0') extra = argv[++i]; + lua_assert(extra != NULL); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); + if (status != LUA_OK) return 0; + break; + } + case 'W': + lua_warning(L, "@on", 0); /* warnings on */ + break; + } + } + return 1; +} + + +static int handle_luainit (lua_State *L) { + const char *name = "=" LUA_INITVARVERSION; + const char *init = getenv(name + 1); + if (init == NULL) { + name = "=" LUA_INIT_VAR; + init = getenv(name + 1); /* try alternative name */ + } + if (init == NULL) return LUA_OK; + else if (init[0] == '@') + return dofile(L, init+1); + else + return dostring(L, init, name); +} + + +/* +** {================================================================== +** Read-Eval-Print Loop (REPL) +** =================================================================== +*/ + +#if !defined(LUA_PROMPT) +#define LUA_PROMPT "> " +#define LUA_PROMPT2 ">> " +#endif + +#if !defined(LUA_MAXINPUT) +#define LUA_MAXINPUT 512 +#endif + + +/* +** lua_stdin_is_tty detects whether the standard input is a 'tty' (that +** is, whether we're running lua interactively). +*/ +#if !defined(lua_stdin_is_tty) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + +#include +#define lua_stdin_is_tty() isatty(0) + +#elif defined(LUA_USE_WINDOWS) /* }{ */ + +#include +#include + +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) + +#else /* }{ */ + +/* ISO C definition */ +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ + +#endif /* } */ + +#endif /* } */ + + +/* +** lua_readline defines how to show a prompt and then read a line from +** the standard input. +** 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_readline) /* { */ + +#if defined(LUA_USE_READLINE) /* { */ + +#include +#include +#define lua_initreadline(L) ((void)L, rl_readline_name="lua") +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,line) ((void)L, add_history(line)) +#define lua_freeline(L,b) ((void)L, free(b)) + +#else /* }{ */ + +#define lua_initreadline(L) ((void)L) +#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,line) { (void)L; (void)line; } +#define lua_freeline(L,b) { (void)L; (void)b; } + +#endif /* } */ + +#endif /* } */ + + /* ** Returns the string to be used as a prompt by the interpreter. */ @@ -406,6 +554,7 @@ static void doREPL (lua_State *L) { int status; const char *oldprogname = progname; progname = NULL; /* no 'progname' on errors in interactive mode */ + lua_initreadline(L); while ((status = loadline(L)) != -1) { if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); @@ -417,134 +566,7 @@ static void doREPL (lua_State *L) { progname = oldprogname; } - -/* -** 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 = argv[0]; - if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - if (status == LUA_OK) { - int n = pushargs(L); /* push arguments to script */ - status = docall(L, n, LUA_MULTRET); - } - return report(L, status); -} - - - -/* 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 */ - -/* -** 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 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': - if (argv[i][2] != '\0') /* extra characters after 1st? */ - return has_error; /* invalid option */ - args |= has_E; - break; - case 'i': - 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; /* FALLTHROUGH */ - 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 has_error; /* no next argument or it is another option */ - } - break; - default: /* invalid option */ - return has_error; - } - } - *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++) { - 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); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; - } - } - return 1; -} - - - -static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVARVERSION; - const char *init = getenv(name + 1); - if (init == NULL) { - name = "=" LUA_INIT_VAR; - init = getenv(name + 1); /* try alternative name */ - } - if (init == NULL) return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, name); -} +/* }================================================================== */ /* @@ -570,6 +592,7 @@ static int pmain (lua_State *L) { } luaL_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ + lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 8a59176a4..a88db558a 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.332.1.2 2018/06/13 16:58:17 roberto Exp $ +** $Id: lua.h $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -17,13 +17,15 @@ #define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "3" -#define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "5" +#define LUA_VERSION_MINOR "4" +#define LUA_VERSION_RELEASE "1" + +#define LUA_VERSION_NUM 504 +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 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-2018 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -49,8 +51,7 @@ #define LUA_ERRRUN 2 #define LUA_ERRSYNTAX 3 #define LUA_ERRMEM 4 -#define LUA_ERRGCMM 5 -#define LUA_ERRERR 6 +#define LUA_ERRERR 5 typedef struct lua_State lua_State; @@ -71,7 +72,7 @@ typedef struct lua_State lua_State; #define LUA_TUSERDATA 7 #define LUA_TTHREAD 8 -#define LUA_NUMTAGS 9 +#define LUA_NUMTYPES 9 @@ -124,6 +125,13 @@ typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); +/* +** Type for warning functions +*/ +typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); + + + /* ** generic extra include file @@ -145,11 +153,12 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); +LUA_API int (lua_resetthread) (lua_State *L); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); -LUA_API const lua_Number *(lua_version) (lua_State *L); +LUA_API lua_Number (lua_version) (lua_State *L); /* @@ -182,7 +191,7 @@ 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 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); +LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); LUA_API void *(lua_touserdata) (lua_State *L, int idx); LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); @@ -251,9 +260,9 @@ 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 void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API int (lua_getuservalue) (lua_State *L, int idx); +LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); /* @@ -267,7 +276,7 @@ LUA_API void (lua_rawset) (lua_State *L, int idx); 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); +LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); /* @@ -292,13 +301,21 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); */ 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_resume) (lua_State *L, lua_State *from, int narg, + int *nres); 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) +/* +** Warning-related functions +*/ +LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); +LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); + + /* ** garbage-collection function and options */ @@ -312,8 +329,10 @@ LUA_API int (lua_isyieldable) (lua_State *L); #define LUA_GCSETPAUSE 6 #define LUA_GCSETSTEPMUL 7 #define LUA_GCISRUNNING 9 +#define LUA_GCGEN 10 +#define LUA_GCINC 11 -LUA_API int (lua_gc) (lua_State *L, int what, int data); +LUA_API int (lua_gc) (lua_State *L, int what, ...); /* @@ -332,6 +351,7 @@ 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); +LUA_API void (lua_toclose) (lua_State *L, int idx); /* @@ -381,7 +401,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); /* ** {============================================================== -** compatibility macros for unsigned conversions +** compatibility macros ** =============================================================== */ #if defined(LUA_COMPAT_APIINTCASTS) @@ -391,6 +411,13 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) #endif + +#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) +#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) +#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) + +#define LUA_NUMTAGS LUA_NUMTYPES + /* }============================================================== */ /* @@ -441,6 +468,7 @@ 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); +LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); struct lua_Debug { int event; @@ -448,6 +476,7 @@ struct lua_Debug { const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ const char *source; /* (S) */ + size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ @@ -455,6 +484,8 @@ struct lua_Debug { unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ char istailcall; /* (t) */ + unsigned short ftransfer; /* (r) index of first value transferred */ + unsigned short ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ @@ -462,14 +493,9 @@ 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-2018 Lua.org, PUC-Rio. +* Copyright (C) 1994-2020 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 549ad3950..56ddc4148 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,5 +1,5 @@ /* -** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ +** $Id: luac.c $ ** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ @@ -18,7 +18,10 @@ #include "lua.h" #include "lauxlib.h" +#include "ldebug.h" #include "lobject.h" +#include "lopcodes.h" +#include "lopnames.h" #include "lstate.h" #include "lundump.h" @@ -34,6 +37,7 @@ static int stripping=0; /* strip debug information? */ static char Output[]={ OUTPUT }; /* default output file name */ static const char* output=Output; /* actual output file name */ static const char* progname=PROGNAME; /* actual program name */ +static TString **tmname; static void fatal(const char* message) { @@ -119,7 +123,7 @@ static int doargs(int argc, char* argv[]) #define FUNCTION "(function()end)();" -static const char* reader(lua_State *L, void *ud, size_t *size) +static const char* reader(lua_State* L, void* ud, size_t* size) { UNUSED(L); if ((*(int*)ud)--) @@ -134,7 +138,7 @@ static const char* reader(lua_State *L, void *ud, size_t *size) } } -#define toproto(L,i) getproto(L->top+(i)) +#define toproto(L,i) getproto(s2v(L->top+(i))) static const Proto* combine(lua_State* L, int n) { @@ -168,6 +172,7 @@ static int pmain(lua_State* L) char** argv=(char**)lua_touserdata(L,2); const Proto* f; int i; + tmname=G(L)->tmname; if (!lua_checkstack(L,argc)) fatal("too many input files"); for (i=0; i -#include - -#define luac_c -#define LUA_CORE - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" - -#define VOID(p) ((const void*)(p)) +#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") +#define VOID(p) ((const void*)(p)) +#define eventname(i) (getstr(tmname[i])) static void PrintString(const TString* ts) { const char* s=getstr(ts); size_t i,n=tsslen(ts); - printf("%c",'"'); + printf("\""); for (i=0; ik[i]; + switch (ttypetag(o)) + { + case LUA_VNIL: + printf("N"); + break; + case LUA_VFALSE: + case LUA_VTRUE: + printf("B"); + break; + case LUA_VNUMFLT: + printf("F"); + break; + case LUA_VNUMINT: + printf("I"); + break; + case LUA_VSHRSTR: + case LUA_VLNGSTR: + printf("S"); + break; + default: /* cannot happen */ + printf("?%d",ttypetag(o)); + break; + } + printf("\t"); } static void PrintConstant(const Proto* f, int i) { const TValue* o=&f->k[i]; - switch (ttype(o)) + switch (ttypetag(o)) { - case LUA_TNIL: + case LUA_VNIL: printf("nil"); break; - case LUA_TBOOLEAN: - printf(bvalue(o) ? "true" : "false"); + case LUA_VFALSE: + printf("false"); + break; + case LUA_VTRUE: + printf("true"); break; - case LUA_TNUMFLT: + case LUA_VNUMFLT: { char buff[100]; sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); @@ -270,20 +314,23 @@ static void PrintConstant(const Proto* f, int i) if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); break; } - case LUA_TNUMINT: + case LUA_VNUMINT: printf(LUA_INTEGER_FMT,ivalue(o)); break; - case LUA_TSHRSTR: case LUA_TLNGSTR: + case LUA_VSHRSTR: + case LUA_VLNGSTR: PrintString(tsvalue(o)); break; default: /* cannot happen */ - printf("? type=%d",ttype(o)); + printf("?%d",ttypetag(o)); break; } } -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") -#define MYK(x) (-1-(x)) +#define COMMENT "\t; " +#define EXTRAARG GETARG_Ax(code[pc+1]) +#define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) +#define ISK (isk ? "k" : "") static void PrintCode(const Proto* f) { @@ -298,98 +345,324 @@ static void PrintCode(const Proto* f) int c=GETARG_C(i); int ax=GETARG_Ax(i); int bx=GETARG_Bx(i); + int sb=GETARG_sB(i); + int sc=GETARG_sC(i); int sbx=GETARG_sBx(i); - int line=getfuncline(f,pc); + int isk=GETARG_k(i); + int line=luaG_getfuncline(f,pc); printf("\t%d\t",pc+1); if (line>0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",luaP_opnames[o]); - switch (getOpMode(o)) - { - case iABC: - printf("%d",a); - if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); - if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); - break; - case iABx: - printf("%d",a); - if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); - if (getBMode(o)==OpArgU) printf(" %d",bx); - break; - case iAsBx: - printf("%d %d",a,sbx); - break; - case iAx: - printf("%d",MYK(ax)); - break; - } + printf("%-9s\t",opnames[o]); switch (o) { + case OP_MOVE: + printf("%d %d",a,b); + break; + case OP_LOADI: + printf("%d %d",a,sbx); + break; + case OP_LOADF: + printf("%d %d",a,sbx); + break; case OP_LOADK: - printf("\t; "); PrintConstant(f,bx); - break; + printf("%d %d",a,bx); + printf(COMMENT); PrintConstant(f,bx); + break; + case OP_LOADKX: + printf("%d",a); + printf(COMMENT); PrintConstant(f,EXTRAARG); + break; + case OP_LOADFALSE: + printf("%d",a); + break; + case OP_LFALSESKIP: + printf("%d",a); + break; + case OP_LOADTRUE: + printf("%d",a); + break; + case OP_LOADNIL: + printf("%d %d",a,b); + printf(COMMENT "%d out",b+1); + break; case OP_GETUPVAL: + printf("%d %d",a,b); + printf(COMMENT "%s",UPVALNAME(b)); + break; case OP_SETUPVAL: - printf("\t; %s",UPVALNAME(b)); - break; + printf("%d %d",a,b); + printf(COMMENT "%s",UPVALNAME(b)); + break; case OP_GETTABUP: - printf("\t; %s",UPVALNAME(b)); - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABUP: - printf("\t; %s",UPVALNAME(a)); - if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; + printf("%d %d %d",a,b,c); + printf(COMMENT "%s",UPVALNAME(b)); + printf(" "); PrintConstant(f,c); + break; case OP_GETTABLE: - case OP_SELF: - if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } - break; + printf("%d %d %d",a,b,c); + break; + case OP_GETI: + printf("%d %d %d",a,b,c); + break; + case OP_GETFIELD: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SETTABUP: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT "%s",UPVALNAME(a)); + printf(" "); PrintConstant(f,b); + if (isk) { printf(" "); PrintConstant(f,c); } + break; case OP_SETTABLE: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_SETI: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_SETFIELD: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT); PrintConstant(f,b); + if (isk) { printf(" "); PrintConstant(f,c); } + break; + case OP_NEWTABLE: + printf("%d %d %d",a,b,c); + printf(COMMENT "%d",c+EXTRAARGC); + break; + case OP_SELF: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_ADDI: + printf("%d %d %d",a,b,sc); + break; + case OP_ADDK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SUBK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_MULK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_MODK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_POWK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_DIVK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_IDIVK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BANDK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BORK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BXORK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SHRI: + printf("%d %d %d",a,b,sc); + break; + case OP_SHLI: + printf("%d %d %d",a,b,sc); + break; case OP_ADD: + printf("%d %d %d",a,b,c); + break; case OP_SUB: + printf("%d %d %d",a,b,c); + break; case OP_MUL: + printf("%d %d %d",a,b,c); + break; case OP_MOD: + printf("%d %d %d",a,b,c); + break; case OP_POW: + printf("%d %d %d",a,b,c); + break; case OP_DIV: + printf("%d %d %d",a,b,c); + break; case OP_IDIV: + printf("%d %d %d",a,b,c); + break; case OP_BAND: + printf("%d %d %d",a,b,c); + break; case OP_BOR: + printf("%d %d %d",a,b,c); + break; case OP_BXOR: + printf("%d %d %d",a,b,c); + break; case OP_SHL: + printf("%d %d %d",a,b,c); + break; case OP_SHR: + printf("%d %d %d",a,b,c); + break; + case OP_MMBIN: + printf("%d %d %d",a,b,c); + printf(COMMENT "%s",eventname(c)); + break; + case OP_MMBINI: + printf("%d %d %d %d",a,sb,c,isk); + printf(COMMENT "%s",eventname(c)); + if (isk) printf(" flip"); + break; + case OP_MMBINK: + printf("%d %d %d %d",a,b,c,isk); + printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); + if (isk) printf(" flip"); + break; + case OP_UNM: + printf("%d %d",a,b); + break; + case OP_BNOT: + printf("%d %d",a,b); + break; + case OP_NOT: + printf("%d %d",a,b); + break; + case OP_LEN: + printf("%d %d",a,b); + break; + case OP_CONCAT: + printf("%d %d",a,b); + break; + case OP_CLOSE: + printf("%d",a); + break; + case OP_TBC: + printf("%d",a); + break; + case OP_JMP: + printf("%d",GETARG_sJ(i)); + printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); + break; case OP_EQ: + printf("%d %d %d",a,b,isk); + break; case OP_LT: + printf("%d %d %d",a,b,isk); + break; case OP_LE: - if (ISK(b) || ISK(c)) - { - printf("\t; "); - if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); - printf(" "); - if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); - } - break; - case OP_JMP: + printf("%d %d %d",a,b,isk); + break; + case OP_EQK: + printf("%d %d %d",a,b,isk); + printf(COMMENT); PrintConstant(f,b); + break; + case OP_EQI: + printf("%d %d %d",a,sb,isk); + break; + case OP_LTI: + printf("%d %d %d",a,sb,isk); + break; + case OP_LEI: + printf("%d %d %d",a,sb,isk); + break; + case OP_GTI: + printf("%d %d %d",a,sb,isk); + break; + case OP_GEI: + printf("%d %d %d",a,sb,isk); + break; + case OP_TEST: + printf("%d %d",a,isk); + break; + case OP_TESTSET: + printf("%d %d %d",a,b,isk); + break; + case OP_CALL: + printf("%d %d %d",a,b,c); + printf(COMMENT); + if (b==0) printf("all in "); else printf("%d in ",b-1); + if (c==0) printf("all out"); else printf("%d out",c-1); + break; + case OP_TAILCALL: + printf("%d %d %d",a,b,c); + printf(COMMENT "%d in",b-1); + break; + case OP_RETURN: + printf("%d %d %d",a,b,c); + printf(COMMENT); + if (b==0) printf("all out"); else printf("%d out",b-1); + break; + case OP_RETURN0: + break; + case OP_RETURN1: + printf("%d",a); + break; case OP_FORLOOP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc-bx+2); + break; case OP_FORPREP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc+bx+2); + break; + case OP_TFORPREP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc+bx+2); + break; + case OP_TFORCALL: + printf("%d %d",a,c); + break; case OP_TFORLOOP: - printf("\t; to %d",sbx+pc+2); - break; - case OP_CLOSURE: - printf("\t; %p",VOID(f->p[bx])); - break; + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc-bx+2); + break; case OP_SETLIST: - if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); - break; + printf("%d %d %d",a,b,c); + if (isk) printf(COMMENT "%d",c+EXTRAARGC); + break; + case OP_CLOSURE: + printf("%d %d",a,bx); + printf(COMMENT "%p",VOID(f->p[bx])); + break; + case OP_VARARG: + printf("%d %d",a,c); + printf(COMMENT); + if (c==0) printf("all out"); else printf("%d out",c-1); + break; + case OP_VARARGPREP: + printf("%d",a); + break; case OP_EXTRAARG: - printf("\t; "); PrintConstant(f,ax); - break; + printf("%d",ax); + break; +#if 0 default: - break; + printf("%d %d %d",a,b,c); + printf(COMMENT "not handled"); + break; +#endif } printf("\n"); } } + #define SS(x) ((x==1)?"":"s") #define S(x) (int)(x),SS(x) @@ -403,7 +676,7 @@ static void PrintHeader(const Proto* f) else s="(string)"; printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", - (f->linedefined==0)?"main":"function",s, + (f->linedefined==0)?"main":"function",s, f->linedefined,f->lastlinedefined, S(f->sizecode),VOID(f)); printf("%d%s param%s, %d slot%s, %d upvalue%s, ", @@ -420,7 +693,8 @@ static void PrintDebug(const Proto* f) printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; i> 30) >= 3) + +/* }================================================================== */ /* -@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. +** {================================================================== +** Configuration for Number types. +** =================================================================== */ -/* 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 + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +/* #define LUA_32BITS */ + + +/* +@@ 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_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 +** Lua should work fine with any mix of these options 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'). @@ -119,7 +140,7 @@ /* ** 32-bit integers and 'float' */ -#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#if LUAI_IS32INT /* use 'int' if big enough */ #define LUA_INT_TYPE LUA_INT_INT #else /* otherwise use 'long' */ #define LUA_INT_TYPE LUA_INT_LONG @@ -151,7 +172,6 @@ - /* ** {================================================================== ** Configuration for Paths. @@ -179,6 +199,7 @@ ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ + #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #if defined(_WIN32) /* { */ /* @@ -188,27 +209,40 @@ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.dll;" \ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif #else /* }{ */ #define LUA_ROOT "/usr/local/" #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + #endif /* } */ @@ -217,12 +251,16 @@ ** CHANGE it if your machine does not use "/" as the directory separator ** and is not Windows. (On Windows Lua automatically uses "\".) */ +#if !defined(LUA_DIRSEP) + #if defined(_WIN32) #define LUA_DIRSEP "\\" #else #define LUA_DIRSEP "/" #endif +#endif + /* }================================================================== */ @@ -256,16 +294,18 @@ #endif /* } */ -/* more often than not the libs go together with the core */ +/* +** More often than not the libs go together with the core. +*/ #define LUALIB_API LUA_API -#define LUAMOD_API LUALIB_API +#define LUAMOD_API LUA_API /* @@ LUAI_FUNC is a mark for all extern functions that are not to be ** 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 +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which 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 @@ -277,12 +317,12 @@ */ #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_FUNC __attribute__((visibility("internal"))) extern #else /* }{ */ #define LUAI_FUNC extern #endif /* } */ -#define LUAI_DDEC LUAI_FUNC +#define LUAI_DDEC(dec) LUAI_FUNC dec #define LUAI_DDEF /* empty */ /* }================================================================== */ @@ -295,88 +335,43 @@ */ /* -@@ 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. +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. ** You can define it to get all options, or change specific options ** to fit your specific needs. */ -#if defined(LUA_COMPAT_5_2) /* { */ +#if defined(LUA_COMPAT_5_3) /* { */ /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) */ #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.) +** (These macros were also officially removed in 5.3, but they are still +** available here.) */ #define LUA_COMPAT_APIINTCASTS -#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'. +@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod +** using '__lt'. */ -#define LUA_COMPAT_UNPACK +#define LUA_COMPAT_LT_LE -/* -@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. -** You can replace it with 'package.searchers'. -*/ -#define LUA_COMPAT_LOADERS - -/* -@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. -** You can call your C function directly (with light C functions). -*/ -#define lua_cpcall(L,f,u) \ - (lua_pushcfunction(L, (f)), \ - lua_pushlightuserdata(L,(u)), \ - lua_pcall(L,1,0,0)) - - -/* -@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. -** You can rewrite 'log10(x)' as 'log(x, 10)'. -*/ -#define LUA_COMPAT_LOG10 - -/* -@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base -** library. You can rewrite 'loadstring(s)' as 'load(s)'. -*/ -#define LUA_COMPAT_LOADSTRING - -/* -@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. -*/ -#define LUA_COMPAT_MAXN /* @@ The following macros supply trivial compatibility for some ** changes in the API. The macros themselves document how to ** change your code to avoid using them. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) */ #define lua_strlen(L,i) lua_rawlen(L, (i)) @@ -385,23 +380,8 @@ #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) -/* -@@ LUA_COMPAT_MODULE controls compatibility with previous -** module functions 'module' (Lua) and 'luaL_register' (C). -*/ -#define LUA_COMPAT_MODULE - #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 */ - /* }================================================================== */ @@ -418,14 +398,14 @@ @@ LUA_NUMBER is the floating-point type used by Lua. @@ 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 +@@ l_floatatt(x) corrects float attribute '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. @@ l_floor takes the floor of a float. -@@ lua_str2number converts a decimal numeric string to a number. +@@ lua_str2number converts a decimal numeral to a number. */ @@ -437,12 +417,13 @@ l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(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.) +@@ lua_numbertointeger converts a float number with an integral value +** 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) && \ @@ -456,7 +437,7 @@ #define LUA_NUMBER float -#define l_mathlim(n) (FLT_##n) +#define l_floatatt(n) (FLT_##n) #define LUAI_UACNUMBER double @@ -472,7 +453,7 @@ #define LUA_NUMBER long double -#define l_mathlim(n) (LDBL_##n) +#define l_floatatt(n) (LDBL_##n) #define LUAI_UACNUMBER long double @@ -487,7 +468,7 @@ #define LUA_NUMBER double -#define l_mathlim(n) (DBL_##n) +#define l_floatatt(n) (DBL_##n) #define LUAI_UACNUMBER double @@ -512,11 +493,13 @@ @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. ** @@ LUAI_UACINT is the result of a 'default argument promotion' -@@ over a lUA_INTEGER. +@@ 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_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. +@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -537,6 +520,9 @@ #define LUA_UNSIGNED unsigned LUAI_UACINT +#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) + + /* now the variable definitions */ #if LUA_INT_TYPE == LUA_INT_INT /* { int */ @@ -547,6 +533,8 @@ #define LUA_MAXINTEGER INT_MAX #define LUA_MININTEGER INT_MIN +#define LUA_MAXUNSIGNED UINT_MAX + #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ #define LUA_INTEGER long @@ -555,6 +543,8 @@ #define LUA_MAXINTEGER LONG_MAX #define LUA_MININTEGER LONG_MIN +#define LUA_MAXUNSIGNED ULONG_MAX + #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ /* use presence of macro LLONG_MAX as proxy for C99 compliance */ @@ -567,6 +557,8 @@ #define LUA_MAXINTEGER LLONG_MAX #define LUA_MININTEGER LLONG_MIN +#define LUA_MAXUNSIGNED ULLONG_MAX + #elif defined(LUA_USE_WINDOWS) /* }{ */ /* in Windows, can use specific Windows types */ @@ -576,6 +568,8 @@ #define LUA_MAXINTEGER _I64_MAX #define LUA_MININTEGER _I64_MIN +#define LUA_MAXUNSIGNED _UI64_MAX + #else /* }{ */ #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ @@ -610,7 +604,7 @@ /* -@@ lua_strx2number converts an hexadecimal numeric string to a number. +@@ lua_strx2number converts a hexadecimal numeral to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. @@ -628,7 +622,7 @@ /* -@@ lua_number2strx converts a float to an hexadecimal numeric string. +@@ lua_number2strx converts a float to a hexadecimal numeral. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. @@ -674,7 +668,7 @@ /* @@ 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'.) +** macro must include the header 'locale.h'.) */ #if !defined(lua_getlocaledecpoint) #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) @@ -715,7 +709,7 @@ ** {================================================================== ** 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. +** Lua). ** ===================================================================== */ @@ -724,8 +718,9 @@ ** CHANGE it if you need a different limit. This limit is arbitrary; ** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). +** (It must fit into max(size_t)/32.) */ -#if LUAI_BITSINT >= 32 +#if LUAI_IS32INT #define LUAI_MAXSTACK 1000000 #else #define LUAI_MAXSTACK 15000 @@ -750,27 +745,18 @@ /* @@ 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 5034 bytes, so a -** smaller buffer would force a memory allocation for each call to -** 'string.format'.) */ -#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE -#define LUAL_BUFFERSIZE 8192 -#else -#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) -#endif - -/* }================================================================== */ +#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) /* -@@ LUA_QL describes how error messages quote program elements. -** Lua does not use these macros anymore; they are here for -** compatibility only. +@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure +** maximum alignment for the other items in that union. */ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") +#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l + +/* }================================================================== */ + diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 1bd3fb512..e5324c919 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lualib.h $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -35,9 +35,6 @@ 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); - #define LUA_MATHLIBNAME "math" LUAMOD_API int (luaopen_math) (lua_State *L); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 7a67d75aa..5aa55c445 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lundump.c $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -25,7 +26,7 @@ #if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ +#define luai_verifycode(L,f) /* empty */ #endif @@ -36,224 +37,276 @@ typedef struct { } LoadState; -static l_noret error(LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); +static l_noret error (LoadState *S, const char *why) { + luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } /* -** All high-level loads go through LoadVector; you can change it to +** 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])) +#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) -static void LoadBlock (LoadState *S, void *b, size_t size) { +static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated"); + error(S, "truncated chunk"); } -#define LoadVar(S,x) LoadVector(S,&x,1) +#define loadVar(S,x) loadVector(S,&x,1) -static lu_byte LoadByte (LoadState *S) { - lu_byte x; - LoadVar(S, x); - return x; +static lu_byte loadByte (LoadState *S) { + int b = zgetc(S->Z); + if (b == EOZ) + error(S, "truncated chunk"); + return cast_byte(b); } -static int LoadInt (LoadState *S) { - int x; - LoadVar(S, x); +static size_t loadUnsigned (LoadState *S, size_t limit) { + size_t x = 0; + int b; + limit >>= 7; + do { + b = loadByte(S); + if (x >= limit) + error(S, "integer overflow"); + x = (x << 7) | (b & 0x7f); + } while ((b & 0x80) == 0); return x; } -static lua_Number LoadNumber (LoadState *S) { +static size_t loadSize (LoadState *S) { + return loadUnsigned(S, ~(size_t)0); +} + + +static int loadInt (LoadState *S) { + return cast_int(loadUnsigned(S, INT_MAX)); +} + + +static lua_Number loadNumber (LoadState *S) { lua_Number x; - LoadVar(S, x); + loadVar(S, x); return x; } -static lua_Integer LoadInteger (LoadState *S) { +static lua_Integer loadInteger (LoadState *S) { lua_Integer x; - LoadVar(S, x); + loadVar(S, x); return x; } -static TString *LoadString (LoadState *S) { - size_t size = LoadByte(S); - if (size == 0xFF) - LoadVar(S, size); - if (size == 0) +/* +** Load a nullable string into prototype 'p'. +*/ +static TString *loadStringN (LoadState *S, Proto *p) { + lua_State *L = S->L; + TString *ts; + size_t size = loadSize(S); + if (size == 0) /* no string? */ return NULL; else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; - LoadVector(S, buff, size); - return luaS_newlstr(S->L, buff, size); + loadVector(S, buff, size); /* load string into buffer */ + ts = luaS_newlstr(L, buff, size); /* create string */ } else { /* long string */ - TString *ts = luaS_createlngstrobj(S->L, size); - LoadVector(S, getstr(ts), size); /* load directly in final place */ - return ts; + ts = luaS_createlngstrobj(L, size); /* create string */ + setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + luaD_inctop(L); + loadVector(S, getstr(ts), size); /* load directly in final place */ + L->top--; /* pop string */ } + luaC_objbarrier(L, p, ts); + return ts; +} + + +/* +** Load a non-nullable string into prototype 'p'. +*/ +static TString *loadString (LoadState *S, Proto *p) { + TString *st = loadStringN(S, p); + if (st == NULL) + error(S, "bad format for constant string"); + return st; } -static void LoadCode (LoadState *S, Proto *f) { - int n = LoadInt(S); - f->code = luaM_newvector(S->L, n, Instruction); +static void loadCode (LoadState *S, Proto *f) { + int n = loadInt(S); + f->code = luaM_newvectorchecked(S->L, n, Instruction); f->sizecode = n; - LoadVector(S, f->code, n); + loadVector(S, f->code, n); } -static void LoadFunction(LoadState *S, Proto *f, TString *psource); +static void loadFunction(LoadState *S, Proto *f, TString *psource); -static void LoadConstants (LoadState *S, Proto *f) { +static void loadConstants (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); - f->k = luaM_newvector(S->L, n, TValue); + int n = loadInt(S); + f->k = luaM_newvectorchecked(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); + 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); + case LUA_VNIL: + setnilvalue(o); + break; + case LUA_VFALSE: + setbfvalue(o); + break; + case LUA_VTRUE: + setbtvalue(o); + break; + case LUA_VNUMFLT: + setfltvalue(o, loadNumber(S)); + break; + case LUA_VNUMINT: + setivalue(o, loadInteger(S)); + break; + case LUA_VSHRSTR: + case LUA_VLNGSTR: + setsvalue2n(S->L, o, loadString(S, f)); + break; + default: lua_assert(0); } } } -static void LoadProtos (LoadState *S, Proto *f) { +static void loadProtos (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); - f->p = luaM_newvector(S->L, n, Proto *); + int n = loadInt(S); + f->p = luaM_newvectorchecked(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); + luaC_objbarrier(S->L, f, f->p[i]); + loadFunction(S, f->p[i], f->source); } } -static void LoadUpvalues (LoadState *S, Proto *f) { +/* +** Load the upvalues for a function. The names must be filled first, +** because the filling of the other fields can raise read errors and +** the creation of the error message can call an emergency collection; +** in that case all prototypes must be consistent for the GC. +*/ +static void loadUpvalues (LoadState *S, Proto *f) { int i, n; - n = LoadInt(S); - f->upvalues = luaM_newvector(S->L, n, Upvaldesc); + n = loadInt(S); + f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; - for (i = 0; i < n; i++) { - f->upvalues[i].instack = LoadByte(S); - f->upvalues[i].idx = LoadByte(S); + for (i = 0; i < n; i++) { /* following calls can raise errors */ + f->upvalues[i].instack = loadByte(S); + f->upvalues[i].idx = loadByte(S); + f->upvalues[i].kind = loadByte(S); } } -static void LoadDebug (LoadState *S, Proto *f) { +static void loadDebug (LoadState *S, Proto *f) { int i, n; - n = LoadInt(S); - f->lineinfo = luaM_newvector(S->L, n, int); + n = loadInt(S); + f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); f->sizelineinfo = n; - LoadVector(S, f->lineinfo, n); - n = LoadInt(S); - f->locvars = luaM_newvector(S->L, n, LocVar); + loadVector(S, f->lineinfo, n); + n = loadInt(S); + f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); + f->sizeabslineinfo = n; + for (i = 0; i < n; i++) { + f->abslineinfo[i].pc = loadInt(S); + f->abslineinfo[i].line = loadInt(S); + } + n = loadInt(S); + f->locvars = luaM_newvectorchecked(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); + f->locvars[i].varname = loadStringN(S, f); + f->locvars[i].startpc = loadInt(S); + f->locvars[i].endpc = loadInt(S); } - n = LoadInt(S); + n = loadInt(S); for (i = 0; i < n; i++) - f->upvalues[i].name = LoadString(S); + f->upvalues[i].name = loadStringN(S, f); } -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadString(S); +static void loadFunction (LoadState *S, Proto *f, TString *psource) { + f->source = loadStringN(S, f); 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); + 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); + 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)); + if (loadByte(S) != size) + error(S, luaO_pushfstring(S->L, "%s size mismatch", 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); + /* skip 1st char (already read and checked) */ + checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); + if (loadByte(S) != LUAC_VERSION) + error(S, "version mismatch"); + if (loadByte(S) != LUAC_FORMAT) + error(S, "format mismatch"); + checkliteral(S, LUAC_DATA, "corrupted chunk"); 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"); + if (loadInteger(S) != LUAC_INT) + error(S, "integer format mismatch"); + if (loadNumber(S) != LUAC_NUM) + error(S, "float format mismatch"); } /* -** load precompiled chunk +** Load precompiled chunk. */ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { LoadState S; @@ -267,13 +320,14 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.L = L; S.Z = Z; checkHeader(&S); - cl = luaF_newLclosure(L, LoadByte(&S)); - setclLvalue(L, L->top, cl); + cl = luaF_newLclosure(L, loadByte(&S)); + setclLvalue2s(L, L->top, cl); luaD_inctop(L); cl->p = luaF_newproto(L); - LoadFunction(&S, cl->p, NULL); + luaC_objbarrier(L, cl, cl->p); + loadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luai_verifycode(L, buff, cl->p); + luai_verifycode(L, cl->p); return cl; } diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index ce492d689..f3748a998 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lundump.h $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -18,8 +18,12 @@ #define LUAC_INT 0x5678 #define LUAC_NUM cast_num(370.5) -#define MYINT(s) (s[0]-'0') +/* +** Encode major-minor version in one byte, one nibble for each +*/ +#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ #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 */ diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index 10bd238a7..901d985f8 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $ +** $Id: lutf8lib.c $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -20,7 +20,20 @@ #include "lauxlib.h" #include "lualib.h" -#define MAXUNICODE 0x10FFFF + +#define MAXUNICODE 0x10FFFFu + +#define MAXUTF 0x7FFFFFFFu + +/* +** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. +*/ +#if (UINT_MAX >> 30) >= 1 +typedef unsigned int utfint; +#else +typedef unsigned long utfint; +#endif + #define iscont(p) ((*(p) & 0xC0) == 0x80) @@ -35,53 +48,62 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { /* -** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. +** Decode one UTF-8 sequence, returning NULL if byte sequence is +** invalid. The array 'limits' stores the minimum value for each +** sequence length, to check for overlong representations. Its first +** entry forces an error for non-ascii bytes with no continuation +** bytes (count == 0). */ -static const char *utf8_decode (const char *o, int *val) { - 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 */ +static const char *utf8_decode (const char *s, utfint *val, int strict) { + static const utfint limits[] = + {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; + unsigned int c = (unsigned char)s[0]; + utfint 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 */ + for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ + unsigned int cc = (unsigned char)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]) + res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ + if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ } + if (strict) { + /* check for invalid code points; too large or surrogates */ + if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) + return NULL; + } if (val) *val = res; - return (const char *)s + 1; /* +1 to include first byte */ + return 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 +** utf8len(s [, i [, j [, lax]]]) --> 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; + lua_Integer n = 0; /* counter for the number of characters */ + size_t len; /* string length in bytes */ 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); + int lax = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, - "initial position out of string"); + "initial position out of bounds"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, - "final position out of string"); + "final position out of bounds"); while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL); + const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ - lua_pushnil(L); /* return nil ... */ + luaL_pushfail(L); /* return fail ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } @@ -94,28 +116,29 @@ static int utflen (lua_State *L) { /* -** codepoint(s, [i, [j]]) -> returns codepoints for all characters -** that start in the range [i,j] +** codepoint(s, [i, [j [, lax]]]) -> 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 lax = lua_toboolean(L, 4); 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"); + luaL_argcheck(L, posi >= 1, 2, "out of bounds"); + luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; + n = (int)(pose - posi) + 1; /* upper bound for number of returns */ luaL_checkstack(L, n, "string slice too long"); - n = 0; - se = s + pose; + n = 0; /* count the number of returns */ + se = s + pose; /* string end */ for (s += posi - 1; s < se;) { - int code; - s = utf8_decode(s, &code); + utfint code; + s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, "invalid UTF-8 code"); lua_pushinteger(L, code); @@ -126,8 +149,8 @@ static int codepoint (lua_State *L) { 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_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); + luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); lua_pushfstring(L, "%U", (long)code); } @@ -164,7 +187,7 @@ static int byteoffset (lua_State *L) { 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"); + "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ while (posi > 0 && iscont(s + posi)) posi--; @@ -193,12 +216,12 @@ static int byteoffset (lua_State *L) { if (n == 0) /* did it find given character? */ lua_pushinteger(L, posi + 1); else /* no such character */ - lua_pushnil(L); + luaL_pushfail(L); return 1; } -static int iter_aux (lua_State *L) { +static int iter_aux (lua_State *L, int strict) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = lua_tointeger(L, 2) - 1; @@ -211,9 +234,9 @@ static int iter_aux (lua_State *L) { 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)) + utfint code; + const char *next = utf8_decode(s + n, &code, strict); + if (next == NULL) return luaL_error(L, "invalid UTF-8 code"); lua_pushinteger(L, n + 1); lua_pushinteger(L, code); @@ -222,9 +245,19 @@ static int iter_aux (lua_State *L) { } +static int iter_auxstrict (lua_State *L) { + return iter_aux(L, 1); +} + +static int iter_auxlax (lua_State *L) { + return iter_aux(L, 0); +} + + static int iter_codes (lua_State *L) { + int lax = lua_toboolean(L, 2); luaL_checkstring(L, 1); - lua_pushcfunction(L, iter_aux); + lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; @@ -232,7 +265,7 @@ static int iter_codes (lua_State *L) { /* pattern to match a single UTF-8 character */ -#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" +#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" static const luaL_Reg funcs[] = { diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index a2393d50b..3d4fd46c3 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.268.1.1 2017/04/19 17:39:34 roberto Exp $ +** $Id: lvm.c $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -31,50 +31,71 @@ #include "lvm.h" -/* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 2000 +/* +** By default, use jump tables in the main interpreter loop on gcc +** and compatible compilers. +*/ +#if !defined(LUA_USE_JUMPTABLE) +#if defined(__GNUC__) +#define LUA_USE_JUMPTABLE 1 +#else +#define LUA_USE_JUMPTABLE 0 +#endif +#endif +/* limit for table tag-method chains (to avoid infinite loops) */ +#define MAXTAGLOOP 2000 + + /* -** '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. +** 'l_intfitsf' checks whether a given integer is in the range that +** can be converted to a float without rounding. Used in comparisons. */ -#if !defined(l_intfitsf) /* number of bits in the mantissa of a float */ -#define NBM (l_mathlim(MANT_DIG)) +#define NBM (l_floatatt(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 +** Check whether some integers may not fit in a float, testing 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.) +** sizeof(long) == 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)) +/* limit for integers that fit in a float */ +#define MAXINTFITSF ((lua_Unsigned)1 << NBM) -#endif +/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ +#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) + +#else /* all integers fit in a float precisely */ + +#define l_intfitsf(i) 1 #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 from string to a number value. +** If the value is not a string or is a string not representing +** a valid numeral (or if coercions from strings to numbers +** are disabled via macro 'cvt2num'), do not modify 'result' +** and return 0. +*/ +static int l_strton (const TValue *obj, TValue *result) { + lua_assert(obj != result); + if (!cvt2num(obj)) /* is object not a string? */ + return 0; + else + return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); } + /* ** Try to convert a value to a float. The float case is already handled ** by the macro 'tonumber'. @@ -85,8 +106,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { *n = cast_num(ivalue(obj)); return 1; } - else if (cvt2num(obj) && /* string convertible to number? */ - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { + else if (l_strton(obj, &v)) { /* string coercible to number? */ *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } @@ -96,76 +116,173 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* -** 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 +** try to convert a float to an integer, rounding according to 'mode'. */ -int luaV_tointeger (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); +int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { + lua_Number f = l_floor(n); + if (n != f) { /* not an integral value? */ + if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ + else if (mode == F2Iceil) /* needs ceil? */ + f += 1; /* convert floor to ceil (remember: n != f) */ } + return lua_numbertointeger(f, p); +} + + +/* +** try to convert a value to an integer, rounding according to 'mode', +** without string coercion. +** ("Fast track" handled by macro 'tointegerns'.) +*/ +int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { + if (ttisfloat(obj)) + return luaV_flttointeger(fltvalue(obj), p, mode); else if (ttisinteger(obj)) { *p = ivalue(obj); return 1; } - else if (cvt2num(obj) && - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { - obj = &v; - goto again; /* convert result from 'luaO_str2num' to an integer */ + else + return 0; +} + + +/* +** try to convert a value to an integer. +*/ +int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { + TValue v; + if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ + obj = &v; /* change it to point to its corresponding number */ + return luaV_tointegerns(obj, p, mode); +} + + +/* +** Try to convert a 'for' limit to an integer, preserving the semantics +** of the loop. Return true if the loop must not run; otherwise, '*p' +** gets the integer limit. +** (The following explanation assumes a positive step; it is valid for +** negative steps mutatis mutandis.) +** If the limit is an integer or can be converted to an integer, +** rounding down, that is the limit. +** Otherwise, check whether the limit can be converted to a float. If +** the float is too large, clip it to LUA_MAXINTEGER. If the float +** is too negative, the loop should not run, because any initial +** integer value is greater than such limit; so, the function returns +** true to signal that. (For this latter case, no integer limit would be +** correct; even a limit of LUA_MININTEGER would run the loop once for +** an initial value equal to LUA_MININTEGER.) +*/ +static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, + lua_Integer *p, lua_Integer step) { + if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { + /* not coercible to in integer */ + lua_Number flim; /* try to convert to float */ + if (!tonumber(lim, &flim)) /* cannot convert to float? */ + luaG_forerror(L, lim, "limit"); + /* else 'flim' is a float out of integer bounds */ + if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ + if (step < 0) return 1; /* initial value must be less than it */ + *p = LUA_MAXINTEGER; /* truncate */ + } + else { /* it is less than min integer */ + if (step > 0) return 1; /* initial value must be greater than it */ + *p = LUA_MININTEGER; /* truncate */ + } } - return 0; /* conversion failed */ + return (step > 0 ? init > *p : init < *p); /* not to run? */ } /* -** 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. +** Prepare a numerical for loop (opcode OP_FORPREP). +** Return true to skip the loop. Otherwise, +** after preparation, stack will be as follows: +** ra : internal index (safe copy of the control variable) +** ra + 1 : loop counter (integer loops) or limit (float loops) +** ra + 2 : step +** ra + 3 : control variable */ -static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, - int *stopnow) { - *stopnow = 0; /* usually, let loops run */ - 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 (luai_numlt(0, n)) { /* if true, float is larger than max integer */ - *p = LUA_MAXINTEGER; - if (step < 0) *stopnow = 1; +static int forprep (lua_State *L, StkId ra) { + TValue *pinit = s2v(ra); + TValue *plimit = s2v(ra + 1); + TValue *pstep = s2v(ra + 2); + if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ + lua_Integer init = ivalue(pinit); + lua_Integer step = ivalue(pstep); + lua_Integer limit; + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + setivalue(s2v(ra + 3), init); /* control variable */ + if (forlimit(L, init, plimit, &limit, step)) + return 1; /* skip the loop */ + else { /* prepare loop counter */ + lua_Unsigned count; + if (step > 0) { /* ascending loop? */ + count = l_castS2U(limit) - l_castS2U(init); + if (step != 1) /* avoid division in the too common case */ + count /= l_castS2U(step); + } + else { /* step < 0; descending loop */ + count = l_castS2U(init) - l_castS2U(limit); + /* 'step+1' avoids negating 'mininteger' */ + count /= l_castS2U(-(step + 1)) + 1u; + } + /* store the counter in place of the limit (which won't be + needed anymore */ + setivalue(plimit, l_castU2S(count)); } - else { /* float is smaller than min integer */ - *p = LUA_MININTEGER; - if (step >= 0) *stopnow = 1; + } + else { /* try making all values floats */ + lua_Number init; lua_Number limit; lua_Number step; + if (unlikely(!tonumber(plimit, &limit))) + luaG_forerror(L, plimit, "limit"); + if (unlikely(!tonumber(pstep, &step))) + luaG_forerror(L, pstep, "step"); + if (unlikely(!tonumber(pinit, &init))) + luaG_forerror(L, pinit, "initial value"); + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + if (luai_numlt(0, step) ? luai_numlt(limit, init) + : luai_numlt(init, limit)) + return 1; /* skip the loop */ + else { + /* make sure internal values are all floats */ + setfltvalue(plimit, limit); + setfltvalue(pstep, step); + setfltvalue(s2v(ra), init); /* internal index */ + setfltvalue(s2v(ra + 3), init); /* control variable */ } } - return 1; + return 0; +} + + +/* +** Execute a step of a float numerical for loop, returning +** true iff the loop must continue. (The integer case is +** written online with opcode OP_FORLOOP, for performance.) +*/ +static int floatforloop (StkId ra) { + lua_Number step = fltvalue(s2v(ra + 2)); + lua_Number limit = fltvalue(s2v(ra + 1)); + lua_Number idx = fltvalue(s2v(ra)); /* internal index */ + idx = luai_numadd(L, idx, step); /* increment index */ + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + chgfltvalue(s2v(ra), idx); /* update internal index */ + setfltvalue(s2v(ra + 3), idx); /* and control variable */ + return 1; /* jump back */ + } + else + return 0; /* finish the loop */ } /* ** 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). +** t[k] entry (which must be empty). */ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot) { @@ -175,25 +292,25 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, if (slot == NULL) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (ttisnil(tm)) + if (unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } else { /* 't' is a table */ - lua_assert(ttisnil(slot)); + lua_assert(isempty(slot)); tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ - setnilvalue(val); /* result is nil */ + setnilvalue(s2v(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 */ + luaT_callTMres(L, tm, t, key, val); /* call it */ return; } t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ + if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ setobj2s(L, val, slot); /* done */ return; } @@ -206,12 +323,12 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId 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.) +** to the entry 't[key]', or to a value with an absent key if there +** is no such entry. (The value at 'slot' must be empty, otherwise +** 'luaV_fastget' would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot) { + TValue *val, const TValue *slot) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ @@ -219,40 +336,43 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, Table *h = hvalue(t); /* save 't' table */ if (isshared(h)) luaG_typeerror(L, t, "change"); - lua_assert(ttisnil(slot)); /* old value must be nil */ + lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (slot == luaO_nilobject) /* no previous entry? */ + if (isabstkey(slot)) /* 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 *, slot), val); /* set its new value */ invalidateTMcache(h); - luaC_barrierback(L, h, val); + luaC_barrierback(L, obj2gco(h), val); return; } /* else will try the metamethod */ } else { /* not a table; check metamethod */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) + tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); + if (unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ if (ttisfunction(tm)) { - luaT_callTM(L, tm, t, key, val, 0); + luaT_callTM(L, tm, t, key, val); return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastset(L, t, key, slot, luaH_get, val)) + if (luaV_fastset(L, t, key, slot, luaH_get)) { + luaV_finishfastset(L, t, slot, val); return; /* done */ - /* else loop */ + } + /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } luaG_runerror(L, "'__newindex' 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'. +** Compare two strings 'ls' x 'rs', returning an integer less-equal- +** -greater than zero if 'ls' is less-equal-greater 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. @@ -271,7 +391,7 @@ static int l_strcmp (const TString *ls, const TString *rs) { 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) */ + return -1; /* 'ls' is less 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; @@ -283,25 +403,24 @@ 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. +** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. +** If 'ceil(f)' is out of integer range, either 'f' is greater than +** all integers or less than all integers. +** (The test with 'l_intfitsf' is only for performance; the else +** case is correct for all values, but it is slow due to the conversion +** from float to int.) +** 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; + if (l_intfitsf(i)) + return luai_numlt(cast_num(i), f); /* compare them as floats */ + else { /* i < f <=> i < ceil(f) */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ + return i < fi; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f > 0; /* greater? */ } -#endif - return luai_numlt(cast_num(i), f); /* compare them as floats */ } @@ -310,17 +429,49 @@ static int LTintfloat (lua_Integer i, lua_Number 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; + if (l_intfitsf(i)) + return luai_numle(cast_num(i), f); /* compare them as floats */ + else { /* i <= f <=> i <= floor(f) */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ + return i <= fi; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f > 0; /* greater? */ + } +} + + +/* +** Check whether float 'f' is less than integer 'i'. +** See comments on previous function. +*/ +static int LTfloatint (lua_Number f, lua_Integer i) { + if (l_intfitsf(i)) + return luai_numlt(f, cast_num(i)); /* compare them as floats */ + else { /* f < i <=> floor(f) < i */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ + return fi < i; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f < 0; /* less? */ + } +} + + +/* +** Check whether float 'f' is less than or equal to integer 'i'. +** See comments on previous function. +*/ +static int LEfloatint (lua_Number f, lua_Integer i) { + if (l_intfitsf(i)) + return luai_numle(f, cast_num(i)); /* compare them as floats */ + else { /* f <= i <=> ceil(f) <= i */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ + return fi <= i; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f < 0; /* less? */ } -#endif - return luai_numle(cast_num(i), f); /* compare them as floats */ } @@ -328,6 +479,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) { ** Return 'l < r', for numbers. */ static int LTnum (const TValue *l, const TValue *r) { + lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) @@ -339,10 +491,8 @@ static int LTnum (const TValue *l, const TValue *r) { 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) ? */ + else /* 'l' is float and 'r' is int */ + return LTfloatint(lf, ivalue(r)); } } @@ -351,6 +501,7 @@ static int LTnum (const TValue *l, const TValue *r) { ** Return 'l <= r', for numbers. */ static int LEnum (const TValue *l, const TValue *r) { + lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) @@ -362,53 +513,53 @@ static int LEnum (const TValue *l, const TValue *r) { 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) ? */ + else /* 'l' is float and 'r' is int */ + return LEfloatint(lf, ivalue(r)); } } +/* +** return 'l < r' for non-numbers. +*/ +static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { + lua_assert(!ttisnumber(l) || !ttisnumber(r)); + if (ttisstring(l) && ttisstring(r)) /* both are strings? */ + return l_strcmp(tsvalue(l), tsvalue(r)) < 0; + else + return luaT_callorderTM(L, l, r, TM_LT); +} + + /* ** 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)) /* 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? */ - luaG_ordererror(L, l, r); /* error */ - return res; + else return lessthanothers(L, l, r); +} + + +/* +** return 'l <= r' for non-numbers. +*/ +static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { + lua_assert(!ttisnumber(l) || !ttisnumber(r)); + if (ttisstring(l) && ttisstring(r)) /* both are strings? */ + return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; + else + return luaT_callorderTM(L, l, r, TM_LE); } /* -** 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) /* try 'le' */ - 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 */ - } + else return lessequalothers(L, l, r); } @@ -418,25 +569,24 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; - if (ttype(t1) != ttype(t2)) { /* not the same variant? */ - if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) + if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */ + if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ lua_Integer i1, i2; /* compare them as integers */ - return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); + return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2); } } /* values have same type and same variant */ - switch (ttype(t1)) { - case LUA_TNIL: return 1; - 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(tsvalue(t1), tsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); - case LUA_TUSERDATA: { + switch (ttypetag(t1)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; + case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); + case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); + case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_VLCF: return fvalue(t1) == fvalue(t2); + case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); + case LUA_VUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); @@ -444,7 +594,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } - case LUA_TTABLE: { + case LUA_VTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); @@ -457,8 +607,10 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } 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); + else { + luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ + return !l_isfalse(s2v(L->top)); + } } @@ -472,8 +624,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { 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)); + size_t l = vslen(s2v(top - n)); /* length of string being copied */ + memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); tl += l; } while (--n > 0); } @@ -484,25 +636,27 @@ static void copy2buff (StkId top, int n, char *buff) { ** from 'L->top - total' up to 'L->top - 1'. */ void luaV_concat (lua_State *L, int total) { - lua_assert(total >= 2); + if (total == 1) + return; /* "all" values already concatenated */ do { StkId top = L->top; 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 (isemptystr(top - 1)) /* second operand is empty? */ - cast_void(tostring(L, top - 2)); /* result is first operand */ - else if (isemptystr(top - 2)) { /* first operand is an empty string? */ + if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || + !tostring(L, s2v(top - 1))) + luaT_tryconcatTM(L); + else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ + cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ + else if (isemptystr(s2v(top - 2))) { /* first operand is 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 = vslen(top - 1); + size_t tl = vslen(s2v(top - 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) + for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { + size_t l = vslen(s2v(top - n - 1)); + if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) luaG_runerror(L, "string length overflow"); tl += l; } @@ -524,34 +678,34 @@ void luaV_concat (lua_State *L, int total) { /* -** Main operation 'ra' = #rb'. +** Main operation 'ra = #rb'. */ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; - switch (ttype(rb)) { - case LUA_TTABLE: { + switch (ttypetag(rb)) { + case LUA_VTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setivalue(ra, luaH_getn(h)); /* else primitive len */ + setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ return; } - case LUA_TSHRSTR: { - setivalue(ra, tsvalue(rb)->shrlen); + case LUA_VSHRSTR: { + setivalue(s2v(ra), tsvalue(rb)->shrlen); return; } - case LUA_TLNGSTR: { - setivalue(ra, tsvalue(rb)->u.lnglen); + case LUA_VLNGSTR: { + setivalue(s2v(ra), tsvalue(rb)->u.lnglen); return; } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (ttisnil(tm)) /* no metamethod? */ + if (unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } } - luaT_callTM(L, tm, rb, rb, ra, 1); + luaT_callTMres(L, tm, rb, rb, ra); } @@ -561,8 +715,8 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** '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 */ +lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { + if (unlikely(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 */ @@ -579,29 +733,41 @@ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { /* ** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment -** about luaV_div.) +** about luaV_idiv.) */ 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 (unlikely(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? */ + if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ r += n; /* correct result for different rounding */ return r; } } +/* +** Float modulus +*/ +lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { + lua_Number r; + luai_nummod(L, m, n, r); + 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'.) */ +#define luaV_shiftr(x,y) luaV_shiftl(x,-(y)) + lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; @@ -616,9 +782,7 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { /* ** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the closure is not cached if prototype is -** already black (which means that 'cache' was already cleared by the -** GC). +** its upvalues. */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { @@ -627,75 +791,69 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; - setclLvalue(L, ra, ncl); /* anchor new closure in stack */ + setclLvalue2s(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->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; - ncl->upvals[i]->refcount++; - /* new closure is white, so we do not need a barrier here */ + luaC_objbarrier(L, ncl, ncl->upvals[i]); } } /* -** finish execution of an opcode interrupted by an yield +** finish execution of an opcode interrupted by a yield */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->u.l.base; + StkId base = ci->func + 1; 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_IDIV: - case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: - case OP_MOD: case OP_POW: + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); + break; + } case OP_UNM: case OP_BNOT: case OP_LEN: - case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { + case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: + case OP_GETFIELD: case OP_SELF: { setobjs2s(L, base + GETARG_A(inst), --L->top); break; } - case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); + case OP_LT: case OP_LE: + case OP_LTI: case OP_LEI: + case OP_GTI: case OP_GEI: + case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ + int res = !l_isfalse(s2v(L->top - 1)); L->top--; +#if defined(LUA_COMPAT_LT_LE) if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - lua_assert(op == OP_LE); ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } +#endif lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ + if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } case OP_CONCAT: { - 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 */ - if (total > 1) { /* are there elements to concat? */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - } - /* move final result to final position */ - setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); - L->top = ci->top; /* restore top */ + StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */ + int a = GETARG_A(inst); /* first element to concatenate */ + int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ + setobjs2s(L, top - 2, top); /* put TM result in proper position */ + L->top = top - 1; /* top is one after last element (at top-2) */ + luaV_concat(L, total); /* concat them (may yield again) */ break; } - case OP_TFORCALL: { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); - L->top = ci->top; /* correct top */ + default: { + /* only these other opcodes can yield */ + lua_assert(op == OP_TFORCALL || op == OP_CALL || + op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || + op == OP_SETI || op == OP_SETFIELD); break; } - case OP_CALL: { - if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = ci->top; /* adjust results */ - break; - } - case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: - break; - default: lua_assert(0); } } @@ -704,10 +862,172 @@ void luaV_finishOp (lua_State *L) { /* ** {================================================================== -** Function 'luaV_execute': main interpreter loop +** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' ** =================================================================== */ +#define l_addi(L,a,b) intop(+, a, b) +#define l_subi(L,a,b) intop(-, a, b) +#define l_muli(L,a,b) intop(*, a, b) +#define l_band(a,b) intop(&, a, b) +#define l_bor(a,b) intop(|, a, b) +#define l_bxor(a,b) intop(^, a, b) + +#define l_lti(a,b) (a < b) +#define l_lei(a,b) (a <= b) +#define l_gti(a,b) (a > b) +#define l_gei(a,b) (a >= b) + + +/* +** Arithmetic operations with immediate operands. 'iop' is the integer +** operation, 'fop' is the float operation. +*/ +#define op_arithI(L,iop,fop) { \ + TValue *v1 = vRB(i); \ + int imm = GETARG_sC(i); \ + if (ttisinteger(v1)) { \ + lua_Integer iv1 = ivalue(v1); \ + pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ + } \ + else if (ttisfloat(v1)) { \ + lua_Number nb = fltvalue(v1); \ + lua_Number fimm = cast_num(imm); \ + pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ + }} + + +/* +** Auxiliary function for arithmetic operations over floats and others +** with two register operands. +*/ +#define op_arithf_aux(L,v1,v2,fop) { \ + lua_Number n1; lua_Number n2; \ + if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ + pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ + }} + + +/* +** Arithmetic operations over floats and others with register operands. +*/ +#define op_arithf(L,fop) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + op_arithf_aux(L, v1, v2, fop); } + + +/* +** Arithmetic operations with K operands for floats. +*/ +#define op_arithfK(L,fop) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + op_arithf_aux(L, v1, v2, fop); } + + +/* +** Arithmetic operations over integers and floats. +*/ +#define op_arith_aux(L,v1,v2,iop,fop) { \ + if (ttisinteger(v1) && ttisinteger(v2)) { \ + lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ + pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ + } \ + else op_arithf_aux(L, v1, v2, fop); } + + +/* +** Arithmetic operations with register operands. +*/ +#define op_arith(L,iop,fop) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + op_arith_aux(L, v1, v2, iop, fop); } + + +/* +** Arithmetic operations with K operands. +*/ +#define op_arithK(L,iop,fop) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + op_arith_aux(L, v1, v2, iop, fop); } + + +/* +** Bitwise operations with constant operand. +*/ +#define op_bitwiseK(L,op) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + lua_Integer i1; \ + lua_Integer i2 = ivalue(v2); \ + if (tointegerns(v1, &i1)) { \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ + }} + + +/* +** Bitwise operations with register operands. +*/ +#define op_bitwise(L,op) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + lua_Integer i1; lua_Integer i2; \ + if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ + }} + + +/* +** Order operations with register operands. 'opn' actually works +** for all numbers, but the fast track improves performance for +** integers. +*/ +#define op_order(L,opi,opn,other) { \ + int cond; \ + TValue *rb = vRB(i); \ + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(s2v(ra)); \ + lua_Integer ib = ivalue(rb); \ + cond = opi(ia, ib); \ + } \ + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ + cond = opn(s2v(ra), rb); \ + else \ + Protect(cond = other(L, s2v(ra), rb)); \ + docondjump(); } + + +/* +** Order operations with immediate operand. (Immediate operand is +** always small enough to have an exact representation as a float.) +*/ +#define op_orderI(L,opi,opf,inv,tm) { \ + int cond; \ + int im = GETARG_sB(i); \ + if (ttisinteger(s2v(ra))) \ + cond = opi(ivalue(s2v(ra)), im); \ + else if (ttisfloat(s2v(ra))) { \ + lua_Number fa = fltvalue(s2v(ra)); \ + lua_Number fim = cast_num(im); \ + cond = opf(fa, fim); \ + } \ + else { \ + int isf = GETARG_C(i); \ + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + } \ + docondjump(); } + +/* }================================================================== */ + + +/* +** {================================================================== +** Function 'luaV_execute': main interpreter loop +** =================================================================== +*/ /* ** some macros for common tasks in 'luaV_execute' @@ -715,40 +1035,89 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) -#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 RB(i) (base+GETARG_B(i)) +#define vRB(i) s2v(RB(i)) +#define KB(i) (k+GETARG_B(i)) +#define RC(i) (base+GETARG_C(i)) +#define vRC(i) s2v(RC(i)) +#define KC(i) (k+GETARG_C(i)) +#define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) + + + +#define updatetrap(ci) (trap = ci->u.l.trap) +#define updatebase(ci) (base = ci->func + 1) + + +#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } + + +/* +** Execute a jump instruction. The 'updatetrap' allows signals to stop +** tight loops. (Without it, the local copy of 'trap' could never change.) +*/ +#define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } -/* 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); \ - ci->u.l.savedpc += GETARG_sBx(i) + e; } /* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } +#define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } + +/* +** do a conditional jump: skip next instruction if 'cond' is not what +** was expected (parameter 'k'), else do next instruction, which must +** be a jump. +*/ +#define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); + + +/* +** Correct global 'pc'. +*/ +#define savepc(L) (ci->u.l.savedpc = pc) + +/* +** Whenever code can raise errors, the global 'pc' and the global +** 'top' must be correct to report occasional errors. +*/ +#define savestate(L,ci) (savepc(L), L->top = ci->top) -#define Protect(x) { {x;}; base = ci->u.l.base; } +/* +** Protect code that, in general, can raise errors, reallocate the +** stack, and change the hooks. +*/ +#define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) + +/* special version that does not change the top */ +#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) + +/* +** Protect code that will finish the loop (returns) or can only raise +** errors. (That is, it will not return to the interpreter main loop +** after changing the stack or hooks.) +*/ +#define halfProtect(exp) (savestate(L,ci), (exp)) + +/* idem, but without changing the stack */ +#define halfProtectNT(exp) (savepc(L), (exp)) + +/* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - Protect(L->top = ci->top)); /* restore top */ \ + { luaC_condGC(L, (savepc(L), L->top = (c)), \ + updatetrap(ci)); \ 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)); \ + if (trap) { /* stack reallocation or hooks? */ \ + trap = luaG_traceexec(L, pc); /* handle hooks */ \ + updatebase(ci); /* correct stack */ \ + } \ + i = *(pc++); \ 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) @@ -756,43 +1125,52 @@ void luaV_finishOp (lua_State *L) { #define vmbreak break -/* -** copy of 'luaV_gettable', but protecting the call to potential -** metamethod (which can reallocate the stack) -*/ -#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' */ -#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; +void luaV_execute (lua_State *L, CallInfo *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); /* 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 */ + const Instruction *pc; + int trap; +#if LUA_USE_JUMPTABLE +#include "ljumptab.h" +#endif + tailcall: + trap = L->hookmask; + cl = clLvalue(s2v(ci->func)); + k = cl->p->k; + pc = ci->u.l.savedpc; + if (trap) { + if (cl->p->is_vararg) + trap = 0; /* hooks will start after VARARGPREP instruction */ + else if (pc == cl->p->code) /* first instruction (not resuming)? */ + luaD_hookcall(L, ci); + ci->u.l.trap = 1; /* there may be other hooks */ + } + base = ci->func + 1; /* main loop of interpreter */ for (;;) { - Instruction i; - StkId ra; + Instruction i; /* instruction being executed */ + StkId ra; /* instruction's A register */ vmfetch(); + lua_assert(base == ci->func + 1); + lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + /* invalidate top for instructions not expecting it */ + lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); vmbreak; } + vmcase(OP_LOADI) { + lua_Integer b = GETARG_sBx(i); + setivalue(s2v(ra), b); + vmbreak; + } + vmcase(OP_LOADF) { + int b = GETARG_sBx(i); + setfltvalue(s2v(ra), cast_num(b)); + vmbreak; + } vmcase(OP_LOADK) { TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); @@ -800,20 +1178,27 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LOADKX) { TValue *rb; - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - rb = k + GETARG_Ax(*ci->u.l.savedpc++); + rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } - vmcase(OP_LOADBOOL) { - setbvalue(ra, GETARG_B(i)); - if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ + vmcase(OP_LOADFALSE) { + setbfvalue(s2v(ra)); + vmbreak; + } + vmcase(OP_LFALSESKIP) { + setbfvalue(s2v(ra)); + pc++; /* skip next instruction */ + vmbreak; + } + vmcase(OP_LOADTRUE) { + setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { int b = GETARG_B(i); do { - setnilvalue(ra++); + setnilvalue(s2v(ra++)); } while (b--); vmbreak; } @@ -822,297 +1207,402 @@ void luaV_execute (lua_State *L) { setobj2s(L, ra, cl->upvals[b]->v); vmbreak; } + vmcase(OP_SETUPVAL) { + UpVal *uv = cl->upvals[GETARG_B(i)]; + setobj(L, uv->v, s2v(ra)); + luaC_barrier(L, uv, s2v(ra)); + vmbreak; + } vmcase(OP_GETTABUP) { + const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v; - TValue *rc = RKC(i); - gettableProtected(L, upval, rc, ra); + TValue *rc = KC(i); + TString *key = tsvalue(rc); /* key must be a string */ + if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { + setobj2s(L, ra, slot); + } + else + Protect(luaV_finishget(L, upval, rc, ra, slot)); vmbreak; } vmcase(OP_GETTABLE) { - StkId rb = RB(i); - TValue *rc = RKC(i); - gettableProtected(L, rb, rc, ra); + const TValue *slot; + TValue *rb = vRB(i); + TValue *rc = vRC(i); + lua_Unsigned n; + if (ttisinteger(rc) /* fast track for integers? */ + ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot)) + : luaV_fastget(L, rb, rc, slot, luaH_get)) { + setobj2s(L, ra, slot); + } + else + Protect(luaV_finishget(L, rb, rc, ra, slot)); + vmbreak; + } + vmcase(OP_GETI) { + const TValue *slot; + TValue *rb = vRB(i); + int c = GETARG_C(i); + if (luaV_fastgeti(L, rb, c, slot)) { + setobj2s(L, ra, slot); + } + else { + TValue key; + setivalue(&key, c); + Protect(luaV_finishget(L, rb, &key, ra, slot)); + } + vmbreak; + } + vmcase(OP_GETFIELD) { + const TValue *slot; + TValue *rb = vRB(i); + TValue *rc = KC(i); + TString *key = tsvalue(rc); /* key must be a string */ + if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { + setobj2s(L, ra, slot); + } + else + Protect(luaV_finishget(L, rb, rc, ra, slot)); vmbreak; } vmcase(OP_SETTABUP) { + const TValue *slot; TValue *upval = cl->upvals[GETARG_A(i)]->v; - TValue *rb = RKB(i); + TValue *rb = KB(i); TValue *rc = RKC(i); - settableProtected(L, upval, rb, rc); + TString *key = tsvalue(rb); /* key must be a string */ + if (luaV_fastset(L, upval, key, slot, luaH_getshortstr)) { + luaV_finishfastset(L, upval, slot, rc); + } + else + Protect(luaV_finishset(L, upval, rb, rc, slot)); vmbreak; } - vmcase(OP_SETUPVAL) { - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_upvalbarrier(L, uv); + vmcase(OP_SETTABLE) { + const TValue *slot; + TValue *rb = vRB(i); /* key (table is in 'ra') */ + TValue *rc = RKC(i); /* value */ + lua_Unsigned n; + if (ttisinteger(rb) /* fast track for integers? */ + ? (cast_void(n = ivalue(rb)), luaV_fastseti(L, s2v(ra), n, slot)) + : luaV_fastset(L, s2v(ra), rb, slot, luaH_get)) { + luaV_finishfastset(L, s2v(ra), slot, rc); + } + else + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } - vmcase(OP_SETTABLE) { - TValue *rb = RKB(i); + vmcase(OP_SETI) { + const TValue *slot; + int c = GETARG_B(i); TValue *rc = RKC(i); - settableProtected(L, ra, rb, rc); + if (luaV_fastseti(L, s2v(ra), c, slot)) { + luaV_finishfastset(L, s2v(ra), slot, rc); + } + else { + TValue key; + setivalue(&key, c); + Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); + } + vmbreak; + } + vmcase(OP_SETFIELD) { + const TValue *slot; + TValue *rb = KB(i); + TValue *rc = RKC(i); + TString *key = tsvalue(rb); /* key must be a string */ + if (luaV_fastset(L, s2v(ra), key, slot, luaH_getshortstr)) { + luaV_finishfastset(L, s2v(ra), slot, rc); + } + else + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } vmcase(OP_NEWTABLE) { - int b = GETARG_B(i); - int c = GETARG_C(i); - Table *t = luaH_new(L); - sethvalue(L, ra, t); + int b = GETARG_B(i); /* log2(hash size) + 1 */ + int c = GETARG_C(i); /* array size */ + Table *t; + if (b > 0) + b = 1 << (b - 1); /* size is 2^(b - 1) */ + lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); + if (TESTARG_k(i)) /* non-zero extra argument? */ + c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ + pc++; /* skip extra argument */ + L->top = ra + 1; /* correct top in case of emergency GC */ + t = luaH_new(L); /* memory allocation */ + sethvalue2s(L, ra, t); if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); + luaH_resize(L, t, c, b); /* idem */ checkGC(L, ra + 1); vmbreak; } vmcase(OP_SELF) { - const TValue *aux; - StkId rb = RB(i); + const TValue *slot; + TValue *rb = vRB(i); 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); + setobj2s(L, ra + 1, rb); + if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { + setobj2s(L, ra, slot); } - else Protect(luaV_finishget(L, rb, rc, ra, aux)); + else + Protect(luaV_finishget(L, rb, rc, ra, slot)); 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)); + vmcase(OP_ADDI) { + op_arithI(L, l_addi, luai_numadd); + vmbreak; + } + vmcase(OP_ADDK) { + op_arithK(L, l_addi, luai_numadd); + vmbreak; + } + vmcase(OP_SUBK) { + op_arithK(L, l_subi, luai_numsub); + vmbreak; + } + vmcase(OP_MULK) { + op_arithK(L, l_muli, luai_nummul); + vmbreak; + } + vmcase(OP_MODK) { + op_arithK(L, luaV_mod, luaV_modf); + vmbreak; + } + vmcase(OP_POWK) { + op_arithfK(L, luai_numpow); + vmbreak; + } + vmcase(OP_DIVK) { + op_arithfK(L, luai_numdiv); + vmbreak; + } + vmcase(OP_IDIVK) { + op_arithK(L, luaV_idiv, luai_numidiv); + vmbreak; + } + vmcase(OP_BANDK) { + op_bitwiseK(L, l_band); + vmbreak; + } + vmcase(OP_BORK) { + op_bitwiseK(L, l_bor); + vmbreak; + } + vmcase(OP_BXORK) { + op_bitwiseK(L, l_bxor); + vmbreak; + } + vmcase(OP_SHRI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numadd(L, nb, nc)); + vmbreak; + } + vmcase(OP_SHLI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } + vmbreak; + } + vmcase(OP_ADD) { + op_arith(L, l_addi, luai_numadd); 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)); } + op_arith(L, l_subi, luai_numsub); 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)); } + op_arith(L, l_muli, luai_nummul); + vmbreak; + } + vmcase(OP_MOD) { + op_arith(L, luaV_mod, luaV_modf); + vmbreak; + } + vmcase(OP_POW) { + op_arithf(L, luai_numpow); 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)); } + op_arithf(L, luai_numdiv); + vmbreak; + } + vmcase(OP_IDIV) { /* floor division */ + op_arith(L, luaV_idiv, luai_numidiv); 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)); } + op_bitwise(L, l_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)); } + op_bitwise(L, l_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)); } + op_bitwise(L, l_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)); } + vmcase(OP_SHR) { + op_bitwise(L, luaV_shiftr); 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)); } + vmcase(OP_SHL) { + op_bitwise(L, luaV_shiftl); 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)); } + vmcase(OP_MMBIN) { + Instruction pi = *(pc - 2); /* original arith. expression */ + TValue *rb = vRB(i); + TMS tm = (TMS)GETARG_C(i); + StkId result = RA(pi); + lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); + Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); 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)); } + vmcase(OP_MMBINI) { + Instruction pi = *(pc - 2); /* original arith. expression */ + int imm = GETARG_sB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); 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)); } + vmcase(OP_MMBINK) { + Instruction pi = *(pc - 2); /* original arith. expression */ + TValue *imm = KB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_UNM) { - TValue *rb = RB(i); + TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { lua_Integer ib = ivalue(rb); - setivalue(ra, intop(-, 0, ib)); + setivalue(s2v(ra), intop(-, 0, ib)); } - else if (tonumber(rb, &nb)) { - setfltvalue(ra, luai_numunm(L, nb)); + else if (tonumberns(rb, nb)) { + setfltvalue(s2v(ra), luai_numunm(L, nb)); } - else { + else Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); - } vmbreak; } vmcase(OP_BNOT) { - TValue *rb = RB(i); + TValue *rb = vRB(i); lua_Integer ib; - if (tointeger(rb, &ib)) { - setivalue(ra, intop(^, ~l_castS2U(0), ib)); + if (tointegerns(rb, &ib)) { + setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } - else { + 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); + TValue *rb = vRB(i); + if (l_isfalse(rb)) + setbtvalue(s2v(ra)); + else + setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LEN) { - Protect(luaV_objlen(L, ra, RB(i))); + Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { - int b = GETARG_B(i); - int c = GETARG_C(i); - 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 */ - rb = base + b; - setobjs2s(L, ra, rb); - checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = ci->top; /* restore top */ + int n = GETARG_B(i); /* number of elements to concatenate */ + L->top = ra + n; /* mark the end of concat operands */ + ProtectNT(luaV_concat(L, n)); + checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ + vmbreak; + } + vmcase(OP_CLOSE) { + Protect(luaF_close(L, ra, LUA_OK)); + vmbreak; + } + vmcase(OP_TBC) { + /* create new to-be-closed upvalue */ + halfProtect(luaF_newtbcupval(L, ra)); vmbreak; } vmcase(OP_JMP) { - lua_checksig(L); dojump(ci, i, 0); vmbreak; } vmcase(OP_EQ) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - Protect( - if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) + int cond; + TValue *rb = vRB(i); + Protect(cond = luaV_equalobj(L, s2v(ra), rb)); + docondjump(); vmbreak; } vmcase(OP_LT) { - Protect( - if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) + op_order(L, l_lti, LTnum, lessthanothers); vmbreak; } vmcase(OP_LE) { - Protect( - if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) + op_order(L, l_lei, LEnum, lessequalothers); + vmbreak; + } + vmcase(OP_EQK) { + TValue *rb = KB(i); + /* basic types do not use '__eq'; we can use raw equality */ + int cond = luaV_rawequalobj(s2v(ra), rb); + docondjump(); + vmbreak; + } + vmcase(OP_EQI) { + int cond; + int im = GETARG_sB(i); + if (ttisinteger(s2v(ra))) + cond = (ivalue(s2v(ra)) == im); + else if (ttisfloat(s2v(ra))) + cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); + else + cond = 0; /* other types cannot be equal to a number */ + docondjump(); + vmbreak; + } + vmcase(OP_LTI) { + op_orderI(L, l_lti, luai_numlt, 0, TM_LT); + vmbreak; + } + vmcase(OP_LEI) { + op_orderI(L, l_lei, luai_numle, 0, TM_LE); + vmbreak; + } + vmcase(OP_GTI) { + op_orderI(L, l_gti, luai_numgt, 1, TM_LT); + vmbreak; + } + vmcase(OP_GEI) { + op_orderI(L, l_gei, luai_numge, 1, TM_LE); vmbreak; } vmcase(OP_TEST) { - if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) - ci->u.l.savedpc++; - else - donextjump(ci); + int cond = !l_isfalse(s2v(ra)); + docondjump(); vmbreak; } vmcase(OP_TESTSET) { - TValue *rb = RB(i); - if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) - ci->u.l.savedpc++; + TValue *rb = vRB(i); + if (l_isfalse(rb) == GETARG_k(i)) + pc++; else { - setobjs2s(L, ra, rb); + setobj2s(L, ra, rb); donextjump(ci); } vmbreak; @@ -1120,184 +1610,195 @@ 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 */ - Protect((void)0); /* update 'base' */ - } - else { /* Lua function */ - ci = L->ci; - goto newframe; /* restart luaV_execute over new Lua function */ - } + if (b != 0) /* fixed number of arguments? */ + L->top = ra + b; /* top signals number of arguments */ + /* else previous instruction set top */ + ProtectNT(luaD_call(L, ra, nresults)); vmbreak; } 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? */ - Protect((void)0); /* update 'base' */ + int b = GETARG_B(i); /* number of arguments + 1 (function) */ + int nparams1 = GETARG_C(i); + /* delat is virtual 'func' - real 'func' (vararg functions) */ + int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; + if (b != 0) + L->top = ra + b; + else /* previous instruction set top */ + b = cast_int(L->top - ra); + savepc(ci); /* some calls here can raise errors */ + if (TESTARG_k(i)) { + /* close upvalues from current call; the compiler ensures + that there are no to-be-closed variables here, so this + call cannot change the stack */ + luaF_close(L, base, NOCLOSINGMETH); + lua_assert(base == ci->func + 1); } - else { - /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame */ - CallInfo *oci = nci->previous; /* caller frame */ - 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; - int aux; - /* close all upvalues from previous call */ - 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); - oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ - oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ - 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); - goto newframe; /* restart luaV_execute over new Lua function */ + while (!ttisfunction(s2v(ra))) { /* not a function? */ + luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ + b++; /* there is now one extra argument */ + checkstackGCp(L, 1, ra); } - vmbreak; + if (!ttisLclosure(s2v(ra))) { /* C function? */ + luaD_call(L, ra, LUA_MULTRET); /* call it */ + updatetrap(ci); + updatestack(ci); /* stack may have been relocated */ + ci->func -= delta; + luaD_poscall(L, ci, cast_int(L->top - ra)); + return; + } + ci->func -= delta; + luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ + goto tailcall; } vmcase(OP_RETURN) { - int b = GETARG_B(i); - if (cl->p->sizep > 0) luaF_close(L, base); - 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; - if (b) L->top = ci->top; - lua_assert(isLua(ci)); - lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over new Lua function */ + int n = GETARG_B(i) - 1; /* number of results */ + int nparams1 = GETARG_C(i); + if (n < 0) /* not fixed? */ + n = cast_int(L->top - ra); /* get what is available */ + savepc(ci); + if (TESTARG_k(i)) { /* may there be open upvalues? */ + if (L->top < ci->top) + L->top = ci->top; + luaF_close(L, base, LUA_OK); + updatetrap(ci); + updatestack(ci); } + if (nparams1) /* vararg function? */ + ci->func -= ci->u.l.nextraargs + nparams1; + L->top = ra + n; /* set call for 'luaD_poscall' */ + luaD_poscall(L, ci, n); + return; } - 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 */ - lua_Integer limit = ivalue(ra + 1); - if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - chgivalue(ra, idx); /* update internal index... */ - setivalue(ra + 3, idx); /* ...and external index */ + vmcase(OP_RETURN0) { + if (L->hookmask) { + L->top = ra; + halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */ + } + else { /* do the 'poscall' here */ + int nres = ci->nresults; + L->ci = ci->previous; /* back to caller */ + L->top = base - 1; + while (nres-- > 0) + setnilvalue(s2v(L->top++)); /* all results are nil */ + } + return; + } + vmcase(OP_RETURN1) { + if (L->hookmask) { + L->top = ra + 1; + halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */ + } + else { /* do the 'poscall' here */ + int nres = ci->nresults; + L->ci = ci->previous; /* back to caller */ + if (nres == 0) + L->top = base - 1; /* asked for no results */ + else { + setobjs2s(L, base - 1, ra); /* at least this result */ + L->top = base; + while (--nres > 0) /* complete missing results */ + setnilvalue(s2v(L->top++)); } } - 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 */ - chgfltvalue(ra, idx); /* update internal index... */ - setfltvalue(ra + 3, idx); /* ...and external index */ + return; + } + vmcase(OP_FORLOOP) { + if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ + lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); + if (count > 0) { /* still more iterations? */ + lua_Integer step = ivalue(s2v(ra + 2)); + lua_Integer idx = ivalue(s2v(ra)); /* internal index */ + chgivalue(s2v(ra + 1), count - 1); /* update counter */ + idx = intop(+, idx, step); /* add step to index */ + chgivalue(s2v(ra), idx); /* update internal index */ + setivalue(s2v(ra + 3), idx); /* and control variable */ + pc -= GETARG_Bx(i); /* jump back */ } } + else if (floatforloop(ra)) /* float loop */ + pc -= GETARG_Bx(i); /* jump back */ + updatetrap(ci); /* allows a signal to break the loop */ 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, intop(-, 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)); - } - ci->u.l.savedpc += GETARG_sBx(i); + savestate(L, ci); /* in case of errors */ + if (forprep(L, ra)) + pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } + vmcase(OP_TFORPREP) { + /* create to-be-closed upvalue (if needed) */ + halfProtect(luaF_newtbcupval(L, ra + 3)); + pc += GETARG_Bx(i); + i = *(pc++); /* go to next instruction */ + lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); + goto l_tforcall; + } vmcase(OP_TFORCALL) { - StkId cb = ra + 3; /* call base */ - setobjs2s(L, cb+2, ra+2); - 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))); - L->top = ci->top; - i = *(ci->u.l.savedpc++); /* go to next instruction */ - ra = RA(i); - lua_assert(GET_OPCODE(i) == OP_TFORLOOP); + l_tforcall: + /* 'ra' has the iterator function, 'ra + 1' has the state, + 'ra + 2' has the control variable, and 'ra + 3' has the + to-be-closed variable. The call will use the stack after + these values (starting at 'ra + 4') + */ + /* push function, state, and control variable */ + memcpy(ra + 4, ra, 3 * sizeof(*ra)); + L->top = ra + 4 + 3; + ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ + updatestack(ci); /* stack may have changed */ + i = *(pc++); /* go to next instruction */ + lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; } 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 */ + if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ + setobjs2s(L, ra + 2, ra + 4); /* save control variable */ + pc -= GETARG_Bx(i); /* jump back */ } vmbreak; } vmcase(OP_SETLIST) { int n = GETARG_B(i); - int c = GETARG_C(i); - unsigned int last; - Table *h; - if (n == 0) n = cast_int(L->top - ra) - 1; - if (c == 0) { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - c = GETARG_Ax(*ci->u.l.savedpc++); + unsigned int last = GETARG_C(i); + Table *h = hvalue(s2v(ra)); + if (n == 0) + n = cast_int(L->top - ra) - 1; /* get up to the top */ + else + L->top = ci->top; /* correct top in case of emergency GC */ + last += n; + if (TESTARG_k(i)) { + last += GETARG_Ax(*pc) * (MAXARG_C + 1); + pc++; } - h = hvalue(ra); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > h->sizearray) /* needs more space? */ + if (last > luaH_realasize(h)) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { - TValue *val = ra+n; - luaH_setint(L, h, last--, val); - luaC_barrierback(L, h, val); + TValue *val = s2v(ra + n); + setobj2t(L, &h->array[last - 1], val); + last--; + luaC_barrierback(L, obj2gco(h), val); } - L->top = ci->top; /* correct top (in case of previous open call) */ vmbreak; } vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; - pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ + halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { - int b = GETARG_B(i) - 1; /* required results */ - int j; - int n = cast_int(base - ci->func) - cl->p->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; + int n = GETARG_C(i) - 1; /* required results */ + Protect(luaT_getvarargs(L, ci, ra, n)); + vmbreak; + } + vmcase(OP_VARARGPREP) { + ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); + if (trap) { + luaD_hookcall(L, ci); + L->oldpc = 1; /* next opcode will be seen as a "new" line */ } - 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); + updatebase(ci); /* function has new base after adjustment */ vmbreak; } vmcase(OP_EXTRAARG) { @@ -1309,4 +1810,3 @@ void luaV_execute (lua_State *L) { } /* }================================================================== */ - diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 94b2a62d8..3deb1280e 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lvm.h $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -33,15 +33,40 @@ ** integral values) */ #if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I 0 +#define LUA_FLOORN2I F2Ieq #endif +/* +** Rounding modes for float->integer coercion + */ +typedef enum { + F2Ieq, /* no rounding; accepts only integral values */ + F2Ifloor, /* takes the floor of the number */ + F2Iceil /* takes the ceil of the number */ +} F2Imod; + + +/* convert an object to a float (including string coercion) */ #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) + +/* convert an object to a float (without string coercion) */ +#define tonumberns(o,n) \ + (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ + (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) + + +/* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + + +/* convert an object to an integer (without string coercion) */ +#define tointegerns(o,i) \ + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) + #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) @@ -49,47 +74,41 @@ /* -** 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. +** fast track for 'gettable': if 't' is a table and 't[k]' is present, +** return 1 with 'slot' pointing to 't[k]' (position of final result). +** Otherwise, return 0 (meaning it will have to check metamethod) +** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL +** (otherwise). 'f' is the raw get function to use. */ #define luaV_fastget(L,t,k,slot,f) \ (!ttistable(t) \ ? (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? */ + !isempty(slot))) /* result not empty? */ + +#define luaV_fastset(L,t,k,slot,f) (luaV_fastget(L,t,k,slot,f)) && !isshared(hvalue(t)) /* -** standard implementation for 'gettable' +** Special case of 'luaV_fastget' for integers, inlining the fast case +** of 'luaH_getint'. */ -#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); } +#define luaV_fastgeti(L,t,k,slot) \ + (!ttistable(t) \ + ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ + : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ + ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ + !isempty(slot))) /* result not empty? */ +#define luaV_fastseti(L,t,k,slot) (luaV_fastgeti(L,t,k,slot)) && !isshared(hvalue(t)) /* -** 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. +** Finish a fast set operation (when fast get succeeds). In that case, +** 'slot' points to the place to put the value. */ -#define luaV_fastset(L,t,k,slot,f,v) \ - (!ttistable(t) \ - ? (slot = NULL, 0) \ - : (slot = f(hvalue(t), k), \ - (ttisnil(slot) || isshared(hvalue(t))) ? 0 \ - : (luaC_barrierback(L, hvalue(t), v), \ - setobj2t(L, cast(TValue *,slot), v), \ - 1))) - +#define luaV_finishfastset(L,t,slot,v) \ + { setobj2t(L, cast(TValue *,slot), v); \ + luaC_barrierback(L, gcvalue(t), v); } -#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); } @@ -97,16 +116,20 @@ 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 int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); +LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, + F2Imod mode); +LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); + TValue *val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); -LUAI_FUNC void luaV_execute (lua_State *L); +LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_idiv (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_Number luaV_modf (lua_State *L, lua_Number x, lua_Number 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); diff --git a/3rd/lua/lzio.c b/3rd/lua/lzio.c index 6f7909441..cd0a02d5f 100644 --- a/3rd/lua/lzio.c +++ b/3rd/lua/lzio.c @@ -1,5 +1,5 @@ /* -** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lzio.c $ ** Buffered streams ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/lzio.h b/3rd/lua/lzio.h index d89787081..38f397fd2 100644 --- a/3rd/lua/lzio.h +++ b/3rd/lua/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ +** $Id: lzio.h $ ** Buffered streams ** See Copyright Notice in lua.h */ diff --git a/Makefile b/Makefile index 36cb83bc3..bb9b18e70 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,6 @@ LUA_CLIB_SKYNET = \ lua-mongo.c \ lua-netpack.c \ lua-memory.c \ - lua-profile.c \ lua-multicast.c \ lua-cluster.c \ lua-crypt.c lsha1.c \ diff --git a/README.md b/README.md index fbc6fbcab..ca483c7ed 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,11 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.3.5 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.1 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. -## How To Use (Sorry, currently only available in Chinese) +## How To Use * Read Wiki for documents https://github.com/cloudwu/skynet/wiki * The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ diff --git a/examples/client.lua b/examples/client.lua index 740732f82..d0e5d1cd3 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -1,8 +1,8 @@ package.cpath = "luaclib/?.so" package.path = "lualib/?.lua;examples/?.lua" -if _VERSION ~= "Lua 5.3" then - error "Use lua 5.3" +if _VERSION ~= "Lua 5.4" then + error "Use lua 5.4" end local socket = require "client.socket" diff --git a/examples/login/client.lua b/examples/login/client.lua index 4143f941c..dfb51a894 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -3,8 +3,8 @@ package.cpath = "luaclib/?.so" local socket = require "client.socket" local crypt = require "client.crypt" -if _VERSION ~= "Lua 5.3" then - error "Use lua 5.3" +if _VERSION ~= "Lua 5.4" then + error "Use lua 5.4" end local fd = assert(socket.connect("127.0.0.1", 8001)) diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c deleted file mode 100644 index 9d9b261df..000000000 --- a/lualib-src/lua-profile.c +++ /dev/null @@ -1,269 +0,0 @@ -#define LUA_LIB - -#include -#include -#include - -#include - -#if defined(__APPLE__) -#include -#include -#endif - -#define NANOSEC 1000000000 -#define MICROSEC 1000000 - -// #define DEBUG_LOG - -static double -get_time() { -#if !defined(__APPLE__) - struct timespec ti; - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); - - int sec = ti.tv_sec & 0xffff; - int nsec = ti.tv_nsec; - - return (double)sec + (double)nsec / NANOSEC; -#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; - } - - int sec = aTaskInfo.user_time.seconds & 0xffff; - int msec = aTaskInfo.user_time.microseconds; - - return (double)sec + (double)msec / MICROSEC; -#endif -} - -static inline double -diff_time(double start) { - double now = get_time(); - if (now < start) { - return now + 0x10000 - start; - } else { - return now - start; - } -} - -static int -lstart(lua_State *L) { - 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_pushvalue(L, 1); // push coroutine - lua_pushnumber(L, 0); - lua_rawset(L, lua_upvalueindex(2)); - - lua_pushvalue(L, 1); // push coroutine - 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; -} - -static int -lstop(lua_State *L) { - 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_pushvalue(L, 1); // push coroutine - lua_rawget(L, lua_upvalueindex(2)); - double total_time = lua_tonumber(L, -1); - - lua_pushvalue(L, 1); // push coroutine - lua_pushnil(L); - lua_rawset(L, lua_upvalueindex(1)); - - lua_pushvalue(L, 1); // push coroutine - lua_pushnil(L); - lua_rawset(L, lua_upvalueindex(2)); - - 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); -#endif - - return 1; -} - -static int -timing_resume(lua_State *L) { - lua_pushvalue(L, -1); - lua_rawget(L, lua_upvalueindex(2)); - if (lua_isnil(L, -1)) { // check total time - lua_pop(L,2); // pop from coroutine - } else { - lua_pop(L,1); - double ti = get_time(); -#ifdef DEBUG_LOG - 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 - } - - lua_CFunction co_resume = lua_tocfunction(L, lua_upvalueindex(3)); - - return co_resume(L); -} - -static int -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); // 'from' coroutine rotate to the top(index -1) - - return timing_resume(L); -} - -static int -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,2); - } else { - double ti = lua_tonumber(L, -1); - lua_pop(L,1); - - lua_pushvalue(L, -1); // push coroutine - lua_rawget(L, lua_upvalueindex(1)); - double starttime = lua_tonumber(L, -1); - lua_pop(L,1); - - double diff = diff_time(starttime); - ti += diff; -#ifdef DEBUG_LOG - fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", from, diff, ti); -#endif - - 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)); - - 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); -} - -LUAMOD_API int -luaopen_skynet_profile(lua_State *L) { - luaL_checkversion(L); - luaL_Reg l[] = { - { "start", lstart }, - { "stop", lstop }, - { "resume", lresume }, - { "yield", lyield }, - { "resume_co", lresume_co }, - { "yield_co", lyield_co }, - { NULL, NULL }, - }; - luaL_newlibtable(L,l); - lua_newtable(L); // table thread->start time - lua_newtable(L); // table thread->total time - - lua_newtable(L); // weak table - lua_pushliteral(L, "kv"); - lua_setfield(L, -2, "__mode"); - - lua_pushvalue(L, -1); - lua_setmetatable(L, -3); - lua_setmetatable(L, -3); - - lua_pushnil(L); // cfunction (coroutine.resume or coroutine.yield) - luaL_setfuncs(L,l,3); - - int libtable = lua_gettop(L); - - lua_getglobal(L, "coroutine"); - lua_getfield(L, -1, "resume"); - - lua_CFunction co_resume = lua_tocfunction(L, -1); - 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); - - return 1; -} diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index e94a395c2..ae4f7a383 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -88,9 +88,6 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t case LUA_ERRERR: skynet_error(context, "lua error in error : [%x to %s : %d]", source , self, session); break; - case LUA_ERRGCMM: - skynet_error(context, "lua gc error : [%x to %s : %d]", source , self, session); - break; }; lua_pop(L,1); diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d1e08b212..03e3c83d5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -6,13 +6,14 @@ local assert = assert local pairs = pairs local pcall = pcall local table = table +local next = next local tremove = table.remove local tinsert = table.insert +local tpack = table.pack +local tunpack = table.unpack local traceback = debug.traceback -local profile = require "skynet.profile" - -local cresume = profile.resume +local cresume = coroutine.resume local running_thread = nil local init_thread = nil @@ -20,7 +21,7 @@ local function coroutine_resume(co, ...) running_thread = co return cresume(co, ...) end -local coroutine_yield = profile.yield +local coroutine_yield = coroutine.yield local coroutine_create = coroutine.create local proto = {} @@ -66,10 +67,147 @@ local watching_session = {} local error_queue = {} local fork_queue = {} +do ---- request/select + local function send_requests(self) + local sessions = {} + self._sessions = sessions + local request_n = 0 + local err + for i = 1, #self do + local req = self[i] + local addr = req[1] + local p = proto[req[2]] + assert(p.unpack) + local tag = session_coroutine_tracetag[running_thread] + if tag then + c.trace(tag, "call", 4) + c.send(addr, skynet.PTYPE_TRACE, 0, tag) + end + local session = c.send(addr, p.id , nil , p.pack(tunpack(req, 3, req.n))) + if session == nil then + err = err or {} + err[#err+1] = req + else + sessions[session] = req + watching_session[session] = addr + session_id_coroutine[session] = self._thread + request_n = request_n + 1 + end + end + self._request = request_n + return err + end + + local function request_thread(self) + while true do + local succ, msg, sz, session = coroutine_yield "SUSPEND" + if session == self._timeout then + self._timeout = nil + self.timeout = true + else + watching_session[session] = nil + local req = self._sessions[session] + local p = proto[req[2]] + if succ then + self._resp[session] = tpack( p.unpack(msg, sz) ) + else + self._resp[session] = false + end + end + skynet.wakeup(self) + end + end + + local function request_iter(self) + return function() + if self._error then + -- invalid address + local e = tremove(self._error) + if e then + return e + end + self._error = nil + end + local session, resp = next(self._resp) + if session == nil then + if self._request == 0 then + return + end + if self.timeout then + return + end + skynet.wait(self) + if self.timeout then + return + end + session, resp = next(self._resp) + end + + self._request = self._request - 1 + local req = self._sessions[session] + self._resp[session] = nil + self._sessions[session] = nil + return req, resp + end + end + + local request_meta = {} ; request_meta.__index = request_meta + + function request_meta:add(obj) + assert(type(obj) == "table" and not self._thread) + self[#self+1] = obj + return self + end + + request_meta.__call = request_meta.add + + function request_meta:close() + if self._request > 0 then + local resp = self._resp + for session, req in pairs(self._sessions) do + if not resp[session] then + session_id_coroutine[session] = "BREAK" + watching_session[session] = nil + end + end + self._request = 0 + end + if self._timeout then + session_id_coroutine[self._timeout] = "BREAK" + self._timeout = nil + end + end + + request_meta.__close = request_meta.close + + function request_meta:select(timeout) + assert(self._thread == nil) + self._thread = coroutine_create(request_thread) + self._error = send_requests(self) + self._resp = {} + if timeout then + self._timeout = c.intcommand("TIMEOUT",timeout) + session_id_coroutine[self._timeout] = self._thread + end + + local running = running_thread + coroutine_resume(self._thread, self) + running_thread = running + return request_iter(self), nil, nil, self + end + + function skynet.request(obj) + local ret = setmetatable({}, request_meta) + if obj then + return ret(obj) + end + return ret + end +end + -- suspend is function local suspend - ----- monitor exit local function dispatch_error_queue() @@ -77,7 +215,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, nil, nil, session)) end end @@ -151,17 +289,22 @@ local function co_create(f) end local function dispatch_wakeup() - local token = tremove(wakeup_queue,1) - if token then - local session = sleep_session[token] - if session then - local co = session_id_coroutine[session] - local tag = session_coroutine_tracetag[co] - if tag then c.trace(tag, "resume") end - session_id_coroutine[session] = "BREAK" - return suspend(co, coroutine_resume(co, false, "BREAK")) + while true do + local token = tremove(wakeup_queue,1) + if token then + local session = sleep_session[token] + if session then + local co = session_id_coroutine[session] + local tag = session_coroutine_tracetag[co] + if tag then c.trace(tag, "resume") end + session_id_coroutine[session] = "BREAK" + return suspend(co, coroutine_resume(co, false, "BREAK", nil, session)) + end + else + break end end + return dispatch_error_queue() end -- suspend is local function @@ -184,8 +327,7 @@ function suspend(co, result, command) error(traceback(co,tostring(command))) end if command == "SUSPEND" then - dispatch_wakeup() - dispatch_error_queue() + return dispatch_wakeup() elseif command == "QUIT" then -- service exit return @@ -572,7 +714,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) local tag = session_coroutine_tracetag[co] if tag then c.trace(tag, "resume") end session_id_coroutine[session] = nil - suspend(co, coroutine_resume(co, true, msg, sz)) + suspend(co, coroutine_resume(co, true, msg, sz, session)) end else local p = proto[prototype] diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index d926ae823..54ac499c6 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -24,24 +24,17 @@ function skynetco.create(f) end do -- begin skynetco.resume - - local profile = require "skynet.profile" - -- 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, from, ...) + local function skynet_yielding(co, ...) skynet_coroutines[co] = false - return unlock(co, skynet_resume(co, from, skynet_yield(from, ...))) + return unlock(co, coroutine_resume(co, coroutine_yield(...))) end - local function resume(co, from, ok, ...) + local function resume(co, ok, ...) if not ok then return ok, ... elseif coroutine_status(co) == "dead" then @@ -52,41 +45,41 @@ do -- begin skynetco.resume return true, select(2, ...) else -- blocked in skynet framework, so raise the yielding message - return resume(co, from, skynet_yielding(co, from, ...)) + return resume(co, skynet_yielding(co, ...)) 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 - 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" + 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 + local from = coroutine_running() + local caller = coroutine_caller[from] or from + coroutine_caller[co] = caller + return resume(co, coroutine_resume(co, ...)) end - 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 + 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 -- end of skynetco.resume diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index afac92f73..59eb02600 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -4,7 +4,7 @@ local table = require "table" local packbytes local packvalue -if _VERSION == "Lua 5.3" then +if _VERSION == "Lua 5.4" then function packbytes(str) return string.pack(" #include #include +#include + +#if defined(__APPLE__) +#include +#include +#endif + +#define NANOSEC 1000000000 +#define MICROSEC 1000000 + +// #define DEBUG_LOG #define MEMORY_WARNING_REPORT (1024 * 1024 * 32) @@ -17,6 +28,8 @@ struct snlua { size_t mem; size_t mem_report; size_t mem_limit; + lua_State * activeL; + volatile int trap; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -46,6 +59,295 @@ codecache(lua_State *L) { #endif +static void +signal_hook(lua_State *L, lua_Debug *ar) { + void *ud = NULL; + lua_getallocf(L, &ud); + struct snlua *l = (struct snlua *)ud; + + lua_sethook (L, NULL, 0, 0); + if (l->trap) { + l->trap = 0; + luaL_error(L, "signal 0"); + } +} + +static void +switchL(lua_State *L, struct snlua *l) { + l->activeL = L; + if (l->trap) { + lua_sethook(L, signal_hook, LUA_MASKCOUNT, 1); + } +} + +static int +lua_resumeX(lua_State *L, lua_State *from, int nargs, int *nresults) { + void *ud = NULL; + lua_getallocf(L, &ud); + struct snlua *l = (struct snlua *)ud; + switchL(L, l); + int err = lua_resume(L, from, nargs, nresults); + switchL(from, l); + return err; +} + +static double +get_time() { +#if !defined(__APPLE__) + struct timespec ti; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); + + int sec = ti.tv_sec & 0xffff; + int nsec = ti.tv_nsec; + + return (double)sec + (double)nsec / NANOSEC; +#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; + } + + int sec = aTaskInfo.user_time.seconds & 0xffff; + int msec = aTaskInfo.user_time.microseconds; + + return (double)sec + (double)msec / MICROSEC; +#endif +} + +static inline double +diff_time(double start) { + double now = get_time(); + if (now < start) { + return now + 0x10000 - start; + } else { + return now - start; + } +} + +// coroutine lib, add profile + +/* +** Resumes a coroutine. Returns the number of results for non-error +** cases or -1 for errors. +*/ +static int auxresume (lua_State *L, lua_State *co, int narg) { + int status, nres; + if (!lua_checkstack(co, narg)) { + lua_pushliteral(L, "too many arguments to resume"); + return -1; /* error flag */ + } + lua_xmove(L, co, narg); + status = lua_resumeX(co, L, narg, &nres); + if (status == LUA_OK || status == LUA_YIELD) { + if (!lua_checkstack(L, nres + 1)) { + lua_pop(co, nres); /* remove results anyway */ + lua_pushliteral(L, "too many results to resume"); + return -1; /* error flag */ + } + lua_xmove(co, L, nres); /* move yielded values */ + return nres; + } + else { + lua_xmove(co, L, 1); /* move error message */ + return -1; /* error flag */ + } +} + +static int +timing_enable(lua_State *L, int co_index, lua_Number *start_time) { + lua_pushvalue(L, co_index); + lua_rawget(L, lua_upvalueindex(1)); + if (lua_isnil(L, -1)) { // check total time + lua_pop(L, 1); + return 0; + } + *start_time = lua_tonumber(L, -1); + lua_pop(L,1); + return 1; +} + +static double +timing_total(lua_State *L, int co_index) { + lua_pushvalue(L, co_index); + lua_rawget(L, lua_upvalueindex(2)); + double total_time = lua_tonumber(L, -1); + lua_pop(L,1); + return total_time; +} + +static int +timing_resume(lua_State *L, int co_index, int n) { + lua_State *co = lua_tothread(L, co_index); + lua_Number start_time = 0; + if (timing_enable(L, co_index, &start_time)) { + start_time = get_time(); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] resume %lf\n", co, ti); +#endif + lua_pushvalue(L, co_index); + lua_pushnumber(L, start_time); + lua_rawset(L, lua_upvalueindex(1)); // set start time + } + + int r = auxresume(L, co, lua_gettop(L) - 1); + + if (timing_enable(L, co_index, &start_time)) { + double total_time = timing_total(L, co_index); + double diff = diff_time(start_time); + total_time += diff; +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", co, diff, total_time); +#endif + lua_pushvalue(L, co_index); + lua_pushnumber(L, total_time); + lua_rawset(L, lua_upvalueindex(2)); + } + + return r; +} + +static int luaB_coresume (lua_State *L) { + luaL_checktype(L, 1, LUA_TTHREAD); + int r = timing_resume(L, 1, lua_gettop(L) - 1); + if (r < 0) { + lua_pushboolean(L, 0); + lua_insert(L, -2); + return 2; /* return false + error message */ + } + else { + lua_pushboolean(L, 1); + lua_insert(L, -(r + 1)); + return r + 1; /* return true + 'resume' returns */ + } +} + +static int luaB_auxwrap (lua_State *L) { + lua_State *co = lua_tothread(L, lua_upvalueindex(3)); + int r = timing_resume(L, lua_upvalueindex(3), lua_gettop(L)); + if (r < 0) { + int stat = lua_status(co); + if (stat != LUA_OK && stat != LUA_YIELD) + lua_resetthread(co); /* close variables in case of errors */ + if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ + luaL_where(L, 1); /* add extra info, if available */ + lua_insert(L, -2); + lua_concat(L, 2); + } + return lua_error(L); /* propagate error */ + } + return r; +} + +static int luaB_cocreate (lua_State *L) { + lua_State *NL; + luaL_checktype(L, 1, LUA_TFUNCTION); + NL = lua_newthread(L); + lua_pushvalue(L, 1); /* move function to top */ + lua_xmove(L, NL, 1); /* move function from L to NL */ + return 1; +} + +static int luaB_cowrap (lua_State *L) { + lua_pushvalue(L, lua_upvalueindex(1)); + lua_pushvalue(L, lua_upvalueindex(2)); + luaB_cocreate(L); + lua_pushcclosure(L, luaB_auxwrap, 3); + return 1; +} + +// profile lib + +static int +lstart(lua_State *L) { + if (lua_gettop(L) != 0) { + lua_settop(L,1); + luaL_checktype(L, 1, LUA_TTHREAD); + } else { + lua_pushthread(L); + } + lua_Number start_time = 0; + if (timing_enable(L, 1, &start_time)) { + return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1)); + } + + // reset total time + lua_pushvalue(L, 1); + lua_pushnumber(L, 0); + lua_rawset(L, lua_upvalueindex(2)); + + // set start time + lua_pushvalue(L, 1); + start_time = get_time(); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] start\n", L); +#endif + lua_pushnumber(L, start_time); + lua_rawset(L, lua_upvalueindex(1)); + + return 0; +} + +static int +lstop(lua_State *L) { + if (lua_gettop(L) != 0) { + lua_settop(L,1); + luaL_checktype(L, 1, LUA_TTHREAD); + } else { + lua_pushthread(L); + } + lua_Number start_time = 0; + if (!timing_enable(L, 1, &start_time)) { + return luaL_error(L, "Call profile.start() before profile.stop()"); + } + double ti = diff_time(start_time); + double total_time = timing_total(L,1); + + lua_pushvalue(L, 1); // push coroutine + lua_pushnil(L); + lua_rawset(L, lua_upvalueindex(1)); + + lua_pushvalue(L, 1); // push coroutine + lua_pushnil(L); + lua_rawset(L, lua_upvalueindex(2)); + + 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); +#endif + + return 1; +} + +static int +init_profile(lua_State *L) { + luaL_Reg l[] = { + { "start", lstart }, + { "stop", lstop }, + { "resume", luaB_coresume }, + { "wrap", luaB_cowrap }, + { NULL, NULL }, + }; + luaL_newlibtable(L,l); + lua_newtable(L); // table thread->start time + lua_newtable(L); // table thread->total time + + lua_newtable(L); // weak table + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); + + lua_pushvalue(L, -1); + lua_setmetatable(L, -3); + lua_setmetatable(L, -3); + + luaL_setfuncs(L,l,2); + + return 1; +} + +/// end of coroutine + static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); @@ -80,11 +382,25 @@ init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t s lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); luaL_openlibs(L); + luaL_requiref(L, "skynet.profile", init_profile, 0); + + int profile_lib = lua_gettop(L); + // replace coroutine.resume / coroutine.wrap + lua_getglobal(L, "coroutine"); + lua_getfield(L, profile_lib, "resume"); + lua_setfield(L, -2, "resume"); + lua_getfield(L, profile_lib, "wrap"); + lua_setfield(L, -2, "wrap"); + + lua_settop(L, profile_lib-1); + lua_pushlightuserdata(L, ctx); lua_setfield(L, LUA_REGISTRYINDEX, "skynet_context"); luaL_requiref(L, "skynet.codecache", codecache , 0); lua_pop(L,1); + lua_gc(L, LUA_GCGEN, 0, 0); + const char *path = optstring(ctx, "lua_path","./lualib/?.lua;./lualib/?/init.lua"); lua_pushstring(L, path); lua_setglobal(L, "LUA_PATH"); @@ -184,6 +500,8 @@ snlua_create(void) { l->mem_report = MEMORY_WARNING_REPORT; l->mem_limit = 0; l->L = lua_newstate(lalloc, l); + l->activeL = NULL; + l->trap = 0; return l; } @@ -197,10 +515,10 @@ 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 + if (l->trap == 0) { + l->trap = 1; + lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); + } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); } diff --git a/service/debug_console.lua b/service/debug_console.lua index 35c4f1d08..e8b26f688 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -11,6 +11,7 @@ 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 TIMEOUT = 300 -- 3 sec local COMMAND = {} local COMMANDX = {} @@ -224,20 +225,32 @@ function COMMAND.list() return skynet.call(".launcher", "lua", "LIST") end -function COMMAND.stat() - return skynet.call(".launcher", "lua", "STAT") +local function timeout(ti) + if ti then + ti = tonumber(ti) + if ti <= 0 then + ti = nil + end + else + ti = TIMEOUT + end + return ti +end + +function COMMAND.stat(ti) + return skynet.call(".launcher", "lua", "STAT", timeout(ti)) end -function COMMAND.mem() - return skynet.call(".launcher", "lua", "MEM") +function COMMAND.mem(ti) + return skynet.call(".launcher", "lua", "MEM", timeout(ti)) end function COMMAND.kill(address) return skynet.call(".launcher", "lua", "KILL", address) end -function COMMAND.gc() - return skynet.call(".launcher", "lua", "GC") +function COMMAND.gc(ti) + return skynet.call(".launcher", "lua", "GC", timeout(ti)) end function COMMAND.exit(address) diff --git a/service/launcher.lua b/service/launcher.lua index 77300a23f..655458800 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -22,18 +22,35 @@ function command.LIST() return list end -function command.STAT() +local function list_srv(ti, fmt_func, ...) local list = {} - for k,v in pairs(services) do - local ok, stat = pcall(skynet.call,k,"debug","STAT") - if not ok then - stat = string.format("ERROR (%s)",v) + local sessions = {} + local req = skynet.request() + for addr in pairs(services) do + local r = { addr, "debug", ... } + req:add(r) + sessions[r] = addr + end + for resp, req in req:select(ti) do + local stat = resp[1] + local addr = req[1] + if resp.ok then + list[skynet.address(addr)] = fmt_func(stat, addr) + else + list[skynet.address(addr)] = fmt_func("ERROR", addr) end - list[skynet.address(k)] = stat + sessions[req] = nil + end + for session, addr in pairs(sessions) do + list[skynet.address(addr)] = fmt_func("TIMEOUT", addr) end return list end +function command.STAT(addr, ti) + return list_srv(ti, function(v) return v end, "STAT") +end + function command.KILL(_, handle) handle = handle_to_address(handle) skynet.kill(handle) @@ -42,24 +59,22 @@ function command.KILL(_, handle) return ret end -function command.MEM() - local list = {} - for k,v in pairs(services) do - local ok, kb = pcall(skynet.call,k,"debug","MEM") - if not ok then - list[skynet.address(k)] = string.format("ERROR (%s)",v) +function command.MEM(addr, ti) + return list_srv(ti, function(kb, addr) + local v = services[addr] + if kb == "TIMEOUT" then + return string.format("TIMEOUT (%s)",v) else - list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) + return string.format("%.2f Kb (%s)",kb,v) end - end - return list + end, "MEM") end -function command.GC() +function command.GC(addr, ti) for k,v in pairs(services) do skynet.send(k,"debug","GC") end - return command.MEM() + return command.MEM(ti) end function command.REMOVE(_, handle, kill) diff --git a/test/testselect.lua b/test/testselect.lua new file mode 100644 index 000000000..4372328bb --- /dev/null +++ b/test/testselect.lua @@ -0,0 +1,94 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "slave" then + +local COMMAND = {} + +function COMMAND.ping(ti, str) + skynet.sleep(ti) + return str +end + +function COMMAND.error() + error "ERROR" +end + +function COMMAND.exit() + skynet.exit() +end + +skynet.start(function() + skynet.dispatch("lua", function(_,_, cmd, ...) + skynet.ret(skynet.pack(COMMAND[cmd](...))) + end) +end) + +else + +local function info(fmt, ...) + skynet.error(string.format(fmt, ...)) +end + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + + for req, resp in skynet.request + { slave, "lua", "ping", 6, "SLEEP 6" } + { slave, "lua", "ping", 5, "SLEEP 5" } + { slave, "lua", "ping", 4, "SLEEP 4" } + { slave, "lua", "ping", 3, "SLEEP 3" } + { slave, "lua", "ping", 2, "SLEEP 2" } + { slave, "lua", "ping", 1, "SLEEP 1" } + :select() do + info("RESP %s", resp[1]) + end + + -- test timeout + local reqs = skynet.request() + + for i = 1, 10 do + reqs:add { slave, "lua", "ping", i*10, "SLEEP " .. i, token = i } + end + + for req, resp in reqs:select(50) do + info("RESP %s token<%s>", resp[1], req.token) + end + + -- test error + + for req, resp in skynet.request + { slave, "lua", "error" } + { slave, "lua", "ping", 0, "PING" } + : select() do + if resp then + info("Ping : %s", resp[1]) + else + info("Error") + end + end + + -- timeout call + + local reqs = skynet.request { slave, "lua", "ping", 100 , "PING" } + for req, resp in reqs:select(10) do + info("%s", resp[1]) + end + + info("Timeout : %s", reqs.timeout) + + -- call in select + for req, resp in skynet.request + { slave, "lua", "ping", 20, "CALL 20" } + { slave, "lua", "ping", 10, "CALL 10" } + : select() do + info("%s", skynet.call( slave, "lua", "ping", 0, "ping in " .. resp[1]) ) + skynet.sleep(50) + end + + skynet.send(slave, "lua", "exit") + skynet.exit() +end) + +end From 4484c1ed50d8baf39b6559296a809430d0dd525f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 10 Oct 2020 13:55:15 +0800 Subject: [PATCH 253/565] Add map for sproto --- lualib-src/sproto/README.md | 10 ++- lualib-src/sproto/lsproto.c | 109 +++++++++++++++++++------ lualib-src/sproto/sproto.c | 154 +++++++++++++++++++++++------------- lualib-src/sproto/sproto.h | 9 ++- lualib/sprotoparser.lua | 57 ++++++++++--- 5 files changed, 245 insertions(+), 94 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index 237e4b5c2..e5011fc90 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -165,6 +165,7 @@ 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 + weight 6 : double # floating number } .AddressBook { @@ -216,11 +217,14 @@ Types * **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. +* **double** : double, floating-point number. * **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. +You can also specify a main index with the syntax likes `*array(id)`, the array would be encode as an unordered map with the `id` field as key. + +For empty main index likes `*array()`, the array would be encoded as an unordered map with the first field as key and the second field as value. User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported. @@ -228,6 +232,8 @@ User defined type can be any name in alphanumeric characters except the build-in 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. +**NOTE** : `double` is supported now. + * Where is enum? In lua, enum types are not very useful. You can use integer to define an enum table in lua. diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 214ec8df5..370ef719d 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -128,6 +128,7 @@ struct encode_ud { const char * array_tag; int array_index; int deep; + int map_entry; int iter_func; int iter_table; int iter_key; @@ -153,13 +154,11 @@ next_list(lua_State *L, struct encode_ud * self) { } static int -encode(const struct sproto_arg *args) { +get_encodefield(const struct sproto_arg *args) { struct encode_ud *self = args->ud; lua_State *L = self->L; - luaL_checkstack(L, 12, NULL); - if (self->deep >= ENCODE_DEEPLEVEL) - return luaL_error(L, "The table is too deep"); if (args->index > 0) { + int map = args->ktagname != NULL; if (args->tagname != self->array_tag) { // a new array self->array_tag = args->tagname; @@ -177,6 +176,13 @@ encode(const struct sproto_arg *args) { self->array_index = lua_gettop(L); } + if (map) { + if (!self->map_entry) { + lua_createtable(L, 0, 2); // key/value entry + self->map_entry = lua_gettop(L); + } + } + if (luaL_getmetafield(L, self->array_index, "__pairs")) { lua_pushvalue(L, self->array_index); lua_call(L, 1, 3); @@ -194,26 +200,39 @@ encode(const struct sproto_arg *args) { self->iter_key = lua_gettop(L); } } - if (args->mainindex >= 0) { + if (args->mainindex >= 0) { // *type(mainindex) if (!next_list(L, self)) { // iterate end lua_pushnil(L); lua_replace(L, self->iter_key); return SPROTO_CB_NIL; } - lua_insert(L, -2); - lua_replace(L, self->iter_key); + if (map) { + lua_pushvalue(L, -2); + lua_replace(L, self->iter_key); + lua_setfield(L, self->map_entry, args->vtagname); + lua_setfield(L, self->map_entry, args->ktagname); + lua_pushvalue(L, self->map_entry); + } else { + lua_insert(L, -2); + lua_replace(L, self->iter_key); + } } else { lua_geti(L, self->array_index, args->index); } } else { lua_getfield(L, self->tbl_index, args->tagname); } - if (lua_isnil(L, -1)) { - lua_pop(L,1); - return SPROTO_CB_NIL; - } - switch (args->type) { + return 0; +} + +static int encode(const struct sproto_arg *args); + +static int +encode_one(const struct sproto_arg *args, struct encode_ud *self) { + lua_State *L = self->L; + int type = args->type; + switch (type) { case SPROTO_TINTEGER: { int64_t v; lua_Integer vh; @@ -282,6 +301,7 @@ encode(const struct sproto_arg *args) { sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; + sub.map_entry = 0; sub.iter_func = 0; sub.iter_table = 0; sub.iter_key = 0; @@ -296,6 +316,25 @@ encode(const struct sproto_arg *args) { } } +static int +encode(const struct sproto_arg *args) { + struct encode_ud *self = args->ud; + lua_State *L = self->L; + int code; + luaL_checkstack(L, 12, NULL); + if (self->deep >= ENCODE_DEEPLEVEL) + return luaL_error(L, "The table is too deep"); + code = get_encodefield(args); + if (code < 0) { + return code; + } + if (lua_isnil(L, -1)) { + lua_pop(L,1); + return SPROTO_CB_NIL; + } + return encode_one(args, self); +} + static void * expand_buffer(lua_State *L, int osz, int nsz) { void *output; @@ -342,6 +381,7 @@ lencode(lua_State *L) { self.deep = 0; lua_settop(L, tbl_index); + self.map_entry = 0; self.iter_func = 0; self.iter_table = 0; self.iter_key = 0; @@ -365,6 +405,7 @@ struct decode_ud { int deep; int mainindex_tag; int key_index; + int map_entry; }; static int @@ -422,14 +463,24 @@ decode(const struct sproto_arg *args) { break; } case SPROTO_TSTRUCT: { + int map = args->ktagname != NULL; struct decode_ud sub; int r; - lua_newtable(L); sub.L = L; - sub.result_index = lua_gettop(L); + if (map) { + if (!self->map_entry) { + lua_newtable(L); + self->map_entry = lua_gettop(L); + } + sub.result_index = self->map_entry; + } else { + lua_newtable(L); + sub.result_index = lua_gettop(L); + } sub.deep = self->deep + 1; sub.array_index = 0; sub.array_tag = NULL; + sub.map_entry = 0; if (args->mainindex >= 0) { // This struct will set into a map, so mark the main index tag. sub.mainindex_tag = args->mainindex; @@ -441,13 +492,26 @@ decode(const struct sproto_arg *args) { return SPROTO_CB_ERROR; if (r != args->length) return r; - 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); + if (map) { + lua_getfield(L, sub.result_index, args->ktagname); + if (lua_isnil(L, -1)) { + luaL_error(L, "Can't find key field in [%s]", args->tagname); + } + lua_getfield(L, sub.result_index, args->vtagname); + if (lua_isnil(L, -1)) { + luaL_error(L, "Can't find value field in [%s]", args->tagname); + } + lua_settable(L, self->array_index); + lua_settop(L, sub.result_index); + } else { + 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_settable(L, self->array_index); + lua_settop(L, sub.result_index-1); } - lua_pushvalue(L, sub.result_index); - lua_settable(L, self->array_index); - lua_settop(L, sub.result_index-1); return 0; } else { sub.mainindex_tag = -1; @@ -524,6 +588,7 @@ ldecode(lua_State *L) { self.deep = 0; self.mainindex_tag = -1; self.key_index = 0; + self.map_entry = 0; r = sproto_decode(st, buffer, (int)sz, decode, &self); if (r < 0) { return luaL_error(L, "decode error"); @@ -679,7 +744,7 @@ lloadproto(lua_State *L) { } static void -push_default(const struct sproto_arg *args, int array) { +push_default(const struct sproto_arg *args, int table) { lua_State *L = args->ud; switch(args->type) { case SPROTO_TINTEGER: @@ -698,7 +763,7 @@ push_default(const struct sproto_arg *args, int array) { lua_pushliteral(L, ""); break; case SPROTO_TSTRUCT: - if (array) { + if (table) { lua_pushstring(L, sproto_name(args->subtype)); } else { lua_createtable(L, 0, 1); diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 62c336e97..0b7f41324 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -6,7 +6,6 @@ #include "sproto.h" -#define SPROTO_TARRAY 0x80 #define CHUNK_SIZE 1000 #define SIZEOF_LENGTH 4 #define SIZEOF_HEADER 2 @@ -20,6 +19,7 @@ struct field { const char * name; struct sproto_type * st; int key; + int map; // interpreted two fields struct as map int extra; }; @@ -206,6 +206,7 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { f->name = NULL; f->st = NULL; f->key = -1; + f->map = -1; f->extra = 0; sz = todword(stream); @@ -262,6 +263,10 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { case 5: // key f->key = value; break; + case 6: // map + if (value) + f->map = 1; + break; default: return NULL; } @@ -281,6 +286,8 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { type 2 : integer tag 3 : integer array 4 : boolean + key 5 : integer + map 6 : boolean // Interpreted two fields struct as map when decoding } name 0 : string fields 1 : *field @@ -487,6 +494,32 @@ sproto_release(struct sproto * s) { pool_release(&s->memory); } +static const char * +get_typename(int type, struct field *f) { + if (type == SPROTO_TSTRUCT) { + return f->st->name; + } else { + switch (type) { + case SPROTO_TINTEGER: + if (f->extra) + return "decimal"; + else + return "integer"; + case SPROTO_TBOOLEAN: + return "boolean"; + case SPROTO_TSTRING: + if (f->extra == SPROTO_TSTRING_BINARY) + return "binary"; + else + return "string"; + case SPROTO_TDOUBLE: + return "double"; + default: + return "invalid"; + } + } +} + void sproto_dump(struct sproto *s) { int i,j; @@ -495,50 +528,30 @@ sproto_dump(struct sproto *s) { struct sproto_type *t = &s->type[i]; printf("%s\n", t->name); for (j=0;jn;j++) { - char array[2] = { 0, 0 }; - const char * type_name = NULL; + char container[2] = { 0, 0 }; + const char * typename = NULL; struct field *f = &t->f[j]; int type = f->type & ~SPROTO_TARRAY; if (f->type & SPROTO_TARRAY) { - array[0] = '*'; + container[0] = '*'; } else { - array[0] = 0; + container[0] = 0; } - if (type == SPROTO_TSTRUCT) { - type_name = f->st->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; - } - } - printf("\t%s (%d) %s%s", f->name, f->tag, array, type_name); + typename = get_typename(type, f); + printf("\t%s (%d) %s%s", f->name, f->tag, container, typename); if (type == SPROTO_TINTEGER && f->extra > 0) { printf("(%d)", f->extra); } if (f->key >= 0) { - printf("[%d]", f->key); + printf(" key[%d]", f->key); + if (f->map >= 0) { + printf(" value[%d]", f->st->f[1].tag); + } } printf("\n"); } } + printf("=== %d protocol ===\n", s->protocol_n); for (i=0;iprotocol_n;i++) { struct protocol *p = &s->proto[i]; @@ -840,6 +853,36 @@ encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffe return buffer; } +static uint8_t * +encode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) { + int sz; + *noarray = 0; + args->index = 1; + for (;;) { + if (size < SIZEOF_LENGTH) + return NULL; + size -= SIZEOF_LENGTH; + args->value = buffer + SIZEOF_LENGTH; + args->length = size; + sz = cb(args); + if (sz < 0) { + if (sz == SPROTO_CB_NIL) { + break; + } + if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it + *noarray = 1; + break; + } + return NULL; // sz == SPROTO_CB_ERROR + } + fill_size(buffer, sz); + buffer += SIZEOF_LENGTH+sz; + size -= sz; + ++args->index; + } + return buffer; +} + static int encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { uint8_t * buffer; @@ -883,30 +926,16 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz ++args->index; } break; - default: - args->index = 1; - for (;;) { - if (size < SIZEOF_LENGTH) - return -1; - size -= SIZEOF_LENGTH; - args->value = buffer+SIZEOF_LENGTH; - args->length = size; - sz = cb(args); - 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; - size -=sz; - ++args->index; - } + default: { + int noarray; + buffer = encode_array_object(cb, args, buffer, size, &noarray); + if (buffer == NULL) + return -1; + if (noarray) + return 0; break; } + } sz = buffer - (data + SIZEOF_LENGTH); return fill_size(data, sz); } @@ -938,8 +967,14 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.subtype = f->st; args.mainindex = f->key; args.extra = f->extra; + args.ktagname = NULL; + args.vtagname = NULL; if (type & SPROTO_TARRAY) { - args.type = type & ~SPROTO_TARRAY; + args.type = type & (~SPROTO_TARRAY); + if (f->map > 0) { + args.ktagname = f->st->f[0].name; + args.vtagname = f->st->f[1].name; + } sz = encode_array(cb, &args, data, size); } else { args.type = type; @@ -1168,13 +1203,20 @@ sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_ continue; args.tagname = f->name; args.tagid = f->tag; - args.type = f->type & ~SPROTO_TARRAY; + args.type = f->type; args.subtype = f->st; args.index = 0; args.mainindex = f->key; args.extra = f->extra; + args.ktagname = NULL; + args.vtagname = NULL; if (value < 0) { if (f->type & SPROTO_TARRAY) { + args.type = f->type & (~SPROTO_TARRAY); + if (f->map > 0) { + args.ktagname = f->st->f[0].name; + args.vtagname = f->st->f[1].name; + } if (decode_array(cb, &args, currentdata)) { return -1; } diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index 0ee5255b3..4ff4952d8 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -16,6 +16,9 @@ struct sproto_type; #define SPROTO_TDOUBLE 3 #define SPROTO_TSTRUCT 4 +// container type +#define SPROTO_TARRAY 0x80 + // sub type of string (sproto_arg.extra) #define SPROTO_TSTRING_STRING 0 #define SPROTO_TSTRING_BINARY 1 @@ -46,9 +49,13 @@ struct sproto_arg { struct sproto_type *subtype; void *value; int length; - int index; // array base 1 + int index; // array base 1, negative value indicates that it is a empty array int mainindex; // for map int extra; // SPROTO_TINTEGER: decimal ; SPROTO_TSTRING 0:utf8 string 1:binary + + // When interpretd two fields struct as map, the following fields must not be NULL. + const char *ktagname; + const char *vtagname; }; typedef int (*sproto_callback)(const struct sproto_arg *args); diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 59eb02600..4335af8e2 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -4,7 +4,7 @@ local table = require "table" local packbytes local packvalue -if _VERSION == "Lua 5.4" then +if _VERSION == "Lua 5.3" then function packbytes(str) return string.pack(" Date: Sat, 10 Oct 2020 14:06:57 +0800 Subject: [PATCH 254/565] Update history --- HISTORY.md | 12 ++++++++++++ lualib/sprotoparser.lua | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 195f0e96a..d79086d1b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,15 @@ +v1.4.0 (2020-10-10) +----------- +* Update Lua to 5.4.1 +* Add skynet.select +* Improve mysql driver (@zero-rp @xiaojin @yxt945) +* Improve websocket and ssl (@lvzixun) +* Improve sproto (double @lvzixun map @t0350) +* Add padding mode PKCS7 for DES +* Add jmem in debug console +* Add skynet_socket_pause for net traffic control +* Add timestamp to default logger + v1.3.0 (2019-11-19) ----------- * Improve mysql driver (@yxt945) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 4335af8e2..cfa25afae 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -4,7 +4,9 @@ local table = require "table" local packbytes local packvalue -if _VERSION == "Lua 5.3" then +local version = _VERSION:match "5.*" + +if version and tonumber(version) >= 5.3 then function packbytes(str) return string.pack(" Date: Sat, 10 Oct 2020 17:01:13 +0800 Subject: [PATCH 255/565] remove global buffer_pool --- lualib/skynet/socket.lua | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index dd2db507a..e85f8d413 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -5,13 +5,11 @@ local assert = assert local BUFFER_LIMIT = 128 * 1024 local socket = {} -- api -local buffer_pool = {} -- store all message buffer object local socket_pool = setmetatable( -- store all socket object {}, { __gc = function(p) for id,v in pairs(p) do driver.close(id) - -- don't need clear v.buffer, because buffer pool will be free at the end p[id] = nil end end @@ -53,7 +51,7 @@ socket_message[1] = function(id, size, data) return end - local sz = driver.push(s.buffer, buffer_pool, data, size) + local sz = driver.push(s.buffer, s.pool, data, size) local rr = s.read_required local rrt = type(rr) if rrt == "number" then @@ -69,7 +67,6 @@ socket_message[1] = function(id, size, data) else if s.buffer_limit and sz > s.buffer_limit then skynet.error(string.format("socket buffer overflow: fd=%d size=%d", id , sz)) - driver.clear(s.buffer,buffer_pool) driver.close(id) return end @@ -192,6 +189,7 @@ local function connect(id, func) local s = { id = id, buffer = newbuffer, + pool = newbuffer and {}, connected = false, connecting = true, read_required = false, @@ -234,7 +232,6 @@ end function socket.shutdown(id) local s = socket_pool[id] if s then - driver.clear(s.buffer,buffer_pool) -- the framework would send SKYNET_SOCKET_TYPE_CLOSE , need close(id) later driver.shutdown(id) end @@ -252,8 +249,6 @@ function socket.close(id) end if s.connected then 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 -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. @@ -265,7 +260,6 @@ function socket.close(id) end s.connected = false end - driver.clear(s.buffer,buffer_pool) assert(s.lock == nil or next(s.lock) == nil) socket_pool[id] = nil end @@ -275,7 +269,7 @@ function socket.read(id, sz) assert(s) if sz == nil then -- read some bytes - local ret = driver.readall(s.buffer, buffer_pool) + local ret = driver.readall(s.buffer, s.pool) if ret ~= "" then return ret end @@ -286,7 +280,7 @@ function socket.read(id, sz) assert(not s.read_required) s.read_required = 0 suspend(s) - ret = driver.readall(s.buffer, buffer_pool) + ret = driver.readall(s.buffer, s.pool) if ret ~= "" then return ret else @@ -294,22 +288,22 @@ function socket.read(id, sz) end end - local ret = driver.pop(s.buffer, buffer_pool, sz) + local ret = driver.pop(s.buffer, s.pool, sz) if ret then return ret end if not s.connected then - return false, driver.readall(s.buffer, buffer_pool) + return false, driver.readall(s.buffer, s.pool) end assert(not s.read_required) s.read_required = sz suspend(s) - ret = driver.pop(s.buffer, buffer_pool, sz) + ret = driver.pop(s.buffer, s.pool, sz) if ret then return ret else - return false, driver.readall(s.buffer, buffer_pool) + return false, driver.readall(s.buffer, s.pool) end end @@ -317,34 +311,34 @@ function socket.readall(id) local s = socket_pool[id] assert(s) if not s.connected then - local r = driver.readall(s.buffer, buffer_pool) + local r = driver.readall(s.buffer, s.pool) return r ~= "" and r end assert(not s.read_required) s.read_required = true suspend(s) assert(s.connected == false) - return driver.readall(s.buffer, buffer_pool) + return driver.readall(s.buffer, s.pool) end function socket.readline(id, sep) sep = sep or "\n" local s = socket_pool[id] assert(s) - local ret = driver.readline(s.buffer, buffer_pool, sep) + local ret = driver.readline(s.buffer, s.pool, sep) if ret then return ret end if not s.connected then - return false, driver.readall(s.buffer, buffer_pool) + return false, driver.readall(s.buffer, s.pool) end assert(not s.read_required) s.read_required = sep suspend(s) if s.connected then - return driver.readline(s.buffer, buffer_pool, sep) + return driver.readline(s.buffer, s.pool, sep) else - return false, driver.readall(s.buffer, buffer_pool) + return false, driver.readall(s.buffer, s.pool) end end @@ -415,7 +409,6 @@ end function socket.abandon(id) local s = socket_pool[id] if s then - driver.clear(s.buffer,buffer_pool) s.connected = false wakeup(s) socket_pool[id] = nil From 623d8182eb93c2cb0cb1ea81ecec926da2ce9849 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 10 Oct 2020 18:04:20 +0800 Subject: [PATCH 256/565] fix #1246 --- service/launcher.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/service/launcher.lua b/service/launcher.lua index 655458800..e28ccff07 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -31,10 +31,10 @@ local function list_srv(ti, fmt_func, ...) req:add(r) sessions[r] = addr end - for resp, req in req:select(ti) do + for req, resp in req:select(ti) do local stat = resp[1] local addr = req[1] - if resp.ok then + if resp then list[skynet.address(addr)] = fmt_func(stat, addr) else list[skynet.address(addr)] = fmt_func("ERROR", addr) @@ -62,8 +62,8 @@ end function command.MEM(addr, ti) return list_srv(ti, function(kb, addr) local v = services[addr] - if kb == "TIMEOUT" then - return string.format("TIMEOUT (%s)",v) + if type(kb) == "string" then + return string.format("%s (%s)", kb, v) else return string.format("%.2f Kb (%s)",kb,v) end @@ -74,7 +74,7 @@ function command.GC(addr, ti) for k,v in pairs(services) do skynet.send(k,"debug","GC") end - return command.MEM(ti) + return command.MEM(addr, ti) end function command.REMOVE(_, handle, kill) From a87b039c6d02c48476f9771a861c30022aee27bc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 10 Oct 2020 19:19:33 +0800 Subject: [PATCH 257/565] use new lua userdata api --- lualib-src/ltls.c | 6 +++--- lualib-src/lua-bson.c | 8 ++++---- lualib-src/lua-crypt.c | 12 ++++++------ lualib-src/lua-datasheet.c | 2 +- lualib-src/lua-debugchannel.c | 2 +- lualib-src/lua-netpack.c | 4 ++-- lualib-src/lua-sharedata.c | 8 ++++---- lualib-src/lua-sharetable.c | 2 +- lualib-src/lua-socket.c | 4 ++-- lualib-src/lua-stm.c | 4 ++-- 10 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 9d352de77..6da40c489 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -317,7 +317,7 @@ _lctx_ciphers(lua_State* L) { static int lnew_ctx(lua_State* L) { - struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdata(L, sizeof(*ctx_p)); + struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdatauv(L, sizeof(*ctx_p), 0); ctx_p->ctx = SSL_CTX_new(SSLv23_method()); if(!ctx_p->ctx) { unsigned int err = ERR_get_error(); @@ -345,12 +345,12 @@ lnew_ctx(lua_State* L) { static int lnew_tls(lua_State* L) { - struct tls_context* tls_p = (struct tls_context*)lua_newuserdata(L, sizeof(*tls_p)); + struct tls_context* tls_p = (struct tls_context*)lua_newuserdatauv(L, sizeof(*tls_p), 1); tls_p->is_close = false; const char* method = luaL_optstring(L, 1, "nil"); struct ssl_ctx* ctx_p = _check_sslctx(L, 2); lua_pushvalue(L, 2); - lua_setuservalue(L, -2); // set ssl_ctx associated to tls_context + lua_setiuservalue(L, -2, 1); // set ssl_ctx associated to tls_context if(strcmp(method, "client") == 0) { _init_client_context(L, tls_p, ctx_p); diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 6fca56236..808ca0f74 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -818,7 +818,7 @@ lmakeindex(lua_State *L) { lua_rawset(L,-3); } } - lua_setuservalue(L,1); + lua_setiuservalue(L,1,1); lua_settop(L,1); return 1; @@ -862,7 +862,7 @@ replace_object(lua_State *L, int type, struct bson * bs) { static int lreplace(lua_State *L) { - lua_getuservalue(L,1); + lua_getiuservalue(L,1,1); if (!lua_istable(L,-1)) { return luaL_error(L, "call makeindex first"); } @@ -954,7 +954,7 @@ encode_bson(lua_State *L) { } else { pack_simple_dict(L, b, 0); } - void * ud = lua_newuserdata(L, b->size); + void * ud = lua_newuserdatauv(L, b->size, 1); memcpy(ud, b->ptr, b->size); return 1; } @@ -984,7 +984,7 @@ encode_bson_byorder(lua_State *L) { lua_settop(L, --n); pack_ordered_dict(L, b, n, 0); lua_settop(L,0); - void * ud = lua_newuserdata(L, b->size); + void * ud = lua_newuserdatauv(L, b->size, 1); memcpy(ud, b->ptr, b->size); return 1; } diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 77ba7c40f..0e4d06cf3 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -470,7 +470,7 @@ ldesencode(lua_State *L) { uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (chunksz > SMALL_CHUNK) { - buffer = lua_newuserdata(L, chunksz); + buffer = lua_newuserdatauv(L, chunksz, 0); } int i; for (i=0;i<(int)textsz-7;i+=8) { @@ -503,7 +503,7 @@ ldesdecode(lua_State *L) { uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (textsz > SMALL_CHUNK) { - buffer = lua_newuserdata(L, textsz); + buffer = lua_newuserdatauv(L, textsz, 0); } for (i=0;i SMALL_CHUNK/2) { - buffer = lua_newuserdata(L, sz * 2); + buffer = lua_newuserdatauv(L, sz * 2, 0); } int i; for (i=0;i SMALL_CHUNK*2) { - buffer = lua_newuserdata(L, sz / 2); + buffer = lua_newuserdatauv(L, sz / 2, 0); } int i; for (i=0;i SMALL_CHUNK) { - buffer = lua_newuserdata(L, encode_sz); + buffer = lua_newuserdatauv(L, encode_sz, 0); } int i,j; j=0; @@ -953,7 +953,7 @@ lb64decode(lua_State *L) { char tmp[SMALL_CHUNK]; char *buffer = tmp; if (decode_sz > SMALL_CHUNK) { - buffer = lua_newuserdata(L, decode_sz); + buffer = lua_newuserdatauv(L, decode_sz, 0); } int i,j; int output = 0; diff --git a/lualib-src/lua-datasheet.c b/lualib-src/lua-datasheet.c index 57e56764c..c2b219db0 100644 --- a/lualib-src/lua-datasheet.c +++ b/lualib-src/lua-datasheet.c @@ -68,7 +68,7 @@ create_proxy(lua_State *L, const void *data, int index) { // NODECACHE, table, PROXYCACHE lua_pushvalue(L, -2); // NODECACHE, table, PROXYCACHE, table - struct proxy * p = lua_newuserdata(L, sizeof(struct proxy)); + struct proxy * p = lua_newuserdatauv(L, sizeof(struct proxy), 0); // NODECACHE, table, PROXYCACHE, table, proxy p->data = data; p->index = index; diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index 7f83c9e94..bfaf3d232 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -152,7 +152,7 @@ new_channel(lua_State *L, struct channel *c) { luaL_error(L, "new channel failed"); // never go here } - struct channel_box * cb = lua_newuserdata(L, sizeof(*cb)); + struct channel_box * cb = lua_newuserdatauv(L, sizeof(*cb), 0); cb->c = c; if (luaL_newmetatable(L, METANAME)) { luaL_Reg l[]={ diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 7f9a7b64a..ac84972dd 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -117,7 +117,7 @@ static struct queue * get_queue(lua_State *L) { struct queue *q = lua_touserdata(L,1); if (q == NULL) { - q = lua_newuserdata(L, sizeof(struct queue)); + q = lua_newuserdatauv(L, sizeof(struct queue), 0); q->cap = QUEUESIZE; q->head = 0; q->tail = 0; @@ -132,7 +132,7 @@ get_queue(lua_State *L) { static void expand_queue(lua_State *L, struct queue *q) { - struct queue *nq = lua_newuserdata(L, sizeof(struct queue) + q->cap * sizeof(struct netpack)); + struct queue *nq = lua_newuserdatauv(L, sizeof(struct queue) + q->cap * sizeof(struct netpack), 0); nq->cap = q->cap + QUEUESIZE; nq->head = 0; nq->tail = q->cap; diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 7f8cdead7..e96bf407a 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -381,7 +381,7 @@ convert_stringmap(struct context *ctx, struct table *tbl) { 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)); + struct state * s = lua_newuserdatauv(L, sizeof(*s), 1); s->dirty = 0; s->ref = 0; s->root = tbl; @@ -678,7 +678,7 @@ lboxconf(lua_State *L) { struct state * s = lua_touserdata(tbl->L, 1); ATOM_INC(&s->ref); - struct ctrl * c = lua_newuserdata(L, sizeof(*c)); + struct ctrl * c = lua_newuserdatauv(L, sizeof(*c), 1); c->root = tbl; c->update = NULL; if (luaL_newmetatable(L, "confctrl")) { @@ -742,7 +742,7 @@ lneedupdate(lua_State *L) { struct ctrl * c = lua_touserdata(L, 1); if (c->update) { lua_pushlightuserdata(L, c->update); - lua_getuservalue(L, 1); + lua_getiuservalue(L, 1, 1); return 2; } return 0; @@ -759,7 +759,7 @@ lupdate(lua_State *L) { return luaL_error(L, "You should update a new object"); } lua_settop(L, 3); - lua_setuservalue(L, 1); + lua_setiuservalue(L, 1, 1); c->update = n; return 0; diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index d74d1e3aa..185a5e857 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -151,7 +151,7 @@ get_size(lua_State *L) { static int box_state(lua_State *L, lua_State *mL) { - struct state_ud *ud = (struct state_ud *)lua_newuserdata(L, sizeof(*ud)); + struct state_ud *ud = (struct state_ud *)lua_newuserdatauv(L, sizeof(*ud), 0); ud->L = mL; if (luaL_newmetatable(L, "BOXMATRIXSTATE")) { lua_pushvalue(L, -1); diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ed9a226b0..593dd7ada 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -53,7 +53,7 @@ lfreepool(lua_State *L) { static int lnewpool(lua_State *L, int sz) { - struct buffer_node * pool = lua_newuserdata(L, sizeof(struct buffer_node) * sz); + struct buffer_node * pool = lua_newuserdatauv(L, sizeof(struct buffer_node) * sz, 0); int i; for (i=0;isize = 0; sb->offset = 0; sb->head = NULL; diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index 3089594bb..ecfa1e57d 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -140,7 +140,7 @@ lnewwriter(lua_State *L) { msg = skynet_malloc(sz); memcpy(msg, tmp, sz); } - struct boxstm * box = lua_newuserdata(L, sizeof(*box)); + struct boxstm * box = lua_newuserdatauv(L, sizeof(*box), 0); box->obj = stm_new(msg,sz); lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); @@ -182,7 +182,7 @@ struct boxreader { static int lnewreader(lua_State *L) { - struct boxreader * box = lua_newuserdata(L, sizeof(*box)); + struct boxreader * box = lua_newuserdatauv(L, sizeof(*box), 0); box->obj = lua_touserdata(L, 1); box->lastcopy = NULL; lua_pushvalue(L, lua_upvalueindex(1)); From 471c7d1101dc2edf52fedc674e37bbee669a5a13 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2020 16:12:01 +0800 Subject: [PATCH 258/565] add socket.pause --- lualib/skynet/socket.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e85f8d413..8bb2f73cc 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -229,6 +229,15 @@ function socket.start(id, func) return connect(id, func) end +function socket.pause(id) + local s = socket_pool[id] + if s == nil or s.pause then + return + end + driver.pause(id) + s.pause = true +end + function socket.shutdown(id) local s = socket_pool[id] if s then From f3acbe46df1bfe15170bf54a79df650dcb791b6a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2020 18:28:24 +0800 Subject: [PATCH 259/565] bugfix: pause will disable write event --- skynet-src/socket_epoll.h | 4 +-- skynet-src/socket_kqueue.h | 8 +++-- skynet-src/socket_poll.h | 2 +- skynet-src/socket_server.c | 65 +++++++++++++++++++------------------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index 58a26afd5..6a3ae733d 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -42,9 +42,9 @@ sp_del(int efd, int sock) { } static void -sp_write(int efd, int sock, void *ud, bool enable) { +sp_enable(int efd, int sock, void *ud, bool read_enable, bool write_enable) { struct epoll_event ev; - ev.events = EPOLLIN | (enable ? EPOLLOUT : 0); + ev.events = (read_enable ? EPOLLIN : 0) | (write_enable ? EPOLLOUT : 0); ev.data.ptr = ud; epoll_ctl(efd, EPOLL_CTL_MOD, sock, &ev); } diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index cf5a59ede..dae8955f6 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -56,9 +56,13 @@ sp_add(int kfd, int sock, void *ud) { } static void -sp_write(int kfd, int sock, void *ud, bool enable) { +sp_write(int kfd, int sock, void *ud, bool read_enable, bool write_enable) { struct kevent ke; - EV_SET(&ke, sock, EVFILT_WRITE, enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); + EV_SET(&ke, sock, EVFILT_READ, read_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { + // todo: check error + } + EV_SET(&ke, sock, EVFILT_WRITE, write_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { // todo: check error } diff --git a/skynet-src/socket_poll.h b/skynet-src/socket_poll.h index 0a01f300c..785ae5b64 100644 --- a/skynet-src/socket_poll.h +++ b/skynet-src/socket_poll.h @@ -18,7 +18,7 @@ static poll_fd sp_create(); static void sp_release(poll_fd fd); static int sp_add(poll_fd fd, int sock, void *ud); static void sp_del(poll_fd fd, int sock); -static void sp_write(poll_fd, int sock, void *ud, bool enable); +static void sp_enable(poll_fd, int sock, void *ud, bool read_enable, bool write_enable); static int sp_wait(poll_fd, struct event *e, int max); static void sp_nonblocking(int sock); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 7d2aa738a..998413ff6 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -37,10 +37,6 @@ #define PRIORITY_HIGH 0 #define PRIORITY_LOW 1 -#define READING_PAUSE 0 -#define READING_RESUME 1 -#define READING_CLOSE 2 - #define HASH_ID(id) (((unsigned)id) % MAX_SOCKET) #define ID_TAG16(id) ((id>>MAX_SOCKET_P) & 0xffff) @@ -99,7 +95,8 @@ struct socket { int id; uint8_t protocol; uint8_t type; - uint8_t reading; + bool reading; + bool writing; int udpconnecting; int64_t warn_size; union { @@ -482,10 +479,7 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_lock *l, s assert(s->type != SOCKET_TYPE_RESERVE); free_wb_list(ss,&s->high); free_wb_list(ss,&s->low); - if (s->reading == READING_RESUME) { - sp_del(ss->event_fd, s->fd); - } - s->reading = READING_CLOSE; + sp_del(ss->event_fd, s->fd); socket_lock(l); if (s->type != SOCKET_TYPE_BIND) { if (close(s->fd) < 0) { @@ -531,20 +525,19 @@ check_wb_list(struct wb_list *s) { } static struct socket * -new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool add) { +new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool reading) { struct socket * s = &ss->slot[HASH_ID(id)]; assert(s->type == SOCKET_TYPE_RESERVE); - if (add) { - if (sp_add(ss->event_fd, fd, s)) { - s->type = SOCKET_TYPE_INVALID; - return NULL; - } + if (sp_add(ss->event_fd, fd, s)) { + s->type = SOCKET_TYPE_INVALID; + return NULL; } s->id = id; s->fd = fd; - s->reading = add ? READING_RESUME : READING_PAUSE; + s->reading = reading; + s->writing = false; s->sending = ID_TAG16(id) << 16 | 0; s->protocol = protocol; s->p.size = MIN_READ_BUFFER; @@ -559,6 +552,22 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, return s; } +static inline void +enable_write(struct socket_server *ss, struct socket *s, bool enable) { + if (s->writing != enable) { + s->writing = enable; + sp_enable(ss->event_fd, s->fd, s, s->reading, enable); + } +} + +static inline void +enable_read(struct socket_server *ss, struct socket *s, bool enable) { + if (s->reading != enable) { + s->reading = enable; + sp_enable(ss->event_fd, s->fd, s, enable, s->writing); + } +} + static inline void stat_read(struct socket_server *ss, struct socket *s, int n) { s->stat.read += n; @@ -636,7 +645,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_OPEN; } else { ns->type = SOCKET_TYPE_CONNECTING; - sp_write(ss->event_fd, ns->fd, ns, true); + enable_write(ss, ns, true); } freeaddrinfo( ai_list ); @@ -817,7 +826,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, } // step 4 assert(send_buffer_empty(s) && s->wb_size == 0); - sp_write(ss->event_fd, s->fd, s, false); + enable_write(ss, s, false); if (s->type == SOCKET_TYPE_HALFCLOSE) { force_close(ss, s, l, result); @@ -954,7 +963,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return -1; } } - sp_write(ss->event_fd, s->fd, s, true); + enable_write(ss, s, true); } else { if (s->protocol == PROTOCOL_TCP) { if (priority == PRIORITY_LOW) { @@ -1067,14 +1076,7 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str } struct socket_lock l; socket_lock_init(s, &l); - if (s->reading == READING_PAUSE) { - if (sp_add(ss->event_fd, s->fd, s)) { - force_close(ss, s, &l, result); - result->data = strerror(errno); - return SOCKET_ERR; - } - s->reading = READING_RESUME; - } + enable_read(ss, s, true); if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; s->opaque = request->opaque; @@ -1097,10 +1099,7 @@ pause_socket(struct socket_server *ss, struct request_resumepause *request) { if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { return; } - if (s->reading == READING_RESUME) { - sp_del(ss->event_fd, s->fd); - s->reading = READING_PAUSE; - } + enable_read(ss, s, false); } static void @@ -1408,7 +1407,7 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l result->id = s->id; result->ud = 0; if (nomore_sending_data(s)) { - sp_write(ss->event_fd, s->fd, s, false); + enable_write(ss, s, false); } union sockaddr_all u; socklen_t slen = sizeof(u); @@ -1709,7 +1708,7 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { s->dw_buffer = clone_buffer(buf, &s->dw_size); s->dw_offset = n; - sp_write(ss->event_fd, s->fd, s, true); + enable_write(ss, s, true); socket_unlock(&l); return 0; From a4221deac17bb230f8a25f50be9f0e306a17284d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2020 22:34:46 +0800 Subject: [PATCH 260/565] enable read --- skynet-src/socket_server.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 998413ff6..f1b01f88a 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -524,6 +524,22 @@ check_wb_list(struct wb_list *s) { assert(s->tail == NULL); } +static inline void +enable_write(struct socket_server *ss, struct socket *s, bool enable) { + if (s->writing != enable) { + s->writing = enable; + sp_enable(ss->event_fd, s->fd, s, s->reading, enable); + } +} + +static inline void +enable_read(struct socket_server *ss, struct socket *s, bool enable) { + if (s->reading != enable) { + s->reading = enable; + sp_enable(ss->event_fd, s->fd, s, enable, s->writing); + } +} + static struct socket * new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool reading) { struct socket * s = &ss->slot[HASH_ID(id)]; @@ -536,7 +552,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->id = id; s->fd = fd; - s->reading = reading; + s->reading = true; s->writing = false; s->sending = ID_TAG16(id) << 16 | 0; s->protocol = protocol; @@ -549,25 +565,10 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->dw_buffer = NULL; s->dw_size = 0; memset(&s->stat, 0, sizeof(s->stat)); + enable_read(ss, s, reading); return s; } -static inline void -enable_write(struct socket_server *ss, struct socket *s, bool enable) { - if (s->writing != enable) { - s->writing = enable; - sp_enable(ss->event_fd, s->fd, s, s->reading, enable); - } -} - -static inline void -enable_read(struct socket_server *ss, struct socket *s, bool enable) { - if (s->reading != enable) { - s->reading = enable; - sp_enable(ss->event_fd, s->fd, s, enable, s->writing); - } -} - static inline void stat_read(struct socket_server *ss, struct socket *s, int n) { s->stat.read += n; From e75d033066d79fa9f214eccb460f0756fefec023 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 13 Oct 2020 10:53:33 +0800 Subject: [PATCH 261/565] fix kqueue --- skynet-src/socket_kqueue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index dae8955f6..87ee52b38 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -56,7 +56,7 @@ sp_add(int kfd, int sock, void *ud) { } static void -sp_write(int kfd, int sock, void *ud, bool read_enable, bool write_enable) { +sp_enable(int kfd, int sock, void *ud, bool read_enable, bool write_enable) { struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, read_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { From 77311c7243521b1bc12f074344e392f37670e3d3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 13 Oct 2020 12:04:08 +0800 Subject: [PATCH 262/565] enable error handle --- skynet-src/socket_epoll.h | 7 +++- skynet-src/socket_kqueue.h | 8 ++-- skynet-src/socket_poll.h | 2 +- skynet-src/socket_server.c | 75 +++++++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index 6a3ae733d..f3c3225d3 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -41,12 +41,15 @@ sp_del(int efd, int sock) { epoll_ctl(efd, EPOLL_CTL_DEL, sock , NULL); } -static void +static int sp_enable(int efd, int sock, void *ud, bool read_enable, bool write_enable) { struct epoll_event ev; ev.events = (read_enable ? EPOLLIN : 0) | (write_enable ? EPOLLOUT : 0); ev.data.ptr = ud; - epoll_ctl(efd, EPOLL_CTL_MOD, sock, &ev); + if (epoll_ctl(efd, EPOLL_CTL_MOD, sock, &ev) == -1) { + return 1; + } + return 0; } static int diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index 87ee52b38..2212db379 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -55,17 +55,19 @@ sp_add(int kfd, int sock, void *ud) { return 0; } -static void +static int sp_enable(int kfd, int sock, void *ud, bool read_enable, bool write_enable) { + int ret = 0; struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, read_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { - // todo: check error + ret |= 1; } EV_SET(&ke, sock, EVFILT_WRITE, write_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { - // todo: check error + ret |= 1; } + return ret; } static int diff --git a/skynet-src/socket_poll.h b/skynet-src/socket_poll.h index 785ae5b64..7d02caba0 100644 --- a/skynet-src/socket_poll.h +++ b/skynet-src/socket_poll.h @@ -18,7 +18,7 @@ static poll_fd sp_create(); static void sp_release(poll_fd fd); static int sp_add(poll_fd fd, int sock, void *ud); static void sp_del(poll_fd fd, int sock); -static void sp_enable(poll_fd, int sock, void *ud, bool read_enable, bool write_enable); +static int sp_enable(poll_fd, int sock, void *ud, bool read_enable, bool write_enable); static int sp_wait(poll_fd, struct event *e, int max); static void sp_nonblocking(int sock); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f1b01f88a..163b812f5 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -524,20 +524,22 @@ check_wb_list(struct wb_list *s) { assert(s->tail == NULL); } -static inline void +static inline int enable_write(struct socket_server *ss, struct socket *s, bool enable) { if (s->writing != enable) { s->writing = enable; - sp_enable(ss->event_fd, s->fd, s, s->reading, enable); + return sp_enable(ss->event_fd, s->fd, s, s->reading, enable); } + return 0; } -static inline void +static inline int enable_read(struct socket_server *ss, struct socket *s, bool enable) { if (s->reading != enable) { s->reading = enable; - sp_enable(ss->event_fd, s->fd, s, enable, s->writing); + return sp_enable(ss->event_fd, s->fd, s, enable, s->writing); } + return 0; } static struct socket * @@ -565,7 +567,10 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->dw_buffer = NULL; s->dw_size = 0; memset(&s->stat, 0, sizeof(s->stat)); - enable_read(ss, s, reading); + if (enable_read(ss, s, reading)) { + s->type = SOCKET_TYPE_INVALID; + return NULL; + } return s; } @@ -646,7 +651,10 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_OPEN; } else { ns->type = SOCKET_TYPE_CONNECTING; - enable_write(ss, ns, true); + if (enable_write(ss, ns, true)) { + result->data = "enable write failed"; + goto _failed; + } } freeaddrinfo( ai_list ); @@ -827,12 +835,21 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, } // step 4 assert(send_buffer_empty(s) && s->wb_size == 0); - enable_write(ss, s, false); + int err = enable_write(ss, s, false); if (s->type == SOCKET_TYPE_HALFCLOSE) { force_close(ss, s, l, result); return SOCKET_CLOSE; } + + if (err) { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = "disable write failed"; + return SOCKET_ERR; + } + if(s->warn_size > 0){ s->warn_size = 0; result->opaque = s->opaque; @@ -964,7 +981,13 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return -1; } } - enable_write(ss, s, true); + if (enable_write(ss, s, true)) { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = "enable write failed"; + return SOCKET_ERR; + } } else { if (s->protocol == PROTOCOL_TCP) { if (priority == PRIORITY_LOW) { @@ -1077,7 +1100,10 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str } struct socket_lock l; socket_lock_init(s, &l); - enable_read(ss, s, true); + if (enable_read(ss, s, true)) { + result->data = "enable read failed"; + return SOCKET_ERR; + } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; s->opaque = request->opaque; @@ -1093,14 +1119,21 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str return -1; } -static void -pause_socket(struct socket_server *ss, struct request_resumepause *request) { +static int +pause_socket(struct socket_server *ss, struct request_resumepause *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; + return -1; + } + if (enable_read(ss, s, false)) { + result->id = id; + result->opaque = request->opaque; + result->ud = 0; + result->data = "enable read failed"; + return SOCKET_ERR; } - enable_read(ss, s, false); + return -1; } static void @@ -1237,8 +1270,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { case 'R': return resume_socket(ss,(struct request_resumepause *)buffer, result); case 'S': - pause_socket(ss,(struct request_resumepause *)buffer); - return -1; + return pause_socket(ss,(struct request_resumepause *)buffer, result); case 'B': return bind_socket(ss,(struct request_bind *)buffer, result); case 'L': @@ -1408,7 +1440,11 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l result->id = s->id; result->ud = 0; if (nomore_sending_data(s)) { - enable_write(ss, s, false); + if (enable_write(ss, s, false)) { + force_close(ss,s,l, result); + result->data = "disable write failed"; + return SOCKET_ERR; + } } union sockaddr_all u; socklen_t slen = sizeof(u); @@ -1709,9 +1745,14 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { s->dw_buffer = clone_buffer(buf, &s->dw_size); s->dw_offset = n; - enable_write(ss, s, true); + int err = enable_write(ss, s, true); socket_unlock(&l); + + if (err) { + fprintf(stderr, "socket-server : enable write (%d) failed.\n", id); + return -1; + } return 0; } socket_unlock(&l); From 0971ce99125d3dcad511c68b304b97e2dbe51841 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 13 Oct 2020 14:21:38 +0800 Subject: [PATCH 263/565] enable write in socket thread --- skynet-src/socket_server.c | 62 ++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 163b812f5..175de427b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -4,6 +4,7 @@ #include "socket_poll.h" #include "atomic.h" #include "spinlock.h" +#include "skynet.h" #include #include @@ -373,17 +374,17 @@ socket_server_create(uint64_t time) { int fd[2]; poll_fd efd = sp_create(); if (sp_invalid(efd)) { - fprintf(stderr, "socket-server: create event pool failed.\n"); + skynet_error(NULL, "socket-server: create event pool failed."); return NULL; } if (pipe(fd)) { sp_release(efd); - fprintf(stderr, "socket-server: create socket pair failed.\n"); + skynet_error(NULL, "socket-server: create socket pair failed."); return NULL; } if (sp_add(efd, fd[0], NULL)) { // add recvctrl_fd to event poll - fprintf(stderr, "socket-server: can't add server fd to event pool.\n"); + skynet_error(NULL, "socket-server: can't add server fd to event pool."); close(fd[0]); close(fd[1]); sp_release(efd); @@ -739,7 +740,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, tmp->udp_address, &sa); if (sasz == 0) { - fprintf(stderr, "socket-server : udp (%d) type mismatch.\n", s->id); + skynet_error(NULL, "socket-server : udp (%d) type mismatch.", s->id); drop_udp(ss, s, list, tmp); return -1; } @@ -750,7 +751,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, case AGAIN_WOULDBLOCK: return -1; } - fprintf(stderr, "socket-server : udp (%d) sendto error %s.\n",s->id, strerror(errno)); + skynet_error(NULL, "socket-server : udp (%d) sendto error %s.",s->id, strerror(errno)); drop_udp(ss, s, list, tmp); return -1; } @@ -931,6 +932,21 @@ append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_ s->wb_size += buf->sz; } +static int +trigger_write(struct socket_server *ss, struct request_send * 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; + if (enable_write(ss, s, true)) { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = "enable write failed"; + return SOCKET_ERR; + } + return -1; +} /* When send a package , we can assign the priority : PRIORITY_HIGH or PRIORITY_LOW @@ -952,7 +968,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return -1; } if (s->type == SOCKET_TYPE_PLISTEN || s->type == SOCKET_TYPE_LISTEN) { - fprintf(stderr, "socket-server: write to listen fd %d.\n", id); + skynet_error(NULL, "socket-server: write to listen fd %d.", id); so.free_func((void *)request->buffer); return -1; } @@ -968,7 +984,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { // udp type mismatch, just drop it. - fprintf(stderr, "socket-server: udp socket (%d) type mistach.\n", id); + skynet_error(NULL, "socket-server: udp socket (%d) type mistach.", id); so.free_func((void *)request->buffer); return -1; } @@ -1154,7 +1170,7 @@ block_readpipe(int pipefd, void *buffer, int sz) { if (n<0) { if (errno == EINTR) continue; - fprintf(stderr, "socket-server : read pipe error %s.\n",strerror(errno)); + skynet_error(NULL, "socket-server : read pipe error %s.",strerror(errno)); return; } // must atomic read from a pipe @@ -1285,6 +1301,8 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { result->ud = 0; result->data = NULL; return SOCKET_EXIT; + case 'W': + return trigger_write(ss, (struct request_send *)buffer, result); case 'D': case 'P': { int priority = (type == 'D') ? PRIORITY_HIGH : PRIORITY_LOW; @@ -1306,7 +1324,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { add_udp_socket(ss, (struct request_udp *)buffer); return -1; default: - fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type); + skynet_error(NULL, "socket-server: Unknown ctrl %c.",type); return -1; }; @@ -1325,7 +1343,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo case EINTR: break; case AGAIN_WOULDBLOCK: - fprintf(stderr, "socket-server: EAGAIN capture.\n"); + skynet_error(NULL, "socket-server: EAGAIN capture."); break; default: // close when error @@ -1590,7 +1608,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int break; } case SOCKET_TYPE_INVALID: - fprintf(stderr, "socket-server: invalid socket\n"); + skynet_error(NULL, "socket-server: invalid socket"); break; default: if (e->read) { @@ -1654,7 +1672,7 @@ send_request(struct socket_server *ss, struct request_package *request, char typ ssize_t n = write(ss->sendctrl_fd, &request->header[6], len+2); if (n<0) { if (errno != EINTR) { - fprintf(stderr, "socket-server : send ctrl command error %s.\n", strerror(errno)); + skynet_error(NULL, "socket-server : send ctrl command error %s.", strerror(errno)); } continue; } @@ -1667,7 +1685,7 @@ 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) { - fprintf(stderr, "socket-server : Invalid addr %s.\n",addr); + skynet_error(NULL, "socket-server : Invalid addr %s.",addr); return -1; } int id = reserve_id(ss); @@ -1723,7 +1741,7 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, s->p.udp_address, &sa); if (sasz == 0) { - fprintf(stderr, "socket-server : set udp (%d) address first.\n", id); + skynet_error(NULL, "socket-server : set udp (%d) address first.", id); socket_unlock(&l); so.free_func((void *)buf->buffer); return -1; @@ -1745,14 +1763,18 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { s->dw_buffer = clone_buffer(buf, &s->dw_size); s->dw_offset = n; - int err = enable_write(ss, s, true); - socket_unlock(&l); - if (err) { - fprintf(stderr, "socket-server : enable write (%d) failed.\n", id); - return -1; - } + inc_sending_ref(s, id); + + struct request_package request; + request.u.send.id = id; + request.u.send.sz = 0; + request.u.send.buffer = NULL; + + // let socket thread enable write event + send_request(ss, &request, 'W', sizeof(request.u.send)); + return 0; } socket_unlock(&l); From 064e61d40fa915874007810fa5dd5969bda89057 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2020 11:12:13 +0800 Subject: [PATCH 264/565] debug GC and socket.resume should wait for message queue --- lualib/skynet/debug.lua | 14 +++++++++++++- lualib/skynet/socket.lua | 24 ++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 78fd1a166..b8257135a 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -18,9 +18,21 @@ local function init(skynet, export) skynet.ret(skynet.pack(kb)) end + local gcing = false function dbgcmd.GC() - + if gcing then + return + end + gcing = true + local before = collectgarbage "count" + local before_time = skynet.now() collectgarbage "collect" + -- skip subsequent GC message + skynet.yield() + local after = collectgarbage "count" + local after_time = skynet.now() + skynet.error(string.format("GC %.2f Kb -> %.2f Kb, cost %.2f sec", before, after, (after_time - before_time) / 100)) + gcing = false end function dbgcmd.STAT() diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 8bb2f73cc..4bea10c1a 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -26,10 +26,22 @@ local function wakeup(s) end end +local function pause_socket(s, size) + if size then + skynet.error(string.format("Pause socket (%d) size : %d" , s.id, size)) + else + skynet.error(string.format("Pause socket (%d)" , s.id)) + end + driver.pause(s.id) + s.pause = true +end + local function suspend(s) assert(not s.co) s.co = coroutine.running() if s.pause then + skynet.yield() -- there are subsequent socket messages in mqueue, maybe. + skynet.error(string.format("Resume socket (%d)", s.id)) driver.start(s.id) s.pause = nil end @@ -60,8 +72,7 @@ socket_message[1] = function(id, size, data) s.read_required = nil wakeup(s) if sz > BUFFER_LIMIT then - driver.pause(id) - s.pause = true + pause_socket(s, sz) end end else @@ -76,13 +87,11 @@ socket_message[1] = function(id, size, data) s.read_required = nil wakeup(s) if sz > BUFFER_LIMIT then - driver.pause(id) - s.pause = true + pause_socket(s, sz) end end elseif sz > BUFFER_LIMIT and not s.pause then - driver.pause(id) - s.pause = true + pause_socket(s, sz) end end end @@ -234,8 +243,7 @@ function socket.pause(id) if s == nil or s.pause then return end - driver.pause(id) - s.pause = true + pause_socket(s) end function socket.shutdown(id) From b6354a049076d374039ae642a62dd8a6177a3d7b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2020 14:12:29 +0800 Subject: [PATCH 265/565] Add skynet.killthread() and debugcommand killtask --- lualib/skynet.lua | 58 +++++++++++++++++++++++++++++++++++++-- lualib/skynet/debug.lua | 11 ++++++++ service/debug_console.lua | 6 ++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 03e3c83d5..d571ea6fe 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -414,6 +414,59 @@ function skynet.wait(token) session_id_coroutine[session] = nil end +function skynet.killthread(thread) + local session + -- find session + if type(thread) == "string" then + for k,v in pairs(session_id_coroutine) do + local thread_string = tostring(v) + if thread_string:find(thread) then + session = k + break + end + end + else + for i = 1, #fork_queue do + if fork_queue[i] == thread then + tremove(fork_queue, i) + return thread + end + end + for k,v in pairs(session_id_coroutine) do + if v == thread then + session = k + break + end + end + end + local co = session_id_coroutine[session] + if co == nil then + return + end + watching_session[session] = nil + local addr = session_coroutine_address[co] + if addr then + session_coroutine_address[co] = nil + session_coroutine_tracetag[co] = nil + c.send(addr, skynet.PTYPE_ERROR, session_coroutine_id[co], "") + session_coroutine_id[co] = nil + end + if watching_session[session] then + session_id_coroutine[session] = "BREAK" + watching_session[session] = nil + else + session_id_coroutine[session] = nil + end + for k,v in pairs(sleep_session) do + if v == session then + sleep_session[k] = nil + break + end + end + coroutine.close(co) + return co +end + function skynet.self() return c.addresscommand "REG" end @@ -941,10 +994,11 @@ function skynet.task(ret) local tt = type(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do + local key = string.format("%s session: %d", tostring(co), session) if timeout_traceback and timeout_traceback[co] then - ret[session] = timeout_traceback[co] + ret[key] = timeout_traceback[co] else - ret[session] = traceback(co) + ret[key] = traceback(co) end end return diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index b8257135a..d80914446 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -44,6 +44,17 @@ local function init(skynet, export) skynet.ret(skynet.pack(stat)) end + function dbgcmd.KILLTASK(threadname) + local co = skynet.killthread(threadname) + if co then + skynet.error(string.format("Kill %s", co)) + skynet.ret() + else + skynet.error(string.format("Kill %s : Not found", threadname)) + skynet.ret(skynet.pack "Not found") + end + end + function dbgcmd.TASK(session) if session then skynet.ret(skynet.pack(skynet.task(session))) diff --git a/service/debug_console.lua b/service/debug_console.lua index e8b26f688..f0e2a853c 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -164,6 +164,7 @@ function COMMAND.help() netstat = "netstat : show netstat", profactive = "profactive [on|off] : active/deactive jemalloc heap profilling", dumpheap = "dumpheap : dump heap profilling", + killtask = "killtask address threadname : threadname listed by task", } end @@ -277,6 +278,11 @@ function COMMAND.task(address) return skynet.call(address,"debug","TASK") end +function COMMAND.killtask(address, threadname) + address = adjust_address(address) + return skynet.call(address, "debug", "KILLTASK", threadname) +end + function COMMAND.uniqtask(address) address = adjust_address(address) return skynet.call(address,"debug","UNIQTASK") From 55f207c1d2506e7b2f4d3cb0c5086ddd6a8276ac Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2020 16:24:04 +0800 Subject: [PATCH 266/565] fix debug console command : kill --- 3rd/jemalloc | 2 +- service/debug_console.lua | 2 +- service/launcher.lua | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index ea6b3e973..b0b3e49a5 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit ea6b3e973b477b8061e0076bb257dbd7f3faa756 +Subproject commit b0b3e49a54ec29e32636f4577d9d5a896d67fd20 diff --git a/service/debug_console.lua b/service/debug_console.lua index f0e2a853c..9601ce527 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -247,7 +247,7 @@ function COMMAND.mem(ti) end function COMMAND.kill(address) - return skynet.call(".launcher", "lua", "KILL", address) + return skynet.call(".launcher", "lua", "KILL", adjust_address(address)) end function COMMAND.gc(ti) diff --git a/service/launcher.lua b/service/launcher.lua index e28ccff07..d07d83248 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -52,7 +52,6 @@ function command.STAT(addr, ti) end function command.KILL(_, handle) - handle = handle_to_address(handle) skynet.kill(handle) local ret = { [skynet.address(handle)] = tostring(services[handle]) } services[handle] = nil From e669913b2abf8f06c20a95044876dd5f5797a22a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2020 17:10:29 +0800 Subject: [PATCH 267/565] log debug console client ip --- service/debug_console.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 9601ce527..c23818bea 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -89,9 +89,9 @@ local function docmd(cmdline, print, fd) end end -local function console_main_loop(stdin, print) +local function console_main_loop(stdin, print, addr) print("Welcome to skynet console") - skynet.error(stdin, "connected") + skynet.error(addr, "connected") local ok, err = pcall(function() while true do local cmdline = socket.readline(stdin, "\n") @@ -113,7 +113,7 @@ local function console_main_loop(stdin, print) if not ok then skynet.error(stdin, err) end - skynet.error(stdin, "disconnected") + skynet.error(addr, "disconnect") socket.close(stdin) end @@ -130,7 +130,7 @@ skynet.start(function() socket.write(id, "\n") end socket.start(id) - skynet.fork(console_main_loop, id , print) + skynet.fork(console_main_loop, id , print, addr) end) end) From e2419209498d7e5eba8a2e9c49077f4634da5dfd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Oct 2020 17:34:44 +0800 Subject: [PATCH 268/565] enable read when closing --- skynet-src/socket_server.c | 1 + 1 file changed, 1 insertion(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 175de427b..f317ecc83 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1069,6 +1069,7 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc struct socket_lock l; socket_lock_init(s, &l); if (!nomore_sending_data(s)) { + enable_read(ss,s,true); int type = send_buffer(ss,s,&l,result); // type : -1 or SOCKET_WARNING or SOCKET_CLOSE, SOCKET_WARNING means nomore_sending_data if (type != -1 && type != SOCKET_WARNING) From 2eea8be7ac1035b75c08f375c4e02540a9b1814d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Oct 2020 18:00:25 +0800 Subject: [PATCH 269/565] don't pause again --- lualib/skynet/socket.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 4bea10c1a..7eadeb1a8 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -27,6 +27,9 @@ local function wakeup(s) end local function pause_socket(s, size) + if s.pause then + return + end if size then skynet.error(string.format("Pause socket (%d) size : %d" , s.id, size)) else From 6dfdebc1a2c2c4eb6d38a375eb9957dce2fbfe20 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Oct 2020 20:25:29 +0800 Subject: [PATCH 270/565] reset pause after wait --- lualib/skynet/socket.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 7eadeb1a8..3f99e9ef1 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -46,9 +46,11 @@ local function suspend(s) skynet.yield() -- there are subsequent socket messages in mqueue, maybe. skynet.error(string.format("Resume socket (%d)", s.id)) driver.start(s.id) + skynet.wait(s.co) s.pause = nil + else + skynet.wait(s.co) end - 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 From 8ea74c2b939386acbd4f01e743e81a3f76009214 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 16 Oct 2020 11:09:39 +0800 Subject: [PATCH 271/565] update to lua 5.4.2 --- 3rd/lua/README | 2 +- 3rd/lua/lapi.c | 19 ++++-- 3rd/lua/ldblib.c | 29 +++++---- 3rd/lua/ldo.c | 145 ++++++++++++++++++++++++++++----------------- 3rd/lua/ldo.h | 1 + 3rd/lua/lfunc.c | 2 +- 3rd/lua/lgc.c | 52 +++++++++------- 3rd/lua/llimits.h | 11 ++++ 3rd/lua/lobject.h | 18 +++--- 3rd/lua/lopcodes.h | 4 +- 3rd/lua/lparser.c | 8 +-- 3rd/lua/lstate.c | 96 ++++++++++-------------------- 3rd/lua/lstate.h | 91 +++++++++------------------- 3rd/lua/lstring.c | 20 ++----- 3rd/lua/lstring.h | 3 +- 3rd/lua/lstrlib.c | 19 +++--- 3rd/lua/ltable.c | 29 +++++---- 3rd/lua/lua.h | 2 +- 3rd/lua/luaconf.h | 15 ----- 3rd/lua/lvm.c | 42 ++++++++----- 20 files changed, 297 insertions(+), 311 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index 4a68ad51d..059ade888 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,4 +1,4 @@ -This is a modify version of lua 5.4.0-rc1 (http://www.lua.org/work/lua-5.4.0-rc1.tar.gz) . +This is a modify version of lua 5.4.2 . For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 13835180b..2b7609f4b 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1427,13 +1427,16 @@ 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) { + static const UpVal *const nullup = NULL; LClosure *f; TValue *fi = index2value(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"); if (pf) *pf = f; - return &f->upvals[n - 1]; /* get its upvalue pointer */ + if (1 <= n && n <= f->p->sizeupvalues) + return &f->upvals[n - 1]; /* get its upvalue pointer */ + else + return (UpVal**)&nullup; } @@ -1445,11 +1448,14 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; - } + if (1 <= n && n <= f->nupvalues) + return &f->upvalue[n - 1]; + /* else */ + } /* FALLTHROUGH */ + case LUA_VLCF: + return NULL; /* light C functions have no upvalues */ default: { - api_check(L, 0, "closure expected"); + api_check(L, 0, "function expected"); return NULL; } } @@ -1461,6 +1467,7 @@ 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); + api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); *up1 = *up2; luaC_objbarrier(L, f1, *up1); } diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 59eb8f0ea..5a326aded 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -281,25 +281,33 @@ 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) { +static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) { + void *id; 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; + id = lua_upvalueid(L, argf, nup); + if (pnup) { + luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index"); + *pnup = nup; + } + return id; } static int db_upvalueid (lua_State *L) { - int n = checkupval(L, 1, 2); - lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); + void *id = checkupval(L, 1, 2, NULL); + if (id != NULL) + lua_pushlightuserdata(L, id); + else + luaL_pushfail(L); return 1; } static int db_upvaluejoin (lua_State *L) { - int n1 = checkupval(L, 1, 2); - int n2 = checkupval(L, 3, 4); + int n1, n2; + checkupval(L, 1, 2, &n1); + checkupval(L, 3, 4, &n2); luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); lua_upvaluejoin(L, 1, n1, 3, n2); @@ -440,10 +448,7 @@ static int db_traceback (lua_State *L) { static int db_setcstacklimit (lua_State *L) { int limit = (int)luaL_checkinteger(L, 1); int res = lua_setcstacklimit(L, limit); - if (res == 0) - lua_pushboolean(L, 0); - else - lua_pushinteger(L, res); + lua_pushinteger(L, res); return 1; } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 5473815a1..5729b1902 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -139,8 +139,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - global_State *g = G(L); - l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci); + l_uint32 oldnCcalls = L->nCcalls; struct lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ @@ -149,7 +148,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci; + L->nCcalls = oldnCcalls; return lj.status; } @@ -183,10 +182,10 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int lim = L->stacksize; - StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); + int lim = stacksize(L); + StkId newstack = luaM_reallocvector(L, L->stack, + lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); if (unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); @@ -196,8 +195,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { setnilvalue(s2v(newstack + lim)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; - L->stacksize = newsize; - L->stack_last = L->stack + newsize - EXTRA_STACK; + L->stack_last = L->stack + newsize; return 1; } @@ -207,51 +205,73 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { - int size = L->stacksize; - int newsize = 2 * size; /* tentative new size */ - if (unlikely(size > LUAI_MAXSTACK)) { /* need more space after extra size? */ + int size = stacksize(L); + if (unlikely(size > LUAI_MAXSTACK)) { + /* if stack is larger than maximum, thread is already using the + extra space reserved for errors, that is, thread is handling + a stack error; cannot grow further than that. */ + lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) luaD_throw(L, LUA_ERRERR); /* error inside message handler */ - else return 0; + return 0; /* if not 'raiseerror', just signal it */ } else { - int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; + int newsize = 2 * size; /* tentative new size */ + int needed = cast_int(L->top - L->stack) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (unlikely(newsize > LUAI_MAXSTACK)) { /* stack overflow? */ + if (likely(newsize <= LUAI_MAXSTACK)) + return luaD_reallocstack(L, newsize, raiseerror); + else { /* stack overflow */ /* add extra size to be able to handle the error message */ luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); if (raiseerror) luaG_runerror(L, "stack overflow"); - else return 0; + return 0; } - } /* else no errors */ - return luaD_reallocstack(L, newsize, raiseerror); + } } static int stackinuse (lua_State *L) { CallInfo *ci; + int res; StkId lim = L->top; for (ci = L->ci; ci != NULL; ci = ci->previous) { 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 */ + res = cast_int(lim - L->stack) + 1; /* part of stack in use */ + if (res < LUA_MINSTACK) + res = LUA_MINSTACK; /* ensure a minimum size */ + return res; } +/* +** If stack size is more than 3 times the current use, reduce that size +** to twice the current use. (So, the final stack size is at most 2/3 the +** previous size, and half of its entries are empty.) +** As a particular case, if stack was handling a stack overflow and now +** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than +** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack +** will be reduced to a "regular" size. +*/ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int goodsize = inuse + BASIC_STACK_SIZE; - if (goodsize > LUAI_MAXSTACK) - goodsize = LUAI_MAXSTACK; /* respect stack limit */ + int nsize = inuse * 2; /* proposed new size */ + int max = inuse * 3; /* maximum "reasonable" size */ + if (max > LUAI_MAXSTACK) { + max = LUAI_MAXSTACK; /* respect stack limit */ + if (nsize > LUAI_MAXSTACK) + nsize = LUAI_MAXSTACK; + } /* 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, 0); /* ok if that fails */ + size is larger than maximum "reasonable" size, shrink it */ + if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) + luaD_reallocstack(L, nsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ @@ -348,7 +368,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { /* ** Check whether 'func' has a '__call' metafield. If so, put it in the -** stack, below original 'func', so that 'luaD_call' can call it. Raise +** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ void luaD_tryfuncTM (lua_State *L, StkId func) { @@ -449,12 +469,14 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { /* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. +** Prepares the call to a function (C or Lua). For C functions, also do +** the call. The function to be called is at '*func'. The arguments +** are on the stack, right after the function. Returns the CallInfo +** to be executed, if it was a Lua function. Otherwise (a C function) +** returns NULL, with all the results on the stack, starting at the +** original function position. */ -void luaD_call (lua_State *L, StkId func, int nresults) { +CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -482,7 +504,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); - break; + return NULL; } case LUA_VLCL: { /* Lua function */ CallInfo *ci; @@ -494,15 +516,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { L->ci = ci = next_ci(L); ci->nresults = nresults; ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = 0; ci->top = func + 1 + fsize; ci->func = func; L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); - luaV_execute(L, ci); /* run the function */ - break; + return ci; } default: { /* not a function */ checkstackGCp(L, 1, func); /* space for metamethod */ @@ -513,17 +533,37 @@ void luaD_call (lua_State *L, StkId func, int nresults) { } +/* +** Call a function (C or Lua). 'inc' can be 1 (increment number +** of recursive invocations in the C stack) or nyci (the same plus +** increment number of non-yieldable calls). +*/ +static void docall (lua_State *L, StkId func, int nResults, int inc) { + CallInfo *ci; + L->nCcalls += inc; + if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + luaE_checkcstack(L); + if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ + ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ + luaV_execute(L, ci); /* call it */ + } + L->nCcalls -= inc; +} + + +/* +** External interface for 'docall' +*/ +void luaD_call (lua_State *L, StkId func, int nResults) { + return docall(L, func, nResults, 1); +} + + /* ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - incXCcalls(L); - if (getCcalls(L) <= CSTACKERR) { /* possible C stack overflow? */ - luaE_exitCcall(L); /* to compensate decrement in next call */ - luaE_enterCcall(L); /* check properly */ - } - luaD_call(L, func, nResults); - decXCcalls(L); + return docall(L, func, nResults, nyci); } @@ -601,12 +641,12 @@ static int recover (lua_State *L, int status) { if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ oldtop = restorestack(L, ci->u2.funcidx); - luaF_close(L, oldtop, status); /* may change the stack */ - oldtop = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - luaD_shrinkstack(L); + status = luaF_close(L, oldtop, status); /* may change the stack */ + oldtop = restorestack(L, ci->u2.funcidx); + luaD_seterrorobj(L, status, oldtop); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ } @@ -637,12 +677,12 @@ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; - if (L->status == LUA_OK) { /* starting a coroutine? */ - luaD_call(L, firstArg - 1, LUA_MULTRET); - } + if (L->status == LUA_OK) /* starting a coroutine? */ + docall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ + luaE_incCstack(L); /* control the C stack */ if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L, ci); /* just continue running Lua code */ else { /* 'common' yield */ @@ -670,12 +710,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); - if (from == NULL) - L->nCcalls = CSTACKTHREAD; - else /* correct 'nCcalls' for this thread */ - L->nCcalls = getCcalls(from) - L->nci - CSTACKCF; - if (L->nCcalls <= CSTACKERR) - return resume_error(L, "C stack overflow", nargs); + L->nCcalls = (from) ? getCcalls(from) : 0; luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); @@ -754,7 +789,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, status = luaF_close(L, oldtop, status); oldtop = restorestack(L, old_top); /* previous call may change stack */ luaD_seterrorobj(L, status, oldtop); - luaD_shrinkstack(L); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; return status; diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 6c6cb2855..4d30d072e 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -59,6 +59,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 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 void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index f2d60d2e1..57b635474 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -54,7 +54,7 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); cl->upvals[i] = uv; - luaC_objbarrier(L, cl, o); + luaC_objbarrier(L, cl, uv); } } diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index c2027fe8a..febc11de1 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -161,18 +161,17 @@ static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { /* -** Clear keys for empty entries in tables. If entry is empty -** and its key is not marked, mark its entry as dead. This allows the -** collection of the key, but keeps its entry in the table (its removal -** could break a chain). The main feature of a dead key is that it must -** be different from any other value, to do not disturb searches. -** Other places never manipulate dead keys, because its associated empty -** value is enough to signal that the entry is logically empty. +** Clear keys for empty entries in tables. If entry is empty, mark its +** entry as dead. This allows the collection of the key, but keeps its +** entry in the table: its removal could break a chain and could break +** a table traversal. Other places never manipulate dead keys, because +** its associated empty value is enough to signal that the entry is +** logically empty. */ static void clearkey (Node *n) { lua_assert(isempty(gval(n))); - if (keyiswhite(n)) - setdeadkey(n); /* unused and unmarked key; remove it */ + if (keyiscollectable(n)) + setdeadkey(n); /* unused key; remove it */ } @@ -303,7 +302,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { if (upisopen(uv)) set2gray(uv); /* open upvalues are kept gray */ else - set2black(o); /* closed upvalues are visited here */ + set2black(uv); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -311,7 +310,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - set2black(o); /* nothing else to mark */ + set2black(u); /* nothing else to mark */ break; } /* else... */ @@ -635,8 +634,7 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - StkId lim = th->stack + th->stacksize; /* real end of stack */ - for (; o < lim; o++) /* clear not-marked stack slice */ + for (; o < th->stack_last; o++) /* clear not-marked stack slice */ setnilvalue(s2v(o)); /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -646,7 +644,7 @@ static int traversethread (global_State *g, lua_State *th) { } else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return 1 + th->stacksize; + return 1 + stacksize(th); } @@ -773,12 +771,16 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_VUPVAL: freeupval(L, gco2upv(o)); break; - case LUA_VLCL: - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + case LUA_VLCL: { + LClosure *cl = gco2lcl(o); + luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); break; - case LUA_VCCL: - luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); + } + case LUA_VCCL: { + CClosure *cl = gco2ccl(o); + luaM_freemem(L, cl, sizeCclosure(cl->nupvalues)); break; + } case LUA_VTABLE: luaH_free(L, gco2t(o)); break; @@ -790,13 +792,17 @@ static void freeobj (lua_State *L, GCObject *o) { luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; } - case LUA_VSHRSTR: - luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); + case LUA_VSHRSTR: { + TString *ts = gco2ts(o); + luaS_remove(L, ts); /* remove it from hash table */ + luaM_freemem(L, ts, sizelstring(ts->shrlen)); break; - case LUA_VLNGSTR: - luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); + } + case LUA_VLNGSTR: { + TString *ts = gco2ts(o); + luaM_freemem(L, ts, sizelstring(ts->u.lnglen)); break; + } default: lua_assert(0); } } diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 48c97f959..d6866d7c1 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -234,6 +234,17 @@ typedef l_uint32 Instruction; #endif +/* +** Maximum depth for nested C calls, syntactical nested non-terminals, +** and other features implemented through recursion in C. (Value must +** fit in a 16-bit unsigned integer. It must also be compatible with +** the size of the C stack.) +*/ +#if !defined(LUAI_MAXCCALLS) +#define LUAI_MAXCCALLS 200 +#endif + + /* ** macros that are executed whenever program enters the Lua core ** ('lua_lock') and leaves the core ('lua_unlock') diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 08b6bd216..a2910dfd5 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -21,10 +21,12 @@ */ #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ + /* -** number of all possible types (including LUA_TNONE) +** number of all possible types (including LUA_TNONE but excluding DEADKEY) */ #define LUA_TOTALTYPES (LUA_TPROTO + 2) @@ -556,7 +558,7 @@ typedef struct Proto { /* ** {================================================================== -** Closures +** Functions ** =================================================================== */ @@ -744,13 +746,13 @@ typedef struct Table { /* -** Use a "nil table" to mark dead keys in a table. Those keys serve -** to keep space for removed entries, which may still be part of -** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE -** set, so these values are considered not collectable and are different -** from any valid value. +** Dead keys in tables have the tag DEADKEY but keep their original +** gcvalue. This distinguishes them from regular keys but allows them to +** be found when searched in a special way. ('next' needs that to find +** keys removed from a table during a traversal.) */ -#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) +#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) +#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) /* }================================================================== */ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 122e5d21f..120cdd943 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -261,7 +261,7 @@ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ 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_LEN,/* A B R[A] := #R[B] (length operator) */ OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ @@ -297,7 +297,7 @@ OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ -OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ +OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index bc7d9a4f2..bcdcfb6d7 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -489,12 +489,10 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { } -/* -** Macros to limit the maximum recursion depth while parsing -*/ -#define enterlevel(ls) luaE_enterCcall((ls)->L) +#define enterlevel(ls) luaE_incCstack(ls->L) + -#define leavelevel(ls) luaE_exitCcall((ls)->L) +#define leavelevel(ls) ((ls)->L->nCcalls--) /* diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 4f6bf8bf7..f7a17ca0f 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -76,7 +76,7 @@ static unsigned int luai_makeseed (lua_State *L) { addbuff(buff, p, &h); /* local variable */ addbuff(buff, p, &lua_newstate); /* public function */ lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h, 1); + return luaS_hash(buff, p, h); } #endif @@ -97,66 +97,14 @@ void luaE_setdebt (global_State *g, l_mem debt) { LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { - global_State *g = G(L); - int ccalls; - luaE_freeCI(L); /* release unused CIs */ - ccalls = getCcalls(L); - if (limit >= 40000) - return 0; /* out of bounds */ - limit += CSTACKERR; - if (L != g-> mainthread) - return 0; /* only main thread can change the C stack */ - else if (ccalls <= CSTACKERR) - return 0; /* handling overflow */ - else { - int diff = limit - g->Cstacklimit; - if (ccalls + diff <= CSTACKERR) - return 0; /* new limit would cause an overflow */ - g->Cstacklimit = limit; /* set new limit */ - L->nCcalls += diff; /* correct 'nCcalls' */ - return limit - diff - CSTACKERR; /* success; return previous limit */ - } -} - - -/* -** Decrement count of "C calls" and check for overflows. In case of -** a stack overflow, check appropriate error ("regular" overflow or -** overflow while handling stack overflow). If 'nCcalls' is smaller -** than CSTACKERR but larger than CSTACKMARK, it means it has just -** entered the "overflow zone", so the function raises an overflow -** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is -** already handling an overflow) but larger than CSTACKERRMARK, does -** not report an error (to allow message handling to work). Otherwise, -** report a stack overflow while handling a stack overflow (probably -** caused by a repeating error in the message handling function). -*/ - -void luaE_enterCcall (lua_State *L) { - int ncalls = getCcalls(L); - L->nCcalls--; - if (ncalls <= CSTACKERR) { /* possible overflow? */ - luaE_freeCI(L); /* release unused CIs */ - ncalls = getCcalls(L); /* update call count */ - if (ncalls <= CSTACKERR) { /* still overflow? */ - if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */ - luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ - else if (ncalls >= CSTACKMARK) { - /* not in error-handling zone; raise the error now */ - L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ - luaG_runerror(L, "C stack overflow"); - } - /* else stack is in the error-handling zone; - allow message handler to work */ - } - } + UNUSED(L); UNUSED(limit); + return LUAI_MAXCCALLS; /* warning?? */ } CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; lua_assert(L->ci->next == NULL); - luaE_enterCcall(L); ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; @@ -175,13 +123,11 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; - L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } - L->nCcalls -= L->nci; /* adjust result */ } @@ -194,7 +140,6 @@ void luaE_shrinkCI (lua_State *L) { CallInfo *next; if (ci == NULL) return; /* no extra elements */ - L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((next = ci->next) != NULL) { /* two extra elements? */ CallInfo *next2 = next->next; /* next's next */ ci->next = next2; /* remove next from the list */ @@ -207,19 +152,39 @@ void luaE_shrinkCI (lua_State *L) { ci = next2; /* continue */ } } - L->nCcalls -= L->nci; /* adjust result */ +} + + +/* +** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. +** If equal, raises an overflow error. If value is larger than +** LUAI_MAXCCALLS (which means it is handling an overflow) but +** not much larger, does not report an error (to allow overflow +** handling to work). +*/ +void luaE_checkcstack (lua_State *L) { + if (getCcalls(L) == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ +} + + +LUAI_FUNC void luaE_incCstack (lua_State *L) { + L->nCcalls++; + if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + luaE_checkcstack(L); } static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); - L1->stacksize = BASIC_STACK_SIZE; + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); for (i = 0; i < BASIC_STACK_SIZE; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; - L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; + L1->stack_last = L1->stack + BASIC_STACK_SIZE; /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; @@ -240,7 +205,7 @@ static void freestack (lua_State *L) { 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 */ + luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */ } @@ -290,7 +255,6 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stack = NULL; L->ci = NULL; L->nci = 0; - L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; L->hook = NULL; @@ -335,7 +299,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); - L1->nCcalls = getCcalls(L); + L1->nCcalls = 0; L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; @@ -396,7 +360,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; + L->nCcalls = 0; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 89d43409b..b70564a06 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -87,49 +87,13 @@ /* -** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of -** how many "C calls" it still can do in the C stack, to avoid C-stack -** overflow. This count is very rough approximation; it considers only -** recursive functions inside the interpreter, as non-recursive calls -** can be considered using a fixed (although unknown) amount of stack -** space. -** -** The count has two parts: the lower part is the count itself; the -** higher part counts the number of non-yieldable calls in the stack. -** (They are together so that we can change both with one instruction.) -** -** Because calls to external C functions can use an unknown amount -** of space (e.g., functions using an auxiliary buffer), calls -** to these functions add more than one to the count (see CSTACKCF). -** -** The proper count excludes the number of CallInfo structures allocated -** by Lua, as a kind of "potential" calls. So, when Lua calls a function -** (and "consumes" one CallInfo), it needs neither to decrement nor to -** check 'nCcalls', as its use of C stack is already accounted for. +** About 'nCcalls': This count has two parts: the lower 16 bits counts +** the number of recursive invocations in the C stack; the higher +** 16 bits counts the number of non-yieldable calls in the stack. +** (They are together so that we can change and save both with one +** instruction.) */ -/* number of "C stack slots" used by an external C function */ -#define CSTACKCF 10 - - -/* -** The C-stack size is sliced in the following zones: -** - larger than CSTACKERR: normal stack; -** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; -** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; -** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; -** (Because the counter can be decremented CSTACKCF at once, we need -** the so called "buffer zones", with at least that size, to properly -** detect a change from one zone to the next.) -*/ -#define CSTACKERR (8 * CSTACKCF) -#define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) -#define CSTACKERRMARK (CSTACKCF + 2) - - -/* initial limit for the C-stack of threads */ -#define CSTACKTHREAD (2 * CSTACKERR) - /* true if this thread does not have non-yieldable calls in the stack */ #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) @@ -144,13 +108,8 @@ /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) -/* Increment the number of non-yieldable calls and decrement nCcalls */ -#define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) - -/* Decrement the number of non-yieldable calls and increment nCcalls */ -#define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) - - +/* Non-yieldable call increment */ +#define nyci (0x10000 | 1) @@ -168,12 +127,20 @@ struct lua_longjmp; /* defined in ldo.c */ #endif -/* extra stack space to handle TM calls and some other extras */ +/* +** Extra stack space to handle TM calls and some other extras. This +** space is not included in 'stack_last'. It is used only to avoid stack +** checks, either because the element will be promptly popped or because +** there will be a stack check soon after the push. Function frames +** never use this extra space, so it does not need to be kept clean. +*/ #define EXTRA_STACK 5 #define BASIC_STACK_SIZE (2*LUA_MINSTACK) +#define stacksize(th) cast_int((th)->stack_last - (th)->stack) + /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ @@ -224,14 +191,15 @@ typedef struct CallInfo { */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_C (1<<1) /* call is running a C function */ -#define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<4) /* call was tail called */ -#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ -#define CIST_FIN (1<<6) /* call is running a finalizer */ -#define CIST_TRAN (1<<7) /* 'ci' has transfer information */ +#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ +#define CIST_HOOKED (1<<3) /* call is running a debug hook */ +#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_FIN (1<<7) /* call is running a finalizer */ +#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<8) /* using __lt for __le */ +#define CIST_LEQ (1<<9) /* using __lt for __le */ #endif /* active function is a Lua function */ @@ -295,7 +263,6 @@ typedef struct global_State { TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ - unsigned int Cstacklimit; /* current limit for the C stack */ } global_State; @@ -310,7 +277,7 @@ struct lua_State { StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - StkId stack_last; /* last free slot in the stack */ + StkId stack_last; /* end of stack (last element + 1) */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ GCObject *gclist; @@ -319,9 +286,8 @@ struct lua_State { CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ + l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ int oldpc; /* last pc traced */ - int stacksize; int basehookcount; int hookcount; volatile l_signalT hookmask; @@ -388,12 +354,11 @@ 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); -LUAI_FUNC void luaE_enterCcall (lua_State *L); +LUAI_FUNC void luaE_checkcstack (lua_State *L); +LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); -#define luaE_exitCcall(L) ((L)->nCcalls++) - #endif diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 48ef659d3..973d96c2c 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -25,16 +25,6 @@ static unsigned int STRSEED; static size_t STRID = 0; -/* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to -** compute its hash -*/ -#if !defined(LUAI_HASHLIMIT) -#define LUAI_HASHLIMIT 5 -#endif - - - /* ** Maximum size for string table. */ @@ -73,10 +63,9 @@ void luaS_share (TString *ts) { ts->id = ATOM_DEC(&STRID); } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed, - size_t step) { +unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast_uint(l); - for (; l >= step; l -= step) + for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } @@ -86,8 +75,7 @@ unsigned int luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; - size_t step = (len >> LUAI_HASHLIMIT) + 1; - ts->hash = luaS_hash(getstr(ts), len, ts->hash, step); + ts->hash = luaS_hash(getstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -243,7 +231,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString *ts; global_State *g = G(L); stringtable *tb = &g->strt; - unsigned int h = luaS_hash(str, l, STRSEED, 1); + unsigned int h = luaS_hash(str, l, STRSEED); TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 600e53eb1..20cbf395b 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -41,8 +41,7 @@ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b) || \ ( ((a)->id == (b)->id) ? ((a)->id != 0) : ((a)->hash == (b)->hash && luaS_eqshrstr(a,b)) ) ) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, - unsigned int seed, size_t step); +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 int luaS_eqshrstr (TString *a, TString *b); diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 2ba8bde47..940a14ca5 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1365,7 +1365,6 @@ typedef union Ftypes { float f; double d; lua_Number n; - char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ } Ftypes; @@ -1535,12 +1534,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n, ** 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, +static void copywithendian (char *dest, const char *src, int size, int islittle) { - if (islittle == nativeendian.little) { - while (size-- != 0) - *(dest++) = *(src++); - } + if (islittle == nativeendian.little) + memcpy(dest, src, size); else { dest += size - 1; while (size-- != 0) @@ -1584,14 +1581,14 @@ static int str_pack (lua_State *L) { break; } case Kfloat: { /* floating-point options */ - volatile Ftypes u; + 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); + copywithendian(buff, (char *)&u, size, h.islittle); luaL_addsize(&b, size); break; } @@ -1717,9 +1714,9 @@ static int str_unpack (lua_State *L) { break; } case Kfloat: { - volatile Ftypes u; + Ftypes u; lua_Number num; - copywithendian(u.buff, data + pos, size, h.islittle); + copywithendian((char *)&u, 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; @@ -1738,7 +1735,7 @@ static int str_unpack (lua_State *L) { break; } case Kzstr: { - size_t len = (int)strlen(data + pos); + size_t len = strlen(data + pos); luaL_argcheck(L, pos + len < ld, 2, "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 45692f4ac..ecfd595a2 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -172,11 +172,17 @@ static Node *mainpositionTV (const Table *t, const TValue *key) { ** be equal to floats. It is assumed that 'eqshrstr' is simply ** pointer equality, so that short strings are handled in the ** default case. -*/ -static int equalkey (const TValue *k1, const Node *n2) { - if (rawtt(k1) != keytt(n2)) /* not the same variants? */ +** A true 'deadok' means to accept dead keys as equal to their original +** values, which can only happen if the original key was collectable. +** All dead values are compared in the default case, by pointer +** identity. (Note that dead long strings are also compared by +** identity). +*/ +static int equalkey (const TValue *k1, const Node *n2, int deadok) { + if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ + !(deadok && keyisdead(n2) && iscollectable(k1))) return 0; /* cannot be same key */ - switch (ttypetag(k1)) { + switch (keytt(n2)) { case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; case LUA_VNUMINT: @@ -187,8 +193,10 @@ static int equalkey (const TValue *k1, const Node *n2) { return pvalue(k1) == pvalueraw(keyval(n2)); case LUA_VLCF: return fvalue(k1) == fvalueraw(keyval(n2)); - case LUA_VLNGSTR: + case ctb(LUA_VLNGSTR): return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); + case ctb(LUA_VSHRSTR): + return eqshrstr(tsvalue(k1), keystrval(n2)); default: return gcvalue(k1) == gcvalueraw(keyval(n2)); } @@ -251,11 +259,12 @@ static unsigned int setlimittosize (Table *t) { /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) +** See explanation about 'deadok' in function 'equalkey'. */ -static const TValue *getgeneric (Table *t, const TValue *key) { +static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (equalkey(key, n)) + if (equalkey(key, n, deadok)) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -292,7 +301,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, if (i - 1u < asize) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { - const TValue *n = getgeneric(t, key); + const TValue *n = getgeneric(t, key, 1); if (unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ @@ -730,7 +739,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { else { /* for long strings, use generic case */ TValue ko; setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko); + return getgeneric(t, &ko, 0); } } @@ -750,7 +759,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { /* else... */ } /* FALLTHROUGH */ default: - return getgeneric(t, key); + return getgeneric(t, key, 0); } } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index a88db558a..aa2763450 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,7 +18,7 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "1" +#define LUA_VERSION_RELEASE "2" #define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index bdf927e77..d9cf18ca1 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -36,21 +36,6 @@ ** ===================================================================== */ -/* -@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and -** also limits the maximum depth of other recursive algorithms in -** the implementation, such as syntactic analysis. A value too -** large may allow the interpreter to crash (C-stack overflow). -** The default value seems ok for regular machines, but may be -** too high for restricted hardware. -** The test file 'cstack.lua' may help finding a good limit. -** (It will crash with a limit too high.) -*/ -#if !defined(LUAI_MAXCSTACK) -#define LUAI_MAXCSTACK 2000 -#endif - - /* @@ 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 diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 3d4fd46c3..24cc81554 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -229,7 +229,7 @@ static int forprep (lua_State *L, StkId ra) { count /= l_castS2U(-(step + 1)) + 1u; } /* store the counter in place of the limit (which won't be - needed anymore */ + needed anymore) */ setivalue(plimit, l_castU2S(count)); } } @@ -1134,7 +1134,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif - tailcall: + execute: trap = L->hookmask; cl = clLvalue(s2v(ci->func)); k = cl->p->k; @@ -1153,7 +1153,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId ra; /* instruction's A register */ vmfetch(); lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { @@ -1608,18 +1608,26 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CALL) { + CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - ProtectNT(luaD_call(L, ra, nresults)); + savepc(L); /* in case of errors */ + if ((newci = luaD_precall(L, ra, nresults)) == NULL) + updatetrap(ci); /* C call; nothing else to be done */ + else { /* Lua call: run function in this same invocation */ + ci = newci; + ci->callstatus = 0; /* call re-uses 'luaV_execute' */ + goto execute; + } vmbreak; } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ int nparams1 = GETARG_C(i); - /* delat is virtual 'func' - real 'func' (vararg functions) */ + /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top = ra + b; @@ -1639,16 +1647,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { checkstackGCp(L, 1, ra); } if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_call(L, ra, LUA_MULTRET); /* call it */ + luaD_precall(L, ra, LUA_MULTRET); /* call it */ updatetrap(ci); updatestack(ci); /* stack may have been relocated */ - ci->func -= delta; - luaD_poscall(L, ci, cast_int(L->top - ra)); - return; + ci->func -= delta; /* restore 'func' (if vararg) */ + luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + goto ret; /* caller returns after the tail call */ } - ci->func -= delta; + ci->func -= delta; /* restore 'func' (if vararg) */ luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto tailcall; + goto execute; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ @@ -1667,7 +1675,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ci->func -= ci->u.l.nextraargs + nparams1; L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); - return; + goto ret; } vmcase(OP_RETURN0) { if (L->hookmask) { @@ -1681,7 +1689,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { while (nres-- > 0) setnilvalue(s2v(L->top++)); /* all results are nil */ } - return; + goto ret; } vmcase(OP_RETURN1) { if (L->hookmask) { @@ -1700,7 +1708,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setnilvalue(s2v(L->top++)); } } - return; + ret: + if (ci->callstatus & CIST_FRESH) + return; /* end this frame */ + else { + ci = ci->previous; + goto execute; /* continue running caller in this frame */ + } } vmcase(OP_FORLOOP) { if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ From 394a065ea27f222e8a54f57ff3ca911fab9302d4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 16 Oct 2020 11:49:28 +0800 Subject: [PATCH 272/565] move yield from suspend to pause, see #1248 --- lualib/skynet/socket.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 3f99e9ef1..330330e7d 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -37,13 +37,13 @@ local function pause_socket(s, size) end driver.pause(s.id) s.pause = true + skynet.yield() -- there are subsequent socket messages in mqueue, maybe. end local function suspend(s) assert(not s.co) s.co = coroutine.running() if s.pause then - skynet.yield() -- there are subsequent socket messages in mqueue, maybe. skynet.error(string.format("Resume socket (%d)", s.id)) driver.start(s.id) skynet.wait(s.co) @@ -75,10 +75,10 @@ socket_message[1] = function(id, size, data) -- read size if sz >= rr then s.read_required = nil - wakeup(s) if sz > BUFFER_LIMIT then pause_socket(s, sz) end + wakeup(s) end else if s.buffer_limit and sz > s.buffer_limit then @@ -90,10 +90,10 @@ socket_message[1] = function(id, size, data) -- read line if driver.readline(s.buffer,nil,rr) then s.read_required = nil - wakeup(s) if sz > BUFFER_LIMIT then pause_socket(s, sz) end + wakeup(s) end elseif sz > BUFFER_LIMIT and not s.pause then pause_socket(s, sz) From 8a7c2b41a1ebbcb42def1989cf560d510f78ee86 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 16 Oct 2020 12:04:40 +0800 Subject: [PATCH 273/565] luaS_hash changed --- 3rd/jemalloc | 2 +- 3rd/lua/lstring.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index b0b3e49a5..ea6b3e973 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit b0b3e49a54ec29e32636f4577d9d5a896d67fd20 +Subproject commit ea6b3e973b477b8061e0076bb257dbd7f3faa756 diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 973d96c2c..935e96a30 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -150,7 +150,7 @@ static unsigned int luai_makeseed(lua_State *L) { buff[1] = cast(size_t, &STRSEED); buff[2] = cast(size_t, &luai_makeseed); buff[3] = cast(size_t, L); - return luaS_hash((const char*)buff, sizeof(buff), h, 1); + return luaS_hash((const char*)buff, sizeof(buff), h); } #endif From 1dfddc0d848d82cbcf756c21e79487e50d4e0873 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 16 Oct 2020 13:06:01 +0800 Subject: [PATCH 274/565] add reading and writing into netstat --- lualib-src/lua-socket.c | 4 ++++ skynet-src/socket_info.h | 2 ++ skynet-src/socket_server.c | 2 ++ 3 files changed, 8 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 593dd7ada..0da565a92 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -758,6 +758,10 @@ getinfo(lua_State *L, struct socket_info *si) { lua_setfield(L, -2, "rtime"); lua_pushinteger(L, si->wtime); lua_setfield(L, -2, "wtime"); + lua_pushboolean(L, si->reading); + lua_setfield(L, -2, "reading"); + lua_pushboolean(L, si->writing); + lua_setfield(L, -2, "writing"); if (si->name[0]) { lua_pushstring(L, si->name); lua_setfield(L, -2, "peer"); diff --git a/skynet-src/socket_info.h b/skynet-src/socket_info.h index 78facf6c2..739536e88 100644 --- a/skynet-src/socket_info.h +++ b/skynet-src/socket_info.h @@ -18,6 +18,8 @@ struct socket_info { uint64_t rtime; uint64_t wtime; int64_t wbuffer; + uint8_t reading; + uint8_t writing; char name[128]; struct socket_info *next; }; diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f317ecc83..7bd50dfcb 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2185,6 +2185,8 @@ query_info(struct socket *s, struct socket_info *si) { si->rtime = s->stat.rtime; si->wtime = s->stat.wtime; si->wbuffer = s->wb_size; + si->reading = s->reading; + si->writing = s->writing; return 1; } From 6c3e969e913e2e38c112f7eaded3d497e0eaa8b0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 19 Oct 2020 10:55:32 +0800 Subject: [PATCH 275/565] shutdown read while halfclose --- skynet-src/socket_server.c | 1 + 1 file changed, 1 insertion(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 7bd50dfcb..6fd975a9b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1082,6 +1082,7 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc return SOCKET_CLOSE; } s->type = SOCKET_TYPE_HALFCLOSE; + shutdown(s->fd, SHUT_RD); return -1; } From 1e945a1dec0d97efe55f80fa8170fc48c3cfb0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Fri, 23 Oct 2020 14:42:31 +0800 Subject: [PATCH 276/565] fix sharetable update and tail call (#1253) Co-authored-by: zixun --- lualib/skynet/sharetable.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index e80d39290..8f0666bbf 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -265,7 +265,7 @@ local function resolve_replace(replace_map) local f = match[tv] if f then record_map[v] = true - f(v) + return f(v) end end @@ -277,7 +277,7 @@ local function resolve_replace(replace_map) nv = getnv(mt) debug.setmetatable(v, nv) else - match_value(mt) + return match_value(mt) end end end @@ -294,7 +294,7 @@ local function resolve_replace(replace_map) for _,v in pairs(internal_types) do match_mt(v) end - match_mt(nil) + return match_mt(nil) end @@ -331,7 +331,7 @@ local function resolve_replace(replace_map) end end end - match_mt(t) + return match_mt(t) end local function match_userdata(u) @@ -341,7 +341,7 @@ local function resolve_replace(replace_map) nv = getnv(uv) setuservalue(u, nv) end - match_mt(u) + return match_mt(u) end local function match_funcinfo(info) @@ -381,7 +381,7 @@ local function resolve_replace(replace_map) local function match_function(f) local info = getinfo(f, "uf") - match_funcinfo(info) + return match_funcinfo(info) end local function match_thread(co, level) @@ -393,13 +393,14 @@ local function resolve_replace(replace_map) match_value(v) end + local uplevel = co == coroutine.running() and 1 or 0 level = level or 1 while true do local info = getinfo(co, level, "uf") if not info then break end - info.level = level + info.level = level + uplevel info.curco = co match_funcinfo(info) level = level + 1 @@ -412,6 +413,7 @@ local function resolve_replace(replace_map) record_map[match] = true record_map[RECORD] = true record_map[record_map] = true + record_map[replace_map] = true record_map[insert_replace] = true record_map[resolve_replace] = true assert(getinfo(co, 3, "f").func == sharetable.update) @@ -449,7 +451,7 @@ function sharetable.update(...) end if next(replace_map) then - resolve_replace(replace_map) + resolve_replace(replace_map) end end From a6adc6909024b828aa44b6ebe4134a66865e7db0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 Oct 2020 18:21:16 +0800 Subject: [PATCH 277/565] Use atomic instruct to avoid concurrence signal 0 --- service-src/service_snlua.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 643d3a4d5..240aeb7cb 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -1,4 +1,5 @@ #include "skynet.h" +#include "atomic.h" #include #include @@ -87,6 +88,10 @@ lua_resumeX(lua_State *L, lua_State *from, int nargs, int *nresults) { struct snlua *l = (struct snlua *)ud; switchL(L, l); int err = lua_resume(L, from, nargs, nresults); + if (l->trap) { + // wait for lua_sethook. (l->trap == -1) + while (l->trap >= 0) ; + } switchL(from, l); return err; } @@ -516,8 +521,12 @@ snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { if (l->trap == 0) { - l->trap = 1; + // only one thread can set trap ( l->trap 0->1 ) + if (!ATOM_CAS(&l->trap, 0, 1)) + return; lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); + // set finish ( l->trap 1 -> -1 ) + l->trap = -1; } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); From 80b60f23c912c4f1552a7f2dc89d222ac0252261 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 Oct 2020 19:20:31 +0800 Subject: [PATCH 278/565] use CAS to finish set --- service-src/service_snlua.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 240aeb7cb..3ef4f0e7f 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -525,8 +525,8 @@ snlua_signal(struct snlua *l, int signal) { if (!ATOM_CAS(&l->trap, 0, 1)) return; lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); - // set finish ( l->trap 1 -> -1 ) - l->trap = -1; + // finish set ( l->trap 1 -> -1 ) + ATOM_CAS(&l->trap, 1, -1); } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); From 84e600bed3cd999568206808262e2b83b8bbdeb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Thu, 29 Oct 2020 14:05:16 +0800 Subject: [PATCH 279/565] #855 add test runCommond for testmongodb (#1255) --- test/testmongodb.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index c588f9eb1..54d5c265e 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -108,6 +108,37 @@ function test_find_and_remove() assert(ret == nil) end +function test_runcommand() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:dropIndex("*") + db.testcoll:drop() + + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) + assert(ok and ret and ret.n == 1, err) + + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) + assert(ok and ret and ret.n == 1, err) + + ok, err, ret = db.testcoll:safe_insert({test_key = 2, test_key2 = 3}) + assert(ok and ret and ret.n == 1, err) + + local pipeline = { + { + ["$group"] = { + _id = mongo.null, + test_key_total = { ["$sum"] = "$test_key"}, + test_key2_total = { ["$sum"] = "$test_key2" }, + } + } + } + ret = db:runCommand("aggregate", "testcoll", "pipeline", pipeline, "cursor", {}) + assert(ret and ret.cursor.firstBatch[1].test_key_total == 4) + assert(ret and ret.cursor.firstBatch[1].test_key2_total == 6) +end + function test_expire_index() local ok, err, ret local c = _create_client() @@ -148,6 +179,8 @@ skynet.start(function() test_insert_with_index() print("Test find and remove") test_find_and_remove() + print("Test runCommand") + test_runcommand() print("Test expire index") test_expire_index() print("mongodb test finish."); From 3f8a2f69dfc2dc2eee97b28f306833c2590774ac Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Nov 2020 10:18:33 +0800 Subject: [PATCH 280/565] update lua 5.4.2 --- 3rd/lua/lcode.c | 14 +++++++++----- 3rd/lua/ldo.c | 16 ++++++++-------- 3rd/lua/llex.c | 25 +++++++++++++------------ 3rd/lua/llimits.h | 2 +- 3rd/lua/lparser.c | 8 ++++---- 3rd/lua/lparser.h | 5 +++-- 3rd/lua/ltable.c | 27 +++++++++++++++++---------- 3rd/lua/lua.c | 16 ++++++++++------ 3rd/lua/lvm.c | 45 +++++++++++++++++++++++++-------------------- 9 files changed, 90 insertions(+), 68 deletions(-) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 6f241c947..14d41f1a7 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -753,7 +753,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { /* -** Ensure that expression 'e' is not a variable (nor a constant). +** Ensure that expression 'e' is not a variable (nor a ). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { @@ -805,8 +805,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { /* -** Ensures expression value is in register 'reg' (and therefore -** 'e' will become a non-relocatable expression). +** Ensure expression value is in register 'reg', making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { @@ -860,7 +860,8 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { /* -** Ensures expression value is in any register. +** Ensure expression value is in a register, making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { @@ -946,8 +947,11 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } + /* else expression has jumps and cannot change its register + to hold the jump values, because it is a local variable. + Go through to the default case. */ } - luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ + luaK_exp2nextreg(fs, e); /* default: use next available register */ return e->u.info; } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 5729b1902..a60972b20 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -534,11 +534,11 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { /* -** Call a function (C or Lua). 'inc' can be 1 (increment number -** of recursive invocations in the C stack) or nyci (the same plus -** increment number of non-yieldable calls). +** Call a function (C or Lua) through C. 'inc' can be 1 (increment +** number of recursive invocations in the C stack) or nyci (the same +** plus increment number of non-yieldable calls). */ -static void docall (lua_State *L, StkId func, int nResults, int inc) { +static void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) @@ -552,10 +552,10 @@ static void docall (lua_State *L, StkId func, int nResults, int inc) { /* -** External interface for 'docall' +** External interface for 'ccall' */ void luaD_call (lua_State *L, StkId func, int nResults) { - return docall(L, func, nResults, 1); + ccall(L, func, nResults, 1); } @@ -563,7 +563,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) { ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - return docall(L, func, nResults, nyci); + ccall(L, func, nResults, nyci); } @@ -678,7 +678,7 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ - docall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ + ccall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 3d6b2b97a..4b8dec998 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -254,9 +254,10 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** reads a sequence '[=*[' or ']=*]', leaving the last bracket. -** If sequence is well formed, return its number of '='s + 2; otherwise, -** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). +** read a sequence '[=*[' or ']=*]', leaving the last bracket. If +** sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if it is a single bracket (no '='s and no 2nd bracket); +** otherwise (an unfinished '[==...') return 0. */ static size_t skip_sep (LexState *ls) { size_t count = 0; @@ -481,34 +482,34 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '=': { next(ls); - if (check_next1(ls, '=')) return TK_EQ; + if (check_next1(ls, '=')) return TK_EQ; /* '==' */ else return '='; } case '<': { next(ls); - if (check_next1(ls, '=')) return TK_LE; - else if (check_next1(ls, '<')) return TK_SHL; + if (check_next1(ls, '=')) return TK_LE; /* '<=' */ + else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ else return '<'; } case '>': { next(ls); - if (check_next1(ls, '=')) return TK_GE; - else if (check_next1(ls, '>')) return TK_SHR; + 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; + if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ else return '/'; } case '~': { next(ls); - if (check_next1(ls, '=')) return TK_NE; + if (check_next1(ls, '=')) return TK_NE; /* '~=' */ else return '~'; } case ':': { next(ls); - if (check_next1(ls, ':')) return TK_DBCOLON; + if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ else return ':'; } case '"': case '\'': { /* short literal strings */ @@ -547,7 +548,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { return TK_NAME; } } - else { /* single-char tokens (+ - / ...) */ + else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ int c = ls->current; next(ls); return c; diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index d6866d7c1..a76c13ed6 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -355,7 +355,7 @@ typedef l_uint32 Instruction; #else /* realloc stack keeping its size */ #define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } + { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } #endif #if !defined(HARDMEMTESTS) diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index bcdcfb6d7..fb7a1264e 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -945,7 +945,7 @@ static void setvararg (FuncState *fs, int nparams) { static void parlist (LexState *ls) { - /* parlist -> [ param { ',' param } ] */ + /* parlist -> [ {NAME ','} (NAME | '...') ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; @@ -953,12 +953,12 @@ static void parlist (LexState *ls) { if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ + case TK_NAME: { new_localvar(ls, str_checkname(ls)); nparams++; break; } - case TK_DOTS: { /* param -> '...' */ + case TK_DOTS: { luaX_next(ls); isvararg = 1; break; @@ -1752,7 +1752,7 @@ static void checktoclose (LexState *ls, int level) { static void localstat (LexState *ls) { - /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ + /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */ FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 618cb0106..2e6dae72f 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -23,7 +23,7 @@ /* kinds of variables/expressions */ typedef enum { - VVOID, /* when 'expdesc' describes the last expression a list, + VVOID, /* when 'expdesc' describes the last expression of a list, this kind means an empty list (so, no expression) */ VNIL, /* constant nil */ VTRUE, /* constant true */ @@ -38,7 +38,8 @@ typedef enum { VLOCAL, /* local variable; var.sidx = stack index (local register); var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ - VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ + VCONST, /* compile-time variable; + info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index ecfd595a2..b3840e77d 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -166,17 +166,24 @@ static Node *mainpositionTV (const Table *t, const TValue *key) { /* -** Check whether key 'k1' is equal to the key in node 'n2'. -** This equality is raw, so there are no metamethods. Floats -** with integer values have been normalized, so integers cannot -** be equal to floats. It is assumed that 'eqshrstr' is simply -** pointer equality, so that short strings are handled in the -** default case. +** Check whether key 'k1' is equal to the key in node 'n2'. This +** equality is raw, so there are no metamethods. Floats with integer +** values have been normalized, so integers cannot be equal to +** floats. It is assumed that 'eqshrstr' is simply pointer equality, so +** that short strings are handled in the default case. ** A true 'deadok' means to accept dead keys as equal to their original -** values, which can only happen if the original key was collectable. -** All dead values are compared in the default case, by pointer -** identity. (Note that dead long strings are also compared by -** identity). +** values. All dead keys are compared in the default case, by pointer +** identity. (Only collectable objects can produce dead keys.) Note that +** dead long strings are also compared by identity. +** Once a key is dead, its corresponding value may be collected, and +** then another value can be created with the same address. If this +** other value is given to 'next', 'equalkey' will signal a false +** positive. In a regular traversal, this situation should never happen, +** as all keys given to 'next' came from the table itself, and therefore +** could not have been collected. Outside a regular traversal, we +** have garbage in, garbage out. What is relevant is that this false +** positive does not break anything. (In particular, 'next' will return +** some other valid item on the table or nil.) */ static int equalkey (const TValue *k1, const Node *n2, int deadok) { if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 454ce12f3..b5b884b6d 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -416,14 +416,18 @@ static int handle_luainit (lua_State *L) { /* -** Returns the string to be used as a prompt by the interpreter. +** Return the string to be used as a prompt by the interpreter. Leave +** the string (or nil, if using the default value) on the stack, to keep +** it anchored. */ static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - return p; + if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) + return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ + else { /* apply 'tostring' over the value */ + const char *p = luaL_tolstring(L, -1, NULL); + lua_remove(L, -2); /* remove original value */ + return p; + } } /* mark in error messages for incomplete statements */ diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 24cc81554..e6e1033a3 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1094,15 +1094,11 @@ void luaV_finishOp (lua_State *L) { #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that will finish the loop (returns) or can only raise -** errors. (That is, it will not return to the interpreter main loop -** after changing the stack or hooks.) +** Protect code that can only raise errors. (That is, it cannnot change +** the stack or hooks.) */ #define halfProtect(exp) (savestate(L,ci), (exp)) -/* idem, but without changing the stack */ -#define halfProtectNT(exp) (savepc(L), (exp)) - /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ { luaC_condGC(L, (savepc(L), L->top = (c)), \ @@ -1134,17 +1130,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif - execute: + startfunc: trap = L->hookmask; + returning: /* trap already set */ cl = clLvalue(s2v(ci->func)); k = cl->p->k; pc = ci->u.l.savedpc; if (trap) { - if (cl->p->is_vararg) - trap = 0; /* hooks will start after VARARGPREP instruction */ - else if (pc == cl->p->code) /* first instruction (not resuming)? */ - luaD_hookcall(L, ci); - ci->u.l.trap = 1; /* there may be other hooks */ + if (pc == cl->p->code) { /* first instruction (not resuming)? */ + if (cl->p->is_vararg) + trap = 0; /* hooks will start after VARARGPREP instruction */ + else /* check 'call' hook */ + luaD_hookcall(L, ci); + } + ci->u.l.trap = 1; /* assume trap is on, for now */ } base = ci->func + 1; /* main loop of interpreter */ @@ -1617,10 +1616,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { savepc(L); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ - else { /* Lua call: run function in this same invocation */ + else { /* Lua call: run function in this same C frame */ ci = newci; ci->callstatus = 0; /* call re-uses 'luaV_execute' */ - goto execute; + goto startfunc; } vmbreak; } @@ -1633,7 +1632,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); - savepc(ci); /* some calls here can raise errors */ + savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { /* close upvalues from current call; the compiler ensures that there are no to-be-closed variables here, so this @@ -1652,11 +1651,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatestack(ci); /* stack may have been relocated */ ci->func -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } ci->func -= delta; /* restore 'func' (if vararg) */ luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto execute; /* execute the callee */ + goto startfunc; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ @@ -1675,12 +1675,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ci->func -= ci->u.l.nextraargs + nparams1; L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); + updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; } vmcase(OP_RETURN0) { if (L->hookmask) { L->top = ra; - halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */ + savepc(ci); + luaD_poscall(L, ci, 0); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ int nres = ci->nresults; @@ -1694,7 +1697,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN1) { if (L->hookmask) { L->top = ra + 1; - halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */ + savepc(ci); + luaD_poscall(L, ci, 1); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ int nres = ci->nresults; @@ -1708,12 +1713,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setnilvalue(s2v(L->top++)); } } - ret: + ret: /* return from a Lua function */ if (ci->callstatus & CIST_FRESH) return; /* end this frame */ else { ci = ci->previous; - goto execute; /* continue running caller in this frame */ + goto returning; /* continue running caller in this frame */ } } vmcase(OP_FORLOOP) { From cb445f17d4c761e1f8d325fa40d2327400b97f6f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Nov 2020 10:20:26 +0800 Subject: [PATCH 281/565] bugfix --- 3rd/lua/ldo.c | 2 +- 3rd/lua/lstate.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index a60972b20..cb10dc869 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -191,7 +191,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize; lim++) + for (; lim < newsize + EXTRA_STACK; lim++) setnilvalue(s2v(newstack + lim)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index b70564a06..096bafd55 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -139,7 +139,7 @@ struct lua_longjmp; /* defined in ldo.c */ #define BASIC_STACK_SIZE (2*LUA_MINSTACK) -#define stacksize(th) cast_int((th)->stack_last - (th)->stack) +#define stacksize(th) cast_int((th)->stack_last + EXTRA_STACK - (th)->stack) /* kinds of Garbage Collection */ From 902fb94274b68856729c5a757d320daec60f1e01 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Nov 2020 10:47:14 +0800 Subject: [PATCH 282/565] Revert "bugfix" This reverts commit cb445f17d4c761e1f8d325fa40d2327400b97f6f. --- 3rd/lua/ldo.c | 2 +- 3rd/lua/lstate.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index cb10dc869..a60972b20 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -191,7 +191,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize + EXTRA_STACK; lim++) + for (; lim < newsize; lim++) setnilvalue(s2v(newstack + lim)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 096bafd55..b70564a06 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -139,7 +139,7 @@ struct lua_longjmp; /* defined in ldo.c */ #define BASIC_STACK_SIZE (2*LUA_MINSTACK) -#define stacksize(th) cast_int((th)->stack_last + EXTRA_STACK - (th)->stack) +#define stacksize(th) cast_int((th)->stack_last - (th)->stack) /* kinds of Garbage Collection */ From f27930e7f7a0f83b9aabbcfc3b1ee647edc52925 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Nov 2020 10:48:35 +0800 Subject: [PATCH 283/565] bugfix --- 3rd/lua/ldo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index a60972b20..b2c793996 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -191,7 +191,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize; lim++) + for (lim += EXTRA_STACK; lim < newsize + EXTRA_STACK; lim++) setnilvalue(s2v(newstack + lim)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; From 2a076d81927579943e23be3aecbb42c145119cad Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Nov 2020 11:24:53 +0800 Subject: [PATCH 284/565] init extra stack --- 3rd/lua/lstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index f7a17ca0f..477c0e6d3 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -181,7 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - for (i = 0; i < BASIC_STACK_SIZE; i++) + for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + BASIC_STACK_SIZE; From c7dc8751c7faf9ff04bf58f830397b8675adead9 Mon Sep 17 00:00:00 2001 From: t0350 Date: Sat, 14 Nov 2020 21:36:58 +0800 Subject: [PATCH 285/565] sync sproto weak type (#1259) --- lualib-src/sproto/lsproto.c | 66 ++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 370ef719d..605ec0c1f 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -81,6 +81,55 @@ lua_seti(lua_State *L, int index, lua_Integer n) { #endif +#if defined(SPROTO_WEAK_TYPE) +static int64_t +tointegerx (lua_State *L, int idx, int *isnum) { + int64_t v; + if (lua_isnumber(L, idx)) { + v = (int64_t)(round(lua_tonumber(L, idx))); + if (isnum) *isnum = 1; + return v; + } else { + return lua_tointegerx(L, idx, isnum); + } +} + +static int +tobooleanx (lua_State *L, int idx, int *isbool) { + if (isbool) *isbool = 1; + return lua_toboolean(L, idx); +} + +static const char * +tolstringx (lua_State *L, int idx, size_t *len, int *isstring) { + const char * str = luaL_tolstring(L, idx, len); // call metamethod, '__tostring' must return a string + if (isstring) { + *isstring = 1; + } + lua_pop(L, 1); + return str; +} + +#else +#define tointegerx(L, idx, isnum) lua_tointegerx((L), (idx), (isnum)) + +static int +tobooleanx (lua_State *L, int idx, int *isbool) { + if (isbool) *isbool = lua_isboolean(L, idx); + return lua_toboolean(L, idx); +} + +static const char * +tolstringx (lua_State *L, int idx, size_t *len, int *isstring) { + if (isstring) { + *isstring = (lua_type(L, idx) == LUA_TSTRING); + } + const char * str = lua_tolstring(L, idx, len); + return str; +} + +#endif + static int lnewproto(lua_State *L) { struct sproto * sp; @@ -243,7 +292,7 @@ encode_one(const struct sproto_arg *args, struct encode_ud *self) { // use 64bit integer for 32bit architecture. v = (int64_t)(round(vn * args->extra)); } else { - v = lua_tointegerx(L, -1, &isnum); + v = 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))); @@ -267,8 +316,9 @@ encode_one(const struct sproto_arg *args, struct encode_ud *self) { return 8; } case SPROTO_TBOOLEAN: { - int v = lua_toboolean(L, -1); - if (!lua_isboolean(L,-1)) { + int isbool; + int v = tobooleanx(L, -1, &isbool); + if (!isbool) { return luaL_error(L, ".%s[%d] is not a boolean (Is a %s)", args->tagname, args->index, lua_typename(L, lua_type(L, -1))); } @@ -278,12 +328,12 @@ encode_one(const struct sproto_arg *args, struct encode_ud *self) { } case SPROTO_TSTRING: { size_t sz = 0; - const char * str; - if (!lua_isstring(L, -1)) { + int isstring; + int type = lua_type(L, -1); // get the type firstly, lua_tolstring may convert value on stack to string + const char * str = tolstringx(L, -1, &sz, &isstring); + if (!isstring) { 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); + args->tagname, args->index, lua_typename(L, type)); } if (sz > args->length) return SPROTO_CB_ERROR; From 9eb37198ea3788ba2632abe4fd89b0717bc21a06 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Nov 2020 10:51:36 +0800 Subject: [PATCH 286/565] update lua 5.4.2 --- 3rd/lua/lauxlib.c | 72 +++++++++++++++++++++++++++++++---------------- 3rd/lua/ldo.c | 4 +-- 3rd/lua/lgc.c | 4 +-- 3rd/lua/llimits.h | 3 +- 3rd/lua/lobject.c | 2 +- 3rd/lua/lparser.c | 50 ++++---------------------------- HISTORY.md | 4 +-- README.md | 2 +- 8 files changed, 64 insertions(+), 77 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 16d16cf72..9bcab27da 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -283,10 +283,10 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { LUALIB_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; /* type of termination */ if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { + const char *what = "exit"; /* type of termination */ l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); @@ -1006,43 +1006,67 @@ static int panic (lua_State *L) { /* -** Emit a warning. '*warnstate' means: -** 0 - warning system is off; -** 1 - ready to start a new message; -** 2 - previous message is to be continued. +** Warning functions: +** warnfoff: warning system is off +** warnfon: ready to start a new message +** warnfcont: previous message is to be continued */ -static void warnf (void *ud, const char *message, int tocont) { - int *warnstate = (int *)ud; - if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ - if (strcmp(message, "@off") == 0) - *warnstate = 0; - else if (strcmp(message, "@on") == 0) - *warnstate = 1; - return; +static void warnfoff (void *ud, const char *message, int tocont); +static void warnfon (void *ud, const char *message, int tocont); +static void warnfcont (void *ud, const char *message, int tocont); + + +/* +** Check whether message is a control message. If so, execute the +** control or ignore it if unknown. +*/ +static int checkcontrol (lua_State *L, const char *message, int tocont) { + if (tocont || *(message++) != '@') /* not a control message? */ + return 0; + else { + if (strcmp(message, "off") == 0) + lua_setwarnf(L, warnfoff, L); /* turn warnings off */ + else if (strcmp(message, "on") == 0) + lua_setwarnf(L, warnfon, L); /* turn warnings on */ + return 1; /* it was a control message */ } - else if (*warnstate == 0) /* warnings off? */ - return; - if (*warnstate == 1) /* previous message was the last? */ - lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ +} + + +static void warnfoff (void *ud, const char *message, int tocont) { + checkcontrol((lua_State *)ud, message, tocont); +} + + +/* +** Writes the message and handle 'tocont', finishing the message +** if needed and setting the next warn function. +*/ +static void warnfcont (void *ud, const char *message, int tocont) { + lua_State *L = (lua_State *)ud; lua_writestringerror("%s", message); /* write message */ if (tocont) /* not the last part? */ - *warnstate = 2; /* to be continued */ + lua_setwarnf(L, warnfcont, L); /* to be continued */ else { /* last part */ lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ - *warnstate = 1; /* ready to start a new message */ + lua_setwarnf(L, warnfon, L); /* next call is a new message */ } } +static void warnfon (void *ud, const char *message, int tocont) { + if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ + return; /* nothing else to be done */ + lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ + warnfcont(ud, message, tocont); /* finish processing */ +} + + LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); if (L) { - int *warnstate; /* space for warning state */ lua_atpanic(L, &panic); - warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); - luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ - *warnstate = 0; /* default is warnings off */ - lua_setwarnf(L, warnf, warnstate); + lua_setwarnf(L, warnfoff, L); /* default is warnings off */ } return L; } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index b2c793996..4b55c31c2 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -191,8 +191,8 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { luaM_error(L); else return 0; /* do not raise an error */ } - for (lim += EXTRA_STACK; lim < newsize + EXTRA_STACK; lim++) - setnilvalue(s2v(newstack + lim)); /* erase new segment */ + for (; lim < newsize; lim++) + setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; L->stack_last = L->stack + newsize; diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index febc11de1..595b2772a 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -634,8 +634,8 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last; o++) /* clear not-marked stack slice */ - setnilvalue(s2v(o)); + for (; o < th->stack_last + EXTRA_STACK; o++) + setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index a76c13ed6..d03948314 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -326,7 +326,8 @@ typedef l_uint32 Instruction; /* exponentiation */ #if !defined(luai_numpow) -#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) +#define luai_numpow(L,a,b) \ + ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) #endif /* the others are quite standard operations */ diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index f8ea917a8..0e504be03 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -258,7 +258,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { if (endptr == NULL) { /* failed? may be a different locale */ char buff[L_MAXLENNUM + 1]; const char *pdot = strchr(s, '.'); - if (strlen(s) > L_MAXLENNUM || pdot == NULL) + if (pdot == NULL || strlen(s) > L_MAXLENNUM) return NULL; /* string too long or no dot; fail */ strcpy(buff, s); /* copy string to buffer */ buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index fb7a1264e..77813a74e 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1623,59 +1623,21 @@ static void forstat (LexState *ls, int line) { } -/* -** Check whether next instruction is a single jump (a 'break', a 'goto' -** to a forward label, or a 'goto' to a backward label with no variable -** to close). If so, set the name of the 'label' it is jumping to -** ("break" for a 'break') or to where it is jumping to ('target') and -** return true. If not a single jump, leave input unchanged, to be -** handled as a regular statement. -*/ -static int issinglejump (LexState *ls, TString **label, int *target) { - if (testnext(ls, TK_BREAK)) { /* a break? */ - *label = luaS_newliteral(ls->L, "break"); - return 1; - } - else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) - return 0; /* not a valid goto */ - else { - TString *lname = ls->lookahead.seminfo.ts; /* label's id */ - Labeldesc *lb = findlabel(ls, lname); - if (lb) { /* a backward jump? */ - /* does it need to close variables? */ - if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) - return 0; /* not a single jump; cannot optimize */ - *target = lb->pc; - } - else /* jump forward */ - *label = lname; - luaX_next(ls); /* skip goto */ - luaX_next(ls); /* skip name */ - return 1; - } -} - - static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ BlockCnt bl; - int line; FuncState *fs = ls->fs; - TString *jlb = NULL; - int target = NO_JUMP; expdesc v; int jf; /* instruction to skip 'then' code (if condition is false) */ luaX_next(ls); /* skip IF or ELSEIF */ expr(ls, &v); /* read condition */ checknext(ls, TK_THEN); - line = ls->linenumber; - if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ + if (ls->t.token == TK_BREAK) { /* 'if x then break' ? */ + int line = ls->linenumber; + luaK_goiffalse(ls->fs, &v); /* will jump if condition is true */ + luaX_next(ls); /* skip 'break' */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - if (jlb != NULL) /* forward jump? */ - newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ - else /* backward jump */ - luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ + newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t); while (testnext(ls, ';')) {} /* skip semicolons */ if (block_follow(ls, 0)) { /* jump is the entire block? */ leaveblock(fs); @@ -1684,7 +1646,7 @@ static void test_then_block (LexState *ls, int *escapelist) { else /* must skip over 'then' part if condition is false */ jf = luaK_jump(fs); } - else { /* regular case (not a jump) */ + else { /* regular case (not a break) */ luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ enterblock(fs, &bl, 0); jf = v.f; diff --git a/HISTORY.md b/HISTORY.md index d79086d1b..6c36e5386 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ -v1.4.0 (2020-10-10) +v1.4.0 (2020-11-16) ----------- -* Update Lua to 5.4.1 +* Update Lua to 5.4.2 * Add skynet.select * Improve mysql driver (@zero-rp @xiaojin @yxt945) * Improve websocket and ssl (@lvzixun) diff --git a/README.md b/README.md index ca483c7ed..55b291c4a 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.4.1 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.2 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. From 5248d443dcbb162f76c2b1de0d662d77a6ab1acc Mon Sep 17 00:00:00 2001 From: najoast Date: Tue, 24 Nov 2020 18:32:28 +0800 Subject: [PATCH 287/565] Fix typo: Causes an error when `resp` is false (#1260) Co-authored-by: zhouyan --- service/launcher.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/launcher.lua b/service/launcher.lua index d07d83248..7c2974770 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -32,9 +32,9 @@ local function list_srv(ti, fmt_func, ...) sessions[r] = addr end for req, resp in req:select(ti) do - local stat = resp[1] local addr = req[1] if resp then + local stat = resp[1] list[skynet.address(addr)] = fmt_func(stat, addr) else list[skynet.address(addr)] = fmt_func("ERROR", addr) From 693b962b43b33e752d4a4eb49595802cc6897cd8 Mon Sep 17 00:00:00 2001 From: hong Date: Tue, 1 Dec 2020 11:51:35 +0800 Subject: [PATCH 288/565] to-be-closed (#1261) --- lualib/skynet.lua | 16 +++++++++++++- test/testtobeclosed.lua | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/testtobeclosed.lua diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d571ea6fe..613826d70 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -14,12 +14,20 @@ local tunpack = table.unpack local traceback = debug.traceback local cresume = coroutine.resume +local coroutine_close = coroutine.close local running_thread = nil local init_thread = nil +local function coroutine_ret(co, ok, ...) + if not ok then + coroutine_close(co) + end + return ok, ... +end + local function coroutine_resume(co, ...) running_thread = co - return cresume(co, ...) + return coroutine_ret(co, cresume(co, ...)) end local coroutine_yield = coroutine.yield local coroutine_create = coroutine.create @@ -329,6 +337,7 @@ function suspend(co, result, command) if command == "SUSPEND" then return dispatch_wakeup() elseif command == "QUIT" then + coroutine_close(co) -- service exit return elseif command == "USER" then @@ -523,6 +532,11 @@ function skynet.exit() c.send(address, skynet.PTYPE_ERROR, session, "") end end + for session, co in pairs(session_id_coroutine) do + if type(co) == "thread" then + coroutine_close(co) + end + end for resp in pairs(unresponse) do resp(false) end diff --git a/test/testtobeclosed.lua b/test/testtobeclosed.lua new file mode 100644 index 000000000..59b9cbe08 --- /dev/null +++ b/test/testtobeclosed.lua @@ -0,0 +1,48 @@ +local skynet = require "skynet" + +local function new_test(name) + return setmetatable({}, { __close = function(...) + skynet.error(...) + end, __name = "closemeta:" .. name}) +end + +local i = 0 +skynet.dispatch("lua", function() + i = i + 1 + if i==2 then + local c = new_test("dispatch_error") + error("dispatch_error") + else + local c = new_test("dispatch_wait") + skynet.wait() + end +end) + +skynet.start(function() + local c = new_test("skynet.exit") + skynet.fork(function() + local a = new_test("stack_raise_error") + error("raise error") + end) + skynet.fork(function() + local a = new_test("session_id_coroutine_wait") + skynet.wait() + end) + skynet.fork(function() + local a = new_test("session_id_coroutine_call") + skynet.call(skynet.self(), "lua") + end) + skynet.fork(function() + skynet.call(skynet.self(), "lua") + end) + skynet.sleep(100) + skynet.fork(function() + local a = new_test("no_running") + skynet.wait() + end) + skynet.exit() +end) + +--[[ +testtobeclosed +]] \ No newline at end of file From 56c70dc7ace1fb668debc254b30adddd711f7e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Thu, 3 Dec 2020 15:47:51 +0800 Subject: [PATCH 289/565] fix socket half close (#1263) Co-authored-by: zixun --- skynet-src/socket_server.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 6fd975a9b..c3a221d13 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1357,8 +1357,14 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo } if (n==0) { FREE(buffer); - force_close(ss, s, l, result); - return SOCKET_CLOSE; + if (nomore_sending_data(s)) { + force_close(ss,s,l,result); + return SOCKET_CLOSE; + } else { + s->type = SOCKET_TYPE_HALFCLOSE; + shutdown(s->fd, SHUT_RD); + return -1; + } } if (s->type == SOCKET_TYPE_HALFCLOSE) { From 3af6b72aed7b522911df65f130e3c316ebb632ad Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 4 Dec 2020 15:42:35 +0800 Subject: [PATCH 290/565] close thread in suspend, See #1261 --- lualib/skynet.lua | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 613826d70..2f771ccb5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -14,20 +14,12 @@ local tunpack = table.unpack local traceback = debug.traceback local cresume = coroutine.resume -local coroutine_close = coroutine.close local running_thread = nil local init_thread = nil -local function coroutine_ret(co, ok, ...) - if not ok then - coroutine_close(co) - end - return ok, ... -end - local function coroutine_resume(co, ...) running_thread = co - return coroutine_ret(co, cresume(co, ...)) + return cresume(co, ...) end local coroutine_yield = coroutine.yield local coroutine_create = coroutine.create @@ -332,12 +324,14 @@ function suspend(co, result, command) session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil skynet.fork(function() end) -- trigger command "SUSPEND" - error(traceback(co,tostring(command))) + local tb = traceback(co,tostring(command)) + coroutine.close(co) + error(tb) end if command == "SUSPEND" then return dispatch_wakeup() elseif command == "QUIT" then - coroutine_close(co) + coroutine.close(co) -- service exit return elseif command == "USER" then @@ -534,7 +528,7 @@ function skynet.exit() end for session, co in pairs(session_id_coroutine) do if type(co) == "thread" then - coroutine_close(co) + coroutine.close(co) end end for resp in pairs(unresponse) do From 8aa158724527d58a4cdd99081947ec36c47899ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=B0?= Date: Fri, 4 Dec 2020 16:42:41 +0800 Subject: [PATCH 291/565] =?UTF-8?q?=E5=B9=B6=E5=8F=91=E8=AF=B7=E6=B1=82=20?= =?UTF-8?q?sharedata.query=20=E8=BF=94=E5=9B=9E=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5=20(#1266)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/sharedata.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib/skynet/sharedata.lua b/lualib/skynet/sharedata.lua index be62f67de..61478ad20 100644 --- a/lualib/skynet/sharedata.lua +++ b/lualib/skynet/sharedata.lua @@ -30,6 +30,10 @@ function sharedata.query(name) return cache[name] end local obj = skynet.call(service, "lua", "query", name) + if cache[name] and cache[name].__obj == obj then + skynet.send(service, "lua", "confirm" , obj) + return cache[name] + end local r = sd.box(obj) skynet.send(service, "lua", "confirm" , obj) skynet.fork(monitor,name, r, obj) From ad06d232a6e54179d1615241b273bc18ab08805c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 4 Dec 2020 18:27:55 +0800 Subject: [PATCH 292/565] This may fix #1268 --- lualib-src/sproto/sproto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 0b7f41324..47d1527ba 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -1386,7 +1386,7 @@ sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { ++src; if (header == 0xff) { int n; - if (srcsz < 0) { + if (srcsz <= 0) { return -1; } n = (src[0] + 1) * 8; @@ -1406,7 +1406,7 @@ sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { for (i=0;i<8;i++) { int nz = (header >> i) & 1; if (nz) { - if (srcsz < 0) + if (srcsz <= 0) return -1; if (bufsz > 0) { *buffer = *src; From 704194226db436ac059e4d7294e63d55533d8333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Mon, 7 Dec 2020 17:10:13 +0800 Subject: [PATCH 293/565] remove duplicate include directory (#1270) --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bb9b18e70..88fc90ef5 100644 --- a/Makefile +++ b/Makefile @@ -101,7 +101,7 @@ $(LUA_CLIB_PATH)/skynet.so : $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) | $(LUA $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -Iskynet-src + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ $(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 $@ From 4faa6499e819e4b5463d0b1efda7cf9d9f6dabcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Tue, 8 Dec 2020 08:47:14 +0800 Subject: [PATCH 294/565] fixed mongodb werror return errmsg (#1271) --- lualib/skynet/db/mongo.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 42dd9eeed..fd9e2c0c1 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -333,14 +333,16 @@ function mongo_collection:insert(doc) end local function werror(r) - local ok = (r.ok == 1 and not r.writeErrors and not r.writeConcernError) + local ok = (r.ok == 1 and not r.writeErrors and not r.writeConcernError and not r.errmsg) local err if not ok then if r.writeErrors then err = r.writeErrors[1].errmsg - else + elseif r.writeConcernError then err = r.writeConcernError.errmsg + else + err = r.errmsg end end return ok, err, r From e8a48483abd3b210f9f687a11a6a8325d76a4227 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 11 Dec 2020 12:02:32 +0800 Subject: [PATCH 295/565] Check address changes before raise error, fix #1273 --- service/clusterd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 5bf36e7a4..aee6f4bf4 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -60,10 +60,10 @@ local function open_channel(t, key) for _, co in ipairs(ct) do skynet.wakeup(co) end - assert(succ, err) if node_address[key] ~= address then return open_channel(t,key) end + assert(succ, err) return c end From 8eac155b1b1e33fc556418b5e98aed23b384148b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Dec 2020 14:44:28 +0800 Subject: [PATCH 296/565] reset connection if node_sender exist, see issue #1273 --- service/clusterd.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index aee6f4bf4..92e0c01f5 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -89,8 +89,9 @@ local function loadconfig(tmp) assert(address == false or type(address) == "string") if node_address[name] ~= address then -- address changed - if rawget(node_channel, name) then - node_channel[name] = nil -- reset connection + if node_sender[name] then + -- reset connection if node_sender[name] exist + node_channel[name] = nil table.insert(reload, name) end node_address[name] = address From 698d032a9b22c4f1b8649f734c6421fb5e777f3e Mon Sep 17 00:00:00 2001 From: t0350 Date: Mon, 14 Dec 2020 10:16:56 +0800 Subject: [PATCH 297/565] fix #1275 (#1276) Co-authored-by: jietao.cjt --- lualib/sprotoparser.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index cfa25afae..6b3741e98 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -348,6 +348,7 @@ local function packtype(name, t, alltypes) else tmp.type = nil end + tmp.map = nil if f.key then assert(f.array) if f.key == "" then From 8022a53e50c01370a4f452ebb304f4aa2df25ab8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Dec 2020 23:05:52 +0800 Subject: [PATCH 298/565] O(1) fork_queue pop, See #1277 --- lualib/skynet.lua | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 2f771ccb5..c65bb9487 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -65,7 +65,7 @@ local sleep_session = {} local watching_session = {} local error_queue = {} -local fork_queue = {} +local fork_queue = { h = 1, t = 0 } do ---- request/select local function send_requests(self) @@ -429,9 +429,12 @@ function skynet.killthread(thread) end end else - for i = 1, #fork_queue do + local t = fork_queue.t + for i = fork_queue.h, t do if fork_queue[i] == thread then - tremove(fork_queue, i) + table.move(fork_queue, i+1, t, i) + fork_queue[t] = nil + fork_queue.t = t - 1 return thread end end @@ -517,7 +520,7 @@ function skynet.time() end function skynet.exit() - fork_queue = {} -- no fork coroutine can be execute after skynet.exit + fork_queue = { h = 1, t = 0 } -- no fork coroutine can be execute after skynet.exit skynet.send(".launcher","lua","REMOVE",skynet.self(), false) -- report the sources that call me for co, session in pairs(session_coroutine_id) do @@ -757,7 +760,9 @@ function skynet.fork(func,...) local args = { ... } co = co_create(function() func(table.unpack(args,1,n)) end) end - tinsert(fork_queue, co) + local t = fork_queue.t + 1 + fork_queue.t = t + fork_queue[t] = co return co end @@ -828,10 +833,18 @@ end function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do - local co = tremove(fork_queue,1) - if co == nil then + if fork_queue.h > fork_queue.t then + -- queue is empty + fork_queue.h = 1 + fork_queue.t = 0 break end + -- pop queue + local h = fork_queue.h + local co = fork_queue[h] + fork_queue[h] = nil + fork_queue.h = h + 1 + local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co)) if not fork_succ then if succ then From 4d88f96ed653c0d68dc22be2a03a87567448ae2d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Dec 2020 19:20:15 +0800 Subject: [PATCH 299/565] bugfix: make proto shared --- 3rd/lua/lfunc.c | 2 +- 3rd/lua/lstring.c | 2 +- lualib-src/lua-sharetable.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 57b635474..250c85f7b 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -301,7 +301,7 @@ const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { void luaF_shareproto (Proto *f) { int i; - if (f == NULL) + if (f == NULL || isshared(f)) return; makeshared(f); luaS_share(f->source); diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 935e96a30..195534ac3 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -57,7 +57,7 @@ int luaS_eqshrstr (TString *a, TString *b) { } void luaS_share (TString *ts) { - if (ts == NULL) + if (ts == NULL || isshared(ts)) return; makeshared(ts); ts->id = ATOM_DEC(&STRID); diff --git a/lualib-src/lua-sharetable.c b/lualib-src/lua-sharetable.c index 185a5e857..8a84e7bfd 100644 --- a/lualib-src/lua-sharetable.c +++ b/lualib-src/lua-sharetable.c @@ -41,6 +41,7 @@ mark_shared(lua_State *L) { } else if (!lua_iscfunction(L, idx)) { LClosure *f = (LClosure *)lua_topointer(L, idx); makeshared(f); + lua_sharefunction(L, idx); } break; case LUA_TSTRING: From 284df5430bfae10eeca0b056fb8b89485fe02e6b Mon Sep 17 00:00:00 2001 From: coder <273461474@qq.com> Date: Mon, 28 Dec 2020 15:42:16 +0800 Subject: [PATCH 300/565] =?UTF-8?q?service=5Flogger.c=20localtime=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=EF=BC=8C=E6=8D=A2=E6=88=90=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E7=9A=84localtime=5Fr=20(#1311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service-src/service_logger.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index 7d58f4a3f..ceeec85de 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -38,8 +38,9 @@ static int timestring(struct logger *inst, char tmp[SIZETIMEFMT]) { uint64_t now = skynet_now(); time_t ti = now/100 + inst->starttime; - struct tm *info = localtime(&ti); - strftime(tmp, SIZETIMEFMT, "%D %T", info); + struct tm info; + (void)localtime_r(&ti,&info); + strftime(tmp, SIZETIMEFMT, "%D %T", &info); return now % 100; } From b164e3a8a9cb1ce8d3a9988fba59d4e73241cfc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Mon, 11 Jan 2021 10:54:34 +0800 Subject: [PATCH 301/565] use stdatomic (#1317) --- 3rd/lua/lstring.c | 4 +- lualib-src/lua-multicast.c | 10 ++-- lualib-src/lua-sharedata.c | 14 ++--- lualib-src/lua-stm.c | 12 ++-- service-src/service_snlua.c | 24 ++++---- skynet-src/atomic.h | 38 ++++++++++-- skynet-src/malloc_hook.c | 20 +++---- skynet-src/rwlock.h | 32 +++++----- skynet-src/skynet_monitor.c | 4 +- skynet-src/skynet_server.c | 38 ++++++------ skynet-src/socket_server.c | 115 +++++++++++++++++++----------------- skynet-src/spinlock.h | 28 +++++++-- 12 files changed, 201 insertions(+), 138 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 195534ac3..89672afef 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -23,7 +23,7 @@ #include "atomic.h" static unsigned int STRSEED; -static size_t STRID = 0; +static ATOM_SIZET STRID = 0; /* ** Maximum size for string table. @@ -60,7 +60,7 @@ void luaS_share (TString *ts) { if (ts == NULL || isshared(ts)) return; makeshared(ts); - ts->id = ATOM_DEC(&STRID); + ts->id = ATOM_FDEC(&STRID) - 1; } unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index dab90ee2a..1c845dc8f 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -10,7 +10,7 @@ #include "atomic.h" struct mc_package { - int reference; + ATOM_INT reference; uint32_t size; void *data; }; @@ -18,7 +18,7 @@ struct mc_package { static int pack(lua_State *L, void *data, size_t size) { struct mc_package * pack = skynet_malloc(sizeof(struct mc_package)); - pack->reference = 0; + ATOM_INIT(&pack->reference, 0); pack->size = (uint32_t)size; pack->data = data; struct mc_package ** ret = skynet_malloc(sizeof(*ret)); @@ -91,10 +91,10 @@ static int mc_bindrefer(lua_State *L) { struct mc_package ** pack = lua_touserdata(L,1); int ref = luaL_checkinteger(L,2); - if ((*pack)->reference != 0) { + if (ATOM_LOAD(&(*pack)->reference) != 0) { return luaL_error(L, "Can't bind a multicast package more than once"); } - (*pack)->reference = ref; + ATOM_STORE(&(*pack)->reference , ref); lua_pushlightuserdata(L, *pack); @@ -110,7 +110,7 @@ static int mc_closelocal(lua_State *L) { struct mc_package *pack = lua_touserdata(L,1); - int ref = ATOM_DEC(&pack->reference); + int ref = ATOM_FDEC(&pack->reference)-1; 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 e96bf407a..8e329f9d1 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -40,7 +40,7 @@ struct node { struct state { int dirty; - int ref; + ATOM_INT ref; struct table * root; }; @@ -383,7 +383,7 @@ convert_stringmap(struct context *ctx, struct table *tbl) { lua_pushvalue(L, 1); struct state * s = lua_newuserdatauv(L, sizeof(*s), 1); s->dirty = 0; - s->ref = 0; + ATOM_INIT(&s->ref , 0); s->root = tbl; lua_replace(L, 1); lua_replace(L, -2); @@ -665,7 +665,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); - ATOM_DEC(&s->ref); + ATOM_FDEC(&s->ref); c->root = NULL; c->update = NULL; @@ -676,7 +676,7 @@ static int lboxconf(lua_State *L) { struct table * tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - ATOM_INC(&s->ref); + ATOM_FINC(&s->ref); struct ctrl * c = lua_newuserdatauv(L, sizeof(*c), 1); c->root = tbl; @@ -712,7 +712,7 @@ 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); + lua_pushinteger(L , ATOM_LOAD(&s->ref)); return 1; } @@ -721,7 +721,7 @@ static int lincref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - int ref = ATOM_INC(&s->ref); + int ref = ATOM_FINC(&s->ref)+1; lua_pushinteger(L , ref); return 1; @@ -731,7 +731,7 @@ static int ldecref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - int ref = ATOM_DEC(&s->ref); + int ref = ATOM_FDEC(&s->ref)-1; lua_pushinteger(L , ref); return 1; diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index ecfa1e57d..86502c91a 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -13,7 +13,7 @@ struct stm_object { struct rwlock lock; - int reference; + ATOM_INT reference; struct stm_copy * copy; }; @@ -27,7 +27,7 @@ struct stm_copy { static struct stm_copy * stm_newcopy(void * msg, int32_t sz) { struct stm_copy * copy = skynet_malloc(sizeof(*copy)); - copy->reference = 1; + ATOM_INIT(©->reference, 1); copy->sz = sz; copy->msg = msg; @@ -38,7 +38,7 @@ 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; + ATOM_INIT(&obj->reference , 1); obj->copy = stm_newcopy(msg, sz); return obj; @@ -48,7 +48,7 @@ static void stm_releasecopy(struct stm_copy *copy) { if (copy == NULL) return; - if (ATOM_DEC(©->reference) == 0) { + if (ATOM_FDEC(©->reference) <= 1) { skynet_free(copy->msg); skynet_free(copy); } @@ -61,7 +61,7 @@ stm_release(struct stm_object *obj) { // writer release the stm object, so release the last copy . stm_releasecopy(obj->copy); obj->copy = NULL; - if (--obj->reference > 0) { + if (ATOM_FDEC(&obj->reference) > 1) { // stm object grab by readers, reset the copy to NULL. rwlock_wunlock(&obj->lock); return; @@ -73,7 +73,7 @@ stm_release(struct stm_object *obj) { static void stm_releasereader(struct stm_object *obj) { rwlock_rlock(&obj->lock); - if (ATOM_DEC(&obj->reference) == 0) { + if (ATOM_FDEC(&obj->reference) == 1) { // last reader, no writer. so no need to unlock assert(obj->copy == NULL); skynet_free(obj); diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 3ef4f0e7f..f3ed03eee 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -30,7 +30,7 @@ struct snlua { size_t mem_report; size_t mem_limit; lua_State * activeL; - volatile int trap; + volatile ATOM_INT trap; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -67,8 +67,8 @@ signal_hook(lua_State *L, lua_Debug *ar) { struct snlua *l = (struct snlua *)ud; lua_sethook (L, NULL, 0, 0); - if (l->trap) { - l->trap = 0; + if (ATOM_LOAD(&l->trap)) { + ATOM_STORE(&l->trap , 0); luaL_error(L, "signal 0"); } } @@ -76,7 +76,7 @@ signal_hook(lua_State *L, lua_Debug *ar) { static void switchL(lua_State *L, struct snlua *l) { l->activeL = L; - if (l->trap) { + if (ATOM_LOAD(&l->trap)) { lua_sethook(L, signal_hook, LUA_MASKCOUNT, 1); } } @@ -88,9 +88,9 @@ lua_resumeX(lua_State *L, lua_State *from, int nargs, int *nresults) { struct snlua *l = (struct snlua *)ud; switchL(L, l); int err = lua_resume(L, from, nargs, nresults); - if (l->trap) { + if (ATOM_LOAD(&l->trap)) { // wait for lua_sethook. (l->trap == -1) - while (l->trap >= 0) ; + while (ATOM_LOAD(&l->trap) >= 0) ; } switchL(from, l); return err; @@ -506,7 +506,7 @@ snlua_create(void) { l->mem_limit = 0; l->L = lua_newstate(lalloc, l); l->activeL = NULL; - l->trap = 0; + ATOM_INIT(&l->trap , 0); return l; } @@ -520,13 +520,17 @@ void snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { - if (l->trap == 0) { + if (ATOM_LOAD(&l->trap) == 0) { + ATOM_INT zero; + ATOM_INIT(&zero, 0); // only one thread can set trap ( l->trap 0->1 ) - if (!ATOM_CAS(&l->trap, 0, 1)) + if (!ATOM_CAS(&l->trap, zero, 1)) return; + ATOM_INT one; + ATOM_INIT(&one, 1); lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); // finish set ( l->trap 1 -> -1 ) - ATOM_CAS(&l->trap, 1, -1); + ATOM_CAS(&l->trap, one, -1); } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index f79c71488..dea6fbeff 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -1,14 +1,42 @@ #ifndef SKYNET_ATOMIC_H #define SKYNET_ATOMIC_H +#ifdef NO_STDATOMIC + +#define ATOM_BYTE unsigned char +#define ATOM_INT int +#define ATOM_POINTER void * +#define ATOM_SIZET size_t +#define ATOM_INIT(ptr, v) (*(ptr) = v) +#define ATOM_LOAD(ptr) (*(ptr)) +#define ATOM_STORE(ptr, v) (*(ptr) = v) #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) +#define ATOM_FADD(ptr,n) __sync_fetch_and_add(ptr, n) +#define ATOM_FSUB(ptr,n) __sync_fetch_and_sub(ptr, n) +#define ATOM_FAND(ptr,n) __sync_fetch_and_and(ptr, n) + +#else + +#include + +#define ATOM_BYTE atomic_uchar +#define ATOM_INT atomic_int +#define ATOM_POINTER atomic_uintptr_t +#define ATOM_SIZET atomic_size_t +#define ATOM_INIT(ref, v) atomic_init(ref, v) +#define ATOM_LOAD(ptr) atomic_load(ptr) +#define ATOM_STORE(ptr, v) atomic_store(ptr, v) +#define ATOM_CAS(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval) +#define ATOM_CAS_POINTER(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval) +#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1) +#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1) +#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n) +#define ATOM_FSUB(ptr,n) atomic_fetch_sub(ptr, n) +#define ATOM_FAND(ptr,n) atomic_fetch_and(ptr, n) + +#endif #endif diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index a3fed30ec..d02113cfd 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -15,8 +15,8 @@ #define MEMORY_ALLOCTAG 0x20140605 #define MEMORY_FREETAG 0x0badf00d -static size_t _used_memory = 0; -static size_t _memory_block = 0; +static ATOM_SIZET _used_memory = 0; +static ATOM_SIZET _memory_block = 0; struct mem_data { uint32_t handle; @@ -67,21 +67,21 @@ get_allocated_field(uint32_t handle) { inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { - ATOM_ADD(&_used_memory, __n); - ATOM_INC(&_memory_block); + ATOM_FADD(&_used_memory, __n); + ATOM_FINC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - ATOM_ADD(allocated, __n); + ATOM_FADD(allocated, __n); } } inline static void update_xmalloc_stat_free(uint32_t handle, size_t __n) { - ATOM_SUB(&_used_memory, __n); - ATOM_DEC(&_memory_block); + ATOM_FSUB(&_used_memory, __n); + ATOM_FDEC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - ATOM_SUB(allocated, __n); + ATOM_FSUB(allocated, __n); } } @@ -273,12 +273,12 @@ mallctl_cmd(const char* name) { size_t malloc_used_memory(void) { - return _used_memory; + return ATOM_LOAD(&_used_memory); } size_t malloc_memory_block(void) { - return _memory_block; + return ATOM_LOAD(&_memory_block); } void diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 5a995918d..56feebf7a 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -3,26 +3,26 @@ #ifndef USE_PTHREAD_LOCK +#include "atomic.h" + struct rwlock { - int write; - int read; + ATOM_INT write; + ATOM_INT read; }; static inline void rwlock_init(struct rwlock *lock) { - lock->write = 0; - lock->read = 0; + ATOM_INIT(&lock->write, 0); + ATOM_INIT(&lock->read, 0); } static inline void rwlock_rlock(struct rwlock *lock) { for (;;) { - while(lock->write) { - __sync_synchronize(); - } - __sync_add_and_fetch(&lock->read,1); - if (lock->write) { - __sync_sub_and_fetch(&lock->read,1); + while(ATOM_LOAD(&lock->write)) {} + ATOM_FINC(&lock->read); + if (ATOM_LOAD(&lock->write)) { + ATOM_FDEC(&lock->read); } else { break; } @@ -31,20 +31,20 @@ rwlock_rlock(struct rwlock *lock) { static inline void rwlock_wlock(struct rwlock *lock) { - while (__sync_lock_test_and_set(&lock->write,1)) {} - while(lock->read) { - __sync_synchronize(); - } + ATOM_INT clear; + ATOM_INIT(&clear, 0); + while (!ATOM_CAS(&lock->write,clear,1)) {} + while(ATOM_LOAD(&lock->read)) {} } static inline void rwlock_wunlock(struct rwlock *lock) { - __sync_lock_release(&lock->write); + ATOM_STORE(&lock->write, 0); } static inline void rwlock_runlock(struct rwlock *lock) { - __sync_sub_and_fetch(&lock->read,1); + ATOM_FDEC(&lock->read); } #else diff --git a/skynet-src/skynet_monitor.c b/skynet-src/skynet_monitor.c index 8d47fc895..48def95b3 100644 --- a/skynet-src/skynet_monitor.c +++ b/skynet-src/skynet_monitor.c @@ -9,7 +9,7 @@ #include struct skynet_monitor { - int version; + ATOM_INT version; int check_version; uint32_t source; uint32_t destination; @@ -31,7 +31,7 @@ void skynet_monitor_trigger(struct skynet_monitor *sm, uint32_t source, uint32_t destination) { sm->source = source; sm->destination = destination; - ATOM_INC(&sm->version); + ATOM_FINC(&sm->version); } void diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 9ee423001..d0cc3fdd4 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -52,7 +52,7 @@ struct skynet_context { char result[32]; uint32_t handle; int session_id; - int ref; + ATOM_INT ref; int message_count; bool init; bool endless; @@ -62,7 +62,7 @@ struct skynet_context { }; struct skynet_node { - int total; + ATOM_INT total; int init; uint32_t monitor_exit; pthread_key_t handle_key; @@ -73,17 +73,17 @@ static struct skynet_node G_NODE; int skynet_context_total() { - return G_NODE.total; + return ATOM_LOAD(&G_NODE.total); } static void context_inc() { - ATOM_INC(&G_NODE.total); + ATOM_FINC(&G_NODE.total); } static void context_dec() { - ATOM_DEC(&G_NODE.total); + ATOM_FDEC(&G_NODE.total); } uint32_t @@ -137,11 +137,11 @@ skynet_context_new(const char * name, const char *param) { ctx->mod = mod; ctx->instance = inst; - ctx->ref = 2; + ATOM_INIT(&ctx->ref , 2); ctx->cb = NULL; ctx->cb_ud = NULL; ctx->session_id = 0; - ctx->logfile = NULL; + ATOM_INIT(&ctx->logfile, NULL); ctx->init = false; ctx->endless = false; @@ -194,7 +194,7 @@ skynet_context_newsession(struct skynet_context *ctx) { void skynet_context_grab(struct skynet_context *ctx) { - ATOM_INC(&ctx->ref); + ATOM_FINC(&ctx->ref); } void @@ -207,8 +207,9 @@ skynet_context_reserve(struct skynet_context *ctx) { static void delete_context(struct skynet_context *ctx) { - if (ctx->logfile) { - fclose(ctx->logfile); + FILE *f = ATOM_LOAD(&ctx->logfile); + if (f) { + fclose(f); } skynet_module_instance_release(ctx->mod, ctx->instance); skynet_mq_mark_release(ctx->queue); @@ -219,7 +220,7 @@ delete_context(struct skynet_context *ctx) { struct skynet_context * skynet_context_release(struct skynet_context *ctx) { - if (ATOM_DEC(&ctx->ref) == 0) { + if (ATOM_FDEC(&ctx->ref) == 1) { delete_context(ctx); return NULL; } @@ -264,8 +265,9 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); 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); + FILE *f = ATOM_LOAD(&ctx->logfile); + if (f) { + skynet_log_output(f, msg->source, type, msg->session, msg->data, sz); } ++ctx->message_count; int reserve_msg; @@ -589,11 +591,13 @@ cmd_logon(struct skynet_context * context, const char * param) { if (ctx == NULL) return NULL; FILE *f = NULL; - FILE * lastf = ctx->logfile; + FILE * lastf = ATOM_LOAD(&ctx->logfile); if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { - if (!ATOM_CAS_POINTER(&ctx->logfile, NULL, f)) { + ATOM_POINTER exp; + ATOM_INIT(&exp, 0); + if (!ATOM_CAS_POINTER(&ctx->logfile, exp, f)) { // logfile opens in other thread, close this one. fclose(f); } @@ -611,7 +615,7 @@ cmd_logoff(struct skynet_context * context, const char * param) { struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) return NULL; - FILE * f = ctx->logfile; + FILE * f = ATOM_LOAD(&ctx->logfile); if (f) { // logfile may close in other thread if (ATOM_CAS_POINTER(&ctx->logfile, f, NULL)) { @@ -806,7 +810,7 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t void skynet_globalinit(void) { - G_NODE.total = 0; + ATOM_INIT(&G_NODE.total , 0); G_NODE.monitor_exit = 0; G_NODE.init = 1; if (pthread_key_create(&G_NODE.handle_key, NULL)) { diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index c3a221d13..c57e26bc3 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -95,7 +95,7 @@ struct socket { int fd; int id; uint8_t protocol; - uint8_t type; + ATOM_BYTE type; bool reading; bool writing; int udpconnecting; @@ -116,7 +116,7 @@ struct socket_server { int sendctrl_fd; int checkctrl; poll_fd event_fd; - int alloc_id; + ATOM_INT alloc_id; int event_n; int event_index; struct socket_object_interface soi; @@ -276,6 +276,11 @@ socket_unlock(struct socket_lock *sl) { } } +static inline int +socket_invalid(struct socket *s, int id) { + return (s->id != id || ATOM_LOAD(&s->type) == SOCKET_TYPE_INVALID); +} + static inline bool send_object_init(struct socket_server *ss, struct send_object *so, const void *object, size_t sz) { if (sz == USEROBJECT) { @@ -339,13 +344,14 @@ static int reserve_id(struct socket_server *ss) { int i; for (i=0;ialloc_id)); + int id = ATOM_FINC(&(ss->alloc_id))+1; if (id < 0) { - id = ATOM_AND(&(ss->alloc_id), 0x7fffffff); + id = ATOM_FAND(&(ss->alloc_id), 0x7fffffff) & 0x7fffffff; } struct socket *s = &ss->slot[HASH_ID(id)]; - if (s->type == SOCKET_TYPE_INVALID) { - if (ATOM_CAS(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { + unsigned char type_invalid = ATOM_LOAD(&s->type); + if (type_invalid == SOCKET_TYPE_INVALID) { + if (ATOM_CAS(&s->type, type_invalid, SOCKET_TYPE_RESERVE)) { s->id = id; s->protocol = PROTOCOL_UNKNOWN; // socket_server_udp_connect may inc s->udpconncting directly (from other thread, before new_fd), @@ -400,12 +406,12 @@ socket_server_create(uint64_t time) { for (i=0;islot[i]; - s->type = SOCKET_TYPE_INVALID; + ATOM_INIT(&s->type, SOCKET_TYPE_INVALID); clear_wb_list(&s->high); clear_wb_list(&s->low); spinlock_init(&s->dw_lock); } - ss->alloc_id = 0; + ATOM_INIT(&ss->alloc_id , 0); ss->event_n = 0; ss->event_index = 0; memset(&ss->soi, 0, sizeof(ss->soi)); @@ -474,20 +480,21 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_lock *l, s result->ud = 0; result->data = NULL; result->opaque = s->opaque; - if (s->type == SOCKET_TYPE_INVALID) { + uint8_t type = ATOM_LOAD(&s->type); + if (type == SOCKET_TYPE_INVALID) { return; } - assert(s->type != SOCKET_TYPE_RESERVE); + assert(type != SOCKET_TYPE_RESERVE); free_wb_list(ss,&s->high); free_wb_list(ss,&s->low); sp_del(ss->event_fd, s->fd); socket_lock(l); - if (s->type != SOCKET_TYPE_BIND) { + if (type != SOCKET_TYPE_BIND) { if (close(s->fd) < 0) { perror("close socket:"); } } - s->type = SOCKET_TYPE_INVALID; + ATOM_STORE(&s->type, SOCKET_TYPE_INVALID); if (s->dw_buffer) { struct socket_sendbuffer tmp; tmp.buffer = s->dw_buffer; @@ -508,7 +515,7 @@ socket_server_release(struct socket_server *ss) { struct socket *s = &ss->slot[i]; struct socket_lock l; socket_lock_init(s, &l); - if (s->type != SOCKET_TYPE_RESERVE) { + if (ATOM_LOAD(&s->type) != SOCKET_TYPE_RESERVE) { force_close(ss, s, &l, &dummy); } spinlock_destroy(&s->dw_lock); @@ -546,10 +553,10 @@ enable_read(struct socket_server *ss, struct socket *s, bool enable) { static struct socket * new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool reading) { struct socket * s = &ss->slot[HASH_ID(id)]; - assert(s->type == SOCKET_TYPE_RESERVE); + assert(ATOM_LOAD(&s->type) == SOCKET_TYPE_RESERVE); if (sp_add(ss->event_fd, fd, s)) { - s->type = SOCKET_TYPE_INVALID; + ATOM_STORE(&s->type, SOCKET_TYPE_INVALID); return NULL; } @@ -569,7 +576,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->dw_size = 0; memset(&s->stat, 0, sizeof(s->stat)); if (enable_read(ss, s, reading)) { - s->type = SOCKET_TYPE_INVALID; + ATOM_STORE(&s->type , SOCKET_TYPE_INVALID); return NULL; } return s; @@ -642,7 +649,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock } if(status == 0) { - ns->type = SOCKET_TYPE_CONNECTED; + ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); struct sockaddr * addr = ai_ptr->ai_addr; void * sin_addr = (ai_ptr->ai_family == AF_INET) ? (void*)&((struct sockaddr_in *)addr)->sin_addr : (void*)&((struct sockaddr_in6 *)addr)->sin6_addr; if (inet_ntop(ai_ptr->ai_family, sin_addr, ss->buffer, sizeof(ss->buffer))) { @@ -651,7 +658,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock freeaddrinfo( ai_list ); return SOCKET_OPEN; } else { - ns->type = SOCKET_TYPE_CONNECTING; + ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTING); if (enable_write(ss, ns, true)) { result->data = "enable write failed"; goto _failed; @@ -838,7 +845,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, assert(send_buffer_empty(s) && s->wb_size == 0); int err = enable_write(ss, s, false); - if (s->type == SOCKET_TYPE_HALFCLOSE) { + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE) { force_close(ss, s, l, result); return SOCKET_CLOSE; } @@ -936,7 +943,7 @@ static int trigger_write(struct socket_server *ss, struct request_send * 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) + if (socket_invalid(s, id)) return -1; if (enable_write(ss, s, true)) { result->opaque = s->opaque; @@ -961,18 +968,19 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock 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) { + uint8_t type = ATOM_LOAD(&s->type); + if (type == SOCKET_TYPE_INVALID || s->id != id + || type == SOCKET_TYPE_HALFCLOSE + || type == SOCKET_TYPE_PACCEPT) { so.free_func((void *)request->buffer); return -1; } - if (s->type == SOCKET_TYPE_PLISTEN || s->type == SOCKET_TYPE_LISTEN) { + if (type == SOCKET_TYPE_PLISTEN || type == SOCKET_TYPE_LISTEN) { skynet_error(NULL, "socket-server: write to listen fd %d.", id); so.free_func((void *)request->buffer); return -1; } - if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { + if (send_buffer_empty(s) && type == SOCKET_TYPE_CONNECTED) { if (s->protocol == PROTOCOL_TCP) { append_sendbuffer(ss, s, request); // add to high priority list, even priority == PRIORITY_LOW } else { @@ -1037,7 +1045,7 @@ listen_socket(struct socket_server *ss, struct request_listen * request, struct if (s == NULL) { goto _failed; } - s->type = SOCKET_TYPE_PLISTEN; + ATOM_STORE(&s->type , SOCKET_TYPE_PLISTEN); return -1; _failed: close(listen_fd); @@ -1059,7 +1067,7 @@ 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[HASH_ID(id)]; - if (s->type == SOCKET_TYPE_INVALID || s->id != id) { + if (socket_invalid(s, id)) { result->id = id; result->opaque = request->opaque; result->ud = 0; @@ -1081,7 +1089,7 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc result->opaque = request->opaque; return SOCKET_CLOSE; } - s->type = SOCKET_TYPE_HALFCLOSE; + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE); shutdown(s->fd, SHUT_RD); return -1; @@ -1099,7 +1107,7 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke return SOCKET_ERR; } sp_nonblocking(request->fd); - s->type = SOCKET_TYPE_BIND; + ATOM_STORE(&s->type , SOCKET_TYPE_BIND); result->data = "binding"; return SOCKET_OPEN; } @@ -1112,7 +1120,7 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->ud = 0; result->data = NULL; struct socket *s = &ss->slot[HASH_ID(id)]; - if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + if (socket_invalid(s, id)) { result->data = "invalid socket"; return SOCKET_ERR; } @@ -1122,12 +1130,13 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "enable read failed"; return SOCKET_ERR; } - if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { - s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; + uint8_t type = ATOM_LOAD(&s->type); + if (type == SOCKET_TYPE_PACCEPT || type == SOCKET_TYPE_PLISTEN) { + ATOM_STORE(&s->type , (type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN); s->opaque = request->opaque; result->data = "start"; return SOCKET_OPEN; - } else if (s->type == SOCKET_TYPE_CONNECTED) { + } else if (type == SOCKET_TYPE_CONNECTED) { // todo: maybe we should send a message SOCKET_TRANSFER to s->opaque s->opaque = request->opaque; result->data = "transfer"; @@ -1141,7 +1150,7 @@ static int pause_socket(struct socket_server *ss, struct request_resumepause *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) { + if (socket_invalid(s, id)) { return -1; } if (enable_read(ss, s, false)) { @@ -1158,7 +1167,7 @@ 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) { + if (socket_invalid(s, id)) { return; } int v = request->value; @@ -1210,7 +1219,7 @@ add_udp_socket(struct socket_server *ss, struct request_udp *udp) { ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return; } - ns->type = SOCKET_TYPE_CONNECTED; + ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address)); } @@ -1218,7 +1227,7 @@ 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) { + if (socket_invalid(s, id)) { return -1; } int type = request->address[0]; @@ -1236,7 +1245,7 @@ set_udp_address(struct socket_server *ss, struct request_setudp *request, struct } else { memcpy(s->p.udp_address, request->address, 1+2+16); // 1 type, 2 port, 16 ipv6 } - ATOM_DEC(&s->udpconnecting); + ATOM_FDEC(&s->udpconnecting); return -1; } @@ -1268,7 +1277,7 @@ dec_sending_ref(struct socket_server *ss, int id) { // Notice: udp may inc sending while type == SOCKET_TYPE_RESERVE if (s->id == id && s->protocol == PROTOCOL_TCP) { assert((s->sending & 0xffff) != 0); - ATOM_DEC(&s->sending); + ATOM_FDEC(&s->sending); } } @@ -1361,13 +1370,13 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo force_close(ss,s,l,result); return SOCKET_CLOSE; } else { - s->type = SOCKET_TYPE_HALFCLOSE; + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE); shutdown(s->fd, SHUT_RD); return -1; } } - if (s->type == SOCKET_TYPE_HALFCLOSE) { + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE) { // discard recv data FREE(buffer); return -1; @@ -1461,7 +1470,7 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l result->data = strerror(errno); return SOCKET_ERR; } else { - s->type = SOCKET_TYPE_CONNECTED; + ATOM_STORE(&s->type , SOCKET_TYPE_CONNECTED); result->opaque = s->opaque; result->id = s->id; result->ud = 0; @@ -1532,7 +1541,7 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message // accept new one connection stat_read(ss,s,1); - ns->type = SOCKET_TYPE_PACCEPT; + ATOM_STORE(&ns->type , SOCKET_TYPE_PACCEPT); result->opaque = s->opaque; result->id = s->id; result->ud = id; @@ -1554,7 +1563,7 @@ clear_closed_event(struct socket_server *ss, struct socket_message * result, int struct event *e = &ss->ev[i]; struct socket *s = e->s; if (s) { - if (s->type == SOCKET_TYPE_INVALID && s->id == id) { + if (socket_invalid(s, id)) { e->s = NULL; break; } @@ -1602,7 +1611,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int } struct socket_lock l; socket_lock_init(s, &l); - switch (s->type) { + switch (ATOM_LOAD(&s->type)) { case SOCKET_TYPE_CONNECTING: return report_connect(ss, s, &l, result); case SOCKET_TYPE_LISTEN: { @@ -1720,7 +1729,7 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a static inline int can_direct_write(struct socket *s, int id) { - return s->id == id && nomore_sending_data(s) && s->type == SOCKET_TYPE_CONNECTED && s->udpconnecting == 0; + return s->id == id && nomore_sending_data(s) && ATOM_LOAD(&s->type) == SOCKET_TYPE_CONNECTED && s->udpconnecting == 0; } // return -1 when error, 0 when success @@ -1728,7 +1737,7 @@ int socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; - if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + if (socket_invalid(s, id)) { free_buffer(ss, buf); return -1; } @@ -1804,7 +1813,7 @@ socket_server_send_lowpriority(struct socket_server *ss, struct socket_sendbuffe int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; - if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + if (socket_invalid(s, id)) { free_buffer(ss, buf); return -1; } @@ -2009,7 +2018,7 @@ int socket_server_udp_send(struct socket_server *ss, const struct socket_udp_address *addr, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; - if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + if (socket_invalid(s, id)) { free_buffer(ss, buf); return -1; } @@ -2070,17 +2079,17 @@ socket_server_udp_send(struct socket_server *ss, const struct socket_udp_address int socket_server_udp_connect(struct socket_server *ss, int id, const char * addr, int port) { struct socket * s = &ss->slot[HASH_ID(id)]; - if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + if (socket_invalid(s, id)) { return -1; } struct socket_lock l; socket_lock_init(s, &l); socket_lock(&l); - if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + if (socket_invalid(s, id)) { socket_unlock(&l); return -1; } - ATOM_INC(&s->udpconnecting); + ATOM_FINC(&s->udpconnecting); socket_unlock(&l); int status; @@ -2158,7 +2167,7 @@ static int query_info(struct socket *s, struct socket_info *si) { union sockaddr_all u; socklen_t slen = sizeof(u); - switch (s->type) { + switch (ATOM_LOAD(&s->type)) { case SOCKET_TYPE_BIND: si->type = SOCKET_INFO_BIND; si->name[0] = '\0'; diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 514546f46..27ef462ad 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -8,28 +8,46 @@ #ifndef USE_PTHREAD_LOCK +#ifdef NO_STDATOMIC + +#define atomic_flag_ int +#define ATOMIC_FLAG_INIT_ 0 +#define atomic_flag_test_and_set_(ptr) __sync_lock_test_and_set(ptr, 1) +#define atomic_flag_clear_(ptr) __sync_lock_release(ptr) + +#else + +#include +#define atomic_flag_ atomic_flag +#define ATOMIC_FLAG_INIT_ ATOMIC_FLAG_INIT +#define atomic_flag_test_and_set_ atomic_flag_test_and_set +#define atomic_flag_clear_ atomic_flag_clear + +#endif + struct spinlock { - int lock; + atomic_flag_ lock; }; static inline void spinlock_init(struct spinlock *lock) { - lock->lock = 0; + atomic_flag_ v = ATOMIC_FLAG_INIT_; + lock->lock = v; } static inline void spinlock_lock(struct spinlock *lock) { - while (__sync_lock_test_and_set(&lock->lock,1)) {} + while (atomic_flag_test_and_set_(&lock->lock)) {} } static inline int spinlock_trylock(struct spinlock *lock) { - return __sync_lock_test_and_set(&lock->lock,1) == 0; + return atomic_flag_test_and_set_(&lock->lock) == 0; } static inline void spinlock_unlock(struct spinlock *lock) { - __sync_lock_release(&lock->lock); + atomic_flag_clear_(&lock->lock); } static inline void From c0e34214629a6110393c889d0151276a141ab1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 11 Jan 2021 15:15:25 +0800 Subject: [PATCH 302/565] bugfix ltls init #1314 (#1318) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bugfix ltls init #1314 * 修改init失败信息 * fix destructor Co-authored-by: zixun --- lualib-src/ltls.c | 41 ++++++++++++++++++++++++++++++++--------- service/bootstrap.lua | 10 ++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 6da40c489..0e8e41b38 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -378,9 +378,11 @@ lnew_tls(lua_State* L) { return 1; } - int luaopen_ltls_c(lua_State* L) { + if(!TLS_IS_INIT) { + luaL_error(L, "ltls need init, Put enablessl = true in you config file."); + } luaL_Reg l[] = { {"newctx", lnew_ctx}, {"newtls", lnew_tls}, @@ -391,18 +393,24 @@ luaopen_ltls_c(lua_State* L) { return 1; } -void __attribute__((constructor)) ltls_init(void) { +// for ltls init +static int +ltls_init_constructor(lua_State* L) { #ifndef OPENSSL_EXTERNAL_INITIALIZATION - SSL_library_init(); - SSL_load_error_strings(); - ERR_load_BIO_strings(); - OpenSSL_add_all_algorithms(); - TLS_IS_INIT = true; + if(!TLS_IS_INIT) { + SSL_library_init(); + SSL_load_error_strings(); + ERR_load_BIO_strings(); + OpenSSL_add_all_algorithms(); + } #endif + TLS_IS_INIT = true; + return 0; } - -void __attribute__((destructor)) ltls_destory(void) { +static int +ltls_init_destructor(lua_State* L) { +#ifndef OPENSSL_EXTERNAL_INITIALIZATION if(TLS_IS_INIT) { ENGINE_cleanup(); CONF_modules_unload(1); @@ -411,4 +419,19 @@ void __attribute__((destructor)) ltls_destory(void) { sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); CRYPTO_cleanup_all_ex_data(); } +#endif + TLS_IS_INIT = false; + return 0; +} + +int +luaopen_ltls_init_c(lua_State* L) { + luaL_Reg l[] = { + {"constructor", ltls_init_constructor}, + {"destructor", ltls_init_destructor}, + {NULL, NULL}, + }; + luaL_checkversion(L); + luaL_newlib(L, l); + return 1; } diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 39f0c49ca..69802b492 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +local service = require "skynet.service" require "skynet.manager" -- import skynet.launch, ... skynet.start(function() @@ -39,6 +40,15 @@ skynet.start(function() skynet.name("DATACENTER", datacenter) end skynet.newservice "service_mgr" + + local enablessl = skynet.getenv "enablessl" + if enablessl then + service.new("ltls_holder", function () + local c = require "ltls.init.c" + c.constructor() + end) + end + pcall(skynet.newservice,skynet.getenv "start" or "main") skynet.exit() end) From 1ed4fdf94d0fb77d22c97214add9b4697bdc38c1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jan 2021 16:57:25 +0800 Subject: [PATCH 303/565] Use _Atomic type for socket.sending and socket.udpconnecting. See #1317 --- service-src/service_snlua.c | 2 +- skynet-src/atomic.h | 4 +++- skynet-src/socket_server.c | 16 ++++++++-------- skynet-src/spinlock.h | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index f3ed03eee..72c2f9544 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -30,7 +30,7 @@ struct snlua { size_t mem_report; size_t mem_limit; lua_State * activeL; - volatile ATOM_INT trap; + ATOM_INT trap; }; // LUA_CACHELIB may defined in patched lua for shared proto diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index dea6fbeff..d327bcddc 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -1,12 +1,13 @@ #ifndef SKYNET_ATOMIC_H #define SKYNET_ATOMIC_H -#ifdef NO_STDATOMIC +#ifdef __STDC_NO_ATOMICS__ #define ATOM_BYTE unsigned char #define ATOM_INT int #define ATOM_POINTER void * #define ATOM_SIZET size_t +#define ATOM_ULONG unsigned long #define ATOM_INIT(ptr, v) (*(ptr) = v) #define ATOM_LOAD(ptr) (*(ptr)) #define ATOM_STORE(ptr, v) (*(ptr) = v) @@ -26,6 +27,7 @@ #define ATOM_INT atomic_int #define ATOM_POINTER atomic_uintptr_t #define ATOM_SIZET atomic_size_t +#define ATOM_ULONG atomic_ulong #define ATOM_INIT(ref, v) atomic_init(ref, v) #define ATOM_LOAD(ptr) atomic_load(ptr) #define ATOM_STORE(ptr, v) atomic_store(ptr, v) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index c57e26bc3..aee51575b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -91,14 +91,14 @@ struct socket { struct wb_list low; int64_t wb_size; struct socket_stat stat; - volatile uint32_t sending; + ATOM_ULONG sending; int fd; int id; uint8_t protocol; ATOM_BYTE type; bool reading; bool writing; - int udpconnecting; + ATOM_INT udpconnecting; int64_t warn_size; union { int size; @@ -356,7 +356,7 @@ reserve_id(struct socket_server *ss) { s->protocol = PROTOCOL_UNKNOWN; // socket_server_udp_connect may inc s->udpconncting directly (from other thread, before new_fd), // so reset it to 0 here rather than in new_fd. - s->udpconnecting = 0; + ATOM_INIT(&s->udpconnecting, 0); s->fd = -1; return id; } else { @@ -564,7 +564,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->fd = fd; s->reading = true; s->writing = false; - s->sending = ID_TAG16(id) << 16 | 0; + ATOM_INIT(&s->sending , ID_TAG16(id) << 16 | 0); s->protocol = protocol; s->p.size = MIN_READ_BUFFER; s->opaque = opaque; @@ -1060,7 +1060,7 @@ listen_socket(struct socket_server *ss, struct request_listen * request, struct static inline int nomore_sending_data(struct socket *s) { - return send_buffer_empty(s) && s->dw_buffer == NULL && (s->sending & 0xffff) == 0; + return send_buffer_empty(s) && s->dw_buffer == NULL && (ATOM_LOAD(&s->sending) & 0xffff) == 0; } static int @@ -1254,7 +1254,7 @@ inc_sending_ref(struct socket *s, int id) { if (s->protocol != PROTOCOL_TCP) return; for (;;) { - uint32_t sending = s->sending; + unsigned long sending = ATOM_LOAD(&s->sending); if ((sending >> 16) == ID_TAG16(id)) { if ((sending & 0xffff) == 0xffff) { // s->sending may overflow (rarely), so busy waiting here for socket thread dec it. see issue #794 @@ -1276,7 +1276,7 @@ dec_sending_ref(struct socket_server *ss, int id) { struct socket * s = &ss->slot[HASH_ID(id)]; // Notice: udp may inc sending while type == SOCKET_TYPE_RESERVE if (s->id == id && s->protocol == PROTOCOL_TCP) { - assert((s->sending & 0xffff) != 0); + assert((ATOM_LOAD(&s->sending) & 0xffff) != 0); ATOM_FDEC(&s->sending); } } @@ -1729,7 +1729,7 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a static inline int can_direct_write(struct socket *s, int id) { - return s->id == id && nomore_sending_data(s) && ATOM_LOAD(&s->type) == SOCKET_TYPE_CONNECTED && s->udpconnecting == 0; + return s->id == id && nomore_sending_data(s) && ATOM_LOAD(&s->type) == SOCKET_TYPE_CONNECTED && ATOM_LOAD(&s->udpconnecting) == 0; } // return -1 when error, 0 when success diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 27ef462ad..673a207c5 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -8,7 +8,7 @@ #ifndef USE_PTHREAD_LOCK -#ifdef NO_STDATOMIC +#ifdef __STDC_NO_ATOMICS__ #define atomic_flag_ int #define ATOMIC_FLAG_INIT_ 0 From bc2fba560f4a8ca48d13992905315120a86450cb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jan 2021 17:10:26 +0800 Subject: [PATCH 304/565] CAS expected should not be _Atomic type, #1317 --- service-src/service_snlua.c | 6 ++---- skynet-src/rwlock.h | 3 +-- skynet-src/skynet_server.c | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 72c2f9544..81a1604a5 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -521,15 +521,13 @@ snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { if (ATOM_LOAD(&l->trap) == 0) { - ATOM_INT zero; - ATOM_INIT(&zero, 0); + int zero = 0; // only one thread can set trap ( l->trap 0->1 ) if (!ATOM_CAS(&l->trap, zero, 1)) return; - ATOM_INT one; - ATOM_INIT(&one, 1); lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); // finish set ( l->trap 1 -> -1 ) + int one = 1; ATOM_CAS(&l->trap, one, -1); } } else if (signal == 1) { diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 56feebf7a..28a49c245 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -31,8 +31,7 @@ rwlock_rlock(struct rwlock *lock) { static inline void rwlock_wlock(struct rwlock *lock) { - ATOM_INT clear; - ATOM_INIT(&clear, 0); + int clear = 0; while (!ATOM_CAS(&lock->write,clear,1)) {} while(ATOM_LOAD(&lock->read)) {} } diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index d0cc3fdd4..d776cea83 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -595,8 +595,7 @@ cmd_logon(struct skynet_context * context, const char * param) { if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { - ATOM_POINTER exp; - ATOM_INIT(&exp, 0); + uintptr_t exp = 0; if (!ATOM_CAS_POINTER(&ctx->logfile, exp, f)) { // logfile opens in other thread, close this one. fclose(f); @@ -618,7 +617,8 @@ cmd_logoff(struct skynet_context * context, const char * param) { FILE * f = ATOM_LOAD(&ctx->logfile); if (f) { // logfile may close in other thread - if (ATOM_CAS_POINTER(&ctx->logfile, f, NULL)) { + uintptr_t fptr = (uintptr_t)f; + if (ATOM_CAS_POINTER(&ctx->logfile, fptr, NULL)) { skynet_log_close(context, f, handle); } } From 1e5e8354d77a70d991af6d8c47c58af1c2629a36 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jan 2021 17:20:16 +0800 Subject: [PATCH 305/565] stm_copy.reference is _Atomic type, #1317 --- lualib-src/lua-stm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index 86502c91a..3926dd38d 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -18,7 +18,7 @@ struct stm_object { }; struct stm_copy { - int reference; + ATOM_INT reference; uint32_t sz; void * msg; }; From 933dbbd5704e3be5fb424d3d9e72981ab09bfb12 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jan 2021 17:30:04 +0800 Subject: [PATCH 306/565] lua-bson atomic #1317 --- lualib-src/lua-bson.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 808ca0f74..22dc64b23 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1245,11 +1245,11 @@ typeclosure(lua_State *L) { } static uint8_t oid_header[5]; -static uint32_t oid_counter; +static ATOM_ULONG oid_counter; static void init_oid_header() { - if (oid_counter) { + if (ATOM_LOAD(&oid_counter)) { // already init return; } @@ -1273,7 +1273,7 @@ init_oid_header() { if (c == 0) { c = 1; } - oid_counter = c; + ATOM_STORE(&oid_counter, c); } static inline int From 649aee1a237362186b3a92e489663d30a82eb5da Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jan 2021 17:32:37 +0800 Subject: [PATCH 307/565] use unsigned long instead of uint32_t to match atomic ulong --- 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 22dc64b23..f29048529 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1269,7 +1269,7 @@ init_oid_header() { oid_header[3] = pid & 0xff; oid_header[4] = (pid >> 8) & 0xff; - uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h; + unsigned long c = h ^ time(NULL) ^ (uintptr_t)&h; if (c == 0) { c = 1; } From 2e067063231013e6ccbb076a505567a8d0b7d328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 11 Jan 2021 17:41:03 +0800 Subject: [PATCH 308/565] fix atomic of macosx (#1320) See #1317 Co-authored-by: zixun --- skynet-src/skynet_server.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index d776cea83..e90133b9e 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -46,7 +46,7 @@ struct skynet_context { void * cb_ud; skynet_cb cb; struct message_queue *queue; - FILE * logfile; + ATOM_POINTER logfile; uint64_t cpu_cost; // in microsec uint64_t cpu_start; // in microsec char result[32]; @@ -141,7 +141,7 @@ skynet_context_new(const char * name, const char *param) { ctx->cb = NULL; ctx->cb_ud = NULL; ctx->session_id = 0; - ATOM_INIT(&ctx->logfile, NULL); + ATOM_INIT(&ctx->logfile, (uintptr_t)NULL); ctx->init = false; ctx->endless = false; @@ -207,7 +207,7 @@ skynet_context_reserve(struct skynet_context *ctx) { static void delete_context(struct skynet_context *ctx) { - FILE *f = ATOM_LOAD(&ctx->logfile); + FILE *f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { fclose(f); } @@ -265,7 +265,7 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); int type = msg->sz >> MESSAGE_TYPE_SHIFT; size_t sz = msg->sz & MESSAGE_TYPE_MASK; - FILE *f = ATOM_LOAD(&ctx->logfile); + FILE *f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { skynet_log_output(f, msg->source, type, msg->session, msg->data, sz); } @@ -591,12 +591,12 @@ cmd_logon(struct skynet_context * context, const char * param) { if (ctx == NULL) return NULL; FILE *f = NULL; - FILE * lastf = ATOM_LOAD(&ctx->logfile); + FILE * lastf = (FILE *)ATOM_LOAD(&ctx->logfile); if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { uintptr_t exp = 0; - if (!ATOM_CAS_POINTER(&ctx->logfile, exp, f)) { + if (!ATOM_CAS_POINTER(&ctx->logfile, exp, (uintptr_t)f)) { // logfile opens in other thread, close this one. fclose(f); } @@ -614,11 +614,11 @@ cmd_logoff(struct skynet_context * context, const char * param) { struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) return NULL; - FILE * f = ATOM_LOAD(&ctx->logfile); + FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { // logfile may close in other thread uintptr_t fptr = (uintptr_t)f; - if (ATOM_CAS_POINTER(&ctx->logfile, fptr, NULL)) { + if (ATOM_CAS_POINTER(&ctx->logfile, fptr, (uintptr_t)NULL)) { skynet_log_close(context, f, handle); } } From 53a74ca02986af0841e1cf32ad7542748c56dfde Mon Sep 17 00:00:00 2001 From: lsg2020 <2468180623@qq.com> Date: Tue, 12 Jan 2021 10:43:42 +0800 Subject: [PATCH 309/565] websocket upgrade (#1279) --- lualib/http/websocket.lua | 50 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index a2220a493..ff6a21c03 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -67,26 +67,32 @@ local function write_handshake(self, host, url, header) end -local function read_handshake(self) - local tmpline = {} - local header_body = internal.recvheader(self.read, tmpline, "") - if not header_body then - return 413 - end +local function read_handshake(self, upgrade_ops) + local header, method, url + if upgrade_ops then + header, method, url = upgrade_ops.header, upgrade_ops.method, upgrade_ops.url + else + local tmpline = {} + local header_body = internal.recvheader(self.read, tmpline, "") + if not header_body then + return 413 + end - local request = assert(tmpline[1]) - local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" - assert(method and url and httpver) - if method ~= "GET" then - return 400, "need GET method" - end + local request = assert(tmpline[1]) + local httpver + method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" + assert(method and url and httpver) + if method ~= "GET" then + return 400, "need GET method" + end - httpver = assert(tonumber(httpver)) - if httpver < 1.1 then - return 505 -- HTTP Version not supported + httpver = assert(tonumber(httpver)) + if httpver < 1.1 then + return 505 -- HTTP Version not supported + end + header = internal.parseheader(tmpline, 2, {}) end - local header = internal.parseheader(tmpline, 2, {}) if not header then return 400 -- Bad request end @@ -239,9 +245,9 @@ local function read_frame(self) end -local function resolve_accept(self) +local function resolve_accept(self, options) try_handle(self, "connect") - local code, err, url = read_handshake(self) + local code, err, url = read_handshake(self, options and options.upgrade) if code then local ok, s = httpd.write_response(self.write, code, err) if not ok then @@ -384,8 +390,10 @@ end -- handle interface -- connect / handshake / message / ping / pong / close / error -function M.accept(socket_id, handle, protocol, addr) - socket.start(socket_id) +function M.accept(socket_id, handle, protocol, addr, options) + if not (options and options.upgrade) then + socket.start(socket_id) + end protocol = protocol or "ws" local ws_obj = _new_server_ws(socket_id, handle, protocol) ws_obj.addr = addr @@ -396,7 +404,7 @@ function M.accept(socket_id, handle, protocol, addr) end) end - local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj) + local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj, options) local closed = _isws_closed(socket_id) if not closed then _close_websocket(ws_obj) From 4cb84ac881b6c160bbc25a3df871442a6fc850c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 14 Jan 2021 10:23:58 +0800 Subject: [PATCH 310/565] skynet.init rework, See #1322 (#1323) * skynet.init rework, See #1322 * restore init_list #1322 * xpcall init_all, See #1322 --- lualib/loader.lua | 2 ++ lualib/skynet.lua | 50 +++++++----------------------------- lualib/skynet/multicast.lua | 2 +- lualib/skynet/require.lua | 51 +++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 lualib/skynet/require.lua diff --git a/lualib/loader.lua b/lualib/loader.lua index 1c4ee58a2..c18597e80 100644 --- a/lualib/loader.lua +++ b/lualib/loader.lua @@ -45,4 +45,6 @@ if LUA_PRELOAD then LUA_PRELOAD = nil end +_G.require = (require "skynet.require").require + main(select(2, table.unpack(args))) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c65bb9487..ad2ba3c93 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -1,5 +1,6 @@ -- read https://github.com/cloudwu/skynet/wiki/FAQ for the module "skynet.core" local c = require "skynet.core" +local skynet_require = require "skynet.require" local tostring = tostring local coroutine = coroutine local assert = assert @@ -925,49 +926,16 @@ do } end -local init_func = {} - -function skynet.init(f, name) - assert(type(f) == "function") - if init_func == nil then - f() - else - tinsert(init_func, f) - if name then - assert(type(name) == "string") - assert(init_func[name] == nil) - init_func[name] = f - end - end -end - -local function init_all() - local funcs = init_func - init_func = nil - if funcs then - for _,f in ipairs(funcs) do - f() - end - end -end - -local function ret(f, ...) - f() - return ... -end - -local function init_template(start, ...) - init_all() - init_func = {} - return ret(init_all, start(...)) -end - -function skynet.pcall(start, ...) - return xpcall(init_template, traceback, start, ...) -end +skynet.init = skynet_require.init +-- skynet.pcall is deprecated, use pcall directly +skynet.pcall = pcall function skynet.init_service(start) - local ok, err = skynet.pcall(start) + local function main() + skynet_require.init_all() + start() + end + local ok, err = xpcall(main, traceback) if not ok then skynet.error("init service failed: " .. tostring(err)) skynet.send(".launcher","lua", "ERROR") diff --git a/lualib/skynet/multicast.lua b/lualib/skynet/multicast.lua index 07e74df6b..8b3a4e5d7 100644 --- a/lualib/skynet/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -95,6 +95,6 @@ local function init() } end -skynet.init(init, "multicast") +skynet.init(init) return multicast \ No newline at end of file diff --git a/lualib/skynet/require.lua b/lualib/skynet/require.lua new file mode 100644 index 000000000..237247269 --- /dev/null +++ b/lualib/skynet/require.lua @@ -0,0 +1,51 @@ +-- skynet module two-step initialize . When you require a skynet module : +-- 1. Run module main function as official lua module behavior. +-- 2. Run the functions register by skynet.init() during the step 1, +-- unless calling `require` in main thread . +-- If you call `require` in main thread ( service main function ), the functions +-- registered by skynet.init() do not execute immediately, they will be executed +-- by skynet.start() before start function. + +local M = {} + +local mainthread, ismain = coroutine.running() +assert(ismain, "skynet.require must initialize in main thread") + +local context = { + [mainthread] = {}, +} + +do + local require = _G.require + function M.require(...) + local co, main = coroutine.running() + if main then + return require(...) + else + local old_init_list = context[co] + local init_list = {} + context[co] = init_list + local ret = require(...) + for _, f in ipairs(init_list) do + f() + end + context[co] = old_init_list + return ret + end + end +end + +function M.init_all() + for _, f in ipairs(context[mainthread]) do + f() + end + context[mainthread] = nil +end + +function M.init(f) + assert(type(f) == "function") + local co = coroutine.running() + table.insert(context[co], f) +end + +return M \ No newline at end of file From cfb638243fe91ee3a21af4234147b1c9d057c1ed Mon Sep 17 00:00:00 2001 From: hong Date: Thu, 14 Jan 2021 11:03:32 +0800 Subject: [PATCH 311/565] skynet.exit (#1324) --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index ad2ba3c93..db79e8aa9 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -531,7 +531,7 @@ function skynet.exit() end end for session, co in pairs(session_id_coroutine) do - if type(co) == "thread" then + if type(co) == "thread" and co ~= running_thread then coroutine.close(co) end end From 4c9c9ce6bd52a6dac245e6c22a3fb1da1a8c7d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1?= <996442717qqcom@gmail.com> Date: Mon, 18 Jan 2021 10:41:30 +0800 Subject: [PATCH 312/565] debug_console add dbgcmd command (#1325) --- service/debug_console.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index c23818bea..09ce89332 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -165,6 +165,7 @@ function COMMAND.help() profactive = "profactive [on|off] : active/deactive jemalloc heap profilling", dumpheap = "dumpheap : dump heap profilling", killtask = "killtask address threadname : threadname listed by task", + dbgcmd = "run address debug command", } end @@ -273,24 +274,25 @@ function COMMAND.inject(address, filename, ...) return output end -function COMMAND.task(address) +function COMMAND.dbgcmd(address, cmd, ...) address = adjust_address(address) - return skynet.call(address,"debug","TASK") + return skynet.call(address, "debug", cmd, ...) +end + +function COMMAND.task(address) + return COMMAND.dbgcmd(address, "TASK") end function COMMAND.killtask(address, threadname) - address = adjust_address(address) - return skynet.call(address, "debug", "KILLTASK", threadname) + return COMMAND.dbgcmd(address, "KILLTASK", threadname) end function COMMAND.uniqtask(address) - address = adjust_address(address) - return skynet.call(address,"debug","UNIQTASK") + return COMMAND.dbgcmd(address, "UNIQTASK") end function COMMAND.info(address, ...) - address = adjust_address(address) - return skynet.call(address,"debug","INFO", ...) + return COMMAND.dbgcmd(address, "INFO", ...) end function COMMANDX.debug(cmd) From 30279f2f8dc238453d337a63b7c7e09a10c04361 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Jan 2021 16:56:46 +0800 Subject: [PATCH 313/565] Add gameserver embed mode, see #1326 --- lualib/snax/gateserver.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index b5165e2b9..74a196692 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -141,7 +141,7 @@ function gateserver.start(handler) end } - skynet.start(function() + local function init() skynet.dispatch("lua", function (_, address, cmd, ...) local f = CMD[cmd] if f then @@ -150,7 +150,13 @@ function gateserver.start(handler) skynet.ret(skynet.pack(handler.command(cmd, address, ...))) end end) - end) + end + + if handler.embed then + init() + else + skynet.start(init) + end end return gateserver From 4532d4f146cc68c201b62a78d13c13e0c6baf13a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Jan 2021 10:34:01 +0800 Subject: [PATCH 314/565] Fix skynet.require recursion issue, See #1331 --- lualib/skynet/require.lua | 77 +++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/lualib/skynet/require.lua b/lualib/skynet/require.lua index 237247269..52f33a746 100644 --- a/lualib/skynet/require.lua +++ b/lualib/skynet/require.lua @@ -17,21 +17,76 @@ local context = { do local require = _G.require - function M.require(...) + local loaded = package.loaded + local loading = {} + + function M.require(name) + local m = loaded[name] + if m ~= nil then + return m + end + local co, main = coroutine.running() if main then - return require(...) - else - local old_init_list = context[co] - local init_list = {} - context[co] = init_list - local ret = require(...) - for _, f in ipairs(init_list) do - f() + return require(name) + end + + local filename = package.searchpath(name, package.path) + if not filename then + return require(name) + end + + local modfunc = loadfile(filename) + if not modfunc then + return require(name) + end + + local loading_queue = loading[name] + if loading_queue then + -- Module is in the init process (require the same mod at the same time in different coroutines) , waiting. + local skynet = require "skynet" + loading_queue[#loading_queue+1] = co + print ("Waiting " .. name) + skynet.wait(co) + local m = loaded[name] + print ("Waiting OK : " .. tostring(m)) + if m == nil then + error(string.format("require %s failed", name)) end - context[co] = old_init_list - return ret + return m end + + loading_queue = {} + loading[name] = loading_queue + + local old_init_list = context[co] + local init_list = {} + context[co] = init_list + + -- We should call modfunc in lua, because modfunc may yield by calling M.require recursive. + local m = modfunc(name, filename) + + for _, f in ipairs(init_list) do + f() + end + context[co] = old_init_list + + if m == nil then + m = true + end + + package.loaded[name] = m + + local waiting = #loading_queue + if waiting > 0 then + local skynet = require "skynet" + for i = 1, waiting do + skynet.wakeup(loading_queue[i]) + end + end + loading[name] = nil + + return m end end From 41cc6d226c9dd68dc8ef541040c2ca732e4bdc58 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Jan 2021 10:47:06 +0800 Subject: [PATCH 315/565] remove debug print --- lualib/skynet/require.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lualib/skynet/require.lua b/lualib/skynet/require.lua index 52f33a746..b4ec9dd8f 100644 --- a/lualib/skynet/require.lua +++ b/lualib/skynet/require.lua @@ -46,10 +46,8 @@ do -- Module is in the init process (require the same mod at the same time in different coroutines) , waiting. local skynet = require "skynet" loading_queue[#loading_queue+1] = co - print ("Waiting " .. name) skynet.wait(co) local m = loaded[name] - print ("Waiting OK : " .. tostring(m)) if m == nil then error(string.format("require %s failed", name)) end From 4534fb5269069ead0e4a3ccb4498580bc406f861 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Jan 2021 20:31:04 +0800 Subject: [PATCH 316/565] pcall execute_module, #1331 --- lualib/skynet/require.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lualib/skynet/require.lua b/lualib/skynet/require.lua index b4ec9dd8f..b902af133 100644 --- a/lualib/skynet/require.lua +++ b/lualib/skynet/require.lua @@ -62,18 +62,23 @@ do context[co] = init_list -- We should call modfunc in lua, because modfunc may yield by calling M.require recursive. - local m = modfunc(name, filename) + local function execute_module() + local m = modfunc(name, filename) - for _, f in ipairs(init_list) do - f() - end - context[co] = old_init_list + for _, f in ipairs(init_list) do + f() + end - if m == nil then - m = true + if m == nil then + m = true + end + + loaded[name] = m end - package.loaded[name] = m + local ok, err = xpcall(execute_module, debug.traceback) + + context[co] = old_init_list local waiting = #loading_queue if waiting > 0 then @@ -84,7 +89,11 @@ do end loading[name] = nil - return m + if ok then + return loaded[name] + else + error(err) + end end end From e23ad8fc6b7bb6812e4e892033de3f1a4f9f52ef Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Feb 2021 16:04:01 +0800 Subject: [PATCH 317/565] fix type --- service-src/databuffer.h | 2 +- service-src/service_gate.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/service-src/databuffer.h b/service-src/databuffer.h index 1563466cc..5fbc79d01 100644 --- a/service-src/databuffer.h +++ b/service-src/databuffer.h @@ -62,7 +62,7 @@ _return_message(struct databuffer *db, struct messagepool *mp) { } static void -databuffer_read(struct databuffer *db, struct messagepool *mp, void * buffer, int sz) { +databuffer_read(struct databuffer *db, struct messagepool *mp, char * buffer, int sz) { assert(db->size >= sz); db->size -= sz; for (;;) { diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 0a7f6c8e2..35fa3ea63 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -175,13 +175,13 @@ _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); + databuffer_read(&c->buffer,&g->mp,(char *)temp, size); skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, fd, temp, size); return; } if (c->agent) { void * temp = skynet_malloc(size); - databuffer_read(&c->buffer,&g->mp,temp, size); + databuffer_read(&c->buffer,&g->mp,(char *)temp, size); skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, fd , temp, size); } else if (g->watchdog) { char * tmp = skynet_malloc(size + 32); From 1a0461a9afafd8cec4bbf01a00ccc4f47157a837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 3 Feb 2021 16:49:40 +0800 Subject: [PATCH 318/565] Halfclose (#1338) * post SOCKET_CLOSE when HALFCLOSE_READ, see #1331 * Add closing status * Add closing stat in info, See #1333 --- lualib-src/lua-socket.c | 3 ++ skynet-src/atomic.h | 2 - skynet-src/socket_info.h | 1 + skynet-src/socket_server.c | 96 +++++++++++++++++++++++--------------- 4 files changed, 62 insertions(+), 40 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 0da565a92..0f7353001 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -742,6 +742,9 @@ getinfo(lua_State *L, struct socket_info *si) { case SOCKET_INFO_BIND: lua_pushstring(L, "BIND"); break; + case SOCKET_INFO_CLOSING: + lua_pushstring(L, "CLOSING"); + break; default: lua_pushstring(L, "UNKNOWN"); lua_setfield(L, -2, "type"); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index d327bcddc..a7b605342 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -3,7 +3,6 @@ #ifdef __STDC_NO_ATOMICS__ -#define ATOM_BYTE unsigned char #define ATOM_INT int #define ATOM_POINTER void * #define ATOM_SIZET size_t @@ -23,7 +22,6 @@ #include -#define ATOM_BYTE atomic_uchar #define ATOM_INT atomic_int #define ATOM_POINTER atomic_uintptr_t #define ATOM_SIZET atomic_size_t diff --git a/skynet-src/socket_info.h b/skynet-src/socket_info.h index 739536e88..a22511ab7 100644 --- a/skynet-src/socket_info.h +++ b/skynet-src/socket_info.h @@ -6,6 +6,7 @@ #define SOCKET_INFO_TCP 2 #define SOCKET_INFO_UDP 3 #define SOCKET_INFO_BIND 4 +#define SOCKET_INFO_CLOSING 5 #include diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index aee51575b..bb38a3309 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -29,9 +29,10 @@ #define SOCKET_TYPE_LISTEN 3 #define SOCKET_TYPE_CONNECTING 4 #define SOCKET_TYPE_CONNECTED 5 -#define SOCKET_TYPE_HALFCLOSE 6 -#define SOCKET_TYPE_PACCEPT 7 -#define SOCKET_TYPE_BIND 8 +#define SOCKET_TYPE_HALFCLOSE_READ 6 +#define SOCKET_TYPE_HALFCLOSE_WRITE 7 +#define SOCKET_TYPE_PACCEPT 8 +#define SOCKET_TYPE_BIND 9 #define MAX_SOCKET (1<alloc_id), 0x7fffffff) & 0x7fffffff; } struct socket *s = &ss->slot[HASH_ID(id)]; - unsigned char type_invalid = ATOM_LOAD(&s->type); + int type_invalid = ATOM_LOAD(&s->type); if (type_invalid == SOCKET_TYPE_INVALID) { if (ATOM_CAS(&s->type, type_invalid, SOCKET_TYPE_RESERVE)) { s->id = id; @@ -564,6 +566,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->fd = fd; s->reading = true; s->writing = false; + s->closing = false; ATOM_INIT(&s->sending , ID_TAG16(id) << 16 | 0); s->protocol = protocol; s->p.size = MIN_READ_BUFFER; @@ -674,6 +677,17 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_ERR; } +static int +close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ) { + force_close(ss,s,l,result); + return SOCKET_CLOSE; + } else { + ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); + return -1; + } +} + static int send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_lock *l, struct socket_message *result) { while (list->head) { @@ -687,8 +701,7 @@ send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, case AGAIN_WOULDBLOCK: return -1; } - force_close(ss,s,l,result); - return SOCKET_CLOSE; + return close_write(ss, s, l, result); } stat_write(ss,s,(int)sz); s->wb_size -= sz; @@ -827,12 +840,18 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, if (send_list(ss,s,&s->high,l,result) == SOCKET_CLOSE) { return SOCKET_CLOSE; } + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE) { + return -1; + } if (s->high.head == NULL) { // step 2 if (s->low.head != NULL) { if (send_list(ss,s,&s->low,l,result) == SOCKET_CLOSE) { return SOCKET_CLOSE; } + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE) { + return -1; + } // step 3 if (list_uncomplete(&s->low)) { raise_uncomplete(s); @@ -843,13 +862,14 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, } // step 4 assert(send_buffer_empty(s) && s->wb_size == 0); - int err = enable_write(ss, s, false); - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE) { - force_close(ss, s, l, result); - return SOCKET_CLOSE; + if (s->closing) { + force_close(ss, s, l, result); + return SOCKET_CLOSE; } + int err = enable_write(ss, s, false); + if (err) { result->opaque = s->opaque; result->id = s->id; @@ -859,12 +879,12 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, } 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; + s->warn_size = 0; + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + return SOCKET_WARNING; } } @@ -970,8 +990,9 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock send_object_init(ss, &so, request->buffer, request->sz); uint8_t type = ATOM_LOAD(&s->type); if (type == SOCKET_TYPE_INVALID || s->id != id - || type == SOCKET_TYPE_HALFCLOSE - || type == SOCKET_TYPE_PACCEPT) { + || type == SOCKET_TYPE_HALFCLOSE_WRITE + || type == SOCKET_TYPE_PACCEPT + || s->closing) { so.free_func((void *)request->buffer); return -1; } @@ -992,7 +1013,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { // udp type mismatch, just drop it. - skynet_error(NULL, "socket-server: udp socket (%d) type mistach.", id); + skynet_error(NULL, "socket-server: udp socket (%d) type mismatch.", id); so.free_func((void *)request->buffer); return -1; } @@ -1060,7 +1081,8 @@ listen_socket(struct socket_server *ss, struct request_listen * request, struct static inline int nomore_sending_data(struct socket *s) { - return send_buffer_empty(s) && s->dw_buffer == NULL && (ATOM_LOAD(&s->sending) & 0xffff) == 0; + return (send_buffer_empty(s) && s->dw_buffer == NULL && (ATOM_LOAD(&s->sending) & 0xffff) == 0) + || (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE); } static int @@ -1076,20 +1098,15 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc } struct socket_lock l; socket_lock_init(s, &l); - if (!nomore_sending_data(s)) { - enable_read(ss,s,true); - int type = send_buffer(ss,s,&l,result); - // type : -1 or SOCKET_WARNING or SOCKET_CLOSE, SOCKET_WARNING means nomore_sending_data - if (type != -1 && type != SOCKET_WARNING) - return type; - } + if (request->shutdown || nomore_sending_data(s)) { force_close(ss,s,&l,result); - result->id = id; - result->opaque = request->opaque; return SOCKET_CLOSE; } - ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE); + + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); + s->closing = true; + enable_read(ss,s,true); shutdown(s->fd, SHUT_RD); return -1; @@ -1142,7 +1159,7 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "transfer"; return SOCKET_OPEN; } - // if s->type == SOCKET_TYPE_HALFCLOSE , SOCKET_CLOSE message will send later + // if s->type == SOCKET_TYPE_HALFCLOSE_* , SOCKET_CLOSE message will send later return -1; } @@ -1368,15 +1385,14 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo FREE(buffer); if (nomore_sending_data(s)) { force_close(ss,s,l,result); - return SOCKET_CLOSE; } else { - ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE); + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); shutdown(s->fd, SHUT_RD); - return -1; } + return SOCKET_CLOSE; } - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE) { + if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ) { // discard recv data FREE(buffer); return -1; @@ -1737,7 +1753,7 @@ int socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; - if (socket_invalid(s, id)) { + if (socket_invalid(s, id) || s->closing) { free_buffer(ss, buf); return -1; } @@ -2167,6 +2183,7 @@ static int query_info(struct socket *s, struct socket_info *si) { union sockaddr_all u; socklen_t slen = sizeof(u); + int closing = 0; switch (ATOM_LOAD(&s->type)) { case SOCKET_TYPE_BIND: si->type = SOCKET_INFO_BIND; @@ -2178,9 +2195,12 @@ query_info(struct socket *s, struct socket_info *si) { getname(&u, si->name, sizeof(si->name)); } break; + case SOCKET_TYPE_HALFCLOSE_READ: + case SOCKET_TYPE_HALFCLOSE_WRITE: + closing = 1; case SOCKET_TYPE_CONNECTED: if (s->protocol == PROTOCOL_TCP) { - si->type = SOCKET_INFO_TCP; + si->type = closing ? SOCKET_INFO_CLOSING : SOCKET_INFO_TCP; if (getpeername(s->fd, &u.s, &slen) == 0) { getname(&u, si->name, sizeof(si->name)); } From 6d941a6de170508f9e45189e2763936338c2b99b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Feb 2021 08:40:16 +0800 Subject: [PATCH 319/565] support nil in array, fix #1340 --- lualib/skynet/db/redis.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lualib/skynet/db/redis.lua b/lualib/skynet/db/redis.lua index 37aeb00ea..9b3944895 100644 --- a/lualib/skynet/db/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -101,14 +101,21 @@ local function compose_message(cmd, msg) local lines = {} if t == "table" then - lines[1] = count_cache[#msg+1] + local n = msg.n or #msg + lines[1] = count_cache[n+1] lines[2] = command_cache[cmd] local idx = 3 - for _,v in ipairs(msg) do - v= tostring(v) - lines[idx] = header_cache[#v] - lines[idx+1] = v - idx = idx + 2 + for i = 1, n do + v = msg[i] + if v == nil then + lines[idx] = "\r\n$-1" + idx = idx + 1 + else + v= tostring(v) + lines[idx] = header_cache[#v] + lines[idx+1] = v + idx = idx + 2 + end end lines[idx] = "\r\n" else @@ -156,7 +163,7 @@ setmetatable(command, { __index = function(t,k) 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) + return self[1]:request(compose_message(cmd, table.pack(v, ...)), read_response) end end t[k] = f @@ -175,7 +182,7 @@ 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", table.pack(key, value)), read_boolean) end local function compose_table(lines, msg) From 51a942d095b23ad6a871ccf24c62a62f4ba0a8ff Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Feb 2021 09:41:59 +0800 Subject: [PATCH 320/565] bugfix: disable fd reading flag after shutdown read --- skynet-src/socket_server.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index bb38a3309..44c0d9ec5 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1141,6 +1141,8 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "invalid socket"; return SOCKET_ERR; } + if (s->closing) + return -1; struct socket_lock l; socket_lock_init(s, &l); if (enable_read(ss, s, true)) { @@ -1385,14 +1387,18 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo FREE(buffer); if (nomore_sending_data(s)) { force_close(ss,s,l,result); - } else { + } else { ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); - shutdown(s->fd, SHUT_RD); + if (!s->closing) { + shutdown(s->fd, SHUT_RD); + s->closing = true; + } + enable_read(ss, s, false); } return SOCKET_CLOSE; } - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ) { + if (s->closing) { // discard recv data FREE(buffer); return -1; From eebd44da9dd95754da5457950ddd63119a94dc66 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Feb 2021 12:00:32 +0800 Subject: [PATCH 321/565] fix #1341 --- lualib/skynet/db/redis.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/db/redis.lua b/lualib/skynet/db/redis.lua index 9b3944895..b1def3fd4 100644 --- a/lualib/skynet/db/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -96,7 +96,17 @@ local count_cache = make_cache(function(t,k) return s end) +local command_np_cache = make_cache(function(t, cmd) + local s = "*1" .. command_cache[cmd] .. "\r\n" + t[cmd] = s + return s + end) + local function compose_message(cmd, msg) + if msg == nil then + return command_np_cache[cmd] + end + local t = type(msg) local lines = {} @@ -160,7 +170,9 @@ end setmetatable(command, { __index = function(t,k) local cmd = string.upper(k) local f = function (self, v, ...) - if type(v) == "table" then + if v == nil then + return self[1]:request(compose_message(cmd), read_response) + elseif type(v) == "table" then return self[1]:request(compose_message(cmd, v), read_response) else return self[1]:request(compose_message(cmd, table.pack(v, ...)), read_response) From b62619859286f00af322d0bc0273aa832a5aedb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1?= <996442717qqcom@gmail.com> Date: Mon, 22 Feb 2021 12:24:00 +0800 Subject: [PATCH 322/565] fix global var (#1342) Co-authored-by: xiaojin --- lualib/skynet/db/redis.lua | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lualib/skynet/db/redis.lua b/lualib/skynet/db/redis.lua index b1def3fd4..8fe0e6012 100644 --- a/lualib/skynet/db/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -1,10 +1,16 @@ -local skynet = require "skynet" -local socket = require "skynet.socket" local socketchannel = require "skynet.socketchannel" +local tostring = tostring +local tonumber = tonumber local table = table local string = string local assert = assert +local setmetatable = setmetatable +local ipairs = ipairs +local type = type +local select = select +local pairs = pairs + local redis = {} local command = {} @@ -116,7 +122,7 @@ local function compose_message(cmd, msg) lines[2] = command_cache[cmd] local idx = 3 for i = 1, n do - v = msg[i] + local v = msg[i] if v == nil then lines[idx] = "\r\n$-1" idx = idx + 1 @@ -221,7 +227,7 @@ function command:pipeline(ops,resp) if resp then return fd:request(cmds, function (fd) - for i=1, #ops do + for _=1, #ops do local ok, out = read_response(fd) table.insert(resp, {ok = ok, out = out}) end @@ -230,7 +236,7 @@ function command:pipeline(ops,resp) else return fd:request(cmds, function (fd) local ok, out - for i=1, #ops do + for _=1, #ops do ok, out = read_response(fd) end -- return last response @@ -307,18 +313,18 @@ function watch:message() local so = self.__sock while true do local ret = so:response(read_response) - local type , channel, data , data2 = ret[1], ret[2], ret[3], ret[4] - if type == "message" then + local ttype , channel, data , data2 = ret[1], ret[2], ret[3], ret[4] + if ttype == "message" then return data, channel - elseif type == "pmessage" then + elseif ttype == "pmessage" then return data2, data, channel - elseif type == "subscribe" then + elseif ttype == "subscribe" then self.__subscribe[channel] = true - elseif type == "psubscribe" then + elseif ttype == "psubscribe" then self.__psubscribe[channel] = true - elseif type == "unsubscribe" then + elseif ttype == "unsubscribe" then self.__subscribe[channel] = nil - elseif type == "punsubscribe" then + elseif ttype == "punsubscribe" then self.__psubscribe[channel] = nil end end From 8e45679d3dbfe1af62b50d15514aa7f188bcf43e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 23 Feb 2021 16:19:01 +0800 Subject: [PATCH 323/565] bugfix : coroutine.wrap for snlua --- service-src/service_snlua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 81a1604a5..4e9381ce8 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -195,7 +195,7 @@ timing_resume(lua_State *L, int co_index, int n) { lua_rawset(L, lua_upvalueindex(1)); // set start time } - int r = auxresume(L, co, lua_gettop(L) - 1); + int r = auxresume(L, co, n); if (timing_enable(L, co_index, &start_time)) { double total_time = timing_total(L, co_index); From 04dd921c5f0d0851f549bb5c38aa8e07964a7475 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Feb 2021 10:57:54 +0800 Subject: [PATCH 324/565] Fix socket half close issues, see #1346 for details --- skynet-src/socket_epoll.h | 4 +- skynet-src/socket_server.c | 125 +++++++++++++++++++++++++------------ 2 files changed, 87 insertions(+), 42 deletions(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index f3c3225d3..753912a1a 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -61,9 +61,9 @@ 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 | EPOLLHUP)) != 0; + e[i].read = (flag & EPOLLIN) != 0; e[i].error = (flag & EPOLLERR) != 0; - e[i].eof = false; + e[i].eof = (flag & EPOLLHUP) != 0; } return n; diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 44c0d9ec5..6dd07fd63 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -677,14 +677,12 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_ERR; } -static int +static void close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ) { + if (s->closing) { force_close(ss,s,l,result); - return SOCKET_CLOSE; } else { ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); - return -1; } } @@ -701,7 +699,8 @@ send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, case AGAIN_WOULDBLOCK: return -1; } - return close_write(ss, s, l, result); + close_write(ss, s, l, result); + return -1; } stat_write(ss,s,(int)sz); s->wb_size -= sz; @@ -865,7 +864,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, if (s->closing) { force_close(ss, s, l, result); - return SOCKET_CLOSE; + return -1; } int err = enable_write(ss, s, false); @@ -1090,26 +1089,22 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { - result->id = id; - result->opaque = request->opaque; - result->ud = 0; - result->data = NULL; - return SOCKET_CLOSE; + // The socket is closed, ignore + return -1; } struct socket_lock l; socket_lock_init(s, &l); if (request->shutdown || nomore_sending_data(s)) { force_close(ss,s,&l,result); - return SOCKET_CLOSE; + } else { + // Don't read socket later + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); + s->closing = true; + enable_read(ss,s,false); + shutdown(s->fd, SHUT_RD); } - - ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); - s->closing = true; - enable_read(ss,s,true); - shutdown(s->fd, SHUT_RD); - - return -1; + return SOCKET_CLOSE; } static int @@ -1361,14 +1356,67 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { return -1; } +struct stream_buffer { + char * buf; + int sz; +}; + +static char * +reserve_buffer(struct stream_buffer *buffer, int sz) { + if (buffer->buf == NULL) { + buffer->buf = (char *)MALLOC(sz); + return buffer->buf; + } else { + char * newbuffer = (char *)MALLOC(sz + buffer->sz); + memcpy(newbuffer, buffer->buf, buffer->sz); + char * ret = newbuffer + buffer->sz; + FREE(buffer->buf); + buffer->buf = newbuffer; + return ret; + } +} + +static int +read_socket(struct socket *s, struct stream_buffer *buffer) { + int sz = s->p.size; + buffer->buf = NULL; + buffer->sz = 0; + for (;;) { + char *buf = reserve_buffer(buffer, sz); + int n = (int)read(s->fd, buf, sz); + if (n <= 0) { + if (buffer->sz == 0) { + // read nothing + FREE(buffer->buf); + return n; + } else { + // ignore the error or hang up, returns buffer + // If socket is hang up, SOCKET_CLOSE will be send later. + // (buffer->sz should be 0 next time) + break; + } + } + buffer->sz += n; + if (n < sz) { + break; + } + // n == sz, read again + } + int r = buffer->sz; + if (r > sz) { + s->p.size = sz * 2; + } else if (sz > MIN_READ_BUFFER && r*2 < sz) { + s->p.size = sz / 2; + } + return r; +} + // return -1 (ignore) when error static int forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message * result) { - int sz = s->p.size; - char * buffer = MALLOC(sz); - int n = (int)read(s->fd, buffer, sz); + struct stream_buffer buf; + int n = read_socket(s, &buf); if (n<0) { - FREE(buffer); switch(errno) { case EINTR: break; @@ -1384,38 +1432,28 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo return -1; } if (n==0) { - FREE(buffer); if (nomore_sending_data(s)) { force_close(ss,s,l,result); - } else { - ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); - if (!s->closing) { - shutdown(s->fd, SHUT_RD); - s->closing = true; - } - enable_read(ss, s, false); + } + if (s->closing) { + // Already returned SOCKET_CLOSE + return -1; } return SOCKET_CLOSE; } if (s->closing) { // discard recv data - FREE(buffer); + FREE(buf.buf); return -1; } stat_read(ss,s,n); - if (n == sz) { - s->p.size *= 2; - } else if (sz > MIN_READ_BUFFER && n*2 < sz) { - s->p.size /= 2; - } - result->opaque = s->opaque; result->id = s->id; result->ud = n; - result->data = buffer; + result->data = buf.buf; return SOCKET_DATA; } @@ -1695,8 +1733,15 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int return SOCKET_ERR; } if(e->eof) { + // For epoll (at least), FIN packets are exchanged both ways. + // See: https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated force_close(ss, s, &l, result); - return SOCKET_CLOSE; + if (s->closing) { + // Already returned SOCKET_CLOSE + return -1; + } else { + return SOCKET_CLOSE; + } } break; } From 6a4a2482fc45563a4b275ad4fab75b6ff3065124 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Feb 2021 11:33:35 +0800 Subject: [PATCH 325/565] expand buffer faster --- skynet-src/socket_server.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 6dd07fd63..4df1daa15 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1381,9 +1381,10 @@ read_socket(struct socket *s, struct stream_buffer *buffer) { int sz = s->p.size; buffer->buf = NULL; buffer->sz = 0; + int rsz = sz; for (;;) { - char *buf = reserve_buffer(buffer, sz); - int n = (int)read(s->fd, buf, sz); + char *buf = reserve_buffer(buffer, rsz); + int n = (int)read(s->fd, buf, rsz); if (n <= 0) { if (buffer->sz == 0) { // read nothing @@ -1397,10 +1398,11 @@ read_socket(struct socket *s, struct stream_buffer *buffer) { } } buffer->sz += n; - if (n < sz) { + if (n < rsz) { break; } - // n == sz, read again + // n == rsz, read again ( and read more ) + rsz *= 2; } int r = buffer->sz; if (r > sz) { From 6a8ca29d99eda2a5d6d8848fef2d964024174113 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Feb 2021 11:54:47 +0800 Subject: [PATCH 326/565] set result for SOCKET_CLOSE, see #1347 --- skynet-src/socket_server.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 4df1daa15..07db9d8b9 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1103,6 +1103,10 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc s->closing = true; enable_read(ss,s,false); shutdown(s->fd, SHUT_RD); + result->id = s->id; + result->ud = 0; + result->data = NULL; + result->opaque = s->opaque; } return SOCKET_CLOSE; } @@ -1436,6 +1440,11 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo if (n==0) { if (nomore_sending_data(s)) { force_close(ss,s,l,result); + } else { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; } if (s->closing) { // Already returned SOCKET_CLOSE From 4bb883b989bd5dda7efb3c8061bb46e932bc64d0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Feb 2021 17:02:23 +0800 Subject: [PATCH 327/565] report write error --- skynet-src/socket_server.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 07db9d8b9..17e4116fa 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -677,12 +677,18 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_ERR; } -static void +static int close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { if (s->closing) { force_close(ss,s,l,result); + return SOCKET_CLOSE; } else { ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); + result->id = s->id; + result->ud = 0; + result->opaque = s->opaque; + result->data = strerror(errno); + return SOCKET_ERR; } } @@ -699,8 +705,7 @@ send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, case AGAIN_WOULDBLOCK: return -1; } - close_write(ss, s, l, result); - return -1; + return close_write(ss, s, l, result); } stat_write(ss,s,(int)sz); s->wb_size -= sz; @@ -836,19 +841,24 @@ static int send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { assert(!list_uncomplete(&s->low)); // step 1 - if (send_list(ss,s,&s->high,l,result) == SOCKET_CLOSE) { - return SOCKET_CLOSE; - } - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE) { + int ret = send_list(ss,s,&s->high,l,result); + if (ret != -1) { + if (ret == SOCKET_ERR) { + // HALFCLOSE_WRITE + return SOCKET_ERR; + } + // SOCKET_CLOSE return -1; } if (s->high.head == NULL) { // step 2 if (s->low.head != NULL) { - if (send_list(ss,s,&s->low,l,result) == SOCKET_CLOSE) { - return SOCKET_CLOSE; - } - if (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE) { + int ret = send_list(ss,s,&s->high,l,result); + if (ret != -1) { + if (ret == SOCKET_ERR) { + // HALFCLOSE_WRITE + return SOCKET_ERR; + } return -1; } // step 3 From ccac1d4526ac8830fe28aca32f76805a3689ff23 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Feb 2021 19:59:12 +0800 Subject: [PATCH 328/565] fix #1348 --- 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 17e4116fa..23e553197 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -853,7 +853,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, if (s->high.head == NULL) { // step 2 if (s->low.head != NULL) { - int ret = send_list(ss,s,&s->high,l,result); + int ret = send_list(ss,s,&s->low,l,result); if (ret != -1) { if (ret == SOCKET_ERR) { // HALFCLOSE_WRITE From 29a212db8ed4bd0b8fb7e68db3fcd9b41db78ab9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Feb 2021 22:30:57 +0800 Subject: [PATCH 329/565] Shutdown SHUT_RD after read 0, See #1346 for detail --- skynet-src/socket_server.c | 48 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 23e553197..8badffaae 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1094,6 +1094,23 @@ nomore_sending_data(struct socket *s) { || (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE); } +static void +close_read(struct socket_server *ss, struct socket * s, struct socket_message *result) { + // Don't read socket later + ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); + enable_read(ss,s,false); + shutdown(s->fd, SHUT_RD); + result->id = s->id; + result->ud = 0; + result->data = NULL; + result->opaque = s->opaque; +} + +static inline int +halfclose_read(struct socket *s) { + return ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ; +} + static int close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) { int id = request->id; @@ -1108,15 +1125,8 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc if (request->shutdown || nomore_sending_data(s)) { force_close(ss,s,&l,result); } else { - // Don't read socket later - ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); s->closing = true; - enable_read(ss,s,false); - shutdown(s->fd, SHUT_RD); - result->id = s->id; - result->ud = 0; - result->data = NULL; - result->opaque = s->opaque; + close_read(ss, s, result); } return SOCKET_CLOSE; } @@ -1150,7 +1160,7 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "invalid socket"; return SOCKET_ERR; } - if (s->closing) + if (halfclose_read(s)) return -1; struct socket_lock l; socket_lock_init(s, &l); @@ -1448,22 +1458,20 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo return -1; } if (n==0) { - if (nomore_sending_data(s)) { - force_close(ss,s,l,result); - } else { - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = NULL; - } if (s->closing) { - // Already returned SOCKET_CLOSE - return -1; + if (nomore_sending_data(s)) { + force_close(ss,s,l,result); + return SOCKET_CLOSE; + } else { + // Already returned SOCKET_CLOSE + return -1; + } } + close_read(ss, s, result); return SOCKET_CLOSE; } - if (s->closing) { + if (halfclose_read(s)) { // discard recv data FREE(buf.buf); return -1; From 0d949678b95ef88f4bdfe455c1fe7cf84d02f018 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Mar 2021 11:18:56 +0800 Subject: [PATCH 330/565] shutdown write when write error, See #1346 --- skynet-src/socket_server.c | 59 ++++++++++++++++++++++++++++++-------- skynet-src/socket_server.h | 1 + 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 8badffaae..2cfb1cea8 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -681,9 +681,21 @@ static int close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { if (s->closing) { force_close(ss,s,l,result); - return SOCKET_CLOSE; + return SOCKET_RST; } else { + int t = ATOM_LOAD(&s->type); + if (t == SOCKET_TYPE_HALFCLOSE_READ) { + // recv 0 before, ignore the error and close fd + force_close(ss,s,l,result); + return SOCKET_RST; + } + if (t == SOCKET_TYPE_HALFCLOSE_WRITE) { + // already raise SOCKET_ERR + return SOCKET_RST; + } ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); + shutdown(s->fd, SHUT_WR); + enable_write(ss, s, false); result->id = s->id; result->ud = 0; result->opaque = s->opaque; @@ -847,7 +859,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, // HALFCLOSE_WRITE return SOCKET_ERR; } - // SOCKET_CLOSE + // SOCKET_RST (ignore) return -1; } if (s->high.head == NULL) { @@ -859,6 +871,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, // HALFCLOSE_WRITE return SOCKET_ERR; } + // SOCKET_RST (ignore) return -1; } // step 3 @@ -873,6 +886,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, assert(send_buffer_empty(s) && s->wb_size == 0); if (s->closing) { + // finish writing force_close(ss, s, l, result); return -1; } @@ -1111,6 +1125,11 @@ halfclose_read(struct socket *s) { return ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ; } +// SOCKET_CLOSE can be raised (only once) in one of two conditions. +// See https://github.com/cloudwu/skynet/issues/1346 for more discussion. +// 1. close socket by self, See close_socket() +// 2. recv 0 or eof event (close socket by remote), See forward_message_tcp() +// It's able to write data after SOCKET_CLOSE (In condition 2), but if remote is closed, SOCKET_ERR may raised. static int close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) { int id = request->id; @@ -1122,13 +1141,21 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc struct socket_lock l; socket_lock_init(s, &l); + int shutdown_read = halfclose_read(s); + if (request->shutdown || nomore_sending_data(s)) { + // If socket is SOCKET_TYPE_HALFCLOSE_READ, Do not raise SOCKET_CLOSE again. + int r = shutdown_read ? -1 : SOCKET_CLOSE; force_close(ss,s,&l,result); - } else { + return r; + } else if (!shutdown_read) { s->closing = true; + // don't read socket after socket.close() close_read(ss, s, result); + return SOCKET_CLOSE; } - return SOCKET_CLOSE; + // recv 0 before (socket is SOCKET_TYPE_HALFCLOSE_READ) and waiting for sending data out. + return -1; } static int @@ -1459,20 +1486,28 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo } if (n==0) { if (s->closing) { + // Rare case : if s->closing is true, reading event is disable, and SOCKET_CLOSE is raised. if (nomore_sending_data(s)) { force_close(ss,s,l,result); - return SOCKET_CLOSE; - } else { - // Already returned SOCKET_CLOSE - return -1; } + return -1; + } + int t = ATOM_LOAD(&s->type); + if (t == SOCKET_TYPE_HALFCLOSE_READ) { + // Rare case : Already shutdown read. + return -1; + } + if (t == SOCKET_TYPE_HALFCLOSE_WRITE) { + // Remote shutdown read (write error) before. + force_close(ss,s,l,result); + } else { + close_read(ss, s, result); } - close_read(ss, s, result); return SOCKET_CLOSE; } if (halfclose_read(s)) { - // discard recv data + // discard recv data (Rare case : if socket is HALFCLOSE_READ, reading event is disable.) FREE(buf.buf); return -1; } @@ -1765,8 +1800,8 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int // For epoll (at least), FIN packets are exchanged both ways. // See: https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated force_close(ss, s, &l, result); - if (s->closing) { - // Already returned SOCKET_CLOSE + if (halfclose_read(s)) { + // Already rasied SOCKET_CLOSE return -1; } else { return SOCKET_CLOSE; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 05ea12898..bf79caf48 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -13,6 +13,7 @@ #define SOCKET_EXIT 5 #define SOCKET_UDP 6 #define SOCKET_WARNING 7 +#define SOCKET_RST 8 // Only for internal use struct socket_server; From 77c965e9e849897f83f1ca34cad6d1d9db34542e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Mar 2021 17:08:12 +0800 Subject: [PATCH 331/565] Don't support old MacOSX --- skynet-src/skynet_timer.c | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index bf54dc3ca..90ceb17e5 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -12,11 +12,12 @@ #include #include -#if defined(__APPLE__) -#include +#ifdef __APPLE__ + #include #include #include + #endif typedef void (*timer_execute_func)(void *ud,void *arg); @@ -233,33 +234,19 @@ skynet_timeout(uint32_t handle, int time, int session) { // centisecond: 1/100 second static void systime(uint32_t *sec, uint32_t *cs) { -#if !defined(__APPLE__) || defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER) struct timespec ti; 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); - *sec = tv.tv_sec; - *cs = tv.tv_usec / 10000; -#endif } static uint64_t gettime() { uint64_t t; -#if !defined(__APPLE__) || defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER) struct timespec ti; clock_gettime(CLOCK_MONOTONIC, &ti); t = (uint64_t)ti.tv_sec * 100; t += ti.tv_nsec / 10000000; -#else - struct timeval tv; - gettimeofday(&tv, NULL); - t = (uint64_t)tv.tv_sec * 100; - t += tv.tv_usec / 10000; -#endif return t; } @@ -306,18 +293,8 @@ skynet_timer_init(void) { uint64_t skynet_thread_time(void) { -#if !defined(__APPLE__) || defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER) 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 } From c12a7d1b992e12dc08191f2fffde138769ed3c33 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 4 Mar 2021 11:40:15 +0800 Subject: [PATCH 332/565] Remove unused include --- skynet-src/skynet_timer.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 90ceb17e5..143dd742a 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -12,14 +12,6 @@ #include #include -#ifdef __APPLE__ - -#include -#include -#include - -#endif - typedef void (*timer_execute_func)(void *ud,void *arg); #define TIME_NEAR_SHIFT 8 From 8d895ab2f0e70a912d02d5bb311ddf97c2d80ec2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 5 Mar 2021 21:36:52 +0800 Subject: [PATCH 333/565] See #1317 --- 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 2cfb1cea8..2595bca0f 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1687,7 +1687,7 @@ clear_closed_event(struct socket_server *ss, struct socket_message * result, int struct event *e = &ss->ev[i]; struct socket *s = e->s; if (s) { - if (socket_invalid(s, id)) { + if (socket_invalid(s, id) && s->id == id) { e->s = NULL; break; } From 0d9bfcb721cd8af12a021b2f94603e656c7b9d2d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 9 Mar 2021 10:54:49 +0800 Subject: [PATCH 334/565] Atomic variable should be volatile, #1356 #1317 --- skynet-src/atomic.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index a7b605342..5acc32df6 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -3,10 +3,12 @@ #ifdef __STDC_NO_ATOMICS__ -#define ATOM_INT int -#define ATOM_POINTER void * -#define ATOM_SIZET size_t -#define ATOM_ULONG unsigned long +#include + +#define ATOM_INT volatile int +#define ATOM_POINTER volatile uintptr_t +#define ATOM_SIZET volatile size_t +#define ATOM_ULONG volatile unsigned long #define ATOM_INIT(ptr, v) (*(ptr) = v) #define ATOM_LOAD(ptr) (*(ptr)) #define ATOM_STORE(ptr, v) (*(ptr) = v) From 600a64327e31e1b272e8d2dde6af0164305dbd3d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 9 Mar 2021 17:45:03 +0800 Subject: [PATCH 335/565] fix halfclose bug, See detail #1358 --- lualib/skynet/socket.lua | 2 +- skynet-src/socket_server.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 330330e7d..e467d2a1b 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -269,8 +269,8 @@ function socket.close(id) if s == nil then return end + driver.close(id) if s.connected then - driver.close(id) if s.co then -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 2595bca0f..694e797f9 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1024,7 +1024,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock so.free_func((void *)request->buffer); return -1; } - if (send_buffer_empty(s) && type == SOCKET_TYPE_CONNECTED) { + if (send_buffer_empty(s)) { if (s->protocol == PROTOCOL_TCP) { append_sendbuffer(ss, s, request); // add to high priority list, even priority == PRIORITY_LOW } else { @@ -1148,8 +1148,9 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc int r = shutdown_read ? -1 : SOCKET_CLOSE; force_close(ss,s,&l,result); return r; - } else if (!shutdown_read) { - s->closing = true; + } + s->closing = true; + if (!shutdown_read) { // don't read socket after socket.close() close_read(ss, s, result); return SOCKET_CLOSE; From 072d78265132e242c1ee1f9d5b64d7c8dc2b4490 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 10 Mar 2021 11:53:37 +0800 Subject: [PATCH 336/565] Update lua 5.4.3 rc1 --- 3rd/lua/lapi.c | 61 +++++--- 3rd/lua/lapi.h | 2 + 3rd/lua/lauxlib.c | 74 ++++++---- 3rd/lua/lauxlib.h | 24 +++- 3rd/lua/lbaselib.c | 15 +- 3rd/lua/lcode.c | 30 ++-- 3rd/lua/lcorolib.c | 17 ++- 3rd/lua/ldblib.c | 11 +- 3rd/lua/ldebug.c | 128 ++++++++++------- 3rd/lua/ldebug.h | 11 ++ 3rd/lua/ldo.c | 350 +++++++++++++++++++++++++++++---------------- 3rd/lua/ldo.h | 3 +- 3rd/lua/lfunc.c | 160 +++++++++------------ 3rd/lua/lfunc.h | 13 +- 3rd/lua/lgc.c | 38 +++-- 3rd/lua/liolib.c | 35 +++-- 3rd/lua/llex.c | 31 ++-- 3rd/lua/llimits.h | 16 --- 3rd/lua/lmathlib.c | 5 +- 3rd/lua/lmem.c | 33 +++-- 3rd/lua/loadlib.c | 17 ++- 3rd/lua/lobject.h | 13 +- 3rd/lua/lopcodes.h | 14 +- 3rd/lua/loslib.c | 8 +- 3rd/lua/lparser.c | 54 +++---- 3rd/lua/lparser.h | 6 +- 3rd/lua/lstate.c | 59 ++++---- 3rd/lua/lstate.h | 47 +++++- 3rd/lua/lstring.c | 12 +- 3rd/lua/lstrlib.c | 111 +++++++------- 3rd/lua/ltable.c | 68 +++++---- 3rd/lua/ltable.h | 8 +- 3rd/lua/ltablib.c | 11 +- 3rd/lua/ltm.c | 5 +- 3rd/lua/lua.c | 26 +++- 3rd/lua/lua.h | 9 +- 3rd/lua/luaconf.h | 103 +++++++------ 3rd/lua/lualib.h | 6 - 3rd/lua/lvm.c | 73 +++++----- 3rd/lua/lvm.h | 6 +- 40 files changed, 1018 insertions(+), 695 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 2b7609f4b..b27374cfd 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -39,7 +39,7 @@ const char lua_ident[] = /* -** Test for a valid index. +** Test for a valid index (one that is not the 'nilvalue'). ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. ** However, it covers the most common cases in a faster way. */ @@ -74,7 +74,8 @@ static TValue *index2value (lua_State *L, int idx) { return &G(L)->nilvalue; /* it has no upvalues */ else { CClosure *func = clCvalue(s2v(ci->func)); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; + return (idx <= func->nupvalues) ? &func->upvalue[idx-1] + : &G(L)->nilvalue; } } } @@ -187,9 +188,25 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } +#if defined(LUA_COMPAT_5_4_0) if (diff < 0 && hastocloseCfunc(ci->nresults)) - luaF_close(L, L->top + diff, LUA_OK); - L->top += diff; /* correct top only after closing any upvalue */ + luaF_close(L, L->top + diff, CLOSEKTOP, 0); +#endif + api_check(L, L->tbclist < L->top + diff, "cannot pop an unclosed slot"); + L->top += diff; + lua_unlock(L); +} + + +LUA_API void lua_closeslot (lua_State *L, int idx) { + StkId level; + lua_lock(L); + level = index2stack(L, idx); + api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, + "no variable to close at given level"); + luaF_close(L, level, CLOSEKTOP, 0); + level = index2stack(L, idx); /* stack may be moved */ + setnilvalue(s2v(level)); lua_unlock(L); } @@ -629,11 +646,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { } +/* +** Get the global table in the registry. Since all predefined +** indices in the registry were inserted right when the registry +** was created and never removed, they must always be in the array +** part of the registry. +*/ +#define getGtable(L) \ + (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) + + LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg; + const TValue *G; lua_lock(L); - reg = hvalue(&G(L)->l_registry); - return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + G = getGtable(L); + return auxgetstr(L, G, name); } @@ -811,10 +838,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg; + const TValue *G; lua_lock(L); /* unlock done in 'auxsetstr' */ - reg = hvalue(&G(L)->l_registry); - auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + G = getGtable(L); + auxsetstr(L, G, name); } @@ -861,12 +888,10 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; - TValue *slot; lua_lock(L); api_checknelems(L, n); t = gettable(L, idx); - slot = luaH_set(L, t, key); - setobj2t(L, slot, s2v(L->top - 1)); + luaH_set(L, t, key, s2v(L->top - 1)); invalidateTMcache(t); luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top -= n; @@ -912,7 +937,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { } switch (ttype(obj)) { case LUA_TTABLE: { - if (isshared(hvalue(obj))) + if (l_unlikely(isshared(hvalue(obj)))) luaG_runerror(L, "can't setmetatable to shared table"); hvalue(obj)->metatable = mt; if (mt) { @@ -1055,8 +1080,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, static void set_env (lua_State *L, LClosure *f) { 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); + const TValue *gt = getGtable(L); /* 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); @@ -1110,7 +1134,7 @@ LUA_API void lua_sharestring (lua_State *L, int index) { LUA_API void lua_clonetable(lua_State *L, const void * tp) { Table *t = cast(Table *, tp); - if (!isshared(t)) + if (l_unlikely(!isshared(t))) luaG_runerror(L, "Not a shared table"); lua_lock(L); @@ -1284,8 +1308,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { lua_lock(L); o = index2stack(L, idx); nresults = L->ci->nresults; - api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, - "marked index below or equal new one"); + api_check(L, L->tbclist < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index 41216b270..9e99cc448 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -42,6 +42,8 @@ #define hastocloseCfunc(n) ((n) < LUA_MULTRET) +/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ #define codeNresults(n) (-(n) - 3) +#define decodeNresults(n) (-(n) - 3) #endif diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 9bcab27da..17bdb84b5 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { } -int luaL_typeerror (lua_State *L, int arg, const char *tname) { +LUALIB_API int luaL_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) @@ -378,7 +378,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, ** but without 'msg'.) */ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - if (!lua_checkstack(L, space)) { + if (l_unlikely(!lua_checkstack(L, space))) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else @@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { - if (lua_type(L, arg) != t) + if (l_unlikely(lua_type(L, arg) != t)) tag_error(L, arg, t); } LUALIB_API void luaL_checkany (lua_State *L, int arg) { - if (lua_type(L, arg) == LUA_TNONE) + if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) luaL_argerror(L, arg, "value expected"); } 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); + if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); return s; } @@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; lua_Number d = lua_tonumberx(L, arg, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) tag_error(L, arg, LUA_TNUMBER); return d; } @@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) { LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; lua_Integer d = lua_tointegerx(L, arg, &isnum); - if (!isnum) { + if (l_unlikely(!isnum)) { interror(L, arg); } return d; @@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { 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? */ + if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ lua_pushliteral(L, "not enough memory"); lua_error(L); /* raise a memory error */ } @@ -515,13 +515,22 @@ static void newbox (lua_State *L) { #define buffonstack(B) ((B)->b != (B)->init.b) +/* +** Whenever buffer is accessed, slot 'idx' must either be a box (which +** cannot be NULL) or it is a placeholder for the buffer. +*/ +#define checkbufferlevel(B,idx) \ + lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ + : lua_touserdata(B->L, idx) == (void*)B) + + /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' ** bytes. */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = B->size * 2; /* double buffer size */ - if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ + if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ return luaL_error(B->L, "buffer too large"); if (newsize < B->n + sz) /* double is not big enough? */ newsize = B->n + sz; @@ -531,10 +540,11 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { /* ** Returns a pointer to a free area with at least 'sz' bytes in buffer -** 'B'. 'boxidx' is the relative position in the stack where the -** buffer's box is or should be. +** 'B'. 'boxidx' is the relative position in the stack where is the +** buffer's box or its placeholder. */ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { + checkbufferlevel(B, boxidx); if (B->size - B->n >= sz) /* enough space? */ return B->b + B->n; else { @@ -545,10 +555,9 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - lua_pushnil(L); /* reserve slot for final result */ + lua_remove(L, boxidx); /* remove placeholder */ newbox(L); /* create a new box */ - /* move box (and slot) to its intended position */ - lua_rotate(L, boxidx - 1, 2); + lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ @@ -583,11 +592,11 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; + checkbufferlevel(B, -1); lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) { - lua_copy(L, -1, -3); /* move string to reserved slot */ - lua_pop(L, 2); /* pop string and box (closing the box) */ - } + if (buffonstack(B)) + lua_closeslot(L, -2); /* close the box */ + lua_remove(L, -2); /* remove box or placeholder from the stack */ } @@ -622,6 +631,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; + lua_pushlightuserdata(L, (void*)B); /* push placeholder */ } @@ -639,10 +649,14 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { ** ======================================================= */ -/* index of free-list header */ -#define freelist 0 - +/* index of free-list header (after the predefined values) */ +#define freelist (LUA_RIDX_LAST + 1) +/* +** The previously freed references form a linked list: +** t[freelist] is the index of a first free index, or zero if list is +** empty; t[t[freelist]] is the index of the second element; etc. +*/ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { @@ -650,9 +664,16 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - lua_pop(L, 1); /* remove it from stack */ + if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ + ref = 0; /* list is empty */ + lua_pushinteger(L, 0); /* initialize as an empty list */ + lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ + } + else { /* already initialized */ + lua_assert(lua_isinteger(L, -1)); + ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ + } + lua_pop(L, 1); /* remove element from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ @@ -668,6 +689,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { t = lua_absindex(L, t); lua_rawgeti(L, t, freelist); + lua_assert(lua_isinteger(L, -1)); lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ lua_pushinteger(L, ref); lua_rawseti(L, t, freelist); /* t[freelist] = ref */ @@ -851,7 +873,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { int isnum; lua_len(L, idx); l = lua_tointegerx(L, -1, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; @@ -1064,7 +1086,7 @@ static void warnfon (void *ud, const char *message, int tocont) { LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); - if (L) { + if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfoff, L); /* default is warnings off */ } diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index e3348ad67..2c047adf3 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -124,6 +124,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, ** =============================================================== */ +#if !defined(l_likely) +#define l_likely(x) x +#endif + #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) @@ -132,10 +136,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + ((void)(l_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ - ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + ((void)(l_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) @@ -159,6 +163,22 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_pushfail(L) lua_pushnil(L) +/* +** Internal assertions for in-house debugging +*/ +#if !defined(lua_assert) + +#if defined LUAI_ASSERT + #include + #define lua_assert(c) assert(c) +#else + #define lua_assert(c) ((void)0) +#endif + +#endif + + + /* ** {====================================================== ** Generic Buffer manipulation diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 747fd45a2..83ad306d9 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -138,7 +138,7 @@ static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); - if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) + if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); @@ -182,7 +182,8 @@ static int luaB_rawset (lua_State *L) { static int pushmode (lua_State *L, int oldmode) { - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" + : "generational"); return 1; } @@ -299,7 +300,7 @@ static int luaB_ipairs (lua_State *L) { static int load_aux (lua_State *L, int status, int envidx) { - if (status == LUA_OK) { + if (l_likely(status == LUA_OK)) { if (envidx != 0) { /* 'env' parameter? */ lua_pushvalue(L, envidx); /* environment for loaded function */ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ @@ -355,7 +356,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { *size = 0; return NULL; } - else if (!lua_isstring(L, -1)) + else if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "reader function must return a string"); lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ return lua_tolstring(L, RESERVEDSLOT, size); @@ -393,7 +394,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) { static int luaB_dofile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) + if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); return dofilecont(L, 0, 0); @@ -401,7 +402,7 @@ static int luaB_dofile (lua_State *L) { static int luaB_assert (lua_State *L) { - if (lua_toboolean(L, 1)) /* condition is true? */ + if (l_likely(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 */ @@ -437,7 +438,7 @@ static int luaB_select (lua_State *L) { ** ignored). */ static int finishpcall (lua_State *L, int status, lua_KContext extra) { - if (status != LUA_OK && status != LUA_YIELD) { /* error? */ + if (l_unlikely(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 */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 14d41f1a7..80d975cb8 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -314,15 +314,6 @@ void luaK_patchtohere (FuncState *fs, int list) { } -/* -** MAXimum number of successive Instructions WiTHout ABSolute line -** information. -*/ -#if !defined(MAXIWTHABS) -#define MAXIWTHABS 120 -#endif - - /* limit for difference between lines in relative line info. */ #define LIMLINEDIFF 0x80 @@ -337,13 +328,13 @@ void luaK_patchtohere (FuncState *fs, int list) { static void savelineinfo (FuncState *fs, Proto *f, int line) { int linedif = line - fs->previousline; int pc = fs->pc - 1; /* last instruction coded */ - if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) { + if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; linedif = ABSLINEINFO; /* signal that there is absolute information */ - fs->iwthabs = 0; /* restart counter */ + fs->iwthabs = 1; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, MAX_INT, "opcodes"); @@ -545,11 +536,14 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { ** 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. +** Note that all functions share the same table, so entering or exiting +** a function can make some indices wrong. */ static int addk (FuncState *fs, TValue *key, TValue *v) { + TValue val; lua_State *L = fs->ls->L; Proto *f = fs->f; - TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ + const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ int k, oldsize; if (ttisinteger(idx)) { /* is there an index there? */ k = cast_int(ivalue(idx)); @@ -563,7 +557,8 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { 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); + setivalue(&val, k); + luaH_finishset(L, fs->ls->h, key, idx, &val); 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); @@ -763,7 +758,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { break; } case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.sidx; + e->u.info = e->u.var.ridx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -1036,7 +1031,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -1276,7 +1271,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { } else { /* register index of the table */ - t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info; + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; if (isKstr(fs, k)) { t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXSTR; @@ -1303,7 +1298,8 @@ static int validop (int op, TValue *v1, TValue *v2) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ lua_Integer i; - return (tointegerns(v1, &i) && tointegerns(v2, &i)); + return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && + luaV_tointegerns(v2, &i, LUA_FLOORN2I)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index c165031d2..fedbebec3 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -31,14 +31,14 @@ static lua_State *getco (lua_State *L) { */ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; - if (!lua_checkstack(co, narg)) { + if (l_unlikely(!lua_checkstack(co, narg))) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } lua_xmove(L, co, narg); status = lua_resume(co, L, narg, &nres); - if (status == LUA_OK || status == LUA_YIELD) { - if (!lua_checkstack(L, nres + 1)) { + if (l_likely(status == LUA_OK || status == LUA_YIELD)) { + if (l_unlikely(!lua_checkstack(L, nres + 1))) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ @@ -57,7 +57,7 @@ static int luaB_coresume (lua_State *L) { lua_State *co = getco(L); int r; r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { + if (l_unlikely(r < 0)) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ @@ -73,10 +73,13 @@ static int luaB_coresume (lua_State *L) { 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) { /* error? */ + if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); - if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */ - lua_resetthread(co); /* close its tbc variables */ + if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ + stat = lua_resetthread(co); /* close its tbc variables */ + lua_assert(stat != LUA_OK); + lua_xmove(co, L, 1); /* copy error message */ + } if (stat != LUA_ERRMEM && /* not a memory error and ... */ lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 5a326aded..6dcbaa982 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -33,7 +33,7 @@ static const char *const HOOKKEY = "_HOOKKEY"; ** checked. */ static void checkstack (lua_State *L, lua_State *L1, int n) { - if (L != L1 && !lua_checkstack(L1, n)) + if (l_unlikely(L != L1 && !lua_checkstack(L1, n))) luaL_error(L, "stack overflow"); } @@ -152,6 +152,7 @@ static int db_getinfo (lua_State *L) { lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); + luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'"); 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 */ @@ -212,7 +213,7 @@ static int db_getlocal (lua_State *L) { lua_Debug ar; const char *name; int level = (int)luaL_checkinteger(L, arg + 1); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ + if (l_unlikely(!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); @@ -237,7 +238,7 @@ static int db_setlocal (lua_State *L) { lua_Debug ar; 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? */ + if (l_unlikely(!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); @@ -377,7 +378,7 @@ static int db_sethook (lua_State *L) { } if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { /* table just created; initialize it */ - lua_pushstring(L, "k"); + lua_pushliteral(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ @@ -420,7 +421,7 @@ static int db_debug (lua_State *L) { for (;;) { char buffer[250]; lua_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || + if (fgets(buffer, sizeof(buffer), stdin) == NULL || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 8cb00e51a..8e3657a92 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -33,8 +33,6 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) -/* inverse of 'pcRel' */ -#define invpcRel(pc, p) ((p)->code + (pc) + 1) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); @@ -48,10 +46,14 @@ static int currentpc (CallInfo *ci) { /* ** Get a "base line" to find the line corresponding to an instruction. -** For that, search the array of absolute line info for the largest saved -** instruction smaller or equal to the wanted instruction. A special -** case is when there is no absolute info or the instruction is before -** the first absolute one. +** Base lines are regularly placed at MAXIWTHABS intervals, so usually +** an integer division gets the right place. When the source file has +** large sequences of empty/comment lines, it may need extra entries, +** so the original estimate needs a correction. +** The assertion that the estimate is a lower bound for the correct base +** is valid as long as the debug info has been generated with the same +** value for MAXIWTHABS or smaller. (Previous releases use a little +** smaller value.) */ static int getbaseline (const Proto *f, int pc, int *basepc) { if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { @@ -59,20 +61,11 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { return f->linedefined; } else { - unsigned int i; - if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) - i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ - else { /* binary search */ - unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ - i = 0; /* abslineinfo[i] <= pc */ - while (i < j - 1) { - unsigned int m = (j + i) / 2; - if (pc >= f->abslineinfo[m].pc) - i = m; - else - j = m; - } - } + int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ + /* estimate must be a lower bond of the correct base */ + lua_assert(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc); + while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) + i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; return f->abslineinfo[i].line; } @@ -305,8 +298,8 @@ static void collectvalidlines (lua_State *L, Closure *f) { sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ - currentline = nextline(p, currentline, i); + for (i = 0; i < p->sizelineinfo; i++) { /* for all instructions */ + currentline = nextline(p, currentline, i); /* get its line */ luaH_setint(L, t, currentline, &v); /* table[line] = true */ } } @@ -629,12 +622,10 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_LEN: tm = TM_LEN; break; case OP_CONCAT: tm = TM_CONCAT; break; case OP_EQ: tm = TM_EQ; break; - case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: - *name = "order"; /* '<=' can call '__lt', etc. */ - return "metamethod"; - case OP_CLOSE: case OP_RETURN: - *name = "close"; - return "metamethod"; + /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */ + case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break; + case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break; + case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break; default: return NULL; /* cannot find a reasonable name */ } @@ -647,14 +638,18 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, /* -** 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. +** Check whether pointer 'o' points to some value in the stack +** frame of the current function. Because 'o' may not point to a +** value in this stack, we cannot compare it with the region +** boundaries (undefined behaviour in ISO C). */ static int isinstack (CallInfo *ci, const TValue *o) { - StkId base = ci->func + 1; - ptrdiff_t i = cast(StkId, o) - base; - return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); + StkId pos; + for (pos = ci->func + 1; pos < ci->top; pos++) { + if (o == s2v(pos)) + return 1; + } + return 0; /* not found */ } @@ -697,6 +692,19 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { } +l_noret luaG_callerror (lua_State *L, const TValue *o) { + CallInfo *ci = L->ci; + const char *name = NULL; /* to avoid warnings */ + const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + if (what != NULL) { + const char *t = luaT_objtypename(L, o); + luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t); + } + else + luaG_typeerror(L, o, "call"); +} + + l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { luaG_runerror(L, "bad 'for' %s (number expected, got %s)", what, luaT_objtypename(L, o)); @@ -722,7 +730,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1, */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; - if (!tointegerns(p1, &temp)) + if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } @@ -780,16 +788,30 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { /* ** Check whether new instruction 'newpc' is in a different line from -** previous instruction 'oldpc'. +** previous instruction 'oldpc'. More often than not, 'newpc' is only +** one or a few instructions after 'oldpc' (it must be after, see +** caller), so try to avoid calling 'luaG_getfuncline'. If they are +** too far apart, there is a good chance of a ABSLINEINFO in the way, +** so it goes directly to 'luaG_getfuncline'. */ static int changedline (const Proto *p, int oldpc, int newpc) { if (p->lineinfo == NULL) /* no debug information? */ return 0; - while (oldpc++ < newpc) { - if (p->lineinfo[oldpc] != 0) - return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); + if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ + int delta = 0; /* line diference */ + int pc = oldpc; + for (;;) { + int lineinfo = p->lineinfo[++pc]; + if (lineinfo == ABSLINEINFO) + break; /* cannot compute delta; fall through */ + delta += lineinfo; + if (pc == newpc) + return (delta != 0); /* delta computed successfully */ + } } - return 0; /* no line changes between positions */ + /* either instructions are too far apart or there is an absolute line + info in the way; compute line difference explicitly */ + return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc)); } @@ -797,20 +819,19 @@ static int changedline (const Proto *p, int oldpc, int newpc) { ** Traces the execution of a Lua function. Called before the execution ** of each opcode, when debug is on. 'L->oldpc' stores the last ** instruction traced, to detect line changes. When entering a new -** function, 'npci' will be zero and will test as a new line without -** the need for 'oldpc'; so, 'oldpc' does not need to be initialized -** before. Some exceptional conditions may return to a function without -** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is -** reset to zero. (A wrong but valid 'oldpc' at most causes an extra -** call to a line hook.) +** function, 'npci' will be zero and will test as a new line whatever +** the value of 'oldpc'. Some exceptional conditions may return to +** a function without setting 'oldpc'. In that case, 'oldpc' may be +** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' +** at most causes an extra call to a line hook.) +** This function is not "Protected" when called, so it should correct +** 'L->top' before calling anything that can run the GC. */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; const Proto *p = ci_func(ci)->p; int counthook; - /* 'L->oldpc' may be invalid; reset it in this case */ - int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ return 0; /* turn off 'trap' */ @@ -826,15 +847,16 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } - if (!isIT(*(ci->u.l.savedpc - 1))) - L->top = ci->top; /* prepare top */ + if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ + L->top = ci->top; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { + /* 'L->oldpc' may be invalid; use zero in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; int npci = pcRel(pc, p); - if (npci == 0 || /* call linehook when enter a new function, */ - pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ - changedline(p, oldpc, npci)) { /* enter new line */ + if (npci <= oldpc || /* call hook when jump back (loop), */ + changedline(p, oldpc, npci)) { /* or when enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index a0a584862..974960e99 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -26,11 +26,22 @@ */ #define ABSLINEINFO (-0x80) + +/* +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. (A power of two allows fast divisions.) +*/ +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 128 +#endif + + LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); +LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 4b55c31c2..7135079b1 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -98,11 +98,12 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } - case CLOSEPROTECT: { + case LUA_OK: { /* special case only for closing upvalues */ setnilvalue(s2v(oldtop)); /* no error message */ break; } default: { + lua_assert(errorstatus(errcode)); /* real error */ setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; } @@ -118,17 +119,13 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); - errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ - L->status = cast_byte(errcode); /* mark it as dead */ + errcode = luaE_resetthread(L, errcode); /* close all upvalues */ 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->panic) { /* panic function? */ - luaD_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->panic(L); /* call panic function (last chance to jump out) */ } @@ -163,9 +160,8 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { CallInfo *ci; UpVal *up; - if (oldstack == newstack) - return; /* stack address did not change */ L->top = (L->top - oldstack) + newstack; + L->tbclist = (L->tbclist - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { @@ -181,19 +177,35 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) +/* +** Reallocate the stack to a new size, correcting all pointers into +** it. (There are pointers to a stack from its upvalues, from its list +** of call infos, plus a few individual pointers.) The reallocation is +** done in two steps (allocation + free) because the correction must be +** done while both addresses (the old stack and the new one) are valid. +** (In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior.) +** In case of allocation error, raise an error or return false according +** to 'raiseerror'. +*/ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int lim = stacksize(L); - StkId newstack = luaM_reallocvector(L, L->stack, - lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); + int oldsize = stacksize(L); + int i; + StkId newstack = luaM_reallocvector(L, NULL, 0, + newsize + EXTRA_STACK, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - if (unlikely(newstack == NULL)) { /* reallocation failed? */ + if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize; lim++) - setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */ + /* number of elements to be copied to the new stack */ + i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; + memcpy(newstack, L->stack, i * sizeof(StackValue)); + for (; i < newsize + EXTRA_STACK; i++) + setnilvalue(s2v(newstack + i)); /* erase new segment */ correctstack(L, L->stack, newstack); + luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); L->stack = newstack; L->stack_last = L->stack + newsize; return 1; @@ -206,7 +218,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { */ int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = stacksize(L); - if (unlikely(size > LUAI_MAXSTACK)) { + if (l_unlikely(size > LUAI_MAXSTACK)) { /* if stack is larger than maximum, thread is already using the extra space reserved for errors, that is, thread is handling a stack error; cannot grow further than that. */ @@ -222,7 +234,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (likely(newsize <= LUAI_MAXSTACK)) + if (l_likely(newsize <= LUAI_MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); else { /* stack overflow */ /* add extra size to be able to handle the error message */ @@ -297,8 +309,8 @@ void luaD_hook (lua_State *L, int event, int line, if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); + ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; @@ -308,8 +320,10 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ftransfer = ftransfer; ci->u2.transferinfo.ntransfer = ntransfer; } + if (isLua(ci) && L->top < ci->top) + L->top = ci->top; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (L->top + LUA_MINSTACK > ci->top) + if (ci->top < L->top + LUA_MINSTACK) ci->top = L->top + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= mask; @@ -331,38 +345,40 @@ void luaD_hook (lua_State *L, int event, int line, ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { - int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; - Proto *p; - if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ - return; /* don't call hook */ - p = clLvalue(s2v(ci->func))->p; - L->top = ci->top; /* prepare top */ - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, hook, -1, 1, p->numparams); - ci->u.l.savedpc--; /* correct 'pc' */ -} - - -static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { - ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ - int delta = 0; - if (isLuacode(ci)) { + L->oldpc = 0; /* set 'oldpc' for new function */ + if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ + int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL + : LUA_HOOKCALL; Proto *p = ci_func(ci)->p; - if (p->is_vararg) - delta = ci->u.l.nextraargs + p->numparams + 1; - if (L->top < ci->top) - L->top = ci->top; /* correct top to run hook */ + ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_hook(L, event, -1, 1, p->numparams); + ci->u.l.savedpc--; /* correct 'pc' */ } +} + + +/* +** Executes a return hook for Lua and C functions and sets/corrects +** 'oldpc'. (Note that this correction is needed by the line hook, so it +** is done even when return hooks are off.) +*/ +static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + StkId firstres = L->top - nres; /* index of first result */ + int delta = 0; /* correction for vararg functions */ int ftransfer; + if (isLua(ci)) { + Proto *p = ci_func(ci)->p; + if (p->is_vararg) + delta = ci->u.l.nextraargs + p->numparams + 1; + } ci->func += delta; /* if vararg, back to virtual 'func' */ ftransfer = cast(unsigned short, firstres - ci->func); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; } if (isLua(ci = ci->previous)) - L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ - return restorestack(L, oldtop); + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ } @@ -374,8 +390,8 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; - if (unlikely(ttisnil(tm))) - luaG_typeerror(L, s2v(func), "call"); /* nothing to call */ + if (l_unlikely(ttisnil(tm))) + luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top++; /* stack space pre-allocated by the caller */ @@ -399,27 +415,34 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ - else + else /* at least one result */ setobjs2s(L, res, L->top - nres); /* move it to proper place */ L->top = res + 1; return; case LUA_MULTRET: wanted = nres; /* we want all results */ break; - default: /* multiple results (or to-be-closed variables) */ + default: /* two/more results and/or to-be-closed variables */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ ptrdiff_t savedres = savestack(L, res); - luaF_close(L, res, LUA_OK); /* may change the stack */ - res = restorestack(L, savedres); - wanted = codeNresults(wanted); /* correct value */ + L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ + L->ci->u2.nres = nres; + luaF_close(L, res, CLOSEKTOP, 1); + L->ci->callstatus &= ~CIST_CLSRET; + if (L->hookmask) /* if needed, call hook after '__close's */ + rethook(L, L->ci, nres); + res = restorestack(L, savedres); /* close and hook can move stack */ + wanted = decodeNresults(wanted); if (wanted == LUA_MULTRET) - wanted = nres; + wanted = nres; /* we want all results */ } break; } + /* generic case */ firstresult = L->top - nres; /* index of first result */ - /* move all results to correct place */ - for (i = 0; i < nres && i < wanted; i++) + if (nres > wanted) /* extra results? */ + nres = wanted; /* don't need them */ + 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(s2v(res + i)); @@ -428,15 +451,21 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { /* -** Finishes a function call: calls hook if necessary, removes CallInfo, -** moves current number of results to proper place. +** Finishes a function call: calls hook if necessary, moves current +** number of results to proper place, and returns to previous call +** info. If function has to close variables, hook must be called after +** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - if (L->hookmask) - L->top = rethook(L, ci, L->top - nres, nres); - L->ci = ci->previous; /* back to caller */ + int wanted = ci->nresults; + if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) + rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func, nres, ci->nresults); + moveresults(L, ci->func, nres, wanted); + /* function cannot be in any of these cases when returning */ + lua_assert(!(ci->callstatus & + (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); + L->ci = ci->previous; /* back to caller (after closing variables) */ } @@ -495,7 +524,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ci->top = L->top + LUA_MINSTACK; ci->func = func; lua_assert(ci->top <= L->stack_last); - if (L->hookmask & LUA_MASKCALL) { + if (l_unlikely(L->hookmask & LUA_MASKCALL)) { int narg = cast_int(L->top - func) - 1; luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); } @@ -541,7 +570,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { static void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; - if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ @@ -568,27 +597,74 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { /* -** Completes the execution of an interrupted C function, calling its -** continuation function. +** Finish the job of 'lua_pcallk' after it was interrupted by an yield. +** (The caller, 'finishCcall', does the final call to 'adjustresults'.) +** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. +** If a '__close' method yields here, eventually control will be back +** to 'finishCcall' (when that '__close' method finally returns) and +** 'finishpcallk' will run again and close any still pending '__close' +** methods. Similarly, if a '__close' method errs, 'precover' calls +** 'unroll' which calls ''finishCcall' and we are back here again, to +** close any pending '__close' methods. +** Note that, up to the call to 'luaF_close', the corresponding +** 'CallInfo' is not modified, so that this repeated run works like the +** first one (except that it has at least one less '__close' to do). In +** particular, field CIST_RECST preserves the error status across these +** multiple runs, changing only if there is a new error. */ -static void finishCcall (lua_State *L, int status) { - CallInfo *ci = L->ci; - int n; - /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && yieldable(L)); - /* 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; /* 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); - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ - lua_lock(L); - api_checknelems(L, n); +static int finishpcallk (lua_State *L, CallInfo *ci) { + int status = getcistrecst(ci); /* get original status */ + if (l_likely(status == LUA_OK)) /* no error? */ + status = LUA_YIELD; /* was interrupted by an yield */ + else { /* error */ + StkId func = restorestack(L, ci->u2.funcidx); + L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ + luaF_close(L, func, status, 1); /* can yield or raise an error */ + func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ + luaD_seterrorobj(L, status, func); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ + setcistrecst(ci, LUA_OK); /* clear original status */ + } + ci->callstatus &= ~CIST_YPCALL; + L->errfunc = ci->u.c.old_errfunc; + /* if it is here, there were errors or yields; unlike 'lua_pcallk', + do not change status */ + return status; +} + + +/* +** Completes the execution of a C function interrupted by an yield. +** The interruption must have happened while the function was either +** closing its tbc variables in 'moveresults' or executing +** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes +** 'luaD_poscall'. In the second case, the call to 'finishpcallk' +** finishes the interrupted execution of 'lua_pcallk'. After that, it +** calls the continuation of the interrupted function and finally it +** completes the job of the 'luaD_call' that called the function. In +** the call to 'adjustresults', we do not know the number of results +** of the function called by 'lua_callk'/'lua_pcallk', so we are +** conservative and use LUA_MULTRET (always adjust). +*/ +static void finishCcall (lua_State *L, CallInfo *ci) { + int n; /* actual number of results from C function */ + if (ci->callstatus & CIST_CLSRET) { /* was returning? */ + lua_assert(hastocloseCfunc(ci->nresults)); + n = ci->u2.nres; /* just redo 'luaD_poscall' */ + /* don't need to reset CIST_CLSRET, as it will be set again anyway */ + } + else { + int status = LUA_YIELD; /* default if there were no errors */ + /* must have a continuation and must be able to call it */ + lua_assert(ci->u.c.k != NULL && yieldable(L)); + if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ + status = finishpcallk(L, ci); /* finish it */ + adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ + lua_unlock(L); + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ + lua_lock(L); + api_checknelems(L, n); + } luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } @@ -596,18 +672,14 @@ static void finishCcall (lua_State *L, int status) { /* ** 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). +** interruption long-jumps out of the loop). */ static void unroll (lua_State *L, void *ud) { CallInfo *ci; - if (ud != NULL) /* error status? */ - finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ + UNUSED(ud); while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ if (!isLua(ci)) /* C function? */ - finishCcall(L, LUA_YIELD); /* complete its execution */ + finishCcall(L, ci); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L, ci); /* execute down to higher C 'boundary' */ @@ -630,28 +702,6 @@ 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); - if (ci == NULL) return 0; /* no recovery point */ - /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->u2.funcidx); - L->ci = ci; - L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - status = luaF_close(L, oldtop, status); /* may change the stack */ - oldtop = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, oldtop); - luaD_shrinkstack(L); /* restore stack size in case of overflow */ - L->errfunc = ci->u.c.old_errfunc; - return 1; /* continue running 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 @@ -683,8 +733,10 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ luaE_incCstack(L); /* control the C stack */ - if (isLua(ci)) /* yielded inside a hook? */ + if (isLua(ci)) { /* yielded inside a hook? */ + L->top = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ + } else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); @@ -698,6 +750,26 @@ static void resume (lua_State *L, void *ud) { } } + +/* +** Unrolls a coroutine in protected mode while there are recoverable +** errors, that is, errors inside a protected call. (Any error +** interrupts 'unroll', and this loop protects it again so it can +** continue.) Stops with a normal end (status == LUA_OK), an yield +** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't +** find a recover point). +*/ +static int precover (lua_State *L, int status) { + CallInfo *ci; + while (errorstatus(status) && (ci = findpcall(L)) != NULL) { + L->ci = ci; /* go down to recovery functions */ + setcistrecst(ci, status); /* status to finish 'pcall' */ + status = luaD_rawrunprotected(L, unroll, NULL); + } + return status; +} + + LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { int status; @@ -715,11 +787,8 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &status); - } - if (likely(!errorstatus(status))) + status = precover(L, status); + if (l_likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ L->status = cast_byte(status); /* mark thread as 'dead' */ @@ -745,22 +814,22 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_lock(L); ci = L->ci; api_checknelems(L, nresults); - if (unlikely(!yieldable(L))) { + if (l_unlikely(!yieldable(L))) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); else luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; + ci->u2.nyield = nresults; /* save number of results */ if (isLua(ci)) { /* inside a hook? */ lua_assert(!isLuacode(ci)); + api_check(L, nresults == 0, "hooks cannot yield values"); api_check(L, k == NULL, "hooks cannot continue after yielding"); - ci->u2.nyield = 0; /* no results */ } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ @@ -769,6 +838,45 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } +/* +** Auxiliary structure to call 'luaF_close' in protected mode. +*/ +struct CloseP { + StkId level; + int status; +}; + + +/* +** Auxiliary function to call 'luaF_close' in protected mode. +*/ +static void closepaux (lua_State *L, void *ud) { + struct CloseP *pcl = cast(struct CloseP *, ud); + luaF_close(L, pcl->level, pcl->status, 0); +} + + +/* +** Calls 'luaF_close' in protected mode. Return the original status +** or, in case of errors, the new status. +*/ +int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { + CallInfo *old_ci = L->ci; + lu_byte old_allowhooks = L->allowhook; + for (;;) { /* keep closing upvalues until no more errors */ + struct CloseP pcl; + pcl.level = restorestack(L, level); pcl.status = status; + status = luaD_rawrunprotected(L, &closepaux, &pcl); + if (l_likely(status == LUA_OK)) /* no more errors? */ + return pcl.status; + else { /* an error occurred; restore saved state and repeat */ + L->ci = old_ci; + L->allowhook = old_allowhooks; + } + } +} + + /* ** Call the C function 'func' in protected mode, restoring basic ** thread information ('allowhook', etc.) and in particular @@ -782,13 +890,11 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); - if (unlikely(status != LUA_OK)) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); + if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ L->ci = old_ci; L->allowhook = old_allowhooks; - status = luaF_close(L, oldtop, status); - oldtop = restorestack(L, old_top); /* previous call may change stack */ - luaD_seterrorobj(L, status, oldtop); + status = luaD_closeprotected(L, old_top, status); + luaD_seterrorobj(L, status, restorestack(L, old_top)); luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 4d30d072e..6bf0ed86f 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -23,7 +23,7 @@ ** at every check. */ #define luaD_checkstackaux(L,n,pre,pos) \ - if (L->stack_last - L->top <= (n)) \ + if (l_unlikely(L->stack_last - L->top <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ else { condmovestack(L,pre,pos); } @@ -63,6 +63,7 @@ LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 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 void luaD_tryfuncTM (lua_State *L, StkId func); +LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 250c85f7b..9a4413da8 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -101,115 +101,76 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { } -static void callclose (lua_State *L, void *ud) { - UNUSED(ud); - luaD_callnoyield(L, L->top - 3, 0); -} - - /* -** Prepare closing method plus its arguments for object 'obj' with -** error message 'err'. (This function assumes EXTRA_STACK.) +** Call closing method for object 'obj' with error message 'err'. The +** boolean 'yy' controls whether the call is yieldable. +** (This function assumes EXTRA_STACK.) */ -static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { +static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { StkId top = L->top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - return 0; /* nothing to call */ setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ L->top = top + 3; /* add function and arguments */ - return 1; + if (yy) + luaD_call(L, top, 0); + else + luaD_callnoyield(L, top, 0); } /* -** Raise an error with message 'msg', inserting the name of the -** local variable at position 'level' in the stack. +** Check whether object at given level has a close metamethod and raise +** an error if not. */ -static void varerror (lua_State *L, StkId level, const char *msg) { - int idx = cast_int(level - L->ci->func); - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, msg, vname); +static void checkclosemth (lua_State *L, StkId level) { + const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); + if (ttisnil(tm)) { /* no metamethod? */ + int idx = cast_int(level - L->ci->func); /* variable index */ + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, "variable '%s' got a non-closable value", vname); + } } /* -** Prepare and call a closing method. If status is OK, code is still -** inside the original protected call, and so any error will be handled -** there. Otherwise, a previous error already activated the original -** protected call, and so the call to the closing method must be -** protected here. (A status == CLOSEPROTECT behaves like a previous -** error, to also run the closing method in protected mode). -** If status is OK, the call to the closing method will be pushed -** at the top of the stack. Otherwise, values are pushed after -** the 'level' of the upvalue being closed, as everything after -** that won't be used again. +** Prepare and call a closing method. +** If status is CLOSEKTOP, the call to the closing method will be pushed +** at the top of the stack. Otherwise, values can be pushed right after +** the 'level' of the upvalue being closed, as everything after that +** won't be used again. */ -static int callclosemth (lua_State *L, StkId level, int status) { +static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { TValue *uv = s2v(level); /* value being closed */ - if (likely(status == LUA_OK)) { - if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ - callclose(L, NULL); /* call closing method */ - else if (!l_isfalse(uv)) /* non-closable non-false value? */ - varerror(L, level, "attempt to close non-closable variable '%s'"); - } - else { /* must close the object in protected mode */ - ptrdiff_t oldtop; - level++; /* space for error message */ - oldtop = savestack(L, level + 1); /* top will be after that */ - luaD_seterrorobj(L, status, level); /* set error message */ - if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ - int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); - if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ - status = newstatus; /* this will be the new error */ - else { - if (newstatus != LUA_OK) /* suppressed error? */ - luaE_warnerror(L, "__close metamethod"); - /* leave original error (or nil) on top */ - L->top = restorestack(L, oldtop); - } - } - /* else no metamethod; ignore this case and keep original error */ + TValue *errobj; + if (status == CLOSEKTOP) + errobj = &G(L)->nilvalue; /* error object is nil */ + else { /* 'luaD_seterrorobj' will set top to level + 2 */ + errobj = s2v(level + 1); /* error object goes after 'uv' */ + luaD_seterrorobj(L, status, level + 1); /* set error object */ } - return status; + callclosemethod(L, uv, errobj, yy); } /* -** Try to create a to-be-closed upvalue -** (can raise a memory-allocation error) -*/ -static void trynewtbcupval (lua_State *L, void *ud) { - newupval(L, 1, cast(StkId, ud), &L->openupval); -} - - -/* -** Create a to-be-closed upvalue. If there is a memory error -** when creating the upvalue, the closing method must be called here, -** as there is no upvalue to call it later. +** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { - TValue *obj = s2v(level); - lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); - if (!l_isfalse(obj)) { /* false doesn't need to be closed */ - int status; - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - varerror(L, level, "variable '%s' got a non-closable value"); - status = luaD_rawrunprotected(L, trynewtbcupval, level); - if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ - lua_assert(status == LUA_ERRMEM); - luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - /* next call must succeed, as object is closable */ - prepclosingmethod(L, s2v(level), s2v(level + 1)); - callclose(L, NULL); /* call closing method */ - luaD_throw(L, LUA_ERRMEM); /* throw memory error */ - } + lua_assert(level > L->tbclist); + if (l_isfalse(s2v(level))) + return; /* false doesn't need to be closed */ + checkclosemth(L, level); /* value must have a close method */ + while (level - L->tbclist > USHRT_MAX) { /* is delta too large? */ + L->tbclist += USHRT_MAX; /* create a dummy node at maximum delta */ + L->tbclist->tbclist.delta = USHRT_MAX; + L->tbclist->tbclist.isdummy = 1; } + level->tbclist.delta = level - L->tbclist; + level->tbclist.isdummy = 0; + L->tbclist = level; } @@ -221,18 +182,16 @@ void luaF_unlinkupval (UpVal *uv) { } -int luaF_close (lua_State *L, StkId level, int status) { +/* +** Close all upvalues up to the given stack level. +*/ +void luaF_closeupval (lua_State *L, StkId level) { UpVal *uv; - while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { + StkId upl; /* stack index pointed by 'uv' */ + while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top); - if (uv->tbc && status != NOCLOSINGMETH) { - /* must run closing method, which may change the stack */ - ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, uplevel(uv), status); - level = restorestack(L, levelrel); - } - luaF_unlinkupval(uv); + luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ @@ -240,7 +199,24 @@ int luaF_close (lua_State *L, StkId level, int status) { luaC_barrier(L, uv, slot); } } - return status; +} + + +/* +** Close all upvalues and to-be-closed variables up to the given stack +** level. +*/ +void luaF_close (lua_State *L, StkId level, int status, int yy) { + ptrdiff_t levelrel = savestack(L, level); + luaF_closeupval(L, level); /* first, close the upvalues */ + while (L->tbclist >= level) { /* traverse tbc's down to that level */ + StkId tbc = L->tbclist; /* get variable index */ + L->tbclist -= tbc->tbclist.delta; /* remove it from list */ + if (!tbc->tbclist.isdummy) { /* not a dummy entry? */ + prepcallclosemth(L, tbc, status, yy); /* close variable */ + level = restorestack(L, levelrel); + } + } } diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 4025ea802..6126123c9 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -42,15 +42,9 @@ #define MAXMISS 10 -/* -** Special "status" for 'luaF_close' -*/ - -/* close upvalues without running their closing methods */ -#define NOCLOSINGMETH (-1) -/* close upvalues running all closing methods in protected mode */ -#define CLOSEPROTECT (-2) +/* special status to close upvalues preserving the top of the stack */ +#define CLOSEKTOP (-1) LUAI_FUNC Proto *luaF_newproto (lua_State *L); @@ -59,7 +53,8 @@ LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); -LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); +LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 595b2772a..e7e1bbe62 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -920,7 +920,7 @@ static void GCTM (lua_State *L) { L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ - if (unlikely(status != LUA_OK)) { /* error while running __gc? */ + if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc metamethod"); L->top--; /* pops error object */ } @@ -1583,52 +1583,64 @@ static int sweepstep (lua_State *L, global_State *g, static lu_mem singlestep (lua_State *L) { global_State *g = G(L); + lu_mem work; + lua_assert(!g->gcstopem); /* collector is not reentrant */ + g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { case GCSpause: { restartcollection(g); g->gcstate = GCSpropagate; - return 1; + work = 1; + break; } case GCSpropagate: { if (g->gray == NULL) { /* no more gray objects? */ g->gcstate = GCSenteratomic; /* finish propagate phase */ - return 0; + work = 0; } else - return propagatemark(g); /* traverse one gray object */ + work = propagatemark(g); /* traverse one gray object */ + break; } case GCSenteratomic: { - lu_mem work = atomic(L); /* work is what was traversed by 'atomic' */ + work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; - return work; + break; } case GCSswpallgc: { /* sweep "regular" objects */ - return sweepstep(L, g, GCSswpfinobj, &g->finobj); + work = sweepstep(L, g, GCSswpfinobj, &g->finobj); + break; } case GCSswpfinobj: { /* sweep objects with finalizers */ - return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + break; } case GCSswptobefnz: { /* sweep objects to be finalized */ - return sweepstep(L, g, GCSswpend, NULL); + work = sweepstep(L, g, GCSswpend, NULL); + break; } case GCSswpend: { /* finish sweeps */ checkSizes(L, g); g->gcstate = GCScallfin; - return 0; + work = 0; + break; } case GCScallfin: { /* call remaining finalizers */ if (g->tobefnz && !g->gcemergency) { - int n = runafewfinalizers(L, GCFINMAX); - return n * GCFINALIZECOST; + g->gcstopem = 0; /* ok collections during finalizers */ + work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ - return 0; + work = 0; } + break; } default: lua_assert(0); return 0; } + g->gcstopem = 0; + return work; } diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 60ab1bfab..b08397da4 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -52,12 +52,6 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ -#if !defined(l_checkmodep) -/* By default, Lua accepts only "r" or "w" as mode */ -#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') -#endif - - #if !defined(l_popen) /* { */ #if defined(LUA_USE_POSIX) /* { */ @@ -70,6 +64,12 @@ static int l_checkmode (const char *mode) { #define l_popen(L,c,m) (_popen(c,m)) #define l_pclose(L,file) (_pclose(file)) +#if !defined(l_checkmodep) +/* Windows accepts "[rw][bt]?" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ + (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) +#endif + #else /* }{ */ /* ISO C definitions */ @@ -83,6 +83,12 @@ static int l_checkmode (const char *mode) { #endif /* } */ + +#if !defined(l_checkmodep) +/* By default, Lua accepts only "r" or "w" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') +#endif + /* }====================================================== */ @@ -180,7 +186,7 @@ static int f_tostring (lua_State *L) { static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); - if (isclosed(p)) + if (l_unlikely(isclosed(p))) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; @@ -255,7 +261,7 @@ static LStream *newfile (lua_State *L) { 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) + if (l_unlikely(p->f == NULL)) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -303,7 +309,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { LStream *p; lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) + if (l_unlikely(isclosed(p))) luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -430,7 +436,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 >= L_MAXLENNUM) { /* buffer overflow? */ + if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ rn->buff[0] = '\0'; /* invalidate result */ return 0; /* fail */ } @@ -493,8 +499,8 @@ static int read_number (lua_State *L, FILE *f) { 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 */ + if (l_likely(lua_stringtonumber(L, rn.buff))) + return 1; /* ok, it is a valid number */ else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ @@ -670,7 +676,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { status = status && (fwrite(s, sizeof(char), l, f) == l); } } - if (status) return 1; /* file handle already on stack top */ + if (l_likely(status)) + return 1; /* file handle already on stack top */ else return luaL_fileresult(L, status, NULL); } @@ -697,7 +704,7 @@ static int f_seek (lua_State *L) { luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); op = l_fseek(f, offset, mode[op]); - if (op) + if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ else { lua_pushinteger(L, (lua_Integer)l_ftell(f)); diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 4b8dec998..e99151787 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -122,26 +122,29 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** 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) +** 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. It also internalizes long strings, +** ensuring there is only one copy of each unique string. The table +** here is used as a set: the string enters as the key, while its value +** is irrelevant. We use the string itself as the value only because it +** is a TValue readly available. Later, the code generation can change +** this value. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; - 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->h, s2v(L->top - 1)); - if (isempty(o)) { /* not in use yet? */ - /* boolean value does not need GC barrier; - table is not a metatable, so it does not need to invalidate cache */ - setbtvalue(o); /* t[string] = true */ + const TValue *o = luaH_getstr(ls->h, ts); + if (!ttisnil(o)) /* string already present? */ + ts = keystrval(nodefromval(o)); /* get saved copy */ + else { /* not in use yet */ + TValue *stv = s2v(L->top++); /* reserve stack space for string */ + setsvalue(L, stv, ts); /* temporarily anchor the string */ + luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ + /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); + L->top--; /* remove string from stack */ } - else { /* string already present */ - ts = keystrval(nodefromval(o)); /* re-use value previously stored */ - } - L->top--; /* remove string from stack */ return ts; } diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index d03948314..025f1c82c 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -149,22 +149,6 @@ typedef LUAI_UACINT l_uacInt; #endif -/* -** macros to improve jump prediction (used mainly for error handling) -*/ -#if !defined(likely) - -#if defined(__GNUC__) -#define likely(x) (__builtin_expect(((x) != 0), 1)) -#define unlikely(x) (__builtin_expect(((x) != 0), 0)) -#else -#define likely(x) (x) -#define unlikely(x) (x) -#endif - -#endif - - /* ** non-return type */ diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 86def470c..5f5983a43 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -73,7 +73,7 @@ static int math_atan (lua_State *L) { static int math_toint (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); - if (valid) + if (l_likely(valid)) lua_pushinteger(L, n); else { luaL_checkany(L, 1); @@ -175,7 +175,8 @@ static int math_log (lua_State *L) { lua_Number base = luaL_checknumber(L, 2); #if !defined(LUA_USE_C89) if (base == l_mathop(2.0)) - res = l_mathop(log2)(x); else + res = l_mathop(log2)(x); + else #endif if (base == l_mathop(10.0)) res = l_mathop(log10)(x); diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index 43739bffd..9029d588c 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -24,12 +24,12 @@ #if defined(EMERGENCYGCTESTS) /* -** First allocation will fail whenever not building initial state -** and not shrinking a block. (This fail will trigger 'tryagain' and -** a full GC cycle at every allocation.) +** First allocation will fail whenever not building initial state. +** (This fail will trigger 'tryagain' and a full GC cycle at every +** allocation.) */ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (ttisnil(&g->nilvalue) && ns > os) + if (completestate(g) && ns > 0) /* frees never fail */ return NULL; /* fail */ else /* normal allocation */ return (*g->frealloc)(g->ud, block, os, ns); @@ -83,7 +83,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ - if (unlikely(size >= limit)) /* cannot grow even a little? */ + if (l_unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } @@ -138,15 +138,17 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* -** In case of allocation fail, this function will call the GC to try -** to free some memory and then try the allocation again. -** (It should not be called when shrinking a block, because then the -** interpreter may be in the middle of a collection step.) +** In case of allocation fail, this function will do an emergency +** collection to free some memory and then try the allocation again. +** The GC should not be called while state is not fully built, as the +** collector is not yet fully initialized. Also, it should not be called +** when 'gcstopem' is true, because then the interpreter is in the +** middle of a collection step. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (ttisnil(&g->nilvalue)) { /* is state fully build? */ + if (completestate(g) && !g->gcstopem) { luaC_fullgc(L, 1); /* try to free some memory... */ return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } @@ -156,17 +158,14 @@ static void *tryagain (lua_State *L, void *block, /* ** Generic allocation routine. -** If allocation fails while shrinking a block, do not try again; the -** GC shrinks some blocks and it is not reentrant. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); newblock = firsttry(g, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) { - if (nsize > osize) /* not shrinking a block? */ - newblock = tryagain(L, block, osize, nsize); + if (l_unlikely(newblock == NULL && nsize > 0)) { + newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ return NULL; /* do not update 'GCdebt' */ } @@ -179,7 +178,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ + if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; } @@ -191,7 +190,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { else { global_State *g = G(L); void *newblock = firsttry(g, NULL, tag, size); - if (unlikely(newblock == NULL)) { + if (l_unlikely(newblock == NULL)) { newblock = tryagain(L, NULL, tag, size); if (newblock == NULL) luaM_error(L); diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index c0ec9a131..6f9fa3736 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -132,14 +132,16 @@ static void lsys_unloadlib (void *lib) { 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()); + if (l_unlikely(lib == NULL)) + lua_pushstring(L, dlerror()); return lib; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = cast_func(dlsym(lib, sym)); - if (f == NULL) lua_pushstring(L, dlerror()); + if (l_unlikely(f == NULL)) + lua_pushstring(L, dlerror()); return f; } @@ -410,7 +412,7 @@ static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); int stat = lookforfunc(L, path, init); - if (stat == 0) /* no errors? */ + if (l_likely(stat == 0)) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ luaL_pushfail(L); @@ -523,14 +525,14 @@ static const char *findfile (lua_State *L, const char *name, const char *path; lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); - if (path == NULL) + if (l_unlikely(path == NULL)) luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } static int checkload (lua_State *L, int stat, const char *filename) { - if (stat) { /* module loaded successfully? */ + if (l_likely(stat)) { /* module loaded successfully? */ lua_pushstring(L, filename); /* will be 2nd argument to module */ return 2; /* return open function and file name */ } @@ -623,13 +625,14 @@ static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ /* push 'package.searchers' to index 3 in the stack */ - if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) + if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") + != LUA_TTABLE)) luaL_error(L, "'package.searchers' must be a table"); luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { luaL_addstring(&msg, "\n\t"); /* error-message prefix */ - if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ + if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_buffsub(&msg, 2); /* remove prefix */ luaL_pushresult(&msg); /* create error message */ diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index a2910dfd5..73e5adabd 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -136,10 +136,18 @@ typedef struct TValue { /* -** Entries in the Lua stack +** Entries in a Lua stack. Field 'tbclist' forms a list of all +** to-be-closed variables active in this stack. Dummy entries are +** used when the distance between two tbc variables does not fit +** in an unsigned short. */ typedef union StackValue { TValue val; + struct { + TValuefields; + lu_byte isdummy; + unsigned short delta; + } tbclist; } StackValue; @@ -571,10 +579,11 @@ typedef struct Proto { #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL) #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) #define ttislcf(o) checktag((o), LUA_VLCF) #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) +#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) + #define isLfunction(o) ttisLclosure(o) diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 120cdd943..d6a47e5af 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -225,13 +225,13 @@ OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ OP_ADDI,/* A B sC R[A] := R[B] + sC */ -OP_ADDK,/* A B C R[A] := R[B] + K[C] */ -OP_SUBK,/* A B C R[A] := R[B] - K[C] */ -OP_MULK,/* A B C R[A] := R[B] * K[C] */ -OP_MODK,/* A B C R[A] := R[B] % K[C] */ -OP_POWK,/* A B C R[A] := R[B] ^ K[C] */ -OP_DIVK,/* A B C R[A] := R[B] / K[C] */ -OP_IDIVK,/* A B C R[A] := R[B] // K[C] */ +OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */ +OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */ +OP_MULK,/* A B C R[A] := R[B] * K[C]:number */ +OP_MODK,/* A B C R[A] := R[B] % K[C]:number */ +OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */ +OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */ +OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */ OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index e65e188bd..3e20d622b 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -170,7 +170,7 @@ static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); - if (err) + if (l_unlikely(err)) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); return 1; @@ -208,7 +208,7 @@ static int os_clock (lua_State *L) { */ static void setfield (lua_State *L, const char *key, int value, int delta) { #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) - if (value > LUA_MAXINTEGER - delta) + if (l_unlikely(value > LUA_MAXINTEGER - delta)) luaL_error(L, "field '%s' is out-of-bound", key); #endif lua_pushinteger(L, (lua_Integer)value + delta); @@ -253,9 +253,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { 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 an integer? */ - if (t != LUA_TNIL) /* some other value? */ + if (l_unlikely(t != LUA_TNIL)) /* some other value? */ return luaL_error(L, "field '%s' is not an integer", key); - else if (d < 0) /* absent field; no default? */ + else if (l_unlikely(d < 0)) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 77813a74e..284ef1f0c 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -128,7 +128,7 @@ static void checknext (LexState *ls, int c) { ** in line 'where' (if that is not the current line). */ static void check_match (LexState *ls, int what, int who, int where) { - if (unlikely(!testnext(ls, what))) { + if (l_unlikely(!testnext(ls, what))) { if (where == ls->linenumber) /* all in the same line? */ error_expected(ls, what); /* do not need a complex message */ else { @@ -222,26 +222,26 @@ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { /* -** Convert 'nvar', a compiler index level, to it corresponding -** stack index level. For that, search for the highest variable -** below that level that is in the stack and uses its stack -** index ('sidx'). +** Convert 'nvar', a compiler index level, to its corresponding +** register. For that, search for the highest variable below that level +** that is in a register and uses its register index ('ridx') plus one. */ -static int stacklevel (FuncState *fs, int nvar) { +static int reglevel (FuncState *fs, int nvar) { while (nvar-- > 0) { - Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ - if (vd->vd.kind != RDKCTC) /* is in the stack? */ - return vd->vd.sidx + 1; + Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ + if (vd->vd.kind != RDKCTC) /* is in a register? */ + return vd->vd.ridx + 1; } - return 0; /* no variables in the stack */ + return 0; /* no variables in registers */ } /* -** Return the number of variables in the stack for function 'fs' +** Return the number of variables in the register stack for the given +** function. */ int luaY_nvarstack (FuncState *fs) { - return stacklevel(fs, fs->nactvar); + return reglevel(fs, fs->nactvar); } @@ -267,7 +267,7 @@ static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; e->u.var.vidx = vidx; - e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; + e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -310,12 +310,12 @@ static void check_readonly (LexState *ls, expdesc *e) { */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; - int stklevel = luaY_nvarstack(fs); + int reglevel = luaY_nvarstack(fs); int i; for (i = 0; i < nvars; i++) { int vidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, vidx); - var->vd.sidx = stklevel++; + var->vd.ridx = reglevel++; var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } } @@ -366,7 +366,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { FuncState *prev = fs->prev; if (v->k == VLOCAL) { up->instack = 1; - up->idx = v->u.var.sidx; + up->idx = v->u.var.ridx; up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); } @@ -517,7 +517,7 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) { Labellist *gl = &ls->dyd->gt; /* list of goto's */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); - if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ + if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); luaK_patchlist(ls->fs, gt->pc, label->pc); for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ @@ -620,7 +620,7 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) { for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; /* leaving a variable scope? */ - if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) + if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar)) gt->close |= bl->upval; /* jump may need a close */ gt->nactvar = bl->nactvar; /* update goto level */ } @@ -661,7 +661,7 @@ static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; int hasclose = 0; - int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ + int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ if (bl->isloop) /* fix pending breaks? */ hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); if (!hasclose && bl->previous && bl->upval) @@ -1330,13 +1330,13 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } } else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.var.sidx) { + lh->v.u.ind.idx == v->u.var.ridx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } @@ -1346,7 +1346,7 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { if (conflict) { /* copy upvalue/local value to a temporary (in position 'extra') */ if (v->k == VLOCAL) - luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); + luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); else luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); @@ -1411,7 +1411,7 @@ static void gotostat (LexState *ls) { newgotoentry(ls, name, line, luaK_jump(fs)); else { /* found a label */ /* backward jump; will be resolved here */ - int lblevel = stacklevel(fs, lb->nactvar); /* label level */ + int lblevel = reglevel(fs, lb->nactvar); /* label level */ if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); /* create jump and link it to the label */ @@ -1435,7 +1435,7 @@ static void breakstat (LexState *ls) { */ static void checkrepeated (LexState *ls, TString *name) { Labeldesc *lb = findlabel(ls, name); - if (unlikely(lb != NULL)) { /* already defined? */ + if (l_unlikely(lb != NULL)) { /* already defined? */ const char *msg = "label '%s' already defined on line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); luaK_semerror(ls, msg); /* error */ @@ -1488,7 +1488,7 @@ static void repeatstat (LexState *ls, int line) { if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ - luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); + luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); condexit = luaK_jump(fs); /* repeat after closing upvalues */ luaK_patchtohere(fs, exit); /* normal exit comes to here */ } @@ -1520,7 +1520,7 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { int offset = dest - (pc + 1); if (back) offset = -offset; - if (unlikely(offset > MAXARG_Bx)) + if (l_unlikely(offset > MAXARG_Bx)) luaX_syntaxerror(fs->ls, "control structure too long"); SETARG_Bx(*jmp, offset); } @@ -1708,7 +1708,7 @@ static void checktoclose (LexState *ls, int level) { FuncState *fs = ls->fs; markupval(fs, level + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); + luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); } } diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 2e6dae72f..5e4500f18 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -35,7 +35,7 @@ typedef enum { (string is fixed by the lexer) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; var.sidx = stack index (local register); + VLOCAL, /* local variable; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VCONST, /* compile-time variable; @@ -77,7 +77,7 @@ typedef struct expdesc { lu_byte t; /* table (register or upvalue) */ } ind; struct { /* for local variables */ - lu_byte sidx; /* index in the stack */ + lu_byte ridx; /* register holding the variable */ unsigned short vidx; /* compiler index (in 'actvar.arr') */ } var; } u; @@ -97,7 +97,7 @@ typedef union Vardesc { struct { TValuefields; /* constant value (if it is a compile-time constant) */ lu_byte kind; - lu_byte sidx; /* index of the variable in the stack */ + lu_byte ridx; /* register holding the variable */ short pidx; /* index of the variable in the Proto's 'locvars' array */ TString *name; /* variable name */ } vd; diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 477c0e6d3..f79255a03 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) { LUAI_FUNC void luaE_incCstack (lua_State *L) { L->nCcalls++; - if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); } @@ -181,6 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); + L1->tbclist = L1->stack; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; @@ -213,24 +214,19 @@ static void freestack (lua_State *L) { ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { - 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, &temp, L); /* temp = L */ - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); - /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); + setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); + /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ + sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); } /* ** open parts of the state that may cause memory-allocation errors. -** ('g->nilvalue' being a nil value flags that the state was completely -** build.) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -241,7 +237,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ - setnilvalue(&g->nilvalue); + setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } @@ -256,6 +252,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->ci = NULL; L->nci = 0; L->twups = L; /* thread has no upvalues */ + L->nCcalls = 0; L->errorJmp = NULL; L->hook = NULL; L->hookmask = 0; @@ -271,10 +268,13 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */ - luaC_freeallobjects(L); /* collect all objects */ - if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ + if (!completestate(g)) /* closing a partially built state? */ + luaC_freeallobjects(L); /* jucst collect its objects */ + else { /* closing a fully built state */ + luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ + luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); + } luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); @@ -299,7 +299,6 @@ LUA_API lua_State *lua_newthread (lua_State *L) { setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); - L1->nCcalls = 0; L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; @@ -316,7 +315,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */ + luaF_closeupval(L1, L1->stack); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -324,23 +323,29 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } -int lua_resetthread (lua_State *L) { - CallInfo *ci; - int status; - lua_lock(L); - L->ci = ci = &L->base_ci; /* unwind CallInfo list */ +int luaE_resetthread (lua_State *L, int status) { + CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ ci->func = L->stack; ci->callstatus = CIST_C; - status = luaF_close(L, L->stack, CLOSEPROTECT); - if (status != CLOSEPROTECT) /* real errors? */ - luaD_seterrorobj(L, status, L->stack + 1); - else { + if (status == LUA_YIELD) status = LUA_OK; + status = luaD_closeprotected(L, 1, status); + if (status != LUA_OK) /* errors? */ + luaD_seterrorobj(L, status, L->stack + 1); + else L->top = L->stack + 1; - } ci->top = L->top + LUA_MINSTACK; - L->status = status; + L->status = cast_byte(status); + luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); + return status; +} + + +LUA_API int lua_resetthread (lua_State *L) { + int status; + lua_lock(L); + status = luaE_resetthread(L, L->status); lua_unlock(L); return status; } @@ -360,7 +365,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - L->nCcalls = 0; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; @@ -374,6 +378,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->panic = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; + g->gcstopem = 0; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index b70564a06..34a9b2e89 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -156,6 +156,18 @@ typedef struct stringtable { /* ** Information about a call. +** About union 'u': +** - field 'l' is used only for Lua functions; +** - field 'c' is used only for C functions. +** About union 'u2': +** - field 'funcidx' is used only by C functions while doing a +** protected call; +** - field 'nyield' is used only while a function is "doing" an +** yield (from the yield until the next resume); +** - field 'nres' is used only while closing tbc variables when +** returning from a C function; +** - field 'transferinfo' is used only during call/returnhooks, +** before the function starts or after it ends. */ typedef struct CallInfo { StkId func; /* function index in the stack */ @@ -176,6 +188,7 @@ typedef struct CallInfo { union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ + int nres; /* number of values returned */ struct { /* info about transferred values (for call/return hooks) */ unsigned short ftransfer; /* offset of first value transferred */ unsigned short ntransfer; /* number of values transferred */ @@ -191,17 +204,34 @@ typedef struct CallInfo { */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_C (1<<1) /* call is running a C function */ -#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ +#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ #define CIST_HOOKED (1<<3) /* call is running a debug hook */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ +#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_FIN (1<<7) /* call is running a finalizer */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ +#define CIST_CLSRET (1<<9) /* function is closing tbc variables */ +/* Bits 10-12 are used for CIST_RECST (see below) */ +#define CIST_RECST 10 #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<9) /* using __lt for __le */ +#define CIST_LEQ (1<<13) /* using __lt for __le */ #endif + +/* +** Field CIST_RECST stores the "recover status", used to keep the error +** status while closing to-be-closed variables in coroutines, so that +** Lua can correctly resume after an yield from a __close method called +** because of an error. (Three bits are enough for error status.) +*/ +#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) +#define setcistrecst(ci,st) \ + check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ + ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ + | ((st) << CIST_RECST))) + + /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) @@ -229,6 +259,7 @@ typedef struct global_State { lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte gcstopem; /* stops emergency collections */ lu_byte genminormul; /* control for minor generational collections */ lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ @@ -280,6 +311,7 @@ struct lua_State { StkId stack_last; /* end of stack (last element + 1) */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ + StkId tbclist; /* list of to-be-closed variables */ GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ @@ -296,6 +328,12 @@ struct lua_State { #define G(L) (L->l_G) +/* +** 'g->nilvalue' being a nil value flags that the state was completely +** build. +*/ +#define completestate(g) ttisnil(&g->nilvalue) + /* ** Union of all collectable objects (only for conversions) @@ -358,6 +396,7 @@ LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); +LUAI_FUNC int luaE_resetthread (lua_State *L, int status); #endif diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 89672afef..29372c6af 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -23,7 +23,7 @@ #include "atomic.h" static unsigned int STRSEED; -static ATOM_SIZET STRID = 0; +static size_t STRID = 0; /* ** Maximum size for string table. @@ -60,7 +60,7 @@ void luaS_share (TString *ts) { if (ts == NULL || isshared(ts)) return; makeshared(ts); - ts->id = ATOM_FDEC(&STRID) - 1; + ts->id = ATOM_DEC(&STRID); } unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { @@ -112,7 +112,7 @@ void luaS_resize (lua_State *L, int nsize) { if (nsize < osize) /* shrinking table? */ tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); - if (unlikely(newvect == NULL)) { /* reallocation failed? */ + if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ @@ -214,7 +214,7 @@ void luaS_remove (lua_State *L, TString *ts) { static void growstrtab (lua_State *L, stringtable *tb) { - if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ + if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ if (tb->nuse == MAX_INT) /* still too many? */ luaM_error(L); /* cannot even create a message... */ @@ -265,7 +265,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) + if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getstr(ts), str, l * sizeof(char)); @@ -301,7 +301,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { Udata *u; int i; GCObject *o; - if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) + if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 940a14ca5..47e5b27a6 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -152,8 +152,9 @@ static int str_rep (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); 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? */ + if (n <= 0) + lua_pushliteral(L, ""); + else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n)) return luaL_error(L, "resulting string too large"); else { size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; @@ -181,7 +182,7 @@ static int str_byte (lua_State *L) { size_t pose = getendpos(L, 3, pi, l); int n, i; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */ + if (l_unlikely(pose - posi >= (size_t)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"); @@ -235,7 +236,7 @@ static int str_dump (lua_State *L) { luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); /* ensure function is on the top of the stack */ state.init = 0; - if (lua_dump(L, writer, &state, strip) != 0) + if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) return luaL_error(L, "unable to dump given function"); luaL_pushresult(&state.B); return 1; @@ -275,7 +276,8 @@ static int tonum (lua_State *L, int arg) { static void trymt (lua_State *L, const char *mtname) { lua_settop(L, 2); /* back to the original arguments */ - if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) + if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || + !luaL_getmetafield(L, 2, mtname))) luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, luaL_typename(L, -2), luaL_typename(L, -1)); lua_insert(L, -3); /* put metamethod before arguments */ @@ -383,7 +385,8 @@ static const char *match (MatchState *ms, const char *s, const char *p); static int check_capture (MatchState *ms, int l) { l -= '1'; - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) + if (l_unlikely(l < 0 || l >= ms->level || + ms->capture[l].len == CAP_UNFINISHED)) return luaL_error(ms->L, "invalid capture index %%%d", l + 1); return l; } @@ -400,14 +403,14 @@ static int capture_to_close (MatchState *ms) { static const char *classend (MatchState *ms, const char *p) { switch (*p++) { case L_ESC: { - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; do { /* look for a ']' */ - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) p++; /* skip escapes (e.g. '%]') */ @@ -482,7 +485,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) + if (l_unlikely(p >= ms->p_end - 1)) luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { @@ -565,7 +568,7 @@ static const char *match_capture (MatchState *ms, const char *s, int l) { static const char *match (MatchState *ms, const char *s, const char *p) { - if (ms->matchdepth-- == 0) + if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); init: /* using goto's to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ @@ -599,7 +602,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case 'f': { /* frontier? */ const char *ep; char previous; p += 2; - if (*p != '[') + if (l_unlikely(*p != '[')) 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); @@ -699,7 +702,7 @@ static const char *lmemfind (const char *s1, size_t l1, static size_t get_onecapture (MatchState *ms, int i, const char *s, const char *e, const char **cap) { if (i >= ms->level) { - if (i != 0) + if (l_unlikely(i != 0)) luaL_error(ms->L, "invalid capture index %%%d", i + 1); *cap = s; return e - s; @@ -707,7 +710,7 @@ static size_t get_onecapture (MatchState *ms, int i, const char *s, else { ptrdiff_t capl = ms->capture[i].len; *cap = ms->capture[i].init; - if (capl == CAP_UNFINISHED) + if (l_unlikely(capl == CAP_UNFINISHED)) luaL_error(ms->L, "unfinished capture"); else if (capl == CAP_POSITION) lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); @@ -926,7 +929,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, luaL_addlstring(b, s, e - s); /* keep original text */ return 0; /* no changes */ } - else if (!lua_isstring(L, -1)) + else if (l_unlikely(!lua_isstring(L, -1))) return luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); else { @@ -1058,7 +1061,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, for (i = 0; i < n; i++) buff[i] = toupper(uchar(buff[i])); } - else if (fmt[SIZELENMOD] != 'a') + else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } @@ -1358,16 +1361,6 @@ struct cD { #define MAXALIGN (offsetof(struct cD, u)) -/* -** Union for serializing floats -*/ -typedef union Ftypes { - float f; - double d; - lua_Number n; -} Ftypes; - - /* ** information to pack/unpack stuff */ @@ -1384,7 +1377,9 @@ typedef struct Header { typedef enum KOption { Kint, /* signed integers */ Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ + Kfloat, /* single-precision floating-point numbers */ + Knumber, /* Lua "native" floating-point numbers */ + Kdouble, /* double-precision floating-point numbers */ Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ Kzstr, /* zero-terminated strings */ @@ -1419,7 +1414,7 @@ static int getnum (const char **fmt, int df) { */ static int getnumlimit (Header *h, const char **fmt, int df) { int sz = getnum(fmt, df); - if (sz > MAXINTSIZE || sz <= 0) + if (l_unlikely(sz > MAXINTSIZE || sz <= 0)) return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE); return sz; @@ -1453,14 +1448,14 @@ static KOption getoption (Header *h, const char **fmt, int *size) { 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 'n': *size = sizeof(lua_Number); return Knumber; + case 'd': *size = sizeof(double); return Kdouble; 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) + if (l_unlikely(*size == -1)) luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -1499,7 +1494,7 @@ static KOption getdetails (Header *h, size_t totalsize, else { if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + if (l_unlikely((align & (align - 1)) != 0)) /* 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); } @@ -1580,15 +1575,27 @@ static int str_pack (lua_State *L) { packint(&b, (lua_Unsigned)n, h.islittle, size, 0); break; } - case Kfloat: { /* floating-point options */ - 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, (char *)&u, size, h.islittle); + case Kfloat: { /* C float */ + float f = (float)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); + break; + } + case Knumber: { /* Lua float */ + lua_Number f = luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); + break; + } + case Kdouble: { /* C double */ + double f = (double)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } @@ -1679,7 +1686,7 @@ static lua_Integer unpackint (lua_State *L, const char *str, 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) + if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); } } @@ -1714,13 +1721,21 @@ static int str_unpack (lua_State *L) { break; } case Kfloat: { - Ftypes u; - lua_Number num; - copywithendian((char *)&u, 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); + float f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); + break; + } + case Knumber: { + lua_Number f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, f); + break; + } + case Kdouble: { + double f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); break; } case Kchar: { diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index b3840e77d..dc838f0bb 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -309,7 +309,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key, 1); - if (unlikely(isabstkey(n))) + if (l_unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ @@ -487,7 +487,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { already present in the table */ TValue k; getnodekey(L, &k, old); - setobjt2t(L, luaH_set(L, t, &k), gval(old)); + luaH_set(L, t, &k, gval(old)); } } } @@ -543,7 +543,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, } /* allocate new array */ newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); - if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ + if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ } @@ -634,10 +634,12 @@ static Node *getfreepos (Table *t) { ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ -TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { +void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { Node *mp; TValue aux; - if (unlikely(ttisnil(key))) + if (l_unlikely(isshared(t))) + luaG_runerror(L, "attempt to change a shared table"); + if (l_unlikely(ttisnil(key))) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Number f = fltvalue(key); @@ -646,9 +648,11 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { setivalue(&aux, k); key = &aux; /* insert it as an integer */ } - else if (unlikely(luai_numisnan(f))) + else if (l_unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } + if (ttisnil(value)) + return; /* do not insert nil values */ mp = mainpositionTV(t, key); if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; @@ -656,7 +660,8 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { if (f == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ /* whatever called 'newkey' takes care of TM cache */ - return luaH_set(L, t, key); /* insert key into grown table */ + luaH_set(L, t, key, value); /* insert key into grown table */ + return; } lua_assert(!isdummy(t)); othern = mainposition(t, keytt(mp), &keyval(mp)); @@ -684,7 +689,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { setnodekey(L, mp, key); luaC_barrierback(L, obj2gco(t), key); lua_assert(isempty(gval(mp))); - return gval(mp); + setobj2t(L, gval(mp), value); } @@ -771,35 +776,46 @@ const TValue *luaH_get (Table *t, const TValue *key) { } +/* +** Finish a raw "set table" operation, where 'slot' is where the value +** should have been (the result of a previous "get table"). +** Beware: when using this function you probably need to check a GC +** barrier and invalidate the TM cache. +*/ +void luaH_finishset (lua_State *L, Table *t, const TValue *key, + const TValue *slot, TValue *value) { + if (isabstkey(slot)) + luaH_newkey(L, t, key, value); + else { + if (l_unlikely(isshared(t))) + luaG_runerror(L, "attempt to change a shared table"); + setobj2t(L, cast(TValue *, slot), value); + } +} + + /* ** beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. */ -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p; - if(isshared(t)) - luaG_runerror(L, "attempt to change a shared table"); - p = luaH_get(t, key); - if (!isabstkey(p)) - return cast(TValue *, p); - else return luaH_newkey(L, t, key); +void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { + const TValue *slot = luaH_get(t, key); + luaH_finishset(L, t, key, slot, value); } void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p; - TValue *cell; - if(isshared(t)) - luaG_runerror(L, "attempt to change a shared table"); - p = luaH_getint(t, key); - if (!isabstkey(p)) - cell = cast(TValue *, p); - else { + const TValue *p = luaH_getint(t, key); + if (isabstkey(p)) { TValue k; setivalue(&k, key); - cell = luaH_newkey(L, t, &k); + luaH_newkey(L, t, &k, value); + } + else { + if (l_unlikely(isshared(t))) + luaG_runerror(L, "attempt to change a shared table"); + setobj2t(L, cast(TValue *, p), value); } - setobj2t(L, cell, value); } diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index c0060f4b6..7bbbcb213 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -41,8 +41,12 @@ LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 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); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); +LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value); +LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, + TValue *value); +LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, + const TValue *slot, TValue *value); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize); diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index d344a47e9..d80eb8015 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -145,8 +145,8 @@ static int tmove (lua_State *L) { 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'", + if (l_unlikely(!lua_isstring(L, -1))) + luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", luaL_typename(L, -1), i); luaL_addvalue(b); } @@ -196,7 +196,8 @@ static int tunpack (lua_State *L) { 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))) + if (l_unlikely(n >= (unsigned int)INT_MAX || + !lua_checkstack(L, (int)(++n)))) return luaL_error(L, "too many results to unpack"); for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ lua_geti(L, 1, i); @@ -300,14 +301,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { for (;;) { /* next loop: repeat ++i while a[i] < P */ while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ + if (l_unlikely(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 ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j < i) /* j < i but a[j] > P ?? */ + if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 4770f96bb..b657b783a 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -147,7 +147,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (!callbinTM(L, p1, p2, res, event)) { + if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { switch (event) { case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { @@ -166,7 +166,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_tryconcatTM (lua_State *L) { StkId top = L->top; - if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT)) + if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, + TM_CONCAT))) luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); } diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index b5b884b6d..46b48dba9 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -37,6 +37,26 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; +#if defined(LUA_USE_POSIX) /* { */ + +/* +** Use 'sigaction' when available. +*/ +static void setsignal (int sig, void (*handler)(int)) { + struct sigaction sa; + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); /* do not mask any signal */ + sigaction(sig, &sa, NULL); +} + +#else /* }{ */ + +#define setsignal signal + +#endif /* } */ + + /* ** Hook set by signal function to stop the interpreter. */ @@ -55,7 +75,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { */ static void laction (int i) { int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; - signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ + setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ lua_sethook(globalL, lstop, flag, 1); } @@ -135,9 +155,9 @@ static int docall (lua_State *L, int narg, int nres) { 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); /* set C-signal handler */ + setsignal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); /* reset C-signal handler */ + setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ lua_remove(L, base); /* remove message handler from the stack */ return status; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index aa2763450..9abe3f803 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,14 +18,14 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "2" +#define LUA_VERSION_RELEASE "3" #define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 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-2020 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2021 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -351,7 +351,8 @@ 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); -LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_closeslot) (lua_State *L, int idx); /* @@ -495,7 +496,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2020 Lua.org, PUC-Rio. +* Copyright (C) 1994-2021 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 d9cf18ca1..ae73e2fd6 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -16,13 +16,13 @@ ** =================================================================== ** General Configuration File for Lua ** -** Some definitions here can be changed externally, through the -** compiler (e.g., with '-D' options). Those are protected by -** '#if !defined' guards. However, several other definitions should -** be changed directly here, either because they affect the Lua -** ABI (by making the changes here, you ensure that all software -** connected to Lua, such as C libraries, will be compiled with the -** same configuration); or because they are seldom changed. +** Some definitions here can be changed externally, through the compiler +** (e.g., with '-D' options): They are commented out or protected +** by '#if !defined' guards. However, several other definitions +** should be changed directly here, either because they affect the +** Lua ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the same +** configuration); or because they are seldom changed. ** ** Search for "@@" to find all configurable definitions. ** =================================================================== @@ -81,26 +81,12 @@ /* ** {================================================================== -** Configuration for Number types. +** Configuration for Number types. These options should not be +** set externally, because any other code connected to Lua must +** use the same configuration. ** =================================================================== */ -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. -*/ -/* #define LUA_32BITS */ - - -/* -@@ 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_INT_TYPE defines the type for Lua integers. @@ LUA_FLOAT_TYPE defines the type for Lua floats. @@ -121,7 +107,31 @@ #define LUA_FLOAT_DOUBLE 2 #define LUA_FLOAT_LONGDOUBLE 3 -#if defined(LUA_32BITS) /* { */ + +/* Default configuration ('long long' and 'double', for 64-bit Lua) */ +#define LUA_INT_DEFAULT LUA_INT_LONGLONG +#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE + + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +#define LUA_32BITS 0 + + +/* +@@ 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 1 +#else +#define LUA_C89_NUMBERS 0 +#endif + + +#if LUA_32BITS /* { */ /* ** 32-bit integers and 'float' */ @@ -132,26 +142,21 @@ #endif #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT -#elif defined(LUA_C89_NUMBERS) /* }{ */ +#elif LUA_C89_NUMBERS /* }{ */ /* ** largest types available for C89 ('long' and 'double') */ #define LUA_INT_TYPE LUA_INT_LONG #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* } */ +#else /* }{ */ +/* use defaults */ +#define LUA_INT_TYPE LUA_INT_DEFAULT +#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT -/* -** default configuration for 64-bit Lua ('long long' and 'double') -*/ -#if !defined(LUA_INT_TYPE) -#define LUA_INT_TYPE LUA_INT_LONGLONG -#endif +#endif /* } */ -#if !defined(LUA_FLOAT_TYPE) -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* }================================================================== */ @@ -373,14 +378,13 @@ /* ** {================================================================== -** Configuration for Numbers. +** Configuration for Numbers (low-level part). ** 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 a 'default argument promotion' @@ over a floating number. @@ l_floatatt(x) corrects float attribute 'x' to the proper float type @@ -473,10 +477,7 @@ /* -@@ LUA_INTEGER is the integer type used by Lua. -** @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. -** @@ 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. @@ -659,6 +660,26 @@ #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) #endif + +/* +** macros to improve jump prediction, used mostly for error handling +** and debug facilities. +*/ +#if (defined(LUA_CORE) || defined(LUA_LIB)) && !defined(l_likely) + +#include +#if defined(__GNUC__) +#define l_likely(x) (__builtin_expect(((x) != 0), 1)) +#define l_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#else +#define l_likely(x) (x) +#define l_unlikely(x) (x) +#endif + +#endif + + + /* }================================================================== */ diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index e5324c919..33cefc6c4 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -52,10 +52,4 @@ LUALIB_API void (luaL_initcodecache) (void); LUALIB_API void (luaL_openlibs) (lua_State *L); - -#if !defined(lua_assert) -#define lua_assert(x) ((void)0) -#endif - - #endif diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index e6e1033a3..cf6bc2b2c 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -235,11 +235,11 @@ static int forprep (lua_State *L, StkId ra) { } else { /* try making all values floats */ lua_Number init; lua_Number limit; lua_Number step; - if (unlikely(!tonumber(plimit, &limit))) + if (l_unlikely(!tonumber(plimit, &limit))) luaG_forerror(L, plimit, "limit"); - if (unlikely(!tonumber(pstep, &step))) + if (l_unlikely(!tonumber(pstep, &step))) luaG_forerror(L, pstep, "step"); - if (unlikely(!tonumber(pinit, &init))) + if (l_unlikely(!tonumber(pinit, &init))) luaG_forerror(L, pinit, "initial value"); if (step == 0) luaG_runerror(L, "'for' step is zero"); @@ -292,7 +292,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, if (slot == NULL) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } @@ -339,10 +339,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (isabstkey(slot)) /* 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 *, slot), val); /* set its new value */ + luaH_finishset(L, h, key, slot, val); /* set new value */ invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; @@ -351,7 +348,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } else { /* not a table; check metamethod */ tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ @@ -573,8 +570,13 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ - lua_Integer i1, i2; /* compare them as integers */ - return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2); + /* One of them is an integer. If the other does not have an + integer value, they cannot be equal; otherwise, compare their + integer values. */ + lua_Integer i1, i2; + return (luaV_tointegerns(t1, &i1, F2Ieq) && + luaV_tointegerns(t2, &i2, F2Ieq) && + i1 == i2); } } /* values have same type and same variant */ @@ -656,7 +658,7 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); - if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) + if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) luaG_runerror(L, "string length overflow"); tl += l; } @@ -700,7 +702,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (unlikely(notm(tm))) /* no metamethod? */ + if (l_unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } @@ -716,7 +718,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** otherwise 'floor(q) == trunc(q) - 1'. */ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(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 */ @@ -736,7 +738,7 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { ** about luaV_idiv.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(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 */ @@ -847,6 +849,10 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ break; } + case OP_CLOSE: case OP_RETURN: { /* yielded closing variables */ + ci->u.l.savedpc--; /* repeat instruction to close other vars. */ + break; + } default: { /* only these other opcodes can yield */ lua_assert(op == OP_TFORCALL || op == OP_CALL || @@ -922,7 +928,7 @@ void luaV_finishOp (lua_State *L) { */ #define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } @@ -951,7 +957,7 @@ void luaV_finishOp (lua_State *L) { */ #define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arith_aux(L, v1, v2, iop, fop); } @@ -1050,7 +1056,8 @@ void luaV_finishOp (lua_State *L) { #define updatebase(ci) (base = ci->func + 1) -#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } +#define updatestack(ci) \ + { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } /* @@ -1108,7 +1115,7 @@ void luaV_finishOp (lua_State *L) { /* fetch an instruction and prepare its execution */ #define vmfetch() { \ - if (trap) { /* stack reallocation or hooks? */ \ + if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ trap = luaG_traceexec(L, pc); /* handle hooks */ \ updatebase(ci); /* correct stack */ \ } \ @@ -1136,7 +1143,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cl = clLvalue(s2v(ci->func)); k = cl->p->k; pc = ci->u.l.savedpc; - if (trap) { + if (l_unlikely(trap)) { if (pc == cl->p->code) { /* first instruction (not resuming)? */ if (cl->p->is_vararg) trap = 0; /* hooks will start after VARARGPREP instruction */ @@ -1151,6 +1158,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ vmfetch(); +// low-level line tracing for debugging Lua +// printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ @@ -1529,7 +1538,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { - Protect(luaF_close(L, ra, LUA_OK)); + Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { @@ -1634,10 +1643,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { b = cast_int(L->top - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { - /* close upvalues from current call; the compiler ensures - that there are no to-be-closed variables here, so this - call cannot change the stack */ - luaF_close(L, base, NOCLOSINGMETH); + luaF_closeupval(L, base); /* close upvalues from current call */ + lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } while (!ttisfunction(s2v(ra))) { /* not a function? */ @@ -1667,7 +1674,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) { /* may there be open upvalues? */ if (L->top < ci->top) L->top = ci->top; - luaF_close(L, base, LUA_OK); + luaF_close(L, base, CLOSEKTOP, 1); updatetrap(ci); updatestack(ci); } @@ -1679,23 +1686,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) { goto ret; } vmcase(OP_RETURN0) { - if (L->hookmask) { + if (l_unlikely(L->hookmask)) { L->top = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ trap = 1; } else { /* do the 'poscall' here */ - int nres = ci->nresults; + int nres; L->ci = ci->previous; /* back to caller */ L->top = base - 1; - while (nres-- > 0) + for (nres = ci->nresults; l_unlikely(nres > 0); nres--) setnilvalue(s2v(L->top++)); /* all results are nil */ } goto ret; } vmcase(OP_RETURN1) { - if (L->hookmask) { + if (l_unlikely(L->hookmask)) { L->top = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ @@ -1709,8 +1716,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { else { setobjs2s(L, base - 1, ra); /* at least this result */ L->top = base; - while (--nres > 0) /* complete missing results */ - setnilvalue(s2v(L->top++)); + for (; l_unlikely(nres > 1); nres--) + setnilvalue(s2v(L->top++)); /* complete missing results */ } } ret: /* return from a Lua function */ @@ -1813,7 +1820,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_VARARGPREP) { ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); - if (trap) { + if (l_unlikely(trap)) { /* previous "Protect" updated trap */ luaD_hookcall(L, ci); L->oldpc = 1; /* next opcode will be seen as a "new" line */ } diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 3deb1280e..a7ab2c195 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -60,12 +60,14 @@ typedef enum { /* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointeger(o,i,LUA_FLOORN2I)) /* convert an object to an integer (without string coercion) */ #define tointegerns(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointegerns(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) From 213c9966e4ed3f23ffc4dcd28ec86cc1de53c55c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 10 Mar 2021 12:01:15 +0800 Subject: [PATCH 337/565] fix build --- 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 29372c6af..d21b44f47 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -60,7 +60,7 @@ void luaS_share (TString *ts) { if (ts == NULL || isshared(ts)) return; makeshared(ts); - ts->id = ATOM_DEC(&STRID); + ts->id = ATOM_FDEC(&STRID)-1; } unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { From 6f2866e5c4b2682c4d6855909e3407e8085f93dc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 10 Mar 2021 12:11:02 +0800 Subject: [PATCH 338/565] avoid warnings --- 3rd/lua/lauxlib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 2c047adf3..5d5a234e6 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -125,7 +125,7 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, */ #if !defined(l_likely) -#define l_likely(x) x +#define l_likely(x) (x) #endif From b0dda0483d28c9970e92c4497c2b3cf76b52f619 Mon Sep 17 00:00:00 2001 From: EfveZombie <34877029+EfveZombie@users.noreply.github.com> Date: Wed, 10 Mar 2021 12:35:56 +0800 Subject: [PATCH 339/565] fix websocket bugs (#1359) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix websocket bugs * revert write_frame Co-authored-by: 郑非凡 --- lualib/http/websocket.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index ff6a21c03..92ac17304 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -43,7 +43,7 @@ local function write_handshake(self, host, url, header) local recvheader = {} local code, body = internal.request(self, "GET", host, url, recvheader, request_header) if code ~= 101 then - error(string.format("websocket handshake error: code[%s] info:%s", code, body)) + error(string.format("websocket handshake error: code[%s] info:%s", code, body)) end if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then @@ -261,6 +261,7 @@ local function resolve_accept(self, options) try_handle(self, "handshake", header, url) local recv_count = 0 local recv_buf = {} + local first_op while true do if _isws_closed(self.id) then try_handle(self, "close") @@ -286,11 +287,13 @@ local function resolve_accept(self, options) if recv_count > MAX_FRAME_SIZE then error("payload_len is too large") end + first_op = first_op or op if fin then local s = table.concat(recv_buf) - try_handle(self, "message", s, op) + try_handle(self, "message", s, first_op) recv_buf = {} -- clear recv_buf recv_count = 0 + first_op = nil end end end @@ -323,7 +326,7 @@ local function _new_client_ws(socket_id, protocol) websocket = true, close = function () socket.close(socket_id) - tls.closefunc(tls_ctx)() + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -369,7 +372,7 @@ local function _new_server_ws(socket_id, handle, protocol) obj = { close = function () socket.close(socket_id) - tls.closefunc(tls_ctx)() + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -430,7 +433,7 @@ function M.connect(url, header, timeout) if protocol ~= "wss" and protocol ~= "ws" then error(string.format("invalid protocol: %s", protocol)) end - + assert(host) local host_name, host_port = string.match(host, "^([^:]+):?(%d*)$") assert(host_name and host_port) @@ -470,7 +473,6 @@ function M.read(id) end end end - assert(false) end From 7c0c13b07b9ca02a49434d8602224f4ccbd5959d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 10 Mar 2021 15:24:25 +0800 Subject: [PATCH 340/565] close unmanaged socket, see issue #1358 --- lualib/skynet/socket.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e467d2a1b..34084468e 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -118,6 +118,7 @@ end socket_message[3] = function(id) local s = socket_pool[id] if s == nil then + driver.close(id) return end s.connected = false @@ -138,6 +139,7 @@ end socket_message[5] = function(id, _, err) local s = socket_pool[id] if s == nil then + driver.shutdown(id) skynet.error("socket: error on unknown", id, err) return end From bf5eacfe806f377a9d59ed868557feee014959c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1?= <996442717qqcom@gmail.com> Date: Wed, 10 Mar 2021 17:35:17 +0800 Subject: [PATCH 341/565] fix: macosx build error (#1360) Co-authored-by: xiaojin --- 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 d21b44f47..2f2214bde 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -23,7 +23,7 @@ #include "atomic.h" static unsigned int STRSEED; -static size_t STRID = 0; +static ATOM_SIZET STRID = 0; /* ** Maximum size for string table. From 094b20aae7352fade66def63d21a0d72611304a3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 11 Mar 2021 11:23:52 +0800 Subject: [PATCH 342/565] fix half close issue, #1358 --- lualib/snax/gateserver.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 74a196692..5bcea104a 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -12,6 +12,9 @@ local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) local nodelay = false local connection = {} +-- true : connected +-- nil : closed +-- false : close read function gateserver.openclient(fd) if connection[fd] then @@ -21,8 +24,8 @@ end function gateserver.closeclient(fd) local c = connection[fd] - if c then - connection[fd] = false + if c ~= nil then + connection[fd] = nil socketdriver.close(fd) end end @@ -80,7 +83,7 @@ function gateserver.start(handler) function MSG.open(fd, msg) if client_number >= maxclient then - socketdriver.close(fd) + socketdriver.shutdown(fd) return end if nodelay then @@ -91,20 +94,17 @@ function gateserver.start(handler) 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 fd ~= socket then + client_number = client_number - 1 + if connection[fd] then + connection[fd] = false -- close read + end if handler.disconnect then handler.disconnect(fd) + else + socketdriver.close(fd) end - close_fd(fd) else socket = nil end @@ -114,10 +114,10 @@ function gateserver.start(handler) if fd == socket then skynet.error("gateserver accpet error:",msg) else + socketdriver.shutdown(fd) if handler.error then handler.error(fd, msg) end - close_fd(fd) end end From 037c3a5c48faba10ad93af982f98367479898772 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 11 Mar 2021 12:31:37 +0800 Subject: [PATCH 343/565] do not close fd if no handler.disconnect, #1358 --- lualib/snax/gateserver.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 5bcea104a..dc21b45c9 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -102,8 +102,6 @@ function gateserver.start(handler) end if handler.disconnect then handler.disconnect(fd) - else - socketdriver.close(fd) end else socket = nil From a39c9b8b10a79da78aa43e51f5414e3eb04dd888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E---=E8=87=AA=E7=94=B1?= <996442717qqcom@gmail.com> Date: Thu, 11 Mar 2021 15:18:13 +0800 Subject: [PATCH 344/565] =?UTF-8?q?clean=E6=97=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9macosx=E4=B8=8B=E7=9A=84=E7=BC=96=E8=AF=91=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=94=9F=E6=95=88=20(#1361)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: macosx build error * 🐎ci(makefile): clean时增加macosx下的编译目录 Co-authored-by: xiaojin --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 88fc90ef5..337ed3e6a 100644 --- a/Makefile +++ b/Makefile @@ -119,7 +119,8 @@ $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ clean : - rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so + rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so && \ + rm -rf $(SKYNET_BUILD_PATH)/*.dSYM $(CSERVICE_PATH)/*.dSYM $(LUA_CLIB_PATH)/*.dSYM cleanall: clean ifneq (,$(wildcard 3rd/jemalloc/Makefile)) From 9228b05dfe1ad30179e805ef4e5ea6e0b3ba85e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 19 Mar 2021 18:52:09 +0800 Subject: [PATCH 345/565] socket.onclose() (#1364) * Add socket.onclose() and close socket immediately when socket channel closed by peer * Revert read_socket(), Add SOCKET_MORE, See #1358 * remove warning log , because it's not a rare case any more. See #1358 --- lualib/skynet/socket.lua | 9 ++++ lualib/skynet/socketchannel.lua | 23 ++++++++- skynet-src/socket_server.c | 82 ++++++++------------------------- skynet-src/socket_server.h | 5 +- 4 files changed, 54 insertions(+), 65 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 34084468e..8371853ad 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -123,6 +123,9 @@ socket_message[3] = function(id) end s.connected = false wakeup(s) + if s.on_close then + s.on_close(id) + end end -- SKYNET_SOCKET_TYPE_ACCEPT = 4 @@ -485,4 +488,10 @@ function socket.warning(id, callback) obj.on_warning = callback end +function socket.onclose(id, callback) + local obj = socket_pool[id] + assert(obj) + obj.on_close = callback +end + return socket diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 147fe81ba..a7d2393d0 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -128,6 +128,16 @@ local function pop_response(self) end end +-- on close callback +local function autoclose_cb(self, fd) + local sock = self.__sock + if self.__wait_response and sock and sock[1] == fd then + -- closed by peer + skynet.error("socket closed by peer : ", self.__host, self.__port) + close_channel_socket(self) + end +end + local function push_response(self, response, co) if self.__response then -- response is session @@ -170,7 +180,15 @@ local function dispatch_by_order(self) wakeup_all(self, "channel_closed") break end - local ok, result_ok, result_data = pcall(get_response, func, self.__sock) + local sock = self.__sock + if not sock then + -- closed by peer + self.__result[co] = socket_error + skynet.wakeup(co) + wakeup_all(self) + break + end + local ok, result_ok, result_data = pcall(get_response, func, sock) if ok then self.__result[co] = result_ok if result_ok and self.__result_data[co] then @@ -197,6 +215,9 @@ local function dispatch_function(self) if self.__response then return dispatch_by_session else + socket.onclose(self.__sock[1], function(fd) + autoclose_cb(self, fd) + end) return dispatch_by_order end end diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 694e797f9..fe08339a7 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1408,74 +1408,17 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { return -1; } -struct stream_buffer { - char * buf; - int sz; -}; - -static char * -reserve_buffer(struct stream_buffer *buffer, int sz) { - if (buffer->buf == NULL) { - buffer->buf = (char *)MALLOC(sz); - return buffer->buf; - } else { - char * newbuffer = (char *)MALLOC(sz + buffer->sz); - memcpy(newbuffer, buffer->buf, buffer->sz); - char * ret = newbuffer + buffer->sz; - FREE(buffer->buf); - buffer->buf = newbuffer; - return ret; - } -} - -static int -read_socket(struct socket *s, struct stream_buffer *buffer) { - int sz = s->p.size; - buffer->buf = NULL; - buffer->sz = 0; - int rsz = sz; - for (;;) { - char *buf = reserve_buffer(buffer, rsz); - int n = (int)read(s->fd, buf, rsz); - if (n <= 0) { - if (buffer->sz == 0) { - // read nothing - FREE(buffer->buf); - return n; - } else { - // ignore the error or hang up, returns buffer - // If socket is hang up, SOCKET_CLOSE will be send later. - // (buffer->sz should be 0 next time) - break; - } - } - buffer->sz += n; - if (n < rsz) { - break; - } - // n == rsz, read again ( and read more ) - rsz *= 2; - } - int r = buffer->sz; - if (r > sz) { - s->p.size = sz * 2; - } else if (sz > MIN_READ_BUFFER && r*2 < sz) { - s->p.size = sz / 2; - } - return r; -} - // return -1 (ignore) when error static int forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message * result) { - struct stream_buffer buf; - int n = read_socket(s, &buf); + int sz = s->p.size; + char * buffer = MALLOC(sz); + int n = (int)read(s->fd, buffer, sz); if (n<0) { + FREE(buffer); switch(errno) { case EINTR: - break; case AGAIN_WOULDBLOCK: - skynet_error(NULL, "socket-server: EAGAIN capture."); break; default: // close when error @@ -1486,6 +1429,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo return -1; } if (n==0) { + FREE(buffer); if (s->closing) { // Rare case : if s->closing is true, reading event is disable, and SOCKET_CLOSE is raised. if (nomore_sending_data(s)) { @@ -1509,7 +1453,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo if (halfclose_read(s)) { // discard recv data (Rare case : if socket is HALFCLOSE_READ, reading event is disable.) - FREE(buf.buf); + FREE(buffer); return -1; } @@ -1518,7 +1462,15 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo result->opaque = s->opaque; result->id = s->id; result->ud = n; - result->data = buf.buf; + result->data = buffer; + + if (n == sz) { + s->p.size *= 2; + return SOCKET_MORE; + } else if (sz > MIN_READ_BUFFER && n*2 < sz) { + s->p.size /= 2; + } + return SOCKET_DATA; } @@ -1757,6 +1709,10 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int int type; if (s->protocol == PROTOCOL_TCP) { type = forward_message_tcp(ss, s, &l, result); + if (type == SOCKET_MORE) { + --ss->event_index; + return SOCKET_DATA; + } } else { type = forward_message_udp(ss, s, &l, result); if (type == SOCKET_UDP) { diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index bf79caf48..beb628ca9 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -13,7 +13,10 @@ #define SOCKET_EXIT 5 #define SOCKET_UDP 6 #define SOCKET_WARNING 7 -#define SOCKET_RST 8 // Only for internal use + +// Only for internal use +#define SOCKET_RST 8 +#define SOCKET_MORE 9 struct socket_server; From 22b5fbf3b8ca843d99566eb2d6be3fd5556d7449 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Mar 2021 14:23:40 +0800 Subject: [PATCH 346/565] fix #1369 --- 3rd/lua/lauxlib.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 17bdb84b5..f96f304e7 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1135,7 +1135,7 @@ luaL_initcodecache(void) { } static const void * -load(const char *key) { +load_proto(const char *key) { if (CC.L == NULL) return NULL; SPIN_LOCK(&CC) @@ -1150,7 +1150,7 @@ load(const char *key) { } static const void * -save(const char *key, const void * proto) { +save_proto(const char *key, const void * proto) { lua_State *L; const void * result = NULL; @@ -1223,7 +1223,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, if (level == CACHE_OFF) { return luaL_loadfilex_(L, filename, mode); } - const void * proto = load(filename); + const void * proto = load_proto(filename); if (proto) { lua_clonefunction(L, proto); return LUA_OK; @@ -1246,7 +1246,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } lua_sharefunction(eL, -1); proto = lua_topointer(eL, -1); - const void * oldv = save(filename, proto); + const void * oldv = save_proto(filename, proto); if (oldv) { lua_close(eL); lua_clonefunction(L, oldv); From 11165ce72725637f754805d94783e8d7d7714f33 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Mar 2021 14:27:37 +0800 Subject: [PATCH 347/565] fix #1368 --- service/datacenterd.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index 8a7b65a0f..0c355186f 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -15,7 +15,7 @@ end function command.QUERY(key, ...) local d = database[key] - if d then + if d ~= nil then return query(d, ...) end end @@ -59,7 +59,7 @@ end function command.UPDATE(...) local ret, value = update(database, ...) - if ret or value == nil then + if ret ~= nil or value == nil then return ret end local q = wakeup(wait_queue, ...) @@ -97,7 +97,7 @@ skynet.start(function() skynet.dispatch("lua", function (_, _, cmd, ...) if cmd == "WAIT" then local ret = command.QUERY(...) - if ret then + if ret ~= nil then skynet.ret(skynet.pack(ret)) else waitfor(wait_queue, ...) From e0d4fda24e6afa4ffbb0873708a5f31c9904ce6e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 30 Mar 2021 10:09:43 +0800 Subject: [PATCH 348/565] Update lua 5.4.3 --- 3rd/lua/README | 2 +- 3rd/lua/lapi.c | 15 ++++++------ 3rd/lua/lauxlib.h | 9 +++---- 3rd/lua/ldebug.c | 5 +++- 3rd/lua/lfunc.c | 40 ++++++++++++++++++++++--------- 3rd/lua/lobject.h | 5 ++-- 3rd/lua/lstring.c | 2 +- 3rd/lua/ltable.c | 61 +++++++++++++++++++++++++++++++---------------- 3rd/lua/luaconf.h | 24 ++++++++++++------- 9 files changed, 105 insertions(+), 58 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index 059ade888..6a0ac4546 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,4 +1,4 @@ -This is a modify version of lua 5.4.2 . +This is a modify version of lua 5.4.3 . For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index b27374cfd..8ebf16f09 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -173,7 +173,7 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { CallInfo *ci; - StkId func; + StkId func, newtop; ptrdiff_t diff; /* difference for new top */ lua_lock(L); ci = L->ci; @@ -188,12 +188,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } -#if defined(LUA_COMPAT_5_4_0) - if (diff < 0 && hastocloseCfunc(ci->nresults)) - luaF_close(L, L->top + diff, CLOSEKTOP, 0); -#endif - api_check(L, L->tbclist < L->top + diff, "cannot pop an unclosed slot"); - L->top += diff; + api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot"); + newtop = L->top + diff; + if (diff < 0 && L->tbclist >= newtop) { + lua_assert(hastocloseCfunc(ci->nresults)); + luaF_close(L, newtop, CLOSEKTOP, 0); + } + L->top = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); } diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 5d5a234e6..7fe4d4e1b 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -12,6 +12,7 @@ #include #include +#include "luaconf.h" #include "lua.h" @@ -124,10 +125,6 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, ** =============================================================== */ -#if !defined(l_likely) -#define l_likely(x) (x) -#endif - #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) @@ -136,10 +133,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)(l_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) + ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ - ((void)(l_likely(cond) || luaL_typeerror(L, (arg), (tname)))) + ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 8e3657a92..1feaab229 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -50,6 +50,8 @@ static int currentpc (CallInfo *ci) { ** an integer division gets the right place. When the source file has ** large sequences of empty/comment lines, it may need extra entries, ** so the original estimate needs a correction. +** If the original estimate is -1, the initial 'if' ensures that the +** 'while' will run at least once. ** The assertion that the estimate is a lower bound for the correct base ** is valid as long as the debug info has been generated with the same ** value for MAXIWTHABS or smaller. (Previous releases use a little @@ -63,7 +65,8 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { else { int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ /* estimate must be a lower bond of the correct base */ - lua_assert(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc); + lua_assert(i < 0 || + (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 9a4413da8..88f6c7e42 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -155,6 +155,15 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { } +/* +** Maximum value for deltas in 'tbclist', dependent on the type +** of delta. (This macro assumes that an 'L' is in scope where it +** is used.) +*/ +#define MAXDELTA \ + ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) + + /* ** Insert a variable in the list of to-be-closed variables. */ @@ -163,13 +172,11 @@ void luaF_newtbcupval (lua_State *L, StkId level) { if (l_isfalse(s2v(level))) return; /* false doesn't need to be closed */ checkclosemth(L, level); /* value must have a close method */ - while (level - L->tbclist > USHRT_MAX) { /* is delta too large? */ - L->tbclist += USHRT_MAX; /* create a dummy node at maximum delta */ - L->tbclist->tbclist.delta = USHRT_MAX; - L->tbclist->tbclist.isdummy = 1; + while (cast_uint(level - L->tbclist) > MAXDELTA) { + L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ + L->tbclist->tbclist.delta = 0; } - level->tbclist.delta = level - L->tbclist; - level->tbclist.isdummy = 0; + level->tbclist.delta = cast(unsigned short, level - L->tbclist); L->tbclist = level; } @@ -202,6 +209,19 @@ void luaF_closeupval (lua_State *L, StkId level) { } +/* +** Remove firt element from the tbclist plus its dummy nodes. +*/ +static void poptbclist (lua_State *L) { + StkId tbc = L->tbclist; + lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ + tbc -= tbc->tbclist.delta; + while (tbc > L->stack && tbc->tbclist.delta == 0) + tbc -= MAXDELTA; /* remove dummy nodes */ + L->tbclist = tbc; +} + + /* ** Close all upvalues and to-be-closed variables up to the given stack ** level. @@ -211,11 +231,9 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist >= level) { /* traverse tbc's down to that level */ StkId tbc = L->tbclist; /* get variable index */ - L->tbclist -= tbc->tbclist.delta; /* remove it from list */ - if (!tbc->tbclist.isdummy) { /* not a dummy entry? */ - prepcallclosemth(L, tbc, status, yy); /* close variable */ - level = restorestack(L, levelrel); - } + poptbclist(L); /* remove it from list */ + prepcallclosemth(L, tbc, status, yy); /* close variable */ + level = restorestack(L, levelrel); } } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 73e5adabd..efb503cc5 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -139,13 +139,14 @@ typedef struct TValue { ** Entries in a Lua stack. Field 'tbclist' forms a list of all ** to-be-closed variables active in this stack. Dummy entries are ** used when the distance between two tbc variables does not fit -** in an unsigned short. +** in an unsigned short. They are represented by delta==0, and +** their real delta is always the maximum value that fits in +** that field. */ typedef union StackValue { TValue val; struct { TValuefields; - lu_byte isdummy; unsigned short delta; } tbclist; } StackValue; diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 2f2214bde..d21b44f47 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -23,7 +23,7 @@ #include "atomic.h" static unsigned int STRSEED; -static ATOM_SIZET STRID = 0; +static size_t STRID = 0; /* ** Maximum size for string table. diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index dc838f0bb..50e5a721b 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -68,20 +68,25 @@ #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) +/* +** When the original hash value is good, hashing by a power of 2 +** avoids the cost of '%'. +*/ #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->hash) -#define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - - /* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. +** for other types, it is better to avoid modulo by power of 2, as +** they can have many 2 factors. */ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) +#define hashstr(t,str) hashpow2(t, (str)->hash) +#define hashboolean(t,p) hashpow2(t, p) + +#define hashint(t,i) hashpow2(t, i) + + #define hashpointer(t,p) hashmod(t, point2uint(p)) @@ -135,24 +140,38 @@ static int l_hashfloat (lua_Number n) { */ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { switch (withvariant(ktt)) { - case LUA_VNUMINT: - return hashint(t, ivalueraw(*kvl)); - case LUA_VNUMFLT: - return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); - case LUA_VSHRSTR: - return hashstr(t, tsvalueraw(*kvl)); - case LUA_VLNGSTR: - return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); + case LUA_VNUMINT: { + lua_Integer key = ivalueraw(*kvl); + return hashint(t, key); + } + case LUA_VNUMFLT: { + lua_Number n = fltvalueraw(*kvl); + return hashmod(t, l_hashfloat(n)); + } + case LUA_VSHRSTR: { + TString *ts = tsvalueraw(*kvl); + return hashstr(t, ts); + } + case LUA_VLNGSTR: { + TString *ts = tsvalueraw(*kvl); + return hashpow2(t, luaS_hashlongstr(ts)); + } case LUA_VFALSE: return hashboolean(t, 0); case LUA_VTRUE: return hashboolean(t, 1); - case LUA_VLIGHTUSERDATA: - return hashpointer(t, pvalueraw(*kvl)); - case LUA_VLCF: - return hashpointer(t, fvalueraw(*kvl)); - default: - return hashpointer(t, gcvalueraw(*kvl)); + case LUA_VLIGHTUSERDATA: { + void *p = pvalueraw(*kvl); + return hashpointer(t, p); + } + case LUA_VLCF: { + lua_CFunction f = fvalueraw(*kvl); + return hashpointer(t, f); + } + default: { + GCObject *o = gcvalueraw(*kvl); + return hashpointer(t, o); + } } } diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index ae73e2fd6..e64d2ee39 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -663,22 +663,30 @@ /* ** macros to improve jump prediction, used mostly for error handling -** and debug facilities. +** and debug facilities. (Some macros in the Lua API use these macros. +** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your +** code.) */ -#if (defined(LUA_CORE) || defined(LUA_LIB)) && !defined(l_likely) +#if !defined(luai_likely) -#include -#if defined(__GNUC__) -#define l_likely(x) (__builtin_expect(((x) != 0), 1)) -#define l_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#if defined(__GNUC__) && !defined(LUA_NOBUILTIN) +#define luai_likely(x) (__builtin_expect(((x) != 0), 1)) +#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) #else -#define l_likely(x) (x) -#define l_unlikely(x) (x) +#define luai_likely(x) (x) +#define luai_unlikely(x) (x) #endif #endif +#if defined(LUA_CORE) || defined(LUA_LIB) +/* shorter names for Lua's own use */ +#define l_likely(x) luai_likely(x) +#define l_unlikely(x) luai_unlikely(x) +#endif + + /* }================================================================== */ From 811ef52b3dd68cc0cc559f47fe1f8c0361b6d7d4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 30 Mar 2021 14:55:02 +0800 Subject: [PATCH 349/565] fix build --- 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 d21b44f47..2f2214bde 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -23,7 +23,7 @@ #include "atomic.h" static unsigned int STRSEED; -static size_t STRID = 0; +static ATOM_SIZET STRID = 0; /* ** Maximum size for string table. From 96c702bb3c35bf53ca408eaab9e81d28282d6faf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Apr 2021 10:33:23 +0800 Subject: [PATCH 350/565] bugfix --- lualib/skynet.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index db79e8aa9..9891bf2cb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -450,7 +450,6 @@ function skynet.killthread(thread) if co == nil then return end - watching_session[session] = nil local addr = session_coroutine_address[co] if addr then session_coroutine_address[co] = nil From ba26c8a432ac8ed9335dd74158694848dd1fd771 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Apr 2021 10:46:41 +0800 Subject: [PATCH 351/565] Do not close on error, see #1373 --- skynet-src/socket_server.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index fe08339a7..47d8b8b07 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1421,8 +1421,6 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo case AGAIN_WOULDBLOCK: break; default: - // close when error - force_close(ss, s, l, result); result->data = strerror(errno); return SOCKET_ERR; } @@ -1737,7 +1735,6 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int 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); @@ -1749,7 +1746,6 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int } else { err = "Unknown error"; } - force_close(ss, s, &l, result); result->data = (char *)err; return SOCKET_ERR; } From 8ad6622936ddd234b43368ae602eb49012613a94 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Apr 2021 15:47:32 +0800 Subject: [PATCH 352/565] halfclose_read should call before force_close --- skynet-src/socket_server.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 47d8b8b07..90222313f 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1749,16 +1749,12 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int result->data = (char *)err; return SOCKET_ERR; } - if(e->eof) { + if (e->eof) { // For epoll (at least), FIN packets are exchanged both ways. // See: https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated + int r = halfclose_read(s) ? -1 : SOCKET_CLOSE; force_close(ss, s, &l, result); - if (halfclose_read(s)) { - // Already rasied SOCKET_CLOSE - return -1; - } else { - return SOCKET_CLOSE; - } + return r; } break; } From 85dde521073f02fb462d81de85e47e8fd8336f7e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Apr 2021 16:45:51 +0800 Subject: [PATCH 353/565] minor bugfix : socket_server_poll never returns -1 --- skynet-src/socket_server.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 90222313f..5a3b201ab 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1672,10 +1672,11 @@ 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; + int err = errno; + if (err != EINTR) { + skynet_error(NULL, "socket-server: %s", strerror(err)); } - return -1; + continue; } } struct event *e = &ss->ev[ss->event_index++]; @@ -1752,9 +1753,11 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int if (e->eof) { // For epoll (at least), FIN packets are exchanged both ways. // See: https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated - int r = halfclose_read(s) ? -1 : SOCKET_CLOSE; + int halfclose = halfclose_read(s); force_close(ss, s, &l, result); - return r; + if (!halfclose) { + return SOCKET_CLOSE; + } } break; } From ceb278adf666b2d3259408245f1c3a96031b17d7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Apr 2021 16:46:14 +0800 Subject: [PATCH 354/565] Add shutdown to client_socket for testing --- lualib-src/lua-clientsocket.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 251890324..532abed22 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -185,6 +185,40 @@ lreadstdin(lua_State *L) { return 1; } +static int +lshutdown(lua_State *L) { + int fd = luaL_checkinteger(L,1); + const char *mode = luaL_checkstring(L,2); + int v = 0; + int i; + int read = 1; + int write = 2; + for (i=0;mode[i];i++) { + switch(mode[i]) { + case 'r': + v |= read; + break; + case 'w': + v |= write; + break; + default: + return luaL_error(L, "Invalid mode %c", mode[i]); + } + } + if (v == 0) { + return luaL_error(L, "mode should be r or/and w"); + } + if (v == read) + v = SHUT_RD; + else if (v == write) + v = SHUT_WR; + else + v = SHUT_RDWR; + printf("SHUTDOWN %d %d\n", fd, v); + shutdown(fd, v); + return 0; +} + LUAMOD_API int luaopen_client_socket(lua_State *L) { luaL_checkversion(L); @@ -192,6 +226,7 @@ luaopen_client_socket(lua_State *L) { { "connect", lconnect }, { "recv", lrecv }, { "send", lsend }, + { "shutdown", lshutdown }, { "close", lclose }, { "usleep", lusleep }, { NULL, NULL }, From 0f5867534cc3be966f71fbdd327abf2498e441aa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 02:54:09 +0000 Subject: [PATCH 355/565] remove unused socket.lock/socket.unlock --- lualib/skynet/socket.lua | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 8371853ad..e99d3ca5b 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -287,7 +287,6 @@ function socket.close(id) end s.connected = false end - assert(s.lock == nil or next(s.lock) == nil) socket_pool[id] = nil end @@ -403,34 +402,6 @@ function socket.listen(host, port, backlog) return driver.listen(host, port, backlog) end -function socket.lock(id) - local s = socket_pool[id] - assert(s) - local lock_set = s.lock - if not lock_set then - lock_set = {} - s.lock = lock_set - end - if #lock_set == 0 then - lock_set[1] = true - else - local co = coroutine.running() - table.insert(lock_set, co) - skynet.wait(co) - end -end - -function socket.unlock(id) - local s = socket_pool[id] - assert(s) - local lock_set = assert(s.lock) - table.remove(lock_set,1) - local co = lock_set[1] - if co then - skynet.wakeup(co) - end -end - -- abandon use to forward socket id to other service -- you must call socket.start(id) later in other service function socket.abandon(id) From f9dc6a375688eb3c70c5368a0160e1ac98353ea7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 03:25:55 +0000 Subject: [PATCH 356/565] fix SOCKET_CLOSE event after socket.close() See #1373 --- lualib/skynet/socket.lua | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e99d3ca5b..b547eb344 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -16,6 +16,7 @@ local socket_pool = setmetatable( -- store all socket object } ) +local socket_onclose = {} local socket_message = {} local function wakeup(s) @@ -117,14 +118,16 @@ end -- SKYNET_SOCKET_TYPE_CLOSE = 3 socket_message[3] = function(id) local s = socket_pool[id] - if s == nil then - driver.close(id) - return + if s then + s.connected = false + wakeup(s) + else + driver.shutdown(id) end - s.connected = false - wakeup(s) - if s.on_close then - s.on_close(id) + local cb = socket_onclose[id] + if cb then + cb(id) + socket_onclose[id] = nil end end @@ -216,6 +219,7 @@ local function connect(id, func) callback = func, protocol = "TCP", } + assert(not socket_onclose[id], "socket has onclose callback") assert(not socket_pool[id], "socket is not closed") socket_pool[id] = s suspend(s) @@ -409,6 +413,7 @@ function socket.abandon(id) if s then s.connected = false wakeup(s) + socket_onclose[id] = nil socket_pool[id] = nil end end @@ -460,9 +465,7 @@ function socket.warning(id, callback) end function socket.onclose(id, callback) - local obj = socket_pool[id] - assert(obj) - obj.on_close = callback + socket_onclose[id] = callback end return socket From e69f8768676bd570e953429fe1fcd9c9e261a06d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 03:39:22 +0000 Subject: [PATCH 357/565] raise error on start, fix #1374 --- skynet-src/socket_server.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 5a3b201ab..1b6ed1686 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1188,8 +1188,11 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "invalid socket"; return SOCKET_ERR; } - if (halfclose_read(s)) - return -1; + if (halfclose_read(s)) { + // The closing socket may be in transit, so raise an error. See https://github.com/cloudwu/skynet/issues/1374 + result->data = "socket closed"; + return SOCKET_ERR; + } struct socket_lock l; socket_lock_init(s, &l); if (enable_read(ss, s, true)) { @@ -1208,7 +1211,7 @@ resume_socket(struct socket_server *ss, struct request_resumepause *request, str result->data = "transfer"; return SOCKET_OPEN; } - // if s->type == SOCKET_TYPE_HALFCLOSE_* , SOCKET_CLOSE message will send later + // if s->type == SOCKET_TYPE_HALFCLOSE_WRITE , SOCKET_CLOSE message will send later return -1; } From bc71164a997ea5f17ffbf573da8db44fc40c1978 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 03:47:19 +0000 Subject: [PATCH 358/565] close may be better than shutdown --- lualib/skynet/socket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index b547eb344..ae9b3724e 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -122,7 +122,7 @@ socket_message[3] = function(id) s.connected = false wakeup(s) else - driver.shutdown(id) + driver.close(id) end local cb = socket_onclose[id] if cb then From df2803469e854a75a7dd28ce1d2c5b5e1c768602 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 07:26:02 +0000 Subject: [PATCH 359/565] Add report_error(), this may fix issue #1376 --- skynet-src/socket_server.c | 52 +++++++++++++------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 1b6ed1686..80baebae2 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -677,6 +677,15 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return SOCKET_ERR; } +static int +report_error(struct socket *s, struct socket_message *result, const char *err) { + result->id = s->id; + result->ud = 0; + result->opaque = s->opaque; + result->data = (char *)err; + return SOCKET_ERR; +} + static int close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { if (s->closing) { @@ -696,11 +705,7 @@ close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, s ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); shutdown(s->fd, SHUT_WR); enable_write(ss, s, false); - result->id = s->id; - result->ud = 0; - result->opaque = s->opaque; - result->data = strerror(errno); - return SOCKET_ERR; + return report_error(s, result, strerror(errno)); } } @@ -894,11 +899,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, int err = enable_write(ss, s, false); if (err) { - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = "disable write failed"; - return SOCKET_ERR; + return report_error(s, result, "disable write failed"); } if(s->warn_size > 0){ @@ -989,11 +990,7 @@ trigger_write(struct socket_server *ss, struct request_send * request, struct so if (socket_invalid(s, id)) return -1; if (enable_write(ss, s, true)) { - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = "enable write failed"; - return SOCKET_ERR; + return report_error(s, result, "enable write failed"); } return -1; } @@ -1050,11 +1047,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock } } if (enable_write(ss, s, true)) { - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = "enable write failed"; - return SOCKET_ERR; + return report_error(s, result, "enable write failed"); } } else { if (s->protocol == PROTOCOL_TCP) { @@ -1223,11 +1216,7 @@ pause_socket(struct socket_server *ss, struct request_resumepause *request, stru return -1; } if (enable_read(ss, s, false)) { - result->id = id; - result->opaque = request->opaque; - result->ud = 0; - result->data = "enable read failed"; - return SOCKET_ERR; + return report_error(s, result, "enable read failed"); } return -1; } @@ -1302,12 +1291,7 @@ set_udp_address(struct socket_server *ss, struct request_setudp *request, struct int type = request->address[0]; if (type != s->protocol) { // protocol mismatch - result->opaque = s->opaque; - result->id = s->id; - result->ud = 0; - result->data = "protocol mismatch"; - - return SOCKET_ERR; + return report_error(s, result, "protocol mismatch"); } if (type == PROTOCOL_UDP) { memcpy(s->p.udp_address, request->address, 1+2+4); // 1 type, 2 port, 4 ipv4 @@ -1424,8 +1408,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lo case AGAIN_WOULDBLOCK: break; default: - result->data = strerror(errno); - return SOCKET_ERR; + return report_error(s, result, strerror(errno)); } return -1; } @@ -1750,8 +1733,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int } else { err = "Unknown error"; } - result->data = (char *)err; - return SOCKET_ERR; + return report_error(s, result, err); } if (e->eof) { // For epoll (at least), FIN packets are exchanged both ways. From 2b71c2755accdbf86ad5ff67235a27e6ca63e8ea Mon Sep 17 00:00:00 2001 From: busymage <45327828+busymage@users.noreply.github.com> Date: Wed, 7 Apr 2021 19:08:29 +0800 Subject: [PATCH 360/565] Fix boostrap only support 1 param. (#1377) --- skynet-src/skynet_start.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index b29910341..01eea2cc7 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -235,7 +235,17 @@ bootstrap(struct skynet_context * logger, const char * cmdline) { int sz = strlen(cmdline); char name[sz+1]; char args[sz+1]; - sscanf(cmdline, "%s %s", name, args); + int arg_pos; + sscanf(cmdline, "%s", name); + arg_pos = strlen(name); + if (arg_pos < sz) { + while(cmdline[arg_pos] == ' ') { + arg_pos++; + } + strncpy(args, cmdline + arg_pos, sz); + } else { + args[0] = '\0'; + } struct skynet_context *ctx = skynet_context_new(name, args); if (ctx == NULL) { skynet_error(NULL, "Bootstrap error : %s\n", cmdline); From 52d65c5dcb5df01cbcddba31d05b110c6e18b285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Sat, 10 Apr 2021 07:02:38 +0800 Subject: [PATCH 361/565] Redis ACL (#1381) * Support redis ACL See #1380 --- lualib/skynet/db/redis.lua | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/db/redis.lua b/lualib/skynet/db/redis.lua index 8fe0e6012..8aad08560 100644 --- a/lualib/skynet/db/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -146,10 +146,17 @@ local function compose_message(cmd, msg) return lines end -local function redis_login(auth, db) +local function redis_login(conf) + local auth = conf.auth + local db = conf.db if auth == nil and db == nil then return end + if auth then + if conf.username then + auth = { conf.username, auth } + end + end return function(so) if auth then so:request(compose_message("AUTH", auth), read_response) @@ -164,7 +171,7 @@ 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), + auth = redis_login(db_conf), nodelay = true, overload = db_conf.overload, } @@ -256,10 +263,11 @@ local watchmeta = { end, } -local function watch_login(obj, auth) +local function watch_login(conf, obj) + local login_auth = redis_login(conf) return function(so) - if auth then - so:request(compose_message("AUTH", auth), read_response) + if login_auth then + login_auth(so) end for k in pairs(obj.__psubscribe) do so:request(compose_message ("PSUBSCRIBE", k)) @@ -278,7 +286,7 @@ function redis.watch(db_conf) local channel = socketchannel.channel { host = db_conf.host, port = db_conf.port or 6379, - auth = watch_login(obj, db_conf.auth), + auth = watch_login(db_conf, obj), nodelay = true, } obj.__sock = channel From f0d24ac718e0cfc2e47c9a491bce63800fe11a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 15 Apr 2021 10:52:57 +0800 Subject: [PATCH 362/565] Bugfix #1385 (#1387) --- lualib/skynet/socketchannel.lua | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index a7d2393d0..6849dc6f5 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -125,6 +125,17 @@ local function pop_response(self) end self.__wait_response = coroutine.running() skynet.wait(self.__wait_response) + if not self.__sock then + -- disconnected before request, terminate dispatch_by_order + return + end + end +end + +local function wakeup_response(self) + if self.__wait_response then + skynet.wakeup(self.__wait_response) + self.__wait_response = nil end end @@ -135,6 +146,7 @@ local function autoclose_cb(self, fd) -- closed by peer skynet.error("socket closed by peer : ", self.__host, self.__port) close_channel_socket(self) + wakeup_response(self) end end @@ -146,10 +158,7 @@ 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 - end + wakeup_response(self) end end @@ -423,12 +432,12 @@ local function block_connect(self, once) else self.__connecting[1] = true err = 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 + self.__connecting[1] = nil end r = check_connection(self) From 1020c403dce2c18cc2ab9dae143a2b708c220e25 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Apr 2021 08:33:48 +0000 Subject: [PATCH 363/565] revert wakeup_response --- lualib/skynet/socketchannel.lua | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 6849dc6f5..521d98325 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -125,17 +125,6 @@ local function pop_response(self) end self.__wait_response = coroutine.running() skynet.wait(self.__wait_response) - if not self.__sock then - -- disconnected before request, terminate dispatch_by_order - return - end - end -end - -local function wakeup_response(self) - if self.__wait_response then - skynet.wakeup(self.__wait_response) - self.__wait_response = nil end end @@ -146,7 +135,6 @@ local function autoclose_cb(self, fd) -- closed by peer skynet.error("socket closed by peer : ", self.__host, self.__port) close_channel_socket(self) - wakeup_response(self) end end @@ -158,7 +146,10 @@ 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) - wakeup_response(self) + if self.__wait_response then + skynet.wakeup(self.__wait_response) + self.__wait_response = nil + end end end From f61a27ac6bf11778df706f8070b5df68f27376cb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 19 Apr 2021 18:45:56 +0800 Subject: [PATCH 364/565] fix #1388 --- lualib-src/lua-bson.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index f29048529..9d20242ae 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1086,6 +1086,7 @@ lbinary(lua_State *L) { luaL_addchar(&b, 0); luaL_addchar(&b, BSON_BINARY); luaL_addchar(&b, 0); // sub type + lua_pushvalue(L,1); luaL_addvalue(&b); luaL_pushresult(&b); From 1e6781bd418a93321953e1c0af5369174a6ee2cb Mon Sep 17 00:00:00 2001 From: t0350 Date: Thu, 29 Apr 2021 14:40:37 +0800 Subject: [PATCH 365/565] add coroutine close for skynetco (#1392) Co-authored-by: jietao.cjt --- lualib/skynet/coroutine.lua | 11 ++++++++++- test/testcoroutine.lua | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index 54ac499c6..6fe92f27a 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -6,6 +6,7 @@ local coroutine_resume = coroutine.resume local coroutine_yield = coroutine.yield local coroutine_status = coroutine.status local coroutine_running = coroutine.running +local coroutine_close = coroutine.close local select = select local skynetco = {} @@ -15,6 +16,9 @@ skynetco.running = coroutine.running skynetco.status = coroutine.status local skynet_coroutines = setmetatable({}, { __mode = "kv" }) +-- true : skynet coroutine +-- false : skynet suspend +-- nil : exit function skynetco.create(f) local co = coroutine.create(f) @@ -84,7 +88,7 @@ do -- begin skynetco.resume end -- end of skynetco.resume function skynetco.status(co) - local status = coroutine.status(co) + local status = coroutine_status(co) if status == "suspended" then if skynet_coroutines[co] == false then return "blocked" @@ -121,4 +125,9 @@ end end -- end of skynetco.wrap +function skynetco.close(co) + skynet_coroutines[co] = nil + return coroutine_close(co) +end + return skynetco diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua index 910bcc0f0..3af017f10 100644 --- a/test/testcoroutine.lua +++ b/test/testcoroutine.lua @@ -50,4 +50,5 @@ skynet.start(function() print("main step", f()) print("main step", f()) -- print("main thread time:", profile.stop()) + print("close", coroutine.close(coroutine.create(main))) end) From 28d47d96b9e70a172f6bdc534a73e1704497ee19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E7=A5=A5=E9=BE=99?= <648708566@qq.com> Date: Fri, 14 May 2021 21:07:27 +0800 Subject: [PATCH 366/565] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E5=89=8D=E4=B8=80?= =?UTF-8?q?=E4=B8=AAssl=E9=94=99=E8=AF=AF=E6=B2=A1=E6=B8=85=EF=BC=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=90=8E=E9=9D=A2=E7=9A=84=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?ssl=E8=B0=83=E7=94=A8=E6=93=8D=E4=BD=9C=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=88=B0=E8=BF=99=E4=B8=AA=E9=94=99=E8=AF=AF=20(#1401)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/ltls.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 0e8e41b38..81bd397b8 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -182,6 +182,7 @@ _ltls_context_handshake(lua_State* L) { return 0; } else if (ret < 0) { int err = SSL_get_error(tls_p->ssl, ret); + ERR_clear_error(); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { int all_read = _bio_read(L, tls_p); if(all_read>0) { @@ -192,6 +193,7 @@ _ltls_context_handshake(lua_State* L) { } } else { int err = SSL_get_error(tls_p->ssl, ret); + ERR_clear_error(); luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret); } } @@ -219,6 +221,7 @@ _ltls_context_read(lua_State* L) { read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); if(read <= 0) { int err = SSL_get_error(tls_p->ssl, read); + ERR_clear_error(); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { break; } @@ -244,6 +247,7 @@ _ltls_context_write(lua_State* L) { int written = SSL_write(tls_p->ssl, unencrypted_data, slen); if(written <= 0) { int err = SSL_get_error(tls_p->ssl, written); + ERR_clear_error(); luaL_error(L, "SSL_write error:%d", err); }else if(written <= slen) { unencrypted_data += written; From 6ad8da0993d4c87ece5b89d804f8a20340c84df3 Mon Sep 17 00:00:00 2001 From: t0350 Date: Sun, 16 May 2021 22:26:39 +0800 Subject: [PATCH 367/565] when skynet.init require the loading module caused circular dependency (#1403) --- lualib/skynet/require.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/require.lua b/lualib/skynet/require.lua index b902af133..f814c3fc3 100644 --- a/lualib/skynet/require.lua +++ b/lualib/skynet/require.lua @@ -43,6 +43,7 @@ do local loading_queue = loading[name] if loading_queue then + assert(loading_queue.co ~= co, "circular dependency") -- Module is in the init process (require the same mod at the same time in different coroutines) , waiting. local skynet = require "skynet" loading_queue[#loading_queue+1] = co @@ -54,7 +55,7 @@ do return m end - loading_queue = {} + loading_queue = {co = co} loading[name] = loading_queue local old_init_list = context[co] From 71317d85025dcbb348d262a42ae60f4b086d0943 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 May 2021 11:16:24 +0800 Subject: [PATCH 368/565] Add socket.resolve, see #1398 --- lualib-src/lua-socket.c | 31 +++++++++++++++++++++++++++++++ lualib/skynet/socket.lua | 1 + 2 files changed, 32 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 0f7353001..1754eb446 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -11,8 +11,10 @@ #include #include +#include #include #include +#include #include "skynet.h" #include "skynet_socket.h" @@ -786,6 +788,34 @@ linfo(lua_State *L) { return 1; } +static int +lresolve(lua_State *L) { + const char * host = luaL_checkstring(L, 1); + int status; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + struct addrinfo *ai_ptr = NULL; + memset( &ai_hints, 0, sizeof( ai_hints ) ); + status = getaddrinfo( host, NULL, &ai_hints, &ai_list); + if ( status != 0 ) { + return luaL_error(L, gai_strerror(status)); + } + lua_newtable(L); + int idx = 1; + char tmp[128]; + for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { + struct sockaddr * addr = ai_ptr->ai_addr; + void * sin_addr = (ai_ptr->ai_family == AF_INET) ? (void*)&((struct sockaddr_in *)addr)->sin_addr : (void*)&((struct sockaddr_in6 *)addr)->sin6_addr; + if (inet_ntop(ai_ptr->ai_family, sin_addr, tmp, sizeof(tmp))) { + lua_pushstring(L, tmp); + lua_rawseti(L, -2, idx++); + } + } + + freeaddrinfo(ai_list); + return 1; +} + LUAMOD_API int luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); @@ -820,6 +850,7 @@ luaopen_skynet_socketdriver(lua_State *L) { { "udp_connect", ludp_connect }, { "udp_send", ludp_send }, { "udp_address", ludp_address }, + { "resolve", lresolve }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index ae9b3724e..2ba2a32e5 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -457,6 +457,7 @@ end socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) socket.netstat = assert(driver.info) +socket.resolve = assert(driver.resolve) function socket.warning(id, callback) local obj = socket_pool[id] From ef4af820d50031d0b17771ffb2fe90d34e9c497c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 21 May 2021 10:15:21 +0800 Subject: [PATCH 369/565] Do not resume the fd after closing, see #1408 --- lualib/skynet/socket.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 2ba2a32e5..2c44d0527 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -28,7 +28,7 @@ local function wakeup(s) end local function pause_socket(s, size) - if s.pause then + if s.pause ~= nil then return end if size then @@ -254,7 +254,7 @@ end function socket.pause(id) local s = socket_pool[id] - if s == nil or s.pause then + if s == nil then return end pause_socket(s) @@ -280,6 +280,7 @@ function socket.close(id) end driver.close(id) if s.connected then + s.pause = false -- Do not resume this fd if it paused. if s.co then -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. From f5eee7dcbb703b2174321838a690177b01eaf577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=9C=E6=AC=A2=E5=85=B0=E8=8A=B1=E5=B1=B1=E4=B8=98?= Date: Sun, 30 May 2021 06:52:02 +0800 Subject: [PATCH 370/565] Update socket_server.c (#1410) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 去除冗余代码 2. fix 保存第一现场的错误码 --- skynet-src/socket_server.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 80baebae2..a109d6a48 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -4,7 +4,6 @@ #include "socket_poll.h" #include "atomic.h" #include "spinlock.h" -#include "skynet.h" #include #include @@ -307,13 +306,13 @@ static inline void send_object_init_from_sendbuffer(struct socket_server *ss, struct send_object *so, struct socket_sendbuffer *buf) { switch (buf->type) { case SOCKET_BUFFER_MEMORY: - send_object_init(ss, so, (void *)buf->buffer, buf->sz); + send_object_init(ss, so, buf->buffer, buf->sz); break; case SOCKET_BUFFER_OBJECT: - send_object_init(ss, so, (void *)buf->buffer, USEROBJECT); + send_object_init(ss, so, buf->buffer, USEROBJECT); break; case SOCKET_BUFFER_RAWPOINTER: - so->buffer = (void *)buf->buffer; + so->buffer = buf->buffer; so->sz = buf->sz; so->free_func = dummy_free; break; @@ -445,7 +444,7 @@ free_buffer(struct socket_server *ss, struct socket_sendbuffer *buf) { void *buffer = (void *)buf->buffer; switch (buf->type) { case SOCKET_BUFFER_MEMORY: - FREE((void *)buffer); + FREE(buffer); break; case SOCKET_BUFFER_OBJECT: ss->soi.free(buffer); @@ -1485,14 +1484,13 @@ forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_lo switch(errno) { case EINTR: case AGAIN_WOULDBLOCK: - break; - default: - // close when error - force_close(ss, s, l, result); - result->data = strerror(errno); - return SOCKET_ERR; + return -1; } - return -1; + int error = errno; + // close when error + force_close(ss, s, l, result); + result->data = strerror(error); + return SOCKET_ERR; } stat_read(ss,s,n); @@ -1524,11 +1522,9 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l socklen_t len = sizeof(error); int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); if (code < 0 || error) { - force_close(ss,s,l, result); - if (code >= 0) - result->data = strerror(error); - else - result->data = strerror(errno); + error = code < 0 ? errno : error; + force_close(ss, s, l, result); + result->data = strerror(error); return SOCKET_ERR; } else { ATOM_STORE(&s->type , SOCKET_TYPE_CONNECTED); @@ -1560,8 +1556,8 @@ static int getname(union sockaddr_all *u, char *buffer, size_t sz) { char tmp[INET6_ADDRSTRLEN]; void * sin_addr = (u->s.sa_family == AF_INET) ? (void*)&u->v4.sin_addr : (void *)&u->v6.sin6_addr; - int sin_port = ntohs((u->s.sa_family == AF_INET) ? u->v4.sin_port : u->v6.sin6_port); if (inet_ntop(u->s.sa_family, sin_addr, tmp, sizeof(tmp))) { + int sin_port = ntohs((u->s.sa_family == AF_INET) ? u->v4.sin_port : u->v6.sin6_port); snprintf(buffer, sz, "%s:%d", tmp, sin_port); return 1; } else { From 30b534b558984b2c046992d3b5a2fda3bee5916c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 3 Jun 2021 08:08:39 +0800 Subject: [PATCH 371/565] ignore break sessions, See issue #1413 --- lualib/skynet.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 9891bf2cb..4f1ae1740 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -968,7 +968,9 @@ function skynet.task(ret) if ret == nil then local t = 0 for session,co in pairs(session_id_coroutine) do - t = t + 1 + if co ~= "BREAK" then + t = t + 1 + end end return t end @@ -983,7 +985,9 @@ function skynet.task(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do local key = string.format("%s session: %d", tostring(co), session) - if timeout_traceback and timeout_traceback[co] then + if co == "BREAK" then + ret[key] = "BREAK" + elseif timeout_traceback and timeout_traceback[co] then ret[key] = timeout_traceback[co] else ret[key] = traceback(co) @@ -993,7 +997,11 @@ function skynet.task(ret) elseif tt == "number" then local co = session_id_coroutine[ret] if co then - return traceback(co) + if co == "BREAK" then + return "BREAK" + else + return traceback(co) + end else return "No session" end From 219b15e9ecc0e4ecb13502d7d19d63b03fb49c9a Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Thu, 3 Jun 2021 19:11:54 +0800 Subject: [PATCH 372/565] Update skynet.lua (#1415) update skynet.ret --- lualib/skynet.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 4f1ae1740..cfa23fb9f 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -633,6 +633,9 @@ function skynet.ret(msg, sz) local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "response") end local co_session = session_coroutine_id[running_thread] + if co_session == nil then + error "No session" + end session_coroutine_id[running_thread] = nil if co_session == 0 then if sz ~= nil then @@ -641,9 +644,6 @@ function skynet.ret(msg, sz) return false -- send don't need ret end local co_address = session_coroutine_address[running_thread] - if not co_session then - error "No session" - end local ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, msg, sz) if ret then return true From c2c585f73ae01862b433b61298787ebf3103fca2 Mon Sep 17 00:00:00 2001 From: colin <124654806@qq.com> Date: Thu, 3 Jun 2021 19:13:30 +0800 Subject: [PATCH 373/565] freebsd compile error (#1414) --- 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 1754eb446..c95273721 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "skynet.h" #include "skynet_socket.h" From c90e2886011d22a8ff18bac77705e84c7e83f30f Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Sat, 5 Jun 2021 00:35:28 +0800 Subject: [PATCH 374/565] Update skynet.lua (#1416) update skynet.wait --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index cfa23fb9f..74507d70a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -413,7 +413,7 @@ end function skynet.wait(token) local session = c.genid() token = token or coroutine.running() - local ret, msg = suspend_sleep(session, token) + suspend_sleep(session, token) sleep_session[token] = nil session_id_coroutine[session] = nil end From 39000670269bcdc02d6d1069b58477ff0c6e3755 Mon Sep 17 00:00:00 2001 From: cm <1625326+pigparadise@users.noreply.github.com> Date: Tue, 8 Jun 2021 17:03:20 +0800 Subject: [PATCH 375/565] bugfix:params changed when cluster sender is connecting (#1420) Co-authored-by: LiuYang --- lualib/skynet/cluster.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 2472b700c..a6886743a 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -5,6 +5,10 @@ local cluster = {} local sender = {} local task_queue = {} +local function repack(address, ...) + return address, skynet.pack(...) +end + local function request_sender(q, node) local ok, c = pcall(skynet.call, clusterd, "lua", "sender", node) if not ok then @@ -16,9 +20,9 @@ local function request_sender(q, node) q.confirm = confirm q.sender = c for _, task in ipairs(q) do - if type(task) == "table" then + if type(task) == "string" then if c then - skynet.send(c, "lua", "push", task[1], skynet.pack(table.unpack(task,2,task.n))) + skynet.send(c, "lua", "push", repack(skynet.unpack(task))) end else skynet.wakeup(task) @@ -53,14 +57,19 @@ end function cluster.call(node, address, ...) -- skynet.pack(...) will free by cluster.core.packrequest - return skynet.call(get_sender(node), "lua", "req", address, skynet.pack(...)) + local s = sender[node] + if not s then + local task = skynet.packstring(address, ...) + return skynet.call(get_sender(node), "lua", "req", repack(skynet.unpack(task))) + end + return skynet.call(s, "lua", "req", address, skynet.pack(...)) end function cluster.send(node, address, ...) -- push is the same with req, but no response local s = sender[node] if not s then - table.insert(task_queue[node], table.pack(address, ...)) + table.insert(task_queue[node], skynet.packstring(address, ...)) else skynet.send(sender[node], "lua", "push", address, skynet.pack(...)) end From 2bfecfceaec2c8a25f072efac15a81d294966efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Wed, 23 Jun 2021 15:52:46 +0800 Subject: [PATCH 376/565] fix lua-seri rb_read (#1427) Co-authored-by: zixun --- lualib-src/lua-seri.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index e6dd531fa..f7b337e21 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -115,7 +115,7 @@ rball_init(struct read_block * rb, char * buffer, int size) { rb->ptr = 0; } -static void * +static const void * rb_read(struct read_block *rb, int sz) { if (rb->len < sz) { return NULL; @@ -363,7 +363,7 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { return 0; case TYPE_NUMBER_BYTE: { uint8_t n; - uint8_t * pn = rb_read(rb,sizeof(n)); + const uint8_t * pn = (const uint8_t *)rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); n = *pn; @@ -371,7 +371,7 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { } case TYPE_NUMBER_WORD: { uint16_t n; - uint16_t * pn = rb_read(rb,sizeof(n)); + const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); @@ -379,7 +379,7 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { } case TYPE_NUMBER_DWORD: { int32_t n; - int32_t * pn = rb_read(rb,sizeof(n)); + const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); @@ -387,7 +387,7 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { } case TYPE_NUMBER_QWORD: { int64_t n; - int64_t * pn = rb_read(rb,sizeof(n)); + const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); @@ -402,7 +402,7 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { static double get_real(lua_State *L, struct read_block *rb) { double n; - double * pn = rb_read(rb,sizeof(n)); + const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); @@ -412,7 +412,7 @@ get_real(lua_State *L, struct read_block *rb) { static void * get_pointer(lua_State *L, struct read_block *rb) { void * userdata = 0; - void ** v = (void **)rb_read(rb,sizeof(userdata)); + const void * v = rb_read(rb,sizeof(userdata)); if (v == NULL) { invalid_stream(L,rb); } @@ -422,7 +422,7 @@ get_pointer(lua_State *L, struct read_block *rb) { static void get_buffer(lua_State *L, struct read_block *rb, int len) { - char * p = rb_read(rb,len); + const char * p = (const char *)rb_read(rb,len); if (p == NULL) { invalid_stream(L,rb); } @@ -435,7 +435,7 @@ static void unpack_table(lua_State *L, struct read_block *rb, int array_size) { if (array_size == MAX_COOKIE-1) { uint8_t type; - uint8_t *t = rb_read(rb, sizeof(type)); + const uint8_t * t = (const uint8_t *)rb_read(rb, sizeof(type)); if (t==NULL) { invalid_stream(L,rb); } @@ -488,7 +488,7 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) { break; case TYPE_LONG_STRING: { if (cookie == 2) { - uint16_t *plen = rb_read(rb, 2); + const void * plen = rb_read(rb, 2); if (plen == NULL) { invalid_stream(L,rb); } @@ -499,7 +499,7 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) { if (cookie != 4) { invalid_stream(L,rb); } - uint32_t *plen = rb_read(rb, 4); + const void * plen = rb_read(rb, 4); if (plen == NULL) { invalid_stream(L,rb); } @@ -523,7 +523,7 @@ 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; - uint8_t *t = rb_read(rb, sizeof(type)); + const uint8_t * t = (const uint8_t *)rb_read(rb, sizeof(type)); if (t==NULL) { invalid_stream(L, rb); } @@ -584,7 +584,7 @@ luaseri_unpack(lua_State *L) { luaL_checkstack(L,LUA_MINSTACK,NULL); } uint8_t type = 0; - uint8_t *t = rb_read(&rb, sizeof(type)); + const uint8_t * t = (const uint8_t *)rb_read(&rb, sizeof(type)); if (t==NULL) break; type = *t; From 5c154d34dc52edcff32355c819bdcc8b999cb3da Mon Sep 17 00:00:00 2001 From: colin <124654806@qq.com> Date: Wed, 30 Jun 2021 12:42:58 +0800 Subject: [PATCH 377/565] improve spinlock (#1431) * improve spinlock * improve spinlock * typo Co-authored-by: colin --- skynet-src/spinlock.h | 60 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 673a207c5..04668082e 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -15,16 +15,6 @@ #define atomic_flag_test_and_set_(ptr) __sync_lock_test_and_set(ptr, 1) #define atomic_flag_clear_(ptr) __sync_lock_release(ptr) -#else - -#include -#define atomic_flag_ atomic_flag -#define ATOMIC_FLAG_INIT_ ATOMIC_FLAG_INIT -#define atomic_flag_test_and_set_ atomic_flag_test_and_set -#define atomic_flag_clear_ atomic_flag_clear - -#endif - struct spinlock { atomic_flag_ lock; }; @@ -55,6 +45,56 @@ spinlock_destroy(struct spinlock *lock) { (void) lock; } +#else // __STDC_NO_ATOMICS__ + +#include +#define atomic_test_and_set_(ptr) atomic_exchange_explicit(ptr, 1, memory_order_acquire) +#define atomic_clear_(ptr) atomic_store_explicit(ptr, 0, memory_order_release); +#define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed) + +#if defined(__x86_64__) +#define atomic_pause_() __builtin_ia32_pause() +#else +#define atomic_pause_() ((void)0) +#endif + +struct spinlock { + atomic_int lock; +}; + +static inline void +spinlock_init(struct spinlock *lock) { + atomic_init(&lock->lock, 0); +} + +static inline void +spinlock_lock(struct spinlock *lock) { + for (;;) { + if (!atomic_test_and_set_(&lock->lock)) + return; + while (atomic_load_relaxed_(&lock->lock)) + atomic_pause_(); + } +} + +static inline int +spinlock_trylock(struct spinlock *lock) { + return !atomic_load_relaxed_(&lock->lock) && + !atomic_test_and_set_(&lock->lock); +} + +static inline void +spinlock_unlock(struct spinlock *lock) { + atomic_clear_(&lock->lock); +} + +static inline void +spinlock_destroy(struct spinlock *lock) { + (void) lock; +} + +#endif // __STDC_NO_ATOMICS__ + #else #include From 338f53fbe0bf10d11259578e2baef7f73539821e Mon Sep 17 00:00:00 2001 From: colin <124654806@qq.com> Date: Thu, 1 Jul 2021 19:29:48 +0800 Subject: [PATCH 378/565] use _mm_pause (#1433) --- skynet-src/spinlock.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 04668082e..7726904d6 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -53,7 +53,8 @@ spinlock_destroy(struct spinlock *lock) { #define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed) #if defined(__x86_64__) -#define atomic_pause_() __builtin_ia32_pause() +#include // For _mm_pause +#define atomic_pause_() _mm_pause() #else #define atomic_pause_() ((void)0) #endif @@ -70,10 +71,10 @@ spinlock_init(struct spinlock *lock) { static inline void spinlock_lock(struct spinlock *lock) { for (;;) { - if (!atomic_test_and_set_(&lock->lock)) - return; - while (atomic_load_relaxed_(&lock->lock)) - atomic_pause_(); + if (!atomic_test_and_set_(&lock->lock)) + return; + while (atomic_load_relaxed_(&lock->lock)) + atomic_pause_(); } } From 39e0aaa27e03b2d6b770b108c4fa094771faa021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sat, 10 Jul 2021 07:24:07 +0800 Subject: [PATCH 379/565] Fix typo (#1436) --- 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 dc21b45c9..3737f0251 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -110,7 +110,7 @@ function gateserver.start(handler) function MSG.error(fd, msg) if fd == socket then - skynet.error("gateserver accpet error:",msg) + skynet.error("gateserver accept error:",msg) else socketdriver.shutdown(fd) if handler.error then From 72a618260146c3966770496d175c914d1a40a10e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 15 Jul 2021 09:35:31 +0800 Subject: [PATCH 380/565] bugfix #1434 (#1439) --- service/clusterd.lua | 23 +++++++++++++++++++---- service/clustersender.lua | 9 +++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 92e0c01f5..a00a4c510 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -15,9 +15,15 @@ local function open_channel(t, key) local ct = connecting[key] if ct then local co = coroutine.running() - table.insert(ct, co) - skynet.wait(co) - return assert(ct.channel) + local channel + while ct do + table.insert(ct, co) + skynet.wait(co) + channel = ct.channel + ct = connecting[key] + -- reload again if ct ~= nil + end + return assert(node_address[key] and channel) end ct = {} connecting[key] = ct @@ -53,8 +59,17 @@ local function open_channel(t, key) else err = string.format("changenode [%s] (%s:%s) failed", key, host, port) end + elseif address == false then + c = node_sender[key] + if c == nil then + -- no sender, always succ + succ = true + else + -- trun off the sender + succ, err = pcall(skynet.call, c, "lua", "changenode", false) + end else - err = string.format("cluster node [%s] is %s.", key, address == false and "down" or "absent") + err = string.format("cluster node [%s] is absent.", key) end connecting[key] = nil for _, co in ipairs(ct) do diff --git a/service/clustersender.lua b/service/clustersender.lua index fe4bde952..f4c0b5724 100644 --- a/service/clustersender.lua +++ b/service/clustersender.lua @@ -59,8 +59,13 @@ local function read_response(sock) end function command.changenode(host, port) - channel:changehost(host, tonumber(port)) - channel:connect(true) + if not host then + skynet.error(string.format("Close cluster sender %s:%d", channel.__host, channel.__port)) + channel:close() + else + channel:changehost(host, tonumber(port)) + channel:connect(true) + end skynet.ret(skynet.pack(nil)) end From d9762b8f38fac491d3b02678b3a2fdbb5105f0b3 Mon Sep 17 00:00:00 2001 From: noname Date: Thu, 15 Jul 2021 20:14:05 +0800 Subject: [PATCH 381/565] =?UTF-8?q?sharedata=20=E5=8E=BB=E6=8E=89=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84value=E5=AD=97=E6=AE=B5=20(#1442)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/sharedatad.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 014723e28..517bb9e34 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -14,7 +14,7 @@ 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 = {} } + local v = {obj = cobj, watch = {} } objmap[cobj] = v pool[name] = v pool_count[name] = { n = 0, threshold = 16 } From 0e9c0c34a91bc0bb5818dd85069eb7b800d6960f Mon Sep 17 00:00:00 2001 From: mrCerberus <416002925@qq.com> Date: Wed, 21 Jul 2021 20:33:22 +0800 Subject: [PATCH 382/565] =?UTF-8?q?'http=E8=AF=B7=E6=B1=82C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=A4=A7=E5=86=99'=20(#1444)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 王羽平.Cerberus --- lualib/http/internal.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 8c26519ab..5b0089209 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -160,11 +160,11 @@ function M.request(interface, 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", method, url, header_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) + 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 0207b9210aa45c03e064405ded3f5916e3bdc93c Mon Sep 17 00:00:00 2001 From: coder <273461474@qq.com> Date: Thu, 5 Aug 2021 16:36:49 +0800 Subject: [PATCH 383/565] Update service_logger.c (#1451) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 格式化时间改为通用的结构,修复windows上时间显示不正确问题 --- service-src/service_logger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index ceeec85de..4147f0446 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -40,7 +40,7 @@ timestring(struct logger *inst, char tmp[SIZETIMEFMT]) { time_t ti = now/100 + inst->starttime; struct tm info; (void)localtime_r(&ti,&info); - strftime(tmp, SIZETIMEFMT, "%D %T", &info); + strftime(tmp, SIZETIMEFMT, "%d/%m/%y %H:%M:%S", &info); return now % 100; } From 740b91533dadee1cfa4f69a16d5a2528ac5c4375 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Aug 2021 19:31:32 +0800 Subject: [PATCH 384/565] Fix #1452 --- lualib/http/internal.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 5b0089209..db0ea0185 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,6 +1,5 @@ local table = table local type = type -local sockethelper = require "http.sockethelper" local M = {} @@ -171,7 +170,7 @@ function M.request(interface, method, host, url, recvheader, header, content) local tmpline = {} local body = M.recvheader(read, tmpline, "") if not body then - error(sockethelper.socket_error) + error("Recv header failed") end local statusline = tmpline[1] From fdc4b3528130a52e72ee8c663199d6eb65cab731 Mon Sep 17 00:00:00 2001 From: JTrancender Date: Wed, 18 Aug 2021 15:11:40 +0800 Subject: [PATCH 385/565] feat: add mongo self batch insert (#1456) * feat: add mongo self batch insert * perf: add safe batch insert test * perf: modify some words --- lualib/skynet/db/mongo.lua | 12 ++++++++++++ test/testmongodb.lua | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index fd9e2c0c1..0eb14e8f8 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -365,6 +365,18 @@ function mongo_collection:batch_insert(docs) sock:request(pack) end +function mongo_collection:safe_batch_insert(docs) + for i = 1, #docs do + if docs[i]._id == nil then + docs[i]._id = bson.objectid() + end + docs[i] = bson_encode(docs[i]) + end + + local r = self.database:runCommand("insert", self.name, "documents", docs) + return werror(r) +end + function mongo_collection:update(selector,update,upsert,multi) local flags = (upsert and 1 or 0) + (multi and 2 or 0) local sock = self.connection.__sock diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 54d5c265e..0a1029422 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -168,6 +168,24 @@ function test_expire_index() assert(false, "test expire index failed"); end +local function test_safe_batch_insert() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:drop() + + local docs, length = {}, 10 + for i = 1, length do + table.insert(docs, {test_key = i}) + end + + db.testcoll:safe_batch_insert(docs) + + local ret = db.testcoll:find() + assert(length == ret:count(), "test safe batch insert failed") +end + skynet.start(function() if username then print("Test auth") @@ -183,5 +201,7 @@ skynet.start(function() test_runcommand() print("Test expire index") test_expire_index() + print("test safe batch insert") + test_safe_batch_insert() print("mongodb test finish."); end) From 6ee8d23ac480e37e9d1802cd7bab6f3884c0734d Mon Sep 17 00:00:00 2001 From: colin <124654806@qq.com> Date: Mon, 23 Aug 2021 16:54:01 +0800 Subject: [PATCH 386/565] ssl request support sni(Server Name Indication) (#1460) * ssl support sni(Server Name Indication) * ssl support sni(Server Name Indication) --- lualib-src/ltls.c | 4 ++++ lualib/http/httpc.lua | 20 ++++++++++++-------- lualib/http/tlshelper.lua | 4 ++-- lualib/http/websocket.lua | 18 +++++++++++------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 81bd397b8..8c50fdea8 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -358,6 +358,10 @@ lnew_tls(lua_State* L) { if(strcmp(method, "client") == 0) { _init_client_context(L, tls_p, ctx_p); + if (!lua_isnoneornil(L, 3)) { + const char* hostname = luaL_checkstring(L, 3); + SSL_set_tlsext_host_name(tls_p->ssl, hostname); + } }else if(strcmp(method, "server") == 0) { _init_server_context(L, tls_p, ctx_p); } else { diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index da4e8b024..936ede47d 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -35,7 +35,7 @@ local function check_protocol(host) end local SSLCTX_CLIENT = nil -local function gen_interface(protocol, fd) +local function gen_interface(protocol, fd, hostname) if protocol == "http" then return { init = nil, @@ -49,7 +49,7 @@ local function gen_interface(protocol, fd) elseif protocol == "https" then local tls = require "http.tlshelper" SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() - local tls_ctx = tls.newtls("client", SSLCTX_CLIENT) + local tls_ctx = tls.newtls("client", SSLCTX_CLIENT, hostname) return { init = tls.init_requestfunc(fd, tls_ctx), close = tls.closefunc(tls_ctx), @@ -67,22 +67,26 @@ function httpc.request(method, host, url, recvheader, header, content) local protocol local timeout = httpc.timeout -- get httpc.timeout before any blocked api protocol, host = check_protocol(host) - local hostname, port = host:match"([^:]+):?(%d*)$" + local hostaddr, port = host:match"([^:]+):?(%d*)$" if port == "" then port = protocol=="http" and 80 or protocol=="https" and 443 else port = tonumber(port) end - if async_dns and not hostname:match(".*%d+$") then - hostname = dns.resolve(hostname) + local hostname + if not hostaddr:match(".*%d+$") then + hostname = hostaddr + if async_dns then + hostaddr = dns.resolve(hostname) + end end - local fd = socket.connect(hostname, port, timeout) + local fd = socket.connect(hostaddr, port, timeout) if not fd then - error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostname, port, timeout)) + error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostaddr, port, timeout)) return end -- print("protocol hostname port", protocol, hostname, port) - local interface = gen_interface(protocol, fd) + local interface = gen_interface(protocol, fd, hostname) local finish if timeout then skynet.timeout(timeout, function() diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua index e25b22922..04a15b513 100644 --- a/lualib/http/tlshelper.lua +++ b/lualib/http/tlshelper.lua @@ -89,8 +89,8 @@ function tlshelper.newctx() return c.newctx() end -function tlshelper.newtls(method, ssl_ctx) - return c.newtls(method, ssl_ctx) +function tlshelper.newtls(method, ssl_ctx, hostname) + return c.newtls(method, ssl_ctx, hostname) end return tlshelper \ No newline at end of file diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 92ac17304..c149bbbae 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -302,7 +302,7 @@ end local SSLCTX_CLIENT = nil -local function _new_client_ws(socket_id, protocol) +local function _new_client_ws(socket_id, protocol, hostname) local obj if protocol == "ws" then obj = { @@ -319,7 +319,7 @@ local function _new_client_ws(socket_id, protocol) elseif protocol == "wss" then local tls = require "http.tlshelper" SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() - local tls_ctx = tls.newtls("client", SSLCTX_CLIENT) + local tls_ctx = tls.newtls("client", SSLCTX_CLIENT, hostname) local init = tls.init_requestfunc(socket_id, tls_ctx) init() obj = { @@ -435,17 +435,21 @@ function M.connect(url, header, timeout) end assert(host) - local host_name, host_port = string.match(host, "^([^:]+):?(%d*)$") - assert(host_name and host_port) + local host_addr, host_port = string.match(host, "^([^:]+):?(%d*)$") + assert(host_addr and host_port) if host_port == "" then host_port = protocol == "ws" and 80 or 443 end + local hostname + if not host_addr:match(".*%d+$") then + hostname = host_addr + end uri = uri == "" and "/" or uri - local socket_id = sockethelper.connect(host_name, host_port, timeout) - local ws_obj = _new_client_ws(socket_id, protocol) + local socket_id = sockethelper.connect(host_addr, host_port, timeout) + local ws_obj = _new_client_ws(socket_id, protocol, hostname) ws_obj.addr = host - write_handshake(ws_obj, host_name, uri, header) + write_handshake(ws_obj, host_addr, uri, header) return socket_id end From 54e733a76f1d96de3693c247132aa63795d46ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Mon, 30 Aug 2021 10:00:57 +0800 Subject: [PATCH 387/565] Request stream (#1463) * split request/response * stream api * fix: global variable (#1464) * fix global variable * fix global variable * Add httpc.head Co-authored-by: JTrancender --- lualib/http/httpc.lua | 62 +++++++++--- lualib/http/internal.lua | 197 ++++++++++++++++++++++++++++++++++---- lualib/http/websocket.lua | 3 +- test/testhttp.lua | 24 ++++- 4 files changed, 250 insertions(+), 36 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 936ede47d..7fdd721dd 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -62,10 +62,8 @@ local function gen_interface(protocol, fd, hostname) end end - -function httpc.request(method, host, url, recvheader, header, content) +local function connect(host, timeout) local protocol - local timeout = httpc.timeout -- get httpc.timeout before any blocked api protocol, host = check_protocol(host) local hostaddr, port = host:match"([^:]+):?(%d*)$" if port == "" then @@ -83,30 +81,38 @@ function httpc.request(method, host, url, recvheader, header, content) local fd = socket.connect(hostaddr, port, timeout) if not fd then error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostaddr, port, timeout)) - return end -- print("protocol hostname port", protocol, hostname, port) local interface = gen_interface(protocol, fd, hostname) - local finish + if interface.init then + interface.init() + end if timeout then skynet.timeout(timeout, function() - if not finish then + if not interface.finish then socket.shutdown(fd) -- shutdown the socket fd, need close later. - if interface.close then - interface.close() - end end end) end - if interface.init then - interface.init() - end - local ok , statuscode, body = pcall(internal.request, interface, method, host, url, recvheader, header, content) - finish = true + return fd, interface, host +end + +local function close_interface(interface, fd) + interface.finish = true socket.close(fd) if interface.close then interface.close() + interface.close = nil + end +end + +function httpc.request(method, hostname, url, recvheader, header, content) + local fd, interface, host = connect(hostname, httpc.timeout) + local ok , statuscode, body , header = pcall(internal.request, interface, method, host, url, recvheader, header, content) + if ok then + ok, body = pcall(internal.response, interface, statuscode, body, header) end + close_interface(interface, fd) if ok then return statuscode, body else @@ -114,6 +120,34 @@ function httpc.request(method, host, url, recvheader, header, content) end end +function httpc.head(hostname, url, recvheader, header, content) + local fd, interface, host = connect(hostname, httpc.timeout) + local ok , statuscode = pcall(internal.request, interface, "HEAD", host, url, recvheader, header, content) + close_interface(interface, fd) + if ok then + return statuscode + else + error(statuscode) + end +end + +function httpc.request_stream(method, hostname, url, header, content) + local fd, interface, host = connect(hostname, httpc.timeout) + local ok , statuscode, body , header = pcall(internal.request, interface, method, host, url, recvheader, header, content) + interface.finish = true -- don't shutdown fd in timeout + local function close_fd() + close_interface(interface, fd) + end + if not ok then + close_fd() + error(statuscode) + end + -- todo: stream support timeout + local stream = internal.response_stream(interface, statuscode, body, header) + stream._onclose = close_fd + return stream +end + function httpc.get(...) return httpc.request("GET", ...) end diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index db0ea0185..02aeef321 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -141,9 +141,29 @@ function M.recvchunkedbody(readbytes, bodylimit, header, body) return result, header end +local function recvbody(interface, code, header, body) + local length = header["content-length"] + if length then + length = tonumber(length) + end + if length then + if #body >= length then + body = body:sub(1,length) + else + local padding = interface.read(length - #body) + body = body .. padding + end + elseif code == 204 or code == 304 or code < 200 then + body = "" + -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response + else + -- no content-length, read all + body = body .. interface.readall() + end + return body +end function M.request(interface, method, host, url, recvheader, header, content) - local is_ws = interface.websocket local read = interface.read local write = interface.write local header_content = "" @@ -181,11 +201,10 @@ function M.request(interface, method, host, url, recvheader, header, content) if not header then error("Invalid HTTP response header") end + return code, body, header +end - local length = header["content-length"] - if length then - length = tonumber(length) - end +function M.response(interface, code, body, header) local mode = header["transfer-encoding"] if mode then if mode ~= "identity" and mode ~= "chunked" then @@ -194,32 +213,172 @@ function M.request(interface, method, host, url, recvheader, header, content) end if mode == "chunked" then - body, header = M.recvchunkedbody(read, nil, header, body) + body, header = M.recvchunkedbody(interface.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) + body = recvbody(interface, code, header, body) + end + + return body +end + +local stream = {}; stream.__index = stream + +function stream:close() + if self._onclose then + self._onclose(self) + self._onclose = nil + end +end + +function stream:padding() + return self._reading(self), self +end + +stream.__close = stream.close +stream.__call = stream.padding + +local function stream_nobody(stream) + stream._reading = stream.close + stream.connected = nil + return "" +end + +local function stream_length(length) + return function(stream) + local body = stream._body + if body == nil then + local ret, padding = interface.read() + if not ret then + -- disconnected + body = padding + stream.connected = false else - local padding = read(length - #body) - body = body .. padding + body = ret end + end + local n = #body + if n >= length then + stream._reading = stream.close + stream.connected = nil + return (body:sub(1,length)) + else + length = length - n + stream._body = nil + if not stream.connected then + stream._reading = stream.close + end + return body + end + end +end + +local function stream_read(stream) + local ret, padding = stream._interface.read() + if ret == "" or not ret then + stream.connected = nil + stream:close() + if padding == "" then + return + end + return padding + end + return ret +end + +local function stream_all(stream) + local body = stream._body + stream._body = nil + stream._reading = stream_read + return body +end + +local function stream_chunked(stream) + local read = stream._interface.read + local sz, body = chunksize(read, stream._body) + if not sz then + stream.connected = false + stream:close() + return + end + + if sz == 0 then + -- last chunk + local tmpline = {} + body = M.recvheader(read, tmpline, body) + if not body then + stream.connected = false + stream:close() + return + end + + M.parseheader(tmpline,1, stream.header) + + stream._reading = stream.close + stream.connected = nil + return "" + end + + local n = #body + local remain + + if n >= sz then + remain = body:sub(sz+1) + body = body:sub(1,sz) + else + body = body .. read(sz - n) + remain = "" + end + remain = readcrln(read, remain) + if not remain then + stream.connected = false + stream:close() + return + end + stream._body = remain + return body +end + +function M.response_stream(interface, code, body, header) + local mode = header["transfer-encoding"] + if mode then + if mode ~= "identity" and mode ~= "chunked" then + error ("Unsupport transfer-encoding") + end + end + + local read_func + + if mode == "chunked" then + readfunc = stream_chunked + else + -- identity mode + local length = header["content-length"] + if length then + length = tonumber(length) + end + if length then + readfunc = stream_length(length) elseif code == 204 or code == 304 or code < 200 then - body = "" - -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response - elseif is_ws and code == 101 then - -- if websocket handshake success - return code, body + readfunc = stream_nobody else - -- no content-length, read all - body = body .. interface.readall() + readfunc = stream_all end end - return code, body + -- todo: timeout + + return setmetatable({ + status = code, + _body = body, + _interface = interface, + _reading = readfunc, + header = header, + connected = true, + }, stream) end return M diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index c149bbbae..896040210 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -45,6 +45,7 @@ local function write_handshake(self, host, url, header) if code ~= 101 then error(string.format("websocket handshake error: code[%s] info:%s", code, body)) end + assert(body == "") -- todo: M.read may need handle it if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then error("websocket handshake upgrade must websocket") @@ -306,7 +307,6 @@ local function _new_client_ws(socket_id, protocol, hostname) local obj if protocol == "ws" then obj = { - websocket = true, close = function () socket.close(socket_id) end, @@ -323,7 +323,6 @@ local function _new_client_ws(socket_id, protocol, hostname) local init = tls.init_requestfunc(socket_id, tls_ctx) init() obj = { - websocket = true, close = function () socket.close(socket_id) tls.closefunc(tls_ctx)() diff --git a/test/testhttp.lua b/test/testhttp.lua index 13c4d3db3..5391574f9 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -25,11 +25,32 @@ local function http_test(protocol) print(status) end +local function http_stream_test() + for resp, stream in httpc.request_stream("GET", "http://baidu.com", "/") do + print("STATUS", stream.status) + for k,v in pairs(stream.header) do + print("HEADER",k,v) + end + print("BODY", resp) + end +end + +local function http_head_test() + httpc.timeout = 100 + local respheader = {} + local status = httpc.head("http://baidu.com", "/", respheader) + for k,v in pairs(respheader) do + print("HEAD", k, v) + end +end local function main() dns.server() - http_test("http") + http_stream_test() + http_head_test() + + http_test("http") if not pcall(require,"ltls.c") then print "No ltls module, https is not supported" else @@ -41,3 +62,4 @@ skynet.start(function() print(pcall(main)) skynet.exit() end) + \ No newline at end of file From 987bb3df4716f5a46eb26f412a04103171e99174 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Wed, 1 Sep 2021 07:12:27 +0800 Subject: [PATCH 388/565] modify from http folder (#1470) * delete unused var and add used arg * use interface from stream --- lualib/http/httpc.lua | 3 +-- lualib/http/httpd.lua | 1 - lualib/http/internal.lua | 12 ++++++------ lualib/http/tlshelper.lua | 1 - 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 7fdd721dd..058e28a48 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -1,6 +1,5 @@ local skynet = require "skynet" local socket = require "http.sockethelper" -local url = require "http.url" local internal = require "http.internal" local dns = require "skynet.dns" local string = string @@ -131,7 +130,7 @@ function httpc.head(hostname, url, recvheader, header, content) end end -function httpc.request_stream(method, hostname, url, header, content) +function httpc.request_stream(method, hostname, url, recvheader, header, content) local fd, interface, host = connect(hostname, httpc.timeout) local ok , statuscode, body , header = pcall(internal.request, interface, method, host, url, recvheader, header, content) interface.finish = true -- don't shutdown fd in timeout diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 0131db0a3..1575f5a64 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -1,6 +1,5 @@ local internal = require "http.internal" -local table = table local string = string local type = type diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 02aeef321..db56ab2b1 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -251,7 +251,7 @@ local function stream_length(length) return function(stream) local body = stream._body if body == nil then - local ret, padding = interface.read() + local ret, padding = stream._interface.read() if not ret then -- disconnected body = padding @@ -353,7 +353,7 @@ function M.response_stream(interface, code, body, header) local read_func if mode == "chunked" then - readfunc = stream_chunked + read_func = stream_chunked else -- identity mode local length = header["content-length"] @@ -361,11 +361,11 @@ function M.response_stream(interface, code, body, header) length = tonumber(length) end if length then - readfunc = stream_length(length) + read_func = stream_length(length) elseif code == 204 or code == 304 or code < 200 then - readfunc = stream_nobody + read_func = stream_nobody else - readfunc = stream_all + read_func = stream_all end end @@ -375,7 +375,7 @@ function M.response_stream(interface, code, body, header) status = code, _body = body, _interface = interface, - _reading = readfunc, + _reading = read_func, header = header, connected = true, }, stream) diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua index 04a15b513..05e3d3ae8 100644 --- a/lualib/http/tlshelper.lua +++ b/lualib/http/tlshelper.lua @@ -77,7 +77,6 @@ function tlshelper.writefunc(fd, tls_ctx) end function tlshelper.readallfunc(fd, tls_ctx) - local readfunc = socket.readfunc(fd) return function () local ds = socket.readall(fd) local s = tls_ctx:read(ds) From edbed9f9800c34e078b5c9149f98129c16349b88 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 26 Sep 2021 11:57:32 +0800 Subject: [PATCH 389/565] bugfix: Don't need inc sending since dw_buffer is not NULL --- skynet-src/socket_server.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index a109d6a48..40ef470cb 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1846,8 +1846,6 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { socket_unlock(&l); - inc_sending_ref(s, id); - struct request_package request; request.u.send.id = id; request.u.send.sz = 0; From 9cbab6f5ec3970cfe36593c1a225f354dbb30a3b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 27 Sep 2021 08:52:23 +0800 Subject: [PATCH 390/565] avoid warnings --- skynet-src/socket_server.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 40ef470cb..8042b3f56 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -67,11 +67,12 @@ struct write_buffer { char *ptr; size_t 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 write_buffer_udp { + struct write_buffer buffer; + uint8_t udp_address[UDP_ADDRESS_SIZE]; +}; struct wb_list { struct write_buffer * head; @@ -777,8 +778,9 @@ 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; + struct write_buffer_udp * udp = (struct write_buffer_udp *)tmp; union sockaddr_all sa; - socklen_t sasz = udp_socket_address(s, tmp->udp_address, &sa); + socklen_t sasz = udp_socket_address(s, udp->udp_address, &sa); if (sasz == 0) { skynet_error(NULL, "socket-server : udp (%d) type mismatch.", s->id); drop_udp(ss, s, list, tmp); @@ -920,7 +922,7 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_lock *l, s return -1; // blocked by direct write, send later. if (s->dw_buffer) { // add direct write buffer before high.head - struct write_buffer * buf = MALLOC(SIZEOF_TCPBUFFER); + struct write_buffer * buf = MALLOC(sizeof(*buf)); struct send_object so; buf->userobject = send_object_init(ss, &so, (void *)s->dw_buffer, s->dw_size); buf->ptr = (char*)so.buffer+s->dw_offset; @@ -965,20 +967,20 @@ append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_s 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); + struct write_buffer_udp *buf = (struct write_buffer_udp *)append_sendbuffer_(ss, wl, request, sizeof(*buf)); memcpy(buf->udp_address, udp_address, UDP_ADDRESS_SIZE); - s->wb_size += buf->sz; + s->wb_size += buf->buffer.sz; } static inline void append_sendbuffer(struct socket_server *ss, struct socket *s, struct request_send * request) { - struct write_buffer *buf = append_sendbuffer_(ss, &s->high, request, SIZEOF_TCPBUFFER); + struct write_buffer *buf = append_sendbuffer_(ss, &s->high, request, sizeof(*buf)); s->wb_size += buf->sz; } static inline void append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_send * request) { - struct write_buffer *buf = append_sendbuffer_(ss, &s->low, request, SIZEOF_TCPBUFFER); + struct write_buffer *buf = append_sendbuffer_(ss, &s->low, request, sizeof(*buf)); s->wb_size += buf->sz; } @@ -1749,8 +1751,9 @@ static void send_request(struct socket_server *ss, struct request_package *request, char type, int len) { request->header[6] = (uint8_t)type; request->header[7] = (uint8_t)len; + const char * req = (const char *)request + offsetof(struct request_package, header[6]); for (;;) { - ssize_t n = write(ss->sendctrl_fd, &request->header[6], len+2); + ssize_t n = write(ss->sendctrl_fd, req, len+2); if (n<0) { if (errno != EINTR) { skynet_error(NULL, "socket-server : send ctrl command error %s.", strerror(errno)); From 0ebecb4e6da85379c8bbf566f37c808197dc0d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Mon, 27 Sep 2021 11:26:28 +0800 Subject: [PATCH 391/565] add ispurewhite : is white and not shared object, see #1479 (#1480) --- 3rd/lua/lgc.c | 10 ++++------ 3rd/lua/lgc.h | 7 ++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index e7e1bbe62..e90766dc0 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -80,9 +80,9 @@ (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) +#define valiswhite(x) (iscollectable(x) && ispurewhite(gcvalue(x))) -#define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) +#define keyiswhite(n) (keyiscollectable(n) && ispurewhite(gckey(n))) /* @@ -96,7 +96,7 @@ #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } -#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } +#define markobject(g,t) { if (ispurewhite(t)) reallymarkobject(g, obj2gco(t)); } /* ** mark an object that can be NULL (either because it is really optional, @@ -188,7 +188,7 @@ static int iscleared (global_State *g, const GCObject *o) { markobject(g, o); /* strings are 'values', so are never weak */ return 0; } - else return iswhite(o); + else return ispurewhite(o); } @@ -289,8 +289,6 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - if (isshared(o)) - return; switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index eb00bcf8f..62a44e1a6 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -120,6 +120,7 @@ #define isshared(x) (getage(x) == G_SHARED) #define makeshared(x) setage(x, G_SHARED) +#define ispurewhite(x) (iswhite(x) && !isshared(x)) #define changeage(o,f,t) \ check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) @@ -167,15 +168,15 @@ #define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v)) && !isshared(gcvalue(v))) ? \ + (iscollectable(v) && isblack(p) && ispurewhite(gcvalue(v))) ? \ luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) #define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v)) && !isshared(gcvalue(v))) ? \ + (iscollectable(v) && isblack(p) && ispurewhite(gcvalue(v))) ? \ luaC_barrierback_(L,p) : cast_void(0)) #define luaC_objbarrier(L,p,o) ( \ - (isblack(p) && iswhite(o) && !isshared(o)) ? \ + (isblack(p) && ispurewhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); From 0ae839b2dd1f9ff916249cd13d302dfec4087be6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 30 Sep 2021 11:24:36 +0800 Subject: [PATCH 392/565] Check req size , See #1481 --- lualib-src/lua-cluster.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index a050f3c02..da6712786 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -366,6 +366,8 @@ lunpackrequest(lua_State *L) { msg = luaL_checklstring(L,1,&ssz); sz = (int)ssz; } + if (sz == 0) + return luaL_error(L, "Invalid req package. size == 0"); switch (msg[0]) { case 0: return unpackreq_number(L, (const uint8_t *)msg, sz); From f80e2cd9b19c5478d63f7da4905fc121fe6aa5ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Oct 2021 12:57:55 +0800 Subject: [PATCH 393/565] bugfix: See #1484 --- lualib-src/lua-bson.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 9d20242ae..517fd40e1 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -512,16 +512,25 @@ pack_meta_dict(lua_State *L, struct bson *b, int depth) { static bool is_rawarray(lua_State *L) { + // test first key, hash part first in Lua 5.4 + lua_pushnil(L); + if (lua_next(L, -2) == 0) { + // empty table + return false; + } + lua_Integer firstkey = lua_tointeger(L, -2); + lua_pop(L, 2); + if (firstkey != 1) + return false; + 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); - } + lua_pushinteger(L, len); + if (lua_next(L,-2) == 0) { + return true; + } else { + lua_pop(L,2); + return false; } - return false; } static void From 0e11c78575abff39d1821ebb4520c032623d2c0b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Oct 2021 15:36:19 +0800 Subject: [PATCH 394/565] avoid string number --- 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 517fd40e1..2bdd6d9ec 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -518,7 +518,7 @@ is_rawarray(lua_State *L) { // empty table return false; } - lua_Integer firstkey = lua_tointeger(L, -2); + lua_Integer firstkey = (lua_type(L, -2) == LUA_TNUMBER) ? lua_tointeger(L, -2) : 0; lua_pop(L, 2); if (firstkey != 1) return false; From 58da8a1a223601f9c612b41d48d410a461edc0db Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Oct 2021 15:41:30 +0800 Subject: [PATCH 395/565] Use lua_isinteger instead --- 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 2bdd6d9ec..8c62c5e8a 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -518,7 +518,7 @@ is_rawarray(lua_State *L) { // empty table return false; } - lua_Integer firstkey = (lua_type(L, -2) == LUA_TNUMBER) ? lua_tointeger(L, -2) : 0; + lua_Integer firstkey = lua_isinteger(L, -2) ? lua_tointeger(L, -2) : 0; lua_pop(L, 2); if (firstkey != 1) return false; From 432b0c07f9f906b30863660f02cf66cabecf0ecd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Oct 2021 23:02:40 +0800 Subject: [PATCH 396/565] array may hash part only --- lualib-src/lua-bson.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 8c62c5e8a..e77a0612d 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -512,7 +512,6 @@ pack_meta_dict(lua_State *L, struct bson *b, int depth) { static bool is_rawarray(lua_State *L) { - // test first key, hash part first in Lua 5.4 lua_pushnil(L); if (lua_next(L, -2) == 0) { // empty table @@ -520,17 +519,7 @@ is_rawarray(lua_State *L) { } lua_Integer firstkey = lua_isinteger(L, -2) ? lua_tointeger(L, -2) : 0; lua_pop(L, 2); - if (firstkey != 1) - return false; - - size_t len = lua_rawlen(L, -1); - lua_pushinteger(L, len); - if (lua_next(L,-2) == 0) { - return true; - } else { - lua_pop(L,2); - return false; - } + return firstkey > 0; } static void From 599181ceaf8f8d19183e1122ee4f81c7f2d5adb3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 22 Oct 2021 10:14:33 +0800 Subject: [PATCH 397/565] bugfix --- lualib/skynet/manager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua index d2437b542..90d389fb4 100644 --- a/lualib/skynet/manager.lua +++ b/lualib/skynet/manager.lua @@ -27,7 +27,7 @@ local function globalname(name, handle) return false end - assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h + 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" From c008476417b20fd06202cc3d196dc0e076cb1c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 22 Oct 2021 17:43:07 +0800 Subject: [PATCH 398/565] Support string address, see #1490 (#1491) --- lualib/skynet/manager.lua | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua index 90d389fb4..e2c5fc4df 100644 --- a/lualib/skynet/manager.lua +++ b/lualib/skynet/manager.lua @@ -1,17 +1,30 @@ local skynet = require "skynet" local c = require "skynet.core" +local function number_address(name) + local t = type(name) + if t == "number" then + return name + elseif t == "string" then + local hex = name:match "^:(%x+)" + if hex then + return tonumber(hex, 16) + end + end +end + function skynet.launch(...) local addr = c.command("LAUNCH", table.concat({...}," ")) if addr then - return tonumber("0x" .. string.sub(addr , 2)) + return tonumber(string.sub(addr , 2), 16) end end function skynet.kill(name) - if type(name) == "number" then - skynet.send(".launcher","lua","REMOVE",name, true) - name = skynet.address(name) + local addr = number_address(name) + if addr then + skynet.send(".launcher","lua","REMOVE", addr, true) + name = skynet.address(addr) end c.command("KILL",name) end From 8f7e19310c1c2e22b127b8dba659b6386836f897 Mon Sep 17 00:00:00 2001 From: yxt945 Date: Wed, 27 Oct 2021 21:00:25 +0800 Subject: [PATCH 399/565] add json support (#1493) --- lualib/skynet/db/mysql.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 67fd39500..ee6f19df7 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -862,6 +862,7 @@ local _binary_parser = { [0x0c] = _get_datetime, [0x0f] = _from_length_coded_str, [0x10] = _from_length_coded_str, + [0xf5] = _from_length_coded_str, [0xf9] = _from_length_coded_str, [0xfa] = _from_length_coded_str, [0xfb] = _from_length_coded_str, From e35cad3053cd68e6bf64c19f93090f711f974e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Fri, 29 Oct 2021 13:55:54 +0800 Subject: [PATCH 400/565] =?UTF-8?q?fix=20#1494=20socket=20read=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E9=97=AE=E9=A2=98=20(#1495)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix #1494 socket read阻塞问题 * 优化readfunc Co-authored-by: zixun --- lualib/http/tlshelper.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua index 05e3d3ae8..359c423a6 100644 --- a/lualib/http/tlshelper.lua +++ b/lualib/http/tlshelper.lua @@ -43,13 +43,16 @@ function tlshelper.closefunc(tls_ctx) end function tlshelper.readfunc(fd, tls_ctx) - local readfunc = socket.readfunc(fd) + local function readfunc() + readfunc = socket.readfunc(fd) + return "" + end local read_buff = "" return function (sz) if not sz then local s = "" if #read_buff == 0 then - local ds = readfunc(sz) + local ds = readfunc() s = tls_ctx:read(ds) end s = read_buff .. s From a0a138c0028306587b4f3b2bef115fea9b36d81c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 9 Nov 2021 13:37:17 +0800 Subject: [PATCH 401/565] Release v1.5.0 --- HISTORY.md | 11 +++++++++++ README.md | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 6c36e5386..775039647 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,14 @@ +v1.5.0 (2020-11-9) +----------- +* Update Lua to 5.4.3 +* Fix socket half close issues +* Fix TLS issues +* Improve websocket support +* Improve redis support +* Rework skynet.init/skynet.require +* Add socket.onclose +* Add httpc.request_stream + v1.4.0 (2020-11-16) ----------- * Update Lua to 5.4.2 diff --git a/README.md b/README.md index 55b291c4a..c5016c4bd 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.4.2 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.3 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. From 38e5b1ca1ca4c0ea37df17984f4bd680193b02cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Tue, 9 Nov 2021 13:55:55 +0800 Subject: [PATCH 402/565] fix wrong date (#1501) --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 775039647..0b10d5830 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -v1.5.0 (2020-11-9) +v1.5.0 (2021-11-9) ----------- * Update Lua to 5.4.3 * Fix socket half close issues From d115e626e68e51968cb4f26c7fafea310a73f180 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Nov 2021 10:57:20 +0800 Subject: [PATCH 403/565] Update Lua 5.4.4 rc1 --- 3rd/lua/Makefile | 8 ++- 3rd/lua/lapi.c | 28 ++++++--- 3rd/lua/lauxlib.c | 1 + 3rd/lua/lauxlib.h | 10 +++- 3rd/lua/lbaselib.c | 10 +++- 3rd/lua/lcode.c | 34 ++++++++--- 3rd/lua/lcorolib.c | 4 +- 3rd/lua/ldebug.c | 59 ++++++++++++++----- 3rd/lua/ldo.c | 142 ++++++++++++++++++++++++++++----------------- 3rd/lua/ldo.h | 4 +- 3rd/lua/llimits.h | 14 +++++ 3rd/lua/lobject.h | 4 +- 3rd/lua/lopcodes.h | 21 +++++-- 3rd/lua/lparser.c | 22 +++++-- 3rd/lua/lstate.c | 6 +- 3rd/lua/lstate.h | 2 +- 3rd/lua/lstrlib.c | 129 ++++++++++++++++++++++++++++------------ 3rd/lua/ltable.c | 34 +++++++---- 3rd/lua/ltablib.c | 5 +- 3rd/lua/lua.c | 37 +++++++----- 3rd/lua/lua.h | 4 +- 3rd/lua/luac.c | 7 ++- 3rd/lua/luaconf.h | 4 -- 3rd/lua/lutf8lib.c | 11 ++-- 3rd/lua/lvm.c | 54 +++++++++-------- 25 files changed, 439 insertions(+), 215 deletions(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 47dd795aa..fd5caf14f 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -26,7 +26,7 @@ MYLIBS= MYOBJS= # Special flags for compiler modules; -Os reduces code size. -CMCFLAGS= -Os +CMCFLAGS= # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= @@ -67,7 +67,7 @@ $(LUAC_T): $(LUAC_O) $(LUA_A) $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) test: - ./lua -v + ./$(LUA_T) -v clean: $(RM) $(ALL_T) $(ALL_O) @@ -79,7 +79,7 @@ echo: @echo "PLAT= $(PLAT)" @echo "CC= $(CC)" @echo "CFLAGS= $(CFLAGS)" - @echo "LDFLAGS= $(SYSLDFLAGS)" + @echo "LDFLAGS= $(LDFLAGS)" @echo "LIBS= $(LIBS)" @echo "AR= $(AR)" @echo "RANLIB= $(RANLIB)" @@ -108,6 +108,8 @@ c89: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" @echo '' @echo '*** C89 does not guarantee 64-bit integers for Lua.' + @echo '*** Make sure to compile all external Lua libraries' + @echo '*** with LUA_USE_C89 to ensure consistency' @echo '' FreeBSD NetBSD OpenBSD freebsd: diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 8ebf16f09..f91769be7 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -53,6 +53,10 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) +/* +** Convert an acceptable index to a pointer to its respective value. +** Non-valid indices return the special nil value 'G(L)->nilvalue'. +*/ static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { @@ -70,22 +74,28 @@ static TValue *index2value (lua_State *L, int idx) { else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(s2v(ci->func))) /* light C function? */ - return &G(L)->nilvalue; /* it has no upvalues */ - else { + if (ttisCclosure(s2v(ci->func))) { /* C closure? */ CClosure *func = clCvalue(s2v(ci->func)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } + else { /* light C function or Lua function (through a hook)?) */ + api_check(L, ttislcf(s2v(ci->func)), "caller not a C function"); + return &G(L)->nilvalue; /* no upvalues */ + } } } -static StkId index2stack (lua_State *L, int idx) { + +/* +** Convert a valid actual index (not a pseudo-index) to its address. +*/ +l_sinline StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func + idx; - api_check(L, o < L->top, "unacceptable index"); + api_check(L, o < L->top, "invalid index"); return o; } else { /* non-positive index */ @@ -218,7 +228,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { ** Note that we move(copy) only the value inside the stack. ** (We do not move additional fields that may exist.) */ -static void reverse (lua_State *L, StkId from, StkId to) { +l_sinline void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; setobj(L, &temp, s2v(from)); @@ -438,7 +448,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { } -static void *touserdata (const TValue *o) { +l_sinline void *touserdata (const TValue *o) { switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -630,7 +640,7 @@ LUA_API int lua_pushthread (lua_State *L) { */ -static int auxgetstr (lua_State *L, const TValue *t, const char *k) { +l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { @@ -705,7 +715,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } -static int finishrawget (lua_State *L, const TValue *val) { +l_sinline int finishrawget (lua_State *L, const TValue *val) { if (isempty(val)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top)); else diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index f96f304e7..27b02f9f8 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -881,6 +881,7 @@ 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) { + idx = lua_absindex(L,idx); if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ if (!lua_isstring(L, -1)) luaL_error(L, "'__tostring' must return a string"); diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 7fe4d4e1b..6f70c3f6e 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -104,7 +104,7 @@ LUALIB_API lua_State *(luaL_newstate) (void); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); -LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, +LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, const char *p, const char *r); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); @@ -156,6 +156,14 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +/* +** Perform arithmetic operations on lua_Integer values with wrap-around +** semantics, as the Lua core does. +*/ +#define luaL_intop(op,v1,v2) \ + ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) + + /* push the value used to represent failure/error */ #define luaL_pushfail(L) lua_pushnil(L) diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 83ad306d9..912c4cc63 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -261,6 +261,11 @@ static int luaB_next (lua_State *L) { } +static int pairscont (lua_State *L, int status, lua_KContext k) { + (void)L; (void)status; (void)k; /* unused */ + return 3; +} + static int luaB_pairs (lua_State *L) { luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ @@ -270,7 +275,7 @@ static int luaB_pairs (lua_State *L) { } else { lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ + lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */ } return 3; } @@ -280,7 +285,8 @@ static int luaB_pairs (lua_State *L) { ** Traversal function for 'ipairs' */ static int ipairsaux (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2) + 1; + lua_Integer i = luaL_checkinteger(L, 2); + i = luaL_intop(+, i, 1); lua_pushinteger(L, i); return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; } diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 80d975cb8..9cba24f9c 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -580,24 +581,41 @@ static int stringK (FuncState *fs, TString *s) { /* ** 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. */ static int luaK_intK (FuncState *fs, lua_Integer n) { - TValue k, o; - setpvalue(&k, cast_voidp(cast_sizet(n))); + TValue o; setivalue(&o, n); - return addk(fs, &k, &o); + return addk(fs, &o, &o); /* use integer itself as key */ } /* -** Add a float to list of constants and return its index. +** Add a float to list of constants and return its index. Floats +** with integral values need a different key, to avoid collision +** with actual integers. To that, we add to the number its smaller +** power-of-two fraction that is still significant in its scale. +** For doubles, that would be 1/2^52. +** (This method is not bulletproof: there may be another float +** with that value, and for floats larger than 2^53 the result is +** still an integer. At worst, this only wastes an entry with +** a duplicate.) */ static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o; + lua_Integer ik; setfltvalue(&o, r); - return addk(fs, &o, &o); /* use number itself as key */ + if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ + return addk(fs, &o, &o); /* use number itself as key */ + else { /* must build an alternative key */ + const int nbm = l_floatatt(MANT_DIG); + const lua_Number q = l_mathop(ldexp)(1.0, -nbm + 1); + const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ + TValue kv; + setfltvalue(&kv, k); + /* result is not an integral value, unless value is too large */ + lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || + l_mathop(fabs)(r) >= l_mathop(1e6)); + return addk(fs, &kv, &o); + } } diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index fedbebec3..785a1e81a 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -78,7 +78,7 @@ static int luaB_auxwrap (lua_State *L) { if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ stat = lua_resetthread(co); /* close its tbc variables */ lua_assert(stat != LUA_OK); - lua_xmove(co, L, 1); /* copy error message */ + lua_xmove(co, L, 1); /* move error message to the caller */ } if (stat != LUA_ERRMEM && /* not a memory error and ... */ lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ @@ -179,7 +179,7 @@ static int luaB_close (lua_State *L) { } else { lua_pushboolean(L, 0); - lua_xmove(co, L, 1); /* copy error message */ + lua_xmove(co, L, 1); /* move error message */ return 2; } } diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 1feaab229..30a28828d 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -64,7 +64,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { } else { int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ - /* estimate must be a lower bond of the correct base */ + /* estimate must be a lower bound of the correct base */ lua_assert(i < 0 || (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) @@ -301,7 +301,14 @@ static void collectvalidlines (lua_State *L, Closure *f) { sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < p->sizelineinfo; i++) { /* for all instructions */ + if (!p->is_vararg) /* regular function? */ + i = 0; /* consider all instructions */ + else { /* vararg function */ + lua_assert(p->code[0] == OP_VARARGPREP); + currentline = nextline(p, currentline, 0); + i = 1; /* skip first instruction (OP_VARARGPREP) */ + } + for (; i < p->sizelineinfo; i++) { /* for each instruction */ currentline = nextline(p, currentline, i); /* get its line */ luaH_setint(L, t, currentline, &v); /* table[line] = true */ } @@ -675,9 +682,21 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, } +static const char *formatvarinfo (lua_State *L, const char *kind, + const char *name) { + if (kind == NULL) + return ""; /* no information */ + else + return luaO_pushfstring(L, " (%s '%s')", kind, name); +} + +/* +** Build a string with a "description" for the value 'o', such as +** "variable 'x'" or "upvalue 'y'". +*/ 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; /* to avoid warnings */ const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ @@ -685,26 +704,40 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getobjname(ci_func(ci)->p, currentpc(ci), cast_int(cast(StkId, o) - (ci->func + 1)), &name); } - return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; + return formatvarinfo(L, kind, name); } -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { +/* +** Raise a type error +*/ +static l_noret typeerror (lua_State *L, const TValue *o, const char *op, + const char *extra) { const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); + luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); } +/* +** Raise a type error with "standard" information about the faulty +** object 'o' (using 'varinfo'). +*/ +l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + typeerror(L, o, op, varinfo(L, o)); +} + + +/* +** Raise an error for calling a non-callable object. Try to find +** a name for the object based on the code that made the call +** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'. +*/ l_noret luaG_callerror (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ - const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; - if (what != NULL) { - const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t); - } - else - luaG_typeerror(L, o, "call"); + const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); + typeerror(L, o, "call", extra); } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 7135079b1..f282a773e 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -387,15 +387,18 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ -void luaD_tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); +StkId luaD_tryfuncTM (lua_State *L, StkId func) { + const TValue *tm; StkId p; + checkstackGCp(L, 1, func); /* space for metamethod */ + tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ + return func; } @@ -405,7 +408,7 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static void moveresults (lua_State *L, StkId res, int nres, int wanted) { +l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { StkId firstresult; int i; switch (wanted) { /* handle typical cases separately */ @@ -473,27 +476,81 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) +l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, + int mask, StkId top) { + CallInfo *ci = L->ci = next_ci(L); /* new frame */ + ci->func = func; + ci->nresults = nret; + ci->callstatus = mask; + ci->top = top; + return ci; +} + + +/* +** precall for C functions +*/ +l_sinline int precallC (lua_State *L, StkId func, int nresults, + lua_CFunction f) { + int n; /* number of returns */ + CallInfo *ci; + checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, + L->top + LUA_MINSTACK); + lua_assert(ci->top <= L->stack_last); + if (l_unlikely(L->hookmask & LUA_MASKCALL)) { + int narg = cast_int(L->top - func) - 1; + luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); + } + lua_unlock(L); + n = (*f)(L); /* do the actual call */ + lua_lock(L); + api_checknelems(L, n); + luaD_poscall(L, ci, n); + return n; +} + + /* ** Prepare a function for a tail call, building its call info on top ** of the current call info. 'narg1' is the number of arguments plus 1 -** (so that it includes the function itself). +** (so that it includes the function itself). Return the number of +** results, if it was a C function, or -1 for a Lua function. */ -void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { - Proto *p = clLvalue(s2v(func))->p; - int fsize = p->maxstacksize; /* frame size */ - int nfixparams = p->numparams; - int i; - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); - func = ci->func; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ +int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta) { + retry: + switch (ttypetag(s2v(func))) { + case LUA_VCCL: /* C closure */ + return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f); + case LUA_VLCF: /* light C function */ + return precallC(L, func, LUA_MULTRET, fvalue(s2v(func))); + case LUA_VLCL: { /* Lua function */ + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; + int i; + ci->func -= delta; /* restore 'func' (if vararg) */ + for (i = 0; i < narg1; i++) /* move down function and arguments */ + setobjs2s(L, ci->func + i, func + i); + checkstackGC(L, fsize); + func = ci->func; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + L->top = func + narg1; /* set top */ + return -1; + } + default: { /* not a function */ + func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ + narg1++; + goto retry; /* try again */ + } + } } @@ -506,35 +563,14 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { ** original function position. */ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - f = clCvalue(s2v(func))->f; - goto Cfunc; + precallC(L, func, nresults, clCvalue(s2v(func))->f); + return NULL; case LUA_VLCF: /* light C function */ - f = fvalue(s2v(func)); - Cfunc: { - int n; /* number of returns */ - CallInfo *ci; - checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - L->ci = ci = next_ci(L); - ci->nresults = nresults; - ci->callstatus = CIST_C; - ci->top = L->top + LUA_MINSTACK; - ci->func = func; - lua_assert(ci->top <= L->stack_last); - if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top - func) - 1; - luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); - } - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, n); + precallC(L, func, nresults, fvalue(s2v(func))); return NULL; - } case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; @@ -542,20 +578,16 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); - L->ci = ci = next_ci(L); - ci->nresults = nresults; + L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ - ci->top = func + 1 + fsize; - ci->func = func; - L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); return ci; } default: { /* not a function */ - checkstackGCp(L, 1, func); /* space for metamethod */ - luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + /* return luaD_precall(L, func, nresults); */ goto retry; /* try again with metamethod */ } } @@ -567,7 +599,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** number of recursive invocations in the C stack) or nyci (the same ** plus increment number of non-yieldable calls). */ -static void ccall (lua_State *L, StkId func, int nResults, int inc) { +l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) @@ -728,11 +760,10 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ - ccall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ + ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - luaE_incCstack(L); /* control the C stack */ if (isLua(ci)) { /* yielded inside a hook? */ L->top = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ @@ -783,6 +814,9 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); L->nCcalls = (from) ? getCcalls(from) : 0; + if (getCcalls(L) >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow", nargs); + L->nCcalls++; luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 6bf0ed86f..911e67f66 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -58,11 +58,11 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 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 void luaD_tryfuncTM (lua_State *L, StkId func); +LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 025f1c82c..6c56ba5a4 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -165,6 +165,20 @@ typedef LUAI_UACINT l_uacInt; #endif +/* +** Inline functions +*/ +#if !defined(LUA_USE_C89) +#define l_inline inline +#elif defined(__GNUC__) +#define l_inline __inline__ +#else +#define l_inline /* empty */ +#endif + +#define l_sinline static l_inline + + /* ** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index efb503cc5..34e6a1948 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -68,7 +68,7 @@ typedef struct TValue { #define val_(o) ((o)->value_) -#define valraw(o) (&val_(o)) +#define valraw(o) (val_(o)) /* raw type tag of a TValue */ @@ -112,7 +112,7 @@ typedef struct TValue { #define settt_(o,t) ((o)->tt_=(t)) -/* main macro to copy values (from 'obj1' to 'obj2') */ +/* main macro to copy values (from 'obj2' to 'obj1') */ #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ io1->value_ = io2->value_; settt_(io1, io2->tt_); \ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index d6a47e5af..7c2745159 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -190,7 +190,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* -** grep "ORDER OP" if you change these enums +** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*) +** has extra descriptions in the notes after the enumeration. */ typedef enum { @@ -203,7 +204,7 @@ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ OP_LOADKX,/* A R[A] := K[extra arg] */ OP_LOADFALSE,/* A R[A] := false */ -OP_LFALSESKIP,/*A R[A] := false; pc++ */ +OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */ OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ @@ -254,7 +255,7 @@ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ OP_SHL,/* A B C R[A] := R[B] << R[C] */ OP_SHR,/* A B C R[A] := R[B] >> R[C] */ -OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */ OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ @@ -280,7 +281,7 @@ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ OP_TEST,/* A k if (not R[A] == k) then pc++ */ -OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ @@ -315,6 +316,18 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ /*=========================================================================== Notes: + + (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean + value, in a code equivalent to (not cond ? false : true). (It + produces false and skips the next instruction producing true.) + + (*) Opcodes OP_MMBIN and variants follow each arithmetic and + bitwise opcode. If the operation succeeds, it skips this next + opcode. Otherwise, this opcode calls the corresponding metamethod. + + (*) Opcode OP_TESTSET is used in short-circuit expressions that need + both to jump and to produce a value, such as (a = b or c). + (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then 'top' is set to last_result+1, so next open instruction (OP_CALL, OP_RETURN*, OP_SETLIST) may use 'top'. diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 284ef1f0c..3abe3d751 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -416,6 +416,17 @@ static void markupval (FuncState *fs, int level) { } +/* +** Mark that current block has a to-be-closed variable. +*/ +static void marktobeclosed (FuncState *fs) { + BlockCnt *bl = fs->bl; + bl->upval = 1; + bl->insidetbc = 1; + fs->needclose = 1; +} + + /* ** Find a variable with the given name 'n'. If it is an upvalue, add ** this upvalue into all intermediate functions. If it is a global, set @@ -1599,7 +1610,7 @@ static void forlist (LexState *ls, TString *indexname) { line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 4); /* control variables */ - markupval(fs, fs->nactvar); /* last control var. must be closed */ + marktobeclosed(fs); /* last control var. must be closed */ luaK_checkstack(fs, 3); /* extra space to call generator */ forbody(ls, base, line, nvars - 4, 1); } @@ -1703,11 +1714,9 @@ static int getlocalattribute (LexState *ls) { } -static void checktoclose (LexState *ls, int level) { +static void checktoclose (FuncState *fs, int level) { if (level != -1) { /* is there a to-be-closed variable? */ - FuncState *fs = ls->fs; - markupval(fs, level + 1); - fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ + marktobeclosed(fs); luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); } } @@ -1751,7 +1760,7 @@ static void localstat (LexState *ls) { adjust_assign(ls, nvars, nexps, &e); adjustlocalvars(ls, nvars); } - checktoclose(ls, toclose); + checktoclose(fs, toclose); } @@ -1776,6 +1785,7 @@ static void funcstat (LexState *ls, int line) { luaX_next(ls); /* skip FUNCTION */ ismethod = funcname(ls, &v); body(ls, &b, ismethod, line); + check_readonly(ls, &v); luaK_storevar(ls->fs, &v, &b); luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index f79255a03..846162a59 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -166,7 +166,7 @@ void luaE_checkcstack (lua_State *L) { if (getCcalls(L) == LUAI_MAXCCALLS) luaG_runerror(L, "C stack overflow"); else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ } @@ -269,7 +269,7 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); if (!completestate(g)) /* closing a partially built state? */ - luaC_freeallobjects(L); /* jucst collect its objects */ + luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ @@ -330,13 +330,13 @@ int luaE_resetthread (lua_State *L, int status) { ci->callstatus = CIST_C; if (status == LUA_YIELD) status = LUA_OK; + L->status = LUA_OK; /* so it can run __close metamethods */ status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); else L->top = L->stack + 1; ci->top = L->top + LUA_MINSTACK; - L->status = cast_byte(status); luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); return status; } diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 34a9b2e89..9f0e41cb9 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -165,7 +165,7 @@ typedef struct stringtable { ** - field 'nyield' is used only while a function is "doing" an ** yield (from the yield until the next resume); ** - field 'nres' is used only while closing tbc variables when -** returning from a C function; +** returning from a function; ** - field 'transferinfo' is used only during call/returnhooks, ** before the function starts or after it ends. */ diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 47e5b27a6..0b4fdbb7b 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1090,13 +1090,31 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* valid flags in a format specification */ -#if !defined(L_FMTFLAGS) -#define L_FMTFLAGS "-+ #0" +#if !defined(L_FMTFLAGSF) + +/* valid flags for a, A, e, E, f, F, g, and G conversions */ +#define L_FMTFLAGSF "-+#0 " + +/* valid flags for o, x, and X conversions */ +#define L_FMTFLAGSX "-#0" + +/* valid flags for d and i conversions */ +#define L_FMTFLAGSI "-+0 " + +/* valid flags for u conversions */ +#define L_FMTFLAGSU "-0" + +/* valid flags for c, p, and s conversions */ +#define L_FMTFLAGSC "-" + #endif /* -** maximum size of each format specification (such as "%-099.99d") +** Maximum size of each format specification (such as "%-099.99d"): +** Initial '%', flags (up to 5), width (2), period, precision (2), +** length modifier (8), conversion specifier, and final '\0', plus some +** extra. */ #define MAX_FORMAT 32 @@ -1189,25 +1207,53 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { } -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ +static const char *get2digits (const char *s) { + if (isdigit(uchar(*s))) { + s++; + if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ + } + return s; +} + + +/* +** Check whether a conversion specification is valid. When called, +** first character in 'form' must be '%' and last character must +** be a valid conversion specifier. 'flags' are the accepted flags; +** 'precision' signals whether to accept a precision. +*/ +static void checkformat (lua_State *L, const char *form, const char *flags, + int precision) { + const char *spec = form + 1; /* skip '%' */ + spec += strspn(spec, flags); /* skip flags */ + if (*spec != '0') { /* a width cannot start with '0' */ + spec = get2digits(spec); /* skip width */ + if (*spec == '.' && precision) { + spec++; + spec = get2digits(spec); /* skip precision */ + } } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); + if (!isalpha(uchar(*spec))) /* did not go to the end? */ + luaL_error(L, "invalid conversion specification: '%s'", form); +} + + +/* +** Get a conversion specification and copy it to 'form'. +** Return the address of its last character. +*/ +static const char *getformat (lua_State *L, const char *strfrmt, + char *form) { + /* spans flags, width, and precision ('0' is included as a flag) */ + size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789."); + len++; /* adds following character (should be the specifier) */ + /* still needs space for '%', '\0', plus a length modifier */ + if (len >= MAX_FORMAT - 10) + luaL_error(L, "invalid format (too long)"); *(form++) = '%'; - memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); - form += (p - strfrmt) + 1; - *form = '\0'; - return p; + memcpy(form, strfrmt, len * sizeof(char)); + *(form + len) = '\0'; + return strfrmt + len - 1; } @@ -1230,6 +1276,7 @@ static int str_format (lua_State *L) { size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; + const char *flags; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { @@ -1239,25 +1286,35 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - int maxitem = MAX_ITEM; - char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */ - int nb = 0; /* number of bytes in added item */ + int maxitem = MAX_ITEM; /* maximum length for the result */ + char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ + int nb = 0; /* number of bytes in result */ if (++arg > top) return luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); + strfrmt = getformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { + checkformat(L, form, L_FMTFLAGSC, 0); nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': - case 'o': case 'u': case 'x': case 'X': { + flags = L_FMTFLAGSI; + goto intcase; + case 'u': + flags = L_FMTFLAGSU; + goto intcase; + case 'o': case 'x': case 'X': + flags = L_FMTFLAGSX; + intcase: { lua_Integer n = luaL_checkinteger(L, arg); + checkformat(L, form, flags, 1); addlenmod(form, LUA_INTEGER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); @@ -1268,12 +1325,14 @@ static int str_format (lua_State *L) { /* FALLTHROUGH */ case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { const void *p = lua_topointer(L, arg); + checkformat(L, form, L_FMTFLAGSC, 0); if (p == NULL) { /* avoid calling 'printf' with argument NULL */ p = "(null)"; /* result */ form[strlen(form) - 1] = 's'; /* format it as a string */ @@ -1294,7 +1353,8 @@ static int str_format (lua_State *L) { luaL_addvalue(&b); /* keep entire string */ else { luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); - if (!strchr(form, '.') && l >= 100) { + checkformat(L, form, L_FMTFLAGSC, 1); + if (strchr(form, '.') == NULL && l >= 100) { /* no precision and string is too long to be formatted */ luaL_addvalue(&b); /* keep entire string */ } @@ -1352,15 +1412,6 @@ static const union { } 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)) - - /* ** information to pack/unpack stuff */ @@ -1435,6 +1486,8 @@ static void initheader (lua_State *L, Header *h) { ** Read and classify next option. 'size' is filled with option's size. */ static KOption getoption (Header *h, const char **fmt, int *size) { + /* dummy structure to get native alignment requirements */ + struct cD { char c; union { LUAI_MAXALIGN; } u; }; int opt = *((*fmt)++); *size = 0; /* default */ switch (opt) { @@ -1465,7 +1518,11 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case '<': h->islittle = 1; break; case '>': h->islittle = 0; break; case '=': h->islittle = nativeendian.little; break; - case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; + case '!': { + const int maxalign = offsetof(struct cD, u); + h->maxalign = getnumlimit(h, fmt, maxalign); + break; + } default: luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 50e5a721b..95cae18e2 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -84,8 +84,6 @@ #define hashstr(t,str) hashpow2(t, (str)->hash) #define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - #define hashpointer(t,p) hashmod(t, point2uint(p)) @@ -101,6 +99,20 @@ static const Node dummynode_ = { static const TValue absentkey = {ABSTKEYCONSTANT}; +/* +** Hash for integers. To allow a good hash, use the remainder operator +** ('%'). If integer fits as a non-negative int, compute an int +** remainder, which is faster. Otherwise, use an unsigned-integer +** remainder, which uses all bits and ensures a non-negative result. +*/ +static Node *hashint (const Table *t, lua_Integer i) { + lua_Unsigned ui = l_castS2U(i); + if (ui <= (unsigned int)INT_MAX) + return hashmod(t, cast_int(ui)); + else + return hashmod(t, ui); +} + /* ** Hash for floating-point numbers. @@ -138,22 +150,22 @@ static int l_hashfloat (lua_Number n) { ** and value in 'vkl') so that we can call it on keys inserted into ** nodes. */ -static Node *mainposition (const Table *t, int ktt, const Value *kvl) { +static Node *mainposition (const Table *t, int ktt, const Value kvl) { switch (withvariant(ktt)) { case LUA_VNUMINT: { - lua_Integer key = ivalueraw(*kvl); + lua_Integer key = ivalueraw(kvl); return hashint(t, key); } case LUA_VNUMFLT: { - lua_Number n = fltvalueraw(*kvl); + lua_Number n = fltvalueraw(kvl); return hashmod(t, l_hashfloat(n)); } case LUA_VSHRSTR: { - TString *ts = tsvalueraw(*kvl); + TString *ts = tsvalueraw(kvl); return hashstr(t, ts); } case LUA_VLNGSTR: { - TString *ts = tsvalueraw(*kvl); + TString *ts = tsvalueraw(kvl); return hashpow2(t, luaS_hashlongstr(ts)); } case LUA_VFALSE: @@ -161,15 +173,15 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { case LUA_VTRUE: return hashboolean(t, 1); case LUA_VLIGHTUSERDATA: { - void *p = pvalueraw(*kvl); + void *p = pvalueraw(kvl); return hashpointer(t, p); } case LUA_VLCF: { - lua_CFunction f = fvalueraw(*kvl); + lua_CFunction f = fvalueraw(kvl); return hashpointer(t, f); } default: { - GCObject *o = gcvalueraw(*kvl); + GCObject *o = gcvalueraw(kvl); return hashpointer(t, o); } } @@ -683,7 +695,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { return; } lua_assert(!isdummy(t)); - othern = mainposition(t, keytt(mp), &keyval(mp)); + othern = mainposition(t, keytt(mp), keyval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index d80eb8015..868d78fd8 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -59,8 +59,9 @@ static void checktab (lua_State *L, int arg, int what) { static int tinsert (lua_State *L) { - lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ + lua_Integer e = aux_getn(L, 1, TAB_RW); + e = luaL_intop(+, e, 1); /* first empty element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ @@ -147,7 +148,7 @@ static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", - luaL_typename(L, -1), i); + luaL_typename(L, -1), (LUAI_UACINT)i); luaL_addvalue(b); } diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 46b48dba9..0f1900444 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -89,14 +89,15 @@ static void print_usage (const char *badoption) { lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" - " -e stat execute string 'stat'\n" - " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name' into global 'name'\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -W turn warnings on\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" + " -e stat execute string 'stat'\n" + " -i enter interactive mode after executing 'script'\n" + " -l mod require library 'mod' into global 'mod'\n" + " -l g=mod require library 'mod' into global 'g'\n" + " -v show version information\n" + " -E ignore environment variables\n" + " -W turn warnings on\n" + " -- stop handling options\n" + " - stop handling options and execute stdin\n" , progname); } @@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* -** Calls 'require(name)' and stores the result in a global variable -** with the given name. +** Receives 'globname[=modname]' and runs 'globname = require(modname)'. */ -static int dolibrary (lua_State *L, const char *name) { +static int dolibrary (lua_State *L, char *globname) { int status; + char *modname = strchr(globname, '='); + if (modname == NULL) /* no explicit name? */ + modname = globname; /* module name is equal to global name */ + else { + *modname = '\0'; /* global name ends here */ + modname++; /* module name starts after the '=' */ + } lua_getglobal(L, "require"); - lua_pushstring(L, name); - status = docall(L, 1, 1); /* call 'require(name)' */ + lua_pushstring(L, modname); + status = docall(L, 1, 1); /* call 'require(modname)' */ if (status == LUA_OK) - lua_setglobal(L, name); /* global[name] = require return */ + lua_setglobal(L, globname); /* globname = require(modname) */ return report(L, status); } @@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) { switch (option) { case 'e': case 'l': { int status; - const char *extra = argv[i] + 2; /* both options need an argument */ + char *extra = argv[i] + 2; /* both options need an argument */ if (*extra == '\0') extra = argv[++i]; lua_assert(extra != NULL); status = (option == 'e') diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 9abe3f803..546c9b195 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,10 +18,10 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "3" +#define LUA_VERSION_RELEASE "4" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 56ddc4148..f6db9cf65 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -155,6 +155,7 @@ static const Proto* combine(lua_State* L, int n) f->p[i]=toproto(L,i-n-1); if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; } + luaM_freearray(L,f->lineinfo,f->sizelineinfo); f->sizelineinfo=0; return f; } @@ -600,11 +601,11 @@ static void PrintCode(const Proto* f) if (c==0) printf("all out"); else printf("%d out",c-1); break; case OP_TAILCALL: - printf("%d %d %d",a,b,c); + printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT "%d in",b-1); break; case OP_RETURN: - printf("%d %d %d",a,b,c); + printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT); if (b==0) printf("all out"); else printf("%d out",b-1); break; @@ -619,7 +620,7 @@ static void PrintCode(const Proto* f) break; case OP_FORPREP: printf("%d %d",a,bx); - printf(COMMENT "to %d",pc+bx+2); + printf(COMMENT "exit to %d",pc+bx+3); break; case OP_TFORPREP: printf("%d %d",a,bx); diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index e64d2ee39..d42d14b7d 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -485,7 +485,6 @@ @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. -@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -506,9 +505,6 @@ #define LUA_UNSIGNED unsigned LUAI_UACINT -#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) - - /* now the variable definitions */ #if LUA_INT_TYPE == LUA_INT_INT /* { int */ diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index 901d985f8..e7bf098f6 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -224,14 +224,11 @@ static int byteoffset (lua_State *L) { static int iter_aux (lua_State *L, int strict) { 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 */ + lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); + if (n < len) { + while (iscont(s + n)) n++; /* skip continuation bytes */ } - if (n >= (lua_Integer)len) + if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { utfint code; diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index cf6bc2b2c..4d6d3db96 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -408,7 +408,7 @@ static int l_strcmp (const TString *ls, const TString *rs) { ** from float to int.) ** When 'f' is NaN, comparisons must result in false. */ -static int LTintfloat (lua_Integer i, lua_Number f) { +l_sinline int LTintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numlt(cast_num(i), f); /* compare them as floats */ else { /* i < f <=> i < ceil(f) */ @@ -425,7 +425,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) { ** 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) { +l_sinline int LEintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numle(cast_num(i), f); /* compare them as floats */ else { /* i <= f <=> i <= floor(f) */ @@ -442,7 +442,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) { ** Check whether float 'f' is less than integer 'i'. ** See comments on previous function. */ -static int LTfloatint (lua_Number f, lua_Integer i) { +l_sinline int LTfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numlt(f, cast_num(i)); /* compare them as floats */ else { /* f < i <=> floor(f) < i */ @@ -459,7 +459,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) { ** Check whether float 'f' is less than or equal to integer 'i'. ** See comments on previous function. */ -static int LEfloatint (lua_Number f, lua_Integer i) { +l_sinline int LEfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numle(f, cast_num(i)); /* compare them as floats */ else { /* f <= i <=> ceil(f) <= i */ @@ -475,7 +475,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) { /* ** Return 'l < r', for numbers. */ -static int LTnum (const TValue *l, const TValue *r) { +l_sinline int LTnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); @@ -497,7 +497,7 @@ static int LTnum (const TValue *l, const TValue *r) { /* ** Return 'l <= r', for numbers. */ -static int LEnum (const TValue *l, const TValue *r) { +l_sinline int LEnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); @@ -768,7 +768,8 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* ** Shift left operation. (Shift right just negates 'y'.) */ -#define luaV_shiftr(x,y) luaV_shiftl(x,-(y)) +#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) + lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ @@ -849,10 +850,19 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ break; } - case OP_CLOSE: case OP_RETURN: { /* yielded closing variables */ + case OP_CLOSE: { /* yielded closing variables */ ci->u.l.savedpc--; /* repeat instruction to close other vars. */ break; } + case OP_RETURN: { /* yielded closing variables */ + StkId ra = base + GETARG_A(inst); + /* adjust top to signal correct number of returns, in case the + return is "up to top" ('isIT') */ + L->top = ra + ci->u2.nres; + /* repeat instruction to close other vars. and complete the return */ + ci->u.l.savedpc--; + break; + } default: { /* only these other opcodes can yield */ lua_assert(op == OP_TFORCALL || op == OP_CALL || @@ -1101,7 +1111,7 @@ void luaV_finishOp (lua_State *L) { #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that can only raise errors. (That is, it cannnot change +** Protect code that can only raise errors. (That is, it cannot change ** the stack or hooks.) */ #define halfProtect(exp) (savestate(L,ci), (exp)) @@ -1158,8 +1168,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ vmfetch(); -// low-level line tracing for debugging Lua -// printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); + #if 0 + /* low-level line tracing for debugging Lua */ + printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); + #endif lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ @@ -1627,13 +1639,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ ci = newci; - ci->callstatus = 0; /* call re-uses 'luaV_execute' */ goto startfunc; } vmbreak; } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ + int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; @@ -1647,23 +1659,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - while (!ttisfunction(s2v(ra))) { /* not a function? */ - luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ - b++; /* there is now one extra argument */ - checkstackGCp(L, 1, ra); - } - if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_precall(L, ra, LUA_MULTRET); /* call it */ - updatetrap(ci); - updatestack(ci); /* stack may have been relocated */ + if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ + goto startfunc; /* execute the callee */ + else { /* C function? */ ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto startfunc; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ @@ -1672,6 +1675,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { n = cast_int(L->top - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { /* may there be open upvalues? */ + ci->u2.nres = n; /* save number of returns */ if (L->top < ci->top) L->top = ci->top; luaF_close(L, base, CLOSEKTOP, 1); From 350248a7cc1102356584ef6f2a6c602d8d42c52c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Nov 2021 13:11:10 +0800 Subject: [PATCH 404/565] fix #1504 --- lualib/skynet/db/mysql.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index ee6f19df7..c165d7941 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -599,7 +599,7 @@ local function _compose_stmt_execute(self, stmt, cursor_type, args) local v = args[i] f = store_types[type(v)] if not f then - error("invalid parameter type", type(v)) + error("invalid parameter type " .. type(v)) end ts, vs = f(v) types_buf = types_buf .. ts @@ -741,7 +741,7 @@ function _M.connect(opts) local user = opts.user or "" local password = opts.password or "" local charset = CHARSET_MAP[opts.charset or "_default"] - local channel = + local channel = socketchannel.channel { host = opts.host, port = opts.port or 3306, From 1e2ed7e6ff7d86d7cc6395933e2f327a403a8e29 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Nov 2021 15:55:14 +0800 Subject: [PATCH 405/565] Use table.pack to support nil in table, See #1505 --- lualib/skynet/db/mysql.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index c165d7941..1b5cd7219 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -565,7 +565,7 @@ store_types["nil"] = function(v) end local function _compose_stmt_execute(self, stmt, cursor_type, args) - local arg_num = #args + local arg_num = args.n if arg_num ~= stmt.param_count then error("require stmt.param_count " .. stmt.param_count .. " get arg_num " .. arg_num) end @@ -1034,7 +1034,7 @@ end err ]] function _M.execute(self, stmt, ...) - local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, {...}) + local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, table.pack(...)) if not querypacket then return { badresult = true, From 53292d1c93fcfc0ac7c861d6506c7acddd07c866 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Thu, 18 Nov 2021 18:42:33 +0800 Subject: [PATCH 406/565] update mysql (#1507) --- lualib/skynet/db/mysql.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 1b5cd7219..a61afd5fa 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -165,25 +165,25 @@ local function _set_byte2(n) return strpack(" Date: Fri, 19 Nov 2021 08:48:27 +0800 Subject: [PATCH 407/565] Fix #1506 --- service-src/service_harbor.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 4e337344e..f10c13020 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -233,6 +233,7 @@ close_harbor(struct harbor *h, int id) { s->status = STATUS_DOWN; if (s->fd) { skynet_socket_close(h->ctx, s->fd); + s->fd = 0; } if (s->queue) { release_queue(s->queue); @@ -256,17 +257,20 @@ harbor_create(void) { return h; } -void -harbor_release(struct harbor *h) { + +static void +close_all_remotes(struct harbor *h) { int i; for (i=1;is[i]; - if (s->fd && s->status != STATUS_DOWN) { - close_harbor(h,i); - // don't call report_harbor_down. - // never call skynet_send during module exit, because of dead lock - } + close_harbor(h,i); + // don't call report_harbor_down. + // never call skynet_send during module exit, because of dead lock } +} + +void +harbor_release(struct harbor *h) { + close_all_remotes(h); hash_delete(h->map); skynet_free(h); } @@ -534,7 +538,7 @@ remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int 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_send(context, destination, source, PTYPE_ERROR, session, 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) { @@ -735,6 +739,9 @@ harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { } h->id = harbor_id; h->slave = slave; + if (harbor_id == 0) { + close_all_remotes(h); + } skynet_callback(ctx, h, mainloop); skynet_harbor_start(ctx); From 839975415d213e8d338d671f6e42a9c5cab5b4b8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Dec 2021 19:31:36 +0800 Subject: [PATCH 408/565] raise socket error during auth --- lualib/skynet/socketchannel.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 521d98325..d76318184 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -418,6 +418,12 @@ local function block_connect(self, once) if #self.__connecting > 0 then -- connecting in other coroutine local co = coroutine.running() + for _, v in ipairs(self.__connecting) do + if v == co then + -- socket closed during auth (connecting) , See Issue #1513 + error(socket_error) + end + end table.insert(self.__connecting, co) skynet.wait(co) else From 3b54b3bd800ded994d55ac81c7548d4601538d79 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Dec 2021 20:26:46 +0800 Subject: [PATCH 409/565] Revert "raise socket error during auth" This reverts commit 839975415d213e8d338d671f6e42a9c5cab5b4b8. --- lualib/skynet/socketchannel.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index d76318184..521d98325 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -418,12 +418,6 @@ local function block_connect(self, once) if #self.__connecting > 0 then -- connecting in other coroutine local co = coroutine.running() - for _, v in ipairs(self.__connecting) do - if v == co then - -- socket closed during auth (connecting) , See Issue #1513 - error(socket_error) - end - end table.insert(self.__connecting, co) skynet.wait(co) else From 208be03a6facb368892f7b0a85a2ae2850db692c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Dec 2021 20:48:51 +0800 Subject: [PATCH 410/565] Disconnected during auth, See #1513 --- lualib/skynet/socketchannel.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 521d98325..3054de43f 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -388,13 +388,17 @@ end local function check_connection(self) if self.__sock then + local authco = self.__authcoroutine if socket.disconnected(self.__sock[1]) then -- closed by peer skynet.error("socket: disconnect detected ", self.__host, self.__port) close_channel_socket(self) + if authco and authco == coroutine.running() then + -- disconnected during auth, See #1513 + return false + end return end - local authco = self.__authcoroutine if not authco then return true end From 9d3edae7976acc03facd38a97d71bb7e42a4f512 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Dec 2021 17:01:50 +0800 Subject: [PATCH 411/565] Check self.__sock before dispatch, See #1513 --- lualib/skynet/socketchannel.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 3054de43f..251ecc28e 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -321,7 +321,10 @@ local function connect_once(self) self.__sock = setmetatable( {fd} , channel_socket_meta ) self.__dispatch_thread = skynet.fork(function() - pcall(dispatch_function(self), self) + if self.__sock then + -- self.__sock can be false (socket closed) if error during connecting, See #1513 + pcall(dispatch_function(self), self) + end -- clear dispatch_thread self.__dispatch_thread = nil end) From 5b5d5906cbd76f058302ca526f5943fde5737501 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Dec 2021 11:11:25 +0800 Subject: [PATCH 412/565] Update Lua lua 5.4.4 rc2 --- 3rd/lua/lapi.c | 17 +++++++------- 3rd/lua/lbaselib.c | 19 ++++++++++++++-- 3rd/lua/ldebug.c | 56 ++++++++++++++++++++++++++-------------------- 3rd/lua/ldo.c | 2 +- 3rd/lua/lgc.c | 13 ++++++----- 3rd/lua/lgc.h | 9 ++++++++ 3rd/lua/llimits.h | 2 +- 3rd/lua/lstate.c | 5 +++-- 3rd/lua/lstate.h | 4 ++-- 3rd/lua/ltable.c | 35 +++++++++++++---------------- 10 files changed, 98 insertions(+), 64 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index f91769be7..6a28c54fb 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1180,18 +1180,19 @@ LUA_API int lua_status (lua_State *L) { LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; - global_State *g; + global_State *g = G(L); + if (g->gcstp & GCSTPGC) /* internal stop? */ + return -1; /* all options are invalid when stopped */ lua_lock(L); - g = G(L); va_start(argp, what); switch (what) { case LUA_GCSTOP: { - g->gcrunning = 0; + g->gcstp = GCSTPUSR; /* stopped by the user */ break; } case LUA_GCRESTART: { luaE_setdebt(g, 0); - g->gcrunning = 1; + g->gcstp = 0; /* (GCSTPGC must be already zero here) */ break; } case LUA_GCCOLLECT: { @@ -1210,8 +1211,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCSTEP: { int data = va_arg(argp, int); l_mem debt = 1; /* =1 to signal that it did an actual step */ - lu_byte oldrunning = g->gcrunning; - g->gcrunning = 1; /* allow GC to run */ + lu_byte oldstp = g->gcstp; + g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ if (data == 0) { luaE_setdebt(g, 0); /* do a basic step */ luaC_step(L); @@ -1221,7 +1222,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { luaE_setdebt(g, debt); luaC_checkGC(L); } - g->gcrunning = oldrunning; /* restore previous state */ + g->gcstp = oldstp; /* restore previous state */ if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ break; @@ -1239,7 +1240,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCISRUNNING: { - res = g->gcrunning; + res = gcrunning(g); break; } case LUA_GCGEN: { diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 912c4cc63..1d60c9ded 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -182,12 +182,20 @@ static int luaB_rawset (lua_State *L) { static int pushmode (lua_State *L, int oldmode) { - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" - : "generational"); + if (oldmode == -1) + luaL_pushfail(L); /* invalid call to 'lua_gc' */ + else + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" + : "generational"); return 1; } +/* +** check whether call to 'lua_gc' was valid (not inside a finalizer) +*/ +#define checkvalres(res) { if (res == -1) break; } + static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", @@ -200,12 +208,14 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCCOUNT: { int k = lua_gc(L, o); int b = lua_gc(L, LUA_GCCOUNTB); + checkvalres(k); lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); return 1; } case LUA_GCSTEP: { int step = (int)luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, step); + checkvalres(res); lua_pushboolean(L, res); return 1; } @@ -213,11 +223,13 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCSETSTEPMUL: { int p = (int)luaL_optinteger(L, 2, 0); int previous = lua_gc(L, o, p); + checkvalres(previous); lua_pushinteger(L, previous); return 1; } case LUA_GCISRUNNING: { int res = lua_gc(L, o); + checkvalres(res); lua_pushboolean(L, res); return 1; } @@ -234,10 +246,13 @@ static int luaB_collectgarbage (lua_State *L) { } default: { int res = lua_gc(L, o); + checkvalres(res); lua_pushinteger(L, res); return 1; } } + luaL_pushfail(L); /* invalid call (inside a finalizer) */ + return 1; } diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 30a28828d..a716d95e2 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -34,8 +34,8 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name); +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name); static int currentpc (CallInfo *ci) { @@ -304,7 +304,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { if (!p->is_vararg) /* regular function? */ i = 0; /* consider all instructions */ else { /* vararg function */ - lua_assert(p->code[0] == OP_VARARGPREP); + lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); currentline = nextline(p, currentline, 0); i = 1; /* skip first instruction (OP_VARARGPREP) */ } @@ -317,15 +317,9 @@ 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); + /* calling function is a known function? */ + if (ci != NULL && !(ci->callstatus & CIST_TAIL)) + return funcnamefromcall(L, ci->previous, name); else return NULL; /* no way to find a name */ } @@ -597,16 +591,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, ** 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) { +static const char *funcnamefromcode (lua_State *L, const Proto *p, + int pc, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - const Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ 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: @@ -643,6 +631,26 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, return "metamethod"; } + +/* +** Try to find a name for a function based on how it was called. +*/ +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name) { + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + *name = "?"; + return "hook"; + } + else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */ + *name = "__gc"; + return "metamethod"; /* report it as such */ + } + else if (isLua(ci)) + return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name); + else + return NULL; +} + /* }====================================================== */ @@ -728,14 +736,14 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { /* -** Raise an error for calling a non-callable object. Try to find -** a name for the object based on the code that made the call -** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'. +** Raise an error for calling a non-callable object. Try to find a name +** for the object based on how it was called ('funcnamefromcall'); if it +** cannot get a name there, try 'varinfo'. */ l_noret luaG_callerror (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ - const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + const char *kind = funcnamefromcall(L, ci, &name); const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); typeerror(L, o, "call", extra); } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index f282a773e..a48e35f9d 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -530,10 +530,10 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int fsize = p->maxstacksize; /* frame size */ int nfixparams = p->numparams; int i; + checkstackGCp(L, fsize - delta, func); ci->func -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); func = ci->func; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index e90766dc0..3d509b142 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -908,18 +908,18 @@ static void GCTM (lua_State *L) { if (!notm(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; - int running = g->gcrunning; + int oldgcstp = g->gcstp; + g->gcstp = GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ - g->gcrunning = 0; /* avoid GC steps */ setobj2s(L, L->top++, tm); /* push finalizer... */ setobj2s(L, L->top++, &v); /* ... and its argument */ 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 */ + g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ - luaE_warnerror(L, "__gc metamethod"); + luaE_warnerror(L, "__gc"); L->top--; /* pops error object */ } } @@ -1508,9 +1508,11 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); + g->gcstp = GCSTPGC; luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); + g->gcstp = 0; callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(g->mainthread)); deletelist(L, g->finobj, NULL); @@ -1653,6 +1655,7 @@ void luaC_runtilstate (lua_State *L, int statesmask) { } + /* ** Performs a basic incremental step. The debt and step size are ** converted from bytes to "units of work"; then the function loops @@ -1684,7 +1687,7 @@ static void incstep (lua_State *L, global_State *g) { void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); - if (g->gcrunning) { /* running? */ + if (gcrunning(g)) { /* running? */ if(isdecGCmodegen(g)) genstep(L, g); else diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 62a44e1a6..cd77ddf16 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -153,6 +153,15 @@ */ #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) + +/* +** Control when GC is running: +*/ +#define GCSTPUSR 1 /* bit true when GC stopped by user */ +#define GCSTPGC 2 /* bit true when GC stopped by itself */ +#define gcrunning(g) ((g)->gcstp == 0) + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 6c56ba5a4..52a32f92e 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -361,7 +361,7 @@ typedef l_uint32 Instruction; #define condchangemem(L,pre,pos) ((void)0) #else #define condchangemem(L,pre,pos) \ - { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } + { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } #endif #endif diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 846162a59..365bcdd6b 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -236,7 +236,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaS_init(L); luaT_init(L); luaX_init(L); - g->gcrunning = 1; /* allow gc */ + g->gcstp = 0; /* allow gc */ setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } @@ -271,6 +271,7 @@ static void close_state (lua_State *L) { if (!completestate(g)) /* closing a partially built state? */ luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ + L->ci = &L->base_ci; /* unwind CallInfo list */ luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); @@ -371,7 +372,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->warnf = NULL; g->ud_warn = NULL; g->mainthread = L; - g->gcrunning = 0; /* no GC while building state */ + g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 9f0e41cb9..b18568e90 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -209,7 +209,7 @@ typedef struct CallInfo { #define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_FIN (1<<7) /* function "called" a finalizer */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ #define CIST_CLSRET (1<<9) /* function is closing tbc variables */ /* Bits 10-12 are used for CIST_RECST (see below) */ @@ -262,7 +262,7 @@ typedef struct global_State { lu_byte gcstopem; /* stops emergency collections */ lu_byte genminormul; /* control for minor generational collections */ lu_byte genmajormul; /* control for major generational collections */ - lu_byte gcrunning; /* true if GC is running */ + lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ lu_byte gcpause; /* size of pause between successive GCs */ lu_byte gcstepmul; /* GC "speed" */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 95cae18e2..045155ea0 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -146,26 +146,24 @@ static int l_hashfloat (lua_Number n) { /* ** returns the 'main' position of an element in a table (that is, -** the index of its hash value). The key comes broken (tag in 'ktt' -** and value in 'vkl') so that we can call it on keys inserted into -** nodes. +** the index of its hash value). */ -static Node *mainposition (const Table *t, int ktt, const Value kvl) { - switch (withvariant(ktt)) { +static Node *mainpositionTV (const Table *t, const TValue *key) { + switch (ttypetag(key)) { case LUA_VNUMINT: { - lua_Integer key = ivalueraw(kvl); - return hashint(t, key); + lua_Integer i = ivalue(key); + return hashint(t, i); } case LUA_VNUMFLT: { - lua_Number n = fltvalueraw(kvl); + lua_Number n = fltvalue(key); return hashmod(t, l_hashfloat(n)); } case LUA_VSHRSTR: { - TString *ts = tsvalueraw(kvl); + TString *ts = tsvalue(key); return hashstr(t, ts); } case LUA_VLNGSTR: { - TString *ts = tsvalueraw(kvl); + TString *ts = tsvalue(key); return hashpow2(t, luaS_hashlongstr(ts)); } case LUA_VFALSE: @@ -173,26 +171,25 @@ static Node *mainposition (const Table *t, int ktt, const Value kvl) { case LUA_VTRUE: return hashboolean(t, 1); case LUA_VLIGHTUSERDATA: { - void *p = pvalueraw(kvl); + void *p = pvalue(key); return hashpointer(t, p); } case LUA_VLCF: { - lua_CFunction f = fvalueraw(kvl); + lua_CFunction f = fvalue(key); return hashpointer(t, f); } default: { - GCObject *o = gcvalueraw(kvl); + GCObject *o = gcvalue(key); return hashpointer(t, o); } } } -/* -** Returns the main position of an element given as a 'TValue' -*/ -static Node *mainpositionTV (const Table *t, const TValue *key) { - return mainposition(t, rawtt(key), valraw(key)); +l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { + TValue key; + getnodekey(cast(lua_State *, NULL), &key, nd); + return mainpositionTV(t, &key); } @@ -695,7 +692,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { return; } lua_assert(!isdummy(t)); - othern = mainposition(t, keytt(mp), keyval(mp)); + othern = mainpositionfromnode(t, mp); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ From 8304747d8eeaac588bd6094ce65a1456fcdec645 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Jan 2022 10:22:31 +0800 Subject: [PATCH 413/565] Lua 5.4.4 rc3 --- 3rd/lua/lcode.c | 2 +- 3rd/lua/lgc.c | 10 +++++----- 3rd/lua/lgc.h | 1 + 3rd/lua/lmathlib.c | 4 ++-- 3rd/lua/lobject.c | 10 +++++----- 3rd/lua/lua.h | 4 ++-- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 9cba24f9c..06425a1db 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -607,7 +607,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { return addk(fs, &o, &o); /* use number itself as key */ else { /* must build an alternative key */ const int nbm = l_floatatt(MANT_DIG); - const lua_Number q = l_mathop(ldexp)(1.0, -nbm + 1); + const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ TValue kv; setfltvalue(&kv, k); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 3d509b142..510d5f622 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -909,7 +909,7 @@ static void GCTM (lua_State *L) { int status; lu_byte oldah = L->allowhook; int oldgcstp = g->gcstp; - g->gcstp = GCSTPGC; /* avoid GC steps */ + g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ setobj2s(L, L->top++, tm); /* push finalizer... */ setobj2s(L, L->top++, &v); /* ... and its argument */ @@ -1013,7 +1013,8 @@ static void correctpointers (global_State *g, GCObject *o) { void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); if (tofinalize(o) || /* obj. is already marked... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ + gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */ + (g->gcstp & GCSTPCLS)) /* or closing state? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; @@ -1508,14 +1509,13 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); - g->gcstp = GCSTPGC; + g->gcstp = GCSTPCLS; /* no extra finalizers after here */ luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - g->gcstp = 0; callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(g->mainthread)); - deletelist(L, g->finobj, NULL); + lua_assert(g->finobj == NULL); /* no new finalizers */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index cd77ddf16..275bad3b1 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -159,6 +159,7 @@ */ #define GCSTPUSR 1 /* bit true when GC stopped by user */ #define GCSTPGC 2 /* bit true when GC stopped by itself */ +#define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 5f5983a43..e0c61a168 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -475,7 +475,7 @@ static lua_Number I2d (Rand64 x) { /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ - ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33))) + (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) /* ** use FIGS - 32 bits from lower half, throwing out the other @@ -486,7 +486,7 @@ static lua_Number I2d (Rand64 x) { /* ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) */ -#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0) +#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0)) static lua_Number I2d (Rand64 x) { diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 0e504be03..301aa900b 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -164,7 +164,7 @@ static int isneg (const char **s) { */ static lua_Number lua_strx2number (const char *s, char **endptr) { int dot = lua_getlocaledecpoint(); - lua_Number r = 0.0; /* result (accumulator) */ + lua_Number r = l_mathop(0.0); /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ @@ -174,7 +174,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return 0.0; /* invalid format (no '0x') */ + return l_mathop(0.0); /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ if (*s == dot) { if (hasdot) break; /* second dot? stop loop */ @@ -184,14 +184,14 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { 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); + r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); else e++; /* too many digits; ignore, but still count for exponent */ if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ } if (nosigdig + sigdig == 0) /* no digits? */ - return 0.0; /* invalid format */ + return l_mathop(0.0); /* invalid format */ *endptr = cast_charp(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ @@ -200,7 +200,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { s++; /* skip 'p' */ neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) - return 0.0; /* invalid; must have at least one digit */ + return l_mathop(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; diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 546c9b195..916fd6e7b 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -25,7 +25,7 @@ #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-2021 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -496,7 +496,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2021 Lua.org, PUC-Rio. +* Copyright (C) 1994-2022 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 From 923651ada3c3d4e3165f08970b45f2ba7b8e797d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Jan 2022 10:24:24 +0800 Subject: [PATCH 414/565] Happy New Year --- LICENSE | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 146f0a9b8..3f8241a62 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2020 codingnow.com +Copyright (c) 2012-2022 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 diff --git a/README.md b/README.md index c5016c4bd..5514756d1 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.4.3 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.4 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. From 6d6be8dec6642e87955ebb2068db41e995932ef1 Mon Sep 17 00:00:00 2001 From: xjdrew <1454565+xjdrew@users.noreply.github.com> Date: Fri, 21 Jan 2022 10:34:00 +0800 Subject: [PATCH 415/565] skynet.service: add close (#1526) Co-authored-by: drew.zxj --- lualib/skynet/service.lua | 10 ++++++++++ service/service_provider.lua | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lualib/skynet/service.lua b/lualib/skynet/service.lua index 341f34fcf..674b19e64 100644 --- a/lualib/skynet/service.lua +++ b/lualib/skynet/service.lua @@ -34,6 +34,16 @@ function service.new(name, mainfunc, ...) return address end +function service.close(name) + local addr = skynet.call(get_provider(), "lua", "close", name) + if addr then + cache[name] = nil + skynet.kill(addr) + return true + end + return false +end + function service.query(name) if not cache[name] then cache[name] = skynet.call(get_provider(), "lua", "query", name) diff --git a/service/service_provider.lua b/service/service_provider.lua index 1d0e238a2..8b17d603e 100644 --- a/service/service_provider.lua +++ b/service/service_provider.lua @@ -91,6 +91,16 @@ function provider.test(name) end end +function provider.close(name) + local s = svr[name] + if not s or s.booting then + return skynet.ret(skynet.pack(nil)) + end + + svr[name] = nil + skynet.ret(skynet.pack(s.address)) +end + skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) provider[cmd](...) From 3c4f6737402bda058cd6e092d2605498d88cc239 Mon Sep 17 00:00:00 2001 From: t0350 Date: Tue, 8 Feb 2022 20:27:29 +0800 Subject: [PATCH 416/565] panic while reading from stdin (#1529) Co-authored-by: jietao.cjt --- 3rd/lua/lauxlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 27b02f9f8..51cbf03e4 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1221,7 +1221,7 @@ static int cache_mode(lua_State *L) { LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { int level = cache_level(L); - if (level == CACHE_OFF) { + if (level == CACHE_OFF || filename == NULL) { return luaL_loadfilex_(L, filename, mode); } const void * proto = load_proto(filename); From b13281b77d4b4af9dbc27b1661e531150bd43853 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 14 Feb 2022 07:52:46 +0800 Subject: [PATCH 417/565] bugfix : See #1535 --- skynet-src/socket_server.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 8042b3f56..f0e1a3956 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -646,7 +646,6 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock 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; } @@ -661,19 +660,21 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock freeaddrinfo( ai_list ); return SOCKET_OPEN; } else { - ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTING); if (enable_write(ss, ns, true)) { result->data = "enable write failed"; goto _failed; } + ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTING); } freeaddrinfo( ai_list ); return -1; _failed: + if (sock >= 0) + close(sock); freeaddrinfo( ai_list ); _failed_getaddrinfo: - ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + ATOM_STORE(&ss->slot[HASH_ID(id)].type, SOCKET_TYPE_INVALID); return SOCKET_ERR; } From 40ee98b40ed638859b898c508f579556f7976c43 Mon Sep 17 00:00:00 2001 From: JTrancender <582865471@qq.com> Date: Tue, 15 Feb 2022 21:01:54 +0800 Subject: [PATCH 418/565] feat: add sharetable queryall (#1536) Co-authored-by: Jason --- lualib/skynet/sharetable.lua | 25 +++++++++++++++++++++++++ test/testsharetable.lua | 13 +++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 8f0666bbf..0c1329a96 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -92,6 +92,16 @@ local function sharetable_service() skynet.ret(skynet.pack(ptr)) end + function sharetable.queryall(source, filenamelist) + local ptrList = {} + for _, filename in ipairs(filenamelist) do + if files[filename] then + ptrList[filename] = query_file(source, filename) + end + end + skynet.ret(skynet.pack(ptrList)) + end + function sharetable.close(source) local list = clients[source] if list then @@ -208,6 +218,21 @@ function sharetable.query(filename) end end +function sharetable.queryall(filenamelist) + local list, t, map = {} + local ptrList = skynet.call(sharetable.address, "lua", "queryall", filenamelist) + for filename, ptr in pairs(ptrList) do + t = core.clone(ptr) + map = RECORD[filename] + if not map then + map = {} + RECORD[filename] = map + end + map[t] = true + list[filename] = t + end + return list +end local pairs = pairs local type = type diff --git a/test/testsharetable.lua b/test/testsharetable.lua index 016a286be..64fe83ea2 100644 --- a/test/testsharetable.lua +++ b/test/testsharetable.lua @@ -1,6 +1,17 @@ local skynet = require "skynet" local sharetable = require "skynet.sharetable" +local function queryall_test() + sharetable.loadtable("test_one", {["message"] = "hello one", x = 1, 1}) + sharetable.loadtable("test_two", {["message"] = "hello two", x = 2, 2}) + local list = sharetable.queryall({"test_one", "test_two"}) + for filename, tbl in pairs(list) do + for k, v in pairs(tbl) do + print(filename, k, v) + end + end +end + skynet.start(function() -- You can also use sharetable.loadfile / sharetable.loadstring sharetable.loadtable ("test", { x=1,y={ 'hello world' },['hello world'] = true }) @@ -13,4 +24,6 @@ skynet.start(function() for k,v in pairs(t) do print(k,v) end + + queryall_test() end) From eaba8f969c177aaf190bce40723e55827452d90c Mon Sep 17 00:00:00 2001 From: JTrancender Date: Wed, 16 Feb 2022 11:22:59 +0800 Subject: [PATCH 419/565] perf: add sharetable queryall default behavior(all share data) (#1537) --- lualib/skynet/sharetable.lua | 18 ++++++++++++++++-- test/testsharetable.lua | 9 +++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/sharetable.lua b/lualib/skynet/sharetable.lua index 0c1329a96..31028c73c 100644 --- a/lualib/skynet/sharetable.lua +++ b/lualib/skynet/sharetable.lua @@ -92,13 +92,27 @@ local function sharetable_service() skynet.ret(skynet.pack(ptr)) end - function sharetable.queryall(source, filenamelist) - local ptrList = {} + local function querylist(source, filenamelist) + local ptrList = {} for _, filename in ipairs(filenamelist) do if files[filename] then ptrList[filename] = query_file(source, filename) end end + return ptrList + end + + local function queryall(source) + local ptrList = {} + for filename in pairs(files) do + ptrList[filename] = query_file(source, filename) + end + return ptrList + end + + function sharetable.queryall(source, filenamelist) + local queryFunc = filenamelist and querylist or queryall + local ptrList = queryFunc(source, filenamelist) skynet.ret(skynet.pack(ptrList)) end diff --git a/test/testsharetable.lua b/test/testsharetable.lua index 64fe83ea2..c8ba8e5cb 100644 --- a/test/testsharetable.lua +++ b/test/testsharetable.lua @@ -4,12 +4,21 @@ local sharetable = require "skynet.sharetable" local function queryall_test() sharetable.loadtable("test_one", {["message"] = "hello one", x = 1, 1}) sharetable.loadtable("test_two", {["message"] = "hello two", x = 2, 2}) + sharetable.loadtable("test_three", {["message"] = "hello three", x = 3, 3}) local list = sharetable.queryall({"test_one", "test_two"}) for filename, tbl in pairs(list) do for k, v in pairs(tbl) do print(filename, k, v) end end + + print("test queryall default") + local defaultlist = sharetable.queryall() + for filename, tbl in pairs(defaultlist) do + for k, v in pairs(tbl) do + print(filename, k, v) + end + end end skynet.start(function() From d1ce950dc1294181ecdaee1bf9e420559726d70b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 19 Mar 2022 07:10:52 +0800 Subject: [PATCH 420/565] Fix #1552 --- service-src/service_snlua.c | 6 ++---- skynet-src/atomic.h | 25 +++++++++++++++++++++++-- skynet-src/malloc_hook.c | 16 ++++++++-------- skynet-src/rwlock.h | 3 +-- skynet-src/skynet_server.c | 6 ++---- skynet-src/socket_server.c | 2 +- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 4e9381ce8..d7871d532 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -521,14 +521,12 @@ snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { if (ATOM_LOAD(&l->trap) == 0) { - int zero = 0; // only one thread can set trap ( l->trap 0->1 ) - if (!ATOM_CAS(&l->trap, zero, 1)) + if (!ATOM_CAS(&l->trap, 0, 1)) return; lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); // finish set ( l->trap 1 -> -1 ) - int one = 1; - ATOM_CAS(&l->trap, one, -1); + ATOM_CAS(&l->trap, 1, -1); } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index 5acc32df6..4dd3ba822 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -13,6 +13,8 @@ #define ATOM_LOAD(ptr) (*(ptr)) #define ATOM_STORE(ptr, v) (*(ptr) = v) #define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_CAS_ULONG(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_CAS_SIZET(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_FINC(ptr) __sync_fetch_and_add(ptr, 1) #define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1) @@ -31,8 +33,27 @@ #define ATOM_INIT(ref, v) atomic_init(ref, v) #define ATOM_LOAD(ptr) atomic_load(ptr) #define ATOM_STORE(ptr, v) atomic_store(ptr, v) -#define ATOM_CAS(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval) -#define ATOM_CAS_POINTER(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval) + +static inline int +ATOM_CAS(atomic_int *ptr, int oval, int nval) { + return atomic_compare_exchange_weak(ptr, &(oval), nval); +} + +static inline int +ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) { + return atomic_compare_exchange_weak(ptr, &(oval), nval); +} + +static inline int +ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) { + return atomic_compare_exchange_weak(ptr, &(oval), nval); +} + +static inline int +ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { + return atomic_compare_exchange_weak(ptr, &(oval), nval); +} + #define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1) #define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1) #define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index d02113cfd..44c718302 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -19,8 +19,8 @@ static ATOM_SIZET _used_memory = 0; static ATOM_SIZET _memory_block = 0; struct mem_data { - uint32_t handle; - ssize_t allocated; + ATOM_ULONG handle; + ATOM_SIZET allocated; }; struct mem_cookie { @@ -44,19 +44,19 @@ static struct mem_data mem_stats[SLOT_SIZE]; #define raw_realloc je_realloc #define raw_free je_free -static ssize_t* +static ATOM_SIZET * get_allocated_field(uint32_t handle) { int h = (int)(handle & (SLOT_SIZE - 1)); struct mem_data *data = &mem_stats[h]; uint32_t old_handle = data->handle; - ssize_t old_alloc = data->allocated; + ssize_t old_alloc = (ssize_t)data->allocated; if(old_handle == 0 || old_alloc <= 0) { // data->allocated may less than zero, because it may not count at start. - if(!ATOM_CAS(&data->handle, old_handle, handle)) { + if(!ATOM_CAS_ULONG(&data->handle, old_handle, handle)) { return 0; } if (old_alloc < 0) { - ATOM_CAS(&data->allocated, old_alloc, 0); + ATOM_CAS_SIZET(&data->allocated, (size_t)old_alloc, 0); } } if(data->handle != handle) { @@ -69,7 +69,7 @@ inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { ATOM_FADD(&_used_memory, __n); ATOM_FINC(&_memory_block); - ssize_t* allocated = get_allocated_field(handle); + ATOM_SIZET * allocated = get_allocated_field(handle); if(allocated) { ATOM_FADD(allocated, __n); } @@ -79,7 +79,7 @@ inline static void update_xmalloc_stat_free(uint32_t handle, size_t __n) { ATOM_FSUB(&_used_memory, __n); ATOM_FDEC(&_memory_block); - ssize_t* allocated = get_allocated_field(handle); + ATOM_SIZET * allocated = get_allocated_field(handle); if(allocated) { ATOM_FSUB(allocated, __n); } diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 28a49c245..2b954bd05 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -31,8 +31,7 @@ rwlock_rlock(struct rwlock *lock) { static inline void rwlock_wlock(struct rwlock *lock) { - int clear = 0; - while (!ATOM_CAS(&lock->write,clear,1)) {} + while (!ATOM_CAS(&lock->write,0,1)) {} while(ATOM_LOAD(&lock->read)) {} } diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e90133b9e..e4d7ea3a8 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -595,8 +595,7 @@ cmd_logon(struct skynet_context * context, const char * param) { if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { - uintptr_t exp = 0; - if (!ATOM_CAS_POINTER(&ctx->logfile, exp, (uintptr_t)f)) { + if (!ATOM_CAS_POINTER(&ctx->logfile, 0, (uintptr_t)f)) { // logfile opens in other thread, close this one. fclose(f); } @@ -617,8 +616,7 @@ cmd_logoff(struct skynet_context * context, const char * param) { FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { // logfile may close in other thread - uintptr_t fptr = (uintptr_t)f; - if (ATOM_CAS_POINTER(&ctx->logfile, fptr, (uintptr_t)NULL)) { + if (ATOM_CAS_POINTER(&ctx->logfile, (uintptr_t)f, (uintptr_t)NULL)) { skynet_log_close(context, f, handle); } } diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f0e1a3956..d1a912eea 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1316,7 +1316,7 @@ inc_sending_ref(struct socket *s, int id) { continue; } // inc sending only matching the same socket id - if (ATOM_CAS(&s->sending, sending, sending + 1)) + if (ATOM_CAS_ULONG(&s->sending, sending, sending + 1)) return; // atom inc failed, retry } else { From fad62566dcef70ef3881ac6b6b821f99096da382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 21 Mar 2022 13:23:03 +0800 Subject: [PATCH 421/565] #1544 fix playload_len (#1553) Co-authored-by: zixun --- lualib/http/websocket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 896040210..f09b826ff 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -184,7 +184,7 @@ local function write_frame(self, op, payload_data, masking_key) -- mask set to 0 if payload_len < 126 then s = string.pack("I1I1", v1, mask | payload_len) - elseif payload_len < 0xffff then + elseif payload_len <= 0xffff then s = string.pack("I1I1>I2", v1, mask | 126, payload_len) else s = string.pack("I1I1>I8", v1, mask | 127, payload_len) From 5d48c02725283bc4e674fb9a9c3007755024edc5 Mon Sep 17 00:00:00 2001 From: t0350 Date: Wed, 23 Mar 2022 09:38:20 +0800 Subject: [PATCH 422/565] see #1554, handle nomore message after skynet.exit (#1556) Co-authored-by: jietao.cjt --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 74507d70a..73a4d3e7b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -545,6 +545,7 @@ function skynet.exit() for address in pairs(tmp) do c.send(address, skynet.PTYPE_ERROR, 0, "") end + c.callback(function() end) c.command("EXIT") -- quit service coroutine_yield "QUIT" From bee8a0e7314231ba37454e62339faee735db590e Mon Sep 17 00:00:00 2001 From: caiyiheng Date: Tue, 29 Mar 2022 16:16:51 +0800 Subject: [PATCH 423/565] update lpeg to 1.0.2 (#1557) --- 3rd/lpeg/HISTORY | 34 ++++++++++-------- 3rd/lpeg/lpcap.c | 64 +++++++++++++++++++++------------ 3rd/lpeg/lpcap.h | 3 +- 3rd/lpeg/lpcode.c | 2 +- 3rd/lpeg/lpcode.h | 2 +- 3rd/lpeg/lpeg.html | 12 ++----- 3rd/lpeg/lpprint.c | 2 +- 3rd/lpeg/lpprint.h | 2 +- 3rd/lpeg/lptree.c | 18 +++++----- 3rd/lpeg/lptree.h | 2 +- 3rd/lpeg/lptypes.h | 10 ++---- 3rd/lpeg/lpvm.c | 88 ++++++++++++++++++++++++++-------------------- 3rd/lpeg/lpvm.h | 2 +- 3rd/lpeg/makefile | 8 ++--- 3rd/lpeg/re.html | 10 ++---- 3rd/lpeg/re.lua | 30 ++++++++++------ 3rd/lpeg/test.lua | 22 +++++++++++- 17 files changed, 180 insertions(+), 131 deletions(-) diff --git a/3rd/lpeg/HISTORY b/3rd/lpeg/HISTORY index 0c10edd0d..66a8e14db 100644 --- a/3rd/lpeg/HISTORY +++ b/3rd/lpeg/HISTORY @@ -1,6 +1,10 @@ -HISTORY for LPeg 1.0 +HISTORY for LPeg 1.0.2 -* Changes from version 0.12 to 1.0 +* Changes from version 1.0.1 to 1.0.2 + --------------------------------- + + some bugs fixed + +* Changes from version 0.12 to 1.0.1 --------------------------------- + group "names" can be any Lua value + some bugs fixed @@ -13,14 +17,14 @@ HISTORY for LPeg 1.0 + 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 @@ -32,34 +36,34 @@ HISTORY for LPeg 1.0 - "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 + + 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 + + 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). @@ -67,7 +71,7 @@ HISTORY for LPeg 1.0 + Minimal documentation for module 're'. * Changes from version 0.4 to 0.5 - ------------------------------- + ------------------------------- + Several optimizations. + lpeg.P now accepts booleans. + Some new examples. @@ -75,18 +79,18 @@ HISTORY for LPeg 1.0 + 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. diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c index c9085de06..b332fde49 100644 --- a/3rd/lpeg/lpcap.c +++ b/3rd/lpeg/lpcap.c @@ -1,5 +1,5 @@ /* -** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $ +** $Id: lpcap.c $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -271,15 +271,15 @@ int finddyncap (Capture *cap, Capture *last) { /* -** 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.) +** Calls a runtime capture. Returns number of captures "removed" by the +** call, that is, those inside the group capture. 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); + Capture *open = findopen(close); /* get open group capture */ assert(captype(open) == Cgroup); id = finddyncap(open, close); /* get first dynamic capture argument */ close->kind = Cclose; /* closes the group */ @@ -299,7 +299,7 @@ int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { } else *rem = 0; /* no dynamic captures removed */ - return close - open; /* number of captures of all kinds removed */ + return close - open - 1; /* number of captures to be removed */ } @@ -441,70 +441,88 @@ static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { } +#if !defined(MAXRECLEVEL) +#define MAXRECLEVEL 200 +#endif + + /* ** 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; + int res; luaL_checkstack(L, 4, "too many captures"); + if (cs->reclevel++ > MAXRECLEVEL) + return luaL_error(L, "subcapture nesting too deep"); switch (captype(cs->cap)) { case Cposition: { lua_pushinteger(L, cs->cap->s - cs->s + 1); cs->cap++; - return 1; + res = 1; + break; } case Cconst: { pushluaval(cs); cs->cap++; - return 1; + res = 1; + break; } case Carg: { int arg = (cs->cap++)->idx; if (arg + FIXEDARGS > cs->ptop) return luaL_error(L, "reference to absent extra argument #%d", arg); lua_pushvalue(L, arg + FIXEDARGS); - return 1; + res = 1; + break; } case Csimple: { int k = pushnestedvalues(cs, 1); lua_insert(L, -k); /* make whole match be first result */ - return k; + res = k; + break; } case Cruntime: { lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */ - return 1; + res = 1; + break; } case Cstring: { luaL_Buffer b; luaL_buffinit(L, &b); stringcap(&b, cs); luaL_pushresult(&b); - return 1; + res = 1; + break; } case Csubst: { luaL_Buffer b; luaL_buffinit(L, &b); substcap(&b, cs); luaL_pushresult(&b); - return 1; + res = 1; + break; } case Cgroup: { if (cs->cap->idx == 0) /* anonymous group? */ - return pushnestedvalues(cs, 0); /* add all nested values */ + res = pushnestedvalues(cs, 0); /* add all nested values */ else { /* named group: add no values */ nextcap(cs); /* skip capture */ - return 0; + res = 0; } + break; } - 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; + case Cbackref: res = backrefcap(cs); break; + case Ctable: res = tablecap(cs); break; + case Cfunction: res = functioncap(cs); break; + case Cnum: res = numcap(cs); break; + case Cquery: res = querycap(cs); break; + case Cfold: res = foldcap(cs); break; + default: assert(0); res = 0; } + cs->reclevel--; + return res; } @@ -521,7 +539,7 @@ int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { int n = 0; if (!isclosecap(capture)) { /* is there any capture? */ CapState cs; - cs.ocap = cs.cap = capture; cs.L = L; + cs.ocap = cs.cap = capture; cs.L = L; cs.reclevel = 0; cs.s = s; cs.valuecached = 0; cs.ptop = ptop; do { /* collect their values */ n += pushcapture(&cs); diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h index 6133df2a0..dc10d6969 100644 --- a/3rd/lpeg/lpcap.h +++ b/3rd/lpeg/lpcap.h @@ -1,5 +1,5 @@ /* -** $Id: lpcap.h,v 1.3 2016/09/13 17:45:58 roberto Exp $ +** $Id: lpcap.h $ */ #if !defined(lpcap_h) @@ -44,6 +44,7 @@ typedef struct CapState { int ptop; /* index of last argument to 'match' */ const char *s; /* original string */ int valuecached; /* value stored in cache slot */ + int reclevel; /* recursion level */ } CapState; diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c index 2722d716b..392345972 100644 --- a/3rd/lpeg/lpcode.c +++ b/3rd/lpeg/lpcode.c @@ -1,5 +1,5 @@ /* -** $Id: lpcode.c,v 1.24 2016/09/15 17:46:13 roberto Exp $ +** $Id: lpcode.c $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h index 2a5861ef0..34ee27637 100644 --- a/3rd/lpeg/lpcode.h +++ b/3rd/lpeg/lpcode.h @@ -1,5 +1,5 @@ /* -** $Id: lpcode.h,v 1.8 2016/09/15 17:46:13 roberto Exp $ +** $Id: lpcode.h $ */ #if !defined(lpcode_h) diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html index 5c9535f7c..8b9f59c2a 100644 --- a/3rd/lpeg/lpeg.html +++ b/3rd/lpeg/lpeg.html @@ -10,7 +10,7 @@ - +

@@ -1391,13 +1391,13 @@

Arithmetic expressions

Download

LPeg -source code.

+source code.

License

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

Permission is hereby granted, free of charge, @@ -1433,12 +1433,6 @@

License

-
-

-$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 f7be408f7..df62cbeed 100644 --- a/3rd/lpeg/lpprint.c +++ b/3rd/lpeg/lpprint.c @@ -1,5 +1,5 @@ /* -** $Id: lpprint.c,v 1.10 2016/09/13 16:06:03 roberto Exp $ +** $Id: lpprint.c $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ diff --git a/3rd/lpeg/lpprint.h b/3rd/lpeg/lpprint.h index 632976076..15ef121d7 100644 --- a/3rd/lpeg/lpprint.h +++ b/3rd/lpeg/lpprint.h @@ -1,5 +1,5 @@ /* -** $Id: lpprint.h,v 1.2 2015/06/12 18:18:08 roberto Exp $ +** $Id: lpprint.h $ */ diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index bda61b91b..5c8de9477 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -1,5 +1,5 @@ /* -** $Id: lptree.c,v 1.22 2016/09/13 18:10:22 roberto Exp $ +** $Id: lptree.c $ ** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -716,6 +716,7 @@ static int capture_aux (lua_State *L, int cap, int labelidx) { /* ** Fill a tree with an empty capture, using an empty (TTrue) sibling. +** (The 'key' field must be filled by the caller to finish the tree.) */ static TTree *auxemptycap (TTree *tree, int cap) { tree->tag = TCapture; @@ -726,15 +727,17 @@ static TTree *auxemptycap (TTree *tree, int cap) { /* -** Create a tree for an empty capture +** Create a tree for an empty capture. */ -static TTree *newemptycap (lua_State *L, int cap) { - return auxemptycap(newtree(L, 2), cap); +static TTree *newemptycap (lua_State *L, int cap, int key) { + TTree *tree = auxemptycap(newtree(L, 2), cap); + tree->key = key; + return tree; } /* -** Create a tree for an empty capture with an associated Lua value +** 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); @@ -795,16 +798,15 @@ static int lp_simplecapture (lua_State *L) { static int lp_poscapture (lua_State *L) { - newemptycap(L, Cposition); + newemptycap(L, Cposition, 0); return 1; } static int lp_argcapture (lua_State *L) { int n = (int)luaL_checkinteger(L, 1); - TTree *tree = newemptycap(L, Carg); - tree->key = n; luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); + newemptycap(L, Carg, n); return 1; } diff --git a/3rd/lpeg/lptree.h b/3rd/lpeg/lptree.h index 34ee15cad..25906d5f4 100644 --- a/3rd/lpeg/lptree.h +++ b/3rd/lpeg/lptree.h @@ -1,5 +1,5 @@ /* -** $Id: lptree.h,v 1.3 2016/09/13 18:07:51 roberto Exp $ +** $Id: lptree.h $ */ #if !defined(lptree_h) diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index 8e78bc81b..1d9d59f6b 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -1,7 +1,7 @@ /* -** $Id: lptypes.h,v 1.16 2017/01/13 13:33:17 roberto Exp $ +** $Id: lptypes.h $ ** LPeg - PEG pattern matching for Lua -** Copyright 2007-2017, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** Copyright 2007-2019, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ @@ -9,17 +9,13 @@ #define lptypes_h -#if !defined(LPEG_DEBUG) -#define NDEBUG -#endif - #include #include #include "lua.h" -#define VERSION "1.0.1" +#define VERSION "1.0.2" #define PATTERN_T "lpeg-pattern" diff --git a/3rd/lpeg/lpvm.c b/3rd/lpeg/lpvm.c index 05a5f68c8..737418c47 100644 --- a/3rd/lpeg/lpvm.c +++ b/3rd/lpeg/lpvm.c @@ -1,5 +1,5 @@ /* -** $Id: lpvm.c,v 1.9 2016/06/03 20:11:18 roberto Exp $ +** $Id: lpvm.c $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -45,18 +45,29 @@ typedef struct Stack { /* -** Make the size of the array of captures 'cap' twice as large as needed -** (which is 'captop'). ('n' is the number of new elements.) +** Ensures the size of array 'capture' (with size '*capsize' and +** 'captop' elements being used) is enough to accomodate 'n' extra +** elements plus one. (Because several opcodes add stuff to the capture +** array, it is simpler to ensure the array always has at least one free +** slot upfront and check its size later.) */ -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 - n) * sizeof(Capture)); - lua_replace(L, caplistidx(ptop)); - return newc; +static Capture *growcap (lua_State *L, Capture *capture, int *capsize, + int captop, int n, int ptop) { + if (*capsize - captop > n) + return capture; /* no need to grow array */ + else { /* must grow */ + Capture *newc; + int newsize = captop + n + 1; /* minimum size needed */ + if (newsize < INT_MAX/((int)sizeof(Capture) * 2)) + newsize *= 2; /* twice that size, if not too big */ + else if (newsize >= INT_MAX/((int)sizeof(Capture))) + luaL_error(L, "too many captures"); + newc = (Capture *)lua_newuserdata(L, newsize * sizeof(Capture)); + memcpy(newc, capture, captop * sizeof(Capture)); + *capsize = newsize; + lua_replace(L, caplistidx(ptop)); + return newc; + } } @@ -109,24 +120,24 @@ static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { /* -** 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). +** Add capture values returned by a dynamic capture to the list +** 'capture', nested inside a group. 'fd' indexes the first capture +** value, 'n' is the number of values (at least 1). The open group +** capture is already in 'capture', before the place for the new entries. */ -static void adddyncaptures (const char *s, Capture *base, int n, int fd) { +static void adddyncaptures (const char *s, Capture *capture, int n, int fd) { int i; - 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; - base[i].siz = 1; /* mark it as closed */ - base[i].idx = fd + i - 1; /* stack index of capture value */ - base[i].s = s; + assert(capture[-1].kind == Cgroup && capture[-1].siz == 0); + capture[-1].idx = 0; /* make group capture an anonymous group */ + for (i = 0; i < n; i++) { /* add runtime captures */ + capture[i].kind = Cruntime; + capture[i].siz = 1; /* mark it as closed */ + capture[i].idx = fd + i; /* stack index of capture value */ + capture[i].s = s; } - base[i].kind = Cclose; /* close group */ - base[i].siz = 1; - base[i].s = s; + capture[n].kind = Cclose; /* close group */ + capture[n].siz = 1; + capture[n].s = s; } @@ -296,7 +307,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, 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; + cs.reclevel = 0; cs.L = L; + cs.s = o; 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 */ @@ -307,15 +319,15 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, s = o + res; /* else update current position */ n = lua_gettop(L) - fr + 1; /* number of new captures */ ndyncap += n; /* update number of dynamic captures */ - if (n > 0) { /* any new capture? */ + if (n == 0) /* no new captures? */ + captop--; /* remove open group */ + else { /* new captures; keep original open group */ 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, n + 2, ptop); - capsize = 2 * captop; - } - /* add new captures to 'capture' list */ - adddyncaptures(s, capture + captop - n - 2, n, fr); + /* add new captures + close group to 'capture' list */ + capture = growcap(L, capture, &capsize, captop, n + 1, ptop); + adddyncaptures(s, capture + captop, n, fr); + captop += n + 1; /* new captures + close group */ } p++; continue; @@ -347,10 +359,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, pushcapture: { capture[captop].idx = p->i.key; capture[captop].kind = getkind(p); - if (++captop >= capsize) { - capture = doublecap(L, capture, captop, 0, ptop); - capsize = 2 * captop; - } + captop++; + capture = growcap(L, capture, &capsize, captop, 0, ptop); p++; continue; } diff --git a/3rd/lpeg/lpvm.h b/3rd/lpeg/lpvm.h index 757b9e135..69ec33dce 100644 --- a/3rd/lpeg/lpvm.h +++ b/3rd/lpeg/lpvm.h @@ -1,5 +1,5 @@ /* -** $Id: lpvm.h,v 1.3 2014/02/21 13:06:41 roberto Exp $ +** $Id: lpvm.h $ */ #if !defined(lpvm_h) diff --git a/3rd/lpeg/makefile b/3rd/lpeg/makefile index 7a8463e3f..1e32195a8 100644 --- a/3rd/lpeg/makefile +++ b/3rd/lpeg/makefile @@ -1,8 +1,8 @@ LIBNAME = lpeg LUADIR = ../lua/ -COPT = -O2 -# COPT = -DLPEG_DEBUG -g +COPT = -O2 -DNDEBUG +# COPT = -g CWARNS = -Wall -Wextra -pedantic \ -Waggregate-return \ @@ -29,11 +29,11 @@ FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o # For Linux linux: - make lpeg.so "DLLFLAGS = -shared -fPIC" + $(MAKE) lpeg.so "DLLFLAGS = -shared -fPIC" # For Mac OS macosx: - make lpeg.so "DLLFLAGS = -bundle -undefined dynamic_lookup" + $(MAKE) lpeg.so "DLLFLAGS = -bundle -undefined dynamic_lookup" lpeg.so: $(FILES) env $(CC) $(DLLFLAGS) $(FILES) -o lpeg.so diff --git a/3rd/lpeg/re.html b/3rd/lpeg/re.html index 32f0a4586..ad60d509e 100644 --- a/3rd/lpeg/re.html +++ b/3rd/lpeg/re.html @@ -10,7 +10,7 @@ - +
@@ -93,6 +93,8 @@

The re Module

equivalent to p / defs[name] p => name match-time capture equivalent to lpeg.Cmt(p, defs[name]) +p ~> name fold capture +equivalent to lpeg.Cf(p, defs[name]) & p and predicate ! p not predicate p1 p2 concatenation @@ -486,12 +488,6 @@

License

-
-

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

-
- diff --git a/3rd/lpeg/re.lua b/3rd/lpeg/re.lua index 3b9974fd9..3bb8af7d4 100644 --- a/3rd/lpeg/re.lua +++ b/3rd/lpeg/re.lua @@ -1,4 +1,4 @@ --- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $ +-- $Id: re.lua $ -- imported functions and modules local tonumber, type, print, error = tonumber, type, print, error @@ -71,13 +71,6 @@ 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) .. "..." @@ -116,6 +109,20 @@ name = m.C(name) -- a defined name only have meaning in a given environment local Def = name * m.Carg(1) + +local function getdef (id, defs) + local c = defs and defs[id] + if not c then error("undefined name: " .. id) end + return c +end + +-- match a name and return a group of its corresponding definition +-- and 'f' (to be folded in 'Suffix') +local function defwithfunc (f) + return m.Cg(Def / getdef * m.Cc(f)) +end + + local num = m.C(m.R"09"^1) * S / tonumber local String = "'" * m.C((any - "'")^0) * "'" + @@ -130,7 +137,7 @@ end local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R -local item = defined + Range + m.C(any) +local item = (defined + Range + m.C(any)) / m.P local Class = "[" @@ -176,9 +183,10 @@ local exp = m.P{ "Exp", ) + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div)) + m.P"{}" * m.Cc(nil, m.Ct) - + m.Cg(Def / getdef * m.Cc(mt.__div)) + + defwithfunc(mt.__div) ) - + "=>" * S * m.Cg(Def / getdef * m.Cc(m.Cmt)) + + "=>" * S * defwithfunc(m.Cmt) + + "~>" * S * defwithfunc(m.Cf) ) * S )^0, function (a,b,f) return f(a,b) end ); Primary = "(" * m.V"Exp" * ")" diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua index 20ad07f8f..8f9f5745d 100755 --- a/3rd/lpeg/test.lua +++ b/3rd/lpeg/test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env lua --- $Id: test.lua,v 1.112 2017/01/14 18:55:22 roberto Exp $ +-- $Id: test.lua $ -- require"strict" -- just to be pedantic @@ -424,6 +424,16 @@ do end +do + -- nesting of captures too deep + local p = m.C(1) + for i = 1, 300 do + p = m.Ct(p) + end + checkerr("too deep", p.match, p, "x") +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 } @@ -1186,6 +1196,9 @@ 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) + +assert(re.match("aaand", "[a]^2") == 3) + local t = {match("abceefe", "( ( & 'e' {} ) ? . ) * ")} checkeq(t, {4, 5, 7}) local t = {match("abceefe", "((&&'e' {})? .)*")} @@ -1360,6 +1373,13 @@ checkeq(x, {tag='x', 'hi', {tag = 'b', 'hello'}, 'but', {'totheend'}}) +-- test for folding captures +c = re.compile([[ + S <- (number (%s+ number)*) ~> add + number <- %d+ -> tonumber +]], {tonumber = tonumber, add = function (a,b) return a + b end}) +assert(c:match("3 401 50") == 3 + 401 + 50) + -- tests for look-ahead captures x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} checkeq(x, {"", "alo", ""}) From f72573a2aaac0c642d2f47a40ee32b6fdb76ef26 Mon Sep 17 00:00:00 2001 From: liaodaiguo123 Date: Thu, 31 Mar 2022 18:57:03 +0800 Subject: [PATCH 424/565] base64:To improve compatibility, there may not be enough equal signs (#1561) --- lualib-src/lua-crypt.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 0e4d06cf3..a287715eb 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -961,10 +961,12 @@ lb64decode(lua_State *L) { int padding = 0; int c[4]; for (j=0;j<4;) { - if (i>=sz) { - return luaL_error(L, "Invalid base64 text"); + if (i>=sz && 4>j){ + /*To improve compatibility, there may not be enough equal signs */ + c[j] = -2; + }else{ + c[j] = b64index(text[i]); } - c[j] = b64index(text[i]); if (c[j] == -1) { ++i; continue; From b555aa19e74f21a36fa0f8b681e17832dfdcdbee Mon Sep 17 00:00:00 2001 From: JTrancender Date: Sat, 2 Apr 2022 12:17:47 +0800 Subject: [PATCH 425/565] feat: add cluster.unregister (#1563) --- examples/cluster1.lua | 2 ++ lualib/skynet/cluster.lua | 5 +++++ service/clusterd.lua | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 796658267..10b2e2f29 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -12,6 +12,8 @@ skynet.start(function() -- register name "sdb" for simpledb, you can use cluster.query() later. -- See cluster2.lua cluster.register("sdb", sdb) + cluster.unregister("sdb") + cluster.register("sdb", sdb) print(skynet.call(sdb, "lua", "SET", "a", "foobar")) print(skynet.call(sdb, "lua", "SET", "b", "foobar2")) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index a6886743a..8118c2f66 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -106,6 +106,11 @@ function cluster.register(name, addr) return skynet.call(clusterd, "lua", "register", name, addr) end +function cluster.unregister(name) + assert(type(name) == "string") + return skynet.call(clusterd, "lua", "unregister", name) +end + function cluster.query(node, name) return skynet.call(get_sender(node), "lua", "req", 0, skynet.pack(name)) end diff --git a/service/clusterd.lua b/service/clusterd.lua index a00a4c510..d13eded52 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -204,6 +204,18 @@ function command.register(source, name, addr) skynet.error(string.format("Register [%s] :%08x", name, addr)) end +function command.unregister(_, name) + if not register_name[name] then + return skynet.ret(nil) + end + local addr = register_name[name] + register_name[addr] = nil + register_name[name] = nil + clearnamecache() + skynet.ret(nil) + skynet.error(string.format("Unregister [%s] :%08x", name, addr)) +end + function command.queryname(source, name) skynet.ret(skynet.pack(register_name[name])) end From c84f7354ffce9ed7f64ab5e4fc48fe9395d97cdb Mon Sep 17 00:00:00 2001 From: coder <273461474@qq.com> Date: Sat, 2 Apr 2022 16:06:04 +0800 Subject: [PATCH 426/565] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99snl?= =?UTF-8?q?ua=E5=AE=9A=E4=B9=89=20(#1564)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/lua-skynet.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index ae4f7a383..fbbc7fbde 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -35,12 +35,6 @@ get_time() { #endif } -struct snlua { - lua_State * L; - struct skynet_context * ctx; - const char * preload; -}; - static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); From 969d3291b6380aa62899df502b063d0a330f805b Mon Sep 17 00:00:00 2001 From: coder <273461474@qq.com> Date: Sat, 2 Apr 2022 19:30:46 +0800 Subject: [PATCH 427/565] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8for?= =?UTF-8?q?warding=E6=95=B0=E6=8D=AE=20(#1565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/gate.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/service/gate.lua b/service/gate.lua index ef4c24af9..d78cdafac 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -3,7 +3,6 @@ local gateserver = require "snax.gateserver" local watchdog local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } -local forwarding = {} -- agent -> connection skynet.register_protocol { name = "client", @@ -41,7 +40,6 @@ end local function unforward(c) if c.agent then - forwarding[c.agent] = nil c.agent = nil c.client = nil end @@ -76,7 +74,6 @@ function CMD.forward(source, fd, client, address) unforward(c) c.client = client or 0 c.agent = address or source - forwarding[c.agent] = c gateserver.openclient(fd) end From 2660b1044243dd654e8e79a5932a903afd8ab70c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Apr 2022 15:19:53 +0800 Subject: [PATCH 428/565] fix #1567 --- lualib-src/lua-seri.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index f7b337e21..dac4596f1 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -256,17 +256,19 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a wb_nil(wb); } -static void +static int 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); + if (lua_pcall(L, 1, 3,0) != LUA_OK) + return 1; for(;;) { lua_pushvalue(L, -2); lua_pushvalue(L, -2); lua_copy(L, -5, -3); - lua_call(L, 2, 2); + if (lua_pcall(L, 2, 2, 0) != LUA_OK) + return 1; int type = lua_type(L, -2); if (type == LUA_TNIL) { lua_pop(L, 4); @@ -277,19 +279,24 @@ wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { lua_pop(L, 1); } wb_nil(wb); + return 0; } -static void +static int wb_table(lua_State *L, struct write_block *wb, int index, int depth) { - luaL_checkstack(L, LUA_MINSTACK, NULL); + if (!lua_checkstack(L, LUA_MINSTACK)) { + lua_pushstring(L, "out of memory"); + return 1; + } if (index < 0) { index = lua_gettop(L) + index + 1; } if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { - wb_table_metapairs(L, wb, index, depth); + return 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); + return 0; } } @@ -330,7 +337,10 @@ pack_one(lua_State *L, struct write_block *b, int index, int depth) { if (index < 0) { index = lua_gettop(L) + index + 1; } - wb_table(L, b, index, depth+1); + if (wb_table(L, b, index, depth+1)) { + wb_free(b); + lua_error(L); + } break; } default: From 9fd7a51b342a0435e0dd6a4d0bea54ca901ceead Mon Sep 17 00:00:00 2001 From: Bruce Date: Thu, 7 Apr 2022 17:22:32 +0800 Subject: [PATCH 429/565] Compat lua compile as cpp (#1568) * Compat lua compile as cpp * Compat lua compile as cpp --- lualib-src/lsha1.c | 4 ++-- lualib-src/lua-bson.c | 38 ++++++++++++++++----------------- lualib-src/lua-crypt.c | 14 ++++++------ lualib-src/lua-mongo.c | 9 ++++---- skynet-src/atomic.h | 48 ++++++++++++++++++++++++------------------ skynet-src/spinlock.h | 13 ++++++------ 6 files changed, 68 insertions(+), 58 deletions(-) diff --git a/lualib-src/lsha1.c b/lualib-src/lsha1.c index 8f539844b..310e8e145 100644 --- a/lualib-src/lsha1.c +++ b/lualib-src/lsha1.c @@ -263,11 +263,11 @@ lsha1(lua_State *L) { #define BLOCKSIZE 64 static inline void -xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { +xor_key(uint8_t key[BLOCKSIZE], uint32_t xor_) { int i; for (i=0;icap <= b->size + sz); if (b->ptr == b->buffer) { - b->ptr = malloc(b->cap); + b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { - b->ptr = realloc(b->ptr, b->cap); + b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } @@ -329,7 +329,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) break; case LUA_TUSERDATA: { append_key(bs, L, BSON_DOCUMENT, key, sz); - int32_t * doc = lua_touserdata(L,-1); + int32_t * doc = (int32_t*)lua_touserdata(L,-1); int32_t sz = *doc; bson_reserve(bs,sz); memcpy(bs->ptr + bs->size, doc, sz); @@ -574,7 +574,7 @@ static int ltostring(lua_State *L) { size_t sz = lua_rawlen(L, 1); void * ud = lua_touserdata(L,1); - lua_pushlstring(L, ud, sz); + lua_pushlstring(L, (const char*)ud, sz); return 1; } @@ -591,7 +591,7 @@ make_object(lua_State *L, int type, const void * ptr, size_t len) { luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, type); - luaL_addlstring(&b, ptr, len); + luaL_addlstring(&b, (const char*)ptr, len); luaL_pushresult(&b); } @@ -600,7 +600,7 @@ 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 }; + struct bson_reader t = { (const uint8_t*)bytes, sz-5 }; int end = read_byte(L, br); if (end != '\0') { luaL_error(L, "Invalid document end"); @@ -632,7 +632,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { if (sz <= 0) { luaL_error(L, "Invalid bson string , length = %d", sz); } - lua_pushlstring(L, read_bytes(L, &t, sz), sz-1); + lua_pushlstring(L, (const char*)read_bytes(L, &t, sz), sz-1); break; } case BSON_DOCUMENT: @@ -650,7 +650,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { luaL_addchar(&b, 0); luaL_addchar(&b, BSON_BINARY); luaL_addchar(&b, subtype); - luaL_addlstring(&b, read_bytes(L, &t, sz), sz); + luaL_addlstring(&b, (const char*)read_bytes(L, &t, sz), sz); luaL_pushresult(&b); break; } @@ -666,7 +666,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { case BSON_MINKEY: case BSON_MAXKEY: case BSON_NULL: { - char key[] = { 0, bt }; + char key[] = { 0, (char)bt }; lua_pushlstring(L, key, sizeof(key)); break; } @@ -739,7 +739,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { static int lmakeindex(lua_State *L) { - int32_t *bson = luaL_checkudata(L,1,"bson"); + int32_t *bson = (int32_t*)luaL_checkudata(L,1,"bson"); const uint8_t * start = (const uint8_t *)bson; struct bson_reader br = { start+4, get_length(start) - 5 }; lua_newtable(L); @@ -871,7 +871,7 @@ lreplace(lua_State *L) { int id = lua_tointeger(L, -1); int type = id & ((1<<(BSON_TYPE_SHIFT)) - 1); int offset = id >> BSON_TYPE_SHIFT; - uint8_t * start = lua_touserdata(L,1); + uint8_t * start = (uint8_t*)lua_touserdata(L,1); struct bson b = { 0,16, start + offset }; switch (type) { case BSON_REAL: @@ -910,7 +910,7 @@ lreplace(lua_State *L) { static int ldecode(lua_State *L) { - const int32_t * data = lua_touserdata(L,1); + const int32_t * data = (const int32_t*)lua_touserdata(L,1); if (data == NULL) { return 0; } @@ -945,7 +945,7 @@ bson_meta(lua_State *L) { static int encode_bson(lua_State *L) { - struct bson *b = lua_touserdata(L, 2); + struct bson *b = (struct bson*)lua_touserdata(L, 2); lua_settop(L, 1); if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { pack_meta_dict(L, b, 0); @@ -978,7 +978,7 @@ lencode(lua_State *L) { static int encode_bson_byorder(lua_State *L) { int n = lua_gettop(L); - struct bson *b = lua_touserdata(L, n); + struct bson *b = (struct bson*)lua_touserdata(L, n); lua_settop(L, --n); pack_ordered_dict(L, b, n, 0); lua_settop(L,0); @@ -1106,7 +1106,7 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { char oid[24]; int i; const uint8_t * id = buf; - static char *hex = "0123456789abcdef"; + static const char *hex = "0123456789abcdef"; for (i=0;i<12;i++) { oid[i*2] = hex[id[i] >> 4]; oid[i*2+1] = hex[id[i] & 0xf]; @@ -1219,7 +1219,7 @@ ltype(lua_State *L) { static void typeclosure(lua_State *L) { - static const char * typename[] = { + static const char * typename_[] = { "number", // 1 "boolean", // 2 "table", // 3 @@ -1236,9 +1236,9 @@ typeclosure(lua_State *L) { "unsupported", // 14 }; int i; - int n = sizeof(typename)/sizeof(typename[0]); + int n = sizeof(typename_)/sizeof(typename_[0]); for (i=0;i SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, chunksz, 0); + buffer = (uint8_t*)lua_newuserdatauv(L, chunksz, 0); } int i; for (i=0;i<(int)textsz-7;i+=8) { @@ -503,7 +503,7 @@ ldesdecode(lua_State *L) { uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (textsz > SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, textsz, 0); + buffer = (uint8_t*)lua_newuserdatauv(L, textsz, 0); } for (i=0;i SMALL_CHUNK/2) { - buffer = lua_newuserdatauv(L, sz * 2, 0); + buffer = (char*)lua_newuserdatauv(L, sz * 2, 0); } int i; for (i=0;i SMALL_CHUNK*2) { - buffer = lua_newuserdatauv(L, sz / 2, 0); + buffer = (char*)lua_newuserdatauv(L, sz / 2, 0); } int i; for (i=0;i SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, encode_sz, 0); + buffer = (char*)lua_newuserdatauv(L, encode_sz, 0); } int i,j; j=0; @@ -953,7 +953,7 @@ lb64decode(lua_State *L) { char tmp[SMALL_CHUNK]; char *buffer = tmp; if (decode_sz > SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, decode_sz, 0); + buffer = (char*)lua_newuserdatauv(L, decode_sz, 0); } int i,j; int output = 0; diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index 4ceffa066..1f6c6b692 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -89,10 +89,10 @@ buffer_reserve(struct buffer *b, int sz) { } while (b->cap <= b->size + sz); if (b->ptr == b->buffer) { - b->ptr = skynet_malloc(b->cap); + b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { - b->ptr = skynet_realloc(b->ptr, b->cap); + b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } @@ -208,7 +208,7 @@ static int op_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); - struct { + struct reply_type { // int32_t length; // total message size, including this int32_t request_id; // identifier for this message int32_t response_id; // requestID from the original request @@ -218,7 +218,8 @@ op_reply(lua_State *L) { int32_t cursor_id[2]; int32_t starting; int32_t number; - } const *reply = (const void *)data; + }; + const struct reply_type* reply = (const struct reply_type*)data; if (data_len < sizeof(*reply)) { lua_pushboolean(L, 0); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index 4dd3ba822..ea98ffd59 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -24,41 +24,49 @@ #else +#if defined (__cplusplus) +#include +#define STD_ std:: +#define atomic_value_type_(p, v) decltype((p)->load())(v) +#else #include +#define STD_ +#define atomic_value_type_(p, v) v +#endif -#define ATOM_INT atomic_int -#define ATOM_POINTER atomic_uintptr_t -#define ATOM_SIZET atomic_size_t -#define ATOM_ULONG atomic_ulong -#define ATOM_INIT(ref, v) atomic_init(ref, v) -#define ATOM_LOAD(ptr) atomic_load(ptr) -#define ATOM_STORE(ptr, v) atomic_store(ptr, v) +#define ATOM_INT STD_ atomic_int +#define ATOM_POINTER STD_ atomic_uintptr_t +#define ATOM_SIZET STD_ atomic_size_t +#define ATOM_ULONG STD_ atomic_ulong +#define ATOM_INIT(ref, v) STD_ atomic_init(ref, v) +#define ATOM_LOAD(ptr) STD_ atomic_load(ptr) +#define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v) static inline int -ATOM_CAS(atomic_int *ptr, int oval, int nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } -#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1) -#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1) -#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n) -#define ATOM_FSUB(ptr,n) atomic_fetch_sub(ptr, n) -#define ATOM_FAND(ptr,n) atomic_fetch_and(ptr, n) +#define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1)) +#define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1)) +#define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n)) +#define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n)) +#define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n)) #endif diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 7726904d6..d0adcc568 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -47,10 +47,11 @@ spinlock_destroy(struct spinlock *lock) { #else // __STDC_NO_ATOMICS__ -#include -#define atomic_test_and_set_(ptr) atomic_exchange_explicit(ptr, 1, memory_order_acquire) -#define atomic_clear_(ptr) atomic_store_explicit(ptr, 0, memory_order_release); -#define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed) +#include "atomic.h" + +#define atomic_test_and_set_(ptr) STD_ atomic_exchange_explicit(ptr, 1, STD_ memory_order_acquire) +#define atomic_clear_(ptr) STD_ atomic_store_explicit(ptr, 0, STD_ memory_order_release); +#define atomic_load_relaxed_(ptr) STD_ atomic_load_explicit(ptr, STD_ memory_order_relaxed) #if defined(__x86_64__) #include // For _mm_pause @@ -60,12 +61,12 @@ spinlock_destroy(struct spinlock *lock) { #endif struct spinlock { - atomic_int lock; + STD_ atomic_int lock; }; static inline void spinlock_init(struct spinlock *lock) { - atomic_init(&lock->lock, 0); + STD_ atomic_init(&lock->lock, 0); } static inline void From 2d42adb739575dbbb3702fde2dd977d873225a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sun, 10 Apr 2022 15:35:46 +0800 Subject: [PATCH 430/565] fix typo: remove duplicate include (#1570) --- skynet-src/skynet_server.c | 1 - 1 file changed, 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e4d7ea3a8..000926eb2 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -10,7 +10,6 @@ #include "skynet_monitor.h" #include "skynet_imp.h" #include "skynet_log.h" -#include "skynet_timer.h" #include "spinlock.h" #include "atomic.h" From 0ddd938400d2e519c6795f57964ebd42e272ed56 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 Apr 2022 10:08:07 +0800 Subject: [PATCH 431/565] Clear callback function in main thread --- lualib-src/lua-skynet.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index fbbc7fbde..6b686b569 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -106,6 +106,8 @@ lcallback(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); + // reload callback function, See _cb() + lua_settop(gL, 0); if (forward) { skynet_callback(context, gL, forward_cb); From e37eb3cba0cd92aae7c51d74e650c9629239a0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Tue, 12 Apr 2022 11:11:08 +0800 Subject: [PATCH 432/565] Callback (#1571) * Use independent thread for callback * bugfix: use new cb_ctx when changing callback --- lualib-src/lua-skynet.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 6b686b569..dc3765bb6 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -46,18 +46,16 @@ traceback (lua_State *L) { return 1; } +struct callback_context { + lua_State *L; +}; + static int _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { - lua_State *L = ud; + struct callback_context *cb_ctx = (struct callback_context *)ud; + lua_State *L = cb_ctx->L; int trace = 1; int r; - int top = lua_gettop(L); - if (top == 0) { - lua_pushcfunction(L, traceback); - lua_rawgetp(L, LUA_REGISTRYINDEX, _cb); - } else { - assert(top == 2); - } lua_pushvalue(L,2); lua_pushinteger(L, type); @@ -102,17 +100,17 @@ lcallback(lua_State *L) { int forward = lua_toboolean(L, 2); luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); - lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); - - lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); - lua_State *gL = lua_tothread(L,-1); - // reload callback function, See _cb() - lua_settop(gL, 0); + struct callback_context *cb_ctx = (struct callback_context *)lua_newuserdata(L, sizeof(*cb_ctx)); + cb_ctx->L = lua_newthread(L); + lua_pushcfunction(cb_ctx->L, traceback); + lua_setuservalue(L, -2); + lua_setfield(L, LUA_REGISTRYINDEX, "callback_context"); + lua_xmove(L, cb_ctx->L, 1); if (forward) { - skynet_callback(context, gL, forward_cb); + skynet_callback(context, cb_ctx, forward_cb); } else { - skynet_callback(context, gL, _cb); + skynet_callback(context, cb_ctx, _cb); } return 0; From ee103e97cdda1eb35639ae6510afe7e327f5089c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 13 Apr 2022 22:36:47 +0800 Subject: [PATCH 433/565] Fix handle register when slot[0] is null will segmentation fault (#1574) --- examples/config.handle | 10 ++++++++++ skynet-src/skynet_handle.c | 8 +++++--- test/testhandle.lua | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 examples/config.handle create mode 100644 test/testhandle.lua diff --git a/examples/config.handle b/examples/config.handle new file mode 100644 index 000000000..da1fe91f4 --- /dev/null +++ b/examples/config.handle @@ -0,0 +1,10 @@ +include "config.path" + +thread = 8 +logger = "skynet.log" +logpath = "." +harbor = 0 +start = "testhandle" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +cpath = root.."cservice/?.so" +--daemon = "./skynet.pid" diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index f20324ee7..249748a2e 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -60,9 +60,11 @@ skynet_handle_register(struct skynet_context *ctx) { struct skynet_context ** new_slot = skynet_malloc(s->slot_size * 2 * sizeof(struct skynet_context *)); memset(new_slot, 0, s->slot_size * 2 * sizeof(struct skynet_context *)); for (i=0;islot_size;i++) { - int hash = skynet_context_handle(s->slot[i]) & (s->slot_size * 2 - 1); - assert(new_slot[hash] == NULL); - new_slot[hash] = s->slot[i]; + if (s->slot[i]) { + int hash = skynet_context_handle(s->slot[i]) & (s->slot_size * 2 - 1); + assert(new_slot[hash] == NULL); + new_slot[hash] = s->slot[i]; + } } skynet_free(s->slot); s->slot = new_slot; diff --git a/test/testhandle.lua b/test/testhandle.lua new file mode 100644 index 000000000..bcef897cf --- /dev/null +++ b/test/testhandle.lua @@ -0,0 +1,33 @@ +local skynet = require "skynet" +require "skynet.manager" + +local mod = ... +if mod == "slave" then + +skynet.start(function() + skynet.error("addr:", skynet.self()) +end) + +else + +skynet.start(function() + skynet.newservice("debug_console",8000) + skynet.error("master addr:", skynet.self()) + + skynet.newservice("testhandle", "slave") + skynet.newservice("testhandle", "slave") + skynet.newservice("testhandle", "slave") + skynet.newservice("testhandle", "slave") + skynet.newservice("testhandle", "slave") + skynet.newservice("testhandle", "slave") + + while true do + local addr = skynet.newservice("testhandle", "slave") + skynet.kill(addr) + if addr > 0xfffff0 then + break + end + end +end) + +end From 6ad379b7a745d78d241d6f97428a7ecabf5e7ace Mon Sep 17 00:00:00 2001 From: "IAN.Z" <1274803758@qq.com> Date: Thu, 14 Apr 2022 12:35:54 +0800 Subject: [PATCH 434/565] feat: add mongo safe batch update (#1575) Co-authored-by: zhuyin.zhu --- lualib/skynet/db/mongo.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 0eb14e8f8..a249155b2 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -394,6 +394,21 @@ function mongo_collection:safe_update(selector, update, upsert, multi) return werror(r) end +function mongo_collection:safe_batch_update(updates) + local updates_tb = {} + for i = 1, #updates do + updates_tb[i] = bson_encode({ + q = updates[i].query, + u = updates[i].update, + upsert = updates[i].upsert, + multi = updates[i].multi, + }) + end + + local r = self.database:runCommand("update", self.name, "updates", updates_tb) + return werror(r) +end + function mongo_collection:delete(selector, single) local sock = self.connection.__sock local pack = driver.delete(self.full_name, single, bson_encode(selector)) From c4da547259fc611c5b49b850e023fd611f41efed Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 15 Apr 2022 08:19:20 +0800 Subject: [PATCH 435/565] fix retireall, see #1576 --- skynet-src/skynet_handle.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index 249748a2e..28bd1efeb 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -121,13 +121,13 @@ skynet_handle_retireall() { rwlock_rlock(&s->lock); struct skynet_context * ctx = s->slot[i]; uint32_t handle = 0; - if (ctx) + if (ctx) { handle = skynet_context_handle(ctx); + ++n; + } rwlock_runlock(&s->lock); if (handle != 0) { - if (skynet_handle_retire(handle)) { - ++n; - } + skynet_handle_retire(handle); } } if (n==0) From bcd012f9cf391889885fd8d1c00aceb6730f28bf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 15 Apr 2022 10:57:37 +0800 Subject: [PATCH 436/565] uintptr_t need stdint.h --- skynet-src/atomic.h | 1 + 1 file changed, 1 insertion(+) diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index ea98ffd59..0ce185e3c 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -4,6 +4,7 @@ #ifdef __STDC_NO_ATOMICS__ #include +#include #define ATOM_INT volatile int #define ATOM_POINTER volatile uintptr_t From 013cb42122da6f532352e351972ff3a8eca9078f Mon Sep 17 00:00:00 2001 From: caiyiheng Date: Fri, 15 Apr 2022 16:20:00 +0800 Subject: [PATCH 437/565] feat: add mongo safe batch delete (#1577) --- lualib/skynet/db/mongo.lua | 12 ++++++++++++ test/testmongodb.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index a249155b2..1fa34b15f 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -423,6 +423,18 @@ function mongo_collection:safe_delete(selector, single) return werror(r) end +function mongo_collection:safe_batch_delete(selectors, single) + local delete_tb = {} + for i = 1, #selectors do + delete_tb[i] = bson_encode({ + q = selectors[i], + limit = single and 1 or 0, + }) + end + local r = self.database:runCommand("delete", self.name, "deletes", delete_tb) + return werror(r) +end + function mongo_collection:findOne(query, selector) local conn = self.connection local request_id = conn:genId() diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 0a1029422..9f1fd8105 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -186,6 +186,32 @@ local function test_safe_batch_insert() assert(length == ret:count(), "test safe batch insert failed") end +local function test_safe_batch_delete() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:drop() + + local docs, length = {}, 10 + for i = 1, length do + table.insert(docs, {test_key = i}) + end + + db.testcoll:safe_batch_insert(docs) + + docs = {} + local del_num = 5 + for i = 1, del_num do + table.insert(docs, {test_key = i}) + end + + db.testcoll:safe_batch_delete(docs) + + local ret = db.testcoll:find() + assert(length == ret:count(), "test safe batch delete failed") +end + skynet.start(function() if username then print("Test auth") @@ -203,5 +229,7 @@ skynet.start(function() test_expire_index() print("test safe batch insert") test_safe_batch_insert() + print("test safe batch delete") + test_safe_batch_delete() print("mongodb test finish."); end) From 341b7924a07f5d63d4a79c8c9f8c6958ae07282b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 20 Apr 2022 17:18:23 +0800 Subject: [PATCH 438/565] remove unused function skynet_module_insert #1579 (#1580) --- skynet-src/skynet_module.c | 13 ------------- skynet-src/skynet_module.h | 1 - 2 files changed, 14 deletions(-) diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index a473e4229..b3e432951 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -129,19 +129,6 @@ skynet_module_query(const char * name) { return result; } -void -skynet_module_insert(struct skynet_module *mod) { - 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; - - SPIN_UNLOCK(M) -} - void * skynet_module_instance_create(struct skynet_module *m) { if (m->create) { diff --git a/skynet-src/skynet_module.h b/skynet-src/skynet_module.h index 52b5e181d..da81cdc7d 100644 --- a/skynet-src/skynet_module.h +++ b/skynet-src/skynet_module.h @@ -17,7 +17,6 @@ struct skynet_module { skynet_dl_signal signal; }; -void skynet_module_insert(struct skynet_module *mod); 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); From 448fe14b9efa2b752a8fc27891c0bf43b09ee733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Fri, 22 Apr 2022 16:35:46 +0800 Subject: [PATCH 439/565] fix comment: profile default is on. (#1581) --- 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 000926eb2..4cc726e34 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -65,7 +65,7 @@ struct skynet_node { int init; uint32_t monitor_exit; pthread_key_t handle_key; - bool profile; // default is off + bool profile; // default is on }; static struct skynet_node G_NODE; From acefd8de2770dd135549681c9a2d98db8b528cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 27 Apr 2022 16:24:27 +0800 Subject: [PATCH 440/565] Update issue templates --- .github/ISSUE_TEMPLATE/custom.md | 10 ++++++++++ .github/ISSUE_TEMPLATE/readme-first.md | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/custom.md create mode 100644 .github/ISSUE_TEMPLATE/readme-first.md diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md new file mode 100644 index 000000000..48d5f81fa --- /dev/null +++ b/.github/ISSUE_TEMPLATE/custom.md @@ -0,0 +1,10 @@ +--- +name: Custom issue template +about: Describe this issue template's purpose here. +title: '' +labels: '' +assignees: '' + +--- + + diff --git a/.github/ISSUE_TEMPLATE/readme-first.md b/.github/ISSUE_TEMPLATE/readme-first.md new file mode 100644 index 000000000..3b097c3e0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/readme-first.md @@ -0,0 +1,23 @@ +--- +name: Readme First +about: Issues is bug report only +title: '' +labels: '' +assignees: '' + +--- + +The **Issues** is for bug report only. + +Goto **Discussions** for feature request , questions, etc. + +**Update to master branch HEAD first**, and + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior. + +**Additional context** +Add any other context about the problem here. From 6d86016247a1dd36f9cebae714e50d4e4c3ff603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 27 Apr 2022 16:25:06 +0800 Subject: [PATCH 441/565] Update issue templates --- .github/ISSUE_TEMPLATE/custom.md | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/custom.md diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md deleted file mode 100644 index 48d5f81fa..000000000 --- a/.github/ISSUE_TEMPLATE/custom.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -name: Custom issue template -about: Describe this issue template's purpose here. -title: '' -labels: '' -assignees: '' - ---- - - From 73c0e306cffc63568a3d7bb599f5faa8e48ba950 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2022 10:41:58 +0800 Subject: [PATCH 442/565] update jemalloc 5.3.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index ea6b3e973..54eaed1d8 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit ea6b3e973b477b8061e0076bb257dbd7f3faa756 +Subproject commit 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c From 389146167f080c600ad0ecc9c93092acc60e3c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 30 May 2022 23:18:08 +0800 Subject: [PATCH 443/565] export queryproto (#1605) Co-authored-by: zixun --- lualib/sproto.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 757140ee4..f5792099d 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -107,6 +107,7 @@ local function queryproto(self, pname) return v end +sproto.queryproto = queryproto function sproto:exist_proto(pname) local v = self.__pcache[pname] From e98378e874690d9f6bb34deda5f48bf7561238ba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Jun 2022 08:03:58 +0800 Subject: [PATCH 444/565] Fix #1609 --- lualib/snax/msgserver.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index 4c6f17246..5ce9c0e76 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -100,8 +100,10 @@ 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 + if connection[u.fd] then + gateserver.closeclient(u.fd) + connection[u.fd] = nil + end end end @@ -153,11 +155,15 @@ function server.start(conf) 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 + -- double check, conf.disconnect_handler may close fd + if connection[fd] then + c.fd = nil + connection[fd] = nil + gateserver.closeclient(fd) + end end end From fad27d777566c08d1d497d8ffbb846008d48d75c Mon Sep 17 00:00:00 2001 From: dxb218 Date: Sat, 11 Jun 2022 20:22:22 +0800 Subject: [PATCH 445/565] Update interface.lua (#1612) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit string.format语法错误 --- 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 5a403cfd4..73023e385 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -26,7 +26,7 @@ return function (name , G, loader) error (string.format("%s method only support string", group)) end if type(func) ~= "function" then - error (string.format("%s.%s must be function"), group, name) + error (string.format("%s.%s must be function", group, name)) end if tmp[name] then error (string.format("%s.%s duplicate definition", group, name)) From 3911e0738b5bf39849e5a5c1f28015a62bc69e64 Mon Sep 17 00:00:00 2001 From: fanyh <8852086@qq.com> Date: Mon, 20 Jun 2022 20:06:08 +0800 Subject: [PATCH 446/565] =?UTF-8?q?=E4=BF=AE=E6=94=B9clusterd=20node=20clo?= =?UTF-8?q?sed=20=E5=90=8E=E5=86=8D=E6=AC=A1=E5=91=BD=E4=B8=AD=5F=5Findex?= =?UTF-8?q?=20=E4=BC=9A=E7=BB=A7=E7=BB=AD=E8=A7=A6=E5=8F=91=20call=20chang?= =?UTF-8?q?enode=20(#1614)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/clusterd.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index d13eded52..d9f61cd49 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -5,6 +5,7 @@ local cluster = require "skynet.cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} local node_sender = {} +local node_sender_closed = {} local command = {} local config = {} local nodename = cluster.nodename() @@ -56,17 +57,21 @@ local function open_channel(t, key) if succ then t[key] = c ct.channel = c + node_sender_closed[key] = nil else err = string.format("changenode [%s] (%s:%s) failed", key, host, port) end elseif address == false then c = node_sender[key] - if c == nil then - -- no sender, always succ + if c == nil or node_sender_closed[key] then + -- no sender or closed, always succ succ = true else -- trun off the sender succ, err = pcall(skynet.call, c, "lua", "changenode", false) + if succ then --trun off failed, wait next index todo turn off + node_sender_closed[key] = true + end end else err = string.format("cluster node [%s] is absent.", key) From 1b543065091b5185bef4a92bc186ede4ab8e2c62 Mon Sep 17 00:00:00 2001 From: yfy <273461474@qq.com> Date: Fri, 5 Aug 2022 10:02:11 +0800 Subject: [PATCH 447/565] =?UTF-8?q?http=E8=AF=B7=E6=B1=82=E5=A4=B4host?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=94=B9=E4=B8=BAHost=20(#1623)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/http/internal.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index db56ab2b1..204767be5 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -168,8 +168,8 @@ function M.request(interface, method, host, url, recvheader, header, content) local write = interface.write local header_content = "" if header then - if not header.host then - header.host = host + 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) From 0c1491ba999ea7e633df8151f4d71d421203b5a3 Mon Sep 17 00:00:00 2001 From: yfy <273461474@qq.com> Date: Sat, 6 Aug 2022 06:05:09 +0800 Subject: [PATCH 448/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhttps=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=95=B0=E6=8D=AE=E8=BF=94=E5=9B=9E0=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20(#1622)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复https读取数据返回0报错 ./skynet/lualib/skynet.lua:861: ./skynet/lualib/skynet.lua:333: ./skynet/lualib/http/httpc.lua:109: ./skynet/lualib/http/tlshelper.lua:54: SSL_read error:6 stack traceback: [C]: in function 'error' ./skynet/lualib/http/httpc.lua:109: in function 'http.httpc.request' * http请求header中host头修改为默认的Host名字 --- lualib-src/ltls.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 8c50fdea8..28488de04 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -219,14 +219,17 @@ _ltls_context_read(lua_State* L) { do { read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); - if(read <= 0) { + if(read < 0) { int err = SSL_get_error(tls_p->ssl, read); ERR_clear_error(); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { break; } luaL_error(L, "SSL_read error:%d", err); - }else if(read <= sizeof(outbuff)) { + }else if(read == 0){ + break; + } + else if(read <= sizeof(outbuff)) { luaL_addlstring(&b, outbuff, read); }else { luaL_error(L, "invalid SSL_read:%d", read); From 8efad5363178eae5cedbf821a85ec78859a9f969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 17 Aug 2022 13:12:51 +0800 Subject: [PATCH 449/565] Emfile (#1626) * Close the connection when EMFILE, See #1625 * close reserve_fd --- skynet-src/socket_server.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index d1a912eea..f726593b0 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -114,6 +114,7 @@ struct socket { struct socket_server { volatile uint64_t time; + int reserve_fd; // for EMFILE int recvctrl_fd; int sendctrl_fd; int checkctrl; @@ -405,6 +406,7 @@ socket_server_create(uint64_t time) { ss->recvctrl_fd = fd[0]; ss->sendctrl_fd = fd[1]; ss->checkctrl = 1; + ss->reserve_fd = dup(1); // reserve an extra fd for EMFILE for (i=0;islot[i]; @@ -525,6 +527,8 @@ socket_server_release(struct socket_server *ss) { close(ss->sendctrl_fd); close(ss->recvctrl_fd); sp_release(ss->event_fd); + if (ss->reserve_fd >= 0) + close(ss->reserve_fd); FREE(ss); } @@ -1581,6 +1585,16 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message result->id = s->id; result->ud = 0; result->data = strerror(errno); + + // See https://stackoverflow.com/questions/47179793/how-to-gracefully-handle-accept-giving-emfile-and-close-the-connection + if (ss->reserve_fd >= 0) { + close(ss->reserve_fd); + client_fd = accept(s->fd, &u.s, &len); + if (client_fd >= 0) { + close(client_fd); + } + ss->reserve_fd = dup(1); + } return -1; } else { return 0; From dfc706615e585713ecf9384f7a3cb71ed72dca6b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Aug 2022 11:51:58 +0800 Subject: [PATCH 450/565] update lua --- 3rd/lua/lapi.c | 20 ++- 3rd/lua/lauxlib.c | 88 ++++++++----- 3rd/lua/lcode.c | 70 +++++----- 3rd/lua/ldebug.c | 5 +- 3rd/lua/ldo.c | 42 +++--- 3rd/lua/ldo.h | 10 +- 3rd/lua/lfunc.c | 7 +- 3rd/lua/lfunc.h | 2 +- 3rd/lua/lgc.c | 63 +++++---- 3rd/lua/loadlib.c | 9 +- 3rd/lua/lobject.c | 34 +++-- 3rd/lua/lobject.h | 2 + 3rd/lua/lparser.c | 17 +-- 3rd/lua/lstring.c | 6 +- 3rd/lua/lua.c | 35 +++-- 3rd/lua/lua.h | 4 +- 3rd/lua/luaconf.h | 2 +- 3rd/lua/lvm.c | 133 +++++++++++++------ 3rd/lua/{Makefile => makefile} | 231 +++++++++++++++------------------ Makefile | 2 +- 20 files changed, 450 insertions(+), 332 deletions(-) rename 3rd/lua/{Makefile => makefile} (52%) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 6a28c54fb..6678f7bd7 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -114,13 +114,8 @@ LUA_API int lua_checkstack (lua_State *L, int 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 */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = luaD_growstack(L, n, 0); - } + else /* need to grow stack */ + res = luaD_growstack(L, n, 0); if (res && ci->top < L->top + n) ci->top = L->top + n; /* adjust frame top */ lua_unlock(L); @@ -202,7 +197,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { newtop = L->top + diff; if (diff < 0 && L->tbclist >= newtop) { lua_assert(hastocloseCfunc(ci->nresults)); - luaF_close(L, newtop, CLOSEKTOP, 0); + newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } L->top = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); @@ -215,8 +210,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { level = index2stack(L, idx); api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, "no variable to close at given level"); - luaF_close(L, level, CLOSEKTOP, 0); - level = index2stack(L, idx); /* stack may be moved */ + level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); lua_unlock(L); } @@ -1129,16 +1123,18 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { } LUA_API void lua_sharefunction (lua_State *L, int index) { + LClosure *f; if (!lua_isfunction(L,index) || lua_iscfunction(L,index)) luaG_runerror(L, "Only Lua function can share"); - LClosure *f = cast(LClosure *, lua_topointer(L, index)); + f = cast(LClosure *, lua_topointer(L, index)); luaF_shareproto(f->p); } LUA_API void lua_sharestring (lua_State *L, int index) { + TString *ts; if (lua_type(L,index) != LUA_TSTRING) luaG_runerror(L, "need a string to share"); - TString *ts = tsvalue(index2value(L,index)); + ts = tsvalue(index2value(L,index)); luaS_share(ts); } diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 51cbf03e4..7e674f198 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -526,7 +526,8 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. +** bytes. (The test for "double is not big enough" also gets the +** case when the multiplication by 2 overflows.) */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = B->size * 2; /* double buffer size */ @@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { ** box (if existent) is not on the top of the stack. So, instead of ** calling 'luaL_addlstring', it replicates the code using -2 as the ** last argument to 'prepbuffsize', signaling that the box is (or will -** be) bellow the string being added to the buffer. (Box creation can +** be) below the string being added to the buffer. (Box creation can ** trigger an emergency GC, so we should not remove the string from the ** stack before we have the space guaranteed.) */ @@ -739,17 +740,18 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { } -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ +/* +** Skip an optional BOM at the start of a stream. If there is an +** incomplete BOM (the first character is correct but the rest is +** not), returns the first character anyway to force an error +** (as no chunk can start with 0xEF). +*/ +static int skipBOM (FILE *f) { + int c = getc(f); /* read first character */ + if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ + return getc(f); /* ignore BOM and return next char */ + else /* no (valid) BOM */ + return c; /* return first character */ } @@ -760,13 +762,13 @@ static int skipBOM (LoadF *lf) { ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); +static int skipcomment (FILE *f, int *cp) { + int c = *cp = skipBOM(f); if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ - c = getc(lf->f); + c = getc(f); } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ + *cp = getc(f); /* next character after comment, if present */ return 1; /* there was a comment */ } else return 0; /* no comment */ @@ -788,12 +790,16 @@ LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ + lf.n = 0; + if (skipcomment(lf.f, &c)) /* read initial portion */ + lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ + if (c == LUA_SIGNATURE[0]) { /* binary file? */ + lf.n = 0; /* remove possible newline */ + if (filename) { /* "real" file? */ + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); + skipcomment(lf.f, &c); /* re-read initial portion */ + } } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ @@ -1116,7 +1122,7 @@ struct codecache { static struct codecache CC; static void -clearcache() { +clearcache(void) { if (CC.L == NULL) return; SPIN_LOCK(&CC) @@ -1126,10 +1132,13 @@ clearcache() { } static void -init() { +init(void) { CC.L = luaL_newstate(); } + +void luaL_initcodecache(void); + LUALIB_API void luaL_initcodecache(void) { SPIN_INIT(&CC); @@ -1137,13 +1146,15 @@ luaL_initcodecache(void) { static const void * load_proto(const char *key) { + lua_State *L; + const void * result; if (CC.L == NULL) return NULL; SPIN_LOCK(&CC) - lua_State *L = CC.L; + L = CC.L; lua_pushstring(L, key); lua_rawget(L, LUA_REGISTRYINDEX); - const void * result = lua_touserdata(L, -1); + result = lua_touserdata(L, -1); lua_pop(L, 1); SPIN_UNLOCK(&CC) @@ -1199,9 +1210,10 @@ static int cache_mode(lua_State *L) { "ON", NULL, }; + int t,r; if (lua_isnoneornil(L,1)) { - int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); - int r = lua_tointeger(L, -1); + t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); + r = lua_tointeger(L, -1); if (t == LUA_TNUMBER) { if (r < 0 || r >= CACHE_ON) { r = CACHE_ON; @@ -1212,7 +1224,7 @@ static int cache_mode(lua_State *L) { lua_pushstring(L, lst[r]); return 1; } - int t = luaL_checkoption(L, 1, "OFF" , lst); + t = luaL_checkoption(L, 1, "OFF" , lst); lua_pushinteger(L, t); lua_rawsetp(L, LUA_REGISTRYINDEX, &cache_key); return 0; @@ -1221,10 +1233,14 @@ static int cache_mode(lua_State *L) { LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { int level = cache_level(L); - if (level == CACHE_OFF || filename == NULL) { + const void * proto; + lua_State * eL; + int err; + const void * oldv; + if (level == CACHE_OFF) { return luaL_loadfilex_(L, filename, mode); } - const void * proto = load_proto(filename); + proto = load_proto(filename); if (proto) { lua_clonefunction(L, proto); return LUA_OK; @@ -1232,12 +1248,12 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, if (level == CACHE_EXIST) { return luaL_loadfilex_(L, filename, mode); } - lua_State * eL = luaL_newstate(); + eL = luaL_newstate(); if (eL == NULL) { lua_pushliteral(L, "New state failed"); return LUA_ERRMEM; } - int err = luaL_loadfilex_(eL, filename, mode); + err = luaL_loadfilex_(eL, filename, mode); if (err != LUA_OK) { size_t sz = 0; const char * msg = lua_tolstring(eL, -1, &sz); @@ -1247,7 +1263,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } lua_sharefunction(eL, -1); proto = lua_topointer(eL, -1); - const void * oldv = save_proto(filename, proto); + oldv = save_proto(filename, proto); if (oldv) { lua_close(eL); lua_clonefunction(L, oldv); @@ -1266,6 +1282,8 @@ cache_clear(lua_State *L) { return 0; } +int luaopen_cache(lua_State *L); + LUAMOD_API int luaopen_cache(lua_State *L) { luaL_Reg l[] = { { "clear", cache_clear }, diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 06425a1db..911dbd5f1 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1391,7 +1391,10 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { - int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ + int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ + /* 'e1' must be already in a register or it is a constant */ + lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || + e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, cast(TMS, (op - OP_ADD) + TM_ADD)); @@ -1410,6 +1413,18 @@ static void codebini (FuncState *fs, OpCode op, } +/* +** Code binary operators with K operand. +*/ +static void codebinK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + TMS event = cast(TMS, opr + TM_ADD); + int v2 = e2->u.info; /* K index */ + OpCode op = cast(OpCode, opr + OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); +} + + /* Try to code a binary operator negating its second operand. ** For the metamethod, 2nd operand must keep its original value. */ @@ -1437,24 +1452,28 @@ static void swapexps (expdesc *e1, expdesc *e2) { } +/* +** Code binary operators with no constant operand. +*/ +static void codebinNoK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + OpCode op = cast(OpCode, opr + OP_ADD); + if (flip) + swapexps(e1, e2); /* back to original order */ + codebinexpval(fs, op, e1, e2, line); /* use standard operators */ +} + + /* ** Code arithmetic operators ('+', '-', ...). If second operand is a ** constant in the proper range, use variant opcodes with K operands. */ static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = cast(TMS, opr + TM_ADD); - if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ - int v2 = e2->u.info; /* K index */ - OpCode op = cast(OpCode, opr + OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); - } - else { /* 'e2' is neither an immediate nor a K operand */ - OpCode op = cast(OpCode, opr + OP_ADD); - if (flip) - swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - } + if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* 'e2' is neither an immediate nor a K operand */ + codebinNoK(fs, opr, e1, e2, flip, line); } @@ -1478,28 +1497,20 @@ static void codecommutative (FuncState *fs, BinOpr op, /* -** Code bitwise operations; they are all associative, so the function +** Code bitwise operations; they are all commutative, so the function ** tries to put an integer constant as the 2nd operand (a K operand). */ static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { int flip = 0; - int v2; - OpCode op; - if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + if (e1->k == VKINT) { swapexps(e1, e2); /* 'e2' will be the constant operand */ flip = 1; } - else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ - op = cast(OpCode, opr + OP_ADD); - codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ - return; - } - v2 = e2->u.info; /* index in K array */ - op = cast(OpCode, opr + OP_ADDK); - lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, - cast(TMS, opr + TM_ADD)); + if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* no constants */ + codebinNoK(fs, opr, e1, e2, flip, line); } @@ -1551,7 +1562,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { op = OP_EQI; r2 = im; /* immediate operand */ } - else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ + else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } @@ -1611,7 +1622,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_SHL: case OPR_SHR: { if (!tonumeral(v, NULL)) luaK_exp2anyreg(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ + /* else keep numeral, which may be folded or used as an immediate + operand */ break; } case OPR_EQ: case OPR_NE: { diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index a716d95e2..fa15eaf68 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -824,8 +824,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ + if (isLua(ci)) { /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); + setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */ + L->top--; + } luaG_errormsg(L); } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index a48e35f9d..419b3db93 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -213,7 +213,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { /* -** Try to grow the stack by at least 'n' elements. when 'raiseerror' +** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { @@ -227,7 +227,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { luaD_throw(L, LUA_ERRERR); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } - else { + else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ int needed = cast_int(L->top - L->stack) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ @@ -236,17 +236,20 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = needed; if (l_likely(newsize <= LUAI_MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); - else { /* stack overflow */ - /* add extra size to be able to handle the error message */ - luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); - if (raiseerror) - luaG_runerror(L, "stack overflow"); - return 0; - } } + /* else stack overflow */ + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + return 0; } +/* +** Compute how much of the stack is being used, by computing the +** maximum top of all call frames in the stack and the current top. +*/ static int stackinuse (lua_State *L) { CallInfo *ci; int res; @@ -254,7 +257,7 @@ static int stackinuse (lua_State *L) { for (ci = L->ci; ci != NULL; ci = ci->previous) { if (lim < ci->top) lim = ci->top; } - lua_assert(lim <= L->stack_last); + lua_assert(lim <= L->stack_last + EXTRA_STACK); res = cast_int(lim - L->stack) + 1; /* part of stack in use */ if (res < LUA_MINSTACK) res = LUA_MINSTACK; /* ensure a minimum size */ @@ -427,14 +430,15 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { break; default: /* two/more results and/or to-be-closed variables */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ - ptrdiff_t savedres = savestack(L, res); L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ L->ci->u2.nres = nres; - luaF_close(L, res, CLOSEKTOP, 1); + res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; - if (L->hookmask) /* if needed, call hook after '__close's */ + if (L->hookmask) { /* if needed, call hook after '__close's */ + ptrdiff_t savedres = savestack(L, res); rethook(L, L->ci, nres); - res = restorestack(L, savedres); /* close and hook can move stack */ + res = restorestack(L, savedres); /* hook can move stack */ + } wanted = decodeNresults(wanted); if (wanted == LUA_MULTRET) wanted = nres; /* we want all results */ @@ -598,12 +602,17 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** Call a function (C or Lua) through C. 'inc' can be 1 (increment ** number of recursive invocations in the C stack) or nyci (the same ** plus increment number of non-yieldable calls). +** This function can be called with some use of EXTRA_STACK, so it should +** check the stack before doing anything else. 'luaD_precall' already +** does that. */ l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; - if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { + checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ luaE_checkcstack(L); + } if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ @@ -651,8 +660,7 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { else { /* error */ StkId func = restorestack(L, ci->u2.funcidx); L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ - luaF_close(L, func, status, 1); /* can yield or raise an error */ - func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ + func = luaF_close(L, func, status, 1); /* can yield or raise an error */ luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ setcistrecst(ci, LUA_OK); /* clear original status */ diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 911e67f66..4661aa007 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -37,6 +37,13 @@ /* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + +/* macro to check stack size and GC, preserving 'p' */ #define checkstackGCp(L,n,p) \ luaD_checkstackaux(L, n, \ ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ @@ -58,7 +65,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); +LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 88f6c7e42..17ff8aef8 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -210,7 +210,7 @@ void luaF_closeupval (lua_State *L, StkId level) { /* -** Remove firt element from the tbclist plus its dummy nodes. +** Remove first element from the tbclist plus its dummy nodes. */ static void poptbclist (lua_State *L) { StkId tbc = L->tbclist; @@ -224,9 +224,9 @@ static void poptbclist (lua_State *L) { /* ** Close all upvalues and to-be-closed variables up to the given stack -** level. +** level. Return restored 'level'. */ -void luaF_close (lua_State *L, StkId level, int status, int yy) { +StkId luaF_close (lua_State *L, StkId level, int status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist >= level) { /* traverse tbc's down to that level */ @@ -235,6 +235,7 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); } + return level; } diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 6126123c9..6e7b3ec37 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -54,7 +54,7 @@ LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); +LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 510d5f622..116fd070a 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1043,7 +1043,25 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** ======================================================= */ -static void setpause (global_State *g); + +/* +** Set the "time" to wait before starting a new GC cycle; cycle will +** start when memory use hits the threshold of ('estimate' * pause / +** PAUSEADJ). (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 threshold, debt; + int pause = getgcparam(g->gcpause); + l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ + lua_assert(estimate > 0); + threshold = (pause < MAX_LMEM / estimate) /* overflow? */ + ? estimate * pause /* no overflow */ + : MAX_LMEM; /* overflow; truncate to maximum */ + debt = gettotalbytes(g) - threshold; + if (debt > 0) debt = 0; + luaE_setdebt(g, debt); +} /* @@ -1291,6 +1309,15 @@ static void atomic2gen (lua_State *L, global_State *g) { } +/* +** Set debt for the next minor collection, which will happen when +** memory grows 'genminormul'%. +*/ +static void setminordebt (global_State *g) { + luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); +} + + /* ** Enter generational mode. Must go until the end of an atomic cycle ** to ensure that all objects are correctly marked and weak tables @@ -1303,6 +1330,7 @@ static lu_mem entergen (lua_State *L, global_State *g) { luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ numobjs = atomic(L); /* propagates all and then do the atomic stuff */ atomic2gen(L, g); + setminordebt(g); /* set debt assuming next cycle will be minor */ return numobjs; } @@ -1348,15 +1376,6 @@ static lu_mem fullgen (lua_State *L, global_State *g) { } -/* -** Set debt for the next minor collection, which will happen when -** memory grows 'genminormul'%. -*/ -static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); -} - - /* ** Does a major collection after last collection was a "bad collection". ** @@ -1428,8 +1447,8 @@ static void genstep (lua_State *L, global_State *g) { lu_mem numobjs = fullgen(L, g); /* do a major collection */ if (gettotalbytes(g) < majorbase + (majorinc / 2)) { /* collected at least half of memory growth since last major - collection; keep doing minor collections */ - setminordebt(g); + collection; keep doing minor collections. */ + lua_assert(g->lastatomic == 0); } else { /* bad collection */ g->lastatomic = numobjs; /* signal that last collection was bad */ @@ -1455,26 +1474,6 @@ static void genstep (lua_State *L, global_State *g) { */ -/* -** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * pause / -** PAUSEADJ). (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 threshold, debt; - int pause = getgcparam(g->gcpause); - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (pause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * pause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; - if (debt > 0) debt = 0; - luaE_setdebt(g, debt); -} - - /* ** Enter first sweep phase. ** The call to 'sweeptolive' makes the pointer point to an object diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 6f9fa3736..d792dffaa 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -708,8 +708,13 @@ static const luaL_Reg ll_funcs[] = { static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; + static const lua_CFunction searchers[] = { + searcher_preload, + searcher_Lua, + searcher_C, + searcher_Croot, + NULL + }; int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 301aa900b..a2c006098 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -386,29 +386,39 @@ void luaO_tostring (lua_State *L, TValue *obj) { ** =================================================================== */ -/* size for buffer space used by 'luaO_pushvfstring' */ -#define BUFVFS 200 +/* +** Size for buffer space used by 'luaO_pushvfstring'. It should be +** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, +** so that 'luaG_addinfo' can work directly on the buffer. +*/ +#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { lua_State *L; - int pushed; /* number of string pieces already on the stack */ + int pushed; /* true if there is a part of the result on the stack */ int blen; /* length of partial string in 'space' */ char space[BUFVFS]; /* holds last part of the result */ } BuffFS; /* -** Push given string to the stack, as part of the buffer, and -** join the partial strings in the stack into one. +** Push given string to the stack, as part of the result, and +** join it to previous partial result if there is one. +** It may call 'luaV_concat' while using one slot from EXTRA_STACK. +** This call cannot invoke metamethods, as both operands must be +** strings. It can, however, raise an error if the result is too +** long. In that case, 'luaV_concat' frees the extra slot before +** raising the error. */ -static void pushstr (BuffFS *buff, const char *str, size_t l) { +static void pushstr (BuffFS *buff, const char *str, size_t lstr) { lua_State *L = buff->L; - setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - L->top++; /* may use one extra slot */ - buff->pushed++; - luaV_concat(L, buff->pushed); /* join partial results into one */ - buff->pushed = 1; + setsvalue2s(L, L->top, luaS_newlstr(L, str, lstr)); + L->top++; /* may use one slot from EXTRA_STACK */ + if (!buff->pushed) /* no previous string on the stack? */ + buff->pushed = 1; /* now there is one */ + else /* join previous string with new one */ + luaV_concat(L, 2); } @@ -454,7 +464,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { /* -** Add a number to the buffer. +** Add a numeral to the buffer. */ static void addnum2buff (BuffFS *buff, TValue *num) { char *numbuff = getbuff(buff, MAXNUMBER2STR); diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 34e6a1948..d56de0b72 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -52,6 +52,8 @@ typedef union Value { lua_CFunction f; /* light C functions */ lua_Integer i; /* integer numbers */ lua_Number n; /* float numbers */ + /* not used, but may avoid warnings for uninitialized value */ + lu_byte ub; } Value; diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 3abe3d751..fe693b571 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -468,6 +468,7 @@ static void singlevar (LexState *ls, expdesc *var) { expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ lua_assert(var->k != VVOID); /* this one must exist */ + luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } @@ -673,19 +674,19 @@ static void leaveblock (FuncState *fs) { LexState *ls = fs->ls; int hasclose = 0; int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ - if (bl->isloop) /* fix pending breaks? */ + removevars(fs, bl->nactvar); /* remove block locals */ + lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ + if (bl->isloop) /* has to fix pending breaks? */ hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) + if (!hasclose && bl->previous && bl->upval) /* still need a 'close'? */ luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); fs->freereg = stklevel; /* free registers */ ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ + fs->bl = bl->previous; /* current block now is previous one */ + if (bl->previous) /* was it a nested block? */ + movegotosout(fs, bl); /* update pending gotos to enclosing block */ else { - if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ + if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } } diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 2f2214bde..bc7a0ba82 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -43,9 +43,10 @@ int luaS_eqlngstr (TString *a, TString *b) { } int luaS_eqshrstr (TString *a, TString *b) { + int r; lu_byte len = a->shrlen; lua_assert(b->tt == LUA_VSHRSTR); - int r = len == b->shrlen && (memcmp(getstr(a), getstr(b), len) == 0); + r = len == b->shrlen && (memcmp(getstr(a), getstr(b), len) == 0); if (r) { if (a->id < b->id) { a->id = b->id; @@ -161,10 +162,11 @@ static unsigned int luai_makeseed(lua_State *L) { void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; + stringtable *tb; if (STRSEED == 0) { STRSEED = luai_makeseed(L); } - stringtable *tb = &G(L)->strt; + tb = &G(L)->strt; tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ tb->size = MINSTRTABSIZE; diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 0f1900444..7f7dc2b22 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -177,10 +177,11 @@ static void print_version (void) { ** 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. +** (If there is no interpreter's name either, 'script' is -1, so +** table sizes are zero.) */ 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++) { @@ -268,14 +269,23 @@ 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 -** (either the script name or a bad argument in case of error). +** needed before running any Lua code or an error code if it finds any +** invalid argument. In case of error, 'first' is the index of the bad +** argument. Otherwise, 'first' is -1 if there is no program name, +** 0 if there is no script name, or the index of the script name. */ static int collectargs (char **argv, int *first) { int args = 0; int i; - for (i = 1; argv[i] != NULL; i++) { + if (argv[0] != NULL) { /* is there a program name? */ + if (argv[0][0]) /* not empty? */ + progname = argv[0]; /* save it */ + } + else { /* no program name */ + *first = -1; + return 0; + } + for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ *first = i; if (argv[i][0] != '-') /* not an option? */ return args; /* stop handling options */ @@ -316,7 +326,7 @@ static int collectargs (char **argv, int *first) { return has_error; } } - *first = i; /* no script name */ + *first = 0; /* no script name */ return args; } @@ -609,8 +619,8 @@ static int pmain (lua_State *L) { char **argv = (char **)lua_touserdata(L, 2); int script; int args = collectargs(argv, &script); + int optlim = (script > 0) ? script : argc; /* first argv not an option */ luaL_checkversion(L); /* check that interpreter has correct version */ - if (argv[0] && argv[0][0]) progname = argv[0]; if (args == has_error) { /* bad arg? */ print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; @@ -628,14 +638,15 @@ static int pmain (lua_State *L) { 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 */ + if (!runargs(L, argv, optlim)) /* 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 (script > 0) { /* execute main script (if there is one) */ + if (handle_script(L, argv + script) != LUA_OK) + return 0; /* interrupt in case of error */ + } if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); doREPL(L); /* do read-eval-print loop */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 916fd6e7b..219e23283 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,10 +18,10 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "4" +#define LUA_VERSION_RELEASE "5" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5) #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 d42d14b7d..fcc0018b3 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -728,7 +728,7 @@ ** CHANGE it if you need a different limit. This limit is arbitrary; ** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). -** (It must fit into max(size_t)/32.) +** (It must fit into max(size_t)/32 and max(int)/2.) */ #if LUAI_IS32INT #define LUAI_MAXSTACK 1000000 diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 4d6d3db96..b92f2682b 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -645,7 +645,7 @@ void luaV_concat (lua_State *L, int total) { int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) - luaT_tryconcatTM(L); + luaT_tryconcatTM(L); /* may invalidate 'top' */ else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ @@ -658,8 +658,10 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); - if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) + if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { + L->top = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); + } tl += l; } if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ @@ -673,8 +675,8 @@ void luaV_concat (lua_State *L, int total) { } 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 */ + total -= n - 1; /* got 'n' strings to create one new */ + L->top -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } @@ -900,6 +902,7 @@ void luaV_finishOp (lua_State *L) { ** operation, 'fop' is the float operation. */ #define op_arithI(L,iop,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ @@ -928,6 +931,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over floats and others with register operands. */ #define op_arithf(L,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ op_arithf_aux(L, v1, v2, fop); } @@ -937,6 +941,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations with K operands for floats. */ #define op_arithfK(L,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } @@ -946,6 +951,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over integers and floats. */ #define op_arith_aux(L,v1,v2,iop,fop) { \ + StkId ra = RA(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ @@ -975,6 +981,7 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with constant operand. */ #define op_bitwiseK(L,op) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ @@ -988,6 +995,7 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with register operands. */ #define op_bitwise(L,op) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ @@ -1002,18 +1010,19 @@ void luaV_finishOp (lua_State *L) { ** integers. */ #define op_order(L,opi,opn,other) { \ - int cond; \ - TValue *rb = vRB(i); \ - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ - lua_Integer ia = ivalue(s2v(ra)); \ - lua_Integer ib = ivalue(rb); \ - cond = opi(ia, ib); \ - } \ - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ - cond = opn(s2v(ra), rb); \ - else \ - Protect(cond = other(L, s2v(ra), rb)); \ - docondjump(); } + StkId ra = RA(i); \ + int cond; \ + TValue *rb = vRB(i); \ + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(s2v(ra)); \ + lua_Integer ib = ivalue(rb); \ + cond = opi(ia, ib); \ + } \ + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ + cond = opn(s2v(ra), rb); \ + else \ + Protect(cond = other(L, s2v(ra), rb)); \ + docondjump(); } /* @@ -1021,20 +1030,21 @@ void luaV_finishOp (lua_State *L) { ** always small enough to have an exact representation as a float.) */ #define op_orderI(L,opi,opf,inv,tm) { \ - int cond; \ - int im = GETARG_sB(i); \ - if (ttisinteger(s2v(ra))) \ - cond = opi(ivalue(s2v(ra)), im); \ - else if (ttisfloat(s2v(ra))) { \ - lua_Number fa = fltvalue(s2v(ra)); \ - lua_Number fim = cast_num(im); \ - cond = opf(fa, fim); \ - } \ - else { \ - int isf = GETARG_C(i); \ - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ - } \ - docondjump(); } + StkId ra = RA(i); \ + int cond; \ + int im = GETARG_sB(i); \ + if (ttisinteger(s2v(ra))) \ + cond = opi(ivalue(s2v(ra)), im); \ + else if (ttisfloat(s2v(ra))) { \ + lua_Number fa = fltvalue(s2v(ra)); \ + lua_Number fim = cast_num(im); \ + cond = opf(fa, fim); \ + } \ + else { \ + int isf = GETARG_C(i); \ + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + } \ + docondjump(); } /* }================================================================== */ @@ -1130,7 +1140,6 @@ void luaV_finishOp (lua_State *L) { updatebase(ci); /* correct stack */ \ } \ i = *(pc++); \ - ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ } #define vmdispatch(o) switch(o) @@ -1166,56 +1175,64 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* main loop of interpreter */ for (;;) { Instruction i; /* instruction being executed */ - StkId ra; /* instruction's A register */ vmfetch(); #if 0 /* low-level line tracing for debugging Lua */ printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); #endif lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack_last); + lua_assert(base <= L->top && L->top <= L->stack_last); /* invalidate top for instructions not expecting it */ lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { + StkId ra = RA(i); setobjs2s(L, ra, RB(i)); vmbreak; } vmcase(OP_LOADI) { + StkId ra = RA(i); lua_Integer b = GETARG_sBx(i); setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADF) { + StkId ra = RA(i); int b = GETARG_sBx(i); setfltvalue(s2v(ra), cast_num(b)); vmbreak; } vmcase(OP_LOADK) { + StkId ra = RA(i); TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADKX) { + StkId ra = RA(i); TValue *rb; rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADFALSE) { + StkId ra = RA(i); setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LFALSESKIP) { + StkId ra = RA(i); setbfvalue(s2v(ra)); pc++; /* skip next instruction */ vmbreak; } vmcase(OP_LOADTRUE) { + StkId ra = RA(i); setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { + StkId ra = RA(i); int b = GETARG_B(i); do { setnilvalue(s2v(ra++)); @@ -1223,17 +1240,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETUPVAL) { + StkId ra = RA(i); int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); vmbreak; } vmcase(OP_SETUPVAL) { + StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { + StkId ra = RA(i); const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v; TValue *rc = KC(i); @@ -1246,6 +1266,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETTABLE) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = vRC(i); @@ -1260,6 +1281,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETI) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); int c = GETARG_C(i); @@ -1274,6 +1296,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETFIELD) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = KC(i); @@ -1299,6 +1322,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETTABLE) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ @@ -1313,6 +1337,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETI) { + StkId ra = RA(i); const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); @@ -1327,6 +1352,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETFIELD) { + StkId ra = RA(i); const TValue *slot; TValue *rb = KB(i); TValue *rc = RKC(i); @@ -1339,6 +1365,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NEWTABLE) { + StkId ra = RA(i); int b = GETARG_B(i); /* log2(hash size) + 1 */ int c = GETARG_C(i); /* array size */ Table *t; @@ -1357,6 +1384,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SELF) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = RKC(i); @@ -1414,6 +1442,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SHRI) { + StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; @@ -1423,6 +1452,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SHLI) { + StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; @@ -1480,6 +1510,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBIN) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *rb = vRB(i); TMS tm = (TMS)GETARG_C(i); @@ -1489,6 +1520,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBINI) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ int imm = GETARG_sB(i); TMS tm = (TMS)GETARG_C(i); @@ -1498,6 +1530,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBINK) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *imm = KB(i); TMS tm = (TMS)GETARG_C(i); @@ -1507,6 +1540,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_UNM) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { @@ -1521,6 +1555,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_BNOT) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { @@ -1531,6 +1566,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NOT) { + StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb)) setbtvalue(s2v(ra)); @@ -1539,10 +1575,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LEN) { + StkId ra = RA(i); Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { + StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ L->top = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); @@ -1550,10 +1588,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { + StkId ra = RA(i); Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { + StkId ra = RA(i); /* create new to-be-closed upvalue */ halfProtect(luaF_newtbcupval(L, ra)); vmbreak; @@ -1563,6 +1603,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQ) { + StkId ra = RA(i); int cond; TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, s2v(ra), rb)); @@ -1578,6 +1619,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQK) { + StkId ra = RA(i); TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ int cond = luaV_rawequalobj(s2v(ra), rb); @@ -1585,6 +1627,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQI) { + StkId ra = RA(i); int cond; int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) @@ -1613,11 +1656,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TEST) { + StkId ra = RA(i); int cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } vmcase(OP_TESTSET) { + StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb) == GETARG_k(i)) pc++; @@ -1628,6 +1673,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CALL) { + StkId ra = RA(i); CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; @@ -1644,6 +1690,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TAILCALL) { + StkId ra = RA(i); int b = GETARG_B(i); /* number of arguments + 1 (function) */ int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); @@ -1669,6 +1716,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } } vmcase(OP_RETURN) { + StkId ra = RA(i); int n = GETARG_B(i) - 1; /* number of results */ int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ @@ -1691,6 +1739,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN0) { if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); L->top = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ @@ -1707,6 +1756,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN1) { if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); L->top = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ @@ -1718,6 +1768,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (nres == 0) L->top = base - 1; /* asked for no results */ else { + StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ L->top = base; for (; l_unlikely(nres > 1); nres--) @@ -1733,6 +1784,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } } vmcase(OP_FORLOOP) { + StkId ra = RA(i); if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); if (count > 0) { /* still more iterations? */ @@ -1751,12 +1803,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_FORPREP) { + StkId ra = RA(i); savestate(L, ci); /* in case of errors */ if (forprep(L, ra)) pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } vmcase(OP_TFORPREP) { + StkId ra = RA(i); /* create to-be-closed upvalue (if needed) */ halfProtect(luaF_newtbcupval(L, ra + 3)); pc += GETARG_Bx(i); @@ -1765,7 +1819,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { goto l_tforcall; } vmcase(OP_TFORCALL) { - l_tforcall: + l_tforcall: { + StkId ra = RA(i); /* 'ra' has the iterator function, 'ra + 1' has the state, 'ra + 2' has the control variable, and 'ra + 3' has the to-be-closed variable. The call will use the stack after @@ -1779,16 +1834,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; - } + }} vmcase(OP_TFORLOOP) { - l_tforloop: + l_tforloop: { + StkId ra = RA(i); if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ setobjs2s(L, ra + 2, ra + 4); /* save control variable */ pc -= GETARG_Bx(i); /* jump back */ } vmbreak; - } + }} vmcase(OP_SETLIST) { + StkId ra = RA(i); int n = GETARG_B(i); unsigned int last = GETARG_C(i); Table *h = hvalue(s2v(ra)); @@ -1812,12 +1869,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSURE) { + StkId ra = RA(i); Proto *p = cl->p->p[GETARG_Bx(i)]; halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { + StkId ra = RA(i); int n = GETARG_C(i) - 1; /* required results */ Protect(luaT_getvarargs(L, ci, ra, n)); vmbreak; diff --git a/3rd/lua/Makefile b/3rd/lua/makefile similarity index 52% rename from 3rd/lua/Makefile rename to 3rd/lua/makefile index fd5caf14f..a56359b90 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/makefile @@ -1,159 +1,140 @@ -# Makefile for building Lua -# See ../doc/readme.html for installation and customization instructions. +# Developer's makefile for building Lua +# see luaconf.h for further customization # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= -# Your platform. See PLATS for possible values. -PLAT= guess - -CC= gcc -std=gnu99 -CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) -LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) -LIBS= -lm $(SYSLIBS) $(MYLIBS) - -AR= ar rcu +# Warnings valid for both C and C++ +CWARNSCPP= \ + -Wfatal-errors \ + -Wextra \ + -Wshadow \ + -Wsign-compare \ + -Wundef \ + -Wwrite-strings \ + -Wredundant-decls \ + -Wdisabled-optimization \ + -Wdouble-promotion \ + -Wmissing-declarations \ + # the next warnings might be useful sometimes, + # but usually they generate too much noise + # -Werror \ + # -pedantic # warns if we use jump tables \ + # -Wconversion \ + # -Wsign-conversion \ + # -Wstrict-overflow=2 \ + # -Wformat=2 \ + # -Wcast-qual \ + + +# Warnings for gcc, not valid for clang +CWARNGCC= \ + -Wlogical-op \ + -Wno-aggressive-loop-optimizations \ + + +# The next warnings are neither valid nor needed for C++ +CWARNSC= -Wdeclaration-after-statement \ + -Wmissing-prototypes \ + -Wnested-externs \ + -Wstrict-prototypes \ + -Wc++-compat \ + -Wold-style-definition \ + + +CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) + +# Some useful compiler options for internal tests: +# -DLUAI_ASSERT turns on all assertions inside Lua. +# -DHARDSTACKTESTS forces a reallocation of the stack at every point where +# the stack can be reallocated. +# -DHARDMEMTESTS forces a full collection at all points where the collector +# can run. +# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation. +# -DEXTERNMEMCHECK removes internal consistency checking of blocks being +# deallocated (useful when an external tool like valgrind does the check). +# -DMAXINDEXRK=k limits range of constants in RK instruction operands. +# -DLUA_COMPAT_5_3 + +# -pg -malign-double +# -DLUA_USE_CTYPE -DLUA_USE_APICHECK +# ('-ftrapv' for runtime checks of integer overflows) +# -fsanitize=undefined -ftrapv -fno-inline +# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g + + +LOCAL = $(TESTS) $(CWARNS) + + +# enable Linux goodies +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -I../../skynet-src +MYLDFLAGS= $(LOCAL) -Wl,-E +MYLIBS= -ldl + + +CC= gcc +CFLAGS= -Wall -O2 $(MYCFLAGS) -fno-stack-protector -fno-common -march=native +AR= ar rc RANLIB= ranlib RM= rm -f -UNAME= uname - -SYSCFLAGS= -SYSLDFLAGS= -SYSLIBS= -MYCFLAGS=-I../../skynet-src -g -MYLDFLAGS= -MYLIBS= -MYOBJS= -# Special flags for compiler modules; -Os reduces code size. -CMCFLAGS= +# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= -# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris +LIBS = -lm -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 lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o -BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) +CORE_T= 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 ltests.o +AUX_O= lauxlib.o +LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ + lutf8lib.o loadlib.o lcorolib.o linit.o LUA_T= lua LUA_O= lua.o -LUAC_T= luac -LUAC_O= luac.o -ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) -ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) -ALL_A= $(LUA_A) - -# Targets start here. -default: $(PLAT) +ALL_T= $(CORE_T) $(LUA_T) +ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O) +ALL_A= $(CORE_T) all: $(ALL_T) + touch all o: $(ALL_O) a: $(ALL_A) -$(LUA_A): $(BASE_O) - $(AR) $@ $(BASE_O) +$(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O) + $(AR) $@ $? $(RANLIB) $@ -$(LUA_T): $(LUA_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) - -$(LUAC_T): $(LUAC_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) +$(LUA_T): $(LUA_O) $(CORE_T) + $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL) -test: - ./$(LUA_T) -v clean: $(RM) $(ALL_T) $(ALL_O) depend: - @$(CC) $(CFLAGS) -MM l*.c + @$(CC) $(CFLAGS) -MM *.c echo: - @echo "PLAT= $(PLAT)" - @echo "CC= $(CC)" - @echo "CFLAGS= $(CFLAGS)" - @echo "LDFLAGS= $(LDFLAGS)" - @echo "LIBS= $(LIBS)" - @echo "AR= $(AR)" - @echo "RANLIB= $(RANLIB)" - @echo "RM= $(RM)" - @echo "UNAME= $(UNAME)" - -# Convenience targets for popular platforms. -ALL= all - -help: - @echo "Do 'make PLATFORM' where PLATFORM is one of these:" - @echo " $(PLATS)" - @echo "See doc/readme.html for complete instructions." - -guess: - @echo Guessing `$(UNAME)` - @$(MAKE) `$(UNAME)` - -AIX aix: - $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" - -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 '*** Make sure to compile all external Lua libraries' - @echo '*** with LUA_USE_C89 to ensure consistency' - @echo '' - -FreeBSD NetBSD OpenBSD freebsd: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" - -generic: $(ALL) - -Linux linux: linux-noreadline - -linux-noreadline: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" - -linux-readline: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" - -Darwin macos macosx: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" - -mingw: - $(MAKE) "LUA_A=lua54.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 - -posix: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" - -SunOS solaris: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" - -# Targets that do not create files (not all makes understand .PHONY). -.PHONY: all $(PLATS) help test clean default o a depend echo - -# Compiler modules may use special flags. -llex.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c - -lparser.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c + @echo "CC = $(CC)" + @echo "CFLAGS = $(CFLAGS)" + @echo "AR = $(AR)" + @echo "RANLIB = $(RANLIB)" + @echo "RM = $(RM)" + @echo "MYCFLAGS = $(MYCFLAGS)" + @echo "MYLDFLAGS = $(MYLDFLAGS)" + @echo "MYLIBS = $(MYLIBS)" + @echo "DL = $(DL)" -lcode.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c +$(ALL_O): makefile ltests.h -# DO NOT DELETE +# DO NOT EDIT +# automatically made with 'gcc -MM l*.c' 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 \ @@ -204,11 +185,13 @@ 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 +ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \ + lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \ + ltable.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 lgc.h lstring.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 ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.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 diff --git a/Makefile b/Makefile index 337ed3e6a..95a299d6b 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua $(LUA_STATICLIB) : - cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) + cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' # https : turn on TLS_MODULE to add https support From 6bd6f53ab8b20fa4e9760e52ab55a4391e07c18f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Aug 2022 15:14:01 +0800 Subject: [PATCH 451/565] update lua --- 3rd/lua/README.md | 7 + 3rd/lua/ltests.c | 1977 +++++++++++++++++++++++++++++++++++++++++++++ 3rd/lua/ltests.h | 151 ++++ 3rd/lua/onelua.c | 107 +++ 4 files changed, 2242 insertions(+) create mode 100644 3rd/lua/README.md create mode 100644 3rd/lua/ltests.c create mode 100644 3rd/lua/ltests.h create mode 100644 3rd/lua/onelua.c diff --git a/3rd/lua/README.md b/3rd/lua/README.md new file mode 100644 index 000000000..5bc0ee77c --- /dev/null +++ b/3rd/lua/README.md @@ -0,0 +1,7 @@ +# Lua + +This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/). + +Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). + +Download official Lua releases from [Lua.org](https://www.lua.org/download.html). diff --git a/3rd/lua/ltests.c b/3rd/lua/ltests.c new file mode 100644 index 000000000..97834e380 --- /dev/null +++ b/3rd/lua/ltests.c @@ -0,0 +1,1977 @@ +/* +** $Id: ltests.c $ +** Internal Module for Debugging of the Lua Implementation +** See Copyright Notice in lua.h +*/ + +#define ltests_c +#define LUA_CORE + +#include "lprefix.h" + + +#include +#include +#include +#include +#include + +#include "lua.h" + +#include "lapi.h" +#include "lauxlib.h" +#include "lcode.h" +#include "lctype.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lmem.h" +#include "lopcodes.h" +#include "lopnames.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "lualib.h" + + + +/* +** The whole module only makes sense with LUA_DEBUG on +*/ +#if defined(LUA_DEBUG) + + +void *l_Trick = 0; + + +#define obj_at(L,k) s2v(L->ci->func + (k)) + + +static int runC (lua_State *L, lua_State *L1, const char *pc); + + +static void setnameval (lua_State *L, const char *name, int val) { + lua_pushinteger(L, val); + lua_setfield(L, -2, name); +} + + +static void pushobject (lua_State *L, const TValue *o) { + setobj2s(L, L->top, o); + api_incr_top(L); +} + + +static void badexit (const char *fmt, const char *s1, const char *s2) { + fprintf(stderr, fmt, s1); + if (s2) + fprintf(stderr, "extra info: %s\n", s2); + /* avoid assertion failures when exiting */ + l_memcontrol.numblocks = l_memcontrol.total = 0; + exit(EXIT_FAILURE); +} + + +static int tpanic (lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "error object is not a string"; + return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", + msg, NULL), + 0); /* do not return to Lua */ +} + + +/* +** Warning function for tests. First, it concatenates all parts of +** a warning in buffer 'buff'. Then, it has three modes: +** - 0.normal: messages starting with '#' are shown on standard output; +** - other messages abort the tests (they represent real warning +** conditions; the standard tests should not generate these conditions +** unexpectedly); +** - 1.allow: all messages are shown; +** - 2.store: all warnings go to the global '_WARN'; +*/ +static void warnf (void *ud, const char *msg, int tocont) { + lua_State *L = cast(lua_State *, ud); + static char buff[200] = ""; /* should be enough for tests... */ + static int onoff = 0; + static int mode = 0; /* start in normal mode */ + static int lasttocont = 0; + if (!lasttocont && !tocont && *msg == '@') { /* control message? */ + if (buff[0] != '\0') + badexit("Control warning during warning: %s\naborting...\n", msg, buff); + if (strcmp(msg, "@off") == 0) + onoff = 0; + else if (strcmp(msg, "@on") == 0) + onoff = 1; + else if (strcmp(msg, "@normal") == 0) + mode = 0; + else if (strcmp(msg, "@allow") == 0) + mode = 1; + else if (strcmp(msg, "@store") == 0) + mode = 2; + else + badexit("Invalid control warning in test mode: %s\naborting...\n", + msg, NULL); + return; + } + lasttocont = tocont; + if (strlen(msg) >= sizeof(buff) - strlen(buff)) + badexit("warnf-buffer overflow (%s)\n", msg, buff); + strcat(buff, msg); /* add new message to current warning */ + if (!tocont) { /* message finished? */ + lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); + lua_getglobal(L, "_WARN"); + if (!lua_toboolean(L, -1)) + lua_pop(L, 1); /* ok, no previous unexpected warning */ + else { + badexit("Unhandled warning in store mode: %s\naborting...\n", + lua_tostring(L, -1), buff); + } + lua_lock(L); + switch (mode) { + case 0: { /* normal */ + if (buff[0] != '#' && onoff) /* unexpected warning? */ + badexit("Unexpected warning in test mode: %s\naborting...\n", + buff, NULL); + } /* FALLTHROUGH */ + case 1: { /* allow */ + if (onoff) + fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */ + break; + } + case 2: { /* store */ + lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); + lua_pushstring(L, buff); + lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ + lua_lock(L); + break; + } + } + buff[0] = '\0'; /* prepare buffer for next warning */ + } +} + + +/* +** {====================================================================== +** Controlled version for realloc. +** ======================================================================= +*/ + +#define MARK 0x55 /* 01010101 (a nice pattern) */ + +typedef union Header { + LUAI_MAXALIGN; + struct { + size_t size; + int type; + } d; +} Header; + + +#if !defined(EXTERNMEMCHECK) + +/* full memory check */ +#define MARKSIZE 16 /* size of marks after each block */ +#define fillmem(mem,size) memset(mem, -MARK, size) + +#else + +/* external memory check: don't do it twice */ +#define MARKSIZE 0 +#define fillmem(mem,size) /* empty */ + +#endif + + +Memcontrol l_memcontrol = + {0, 0UL, 0UL, 0UL, 0UL, (~0UL), + {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; + + +static void freeblock (Memcontrol *mc, Header *block) { + if (block) { + size_t size = block->d.size; + int i; + for (i = 0; i < MARKSIZE; i++) /* check marks after block */ + lua_assert(*(cast_charp(block + 1) + size + i) == MARK); + mc->objcount[block->d.type]--; + fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ + free(block); /* actually free block */ + mc->numblocks--; /* update counts */ + mc->total -= size; + } +} + + +void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { + Memcontrol *mc = cast(Memcontrol *, ud); + Header *block = cast(Header *, b); + int type; + if (mc->memlimit == 0) { /* first time? */ + char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ + mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; + } + if (block == NULL) { + type = (oldsize < LUA_NUMTAGS) ? oldsize : 0; + oldsize = 0; + } + else { + block--; /* go to real header */ + type = block->d.type; + lua_assert(oldsize == block->d.size); + } + if (size == 0) { + freeblock(mc, block); + return NULL; + } + if (mc->failnext) { + mc->failnext = 0; + return NULL; /* fake a single memory allocation error */ + } + if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */ + if (mc->countlimit == 0) + return NULL; /* fake a memory allocation error */ + mc->countlimit--; + } + if (size > oldsize && mc->total+size-oldsize > mc->memlimit) + return NULL; /* fake a memory allocation error */ + else { + Header *newblock; + int i; + size_t commonsize = (oldsize < size) ? oldsize : size; + size_t realsize = sizeof(Header) + size + MARKSIZE; + if (realsize < size) return NULL; /* arithmetic overflow! */ + newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ + if (newblock == NULL) + return NULL; /* really out of memory? */ + if (block) { + memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */ + freeblock(mc, block); /* erase (and check) old copy */ + } + /* initialize new part of the block with something weird */ + fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize); + /* initialize marks after block */ + for (i = 0; i < MARKSIZE; i++) + *(cast_charp(newblock + 1) + size + i) = MARK; + newblock->d.size = size; + newblock->d.type = type; + mc->total += size; + if (mc->total > mc->maxmem) + mc->maxmem = mc->total; + mc->numblocks++; + mc->objcount[type]++; + return newblock + 1; + } +} + + +/* }====================================================================== */ + + + +/* +** {===================================================================== +** Functions to check memory consistency. +** Most of these checks are done through asserts, so this code does +** not make sense with asserts off. For this reason, it uses 'assert' +** directly, instead of 'lua_assert'. +** ====================================================================== +*/ + +#include + +/* +** Check GC invariants. For incremental mode, a black object cannot +** point to a white one. For generational mode, really old objects +** cannot point to young objects. Both old1 and touched2 objects +** cannot point to new objects (but can point to survivals). +** (Threads and open upvalues, despite being marked "really old", +** continue to be visited in all collections, and therefore can point to +** new objects. They, and only they, are old but gray.) +*/ +static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { + if (isdead(g,t)) return 0; + if (issweepphase(g)) + return 1; /* no invariants */ + else if (g->gckind == KGC_INC) + return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ + else { /* generational mode */ + if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) + return 0; + if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) && + getage(t) == G_NEW) + return 0; + return 1; + } +} + + +static void printobj (global_State *g, GCObject *o) { + printf("||%s(%p)-%c%c(%02X)||", + ttypename(novariant(o->tt)), (void *)o, + isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', + "ns01oTt"[getage(o)], o->marked); + if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR) + printf(" '%s'", getstr(gco2ts(o))); +} + + +void lua_printobj (lua_State *L, struct GCObject *o) { + printobj(G(L), o); +} + +static int testobjref (global_State *g, GCObject *f, GCObject *t) { + int r1 = testobjref1(g, f, t); + if (!r1) { + printf("%d(%02X) - ", g->gcstate, g->currentwhite); + printobj(g, f); + printf(" -> "); + printobj(g, t); + printf("\n"); + } + return r1; +} + + +static void checkobjref (global_State *g, GCObject *f, GCObject *t) { + assert(testobjref(g, f, t)); +} + + +/* +** Version where 't' can be NULL. In that case, it should not apply the +** macro 'obj2gco' over the object. ('t' may have several types, so this +** definition must be a macro.) Most checks need this version, because +** the check may run while an object is still being created. +*/ +#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); } + + +static void checkvalref (global_State *g, GCObject *f, const TValue *t) { + assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t)))); +} + + +static void checktable (global_State *g, Table *h) { + unsigned int i; + unsigned int asize = luaH_realasize(h); + Node *n, *limit = gnode(h, sizenode(h)); + GCObject *hgc = obj2gco(h); + checkobjrefN(g, hgc, h->metatable); + for (i = 0; i < asize; i++) + checkvalref(g, hgc, &h->array[i]); + for (n = gnode(h, 0); n < limit; n++) { + if (!isempty(gval(n))) { + TValue k; + getnodekey(g->mainthread, &k, n); + assert(!keyisnil(n)); + checkvalref(g, hgc, &k); + checkvalref(g, hgc, gval(n)); + } + } +} + + +static void checkudata (global_State *g, Udata *u) { + int i; + GCObject *hgc = obj2gco(u); + checkobjrefN(g, hgc, u->metatable); + for (i = 0; i < u->nuvalue; i++) + checkvalref(g, hgc, &u->uv[i].uv); +} + + +static void checkproto (global_State *g, Proto *f) { + int i; + GCObject *fgc = obj2gco(f); + checkobjrefN(g, fgc, f->source); + for (i=0; isizek; i++) { + if (iscollectable(f->k + i)) + checkobjref(g, fgc, gcvalue(f->k + i)); + } + for (i=0; isizeupvalues; i++) + checkobjrefN(g, fgc, f->upvalues[i].name); + for (i=0; isizep; i++) + checkobjrefN(g, fgc, f->p[i]); + for (i=0; isizelocvars; i++) + checkobjrefN(g, fgc, f->locvars[i].varname); +} + + +static void checkCclosure (global_State *g, CClosure *cl) { + GCObject *clgc = obj2gco(cl); + int i; + for (i = 0; i < cl->nupvalues; i++) + checkvalref(g, clgc, &cl->upvalue[i]); +} + + +static void checkLclosure (global_State *g, LClosure *cl) { + GCObject *clgc = obj2gco(cl); + int i; + checkobjrefN(g, clgc, cl->p); + for (i=0; inupvalues; i++) { + UpVal *uv = cl->upvals[i]; + if (uv) { + checkobjrefN(g, clgc, uv); + if (!upisopen(uv)) + checkvalref(g, obj2gco(uv), uv->v); + } + } +} + + +static int lua_checkpc (CallInfo *ci) { + if (!isLua(ci)) return 1; + else { + StkId f = ci->func; + Proto *p = clLvalue(s2v(f))->p; + return p->code <= ci->u.l.savedpc && + ci->u.l.savedpc <= p->code + p->sizecode; + } +} + + +static void checkstack (global_State *g, lua_State *L1) { + StkId o; + CallInfo *ci; + UpVal *uv; + assert(!isdead(g, L1)); + if (L1->stack == NULL) { /* incomplete thread? */ + assert(L1->openupval == NULL && L1->ci == NULL); + return; + } + for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) + assert(upisopen(uv)); /* must be open */ + assert(L1->top <= L1->stack_last); + assert(L1->tbclist <= L1->top); + for (ci = L1->ci; ci != NULL; ci = ci->previous) { + assert(ci->top <= L1->stack_last); + assert(lua_checkpc(ci)); + } + for (o = L1->stack; o < L1->stack_last; o++) + checkliveness(L1, s2v(o)); /* entire stack must have valid values */ +} + + +static void checkrefs (global_State *g, GCObject *o) { + switch (o->tt) { + case LUA_VUSERDATA: { + checkudata(g, gco2u(o)); + break; + } + case LUA_VUPVAL: { + checkvalref(g, o, gco2upv(o)->v); + break; + } + case LUA_VTABLE: { + checktable(g, gco2t(o)); + break; + } + case LUA_VTHREAD: { + checkstack(g, gco2th(o)); + break; + } + case LUA_VLCL: { + checkLclosure(g, gco2lcl(o)); + break; + } + case LUA_VCCL: { + checkCclosure(g, gco2ccl(o)); + break; + } + case LUA_VPROTO: { + checkproto(g, gco2p(o)); + break; + } + case LUA_VSHRSTR: + case LUA_VLNGSTR: { + assert(!isgray(o)); /* strings are never gray */ + break; + } + default: assert(0); + } +} + + +/* +** Check consistency of an object: +** - Dead objects can only happen in the 'allgc' list during a sweep +** phase (controlled by the caller through 'maybedead'). +** - During pause, all objects must be white. +** - In generational mode: +** * objects must be old enough for their lists ('listage'). +** * old objects cannot be white. +** * old objects must be black, except for 'touched1', 'old0', +** threads, and open upvalues. +*/ +static void checkobject (global_State *g, GCObject *o, int maybedead, + int listage) { + if (isdead(g, o)) + assert(maybedead); + else { + assert(g->gcstate != GCSpause || iswhite(o)); + if (g->gckind == KGC_GEN) { /* generational mode? */ + assert(getage(o) >= listage); + assert(!iswhite(o) || !isold(o)); + if (isold(o)) { + assert(isblack(o) || + getage(o) == G_TOUCHED1 || + getage(o) == G_OLD0 || + o->tt == LUA_VTHREAD || + (o->tt == LUA_VUPVAL && upisopen(gco2upv(o)))); + } + } + checkrefs(g, o); + } +} + + +static lu_mem checkgraylist (global_State *g, GCObject *o) { + int total = 0; /* count number of elements in the list */ + ((void)g); /* better to keep it available if we need to print an object */ + while (o) { + assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); + assert(!testbit(o->marked, TESTBIT)); + if (keepinvariant(g)) + l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ + total++; + switch (o->tt) { + case LUA_VTABLE: o = gco2t(o)->gclist; break; + case LUA_VLCL: o = gco2lcl(o)->gclist; break; + case LUA_VCCL: o = gco2ccl(o)->gclist; break; + case LUA_VTHREAD: o = gco2th(o)->gclist; break; + case LUA_VPROTO: o = gco2p(o)->gclist; break; + case LUA_VUSERDATA: + assert(gco2u(o)->nuvalue > 0); + o = gco2u(o)->gclist; + break; + default: assert(0); /* other objects cannot be in a gray list */ + } + } + return total; +} + + +/* +** Check objects in gray lists. +*/ +static lu_mem checkgrays (global_State *g) { + int total = 0; /* count number of elements in all lists */ + if (!keepinvariant(g)) return total; + total += checkgraylist(g, g->gray); + total += checkgraylist(g, g->grayagain); + total += checkgraylist(g, g->weak); + total += checkgraylist(g, g->allweak); + total += checkgraylist(g, g->ephemeron); + return total; +} + + +/* +** Check whether 'o' should be in a gray list. If so, increment +** 'count' and check its TESTBIT. (It must have been previously set by +** 'checkgraylist'.) +*/ +static void incifingray (global_State *g, GCObject *o, lu_mem *count) { + if (!keepinvariant(g)) + return; /* gray lists not being kept in these phases */ + if (o->tt == LUA_VUPVAL) { + /* only open upvalues can be gray */ + assert(!isgray(o) || upisopen(gco2upv(o))); + return; /* upvalues are never in gray lists */ + } + /* these are the ones that must be in gray lists */ + if (isgray(o) || getage(o) == G_TOUCHED2) { + (*count)++; + assert(testbit(o->marked, TESTBIT)); + resetbit(o->marked, TESTBIT); /* prepare for next cycle */ + } +} + + +static lu_mem checklist (global_State *g, int maybedead, int tof, + GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { + GCObject *o; + lu_mem total = 0; /* number of object that should be in gray lists */ + for (o = newl; o != survival; o = o->next) { + checkobject(g, o, maybedead, G_NEW); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); + } + for (o = survival; o != old; o = o->next) { + checkobject(g, o, 0, G_SURVIVAL); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); + } + for (o = old; o != reallyold; o = o->next) { + checkobject(g, o, 0, G_OLD1); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); + } + for (o = reallyold; o != NULL; o = o->next) { + checkobject(g, o, 0, G_OLD); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); + } + return total; +} + + +int lua_checkmemory (lua_State *L) { + global_State *g = G(L); + GCObject *o; + int maybedead; + lu_mem totalin; /* total of objects that are in gray lists */ + lu_mem totalshould; /* total of objects that should be in gray lists */ + if (keepinvariant(g)) { + assert(!iswhite(g->mainthread)); + assert(!iswhite(gcvalue(&g->l_registry))); + } + assert(!isdead(g, gcvalue(&g->l_registry))); + assert(g->sweepgc == NULL || issweepphase(g)); + totalin = checkgrays(g); + + /* check 'fixedgc' list */ + for (o = g->fixedgc; o != NULL; o = o->next) { + assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); + } + + /* check 'allgc' list */ + maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); + totalshould = checklist(g, maybedead, 0, g->allgc, + g->survival, g->old1, g->reallyold); + + /* check 'finobj' list */ + totalshould += checklist(g, 0, 1, g->finobj, + g->finobjsur, g->finobjold1, g->finobjrold); + + /* check 'tobefnz' list */ + for (o = g->tobefnz; o != NULL; o = o->next) { + checkobject(g, o, 0, G_NEW); + incifingray(g, o, &totalshould); + assert(tofinalize(o)); + assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); + } + if (keepinvariant(g)) + assert(totalin == totalshould); + return 0; +} + +/* }====================================================== */ + + + +/* +** {====================================================== +** Disassembler +** ======================================================= +*/ + + +static char *buildop (Proto *p, int pc, char *buff) { + char *obuff = buff; + Instruction i = p->code[pc]; + OpCode o = GET_OPCODE(i); + const char *name = opnames[o]; + int line = luaG_getfuncline(p, pc); + int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0; + if (lineinfo == ABSLINEINFO) + buff += sprintf(buff, "(__"); + else + buff += sprintf(buff, "(%2d", lineinfo); + buff += sprintf(buff, " - %4d) %4d - ", line, pc); + switch (getOpMode(o)) { + case iABC: + sprintf(buff, "%-12s%4d %4d %4d%s", name, + GETARG_A(i), GETARG_B(i), GETARG_C(i), + GETARG_k(i) ? " (k)" : ""); + break; + case iABx: + sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); + break; + case iAsBx: + sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i)); + break; + case iAx: + sprintf(buff, "%-12s%4d", name, GETARG_Ax(i)); + break; + case isJ: + sprintf(buff, "%-12s%4d", name, GETARG_sJ(i)); + break; + } + return obuff; +} + + +#if 0 +void luaI_printcode (Proto *pt, int size) { + int pc; + for (pc=0; pcmaxstacksize); + setnameval(L, "numparams", p->numparams); + for (pc=0; pcsizecode; pc++) { + char buff[100]; + lua_pushinteger(L, pc+1); + lua_pushstring(L, buildop(p, pc, buff)); + lua_settable(L, -3); + } + return 1; +} + + +static int printcode (lua_State *L) { + int pc; + Proto *p; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + printf("maxstack: %d\n", p->maxstacksize); + printf("numparams: %d\n", p->numparams); + for (pc=0; pcsizecode; pc++) { + char buff[100]; + printf("%s\n", buildop(p, pc, buff)); + } + return 0; +} + + +static int listk (lua_State *L) { + Proto *p; + int i; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + lua_createtable(L, p->sizek, 0); + for (i=0; isizek; i++) { + pushobject(L, p->k+i); + lua_rawseti(L, -2, i+1); + } + return 1; +} + + +static int listabslineinfo (lua_State *L) { + Proto *p; + int i; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info"); + lua_createtable(L, 2 * p->sizeabslineinfo, 0); + for (i=0; i < p->sizeabslineinfo; i++) { + lua_pushinteger(L, p->abslineinfo[i].pc); + lua_rawseti(L, -2, 2 * i + 1); + lua_pushinteger(L, p->abslineinfo[i].line); + lua_rawseti(L, -2, 2 * i + 2); + } + return 1; +} + + +static int listlocals (lua_State *L) { + Proto *p; + int pc = cast_int(luaL_checkinteger(L, 2)) - 1; + int i = 0; + const char *name; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + while ((name = luaF_getlocalname(p, ++i, pc)) != NULL) + lua_pushstring(L, name); + return i-1; +} + +/* }====================================================== */ + + + +static void printstack (lua_State *L) { + int i; + int n = lua_gettop(L); + printf("stack: >>\n"); + for (i = 1; i <= n; i++) { + printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); + lua_pop(L, 1); + } + printf("<<\n"); +} + + +static int get_limits (lua_State *L) { + lua_createtable(L, 0, 6); + setnameval(L, "IS32INT", LUAI_IS32INT); + setnameval(L, "MAXARG_Ax", MAXARG_Ax); + setnameval(L, "MAXARG_Bx", MAXARG_Bx); + setnameval(L, "OFFSET_sBx", OFFSET_sBx); + setnameval(L, "LFPF", LFIELDS_PER_FLUSH); + setnameval(L, "NUM_OPCODES", NUM_OPCODES); + return 1; +} + + +static int mem_query (lua_State *L) { + if (lua_isnone(L, 1)) { + lua_pushinteger(L, l_memcontrol.total); + lua_pushinteger(L, l_memcontrol.numblocks); + lua_pushinteger(L, l_memcontrol.maxmem); + return 3; + } + else if (lua_isnumber(L, 1)) { + unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1)); + if (limit == 0) limit = ULONG_MAX; + l_memcontrol.memlimit = limit; + return 0; + } + else { + const char *t = luaL_checkstring(L, 1); + int i; + for (i = LUA_NUMTAGS - 1; i >= 0; i--) { + if (strcmp(t, ttypename(i)) == 0) { + lua_pushinteger(L, l_memcontrol.objcount[i]); + return 1; + } + } + return luaL_error(L, "unknown type '%s'", t); + } +} + + +static int alloc_count (lua_State *L) { + if (lua_isnone(L, 1)) + l_memcontrol.countlimit = ~0L; + else + l_memcontrol.countlimit = luaL_checkinteger(L, 1); + return 0; +} + + +static int alloc_failnext (lua_State *L) { + UNUSED(L); + l_memcontrol.failnext = 1; + return 0; +} + + +static int settrick (lua_State *L) { + if (ttisnil(obj_at(L, 1))) + l_Trick = NULL; + else + l_Trick = gcvalue(obj_at(L, 1)); + return 0; +} + + +static int gc_color (lua_State *L) { + TValue *o; + luaL_checkany(L, 1); + o = obj_at(L, 1); + if (!iscollectable(o)) + lua_pushstring(L, "no collectable"); + else { + GCObject *obj = gcvalue(o); + lua_pushstring(L, isdead(G(L), obj) ? "dead" : + iswhite(obj) ? "white" : + isblack(obj) ? "black" : "gray"); + } + return 1; +} + + +static int gc_age (lua_State *L) { + TValue *o; + luaL_checkany(L, 1); + o = obj_at(L, 1); + if (!iscollectable(o)) + lua_pushstring(L, "no collectable"); + else { + static const char *gennames[] = {"new", "survival", "old0", "old1", + "old", "touched1", "touched2"}; + GCObject *obj = gcvalue(o); + lua_pushstring(L, gennames[getage(obj)]); + } + return 1; +} + + +static int gc_printobj (lua_State *L) { + TValue *o; + luaL_checkany(L, 1); + o = obj_at(L, 1); + if (!iscollectable(o)) + printf("no collectable\n"); + else { + GCObject *obj = gcvalue(o); + printobj(G(L), obj); + printf("\n"); + } + return 0; +} + + +static int gc_state (lua_State *L) { + static const char *statenames[] = { + "propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj", + "sweeptobefnz", "sweepend", "callfin", "pause", ""}; + static const int states[] = { + GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, + GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; + int option = states[luaL_checkoption(L, 1, "", statenames)]; + if (option == -1) { + lua_pushstring(L, statenames[G(L)->gcstate]); + return 1; + } + else { + global_State *g = G(L); + if (G(L)->gckind == KGC_GEN) + luaL_error(L, "cannot change states in generational mode"); + lua_lock(L); + if (option < g->gcstate) { /* must cross 'pause'? */ + luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */ + } + luaC_runtilstate(L, bitmask(option)); + lua_assert(G(L)->gcstate == option); + lua_unlock(L); + return 0; + } +} + + +static int hash_query (lua_State *L) { + if (lua_isnone(L, 2)) { + luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); + lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash); + } + else { + TValue *o = obj_at(L, 1); + Table *t; + luaL_checktype(L, 2, LUA_TTABLE); + t = hvalue(obj_at(L, 2)); + lua_pushinteger(L, luaH_mainposition(t, o) - t->node); + } + return 1; +} + + +static int stacklevel (lua_State *L) { + unsigned long a = 0; + lua_pushinteger(L, (L->top - L->stack)); + lua_pushinteger(L, stacksize(L)); + lua_pushinteger(L, L->nCcalls); + lua_pushinteger(L, L->nci); + lua_pushinteger(L, (unsigned long)&a); + return 5; +} + + +static int table_query (lua_State *L) { + const Table *t; + int i = cast_int(luaL_optinteger(L, 2, -1)); + unsigned int asize; + luaL_checktype(L, 1, LUA_TTABLE); + t = hvalue(obj_at(L, 1)); + asize = luaH_realasize(t); + if (i == -1) { + lua_pushinteger(L, asize); + lua_pushinteger(L, allocsizenode(t)); + lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node); + lua_pushinteger(L, t->alimit); + return 4; + } + else if ((unsigned int)i < asize) { + lua_pushinteger(L, i); + pushobject(L, &t->array[i]); + lua_pushnil(L); + } + else if ((i -= asize) < sizenode(t)) { + TValue k; + getnodekey(L, &k, gnode(t, i)); + if (!isempty(gval(gnode(t, i))) || + ttisnil(&k) || + ttisnumber(&k)) { + pushobject(L, &k); + } + else + lua_pushliteral(L, ""); + pushobject(L, gval(gnode(t, i))); + if (gnext(&t->node[i]) != 0) + lua_pushinteger(L, gnext(&t->node[i])); + else + lua_pushnil(L); + } + return 3; +} + + +static int string_query (lua_State *L) { + stringtable *tb = &G(L)->strt; + int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; + if (s == -1) { + lua_pushinteger(L ,tb->size); + lua_pushinteger(L ,tb->nuse); + return 2; + } + else if (s < tb->size) { + TString *ts; + int n = 0; + for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) { + setsvalue2s(L, L->top, ts); + api_incr_top(L); + n++; + } + return n; + } + else return 0; +} + + +static int tref (lua_State *L) { + int level = lua_gettop(L); + luaL_checkany(L, 1); + lua_pushvalue(L, 1); + lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); + (void)level; /* to avoid warnings */ + lua_assert(lua_gettop(L) == level+1); /* +1 for result */ + return 1; +} + +static int getref (lua_State *L) { + int level = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); + (void)level; /* to avoid warnings */ + lua_assert(lua_gettop(L) == level+1); + return 1; +} + +static int unref (lua_State *L) { + int level = lua_gettop(L); + luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); + (void)level; /* to avoid warnings */ + lua_assert(lua_gettop(L) == level); + return 0; +} + + +static int upvalue (lua_State *L) { + int n = cast_int(luaL_checkinteger(L, 2)); + luaL_checktype(L, 1, LUA_TFUNCTION); + if (lua_isnone(L, 3)) { + const char *name = lua_getupvalue(L, 1, n); + if (name == NULL) return 0; + lua_pushstring(L, name); + return 2; + } + else { + const char *name = lua_setupvalue(L, 1, n); + lua_pushstring(L, name); + return 1; + } +} + + +static int newuserdata (lua_State *L) { + size_t size = cast_sizet(luaL_optinteger(L, 1, 0)); + int nuv = luaL_optinteger(L, 2, 0); + char *p = cast_charp(lua_newuserdatauv(L, size, nuv)); + while (size--) *p++ = '\0'; + return 1; +} + + +static int pushuserdata (lua_State *L) { + lua_Integer u = luaL_checkinteger(L, 1); + lua_pushlightuserdata(L, cast_voidp(cast_sizet(u))); + return 1; +} + + +static int udataval (lua_State *L) { + lua_pushinteger(L, cast(long, lua_touserdata(L, 1))); + return 1; +} + + +static int doonnewstack (lua_State *L) { + lua_State *L1 = lua_newthread(L); + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + int status = luaL_loadbuffer(L1, s, l, s); + if (status == LUA_OK) + status = lua_pcall(L1, 0, 0, 0); + lua_pushinteger(L, status); + return 1; +} + + +static int s2d (lua_State *L) { + lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1)))); + return 1; +} + + +static int d2s (lua_State *L) { + double d = cast(double, luaL_checknumber(L, 1)); + lua_pushlstring(L, cast_charp(&d), sizeof(d)); + return 1; +} + + +static int num2int (lua_State *L) { + lua_pushinteger(L, lua_tointeger(L, 1)); + return 1; +} + + +static int newstate (lua_State *L) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + lua_State *L1 = lua_newstate(f, ud); + if (L1) { + lua_atpanic(L1, tpanic); + lua_pushlightuserdata(L, L1); + } + else + lua_pushnil(L); + return 1; +} + + +static lua_State *getstate (lua_State *L) { + lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1)); + luaL_argcheck(L, L1 != NULL, 1, "state expected"); + return L1; +} + + +static int loadlib (lua_State *L) { + static const luaL_Reg libs[] = { + {LUA_GNAME, luaopen_base}, + {"coroutine", luaopen_coroutine}, + {"debug", luaopen_debug}, + {"io", luaopen_io}, + {"os", luaopen_os}, + {"math", luaopen_math}, + {"string", luaopen_string}, + {"table", luaopen_table}, + {"T", luaB_opentests}, + {NULL, NULL} + }; + lua_State *L1 = getstate(L); + int i; + luaL_requiref(L1, "package", luaopen_package, 0); + lua_assert(lua_type(L1, -1) == LUA_TTABLE); + /* 'requiref' should not reload module already loaded... */ + luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ + /* ...but should return the same module */ + lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); + luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); + for (i = 0; libs[i].name; i++) { + lua_pushcfunction(L1, libs[i].func); + lua_setfield(L1, -2, libs[i].name); + } + return 0; +} + +static int closestate (lua_State *L) { + lua_State *L1 = getstate(L); + lua_close(L1); + return 0; +} + +static int doremote (lua_State *L) { + lua_State *L1 = getstate(L); + size_t lcode; + const char *code = luaL_checklstring(L, 2, &lcode); + int status; + lua_settop(L1, 0); + status = luaL_loadbuffer(L1, code, lcode, code); + if (status == LUA_OK) + status = lua_pcall(L1, 0, LUA_MULTRET, 0); + if (status != LUA_OK) { + lua_pushnil(L); + lua_pushstring(L, lua_tostring(L1, -1)); + lua_pushinteger(L, status); + return 3; + } + else { + int i = 0; + while (!lua_isnone(L1, ++i)) + lua_pushstring(L, lua_tostring(L1, i)); + lua_pop(L1, i-1); + return i-1; + } +} + + +static int log2_aux (lua_State *L) { + unsigned int x = (unsigned int)luaL_checkinteger(L, 1); + lua_pushinteger(L, luaO_ceillog2(x)); + return 1; +} + + +struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; }; + +/* +** does a long-jump back to "main program". +*/ +static int panicback (lua_State *L) { + struct Aux *b; + lua_checkstack(L, 1); /* open space for 'Aux' struct */ + lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */ + b = (struct Aux *)lua_touserdata(L, -1); + lua_pop(L, 1); /* remove 'Aux' struct */ + runC(b->L, L, b->paniccode); /* run optional panic code */ + longjmp(b->jb, 1); + return 1; /* to avoid warnings */ +} + +static int checkpanic (lua_State *L) { + struct Aux b; + void *ud; + lua_State *L1; + const char *code = luaL_checkstring(L, 1); + lua_Alloc f = lua_getallocf(L, &ud); + b.paniccode = luaL_optstring(L, 2, ""); + b.L = L; + L1 = lua_newstate(f, ud); /* create new state */ + if (L1 == NULL) { /* error? */ + lua_pushnil(L); + return 1; + } + lua_atpanic(L1, panicback); /* set its panic function */ + lua_pushlightuserdata(L1, &b); + lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */ + if (setjmp(b.jb) == 0) { /* set jump buffer */ + runC(L, L1, code); /* run code unprotected */ + lua_pushliteral(L, "no errors"); + } + else { /* error handling */ + /* move error message to original state */ + lua_pushstring(L, lua_tostring(L1, -1)); + } + lua_close(L1); + return 1; +} + + + +/* +** {==================================================================== +** function to test the API with C. It interprets a kind of assembler +** language with calls to the API, so the test can be driven by Lua code +** ===================================================================== +*/ + + +static void sethookaux (lua_State *L, int mask, int count, const char *code); + +static const char *const delimits = " \t\n,;"; + +static void skip (const char **pc) { + for (;;) { + if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++; + else if (**pc == '#') { /* comment? */ + while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */ + } + else break; + } +} + +static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { + int res = 0; + int sig = 1; + skip(pc); + if (**pc == '.') { + res = cast_int(lua_tointeger(L1, -1)); + lua_pop(L1, 1); + (*pc)++; + return res; + } + else if (**pc == '*') { + res = lua_gettop(L1); + (*pc)++; + return res; + } + else if (**pc == '-') { + sig = -1; + (*pc)++; + } + if (!lisdigit(cast_uchar(**pc))) + luaL_error(L, "number expected (%s)", *pc); + while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0'; + return sig*res; +} + +static const char *getstring_aux (lua_State *L, char *buff, const char **pc) { + int i = 0; + skip(pc); + if (**pc == '"' || **pc == '\'') { /* quoted string? */ + int quote = *(*pc)++; + while (**pc != quote) { + if (**pc == '\0') luaL_error(L, "unfinished string in C script"); + buff[i++] = *(*pc)++; + } + (*pc)++; + } + else { + while (**pc != '\0' && !strchr(delimits, **pc)) + buff[i++] = *(*pc)++; + } + buff[i] = '\0'; + return buff; +} + + +static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { + skip(pc); + switch (*(*pc)++) { + case 'R': return LUA_REGISTRYINDEX; + case 'G': return luaL_error(L, "deprecated index 'G'"); + case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc)); + default: (*pc)--; return getnum_aux(L, L1, pc); + } +} + + +static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", + "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; + +/* +** Avoid these stat codes from being collected, to avoid possible +** memory error when pushing them. +*/ +static void regcodes (lua_State *L) { + unsigned int i; + for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { + lua_pushboolean(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); + } +} + + +#define EQ(s1) (strcmp(s1, inst) == 0) + +#define getnum (getnum_aux(L, L1, &pc)) +#define getstring (getstring_aux(L, buff, &pc)) +#define getindex (getindex_aux(L, L1, &pc)) + + +static int testC (lua_State *L); +static int Cfunck (lua_State *L, int status, lua_KContext ctx); + +/* +** arithmetic operation encoding for 'arith' instruction +** LUA_OPIDIV -> \ +** LUA_OPSHL -> < +** LUA_OPSHR -> > +** LUA_OPUNM -> _ +** LUA_OPBNOT -> ! +*/ +static const char ops[] = "+-*%^/\\&|~<>_!"; + +static int runC (lua_State *L, lua_State *L1, const char *pc) { + char buff[300]; + int status = 0; + if (pc == NULL) return luaL_error(L, "attempt to runC null script"); + for (;;) { + const char *inst = getstring; + if EQ("") return 0; + else if EQ("absindex") { + lua_pushnumber(L1, lua_absindex(L1, getindex)); + } + else if EQ("append") { + int t = getindex; + int i = lua_rawlen(L1, t); + lua_rawseti(L1, t, i + 1); + } + else if EQ("arith") { + int op; + skip(&pc); + op = strchr(ops, *pc++) - ops; + lua_arith(L1, op); + } + else if EQ("call") { + int narg = getnum; + int nres = getnum; + lua_call(L1, narg, nres); + } + else if EQ("callk") { + int narg = getnum; + int nres = getnum; + int i = getindex; + lua_callk(L1, narg, nres, i, Cfunck); + } + else if EQ("checkstack") { + int sz = getnum; + const char *msg = getstring; + if (*msg == '\0') + msg = NULL; /* to test 'luaL_checkstack' with no message */ + luaL_checkstack(L1, sz, msg); + } + else if EQ("rawcheckstack") { + int sz = getnum; + lua_pushboolean(L1, lua_checkstack(L1, sz)); + } + else if EQ("compare") { + const char *opt = getstring; /* EQ, LT, or LE */ + int op = (opt[0] == 'E') ? LUA_OPEQ + : (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE; + int a = getindex; + int b = getindex; + lua_pushboolean(L1, lua_compare(L1, a, b, op)); + } + else if EQ("concat") { + lua_concat(L1, getnum); + } + else if EQ("copy") { + int f = getindex; + lua_copy(L1, f, getindex); + } + else if EQ("func2num") { + lua_CFunction func = lua_tocfunction(L1, getindex); + lua_pushnumber(L1, cast_sizet(func)); + } + else if EQ("getfield") { + int t = getindex; + lua_getfield(L1, t, getstring); + } + else if EQ("getglobal") { + lua_getglobal(L1, getstring); + } + else if EQ("getmetatable") { + if (lua_getmetatable(L1, getindex) == 0) + lua_pushnil(L1); + } + else if EQ("gettable") { + lua_gettable(L1, getindex); + } + else if EQ("gettop") { + lua_pushinteger(L1, lua_gettop(L1)); + } + else if EQ("gsub") { + int a = getnum; int b = getnum; int c = getnum; + luaL_gsub(L1, lua_tostring(L1, a), + lua_tostring(L1, b), + lua_tostring(L1, c)); + } + else if EQ("insert") { + lua_insert(L1, getnum); + } + else if EQ("iscfunction") { + lua_pushboolean(L1, lua_iscfunction(L1, getindex)); + } + else if EQ("isfunction") { + lua_pushboolean(L1, lua_isfunction(L1, getindex)); + } + else if EQ("isnil") { + lua_pushboolean(L1, lua_isnil(L1, getindex)); + } + else if EQ("isnull") { + lua_pushboolean(L1, lua_isnone(L1, getindex)); + } + else if EQ("isnumber") { + lua_pushboolean(L1, lua_isnumber(L1, getindex)); + } + else if EQ("isstring") { + lua_pushboolean(L1, lua_isstring(L1, getindex)); + } + else if EQ("istable") { + lua_pushboolean(L1, lua_istable(L1, getindex)); + } + else if EQ("isudataval") { + lua_pushboolean(L1, lua_islightuserdata(L1, getindex)); + } + else if EQ("isuserdata") { + lua_pushboolean(L1, lua_isuserdata(L1, getindex)); + } + else if EQ("len") { + lua_len(L1, getindex); + } + else if EQ("Llen") { + lua_pushinteger(L1, luaL_len(L1, getindex)); + } + else if EQ("loadfile") { + luaL_loadfile(L1, luaL_checkstring(L1, getnum)); + } + else if EQ("loadstring") { + const char *s = luaL_checkstring(L1, getnum); + luaL_loadstring(L1, s); + } + else if EQ("newmetatable") { + lua_pushboolean(L1, luaL_newmetatable(L1, getstring)); + } + else if EQ("newtable") { + lua_newtable(L1); + } + else if EQ("newthread") { + lua_newthread(L1); + } + else if EQ("resetthread") { + lua_pushinteger(L1, lua_resetthread(L1)); + } + else if EQ("newuserdata") { + lua_newuserdata(L1, getnum); + } + else if EQ("next") { + lua_next(L1, -2); + } + else if EQ("objsize") { + lua_pushinteger(L1, lua_rawlen(L1, getindex)); + } + else if EQ("pcall") { + int narg = getnum; + int nres = getnum; + status = lua_pcall(L1, narg, nres, getnum); + } + else if EQ("pcallk") { + int narg = getnum; + int nres = getnum; + int i = getindex; + status = lua_pcallk(L1, narg, nres, 0, i, Cfunck); + } + else if EQ("pop") { + lua_pop(L1, getnum); + } + else if EQ("printstack") { + int n = getnum; + if (n != 0) { + printf("%s\n", luaL_tolstring(L1, n, NULL)); + lua_pop(L1, 1); + } + else printstack(L1); + } + else if EQ("print") { + const char *msg = getstring; + printf("%s\n", msg); + } + else if EQ("warningC") { + const char *msg = getstring; + lua_warning(L1, msg, 1); + } + else if EQ("warning") { + const char *msg = getstring; + lua_warning(L1, msg, 0); + } + else if EQ("pushbool") { + lua_pushboolean(L1, getnum); + } + else if EQ("pushcclosure") { + lua_pushcclosure(L1, testC, getnum); + } + else if EQ("pushint") { + lua_pushinteger(L1, getnum); + } + else if EQ("pushnil") { + lua_pushnil(L1); + } + else if EQ("pushnum") { + lua_pushnumber(L1, (lua_Number)getnum); + } + else if EQ("pushstatus") { + lua_pushstring(L1, statcodes[status]); + } + else if EQ("pushstring") { + lua_pushstring(L1, getstring); + } + else if EQ("pushupvalueindex") { + lua_pushinteger(L1, lua_upvalueindex(getnum)); + } + else if EQ("pushvalue") { + lua_pushvalue(L1, getindex); + } + else if EQ("pushfstringI") { + lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1)); + } + else if EQ("pushfstringS") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1)); + } + else if EQ("pushfstringP") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); + } + else if EQ("rawget") { + int t = getindex; + lua_rawget(L1, t); + } + else if EQ("rawgeti") { + int t = getindex; + lua_rawgeti(L1, t, getnum); + } + else if EQ("rawgetp") { + int t = getindex; + lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); + } + else if EQ("rawset") { + int t = getindex; + lua_rawset(L1, t); + } + else if EQ("rawseti") { + int t = getindex; + lua_rawseti(L1, t, getnum); + } + else if EQ("rawsetp") { + int t = getindex; + lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); + } + else if EQ("remove") { + lua_remove(L1, getnum); + } + else if EQ("replace") { + lua_replace(L1, getindex); + } + else if EQ("resume") { + int i = getindex; + int nres; + status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); + } + else if EQ("return") { + int n = getnum; + if (L1 != L) { + int i; + for (i = 0; i < n; i++) { + int idx = -(n - i); + switch (lua_type(L1, idx)) { + case LUA_TBOOLEAN: + lua_pushboolean(L, lua_toboolean(L1, idx)); + break; + default: + lua_pushstring(L, lua_tostring(L1, idx)); + break; + } + } + } + return n; + } + else if EQ("rotate") { + int i = getindex; + lua_rotate(L1, i, getnum); + } + else if EQ("setfield") { + int t = getindex; + const char *s = getstring; + lua_setfield(L1, t, s); + } + else if EQ("seti") { + int t = getindex; + lua_seti(L1, t, getnum); + } + else if EQ("setglobal") { + const char *s = getstring; + lua_setglobal(L1, s); + } + else if EQ("sethook") { + int mask = getnum; + int count = getnum; + const char *s = getstring; + sethookaux(L1, mask, count, s); + } + else if EQ("setmetatable") { + int idx = getindex; + lua_setmetatable(L1, idx); + } + else if EQ("settable") { + lua_settable(L1, getindex); + } + else if EQ("settop") { + lua_settop(L1, getnum); + } + else if EQ("testudata") { + int i = getindex; + lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL); + } + else if EQ("error") { + lua_error(L1); + } + else if EQ("abort") { + abort(); + } + else if EQ("throw") { +#if defined(__cplusplus) +static struct X { int x; } x; + throw x; +#else + luaL_error(L1, "C++"); +#endif + break; + } + else if EQ("tobool") { + lua_pushboolean(L1, lua_toboolean(L1, getindex)); + } + else if EQ("tocfunction") { + lua_pushcfunction(L1, lua_tocfunction(L1, getindex)); + } + else if EQ("tointeger") { + lua_pushinteger(L1, lua_tointeger(L1, getindex)); + } + else if EQ("tonumber") { + lua_pushnumber(L1, lua_tonumber(L1, getindex)); + } + else if EQ("topointer") { + lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex))); + } + else if EQ("touserdata") { + lua_pushlightuserdata(L1, lua_touserdata(L1, getindex)); + } + else if EQ("tostring") { + const char *s = lua_tostring(L1, getindex); + const char *s1 = lua_pushstring(L1, s); + (void)s1; /* to avoid warnings */ + lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); + } + else if EQ("Ltolstring") { + luaL_tolstring(L1, getindex, NULL); + } + else if EQ("type") { + lua_pushstring(L1, luaL_typename(L1, getnum)); + } + else if EQ("xmove") { + int f = getindex; + int t = getindex; + lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f); + lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t); + int n = getnum; + if (n == 0) n = lua_gettop(fs); + lua_xmove(fs, ts, n); + } + else if EQ("isyieldable") { + lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex))); + } + else if EQ("yield") { + return lua_yield(L1, getnum); + } + else if EQ("yieldk") { + int nres = getnum; + int i = getindex; + return lua_yieldk(L1, nres, i, Cfunck); + } + else if EQ("toclose") { + lua_toclose(L1, getnum); + } + else if EQ("closeslot") { + lua_closeslot(L1, getnum); + } + else luaL_error(L, "unknown instruction %s", buff); + } + return 0; +} + + +static int testC (lua_State *L) { + lua_State *L1; + const char *pc; + if (lua_isuserdata(L, 1)) { + L1 = getstate(L); + pc = luaL_checkstring(L, 2); + } + else if (lua_isthread(L, 1)) { + L1 = lua_tothread(L, 1); + pc = luaL_checkstring(L, 2); + } + else { + L1 = L; + pc = luaL_checkstring(L, 1); + } + return runC(L, L1, pc); +} + + +static int Cfunc (lua_State *L) { + return runC(L, L, lua_tostring(L, lua_upvalueindex(1))); +} + + +static int Cfunck (lua_State *L, int status, lua_KContext ctx) { + lua_pushstring(L, statcodes[status]); + lua_setglobal(L, "status"); + lua_pushinteger(L, ctx); + lua_setglobal(L, "ctx"); + return runC(L, L, lua_tostring(L, ctx)); +} + + +static int makeCfunc (lua_State *L) { + luaL_checkstring(L, 1); + lua_pushcclosure(L, Cfunc, lua_gettop(L)); + return 1; +} + + +/* }====================================================== */ + + +/* +** {====================================================== +** tests for C hooks +** ======================================================= +*/ + +/* +** C hook that runs the C script stored in registry.C_HOOK[L] +*/ +static void Chook (lua_State *L, lua_Debug *ar) { + const char *scpt; + const char *const events [] = {"call", "ret", "line", "count", "tailcall"}; + lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); + lua_pushlightuserdata(L, L); + lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */ + scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */ + lua_pop(L, 2); /* remove C_HOOK and script */ + lua_pushstring(L, events[ar->event]); /* may be used by script */ + lua_pushinteger(L, ar->currentline); /* may be used by script */ + runC(L, L, scpt); /* run script from C_HOOK[L] */ +} + + +/* +** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook +*/ +static void sethookaux (lua_State *L, int mask, int count, const char *scpt) { + if (*scpt == '\0') { /* no script? */ + lua_sethook(L, NULL, 0, 0); /* turn off hooks */ + return; + } + lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */ + if (!lua_istable(L, -1)) { /* no hook table? */ + lua_pop(L, 1); /* remove previous value */ + lua_newtable(L); /* create new C_HOOK table */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */ + } + lua_pushlightuserdata(L, L); + lua_pushstring(L, scpt); + lua_settable(L, -3); /* C_HOOK[L] = script */ + lua_sethook(L, Chook, mask, count); +} + + +static int sethook (lua_State *L) { + if (lua_isnoneornil(L, 1)) + lua_sethook(L, NULL, 0, 0); /* turn off hooks */ + else { + const char *scpt = luaL_checkstring(L, 1); + const char *smask = luaL_checkstring(L, 2); + int count = cast_int(luaL_optinteger(L, 3, 0)); + 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; + sethookaux(L, mask, count, scpt); + } + return 0; +} + + +static int coresume (lua_State *L) { + int status, nres; + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "coroutine expected"); + status = lua_resume(co, L, 0, &nres); + if (status != LUA_OK && status != LUA_YIELD) { + lua_pushboolean(L, 0); + lua_insert(L, -2); + return 2; /* return false + error message */ + } + else { + lua_pushboolean(L, 1); + return 1; + } +} + +/* }====================================================== */ + + + +static const struct luaL_Reg tests_funcs[] = { + {"checkmemory", lua_checkmemory}, + {"closestate", closestate}, + {"d2s", d2s}, + {"doonnewstack", doonnewstack}, + {"doremote", doremote}, + {"gccolor", gc_color}, + {"gcage", gc_age}, + {"gcstate", gc_state}, + {"pobj", gc_printobj}, + {"getref", getref}, + {"hash", hash_query}, + {"log2", log2_aux}, + {"limits", get_limits}, + {"listcode", listcode}, + {"printcode", printcode}, + {"listk", listk}, + {"listabslineinfo", listabslineinfo}, + {"listlocals", listlocals}, + {"loadlib", loadlib}, + {"checkpanic", checkpanic}, + {"newstate", newstate}, + {"newuserdata", newuserdata}, + {"num2int", num2int}, + {"pushuserdata", pushuserdata}, + {"querystr", string_query}, + {"querytab", table_query}, + {"ref", tref}, + {"resume", coresume}, + {"s2d", s2d}, + {"sethook", sethook}, + {"stacklevel", stacklevel}, + {"testC", testC}, + {"makeCfunc", makeCfunc}, + {"totalmem", mem_query}, + {"alloccount", alloc_count}, + {"allocfailnext", alloc_failnext}, + {"trick", settrick}, + {"udataval", udataval}, + {"unref", unref}, + {"upvalue", upvalue}, + {NULL, NULL} +}; + + +static void checkfinalmem (void) { + lua_assert(l_memcontrol.numblocks == 0); + lua_assert(l_memcontrol.total == 0); +} + + +int luaB_opentests (lua_State *L) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + lua_atpanic(L, &tpanic); + lua_setwarnf(L, &warnf, L); + lua_pushboolean(L, 0); + lua_setglobal(L, "_WARN"); /* _WARN = false */ + regcodes(L); + atexit(checkfinalmem); + lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol)); + lua_setallocf(L, f, ud); /* exercise this function */ + luaL_newlib(L, tests_funcs); + return 1; +} + +#endif + diff --git a/3rd/lua/ltests.h b/3rd/lua/ltests.h new file mode 100644 index 000000000..ec520498b --- /dev/null +++ b/3rd/lua/ltests.h @@ -0,0 +1,151 @@ +/* +** $Id: ltests.h $ +** Internal Header for Debugging of the Lua Implementation +** See Copyright Notice in lua.h +*/ + +#ifndef ltests_h +#define ltests_h + + +#include +#include + +/* test Lua with compatibility code */ +#define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_LT_LE + + +#define LUA_DEBUG + + +/* turn on assertions */ +#define LUAI_ASSERT + + +/* to avoid warnings, and to make sure value is really unused */ +#define UNUSED(x) (x=0, (void)(x)) + + +/* test for sizes in 'l_sprintf' (make sure whole buffer is available) */ +#undef l_sprintf +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), snprintf(s,sz,f,i)) +#else +#define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), sprintf(s,f,i)) +#endif + + +/* get a chance to test code without jump tables */ +#define LUA_USE_JUMPTABLE 0 + + +/* use 32-bit integers in random generator */ +#define LUA_RAND32 + + +/* memory-allocator control variables */ +typedef struct Memcontrol { + int failnext; + unsigned long numblocks; + unsigned long total; + unsigned long maxmem; + unsigned long memlimit; + unsigned long countlimit; + unsigned long objcount[LUA_NUMTYPES]; +} Memcontrol; + +LUA_API Memcontrol l_memcontrol; + + +/* +** generic variable for debug tricks +*/ +extern void *l_Trick; + + + +/* +** Function to traverse and check all memory used by Lua +*/ +LUAI_FUNC int lua_checkmemory (lua_State *L); + +/* +** Function to print an object GC-friendly +*/ +struct GCObject; +LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); + + +/* test for lock/unlock */ + +struct L_EXTRA { int lock; int *plock; }; +#undef LUA_EXTRASPACE +#define LUA_EXTRASPACE sizeof(struct L_EXTRA) +#define getlock(l) cast(struct L_EXTRA*, lua_getextraspace(l)) +#define luai_userstateopen(l) \ + (getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock)) +#define luai_userstateclose(l) \ + lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock)) +#define luai_userstatethread(l,l1) \ + lua_assert(getlock(l1)->plock == getlock(l)->plock) +#define luai_userstatefree(l,l1) \ + lua_assert(getlock(l)->plock == getlock(l1)->plock) +#define lua_lock(l) lua_assert((*getlock(l)->plock)++ == 0) +#define lua_unlock(l) lua_assert(--(*getlock(l)->plock) == 0) + + + +LUA_API int luaB_opentests (lua_State *L); + +LUA_API void *debug_realloc (void *ud, void *block, + size_t osize, size_t nsize); + +#if defined(lua_c) +#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol) +#define luaL_openlibs(L) \ + { (luaL_openlibs)(L); \ + luaL_requiref(L, "T", luaB_opentests, 1); \ + lua_pop(L, 1); } +#endif + + + +/* change some sizes to give some bugs a chance */ + +#undef LUAL_BUFFERSIZE +#define LUAL_BUFFERSIZE 23 +#define MINSTRTABSIZE 2 +#define MAXIWTHABS 3 + +#define STRCACHE_N 23 +#define STRCACHE_M 5 + +#undef LUAI_USER_ALIGNMENT_T +#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } + + +/* +** This one is not compatible with tests for opcode optimizations, +** as it blocks some optimizations +#define MAXINDEXRK 0 +*/ + + +/* make stack-overflow tests run faster */ +#undef LUAI_MAXSTACK +#define LUAI_MAXSTACK 50000 + + +/* test mode uses more stack space */ +#undef LUAI_MAXCCALLS +#define LUAI_MAXCCALLS 180 + + +/* force Lua to use its own implementations */ +#undef lua_strx2number +#undef lua_number2strx + + +#endif + diff --git a/3rd/lua/onelua.c b/3rd/lua/onelua.c new file mode 100644 index 000000000..3c605981f --- /dev/null +++ b/3rd/lua/onelua.c @@ -0,0 +1,107 @@ +/* +* one.c -- Lua core, libraries, and interpreter in a single file +*/ + +/* default is to build the full interpreter */ +#ifndef MAKE_LIB +#ifndef MAKE_LUAC +#ifndef MAKE_LUA +#define MAKE_LUA +#endif +#endif +#endif + +/* choose suitable platform-specific features */ +/* some of these may need extra libraries such as -ldl -lreadline -lncurses */ +#if 0 +#define LUA_USE_LINUX +#define LUA_USE_MACOSX +#define LUA_USE_POSIX +#define LUA_ANSI +#endif + +/* no need to change anything below this line ----------------------------- */ + +#include "lprefix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* setup for luaconf.h */ +#define LUA_CORE +#define LUA_LIB +#define ltable_c +#define lvm_c +#include "luaconf.h" + +/* do not export internal symbols */ +#undef LUAI_FUNC +#undef LUAI_DDEC +#undef LUAI_DDEF +#define LUAI_FUNC static +#define LUAI_DDEC(def) /* empty */ +#define LUAI_DDEF static + +/* core -- used by all */ +#include "lzio.c" +#include "lctype.c" +#include "lopcodes.c" +#include "lmem.c" +#include "lundump.c" +#include "ldump.c" +#include "lstate.c" +#include "lgc.c" +#include "llex.c" +#include "lcode.c" +#include "lparser.c" +#include "ldebug.c" +#include "lfunc.c" +#include "lobject.c" +#include "ltm.c" +#include "lstring.c" +#include "ltable.c" +#include "ldo.c" +#include "lvm.c" +#include "lapi.c" + +/* auxiliary library -- used by all */ +#include "lauxlib.c" + +/* standard library -- not used by luac */ +#ifndef MAKE_LUAC +#include "lbaselib.c" +#include "lcorolib.c" +#include "ldblib.c" +#include "liolib.c" +#include "lmathlib.c" +#include "loadlib.c" +#include "loslib.c" +#include "lstrlib.c" +#include "ltablib.c" +#include "lutf8lib.c" +#include "linit.c" +#endif + +/* lua */ +#ifdef MAKE_LUA +#include "lua.c" +#endif + +/* luac */ +#ifdef MAKE_LUAC +#include "luac.c" +#endif From c1eeba215d7ab8b2bae12514615c32ae3eb09354 Mon Sep 17 00:00:00 2001 From: zaxbbun Date: Sat, 3 Sep 2022 07:19:03 +0800 Subject: [PATCH 452/565] Update the return values of socket.listen, also return the real port bind to zero (#1638) --- lualib-src/lua-socket.c | 5 +++-- lualib/skynet/cluster.lua | 4 ++-- lualib/snax/gateserver.lua | 4 ++-- service-src/service_gate.c | 2 +- service/clusterd.lua | 5 +++-- service/gate.lua | 3 ++- skynet-src/skynet_socket.c | 2 +- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 19 ++++++++++++++----- skynet-src/socket_server.h | 2 +- 10 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index c95273721..ea97dce68 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -488,13 +488,14 @@ llisten(lua_State *L) { int port = luaL_checkinteger(L,2); int backlog = luaL_optinteger(L,3,BACKLOG); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); - int id = skynet_socket_listen(ctx, host,port,backlog); + int id = skynet_socket_listen(ctx, host, &port, backlog); if (id < 0) { return luaL_error(L, "Listen error"); } lua_pushinteger(L,id); - return 1; + lua_pushinteger(L,port); + return 2; } static size_t diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 8118c2f66..e3437b43b 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -77,9 +77,9 @@ end function cluster.open(port) if type(port) == "string" then - skynet.call(clusterd, "lua", "listen", port) + return skynet.call(clusterd, "lua", "listen", port) else - skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) end end diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 3737f0251..a896b5874 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -41,10 +41,10 @@ function gateserver.start(handler) maxclient = conf.maxclient or 1024 nodelay = conf.nodelay skynet.error(string.format("Listen on %s:%d", address, port)) - socket = socketdriver.listen(address, port) + socket, port = socketdriver.listen(address, port) socketdriver.start(socket) if handler.open then - return handler.open(source, conf) + return handler.open(source, conf, port) end end diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 35fa3ea63..6481937b0 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -331,7 +331,7 @@ start_listen(struct gate *g, char * listen_addr) { portstr[0] = '\0'; host = listen_addr; } - g->listen_id = skynet_socket_listen(ctx, host, port, BACKLOG); + g->listen_id = skynet_socket_listen(ctx, host, &port, BACKLOG); if (g->listen_id < 0) { return 1; } diff --git a/service/clusterd.lua b/service/clusterd.lua index d9f61cd49..9b2758f09 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -148,8 +148,9 @@ function command.listen(source, addr, port) local address = assert(node_address[addr], addr .. " is down") addr, port = string.match(address, "([^:]+):(.*)$") end - skynet.call(gate, "lua", "open", { address = addr, port = port }) - skynet.ret(skynet.pack(nil)) + + local param = { address = addr, port = port } + skynet.ret(skynet.pack(skynet.call(gate, "lua", "open", param))) end function command.sender(source, node) diff --git a/service/gate.lua b/service/gate.lua index d78cdafac..5e530e406 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -11,8 +11,9 @@ skynet.register_protocol { local handler = {} -function handler.open(source, conf) +function handler.open(source, conf, port) watchdog = conf.watchdog or source + return port end function handler.message(fd, msg, sz) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 7ba7a6837..31acafc0b 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -127,7 +127,7 @@ skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_s } int -skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog) { +skynet_socket_listen(struct skynet_context *ctx, const char *host, int *port, int backlog) { uint32_t source = skynet_context_handle(ctx); return socket_server_listen(SOCKET_SERVER, source, host, port, backlog); } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index dd66f834c..59aabb86d 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -29,7 +29,7 @@ void skynet_socket_updatetime(); int skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer); int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer); -int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog); +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); void skynet_socket_close(struct skynet_context *ctx, int id); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f726593b0..b9da6b977 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1936,7 +1936,7 @@ socket_server_shutdown(struct socket_server *ss, uintptr_t opaque, int id) { // return -1 means failed // or return AF_INET or AF_INET6 static int -do_bind(const char *host, int port, int protocol, int *family) { +do_bind(const char *host, int *port, int protocol, int *family) { int fd; int status; int reuse = 1; @@ -1946,7 +1946,7 @@ do_bind(const char *host, int port, int protocol, int *family) { if (host == NULL || host[0] == 0) { host = "0.0.0.0"; // INADDR_ANY } - sprintf(portstr, "%d", port); + sprintf(portstr, "%d", *port); memset( &ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; if (protocol == IPPROTO_TCP) { @@ -1973,6 +1973,15 @@ do_bind(const char *host, int port, int protocol, int *family) { if (status != 0) goto _failed; + if (*port == 0) { + union sockaddr_all sa; + socklen_t len = sizeof(sa); + + if (getsockname(fd, (struct sockaddr *)&sa, &len) == 0) { + *port = ntohs((*family == AF_INET) ? sa.v4.sin_port : sa.v6.sin6_port); + } + } + freeaddrinfo( ai_list ); return fd; _failed: @@ -1983,7 +1992,7 @@ do_bind(const char *host, int port, int protocol, int *family) { } static int -do_listen(const char * host, int port, int backlog) { +do_listen(const char * host, int *port, int backlog) { int family = 0; int listen_fd = do_bind(host, port, IPPROTO_TCP, &family); if (listen_fd < 0) { @@ -1996,8 +2005,8 @@ do_listen(const char * host, int port, int backlog) { return listen_fd; } -int -socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int port, int backlog) { +int +socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int *port, int backlog) { int fd = do_listen(addr, port, backlog); if (fd < 0) { return -1; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index beb628ca9..ae47c855c 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -43,7 +43,7 @@ int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer) int socket_server_send_lowpriority(struct socket_server *, struct socket_sendbuffer *buffer); // ctrl command below returns id -int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); +int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int *port, int backlog); 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); From 01b5d67b110d204e6bbdfc2f6fef372e8e9a3975 Mon Sep 17 00:00:00 2001 From: caiyiheng Date: Sat, 3 Sep 2022 13:45:22 +0800 Subject: [PATCH 453/565] Update .gitignore (#1639) --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 569191276..402b1488a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /skynet.pid 3rd/lua/lua 3rd/lua/luac +3rd/lua/all /cservice /luaclib *.so From 2a8c095e387fa5ad4907ee2c8fa350b388fc7139 Mon Sep 17 00:00:00 2001 From: CKT Date: Sat, 3 Sep 2022 20:51:19 +0800 Subject: [PATCH 454/565] udp socket bugfix (#1640) Co-authored-by: chenkete --- lualib-src/lua-socket.c | 5 +++-- skynet-src/skynet_socket.c | 2 +- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 2 +- skynet-src/socket_server.h | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ea97dce68..3662e7cd7 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -648,12 +648,13 @@ ludp(lua_State *L) { host = address_port(L, tmp, addr, 2, &port); } - int id = skynet_socket_udp(ctx, host, 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; + lua_pushinteger(L, port); + return 2; } static int diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 31acafc0b..00fd77784 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -175,7 +175,7 @@ skynet_socket_nodelay(struct skynet_context *ctx, int id) { } int -skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { +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); } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 59aabb86d..3a6f263fd 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -38,7 +38,7 @@ void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_pause(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(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_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index b9da6b977..745a6335e 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2070,7 +2070,7 @@ socket_server_userobject(struct socket_server *ss, struct socket_object_interfac // UDP int -socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { +socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int *port) { int fd; int family; if (port != 0 || addr != NULL) { diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index ae47c855c..c70438c97 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -54,7 +54,7 @@ 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); +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 From 6dda5f7f98362ae332ba8327ff9762be6eb2dcb9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 3 Sep 2022 22:02:45 +0800 Subject: [PATCH 455/565] Revert "udp socket bugfix (#1640)" This reverts commit 2a8c095e387fa5ad4907ee2c8fa350b388fc7139. --- lualib-src/lua-socket.c | 5 ++--- skynet-src/skynet_socket.c | 2 +- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 2 +- skynet-src/socket_server.h | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 3662e7cd7..ea97dce68 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -648,13 +648,12 @@ ludp(lua_State *L) { host = address_port(L, tmp, addr, 2, &port); } - int id = skynet_socket_udp(ctx, host, &port); + int id = skynet_socket_udp(ctx, host, port); if (id < 0) { return luaL_error(L, "udp init failed"); } lua_pushinteger(L, id); - lua_pushinteger(L, port); - return 2; + return 1; } static int diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 00fd77784..31acafc0b 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -175,7 +175,7 @@ skynet_socket_nodelay(struct skynet_context *ctx, int id) { } int -skynet_socket_udp(struct skynet_context *ctx, const char * addr, int *port) { +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); } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 3a6f263fd..59aabb86d 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -38,7 +38,7 @@ void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_pause(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(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_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 745a6335e..b9da6b977 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2070,7 +2070,7 @@ socket_server_userobject(struct socket_server *ss, struct socket_object_interfac // UDP int -socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int *port) { +socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { int fd; int family; if (port != 0 || addr != NULL) { diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index c70438c97..ae47c855c 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -54,7 +54,7 @@ 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); +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 From 800a3376d66bacf9fd82c4080a92eb2a5cd6a887 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 3 Sep 2022 22:03:11 +0800 Subject: [PATCH 456/565] Revert "Update the return values of socket.listen, also return the real port bind to zero (#1638)" This reverts commit c1eeba215d7ab8b2bae12514615c32ae3eb09354. --- lualib-src/lua-socket.c | 5 ++--- lualib/skynet/cluster.lua | 4 ++-- lualib/snax/gateserver.lua | 4 ++-- service-src/service_gate.c | 2 +- service/clusterd.lua | 5 ++--- service/gate.lua | 3 +-- skynet-src/skynet_socket.c | 2 +- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 19 +++++-------------- skynet-src/socket_server.h | 2 +- 10 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ea97dce68..c95273721 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -488,14 +488,13 @@ llisten(lua_State *L) { int port = luaL_checkinteger(L,2); int backlog = luaL_optinteger(L,3,BACKLOG); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); - int id = skynet_socket_listen(ctx, host, &port, backlog); + int id = skynet_socket_listen(ctx, host,port,backlog); if (id < 0) { return luaL_error(L, "Listen error"); } lua_pushinteger(L,id); - lua_pushinteger(L,port); - return 2; + return 1; } static size_t diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index e3437b43b..8118c2f66 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -77,9 +77,9 @@ end function cluster.open(port) if type(port) == "string" then - return skynet.call(clusterd, "lua", "listen", port) + skynet.call(clusterd, "lua", "listen", port) else - return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) end end diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index a896b5874..3737f0251 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -41,10 +41,10 @@ function gateserver.start(handler) maxclient = conf.maxclient or 1024 nodelay = conf.nodelay skynet.error(string.format("Listen on %s:%d", address, port)) - socket, port = socketdriver.listen(address, port) + socket = socketdriver.listen(address, port) socketdriver.start(socket) if handler.open then - return handler.open(source, conf, port) + return handler.open(source, conf) end end diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 6481937b0..35fa3ea63 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -331,7 +331,7 @@ start_listen(struct gate *g, char * listen_addr) { portstr[0] = '\0'; host = listen_addr; } - g->listen_id = skynet_socket_listen(ctx, host, &port, BACKLOG); + g->listen_id = skynet_socket_listen(ctx, host, port, BACKLOG); if (g->listen_id < 0) { return 1; } diff --git a/service/clusterd.lua b/service/clusterd.lua index 9b2758f09..d9f61cd49 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -148,9 +148,8 @@ function command.listen(source, addr, port) local address = assert(node_address[addr], addr .. " is down") addr, port = string.match(address, "([^:]+):(.*)$") end - - local param = { address = addr, port = port } - skynet.ret(skynet.pack(skynet.call(gate, "lua", "open", param))) + skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.ret(skynet.pack(nil)) end function command.sender(source, node) diff --git a/service/gate.lua b/service/gate.lua index 5e530e406..d78cdafac 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -11,9 +11,8 @@ skynet.register_protocol { local handler = {} -function handler.open(source, conf, port) +function handler.open(source, conf) watchdog = conf.watchdog or source - return port end function handler.message(fd, msg, sz) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 31acafc0b..7ba7a6837 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -127,7 +127,7 @@ skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_s } int -skynet_socket_listen(struct skynet_context *ctx, const char *host, int *port, int backlog) { +skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog) { uint32_t source = skynet_context_handle(ctx); return socket_server_listen(SOCKET_SERVER, source, host, port, backlog); } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 59aabb86d..dd66f834c 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -29,7 +29,7 @@ void skynet_socket_updatetime(); int skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer); int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer); -int skynet_socket_listen(struct skynet_context *ctx, const char *host, int *port, int backlog); +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); void skynet_socket_close(struct skynet_context *ctx, int id); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index b9da6b977..f726593b0 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1936,7 +1936,7 @@ socket_server_shutdown(struct socket_server *ss, uintptr_t opaque, int id) { // return -1 means failed // or return AF_INET or AF_INET6 static int -do_bind(const char *host, int *port, int protocol, int *family) { +do_bind(const char *host, int port, int protocol, int *family) { int fd; int status; int reuse = 1; @@ -1946,7 +1946,7 @@ do_bind(const char *host, int *port, int protocol, int *family) { if (host == NULL || host[0] == 0) { host = "0.0.0.0"; // INADDR_ANY } - sprintf(portstr, "%d", *port); + sprintf(portstr, "%d", port); memset( &ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; if (protocol == IPPROTO_TCP) { @@ -1973,15 +1973,6 @@ do_bind(const char *host, int *port, int protocol, int *family) { if (status != 0) goto _failed; - if (*port == 0) { - union sockaddr_all sa; - socklen_t len = sizeof(sa); - - if (getsockname(fd, (struct sockaddr *)&sa, &len) == 0) { - *port = ntohs((*family == AF_INET) ? sa.v4.sin_port : sa.v6.sin6_port); - } - } - freeaddrinfo( ai_list ); return fd; _failed: @@ -1992,7 +1983,7 @@ do_bind(const char *host, int *port, int protocol, int *family) { } static int -do_listen(const char * host, int *port, int backlog) { +do_listen(const char * host, int port, int backlog) { int family = 0; int listen_fd = do_bind(host, port, IPPROTO_TCP, &family); if (listen_fd < 0) { @@ -2005,8 +1996,8 @@ do_listen(const char * host, int *port, int backlog) { return listen_fd; } -int -socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int *port, int backlog) { +int +socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int port, int backlog) { int fd = do_listen(addr, port, backlog); if (fd < 0) { return -1; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index ae47c855c..beb628ca9 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -43,7 +43,7 @@ int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer) int socket_server_send_lowpriority(struct socket_server *, struct socket_sendbuffer *buffer); // ctrl command below returns id -int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int *port, int backlog); +int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); 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); From c8197328a4a17b5b1ec6cbac5bb36acb876558d1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 00:13:49 +0800 Subject: [PATCH 457/565] socket.listen returns addr,port --- lualib/skynet/socket.lua | 21 ++++++++++++++++++--- lualib/snax/loginserver.lua | 2 +- service/cslave.lua | 5 +---- service/debug_console.lua | 2 +- skynet-src/socket_server.c | 23 ++++++++++++++++++++++- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 2c44d0527..259fa98da 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -103,13 +103,17 @@ socket_message[1] = function(id, size, data) end -- SKYNET_SOCKET_TYPE_CONNECT = 2 -socket_message[2] = function(id, _ , addr) +socket_message[2] = function(id, ud , addr) local s = socket_pool[id] if s == nil then return end -- log remote addr if not s.connected then -- resume may also post connect message + if s.listen then + s.addr = addr + s.port = ud + end s.connected = true wakeup(s) end @@ -220,7 +224,10 @@ local function connect(id, func) protocol = "TCP", } assert(not socket_onclose[id], "socket has onclose callback") - assert(not socket_pool[id], "socket is not closed") + local s2 = socket_pool[id] + if s2 and not s2.listen then + error("socket is not closed") + end socket_pool[id] = s suspend(s) local err = s.connecting @@ -404,7 +411,15 @@ function socket.listen(host, port, backlog) host, port = string.match(host, "([^:]+):(.+)$") port = tonumber(port) end - return driver.listen(host, port, backlog) + local id = driver.listen(host, port, backlog) + local s = { + id = id, + connected = false, + listen = true, + } + socket_pool[id] = s + suspend(s) + return id, s.addr, s.port end -- abandon use to forward socket id to other service diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 7ea36b21a..d672e479c 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -177,7 +177,7 @@ local function launch_master(conf) skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err)) end end - socket.close_fd(fd) -- We haven't call socket.start, so use socket.close_fd rather than socket.close. + socket.close(fd) end) end diff --git a/service/cslave.lua b/service/cslave.lua index daed0116a..4362824bb 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -263,11 +263,8 @@ skynet.start(function() end end) skynet.wait() - socket.close(slave_fd) - else - -- slave_fd does not start, so use close_fd. - socket.close_fd(slave_fd) 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 09ce89332..ea6379118 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -118,7 +118,7 @@ local function console_main_loop(stdin, print, addr) end skynet.start(function() - local listen_socket = socket.listen (ip, port) + local listen_socket, ip, port = socket.listen (ip, port) skynet.error("Start debug console at " .. ip .. ":" .. port) socket.start(listen_socket , function(id, addr) local function print(...) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f726593b0..9974f32f0 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1089,7 +1089,28 @@ listen_socket(struct socket_server *ss, struct request_listen * request, struct goto _failed; } ATOM_STORE(&s->type , SOCKET_TYPE_PLISTEN); - return -1; + result->opaque = request->opaque; + result->id = id; + result->ud = 0; + result->data = "listen"; + + union sockaddr_all u; + socklen_t slen = sizeof(u); + if (getsockname(listen_fd, &u.s, &slen) == 0) { + 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)) == 0) { + result->data = strerror(errno); + return SOCKET_ERR; + } + int sin_port = ntohs((u.s.sa_family == AF_INET) ? u.v4.sin_port : u.v6.sin6_port); + result->data = ss->buffer; + result->ud = sin_port; + } else { + result->data = strerror(errno); + return SOCKET_ERR; + } + + return SOCKET_OPEN; _failed: close(listen_fd); result->opaque = request->opaque; From 01314de8e130ce70252623c253434891ada5e250 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 00:20:38 +0800 Subject: [PATCH 458/565] Add assert --- lualib/skynet/socket.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 259fa98da..6ec3d6bad 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -417,6 +417,7 @@ function socket.listen(host, port, backlog) connected = false, listen = true, } + assert(socket_pool[id] == nil) socket_pool[id] = s suspend(s) return id, s.addr, s.port From b61daaccf1c98d3ca878ca94e9a441d2140e045d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 14:22:13 +0800 Subject: [PATCH 459/565] watchdog returns addr,port --- examples/main.lua | 4 ++-- examples/watchdog.lua | 2 +- lualib-src/lua-netpack.c | 11 ++++++++--- lualib/snax/gateserver.lua | 21 +++++++++++++++++++++ service/gate.lua | 1 + 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 5a2150dbf..4d2bac4af 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -12,11 +12,11 @@ skynet.start(function() skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") - skynet.call(watchdog, "lua", "start", { + local addr,port = skynet.call(watchdog, "lua", "start", { port = 8888, maxclient = max_client, nodelay = true, }) - skynet.error("Watchdog listen on", 8888) + skynet.error("Watchdog listen on " .. addr .. ":" .. port) skynet.exit() end) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index dabbd097a..9b04933a9 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -40,7 +40,7 @@ function SOCKET.data(fd, msg) end function CMD.start(conf) - skynet.call(gate, "lua", "open" , conf) + return skynet.call(gate, "lua", "open" , conf) end function CMD.close(fd) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index ac84972dd..70016f227 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -22,6 +22,7 @@ #define TYPE_OPEN 4 #define TYPE_CLOSE 5 #define TYPE_WARNING 6 +#define TYPE_INIT 7 /* Each package is uint16 + data , uint16 (serialized in big-endian) is the number of bytes comprising the data . @@ -354,8 +355,11 @@ lfilter(lua_State *L) { assert(size == -1); // never padding string return filter_data(L, message->id, (uint8_t *)buffer, message->ud); case SKYNET_SOCKET_TYPE_CONNECT: - // ignore listen fd connect - return 1; + lua_pushvalue(L, lua_upvalueindex(TYPE_INIT)); + lua_pushinteger(L, message->id); + lua_pushlstring(L, buffer, size); + lua_pushinteger(L, message->ud); + return 5; case SKYNET_SOCKET_TYPE_CLOSE: // no more data in fd (message->id) close_uncomplete(L, message->id); @@ -483,8 +487,9 @@ luaopen_skynet_netpack(lua_State *L) { lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); lua_pushliteral(L, "warning"); + lua_pushliteral(L, "init"); - lua_pushcclosure(L, lfilter, 6); + lua_pushcclosure(L, lfilter, 7); lua_setfield(L, -2, "filter"); return 1; diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 3737f0251..75cd6e54e 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -34,6 +34,8 @@ function gateserver.start(handler) assert(handler.message) assert(handler.connect) + local listen_context = {} + function CMD.open( source, conf ) assert(not socket) local address = conf.address or "0.0.0.0" @@ -42,6 +44,12 @@ function gateserver.start(handler) nodelay = conf.nodelay skynet.error(string.format("Listen on %s:%d", address, port)) socket = socketdriver.listen(address, port) + listen_context.co = coroutine.running() + listen_context.fd = socket + skynet.wait(listen_context.co) + conf.address = listen_context.addr + conf.port = listen_context.port + listen_context = nil socketdriver.start(socket) if handler.open then return handler.open(source, conf) @@ -125,6 +133,19 @@ function gateserver.start(handler) end end + function MSG.init(id, addr, port) + if listen_context then + local co = listen_context.co + if co then + assert(id == listen_context.fd) + listen_context.addr = addr + listen_context.port = port + skynet.wakeup(co) + listen_context.co = nil + end + end + end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 diff --git a/service/gate.lua b/service/gate.lua index d78cdafac..1fe35afe4 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -13,6 +13,7 @@ local handler = {} function handler.open(source, conf) watchdog = conf.watchdog or source + return conf.address, conf.port end function handler.message(fd, msg, sz) From 2d5e4ae528aa12d28a9278b02634fa40404aa357 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 14:32:31 +0800 Subject: [PATCH 460/565] cluster.open returns addr, port --- examples/cluster1.lua | 4 ++-- lualib/skynet/cluster.lua | 4 ++-- service/clusterd.lua | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 10b2e2f29..921453d8e 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -19,8 +19,8 @@ skynet.start(function() 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" + skynet.error("Open db", cluster.open "db") + skynet.error("Open db2", cluster.open "db2") -- unique snax service snax.uniqueservice "pingserver" end) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 8118c2f66..e3437b43b 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -77,9 +77,9 @@ end function cluster.open(port) if type(port) == "string" then - skynet.call(clusterd, "lua", "listen", port) + return skynet.call(clusterd, "lua", "listen", port) else - skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) end end diff --git a/service/clusterd.lua b/service/clusterd.lua index d9f61cd49..440cb865a 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -148,8 +148,8 @@ function command.listen(source, addr, port) local address = assert(node_address[addr], addr .. " is down") addr, port = string.match(address, "([^:]+):(.*)$") end - skynet.call(gate, "lua", "open", { address = addr, port = port }) - skynet.ret(skynet.pack(nil)) + local addr, port = skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.ret(skynet.pack(addr, port)) end function command.sender(source, node) From 7b76bcd039d42fbbd294f24bbdca87cb7bcca9d0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 15:42:59 +0800 Subject: [PATCH 461/565] cluster.open(0) returns addr, port --- examples/cluster1.lua | 4 ++-- service/clusterd.lua | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 921453d8e..10b2e2f29 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -19,8 +19,8 @@ skynet.start(function() print(skynet.call(sdb, "lua", "SET", "b", "foobar2")) print(skynet.call(sdb, "lua", "GET", "a")) print(skynet.call(sdb, "lua", "GET", "b")) - skynet.error("Open db", cluster.open "db") - skynet.error("Open db2", cluster.open "db2") + cluster.open "db" + cluster.open "db2" -- unique snax service snax.uniqueservice "pingserver" end) diff --git a/service/clusterd.lua b/service/clusterd.lua index 440cb865a..20faedfa0 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -146,10 +146,15 @@ function command.listen(source, addr, port) local gate = skynet.newservice("gate") if port == nil then local address = assert(node_address[addr], addr .. " is down") - addr, port = string.match(address, "([^:]+):(.*)$") + addr, port = string.match(address, "(.+):([^:]+)$") + port = tonumber(port) + assert(port ~= 0) + skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.ret(skynet.pack(addr, port)) + else + local realaddr, realport = skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.ret(skynet.pack(realaddr, realport)) end - local addr, port = skynet.call(gate, "lua", "open", { address = addr, port = port }) - skynet.ret(skynet.pack(addr, port)) end function command.sender(source, node) From 44e2583dc28962299e98f61937627421b0ed2280 Mon Sep 17 00:00:00 2001 From: ykxpb Date: Tue, 20 Sep 2022 10:56:33 +0800 Subject: [PATCH 462/565] =?UTF-8?q?mongodb=20=E4=BA=A4=E4=BA=92=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=B0=83=E6=95=B4=E4=B8=BA=20OP=5FMSG=20(#1649)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mongodb 交互协议调整为 OP_MSG * Update Co-authored-by: yy151474 --- lualib-src/lua-mongo.c | 443 +++++++------------------------------ lualib/skynet/db/mongo.lua | 229 +++++++++++-------- test/testmongodb.lua | 4 +- 3 files changed, 221 insertions(+), 455 deletions(-) diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index 1f6c6b692..1db07e5d9 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -10,21 +10,17 @@ #include #include -#define OP_REPLY 1 -#define OP_MSG 1000 -#define OP_UPDATE 2001 -#define OP_INSERT 2002 -#define OP_QUERY 2004 -#define OP_GET_MORE 2005 -#define OP_DELETE 2006 -#define OP_KILL_CURSORS 2007 - -#define REPLY_CURSORNOTFOUND 1 -#define REPLY_QUERYFAILURE 2 -#define REPLY_AWAITCAPABLE 8 // ignore because mongo 1.6+ always set it +#define OP_COMPRESSED 2012 +#define OP_MSG 2013 + +typedef enum { + MSG_CHECKSUM_PRESENT = 1 << 0, + MSG_MORE_TO_COME = 1 << 1, + MSG_EXHAUST_ALLOWED = 1 << 16, +} msg_flags_t; -#define DEFAULT_CAP 128 +#define DEFAULT_CAP 128 struct connection { int sock; int id; @@ -106,6 +102,13 @@ write_int32(struct buffer *b, int32_t v) { b->ptr[b->size++] = (uv >> 24)&0xff; } +static inline void +write_int8(struct buffer *b, int8_t v) { + uint8_t uv = (uint8_t)v; + buffer_reserve(b, 1); + b->ptr[b->size++] = uv; +} + static inline void write_bytes(struct buffer *b, const void * buf, int sz) { buffer_reserve(b,sz); @@ -113,6 +116,7 @@ write_bytes(struct buffer *b, const void * buf, int sz) { b->size += sz; } +/* static void write_string(struct buffer *b, const char *key, size_t sz) { buffer_reserve(b,sz+1); @@ -120,6 +124,7 @@ write_string(struct buffer *b, const char *key, size_t sz) { b->ptr[b->size+sz] = '\0'; b->size+=sz+1; } +*/ static inline int reserve_length(struct buffer *b) { @@ -138,418 +143,132 @@ write_length(struct buffer *b, int32_t v, int off) { b->ptr[off++] = (uv >> 24)&0xff; } -// 1 integer id -// 2 integer flags -// 3 string collection name -// 4 integer skip -// 5 integer return number -// 6 document query -// 7 document selector (optional) -// return string package -static int -op_query(lua_State *L) { - int id = luaL_checkinteger(L,1); - document query = lua_touserdata(L,6); - if (query == NULL) { - return luaL_error(L, "require query document"); - } - document selector = lua_touserdata(L,7); - int flags = luaL_checkinteger(L, 2); - size_t nsz = 0; - const char *name = luaL_checklstring(L,3,&nsz); - int skip = luaL_checkinteger(L, 4); - int number = luaL_checkinteger(L, 5); - - luaL_Buffer b; - luaL_buffinit(L,&b); - - struct buffer buf; - buffer_create(&buf); - int len = reserve_length(&buf); - write_int32(&buf, id); - write_int32(&buf, 0); - write_int32(&buf, OP_QUERY); - write_int32(&buf, flags); - write_string(&buf, name, nsz); - write_int32(&buf, skip); - write_int32(&buf, number); - - int32_t query_len = get_length(query); - int total = buf.size + query_len; - int32_t selector_len = 0; - if (selector) { - selector_len = get_length(selector); - total += selector_len; - } - - write_length(&buf, total, len); - luaL_addlstring(&b, (const char *)buf.ptr, buf.size); - buffer_destroy(&buf); - - luaL_addlstring(&b, (const char *)query, query_len); - - if (selector) { - luaL_addlstring(&b, (const char *)selector, selector_len); - } - - luaL_pushresult(&b); +struct header_t { + //int32_t message_length; // total message size, include this + int32_t request_id; // identifier for this message + int32_t response_to; // requestID from the original request(used in responses from the database) + int32_t opcode; // message type - return 1; -} + int32_t flags; +}; // 1 string data // 2 result document table // return boolean succ (false -> request id, error document) // number request_id // document first -// string cursor_id -// integer startfrom static int -op_reply(lua_State *L) { +unpack_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); - struct reply_type { -// int32_t length; // total message size, including this - int32_t request_id; // identifier for this message - int32_t response_id; // requestID from the original request - // (used in reponses from db) - int32_t opcode; // request type - int32_t flags; - int32_t cursor_id[2]; - int32_t starting; - int32_t number; - }; - const struct reply_type* reply = (const struct reply_type*)data; + const struct header_t* h = (const struct header_t*)data; - if (data_len < sizeof(*reply)) { + if (data_len < sizeof(*h)) { lua_pushboolean(L, 0); return 1; } - int id = little_endian(reply->response_id); - int flags = little_endian(reply->flags); - if (flags & REPLY_QUERYFAILURE) { - lua_pushboolean(L,0); - lua_pushinteger(L, id); - lua_pushlightuserdata(L, (void *)(reply+1)); - return 3; + int opcode = little_endian(h->opcode); + if (opcode != OP_MSG) { + return luaL_error(L, "Unsupported opcode:%d", opcode); } - int starting_from = little_endian(reply->starting); - int number = little_endian(reply->number); - int sz = (int)data_len - sizeof(*reply); - const uint8_t * doc = (const uint8_t *)(reply+1); - - if (lua_istable(L,2)) { - int i = 1; - while (sz > 4) { - lua_pushlightuserdata(L, (void *)doc); - lua_rawseti(L, 2, i); + int id = little_endian(h->response_to); + int flags = little_endian(h->flags); - int32_t doc_len = get_length((document)doc); - if (doc_len <= 0) { - return luaL_error(L, "Invalid result bson document"); - } - - doc += doc_len; - sz -= doc_len; - - ++i; - } - if (i != number + 1) { - lua_pushboolean(L,0); - lua_pushinteger(L, id); - return 2; - } - int c = lua_rawlen(L, 2); - for (;i<=c;i++) { - lua_pushnil(L); - lua_rawseti(L, 2, i); + if (flags != 0) { + if ((flags & MSG_CHECKSUM_PRESENT) != 0) { + return luaL_error(L, "Unsupported OP_MSG flag checksumPresent"); } - } 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); - if (number == 0) - lua_pushnil(L); - else - lua_pushlightuserdata(L, (void *)(reply+1)); - if (reply->cursor_id[0] == 0 && reply->cursor_id[1]==0) { - // closed cursor - lua_pushnil(L); - } else { - lua_pushlstring(L, (const char *)(reply->cursor_id), 8); - } - lua_pushinteger(L, starting_from); - return 5; -} - -/* - 1 string cursor_id - return string package - */ -static int -op_kill(lua_State *L) { - size_t cursor_len = 0; - const char * cursor_id = luaL_tolstring(L, 1, &cursor_len); - if (cursor_len != 8) { - return luaL_error(L, "Invalid cursor id"); + if ((flags ^ MSG_MORE_TO_COME) != 0) { + return luaL_error(L, "Unsupported OP_MSG flag:%d", flags); + } } - struct buffer buf; - buffer_create(&buf); + int sz = (int)data_len - sizeof(*h); - int len = reserve_length(&buf); - write_int32(&buf, 0); - write_int32(&buf, 0); - write_int32(&buf, OP_KILL_CURSORS); + const uint8_t * section = (const uint8_t *)(h+1); - write_int32(&buf, 0); - write_int32(&buf, 1); - write_bytes(&buf, cursor_id, 8); + uint8_t payload_type = *section; + const uint8_t * doc = section+1; - write_length(&buf, buf.size, len); + if (payload_type != 0) { + return luaL_error(L, "Unsupported OP_MSG payload type: %d", payload_type); + } - lua_pushlstring(L, (const char *)buf.ptr, buf.size); - buffer_destroy(&buf); + int32_t doc_sz = get_length((document)(doc)); + if ((sz - 1) != doc_sz) { + return luaL_error(L, "Unsupported OP_MSG reply: >1 section"); + } - return 1; + lua_pushboolean(L, 1); + lua_pushinteger(L, id); + lua_pushlightuserdata(L, (void *)(doc)); + return 3; } -/* - 1 string collection - 2 integer single remove - 3 document selector - - return string package - */ +// string 4 bytes length +// return integer static int -op_delete(lua_State *L) { - document selector = lua_touserdata(L,3); - if (selector == NULL) { - luaL_error(L, "Invalid param"); - } - size_t sz = 0; - const char * name = luaL_checklstring(L,1,&sz); - - luaL_Buffer b; - luaL_buffinit(L,&b); - - struct buffer buf; - buffer_create(&buf); - int len = reserve_length(&buf); - write_int32(&buf, 0); - write_int32(&buf, 0); - write_int32(&buf, OP_DELETE); - write_int32(&buf, 0); - write_string(&buf, name, sz); - write_int32(&buf, lua_tointeger(L,2)); - - int32_t selector_len = get_length(selector); - int total = buf.size + selector_len; - write_length(&buf, total, len); - - luaL_addlstring(&b, (const char *)buf.ptr, buf.size); - buffer_destroy(&buf); - - luaL_addlstring(&b, (const char *)selector, selector_len); - luaL_pushresult(&b); - +reply_length(lua_State *L) { + const char * rawlen_str = luaL_checkstring(L, 1); + int rawlen = 0; + memcpy(&rawlen, rawlen_str, sizeof(int)); + int length = little_endian(rawlen); + lua_pushinteger(L, length - 4); return 1; } -/* - 1 integer id - 2 string collection - 3 integer number - 4 cursor_id (8 bytes string/ 64bit) - - return string package - */ +// @param 1 request_id int +// @param 2 flags int +// @param 3 command bson document +// @return static int -op_get_more(lua_State *L) { +op_msg(lua_State *L) { int id = luaL_checkinteger(L, 1); - size_t sz = 0; - const char * name = luaL_checklstring(L,2,&sz); - int number = luaL_checkinteger(L, 3); - size_t cursor_len = 0; - const char * cursor_id = luaL_tolstring(L, 4, &cursor_len); - if (cursor_len != 8) { - return luaL_error(L, "Invalid cursor id"); - } - - struct buffer buf; - buffer_create(&buf); - int len = reserve_length(&buf); - write_int32(&buf, id); - write_int32(&buf, 0); - write_int32(&buf, OP_GET_MORE); - write_int32(&buf, 0); - write_string(&buf, name, sz); - write_int32(&buf, number); - write_bytes(&buf, cursor_id, 8); - write_length(&buf, buf.size, len); - - lua_pushlstring(L, (const char *)buf.ptr, buf.size); - buffer_destroy(&buf); - - return 1; -} + int flags = luaL_checkinteger(L, 2); + document cmd = lua_touserdata(L, 3); -// 1 string collection -// 2 integer flags -// 3 document selector -// 4 document update -// return string package -static int -op_update(lua_State *L) { - document selector = lua_touserdata(L,3); - document update = lua_touserdata(L,4); - if (selector == NULL || update == NULL) { - luaL_error(L, "Invalid param"); + if (cmd == NULL) { + return luaL_error(L, "opmsg require cmd document"); } - size_t sz = 0; - const char * name = luaL_checklstring(L,1,&sz); luaL_Buffer b; luaL_buffinit(L, &b); struct buffer buf; buffer_create(&buf); - // make package header, don't raise L error int len = reserve_length(&buf); + write_int32(&buf, id); write_int32(&buf, 0); - write_int32(&buf, 0); - write_int32(&buf, OP_UPDATE); - write_int32(&buf, 0); - write_string(&buf, name, sz); - write_int32(&buf, lua_tointeger(L,2)); - - int32_t selector_len = get_length(selector); - int32_t update_len = get_length(update); - - int total = buf.size + selector_len + update_len; - write_length(&buf, total, len); - - luaL_addlstring(&b, (const char *)buf.ptr, buf.size); - buffer_destroy(&buf); - - luaL_addlstring(&b, (const char *)selector, selector_len); - luaL_addlstring(&b, (const char *)update, update_len); - - luaL_pushresult(&b); - - return 1; -} - -static int -document_length(lua_State *L) { - if (lua_isuserdata(L, 3)) { - document doc = lua_touserdata(L,3); - return get_length(doc); - } - if (lua_istable(L,3)) { - int total = 0; - int s = lua_rawlen(L,3); - int i; - for (i=1;i<=s;i++) { - lua_rawgeti(L, 3, i); - document doc = lua_touserdata(L,-1); - if (doc == NULL) { - lua_pop(L,1); - return luaL_error(L, "Invalid document at %d", i); - } else { - total += get_length(doc); - lua_pop(L,1); - } - } - return total; - } - return luaL_error(L, "Insert need documents"); -} - -// 1 integer flags -// 2 string collection -// 3 documents -// return string package -static int -op_insert(lua_State *L) { - size_t sz = 0; - const char * name = luaL_checklstring(L,2,&sz); - int dsz = document_length(L); + write_int32(&buf, OP_MSG); + write_int32(&buf, flags); + write_int8(&buf, 0); - luaL_Buffer b; - luaL_buffinit(L, &b); + int32_t cmd_len = get_length(cmd); + int total = buf.size + cmd_len; - struct buffer buf; - buffer_create(&buf); - // make package header, don't raise L error - int len = reserve_length(&buf); - write_int32(&buf, 0); - write_int32(&buf, 0); - write_int32(&buf, OP_INSERT); - write_int32(&buf, lua_tointeger(L,1)); - write_string(&buf, name, sz); - - int total = buf.size + dsz; write_length(&buf, total, len); - luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); - - if (lua_isuserdata(L,3)) { - document doc = lua_touserdata(L,3); - luaL_addlstring(&b, (const char *)doc, get_length(doc)); - } else { - int s = lua_rawlen(L, 3); - int i; - 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)); - } - } + luaL_addlstring(&b, (const char *)cmd, cmd_len); luaL_pushresult(&b); - return 1; } -// string 4 bytes length -// return integer -static int -reply_length(lua_State *L) { - const char * rawlen_str = luaL_checkstring(L, 1); - int rawlen = 0; - memcpy(&rawlen, rawlen_str, sizeof(int)); - int length = little_endian(rawlen); - lua_pushinteger(L, length - 4); - return 1; -} LUAMOD_API int luaopen_skynet_mongo_driver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] ={ - { "query", op_query }, - { "reply", op_reply }, - { "kill", op_kill }, - { "delete", op_delete }, - { "more", op_get_more }, - { "update", op_update }, - { "insert", op_insert }, + { "reply", unpack_reply }, // 接收响应 { "length", reply_length }, + { "op_msg", op_msg}, { NULL, NULL }, }; luaL_newlib(L,l); return 1; -} +} \ No newline at end of file diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 1fa34b15f..00ae86d5a 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -1,5 +1,7 @@ local bson = require "bson" -local socket = require "skynet.socket" + +require "skynet.socket" + local socketchannel = require "skynet.socketchannel" local skynet = require "skynet" local driver = require "skynet.mongo.driver" @@ -68,11 +70,9 @@ local collection_meta = { 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) + local result = {} + local succ, reply_id, document = driver.reply(reply) result.document = document - result.cursor_id = cursor_id - result.startfrom = startfrom result.data = reply return reply_id, succ, result end @@ -298,17 +298,37 @@ function mongo_db:runCommand(cmd,cmd_v,...) local sock = conn.__sock local bson_cmd if not cmd_v then - bson_cmd = bson_encode_order(cmd,1) + -- ensure cmd remains in first place + bson_cmd = bson_encode_order(cmd,1, "$db", self.name) else - bson_cmd = bson_encode_order(cmd,cmd_v,...) + bson_cmd = bson_encode_order(cmd,cmd_v, "$db", self.name, ...) end - local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd) + + local pack = driver.op_msg(request_id, 0, 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 +--- send command without response +function mongo_db:send_command(cmd, cmd_v, ...) + local conn = self.connection + local request_id = conn:genId() + local sock = conn.__sock + local bson_cmd + if not cmd_v then + -- ensure cmd remains in first place + bson_cmd = bson_encode_order(cmd, 1, "$db", self.name, "writeConcern", {w=0}) + else + bson_cmd = bson_encode_order(cmd, cmd_v, "$db", self.name, "writeConcern", {w=0}, ...) + end + + local pack = driver.op_msg(request_id, 2, bson_cmd) + sock:request(pack) + return {ok=1} -- fake successful response +end + function mongo_db:getCollection(collection) local col = { connection = self.connection, @@ -326,10 +346,7 @@ function mongo_collection:insert(doc) if doc._id == nil then 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 - sock:request(pack) + self.database:send_command("insert", self.name, "documents", {bson_encode(doc)}) end local function werror(r) @@ -353,6 +370,11 @@ function mongo_collection:safe_insert(doc) return werror(r) end +function mongo_collection:raw_safe_insert(doc) + local r = self.database:runCommand("insert", self.name, "documents", {doc}) + return werror(r) +end + function mongo_collection:batch_insert(docs) for i=1,#docs do if docs[i]._id == nil then @@ -360,11 +382,12 @@ function mongo_collection:batch_insert(docs) end docs[i] = bson_encode(docs[i]) end - local sock = self.connection.__sock - local pack = driver.insert(0, self.full_name, docs) - sock:request(pack) + + self.database:send_command("insert", self.name, "documents", docs) end +mongo_collection.insert_many = mongo_collection.batch_insert + function mongo_collection:safe_batch_insert(docs) for i = 1, #docs do if docs[i]._id == nil then @@ -377,16 +400,20 @@ function mongo_collection:safe_batch_insert(docs) return werror(r) end -function mongo_collection:update(selector,update,upsert,multi) - 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) +mongo_collection.safe_insert_many = mongo_collection.safe_batch_insert + +function mongo_collection:update(query,update,upsert,multi) + self.database:send_command("update", self.name, "updates", {bson_encode({ + q = query, + u = update, + upsert = upsert, + multi = multi + })}) end -function mongo_collection:safe_update(selector, update, upsert, multi) +function mongo_collection:safe_update(query, update, upsert, multi) local r = self.database:runCommand("update", self.name, "updates", {bson_encode({ - q = selector, + q = query, u = update, upsert = upsert, multi = multi, @@ -394,6 +421,20 @@ function mongo_collection:safe_update(selector, update, upsert, multi) return werror(r) end +function mongo_collection:batch_update(updates) + local updates_tb = {} + for i = 1, #updates do + updates_tb[i] = bson_encode({ + q = updates[i].query, + u = updates[i].update, + upsert = updates[i].upsert, + multi = updates[i].multi, + }) + end + + self.database:send_command("update", self.name, "updates", updates_tb) +end + function mongo_collection:safe_batch_update(updates) local updates_tb = {} for i = 1, #updates do @@ -409,25 +450,31 @@ function mongo_collection:safe_batch_update(updates) return werror(r) end -function mongo_collection:delete(selector, single) - local sock = self.connection.__sock - local pack = driver.delete(self.full_name, single, bson_encode(selector)) - sock:request(pack) +function mongo_collection:raw_safe_update(update) + local r = self.database:runCommand("update", self.name, "updates", {update}) + return werror(r) end -function mongo_collection:safe_delete(selector, single) +function mongo_collection:delete(query, single) + self.database:runCommand("delete", self.name, "deletes", {bson_encode({ + q = query, + limit = single and 1 or 0, + })}) +end + +function mongo_collection:safe_delete(query, single) local r = self.database:runCommand("delete", self.name, "deletes", {bson_encode({ - q = selector, + q = query, limit = single and 1 or 0, })}) return werror(r) end -function mongo_collection:safe_batch_delete(selectors, single) +function mongo_collection:safe_batch_delete(deletes, single) local delete_tb = {} - for i = 1, #selectors do + for i = 1, #deletes do delete_tb[i] = bson_encode({ - q = selectors[i], + q = deletes[i], limit = single and 1 or 0, }) end @@ -435,30 +482,29 @@ function mongo_collection:safe_batch_delete(selectors, single) return werror(r) end -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 - return bson_decode(doc) +function mongo_collection:raw_safe_delete(delete) + local r = self.database:runCommand("delete", self.name, "deletes", {delete}) + return werror(r) +end + +function mongo_collection:findOne(query, projection) + local cursor = self:find(query, projection) + return cursor:hasNext() and cursor:next() end -function mongo_collection:find(query, selector) +function mongo_collection:find(query, projection) return setmetatable( { __collection = self, __query = query and bson_encode(query) or empty_bson, - __selector = selector and bson_encode(selector), + __projection = projection and bson_encode(projection) or empty_bson, __ptr = nil, __data = nil, __cursor = nil, __document = {}, __flags = 0, __skip = 0, - __sortquery = nil, __limit = 0, + __sort = empty_bson, } , cursor_meta) end @@ -479,7 +525,7 @@ function mongo_cursor:sort(key, key_v, ...) 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} + self.__sort = key return self end @@ -610,21 +656,37 @@ function mongo_collection:findAndModify(doc) return self.database:runCommand(table.unpack(cmd)) end +-- https://docs.mongodb.com/manual/reference/command/aggregate/ +-- collection:aggregate({ { ["$project"] = {tags = 1} } }, {cursor={}}) +-- @param pipeline: array +-- @param options: map +-- @return +function mongo_collection:aggregate(pipeline, options) + assert(pipeline) + local cmd = {"aggregate", self.name, "pipeline", pipeline} + for k, v in pairs(options) do + table.insert(cmd, k) + table.insert(cmd, v) + end + return self.database:runCommand(table.unpack(cmd)) +end + function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then return false end - local conn = self.__collection.connection - local request_id = conn:genId() - local sock = conn.__sock - local pack + local response + + local database = self.__collection.database if self.__data == nil then - 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) + local name = self.__collection.name + response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort, + "skip", self.__skip, "limit", self.__limit, "projection", self.__projection) else - if self.__cursor then - pack = driver.more(request_id, self.__collection.full_name, self.__limit, self.__cursor) + if self.__cursor and self.__cursor > 0 then + local name = self.__collection.name + response = database:runCommand("getMore", bson.int64(self.__cursor), "collection", name, "batchSize", self.__limit) else -- no more self.__document = nil @@ -633,45 +695,31 @@ function mongo_cursor:hasNext() end end - local ok, result = pcall(sock.request,sock,pack, request_id) - - local doc = result.document - local cursor = result.cursor_id - - if ok then - if doc then - local doc = result.result - self.__document = doc - self.__data = result.data - self.__ptr = 1 - self.__cursor = cursor - local limit = self.__limit - if cursor and limit > 0 then - limit = limit - #doc - if limit <= 0 then - -- reach limit - self:close() - end - self.__limit = limit - end - return true - else - self.__document = nil - self.__data = nil - self.__cursor = nil - return false - end - else + if response.ok ~= 1 then self.__document = nil self.__data = nil self.__cursor = nil - if doc then - local err = bson_decode(doc) - error(err["$err"]) - else - error("Reply from mongod error") + error(response["$err"] or "Reply from mongod error") + end + + local cursor = response.cursor + self.__document = cursor.firstBatch or cursor.nextBatch + self.__data = response + self.__ptr = 1 + self.__cursor = cursor.id + + local limit = self.__limit + if cursor.id > 0 and limit > 0 then + limit = limit - #self.__document + if limit <= 0 then + -- reach limit + self:close() end + + self.__limit = limit end + + return true end return true @@ -681,7 +729,7 @@ function mongo_cursor:next() if self.__ptr == nil then error "Call hasNext first" end - local r = bson_decode(self.__document[self.__ptr]) + local r = self.__document[self.__ptr] self.__ptr = self.__ptr + 1 if self.__ptr > #self.__document then self.__ptr = nil @@ -692,11 +740,10 @@ end function mongo_cursor:close() if self.__cursor then - local sock = self.__collection.connection.__sock - local pack = driver.kill(self.__cursor) - sock:request(pack) + local coll = self.__collection + coll.database:send_command("killCursors", coll.name, "cursors", {self.__cursor}) self.__cursor = nil end end -return mongo +return mongo \ No newline at end of file diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 9f1fd8105..bff60fab2 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -14,7 +14,7 @@ local function _create_client() { host = host, port = port, username = username, password = password, - authdb = db_name, + authdb = "admin", } ) end @@ -209,7 +209,7 @@ local function test_safe_batch_delete() db.testcoll:safe_batch_delete(docs) local ret = db.testcoll:find() - assert(length == ret:count(), "test safe batch delete failed") + assert((length - del_num) == ret:count(), "test safe batch delete failed") end skynet.start(function() From 182a1a8d56f5247f4f4e6975a6b7d3d0b127e74f Mon Sep 17 00:00:00 2001 From: ykxpb Date: Tue, 20 Sep 2022 22:20:14 +0800 Subject: [PATCH 463/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20mongo=20=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=91=8A=E8=AD=A6=EF=BC=8C=E5=88=A0=E9=99=A4=20unused?= =?UTF-8?q?=20function=20(#1650)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: yy151474 --- 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 1db07e5d9..09a66b157 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -109,6 +109,7 @@ write_int8(struct buffer *b, int8_t v) { b->ptr[b->size++] = uv; } +/* static inline void write_bytes(struct buffer *b, const void * buf, int sz) { buffer_reserve(b,sz); @@ -116,7 +117,6 @@ write_bytes(struct buffer *b, const void * buf, int sz) { b->size += sz; } -/* static void write_string(struct buffer *b, const char *key, size_t sz) { buffer_reserve(b,sz+1); From ccfdc9440aa2d89b9e6d72f1fdf41be11d062a4a Mon Sep 17 00:00:00 2001 From: ykxpb Date: Thu, 22 Sep 2022 14:51:04 +0800 Subject: [PATCH 464/565] Revert lua makefile of "update lua" (#1652) This reverts commit dfc706615e585713ecf9384f7a3cb71ed72dca6b. Co-authored-by: yy151474 --- 3rd/lua/makefile | 231 +++++++++++++++++++++++++---------------------- Makefile | 2 +- 2 files changed, 125 insertions(+), 108 deletions(-) diff --git a/3rd/lua/makefile b/3rd/lua/makefile index a56359b90..fd5caf14f 100644 --- a/3rd/lua/makefile +++ b/3rd/lua/makefile @@ -1,140 +1,159 @@ -# Developer's makefile for building Lua -# see luaconf.h for further customization +# Makefile for building Lua +# See ../doc/readme.html for installation and customization instructions. # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= -# Warnings valid for both C and C++ -CWARNSCPP= \ - -Wfatal-errors \ - -Wextra \ - -Wshadow \ - -Wsign-compare \ - -Wundef \ - -Wwrite-strings \ - -Wredundant-decls \ - -Wdisabled-optimization \ - -Wdouble-promotion \ - -Wmissing-declarations \ - # the next warnings might be useful sometimes, - # but usually they generate too much noise - # -Werror \ - # -pedantic # warns if we use jump tables \ - # -Wconversion \ - # -Wsign-conversion \ - # -Wstrict-overflow=2 \ - # -Wformat=2 \ - # -Wcast-qual \ - - -# Warnings for gcc, not valid for clang -CWARNGCC= \ - -Wlogical-op \ - -Wno-aggressive-loop-optimizations \ - - -# The next warnings are neither valid nor needed for C++ -CWARNSC= -Wdeclaration-after-statement \ - -Wmissing-prototypes \ - -Wnested-externs \ - -Wstrict-prototypes \ - -Wc++-compat \ - -Wold-style-definition \ - - -CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) - -# Some useful compiler options for internal tests: -# -DLUAI_ASSERT turns on all assertions inside Lua. -# -DHARDSTACKTESTS forces a reallocation of the stack at every point where -# the stack can be reallocated. -# -DHARDMEMTESTS forces a full collection at all points where the collector -# can run. -# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation. -# -DEXTERNMEMCHECK removes internal consistency checking of blocks being -# deallocated (useful when an external tool like valgrind does the check). -# -DMAXINDEXRK=k limits range of constants in RK instruction operands. -# -DLUA_COMPAT_5_3 - -# -pg -malign-double -# -DLUA_USE_CTYPE -DLUA_USE_APICHECK -# ('-ftrapv' for runtime checks of integer overflows) -# -fsanitize=undefined -ftrapv -fno-inline -# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g - - -LOCAL = $(TESTS) $(CWARNS) - - -# enable Linux goodies -MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -I../../skynet-src -MYLDFLAGS= $(LOCAL) -Wl,-E -MYLIBS= -ldl - - -CC= gcc -CFLAGS= -Wall -O2 $(MYCFLAGS) -fno-stack-protector -fno-common -march=native -AR= ar rc +# Your platform. See PLATS for possible values. +PLAT= guess + +CC= gcc -std=gnu99 +CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) +LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) +LIBS= -lm $(SYSLIBS) $(MYLIBS) + +AR= ar rcu RANLIB= ranlib RM= rm -f +UNAME= uname + +SYSCFLAGS= +SYSLDFLAGS= +SYSLIBS= +MYCFLAGS=-I../../skynet-src -g +MYLDFLAGS= +MYLIBS= +MYOBJS= -# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= +# Special flags for compiler modules; -Os reduces code size. +CMCFLAGS= +# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -LIBS = -lm +PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris -CORE_T= 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 ltests.o -AUX_O= lauxlib.o -LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ - lutf8lib.o loadlib.o lcorolib.o linit.o +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 lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o +BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) LUA_T= lua LUA_O= lua.o +LUAC_T= luac +LUAC_O= luac.o -ALL_T= $(CORE_T) $(LUA_T) -ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O) -ALL_A= $(CORE_T) +ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) +ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) +ALL_A= $(LUA_A) + +# Targets start here. +default: $(PLAT) all: $(ALL_T) - touch all o: $(ALL_O) a: $(ALL_A) -$(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O) - $(AR) $@ $? +$(LUA_A): $(BASE_O) + $(AR) $@ $(BASE_O) $(RANLIB) $@ -$(LUA_T): $(LUA_O) $(CORE_T) - $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL) +$(LUA_T): $(LUA_O) $(LUA_A) + $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) + +$(LUAC_T): $(LUAC_O) $(LUA_A) + $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) +test: + ./$(LUA_T) -v clean: $(RM) $(ALL_T) $(ALL_O) depend: - @$(CC) $(CFLAGS) -MM *.c + @$(CC) $(CFLAGS) -MM l*.c echo: - @echo "CC = $(CC)" - @echo "CFLAGS = $(CFLAGS)" - @echo "AR = $(AR)" - @echo "RANLIB = $(RANLIB)" - @echo "RM = $(RM)" - @echo "MYCFLAGS = $(MYCFLAGS)" - @echo "MYLDFLAGS = $(MYLDFLAGS)" - @echo "MYLIBS = $(MYLIBS)" - @echo "DL = $(DL)" + @echo "PLAT= $(PLAT)" + @echo "CC= $(CC)" + @echo "CFLAGS= $(CFLAGS)" + @echo "LDFLAGS= $(LDFLAGS)" + @echo "LIBS= $(LIBS)" + @echo "AR= $(AR)" + @echo "RANLIB= $(RANLIB)" + @echo "RM= $(RM)" + @echo "UNAME= $(UNAME)" + +# Convenience targets for popular platforms. +ALL= all + +help: + @echo "Do 'make PLATFORM' where PLATFORM is one of these:" + @echo " $(PLATS)" + @echo "See doc/readme.html for complete instructions." + +guess: + @echo Guessing `$(UNAME)` + @$(MAKE) `$(UNAME)` + +AIX aix: + $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" + +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 '*** Make sure to compile all external Lua libraries' + @echo '*** with LUA_USE_C89 to ensure consistency' + @echo '' + +FreeBSD NetBSD OpenBSD freebsd: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" + +generic: $(ALL) + +Linux linux: linux-noreadline + +linux-noreadline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" + +linux-readline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" + +Darwin macos macosx: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" + +mingw: + $(MAKE) "LUA_A=lua54.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 + +posix: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" + +SunOS solaris: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" + +# Targets that do not create files (not all makes understand .PHONY). +.PHONY: all $(PLATS) help test clean default o a depend echo + +# Compiler modules may use special flags. +llex.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c + +lparser.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c -$(ALL_O): makefile ltests.h +lcode.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c -# DO NOT EDIT -# automatically made with 'gcc -MM l*.c' +# 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 \ @@ -185,13 +204,11 @@ 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 -ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \ - lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \ - ltable.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 lgc.h lstring.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 ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.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 diff --git a/Makefile b/Makefile index 95a299d6b..337ed3e6a 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua $(LUA_STATICLIB) : - cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' + cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # https : turn on TLS_MODULE to add https support From bc4800f326f3ea4844810e412e805576f8a7110c Mon Sep 17 00:00:00 2001 From: ykxpb Date: Sat, 24 Sep 2022 08:41:37 +0800 Subject: [PATCH 465/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20mongo=20=E6=B8=B8?= =?UTF-8?q?=E6=A0=87=E6=B2=A1=E6=9C=89=E6=95=B0=E6=8D=AE=20hasNext=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20true=EF=BC=8C=E4=BB=A5=E5=8F=8A=E6=97=A0?= =?UTF-8?q?=E6=95=88=E5=85=B3=E9=97=AD=E6=B8=B8=E6=A0=87=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#1653)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: yy151474 --- lualib/skynet/db/mongo.lua | 16 ++++++++++++---- test/testmongodb.lua | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 00ae86d5a..d51b4d148 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -14,6 +14,7 @@ local table = table local bson_encode = bson.encode local bson_encode_order = bson.encode_order local bson_decode = bson.decode +local bson_int64 = bson.int64 local empty_bson = bson_encode {} local mongo = {} @@ -489,7 +490,10 @@ end function mongo_collection:findOne(query, projection) local cursor = self:find(query, projection) - return cursor:hasNext() and cursor:next() + if cursor:hasNext() then + return cursor:next() + end + return nil end function mongo_collection:find(query, projection) @@ -686,7 +690,7 @@ function mongo_cursor:hasNext() else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name - response = database:runCommand("getMore", bson.int64(self.__cursor), "collection", name, "batchSize", self.__limit) + response = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name, "batchSize", self.__limit) else -- no more self.__document = nil @@ -719,6 +723,10 @@ function mongo_cursor:hasNext() self.__limit = limit end + if cursor.id == 0 and #self.__document == 0 then -- nomore + return false + end + return true end @@ -739,9 +747,9 @@ function mongo_cursor:next() end function mongo_cursor:close() - if self.__cursor then + if self.__cursor and self.__cursor > 0 then local coll = self.__collection - coll.database:send_command("killCursors", coll.name, "cursors", {self.__cursor}) + coll.database:send_command("killCursors", coll.name, "cursors", {bson_int64(self.__cursor)}) self.__cursor = nil end end diff --git a/test/testmongodb.lua b/test/testmongodb.lua index bff60fab2..89033e994 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -79,11 +79,20 @@ function test_find_and_remove() db.testcoll:dropIndex("*") db.testcoll:drop() + local cursor = db.testcoll:find() + assert(cursor:hasNext() == false) + db.testcoll:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) assert(ok and ret and ret.n == 1, err) + cursor = db.testcoll:find() + assert(cursor:hasNext() == true) + local v = cursor:next() + assert(v) + assert(v.test_key == 1) + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) assert(ok and ret and ret.n == 1, err) From feeda06e9db51eec446ba307fda9fe4a2292df82 Mon Sep 17 00:00:00 2001 From: samoyedsun Date: Tue, 27 Sep 2022 10:54:22 +0800 Subject: [PATCH 466/565] fix annotation (#1655) --- 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 9974f32f0..e21a17415 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1365,7 +1365,7 @@ dec_sending_ref(struct socket_server *ss, int id) { static int ctrl_cmd(struct socket_server *ss, struct socket_message *result) { int fd = ss->recvctrl_fd; - // the length of message is one byte, so 256+8 buffer size is enough. + // the length of message is one byte, so 256 buffer size is enough. uint8_t buffer[256]; uint8_t header[2]; block_readpipe(fd, header, sizeof(header)); From 1a1d8abd2ebdbc592d5d407c9133afb9a4a76f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E4=BA=91?= Date: Mon, 17 Oct 2022 19:29:33 +0800 Subject: [PATCH 467/565] read 'x-real-ip' header from nginx in websocket lib. (#1665) * read 'x-real-ip' header from nginx in websocket lib. * change default real_ip value to nil --- lualib/http/websocket.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index f09b826ff..a7e62ea7b 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -139,6 +139,9 @@ local function read_handshake(self, upgrade_ops) end end + -- read 'x-real-ip' header from nginx + self.real_ip = header["x-real-ip"] + -- response handshake local accept = crypt.base64encode(crypt.sha1(sw_key .. self.guid)) local resp = "HTTP/1.1 101 Switching Protocols\r\n".. @@ -497,6 +500,11 @@ function M.addrinfo(id) return ws_obj.addr end +function M.real_ip(id) + local ws_obj = assert(ws_pool[id]) + return ws_obj.real_ip +end + function M.close(id, code ,reason) local ws_obj = ws_pool[id] if not ws_obj then From ae35b3c4e894d67e896e76643334d5663120dd84 Mon Sep 17 00:00:00 2001 From: learno Date: Wed, 19 Oct 2022 17:04:49 +0800 Subject: [PATCH 468/565] bugfix: mongo getMore command field batchSize cannot replace with limit (#1666) --- lualib/skynet/db/mongo.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index d51b4d148..958f96732 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -690,7 +690,7 @@ function mongo_cursor:hasNext() else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name - response = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name, "batchSize", self.__limit) + response = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name) else -- no more self.__document = nil @@ -754,4 +754,4 @@ function mongo_cursor:close() end end -return mongo \ No newline at end of file +return mongo From 94a76269aaf313e2d6dcf9a08ed1e697302d8d89 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 27 Oct 2022 15:30:06 +0800 Subject: [PATCH 469/565] Fix #1668 --- lualib-src/lua-cluster.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index da6712786..668bb1f1c 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -603,6 +603,11 @@ lnodename(lua_State *L) { pid_t pid = getpid(); char hostname[256]; if (gethostname(hostname, sizeof(hostname))==0) { + int i; + for (i=0; hostname[i]; i++) { + if (hostname[i] <= ' ') + hostname[i] = '_'; + } lua_pushfstring(L, "%s%d", hostname, (int)pid); } else { lua_pushfstring(L, "noname%d", (int)pid); From 499610965a1dd88015b7e8e36ceb830d02ebfe73 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Nov 2022 10:28:38 +0800 Subject: [PATCH 470/565] Update lua --- 3rd/lua/lapi.c | 239 ++++++------ 3rd/lua/lapi.h | 17 +- 3rd/lua/lauxlib.c | 8 +- 3rd/lua/lcorolib.c | 4 +- 3rd/lua/ldebug.c | 52 +-- 3rd/lua/ldebug.h | 2 +- 3rd/lua/ldo.c | 175 +++++---- 3rd/lua/ldo.h | 7 +- 3rd/lua/lfunc.c | 47 ++- 3rd/lua/lfunc.h | 4 +- 3rd/lua/lgc.c | 34 +- 3rd/lua/lgc.h | 2 + 3rd/lua/llex.c | 4 +- 3rd/lua/lobject.c | 8 +- 3rd/lua/lobject.h | 17 +- 3rd/lua/loslib.c | 4 +- 3rd/lua/lparser.c | 6 +- 3rd/lua/lstate.c | 57 ++- 3rd/lua/lstate.h | 14 +- 3rd/lua/ltable.c | 6 +- 3rd/lua/ltable.h | 1 - 3rd/lua/ltablib.c | 2 +- 3rd/lua/ltests.c | 36 +- 3rd/lua/ltm.c | 38 +- 3rd/lua/lua.h | 2 +- 3rd/lua/luac.c | 725 ------------------------------------ 3rd/lua/luaconf.h | 5 +- 3rd/lua/lundump.c | 6 +- 3rd/lua/lutf8lib.c | 27 +- 3rd/lua/lvm.c | 104 +++--- 3rd/lua/lvm.h | 5 + 3rd/lua/makefile | 2 +- service-src/service_snlua.c | 2 +- 33 files changed, 494 insertions(+), 1168 deletions(-) delete mode 100644 3rd/lua/luac.c diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 6678f7bd7..25184ece5 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -60,27 +60,28 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return &G(L)->nilvalue; + StkId o = ci->func.p + idx; + api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); + if (o >= L->top.p) return &G(L)->nilvalue; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return s2v(L->top + idx); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); + return s2v(L->top.p + 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"); - if (ttisCclosure(s2v(ci->func))) { /* C closure? */ - CClosure *func = clCvalue(s2v(ci->func)); + if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ + CClosure *func = clCvalue(s2v(ci->func.p)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } else { /* light C function or Lua function (through a hook)?) */ - api_check(L, ttislcf(s2v(ci->func)), "caller not a C function"); + api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function"); return &G(L)->nilvalue; /* no upvalues */ } } @@ -94,14 +95,15 @@ static TValue *index2value (lua_State *L, int idx) { l_sinline StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, o < L->top, "invalid index"); + StkId o = ci->func.p + idx; + api_check(L, o < L->top.p, "invalid index"); return o; } else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); - return L->top + idx; + return L->top.p + idx; } } @@ -112,12 +114,12 @@ LUA_API int lua_checkstack (lua_State *L, int n) { lua_lock(L); ci = L->ci; api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ + if (L->stack_last.p - L->top.p > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else /* need to grow stack */ res = luaD_growstack(L, n, 0); - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ + if (res && ci->top.p < L->top.p + n) + ci->top.p = L->top.p + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -129,11 +131,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, "stack overflow"); - from->top -= n; + api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); + from->top.p -= n; for (i = 0; i < n; i++) { - setobjs2s(to, to->top, from->top + i); - to->top++; /* stack already checked by previous 'api_check' */ + setobjs2s(to, to->top.p, from->top.p + i); + to->top.p++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } @@ -167,12 +169,12 @@ LUA_API 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.p - L->ci->func.p) + idx; } LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); + return cast_int(L->top.p - (L->ci->func.p + 1)); } @@ -182,24 +184,24 @@ LUA_API void lua_settop (lua_State *L, int idx) { ptrdiff_t diff; /* difference for new top */ lua_lock(L); ci = L->ci; - func = ci->func; + func = ci->func.p; if (idx >= 0) { - api_check(L, idx <= ci->top - (func + 1), "new top too large"); - diff = ((func + 1) + idx) - L->top; + api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); + diff = ((func + 1) + idx) - L->top.p; for (; diff > 0; diff--) - setnilvalue(s2v(L->top++)); /* clear new slots */ + setnilvalue(s2v(L->top.p++)); /* clear new slots */ } else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); + api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } - api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot"); - newtop = L->top + diff; - if (diff < 0 && L->tbclist >= newtop) { + api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); + newtop = L->top.p + diff; + if (diff < 0 && L->tbclist.p >= newtop) { lua_assert(hastocloseCfunc(ci->nresults)); newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } - L->top = newtop; /* correct top only after closing any upvalue */ + L->top.p = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); } @@ -208,7 +210,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); - api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, + api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level, "no variable to close at given level"); level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); @@ -239,7 +241,7 @@ l_sinline void reverse (lua_State *L, StkId from, StkId to) { LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); - t = L->top - 1; /* end of stack segment being rotated */ + t = L->top.p - 1; /* end of stack segment being rotated */ p = index2stack(L, idx); /* start of segment */ api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ @@ -258,7 +260,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -267,7 +269,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); - setobj2s(L, L->top, index2value(L, idx)); + setobj2s(L, L->top.p, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } @@ -336,12 +338,12 @@ LUA_API void lua_arith (lua_State *L, int op) { 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); + setobjs2s(L, L->top.p, L->top.p - 1); api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); - L->top--; /* remove second operand */ + luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); + L->top.p--; /* remove second operand */ lua_unlock(L); } @@ -367,7 +369,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, s2v(L->top)); + size_t sz = luaO_str2num(s, s2v(L->top.p)); if (sz != 0) api_incr_top(L); return sz; @@ -494,7 +496,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -502,7 +504,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setfltvalue(s2v(L->top), n); + setfltvalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -510,7 +512,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); - setivalue(s2v(L->top), n); + setivalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -525,7 +527,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -536,11 +538,11 @@ 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_lock(L); if (s == NULL) - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); else { TString *ts; ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); @@ -577,7 +579,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { - setfvalue(s2v(L->top), fn); + setfvalue(s2v(L->top.p), fn); api_incr_top(L); } else { @@ -586,13 +588,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { api_check(L, n <= MAXUPVAL, "upvalue index too large"); cl = luaF_newCclosure(L, n); cl->f = fn; - L->top -= n; + L->top.p -= n; while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); + setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); /* does not need barrier because closure is white */ lua_assert(iswhite(cl)); } - setclCvalue(L, s2v(L->top), cl); + setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L); luaC_checkGC(L); } @@ -603,9 +605,9 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); if (b) - setbtvalue(s2v(L->top)); + setbtvalue(s2v(L->top.p)); else - setbfvalue(s2v(L->top)); + setbfvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -613,7 +615,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); - setpvalue(s2v(L->top), p); + setpvalue(s2v(L->top.p), p); api_incr_top(L); lua_unlock(L); } @@ -621,7 +623,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); - setthvalue(L, s2v(L->top), L); + setthvalue(L, s2v(L->top.p), L); api_incr_top(L); lua_unlock(L); return (G(L)->mainthread == L); @@ -638,16 +640,16 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); + setobj2s(L, L->top.p, slot); api_incr_top(L); } else { - setsvalue2s(L, L->top, str); + setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); } lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -674,13 +676,13 @@ LUA_API int lua_gettable (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { - setobj2s(L, L->top - 1, slot); + if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) { + setobj2s(L, L->top.p - 1, slot); } else - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -696,27 +698,27 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); t = index2value(L, idx); if (luaV_fastgeti(L, t, n, slot)) { - setobj2s(L, L->top, slot); + setobj2s(L, L->top.p, slot); } else { TValue aux; setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top, slot); + luaV_finishget(L, t, &aux, L->top.p, slot); } api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } l_sinline int finishrawget (lua_State *L, const TValue *val) { if (isempty(val)) /* avoid copying empty items to the stack */ - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); else - setobj2s(L, L->top, val); + setobj2s(L, L->top.p, val); api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -733,8 +735,8 @@ LUA_API int lua_rawget (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - val = luaH_get(t, s2v(L->top - 1)); - L->top--; /* remove key */ + val = luaH_get(t, s2v(L->top.p - 1)); + L->top.p--; /* remove key */ return finishrawget(L, val); } @@ -761,7 +763,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); - sethvalue2s(L, L->top, t); + sethvalue2s(L, L->top.p, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); @@ -788,7 +790,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { break; } if (mt != NULL) { - sethvalue2s(L, L->top, mt); + sethvalue2s(L, L->top.p, mt); api_incr_top(L); res = 1; } @@ -804,12 +806,12 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (n <= 0 || n > uvalue(o)->nuvalue) { - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); t = LUA_TNONE; } else { - setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); - t = ttype(s2v(L->top)); + setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); + t = ttype(s2v(L->top.p)); } api_incr_top(L); lua_unlock(L); @@ -829,14 +831,14 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { TString *str = luaS_new(L, k); api_checknelems(L, 1); if (luaV_fastset(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - L->top--; /* pop value */ + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); + L->top.p--; /* pop value */ } else { - setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ + setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); - L->top -= 2; /* pop value and key */ + luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); + L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ } @@ -856,12 +858,12 @@ LUA_API void lua_settable (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 2); t = index2value(L, idx); - if (luaV_fastset(L, t, s2v(L->top - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + if (luaV_fastset(L, t, s2v(L->top.p - 2), slot, luaH_get)) { + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); } else - luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); - L->top -= 2; /* pop index and value */ + luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); + L->top.p -= 2; /* pop index and value */ lua_unlock(L); } @@ -879,14 +881,14 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { api_checknelems(L, 1); t = index2value(L, idx); if (luaV_fastseti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); } else { TValue aux; setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); + luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); } - L->top--; /* pop value */ + L->top.p--; /* pop value */ lua_unlock(L); } @@ -896,16 +898,16 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { lua_lock(L); api_checknelems(L, n); t = gettable(L, idx); - luaH_set(L, t, key, s2v(L->top - 1)); + luaH_set(L, t, key, s2v(L->top.p - 1)); invalidateTMcache(t); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top -= n; + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p -= n; lua_unlock(L); } LUA_API void lua_rawset (lua_State *L, int idx) { - aux_rawset(L, idx, s2v(L->top - 2), 2); + aux_rawset(L, idx, s2v(L->top.p - 2), 2); } @@ -921,9 +923,9 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - luaH_setint(L, t, n, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; + luaH_setint(L, t, n, s2v(L->top.p - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p--; lua_unlock(L); } @@ -934,11 +936,11 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { lua_lock(L); api_checknelems(L, 1); obj = index2value(L, objindex); - if (ttisnil(s2v(L->top - 1))) + if (ttisnil(s2v(L->top.p - 1))) mt = NULL; else { - api_check(L, ttistable(s2v(L->top - 1)), "table expected"); - mt = hvalue(s2v(L->top - 1)); + api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); + mt = hvalue(s2v(L->top.p - 1)); } switch (ttype(obj)) { case LUA_TTABLE: { @@ -964,7 +966,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { break; } } - L->top--; + L->top.p--; lua_unlock(L); return 1; } @@ -980,11 +982,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ else { - setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); res = 1; } - L->top--; + L->top.p--; lua_unlock(L); return res; } @@ -996,7 +998,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + api_check(L, (nr) == LUA_MULTRET \ + || (L->ci->top.p - L->top.p >= (nr) - (na)), \ "results from function overflow current stack size") @@ -1009,7 +1012,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); - func = L->top - (nargs+1); + func = L->top.p - (nargs+1); if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ @@ -1057,7 +1060,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } - c.func = L->top - (nargs+1); /* function to be called */ + c.func = L->top.p - (nargs+1); /* function to be called */ if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); @@ -1087,7 +1090,7 @@ static void set_env (lua_State *L, LClosure *f) { /* get global table from registry */ const TValue *gt = getGtable(L); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); + setobj(L, f->upvals[0]->v.p, gt); luaC_barrier(L, f->upvals[0], gt); } } @@ -1101,7 +1104,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, luaZ_init(L, &z, reader, data); status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ + LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ set_env(L,f); } lua_unlock(L); @@ -1114,7 +1117,7 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { api_check(L, isshared(f->p), "Not a shared proto"); lua_lock(L); cl = luaF_newLclosure(L,f->nupvalues); - setclLvalue2s(L,L->top,cl); + setclLvalue2s(L,L->top.p,cl); api_incr_top(L); cl->p = f->p; luaF_initupvals(L, cl); @@ -1145,7 +1148,7 @@ LUA_API void lua_clonetable(lua_State *L, const void * tp) { luaG_runerror(L, "Not a shared table"); lua_lock(L); - sethvalue2s(L, L->top, t); + sethvalue2s(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } @@ -1155,7 +1158,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { TValue *o; lua_lock(L); api_checknelems(L, 1); - o = s2v(L->top - 1); + o = s2v(L->top.p - 1); if (isLfunction(o)) status = luaU_dump(L, getproto(o), writer, data, strip); else @@ -1281,7 +1284,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { LUA_API int lua_error (lua_State *L) { TValue *errobj; lua_lock(L); - errobj = s2v(L->top - 1); + errobj = s2v(L->top.p - 1); api_checknelems(L, 1); /* error object is the memory error message? */ if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) @@ -1299,12 +1302,12 @@ LUA_API int lua_next (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - more = luaH_next(L, t, L->top - 1); + more = luaH_next(L, t, L->top.p - 1); if (more) { api_incr_top(L); } else /* no more elements */ - L->top -= 1; /* remove key */ + L->top.p -= 1; /* remove key */ lua_unlock(L); return more; } @@ -1316,7 +1319,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { lua_lock(L); o = index2stack(L, idx); nresults = L->ci->nresults; - api_check(L, L->tbclist < o, "given index below or equal a marked one"); + api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ @@ -1331,7 +1334,7 @@ LUA_API void lua_concat (lua_State *L, int n) { if (n > 0) luaV_concat(L, n); else { /* nothing to concatenate */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */ + setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } luaC_checkGC(L); @@ -1343,7 +1346,7 @@ LUA_API void lua_len (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); - luaV_objlen(L, L->top, t); + luaV_objlen(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } @@ -1388,7 +1391,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { lua_lock(L); api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); u = luaS_newudata(L, size, nuvalue); - setuvalue(L, s2v(L->top), u); + setuvalue(L, s2v(L->top.p), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -1414,7 +1417,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, Proto *p = f->p; if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) return NULL; /* 'n' not in [1, p->sizeupvalues] */ - *val = f->upvals[n-1]->v; + *val = f->upvals[n-1]->v.p; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; return (name == NULL) ? "(no name)" : getstr(name); @@ -1430,7 +1433,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { lua_lock(L); name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { - setobj2s(L, L->top, val); + setobj2s(L, L->top.p, val); api_incr_top(L); } lua_unlock(L); @@ -1448,8 +1451,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { api_checknelems(L, 1); name = aux_upvalue(fi, n, &val, &owner); if (name) { - L->top--; - setobj(L, val, s2v(L->top)); + L->top.p--; + setobj(L, val, s2v(L->top.p)); luaC_barrier(L, owner, val); } lua_unlock(L); diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index 9e99cc448..a742427cd 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -12,23 +12,26 @@ #include "lstate.h" -/* Increments 'L->top', checking for stack overflows */ -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} +/* Increments 'L->top.p', checking for stack overflows */ +#define api_incr_top(L) {L->top.p++; \ + api_check(L, L->top.p <= L->ci->top.p, \ + "stack overflow");} /* ** If a call returns too many multiple returns, the callee may not have ** stack space to accommodate all results. In this case, this macro -** increases its stack space ('L->ci->top'). +** increases its stack space ('L->ci->top.p'). */ #define adjustresults(L,nres) \ - { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \ + L->ci->top.p = L->top.p; } /* Ensure the stack has at least 'n' elements */ -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") +#define api_checknelems(L,n) \ + api_check(L, (n) < (L->top.p - L->ci->func.p), \ + "not enough elements in the stack") /* diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 7e674f198..b922d9358 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -526,14 +526,14 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. (The test for "double is not big enough" also gets the -** case when the multiplication by 2 overflows.) +** bytes. (The test for "not big enough" also gets the case when the +** computation of 'newsize' overflows.) */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = B->size * 2; /* double buffer size */ + size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz) /* double is not big enough? */ + if (newsize < B->n + sz) /* not big enough? */ newsize = B->n + sz; return newsize; } diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index 785a1e81a..40b880b14 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -76,7 +76,7 @@ static int luaB_auxwrap (lua_State *L) { if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ - stat = lua_resetthread(co); /* close its tbc variables */ + stat = lua_resetthread(co, L); /* close its tbc variables */ lua_assert(stat != LUA_OK); lua_xmove(co, L, 1); /* move error message to the caller */ } @@ -172,7 +172,7 @@ static int luaB_close (lua_State *L) { int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co); + status = lua_resetthread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index fa15eaf68..3fae5cf25 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -182,10 +182,10 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func))->p->is_vararg) { + if (clLvalue(s2v(ci->func.p))->p->is_vararg) { int nextra = ci->u.l.nextraargs; if (n >= -nextra) { /* 'n' is negative */ - *pos = ci->func - nextra - (n + 1); + *pos = ci->func.p - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } @@ -194,7 +194,7 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) { const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ @@ -203,7 +203,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; + StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p; if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ /* generic name for any valid slot */ name = isLua(ci) ? "(temporary)" : "(C temporary)"; @@ -221,16 +221,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ + if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); + name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, L->top, pos); + setobjs2s(L, L->top.p, pos); api_incr_top(L); } } @@ -245,8 +245,8 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { lua_lock(L); name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ + setobjs2s(L, pos, L->top.p - 1); + L->top.p--; /* pop value */ } lua_unlock(L); return name; @@ -289,7 +289,7 @@ static int nextline (const Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { if (noLuaClosure(f)) { - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { @@ -298,7 +298,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue2s(L, L->top, t); /* push it on stack */ + sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ if (!p->is_vararg) /* regular function? */ @@ -388,20 +388,20 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { lua_lock(L); if (*what == '>') { ci = NULL; - func = s2v(L->top - 1); + func = s2v(L->top.p - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ - L->top--; /* pop function */ + L->top.p--; /* pop function */ } else { ci = ar->i_ci; - func = s2v(ci->func); + func = s2v(ci->func.p); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { - setobj2s(L, L->top, func); + setobj2s(L, L->top.p, func); api_incr_top(L); } if (strchr(what, 'L')) @@ -663,7 +663,7 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, */ static int isinstack (CallInfo *ci, const TValue *o) { StkId pos; - for (pos = ci->func + 1; pos < ci->top; pos++) { + for (pos = ci->func.p + 1; pos < ci->top.p; pos++) { if (o == s2v(pos)) return 1; } @@ -681,7 +681,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, LClosure *c = ci_func(ci); int i; for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { + if (c->upvals[i]->v.p == o) { *name = upvalname(c->p, i); return "upvalue"; } @@ -710,7 +710,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(ci, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func + 1)), &name); + cast_int(cast(StkId, o) - (ci->func.p + 1)), &name); } return formatvarinfo(L, kind, name); } @@ -807,10 +807,10 @@ l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); lua_assert(ttisfunction(s2v(errfunc))); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top - 2, 1); /* call it */ + setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */ + setobjs2s(L, L->top.p - 1, errfunc); /* push function */ + L->top.p++; /* assume EXTRA_STACK */ + luaD_callnoyield(L, L->top.p - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); } @@ -826,8 +826,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_end(argp); if (isLua(ci)) { /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); - setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */ - L->top--; + setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ + L->top.p--; } luaG_errormsg(L); } @@ -872,7 +872,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) { ** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' ** at most causes an extra call to a line hook.) ** This function is not "Protected" when called, so it should correct -** 'L->top' before calling anything that can run the GC. +** 'L->top.p' before calling anything that can run the GC. */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; @@ -895,7 +895,7 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { return 1; /* do not call hook again (VM yielded, so it did not move) */ } if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ - L->top = ci->top; /* correct top */ + L->top.p = ci->top.p; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 974960e99..2c3074c61 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -15,7 +15,7 @@ /* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) +#define ci_func(ci) (clLvalue(s2v((ci)->func.p))) #define resethookcount(L) (L->hookcount = L->basehookcount) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 419b3db93..c30cde76f 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -104,11 +104,11 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { } default: { lua_assert(errorstatus(errcode)); /* real error */ - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ + setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */ break; } } - L->top = oldtop + 1; + L->top.p = oldtop + 1; } @@ -121,7 +121,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { global_State *g = G(L); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ @@ -157,16 +157,38 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { + + +/* +** Change all pointers to the stack into offsets. +*/ +static void relstack (lua_State *L) { CallInfo *ci; UpVal *up; - L->top = (L->top - oldstack) + newstack; - L->tbclist = (L->tbclist - oldstack) + newstack; + L->top.offset = savestack(L, L->top.p); + L->tbclist.offset = savestack(L, L->tbclist.p); for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = s2v((uplevel(up) - oldstack) + newstack); + up->v.offset = savestack(L, uplevel(up)); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + newstack; - ci->func = (ci->func - oldstack) + newstack; + ci->top.offset = savestack(L, ci->top.p); + ci->func.offset = savestack(L, ci->func.p); + } +} + + +/* +** Change back all offsets into pointers. +*/ +static void correctstack (lua_State *L) { + CallInfo *ci; + UpVal *up; + L->top.p = restorestack(L, L->top.offset); + L->tbclist.p = restorestack(L, L->tbclist.offset); + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v.p = s2v(restorestack(L, up->v.offset)); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top.p = restorestack(L, ci->top.offset); + ci->func.p = restorestack(L, ci->func.offset); if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } @@ -176,38 +198,39 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { /* some space for error handling */ #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - /* -** Reallocate the stack to a new size, correcting all pointers into -** it. (There are pointers to a stack from its upvalues, from its list -** of call infos, plus a few individual pointers.) The reallocation is -** done in two steps (allocation + free) because the correction must be -** done while both addresses (the old stack and the new one) are valid. -** (In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior.) +** Reallocate the stack to a new size, correcting all pointers into it. +** In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior. So, before the reallocation, all pointers are +** changed to offsets, and after the reallocation they are changed back +** to pointers. As during the reallocation the pointers are invalid, the +** reallocation cannot run emergency collections. +** ** In case of allocation error, raise an error or return false according ** to 'raiseerror'. */ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int oldsize = stacksize(L); int i; - StkId newstack = luaM_reallocvector(L, NULL, 0, - newsize + EXTRA_STACK, StackValue); + StkId newstack; + int oldgcstop = G(L)->gcstopem; lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); + relstack(L); /* change pointers to offsets */ + G(L)->gcstopem = 1; /* stop emergency collection */ + newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK, + newsize + EXTRA_STACK, StackValue); + G(L)->gcstopem = oldgcstop; /* restore emergency collection */ if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ + correctstack(L); /* change offsets back to pointers */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } - /* number of elements to be copied to the new stack */ - i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; - memcpy(newstack, L->stack, i * sizeof(StackValue)); - for (; i < newsize + EXTRA_STACK; i++) + L->stack.p = newstack; + correctstack(L); /* change offsets back to pointers */ + L->stack_last.p = L->stack.p + newsize; + for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) setnilvalue(s2v(newstack + i)); /* erase new segment */ - correctstack(L, L->stack, newstack); - luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); - L->stack = newstack; - L->stack_last = L->stack + newsize; return 1; } @@ -229,7 +252,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { } else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ - int needed = cast_int(L->top - L->stack) + n; + int needed = cast_int(L->top.p - L->stack.p) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ @@ -253,12 +276,12 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { static int stackinuse (lua_State *L) { CallInfo *ci; int res; - StkId lim = L->top; + StkId lim = L->top.p; for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; + if (lim < ci->top.p) lim = ci->top.p; } - lua_assert(lim <= L->stack_last + EXTRA_STACK); - res = cast_int(lim - L->stack) + 1; /* part of stack in use */ + lua_assert(lim <= L->stack_last.p + EXTRA_STACK); + res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */ if (res < LUA_MINSTACK) res = LUA_MINSTACK; /* ensure a minimum size */ return res; @@ -295,7 +318,7 @@ void luaD_shrinkstack (lua_State *L) { void luaD_inctop (lua_State *L) { luaD_checkstack(L, 1); - L->top++; + L->top.p++; } /* }================================================================== */ @@ -312,8 +335,8 @@ void luaD_hook (lua_State *L, int event, int line, if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ - ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ + ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; @@ -323,11 +346,11 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ftransfer = ftransfer; ci->u2.transferinfo.ntransfer = ntransfer; } - if (isLua(ci) && L->top < ci->top) - L->top = ci->top; /* protect entire activation register */ + if (isLua(ci) && L->top.p < ci->top.p) + L->top.p = ci->top.p; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (ci->top < L->top + LUA_MINSTACK) - ci->top = L->top + LUA_MINSTACK; + if (ci->top.p < L->top.p + LUA_MINSTACK) + ci->top.p = L->top.p + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= mask; lua_unlock(L); @@ -335,8 +358,8 @@ void luaD_hook (lua_State *L, int event, int line, lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); + ci->top.p = restorestack(L, ci_top); + L->top.p = restorestack(L, top); ci->callstatus &= ~mask; } } @@ -367,7 +390,7 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { */ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ - StkId firstres = L->top - nres; /* index of first result */ + StkId firstres = L->top.p - nres; /* index of first result */ int delta = 0; /* correction for vararg functions */ int ftransfer; if (isLua(ci)) { @@ -375,10 +398,10 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; } - ci->func += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func); + ci->func.p += delta; /* if vararg, back to virtual 'func' */ + ftransfer = cast(unsigned short, firstres - ci->func.p); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ - ci->func -= delta; + ci->func.p -= delta; } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ @@ -397,9 +420,9 @@ StkId luaD_tryfuncTM (lua_State *L, StkId func) { tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ - for (p = L->top; p > func; p--) /* open space for metamethod */ + for (p = L->top.p; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); - L->top++; /* stack space pre-allocated by the caller */ + L->top.p++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ return func; } @@ -416,14 +439,14 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { int i; switch (wanted) { /* handle typical cases separately */ case 0: /* no values needed */ - L->top = res; + L->top.p = res; return; case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else /* at least one result */ - setobjs2s(L, res, L->top - nres); /* move it to proper place */ - L->top = res + 1; + setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ + L->top.p = res + 1; return; case LUA_MULTRET: wanted = nres; /* we want all results */ @@ -446,14 +469,14 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { break; } /* generic case */ - firstresult = L->top - nres; /* index of first result */ + firstresult = L->top.p - nres; /* index of first result */ if (nres > wanted) /* extra results? */ nres = wanted; /* don't need them */ 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(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ + L->top.p = res + wanted; /* top points after the last result */ } @@ -468,7 +491,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func, nres, wanted); + moveresults(L, ci->func.p, nres, wanted); /* function cannot be in any of these cases when returning */ lua_assert(!(ci->callstatus & (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); @@ -483,10 +506,10 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, int mask, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ - ci->func = func; + ci->func.p = func; ci->nresults = nret; ci->callstatus = mask; - ci->top = top; + ci->top.p = top; return ci; } @@ -500,10 +523,10 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults, CallInfo *ci; checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, - L->top + LUA_MINSTACK); - lua_assert(ci->top <= L->stack_last); + L->top.p + LUA_MINSTACK); + lua_assert(ci->top.p <= L->stack_last.p); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top - func) - 1; + int narg = cast_int(L->top.p - func) - 1; luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); } lua_unlock(L); @@ -535,17 +558,17 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int nfixparams = p->numparams; int i; checkstackGCp(L, fsize - delta, func); - ci->func -= delta; /* restore 'func' (if vararg) */ + ci->func.p -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - func = ci->func; /* moved-down function */ + setobjs2s(L, ci->func.p + i, func + i); + func = ci->func.p; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); + ci->top.p = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top.p <= L->stack_last.p); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ + L->top.p = func + narg1; /* set top */ return -1; } default: { /* not a function */ @@ -578,15 +601,15 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; - int narg = cast_int(L->top - func) - 1; /* number of real arguments */ + int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ for (; narg < nfixparams; narg++) - setnilvalue(s2v(L->top++)); /* complete missing arguments */ - lua_assert(ci->top <= L->stack_last); + setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ + lua_assert(ci->top.p <= L->stack_last.p); return ci; } default: { /* not a function */ @@ -748,8 +771,8 @@ static CallInfo *findpcall (lua_State *L) { ** coroutine error handler and should not kill the coroutine.) */ 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 */ + L->top.p -= narg; /* remove args from the stack */ + setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ api_incr_top(L); lua_unlock(L); return LUA_ERRRUN; @@ -765,7 +788,7 @@ static int resume_error (lua_State *L, const char *msg, int narg) { */ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ - StkId firstArg = L->top - n; /* first argument */ + StkId firstArg = L->top.p - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ @@ -773,7 +796,7 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) { /* yielded inside a hook? */ - L->top = firstArg; /* discard arguments */ + L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ } else { /* 'common' yield */ @@ -816,7 +839,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int 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->top - (L->ci->func + 1) == nargs) /* no function? */ + else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */ return resume_error(L, "cannot resume dead coroutine", nargs); } else if (L->status != LUA_YIELD) /* ended with errors? */ @@ -834,11 +857,11 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->top); /* push error message */ - L->ci->top = L->top; + luaD_seterrorobj(L, status, L->top.p); /* push error message */ + L->ci->top.p = L->top.p; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : cast_int(L->top - (L->ci->func + 1)); + : cast_int(L->top.p - (L->ci->func.p + 1)); lua_unlock(L); return status; } @@ -993,7 +1016,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; p.dyd.label.arr = NULL; p.dyd.label.size = 0; luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); luaZ_freebuffer(L, &p.buff); luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 4661aa007..1aa446ad0 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -8,6 +8,7 @@ #define ldo_h +#include "llimits.h" #include "lobject.h" #include "lstate.h" #include "lzio.h" @@ -23,7 +24,7 @@ ** at every check. */ #define luaD_checkstackaux(L,n,pre,pos) \ - if (l_unlikely(L->stack_last - L->top <= (n))) \ + if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ else { condmovestack(L,pre,pos); } @@ -32,8 +33,8 @@ -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) +#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p)) +#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n)) /* macro to check stack size, preserving 'p' */ diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 17ff8aef8..f6d631270 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -51,8 +51,8 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { for (i = 0; i < cl->nupvalues; i++) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); - uv->v = &uv->u.value; /* make it closed */ - setnilvalue(uv->v); + uv->v.p = &uv->u.value; /* make it closed */ + setnilvalue(uv->v.p); cl->upvals[i] = uv; luaC_objbarrier(L, cl, uv); } @@ -63,12 +63,11 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { ** Create a new upvalue at the given level, and link it to the list of ** open upvalues of 'L' after entry 'prev'. **/ -static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { +static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; - uv->v = s2v(level); /* current value lives in the stack */ - uv->tbc = tbc; + uv->v.p = s2v(level); /* current value lives in the stack */ uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; if (next) @@ -97,7 +96,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { pp = &p->u.open.next; } /* not found: create a new upvalue after 'pp' */ - return newupval(L, 0, level, pp); + return newupval(L, level, pp); } @@ -107,12 +106,12 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { ** (This function assumes EXTRA_STACK.) */ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { - StkId top = L->top; + StkId top = L->top.p; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ - L->top = top + 3; /* add function and arguments */ + L->top.p = top + 3; /* add function and arguments */ if (yy) luaD_call(L, top, 0); else @@ -127,7 +126,7 @@ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { static void checkclosemth (lua_State *L, StkId level) { const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); if (ttisnil(tm)) { /* no metamethod? */ - int idx = cast_int(level - L->ci->func); /* variable index */ + int idx = cast_int(level - L->ci->func.p); /* variable index */ const char *vname = luaG_findlocal(L, L->ci, idx, NULL); if (vname == NULL) vname = "?"; luaG_runerror(L, "variable '%s' got a non-closable value", vname); @@ -161,23 +160,23 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { ** is used.) */ #define MAXDELTA \ - ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) + ((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1) /* ** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { - lua_assert(level > L->tbclist); + lua_assert(level > L->tbclist.p); if (l_isfalse(s2v(level))) return; /* false doesn't need to be closed */ checkclosemth(L, level); /* value must have a close method */ - while (cast_uint(level - L->tbclist) > MAXDELTA) { - L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ - L->tbclist->tbclist.delta = 0; + while (cast_uint(level - L->tbclist.p) > MAXDELTA) { + L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */ + L->tbclist.p->tbclist.delta = 0; } - level->tbclist.delta = cast(unsigned short, level - L->tbclist); - L->tbclist = level; + level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); + L->tbclist.p = level; } @@ -197,10 +196,10 @@ void luaF_closeupval (lua_State *L, StkId level) { StkId upl; /* stack index pointed by 'uv' */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ - lua_assert(uplevel(uv) < L->top); + lua_assert(uplevel(uv) < L->top.p); luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ - setobj(L, slot, uv->v); /* move value to upvalue slot */ - uv->v = slot; /* now current value lives here */ + setobj(L, slot, uv->v.p); /* move value to upvalue slot */ + uv->v.p = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); @@ -213,12 +212,12 @@ void luaF_closeupval (lua_State *L, StkId level) { ** Remove first element from the tbclist plus its dummy nodes. */ static void poptbclist (lua_State *L) { - StkId tbc = L->tbclist; + StkId tbc = L->tbclist.p; lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ tbc -= tbc->tbclist.delta; - while (tbc > L->stack && tbc->tbclist.delta == 0) + while (tbc > L->stack.p && tbc->tbclist.delta == 0) tbc -= MAXDELTA; /* remove dummy nodes */ - L->tbclist = tbc; + L->tbclist.p = tbc; } @@ -229,8 +228,8 @@ static void poptbclist (lua_State *L) { StkId luaF_close (lua_State *L, StkId level, int status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ - while (L->tbclist >= level) { /* traverse tbc's down to that level */ - StkId tbc = L->tbclist; /* get variable index */ + while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ + StkId tbc = L->tbclist.p; /* get variable index */ poptbclist(L); /* remove it from list */ prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 6e7b3ec37..a8819140b 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -29,10 +29,10 @@ #define MAXUPVAL 255 -#define upisopen(up) ((up)->v != &(up)->u.value) +#define upisopen(up) ((up)->v.p != &(up)->u.value) -#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) +#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p)) /* diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 116fd070a..7ae8ce976 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -252,12 +252,13 @@ void luaC_fix (lua_State *L, GCObject *o) { /* -** create a new collectable object (with given type and size) and link -** it to 'allgc' list. +** create a new collectable object (with given type, size, and offset) +** and link it to 'allgc' list. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { +GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { global_State *g = G(L); - GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); + char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); + GCObject *o = cast(GCObject *, p + offset); o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -265,6 +266,11 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { return o; } + +GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { + return luaC_newobjdt(L, tt, sz, 0); +} + /* }====================================================== */ @@ -301,7 +307,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { set2gray(uv); /* open upvalues are kept gray */ else set2black(uv); /* closed upvalues are visited here */ - markvalue(g, uv->v); /* mark its content */ + markvalue(g, uv->v.p); /* mark its content */ break; } case LUA_VUSERDATA: { @@ -376,7 +382,7 @@ static int remarkupvals (global_State *g) { work++; if (!iswhite(uv)) { /* upvalue already visited? */ lua_assert(upisopen(uv) && isgray(uv)); - markvalue(g, uv->v); /* mark its value */ + markvalue(g, uv->v.p); /* mark its value */ } } } @@ -620,19 +626,19 @@ static int traverseLclosure (global_State *g, LClosure *cl) { */ static int traversethread (global_State *g, lua_State *th) { UpVal *uv; - StkId o = th->stack; + StkId o = th->stack.p; if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ + for (; o < th->top.p; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last + EXTRA_STACK; o++) + for (; o < th->stack_last.p + EXTRA_STACK; o++) setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -894,7 +900,7 @@ static GCObject *udata2finalize (global_State *g) { static void dothecall (lua_State *L, void *ud) { UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); + luaD_callnoyield(L, L->top.p - 2, 0); } @@ -911,16 +917,16 @@ static void GCTM (lua_State *L) { int oldgcstp = g->gcstp; g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ - setobj2s(L, L->top++, tm); /* push finalizer... */ - setobj2s(L, L->top++, &v); /* ... and its argument */ + setobj2s(L, L->top.p++, tm); /* push finalizer... */ + setobj2s(L, L->top.p++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); + status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc"); - L->top--; /* pops error object */ + L->top.p--; /* pops error object */ } } } diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 275bad3b1..294cc3a02 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -195,6 +195,8 @@ LUAI_FUNC void luaC_step (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); +LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, + size_t offset); 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_checkfinalizer (lua_State *L, GCObject *o, Table *mt); diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index e99151787..b0dc0acc2 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -138,12 +138,12 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { if (!ttisnil(o)) /* string already present? */ ts = keystrval(nodefromval(o)); /* get saved copy */ else { /* not in use yet */ - TValue *stv = s2v(L->top++); /* reserve stack space for string */ + TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ setsvalue(L, stv, ts); /* temporarily anchor the string */ luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); - L->top--; /* remove string from stack */ + L->top.p--; /* remove string from stack */ } return ts; } diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index a2c006098..f73ffc6d9 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -62,7 +62,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, 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_OPSHR: return luaV_shiftr(v1, v2); case LUA_OPUNM: return intop(-, 0, v1); case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; @@ -413,8 +413,8 @@ typedef struct BuffFS { */ static void pushstr (BuffFS *buff, const char *str, size_t lstr) { lua_State *L = buff->L; - setsvalue2s(L, L->top, luaS_newlstr(L, str, lstr)); - L->top++; /* may use one slot from EXTRA_STACK */ + setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr)); + L->top.p++; /* may use one slot from EXTRA_STACK */ if (!buff->pushed) /* no previous string on the stack? */ buff->pushed = 1; /* now there is one */ else /* join previous string with new one */ @@ -542,7 +542,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(&buff); /* empty buffer into the stack */ lua_assert(buff.pushed == 1); - return svalue(s2v(L->top - 1)); + return svalue(s2v(L->top.p - 1)); } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index d56de0b72..aeaa9d563 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -157,6 +157,17 @@ typedef union StackValue { /* index to stack elements */ typedef StackValue *StkId; + +/* +** When reallocating the stack, change all pointers to the stack into +** proper offsets. +*/ +typedef union { + StkId p; /* actual pointer */ + ptrdiff_t offset; /* used while the stack is being reallocated */ +} StkIdRel; + + /* convert a 'StackValue' to a 'TValue' */ #define s2v(o) (&(o)->val) @@ -618,8 +629,10 @@ typedef struct Proto { */ typedef struct UpVal { CommonHeader; - lu_byte tbc; /* true if it represents a to-be-closed variable */ - TValue *v; /* points to stack or to its own value */ + union { + TValue *p; /* points to stack or to its own value */ + ptrdiff_t offset; /* used while the stack is being reallocated */ + } v; union { struct { /* (when open) */ struct UpVal *next; /* linked list */ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 3e20d622b..854dcf691 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -260,9 +260,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { res = d; } else { - /* unsigned avoids overflow when lua_Integer has 32 bits */ - if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta - : (lua_Integer)INT_MIN + delta <= res)) + if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index fe693b571..24668c248 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1944,10 +1944,10 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ + setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top, lexstate.h); /* anchor it */ + sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); @@ -1961,7 +1961,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 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); - L->top--; /* remove scanner's table */ + L->top.p--; /* remove scanner's table */ return cl; /* closure is on the stack, too */ } diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 365bcdd6b..c1e6e531e 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -180,33 +180,33 @@ LUAI_FUNC void luaE_incCstack (lua_State *L) { static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - L1->tbclist = L1->stack; + L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); + L1->tbclist.p = L1->stack.p; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) - setnilvalue(s2v(L1->stack + i)); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + BASIC_STACK_SIZE; + setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ + L1->top.p = L1->stack.p; + L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; ci->callstatus = CIST_C; - ci->func = L1->top; + ci->func.p = L1->top.p; ci->u.c.k = NULL; ci->nresults = 0; - setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ - L1->top++; - ci->top = L1->top + LUA_MINSTACK; + setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ + L1->top.p++; + ci->top.p = L1->top.p + LUA_MINSTACK; L1->ci = ci; } static void freestack (lua_State *L) { - if (L->stack == NULL) + if (L->stack.p == NULL) 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, stacksize(L) + EXTRA_STACK); /* free stack */ + luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ } @@ -248,7 +248,7 @@ static void f_luaopen (lua_State *L, void *ud) { */ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; - L->stack = NULL; + L->stack.p = NULL; L->ci = NULL; L->nci = 0; L->twups = L; /* thread has no upvalues */ @@ -284,20 +284,16 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g; + global_State *g = G(L); + GCObject *o; lua_State *L1; lua_lock(L); - g = G(L); luaC_checkGC(L); /* create new thread */ - L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; - L1->marked = luaC_white(g); - L1->tt = LUA_VTHREAD; - /* link it on list 'allgc' */ - L1->next = g->allgc; - g->allgc = obj2gco(L1); + o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); + L1 = gco2th(o); /* anchor it on L stack */ - setthvalue2s(L, L->top, L1); + setthvalue2s(L, L->top.p, L1); api_incr_top(L); preinit_thread(L1, g); L1->hookmask = L->hookmask; @@ -316,7 +312,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_closeupval(L1, L1->stack); /* close all upvalues */ + luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -326,26 +322,27 @@ void luaE_freethread (lua_State *L, lua_State *L1) { int luaE_resetthread (lua_State *L, int status) { CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ - setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ - ci->func = L->stack; + setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ + ci->func.p = L->stack.p; ci->callstatus = CIST_C; if (status == LUA_YIELD) status = LUA_OK; L->status = LUA_OK; /* so it can run __close metamethods */ status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ - luaD_seterrorobj(L, status, L->stack + 1); + luaD_seterrorobj(L, status, L->stack.p + 1); else - L->top = L->stack + 1; - ci->top = L->top + LUA_MINSTACK; - luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); + L->top.p = L->stack.p + 1; + ci->top.p = L->top.p + LUA_MINSTACK; + luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); return status; } -LUA_API int lua_resetthread (lua_State *L) { +LUA_API int lua_resetthread (lua_State *L, lua_State *from) { int status; lua_lock(L); + L->nCcalls = (from) ? getCcalls(from) : 0; status = luaE_resetthread(L, L->status); lua_unlock(L); return status; @@ -425,7 +422,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { ** Generate a warning from an error message */ void luaE_warnerror (lua_State *L, const char *where) { - TValue *errobj = s2v(L->top - 1); /* error object */ + TValue *errobj = s2v(L->top.p - 1); /* error object */ const char *msg = (ttisstring(errobj)) ? svalue(errobj) : "error object is not a string"; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index b18568e90..c413f2efa 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -139,7 +139,7 @@ struct lua_longjmp; /* defined in ldo.c */ #define BASIC_STACK_SIZE (2*LUA_MINSTACK) -#define stacksize(th) cast_int((th)->stack_last - (th)->stack) +#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p) /* kinds of Garbage Collection */ @@ -170,8 +170,8 @@ typedef struct stringtable { ** before the function starts or after it ends. */ typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ + StkIdRel func; /* function index in the stack */ + StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ @@ -305,13 +305,13 @@ struct lua_State { lu_byte status; lu_byte allowhook; unsigned short nci; /* number of items in 'ci' list */ - StkId top; /* first free slot in the stack */ + StkIdRel top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - StkId stack_last; /* end of stack (last element + 1) */ - StkId stack; /* stack base */ + StkIdRel stack_last; /* end of stack (last element + 1) */ + StkIdRel stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ - StkId tbclist; /* list of to-be-closed variables */ + StkIdRel tbclist; /* list of to-be-closed variables */ GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 045155ea0..7af02a612 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -107,7 +107,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT}; */ static Node *hashint (const Table *t, lua_Integer i) { lua_Unsigned ui = l_castS2U(i); - if (ui <= (unsigned int)INT_MAX) + if (ui <= cast_uint(INT_MAX)) return hashmod(t, cast_int(ui)); else return hashmod(t, ui); @@ -490,7 +490,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); - for (i = 0; i < (int)size; i++) { + for (i = 0; i < cast_int(size); i++) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); @@ -985,6 +985,4 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); } -int luaH_isdummy (const Table *t) { return isdummy(t); } - #endif diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 7bbbcb213..75dd9e26e 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -59,7 +59,6 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (const Table *t); #endif diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index 868d78fd8..e6bc4d04a 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -93,7 +93,7 @@ static int tremove (lua_State *L) { lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ /* check whether 'pos' is in [1, size + 1] */ - luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, + luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2, "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { diff --git a/3rd/lua/ltests.c b/3rd/lua/ltests.c index 97834e380..4a0a6af1f 100644 --- a/3rd/lua/ltests.c +++ b/3rd/lua/ltests.c @@ -44,7 +44,7 @@ void *l_Trick = 0; -#define obj_at(L,k) s2v(L->ci->func + (k)) +#define obj_at(L,k) s2v(L->ci->func.p + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); @@ -57,7 +57,7 @@ static void setnameval (lua_State *L, const char *name, int val) { static void pushobject (lua_State *L, const TValue *o) { - setobj2s(L, L->top, o); + setobj2s(L, L->top.p, o); api_incr_top(L); } @@ -419,7 +419,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { if (uv) { checkobjrefN(g, clgc, uv); if (!upisopen(uv)) - checkvalref(g, obj2gco(uv), uv->v); + checkvalref(g, obj2gco(uv), uv->v.p); } } } @@ -428,7 +428,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { static int lua_checkpc (CallInfo *ci) { if (!isLua(ci)) return 1; else { - StkId f = ci->func; + StkId f = ci->func.p; Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; @@ -441,19 +441,19 @@ static void checkstack (global_State *g, lua_State *L1) { CallInfo *ci; UpVal *uv; assert(!isdead(g, L1)); - if (L1->stack == NULL) { /* incomplete thread? */ + if (L1->stack.p == NULL) { /* incomplete thread? */ assert(L1->openupval == NULL && L1->ci == NULL); return; } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) assert(upisopen(uv)); /* must be open */ - assert(L1->top <= L1->stack_last); - assert(L1->tbclist <= L1->top); + assert(L1->top.p <= L1->stack_last.p); + assert(L1->tbclist.p <= L1->top.p); for (ci = L1->ci; ci != NULL; ci = ci->previous) { - assert(ci->top <= L1->stack_last); + assert(ci->top.p <= L1->stack_last.p); assert(lua_checkpc(ci)); } - for (o = L1->stack; o < L1->stack_last; o++) + for (o = L1->stack.p; o < L1->stack_last.p; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } @@ -465,7 +465,7 @@ static void checkrefs (global_State *g, GCObject *o) { break; } case LUA_VUPVAL: { - checkvalref(g, o, gco2upv(o)->v); + checkvalref(g, o, gco2upv(o)->v.p); break; } case LUA_VTABLE: { @@ -533,7 +533,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, static lu_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ - ((void)g); /* better to keep it available if we need to print an object */ + cast_void(g); /* better to keep it if we need to print an object */ while (o) { assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); assert(!testbit(o->marked, TESTBIT)); @@ -980,7 +980,7 @@ static int hash_query (lua_State *L) { static int stacklevel (lua_State *L) { unsigned long a = 0; - lua_pushinteger(L, (L->top - L->stack)); + lua_pushinteger(L, (L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); lua_pushinteger(L, L->nCcalls); lua_pushinteger(L, L->nci); @@ -1040,7 +1040,7 @@ static int string_query (lua_State *L) { TString *ts; int n = 0; for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) { - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); n++; } @@ -1055,7 +1055,7 @@ static int tref (lua_State *L) { luaL_checkany(L, 1); lua_pushvalue(L, 1); lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } @@ -1063,7 +1063,7 @@ static int tref (lua_State *L) { static int getref (lua_State *L) { int level = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } @@ -1071,7 +1071,7 @@ static int getref (lua_State *L) { static int unref (lua_State *L) { int level = lua_gettop(L); luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; } @@ -1533,7 +1533,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_newthread(L1); } else if EQ("resetthread") { - lua_pushinteger(L1, lua_resetthread(L1)); + lua_pushinteger(L1, lua_resetthread(L1, L)); } else if EQ("newuserdata") { lua_newuserdata(L1, getnum); @@ -1740,7 +1740,7 @@ static struct X { int x; } x; else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); const char *s1 = lua_pushstring(L1, s); - (void)s1; /* to avoid warnings */ + cast_void(s1); /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } else if EQ("Ltolstring") { diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index b657b783a..07a060811 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -102,12 +102,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { - StkId func = L->top; + StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ setobj2s(L, func + 3, p3); /* 3rd argument */ - L->top = func + 4; + L->top.p = func + 4; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 0); @@ -119,18 +119,18 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); - StkId func = L->top; + StkId func = L->top.p; 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; + L->top.p += 3; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); res = restorestack(L, result); - setobjs2s(L, res, --L->top); /* move result to its place */ + setobjs2s(L, res, --L->top.p); /* move result to its place */ } @@ -165,7 +165,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_tryconcatTM (lua_State *L) { - StkId top = L->top; + StkId top = L->top.p; if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT))) luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); @@ -200,15 +200,15 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, */ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ - return !l_isfalse(s2v(L->top)); + if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */ + return !l_isfalse(s2v(L->top.p)); #if defined(LUA_COMPAT_LT_LE) else if (event == TM_LE) { /* try '!(p2 < p1)' for '(p1 <= p2)' */ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - if (callbinTM(L, p2, p1, L->top, TM_LT)) { + if (callbinTM(L, p2, p1, L->top.p, TM_LT)) { L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top)); + return l_isfalse(s2v(L->top.p)); } /* else error will remove this 'ci'; no need to clear mark */ } @@ -238,20 +238,20 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, const Proto *p) { int i; - int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ + int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; luaD_checkstack(L, p->maxstacksize + 1); /* copy function to the top of the stack */ - setobjs2s(L, L->top++, ci->func); + setobjs2s(L, L->top.p++, ci->func.p); /* move fixed parameters to the top of the stack */ for (i = 1; i <= nfixparams; i++) { - setobjs2s(L, L->top++, ci->func + i); - setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ + setobjs2s(L, L->top.p++, ci->func.p + i); + setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - ci->func += actual + 1; - ci->top += actual + 1; - lua_assert(L->top <= ci->top && ci->top <= L->stack_last); + ci->func.p += actual + 1; + ci->top.p += actual + 1; + lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } @@ -261,10 +261,10 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { if (wanted < 0) { wanted = nextra; /* get all extra arguments available */ checkstackGCp(L, nextra, where); /* ensure stack space */ - L->top = where + nextra; /* next instruction will need top */ + L->top.p = where + nextra; /* next instruction will need top */ } for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func - nextra + i); + setobjs2s(L, where + i, ci->func.p - nextra + i); for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 219e23283..de07c99cc 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -153,7 +153,7 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L); +LUA_API int (lua_resetthread) (lua_State *L, lua_State *from); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c deleted file mode 100644 index f6db9cf65..000000000 --- a/3rd/lua/luac.c +++ /dev/null @@ -1,725 +0,0 @@ -/* -** $Id: luac.c $ -** 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 - -#include "lua.h" -#include "lauxlib.h" - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lopnames.h" -#include "lstate.h" -#include "lundump.h" - -static void PrintFunction(const Proto* f, int full); -#define luaU_print PrintFunction - -#define PROGNAME "luac" /* default program name */ -#define OUTPUT PROGNAME ".out" /* default output file */ - -static int listing=0; /* list bytecodes? */ -static int dumping=1; /* dump bytecodes? */ -static int stripping=0; /* strip debug information? */ -static char Output[]={ OUTPUT }; /* default output file name */ -static const char* output=Output; /* actual output file name */ -static const char* progname=PROGNAME; /* actual program name */ -static TString **tmname; - -static void fatal(const char* message) -{ - fprintf(stderr,"%s: %s\n",progname,message); - exit(EXIT_FAILURE); -} - -static void cannot(const char* what) -{ - fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); - exit(EXIT_FAILURE); -} - -static void usage(const char* message) -{ - if (*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 'name' (default is \"%s\")\n" - " -p parse only\n" - " -s strip debug information\n" - " -v show version information\n" - " -- stop handling options\n" - " - stop handling options and process stdin\n" - ,progname,Output); - exit(EXIT_FAILURE); -} - -#define IS(s) (strcmp(argv[i],s)==0) - -static int doargs(int argc, char* argv[]) -{ - int i; - int version=0; - if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; - for (i=1; itop+(i))) - -static const Proto* combine(lua_State* L, int n) -{ - if (n==1) - return toproto(L,-1); - else - { - Proto* f; - int i=n; - if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); - f=toproto(L,-1); - for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; - } - luaM_freearray(L,f->lineinfo,f->sizelineinfo); - f->sizelineinfo=0; - return f; - } -} - -static int writer(lua_State* L, const void* p, size_t size, void* u) -{ - UNUSED(L); - return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); -} - -static int pmain(lua_State* L) -{ - int argc=(int)lua_tointeger(L,1); - char** argv=(char**)lua_touserdata(L,2); - const Proto* f; - int i; - tmname=G(L)->tmname; - if (!lua_checkstack(L,argc)) fatal("too many input files"); - for (i=0; i1); - if (dumping) - { - FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); - if (D==NULL) cannot("open"); - lua_lock(L); - luaU_dump(L,f,writer,D,stripping); - lua_unlock(L); - if (ferror(D)) cannot("write"); - if (fclose(D)) cannot("close"); - } - return 0; -} - -int main(int argc, char* argv[]) -{ - lua_State* L; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=luaL_newstate(); - if (L==NULL) fatal("cannot create state: not enough memory"); - lua_pushcfunction(L,&pmain); - lua_pushinteger(L,argc); - lua_pushlightuserdata(L,argv); - if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} - -/* -** print bytecodes -*/ - -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") -#define VOID(p) ((const void*)(p)) -#define eventname(i) (getstr(tmname[i])) - -static void PrintString(const TString* ts) -{ - const char* s=getstr(ts); - size_t i,n=tsslen(ts); - printf("\""); - for (i=0; ik[i]; - switch (ttypetag(o)) - { - case LUA_VNIL: - printf("N"); - break; - case LUA_VFALSE: - case LUA_VTRUE: - printf("B"); - break; - case LUA_VNUMFLT: - printf("F"); - break; - case LUA_VNUMINT: - printf("I"); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - printf("S"); - break; - default: /* cannot happen */ - printf("?%d",ttypetag(o)); - break; - } - printf("\t"); -} - -static void PrintConstant(const Proto* f, int i) -{ - const TValue* o=&f->k[i]; - switch (ttypetag(o)) - { - case LUA_VNIL: - printf("nil"); - break; - case LUA_VFALSE: - printf("false"); - break; - case LUA_VTRUE: - printf("true"); - break; - case LUA_VNUMFLT: - { - char buff[100]; - sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); - printf("%s",buff); - if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); - break; - } - case LUA_VNUMINT: - printf(LUA_INTEGER_FMT,ivalue(o)); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - PrintString(tsvalue(o)); - break; - default: /* cannot happen */ - printf("?%d",ttypetag(o)); - break; - } -} - -#define COMMENT "\t; " -#define EXTRAARG GETARG_Ax(code[pc+1]) -#define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) -#define ISK (isk ? "k" : "") - -static void PrintCode(const Proto* f) -{ - const Instruction* code=f->code; - int pc,n=f->sizecode; - for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",opnames[o]); - switch (o) - { - case OP_MOVE: - printf("%d %d",a,b); - break; - case OP_LOADI: - printf("%d %d",a,sbx); - break; - case OP_LOADF: - printf("%d %d",a,sbx); - break; - case OP_LOADK: - printf("%d %d",a,bx); - printf(COMMENT); PrintConstant(f,bx); - break; - case OP_LOADKX: - printf("%d",a); - printf(COMMENT); PrintConstant(f,EXTRAARG); - break; - case OP_LOADFALSE: - printf("%d",a); - break; - case OP_LFALSESKIP: - printf("%d",a); - break; - case OP_LOADTRUE: - printf("%d",a); - break; - case OP_LOADNIL: - printf("%d %d",a,b); - printf(COMMENT "%d out",b+1); - break; - case OP_GETUPVAL: - printf("%d %d",a,b); - printf(COMMENT "%s",UPVALNAME(b)); - break; - case OP_SETUPVAL: - printf("%d %d",a,b); - printf(COMMENT "%s",UPVALNAME(b)); - break; - case OP_GETTABUP: - printf("%d %d %d",a,b,c); - printf(COMMENT "%s",UPVALNAME(b)); - printf(" "); PrintConstant(f,c); - break; - case OP_GETTABLE: - printf("%d %d %d",a,b,c); - break; - case OP_GETI: - printf("%d %d %d",a,b,c); - break; - case OP_GETFIELD: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SETTABUP: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT "%s",UPVALNAME(a)); - printf(" "); PrintConstant(f,b); - if (isk) { printf(" "); PrintConstant(f,c); } - break; - case OP_SETTABLE: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_SETI: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_SETFIELD: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT); PrintConstant(f,b); - if (isk) { printf(" "); PrintConstant(f,c); } - break; - case OP_NEWTABLE: - printf("%d %d %d",a,b,c); - printf(COMMENT "%d",c+EXTRAARGC); - break; - case OP_SELF: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_ADDI: - printf("%d %d %d",a,b,sc); - break; - case OP_ADDK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SUBK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_MULK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_MODK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_POWK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_DIVK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_IDIVK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BANDK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BORK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BXORK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SHRI: - printf("%d %d %d",a,b,sc); - break; - case OP_SHLI: - printf("%d %d %d",a,b,sc); - break; - case OP_ADD: - printf("%d %d %d",a,b,c); - break; - case OP_SUB: - printf("%d %d %d",a,b,c); - break; - case OP_MUL: - printf("%d %d %d",a,b,c); - break; - case OP_MOD: - printf("%d %d %d",a,b,c); - break; - case OP_POW: - printf("%d %d %d",a,b,c); - break; - case OP_DIV: - printf("%d %d %d",a,b,c); - break; - case OP_IDIV: - printf("%d %d %d",a,b,c); - break; - case OP_BAND: - printf("%d %d %d",a,b,c); - break; - case OP_BOR: - printf("%d %d %d",a,b,c); - break; - case OP_BXOR: - printf("%d %d %d",a,b,c); - break; - case OP_SHL: - printf("%d %d %d",a,b,c); - break; - case OP_SHR: - printf("%d %d %d",a,b,c); - break; - case OP_MMBIN: - printf("%d %d %d",a,b,c); - printf(COMMENT "%s",eventname(c)); - break; - case OP_MMBINI: - printf("%d %d %d %d",a,sb,c,isk); - printf(COMMENT "%s",eventname(c)); - if (isk) printf(" flip"); - break; - case OP_MMBINK: - printf("%d %d %d %d",a,b,c,isk); - printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); - if (isk) printf(" flip"); - break; - case OP_UNM: - printf("%d %d",a,b); - break; - case OP_BNOT: - printf("%d %d",a,b); - break; - case OP_NOT: - printf("%d %d",a,b); - break; - case OP_LEN: - printf("%d %d",a,b); - break; - case OP_CONCAT: - printf("%d %d",a,b); - break; - case OP_CLOSE: - printf("%d",a); - break; - case OP_TBC: - printf("%d",a); - break; - case OP_JMP: - printf("%d",GETARG_sJ(i)); - printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); - break; - case OP_EQ: - printf("%d %d %d",a,b,isk); - break; - case OP_LT: - printf("%d %d %d",a,b,isk); - break; - case OP_LE: - printf("%d %d %d",a,b,isk); - break; - case OP_EQK: - printf("%d %d %d",a,b,isk); - printf(COMMENT); PrintConstant(f,b); - break; - case OP_EQI: - printf("%d %d %d",a,sb,isk); - break; - case OP_LTI: - printf("%d %d %d",a,sb,isk); - break; - case OP_LEI: - printf("%d %d %d",a,sb,isk); - break; - case OP_GTI: - printf("%d %d %d",a,sb,isk); - break; - case OP_GEI: - printf("%d %d %d",a,sb,isk); - break; - case OP_TEST: - printf("%d %d",a,isk); - break; - case OP_TESTSET: - printf("%d %d %d",a,b,isk); - break; - case OP_CALL: - printf("%d %d %d",a,b,c); - printf(COMMENT); - if (b==0) printf("all in "); else printf("%d in ",b-1); - if (c==0) printf("all out"); else printf("%d out",c-1); - break; - case OP_TAILCALL: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT "%d in",b-1); - break; - case OP_RETURN: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT); - if (b==0) printf("all out"); else printf("%d out",b-1); - break; - case OP_RETURN0: - break; - case OP_RETURN1: - printf("%d",a); - break; - case OP_FORLOOP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc-bx+2); - break; - case OP_FORPREP: - printf("%d %d",a,bx); - printf(COMMENT "exit to %d",pc+bx+3); - break; - case OP_TFORPREP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc+bx+2); - break; - case OP_TFORCALL: - printf("%d %d",a,c); - break; - case OP_TFORLOOP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc-bx+2); - break; - case OP_SETLIST: - printf("%d %d %d",a,b,c); - if (isk) printf(COMMENT "%d",c+EXTRAARGC); - break; - case OP_CLOSURE: - printf("%d %d",a,bx); - printf(COMMENT "%p",VOID(f->p[bx])); - break; - case OP_VARARG: - printf("%d %d",a,c); - printf(COMMENT); - if (c==0) printf("all out"); else printf("%d out",c-1); - break; - case OP_VARARGPREP: - printf("%d",a); - break; - case OP_EXTRAARG: - printf("%d",ax); - break; -#if 0 - default: - printf("%d %d %d",a,b,c); - printf(COMMENT "not handled"); - break; -#endif - } - printf("\n"); - } -} - - -#define SS(x) ((x==1)?"":"s") -#define S(x) (int)(x),SS(x) - -static void PrintHeader(const Proto* f) -{ - const char* s=f->source ? getstr(f->source) : "=?"; - if (*s=='@' || *s=='=') - s++; - else if (*s==LUA_SIGNATURE[0]) - s="(bstring)"; - else - s="(string)"; - printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", - (f->linedefined==0)?"main":"function",s, - f->linedefined,f->lastlinedefined, - S(f->sizecode),VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", - (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), - S(f->maxstacksize),S(f->sizeupvalues)); - printf("%d local%s, %d constant%s, %d function%s\n", - S(f->sizelocvars),S(f->sizek),S(f->sizep)); -} - -static void PrintDebug(const Proto* f) -{ - int i,n; - n=f->sizek; - printf("constants (%d) for %p:\n",n,VOID(f)); - for (i=0; isizelocvars; - 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); - } - n=f->sizeupvalues; - printf("upvalues (%d) for %p:\n",n,VOID(f)); - for (i=0; iupvalues[i].instack,f->upvalues[i].idx); - } -} - -static void PrintFunction(const Proto* f, int full) -{ - 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 fcc0018b3..e4650fbce 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -747,14 +747,15 @@ /* @@ LUA_IDSIZE gives the maximum size for the description of the source -@@ of a function in debug information. +** of a function in debug information. ** CHANGE it if you want a different size. */ #define LUA_IDSIZE 60 /* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +@@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib +** buffer system. */ #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 5aa55c445..aba93f828 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -120,10 +120,10 @@ static TString *loadStringN (LoadState *S, Proto *p) { } else { /* long string */ ts = luaS_createlngstrobj(L, size); /* create string */ - setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ luaD_inctop(L); loadVector(S, getstr(ts), size); /* load directly in final place */ - L->top--; /* pop string */ + L->top.p--; /* pop string */ } luaC_objbarrier(L, p, ts); return ts; @@ -321,7 +321,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.Z = Z; checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); - setclLvalue2s(L, L->top, cl); + setclLvalue2s(L, L->top.p, cl); luaD_inctop(L); cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index e7bf098f6..3a5b9bc38 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -25,6 +25,9 @@ #define MAXUTF 0x7FFFFFFFu + +#define MSGInvalid "invalid UTF-8 code" + /* ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ @@ -35,7 +38,8 @@ typedef unsigned long utfint; #endif -#define iscont(p) ((*(p) & 0xC0) == 0x80) +#define iscont(c) (((c) & 0xC0) == 0x80) +#define iscontp(p) iscont(*(p)) /* from strlib */ @@ -65,7 +69,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { int count = 0; /* to count number of continuation bytes */ for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ unsigned int cc = (unsigned char)s[++count]; /* read next byte */ - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + if (!iscont(cc)) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } @@ -140,7 +144,7 @@ static int codepoint (lua_State *L) { utfint code; s = utf8_decode(s, &code, !lax); if (s == NULL) - return luaL_error(L, "invalid UTF-8 code"); + return luaL_error(L, MSGInvalid); lua_pushinteger(L, code); n++; } @@ -190,16 +194,16 @@ static int byteoffset (lua_State *L) { "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ - while (posi > 0 && iscont(s + posi)) posi--; + while (posi > 0 && iscontp(s + posi)) posi--; } else { - if (iscont(s + posi)) + if (iscontp(s + posi)) return 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)); + } while (posi > 0 && iscontp(s + posi)); n++; } } @@ -208,7 +212,7 @@ static int byteoffset (lua_State *L) { while (n > 0 && posi < (lua_Integer)len) { do { /* find beginning of next character */ posi++; - } while (iscont(s + posi)); /* (cannot pass final '\0') */ + } while (iscontp(s + posi)); /* (cannot pass final '\0') */ n--; } } @@ -226,15 +230,15 @@ static int iter_aux (lua_State *L, int strict) { const char *s = luaL_checklstring(L, 1, &len); lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); if (n < len) { - while (iscont(s + n)) n++; /* skip continuation bytes */ + while (iscontp(s + n)) n++; /* go to next character */ } if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { utfint code; const char *next = utf8_decode(s + n, &code, strict); - if (next == NULL) - return luaL_error(L, "invalid UTF-8 code"); + if (next == NULL || iscontp(next)) + return luaL_error(L, MSGInvalid); lua_pushinteger(L, n + 1); lua_pushinteger(L, code); return 2; @@ -253,7 +257,8 @@ static int iter_auxlax (lua_State *L) { static int iter_codes (lua_State *L) { int lax = lua_toboolean(L, 2); - luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 1); + luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index b92f2682b..8aab051e6 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -610,8 +610,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (tm == NULL) /* no TM? */ return 0; /* objects are different */ else { - luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ - return !l_isfalse(s2v(L->top)); + luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ + return !l_isfalse(s2v(L->top.p)); } } @@ -635,13 +635,13 @@ static void copy2buff (StkId top, int n, char *buff) { /* ** Main operation for concatenation: concat 'total' values in the stack, -** from 'L->top - total' up to 'L->top - 1'. +** from 'L->top.p - total' up to 'L->top.p - 1'. */ void luaV_concat (lua_State *L, int total) { if (total == 1) return; /* "all" values already concatenated */ do { - StkId top = L->top; + StkId top = L->top.p; int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) @@ -659,7 +659,7 @@ void luaV_concat (lua_State *L, int total) { for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { - L->top = top - total; /* pop strings to avoid wasting stack */ + L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); } tl += l; @@ -676,7 +676,7 @@ void luaV_concat (lua_State *L, int total) { setsvalue2s(L, top - n, ts); /* create result */ } total -= n - 1; /* got 'n' strings to create one new */ - L->top -= n - 1; /* popped 'n' strings and pushed one */ + L->top.p -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } @@ -767,12 +767,10 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* number of bits in an integer */ #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) + /* ** Shift left operation. (Shift right just negates 'y'.) */ -#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) - - lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; @@ -812,26 +810,26 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { - setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); + setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); break; } case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); + setobjs2s(L, base + GETARG_A(inst), --L->top.p); break; } case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: case OP_GTI: case OP_GEI: case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ - int res = !l_isfalse(s2v(L->top - 1)); - L->top--; + int res = !l_isfalse(s2v(L->top.p - 1)); + L->top.p--; #if defined(LUA_COMPAT_LT_LE) if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ ci->callstatus ^= CIST_LEQ; /* clear mark */ @@ -844,11 +842,11 @@ void luaV_finishOp (lua_State *L) { break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */ + StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ - L->top = top - 1; /* top is one after last element (at top-2) */ + L->top.p = top - 1; /* top is one after last element (at top-2) */ luaV_concat(L, total); /* concat them (may yield again) */ break; } @@ -860,7 +858,7 @@ void luaV_finishOp (lua_State *L) { StkId ra = base + GETARG_A(inst); /* adjust top to signal correct number of returns, in case the return is "up to top" ('isIT') */ - L->top = ra + ci->u2.nres; + L->top.p = ra + ci->u2.nres; /* repeat instruction to close other vars. and complete the return */ ci->u.l.savedpc--; break; @@ -1073,7 +1071,7 @@ void luaV_finishOp (lua_State *L) { #define updatetrap(ci) (trap = ci->u.l.trap) -#define updatebase(ci) (base = ci->func + 1) +#define updatebase(ci) (base = ci->func.p + 1) #define updatestack(ci) \ @@ -1108,7 +1106,7 @@ void luaV_finishOp (lua_State *L) { ** Whenever code can raise errors, the global 'pc' and the global ** 'top' must be correct to report occasional errors. */ -#define savestate(L,ci) (savepc(L), L->top = ci->top) +#define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) /* @@ -1128,7 +1126,7 @@ void luaV_finishOp (lua_State *L) { /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, (savepc(L), L->top = (c)), \ + { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } @@ -1159,7 +1157,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { startfunc: trap = L->hookmask; returning: /* trap already set */ - cl = clLvalue(s2v(ci->func)); + cl = clLvalue(s2v(ci->func.p)); k = cl->p->k; pc = ci->u.l.savedpc; if (l_unlikely(trap)) { @@ -1171,7 +1169,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } ci->u.l.trap = 1; /* assume trap is on, for now */ } - base = ci->func + 1; + base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { Instruction i; /* instruction being executed */ @@ -1180,10 +1178,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* low-level line tracing for debugging Lua */ printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); #endif - lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top <= L->stack_last); + lua_assert(base == ci->func.p + 1); + lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (cast_void(L->top = base), 1)); + lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { StkId ra = RA(i); @@ -1242,20 +1240,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_GETUPVAL) { StkId ra = RA(i); int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); + setobj2s(L, ra, cl->upvals[b]->v.p); vmbreak; } vmcase(OP_SETUPVAL) { StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, s2v(ra)); + setobj(L, uv->v.p, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { StkId ra = RA(i); const TValue *slot; - TValue *upval = cl->upvals[GETARG_B(i)]->v; + TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { @@ -1310,7 +1308,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SETTABUP) { const TValue *slot; - TValue *upval = cl->upvals[GETARG_A(i)]->v; + TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ @@ -1375,7 +1373,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) /* non-zero extra argument? */ c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ pc++; /* skip extra argument */ - L->top = ra + 1; /* correct top in case of emergency GC */ + L->top.p = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1582,9 +1580,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_CONCAT) { StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ - L->top = ra + n; /* mark the end of concat operands */ + L->top.p = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); - checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ + checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ vmbreak; } vmcase(OP_CLOSE) { @@ -1678,7 +1676,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ - L->top = ra + b; /* top signals number of arguments */ + L->top.p = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(L); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) @@ -1697,19 +1695,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) - L->top = ra + b; + L->top.p = ra + b; else /* previous instruction set top */ - b = cast_int(L->top - ra); + b = cast_int(L->top.p - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ - lua_assert(L->tbclist < base); /* no pending tbc variables */ - lua_assert(base == ci->func + 1); + lua_assert(L->tbclist.p < base); /* no pending tbc variables */ + lua_assert(base == ci->func.p + 1); } if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ goto startfunc; /* execute the callee */ else { /* C function? */ - ci->func -= delta; /* restore 'func' (if vararg) */ + ci->func.p -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ @@ -1720,19 +1718,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int n = GETARG_B(i) - 1; /* number of results */ int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ - n = cast_int(L->top - ra); /* get what is available */ + n = cast_int(L->top.p - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { /* may there be open upvalues? */ ci->u2.nres = n; /* save number of returns */ - if (L->top < ci->top) - L->top = ci->top; + if (L->top.p < ci->top.p) + L->top.p = ci->top.p; luaF_close(L, base, CLOSEKTOP, 1); updatetrap(ci); updatestack(ci); } if (nparams1) /* vararg function? */ - ci->func -= ci->u.l.nextraargs + nparams1; - L->top = ra + n; /* set call for 'luaD_poscall' */ + ci->func.p -= ci->u.l.nextraargs + nparams1; + L->top.p = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; @@ -1740,7 +1738,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN0) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); - L->top = ra; + L->top.p = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ trap = 1; @@ -1748,16 +1746,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { else { /* do the 'poscall' here */ int nres; L->ci = ci->previous; /* back to caller */ - L->top = base - 1; + L->top.p = base - 1; for (nres = ci->nresults; l_unlikely(nres > 0); nres--) - setnilvalue(s2v(L->top++)); /* all results are nil */ + setnilvalue(s2v(L->top.p++)); /* all results are nil */ } goto ret; } vmcase(OP_RETURN1) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); - L->top = ra + 1; + L->top.p = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ trap = 1; @@ -1766,13 +1764,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ if (nres == 0) - L->top = base - 1; /* asked for no results */ + L->top.p = base - 1; /* asked for no results */ else { StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ - L->top = base; + L->top.p = base; for (; l_unlikely(nres > 1); nres--) - setnilvalue(s2v(L->top++)); /* complete missing results */ + setnilvalue(s2v(L->top.p++)); /* complete missing results */ } } ret: /* return from a Lua function */ @@ -1828,7 +1826,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { */ /* push function, state, and control variable */ memcpy(ra + 4, ra, 3 * sizeof(*ra)); - L->top = ra + 4 + 3; + L->top.p = ra + 4 + 3; ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ @@ -1850,9 +1848,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { unsigned int last = GETARG_C(i); Table *h = hvalue(s2v(ra)); if (n == 0) - n = cast_int(L->top - ra) - 1; /* get up to the top */ + n = cast_int(L->top.p - ra) - 1; /* get up to the top */ else - L->top = ci->top; /* correct top in case of emergency GC */ + L->top.p = ci->top.p; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { last += GETARG_Ax(*pc) * (MAXARG_C + 1); diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index a7ab2c195..c44244b7b 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -112,6 +112,11 @@ typedef enum { luaC_barrierback(L, gcvalue(t), v); } +/* +** Shift right is the same as shift left with a negative 'y' +*/ +#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) + LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); diff --git a/3rd/lua/makefile b/3rd/lua/makefile index fd5caf14f..fd386f65c 100644 --- a/3rd/lua/makefile +++ b/3rd/lua/makefile @@ -44,7 +44,7 @@ LUAC_T= luac LUAC_O= luac.o ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) -ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) +ALL_T= $(LUA_A) $(LUA_T) ALL_A= $(LUA_A) # Targets start here. diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index d7871d532..8794def0e 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -233,7 +233,7 @@ static int luaB_auxwrap (lua_State *L) { if (r < 0) { int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) - lua_resetthread(co); /* close variables in case of errors */ + lua_resetthread(co, L); /* close variables in case of errors */ if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); From 98311b9b91e694bae6711f05bf6a757c7f9877c3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Nov 2022 10:46:35 +0800 Subject: [PATCH 471/565] release 1.6.0 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 0b10d5830..5fe4b1441 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.6.0 (2022-11-16) +----------- +* Update Lua to 5.4.4 (github Nov 8, 2022) +* Update jemalloc to 5.3.0 +* Update lpeg to 1.0.2 (For sproto) +* Update mongo driver to support the newest wire protocol +* socket.listen()/cluster.open() returns ip address and port +* Add service.close() + v1.5.0 (2021-11-9) ----------- * Update Lua to 5.4.3 From 58b1bda0d94b131044fcb6838f660decb1902467 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Nov 2022 19:26:16 +0800 Subject: [PATCH 472/565] first key can't can be in hash part, see #1484 --- lualib-src/lua-bson.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 28414d51a..29d6c70c5 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -519,7 +519,10 @@ is_rawarray(lua_State *L) { } lua_Integer firstkey = lua_isinteger(L, -2) ? lua_tointeger(L, -2) : 0; lua_pop(L, 2); - return firstkey > 0; + if (firstkey <= 1) { + return firstkey > 0; + } + return firstkey <= lua_rawlen(L, -1); } static void From cc562547a54f00fde96ffa9fc379dc97e62130d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Fri, 6 Jan 2023 11:23:22 +0800 Subject: [PATCH 473/565] ignore .vscode (#1689) Co-authored-by: Faker2 --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 402b1488a..8c4288a9d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ *.so *.dSYM .DS_Store +.vscode \ No newline at end of file From 39f3ddbc1041f043ab741c08863b610d49c5a2ec Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 29 Jan 2023 11:08:14 +0800 Subject: [PATCH 474/565] update lua --- 3rd/lua/lgc.c | 9 ++++-- 3rd/lua/lgc.h | 17 ++++++------ 3rd/lua/lmathlib.c | 10 +++---- 3rd/lua/lmem.c | 68 ++++++++++++++++++++++++++++------------------ 3rd/lua/loslib.c | 18 +++++++++++- 3rd/lua/lstate.h | 11 ++++++-- 3rd/lua/ltm.h | 5 ++-- 3rd/lua/lua.c | 4 ++- 3rd/lua/lua.h | 16 +++++++---- 9 files changed, 102 insertions(+), 56 deletions(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 7ae8ce976..fe376fe6b 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1687,12 +1687,15 @@ static void incstep (lua_State *L, global_State *g) { } /* -** performs a basic GC step if collector is running +** Performs a basic GC step if collector is running. (If collector is +** not running, set a reasonable debt to avoid it being called at +** every single check.) */ void luaC_step (lua_State *L) { global_State *g = G(L); - lua_assert(!g->gcemergency); - if (gcrunning(g)) { /* running? */ + if (!gcrunning(g)) /* not running? */ + luaE_setdebt(g, -2000); + else { if(isdecGCmodegen(g)) genstep(L, g); else diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 294cc3a02..d6bb5de68 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -177,18 +177,19 @@ #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) -#define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && ispurewhite(gcvalue(v))) ? \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && ispurewhite(gcvalue(v))) ? \ - luaC_barrierback_(L,p) : cast_void(0)) - #define luaC_objbarrier(L,p,o) ( \ (isblack(p) && ispurewhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) +#define luaC_barrier(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) + +#define luaC_objbarrierback(L,p,o) ( \ + (isblack(p) && ispurewhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) + +#define luaC_barrierback(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0)) + 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); diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index e0c61a168..d0b1e1e5d 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -267,7 +267,7 @@ static int math_type (lua_State *L) { /* try to find an integer type with at least 64 bits */ -#if (ULONG_MAX >> 31 >> 31) >= 3 +#if ((ULONG_MAX >> 31) >> 31) >= 3 /* 'long' has at least 64 bits */ #define Rand64 unsigned long @@ -277,9 +277,9 @@ static int math_type (lua_State *L) { /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long -#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3 +#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 -/* 'lua_Integer' has at least 64 bits */ +/* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned #endif @@ -500,12 +500,12 @@ static lua_Number I2d (Rand64 x) { /* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { - return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); + return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l); } /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); + return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n); } #endif /* } */ diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index 9029d588c..9800a86fc 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -22,25 +22,6 @@ #include "lstate.h" -#if defined(EMERGENCYGCTESTS) -/* -** First allocation will fail whenever not building initial state. -** (This fail will trigger 'tryagain' and a full GC cycle at every -** allocation.) -*/ -static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (completestate(g) && ns > 0) /* frees never fail */ - return NULL; /* fail */ - else /* normal allocation */ - return (*g->frealloc)(g->ud, block, os, ns); -} -#else -#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) -#endif - - - - /* ** About the realloc function: @@ -60,6 +41,43 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { */ +/* +** Macro to call the allocation function. +*/ +#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) + + +/* +** When an allocation fails, it will try again after an emergency +** collection, except when it cannot run a collection. The GC should +** not be called while the state is not fully built, as the collector +** is not yet fully initialized. Also, it should not be called when +** 'gcstopem' is true, because then the interpreter is in the middle of +** a collection step. +*/ +#define cantryagain(g) (completestate(g) && !g->gcstopem) + + + + +#if defined(EMERGENCYGCTESTS) +/* +** First allocation will fail except when freeing a block (frees never +** fail) and when it cannot try again; this fail will trigger 'tryagain' +** and a full GC cycle at every allocation. +*/ +static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { + if (ns > 0 && cantryagain(g)) + return NULL; /* fail */ + else /* normal allocation */ + return callfrealloc(g, block, os, ns); +} +#else +#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) +#endif + + + /* @@ -132,7 +150,7 @@ l_noret luaM_toobig (lua_State *L) { void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); - (*g->frealloc)(g->ud, block, osize, 0); + callfrealloc(g, block, osize, 0); g->GCdebt -= osize; } @@ -140,19 +158,15 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* ** In case of allocation fail, this function will do an emergency ** collection to free some memory and then try the allocation again. -** The GC should not be called while state is not fully built, as the -** collector is not yet fully initialized. Also, it should not be called -** when 'gcstopem' is true, because then the interpreter is in the -** middle of a collection step. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (completestate(g) && !g->gcstopem) { + if (cantryagain(g)) { luaC_fullgc(L, 1); /* try to free some memory... */ - return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + return callfrealloc(g, block, osize, nsize); /* try again */ } - else return NULL; /* cannot free any memory without a full state */ + else return NULL; /* cannot run an emergency collection */ } diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 854dcf691..7eb05cafd 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -138,12 +138,28 @@ /* }================================================================== */ +/* +** Despite claiming to be ISO, the C library in some Apple platforms +** does not implement 'system'. +*/ +#if !defined(l_system) && defined(__APPLE__) /* { */ +#include "TargetConditionals.h" +#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV +#define l_system(cmd) ((cmd) == NULL ? 0 : -1) +#endif +#endif /* } */ + +#if !defined(l_system) +#define l_system(cmd) system(cmd) /* default definition */ +#endif + + static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); int stat; errno = 0; - stat = system(cmd); + stat = l_system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index c413f2efa..e2763073e 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -9,6 +9,11 @@ #include "lua.h" + +/* Some header files included here need this definition */ +typedef struct CallInfo CallInfo; + + #include "lobject.h" #include "ltm.h" #include "lzio.h" @@ -169,7 +174,7 @@ typedef struct stringtable { ** - field 'transferinfo' is used only during call/returnhooks, ** before the function starts or after it ends. */ -typedef struct CallInfo { +struct CallInfo { StkIdRel func; /* function index in the stack */ StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ @@ -196,7 +201,7 @@ typedef struct CallInfo { } u2; short nresults; /* expected number of results from this function */ unsigned short callstatus; -} CallInfo; +}; /* @@ -290,7 +295,7 @@ typedef struct global_State { struct lua_State *mainthread; TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ + struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 73b833c60..c309e2ae1 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -9,6 +9,7 @@ #include "lobject.h" +#include "lstate.h" /* @@ -95,8 +96,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, + CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted); diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 7f7dc2b22..715430a0d 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -633,7 +633,8 @@ static int pmain (lua_State *L) { } luaL_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ - lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ + lua_gc(L, LUA_GCRESTART); /* start GC... */ + lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ @@ -665,6 +666,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } + lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index de07c99cc..5aee8cefb 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -131,6 +131,16 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); +/* +** Type used by the debug API to collect debug information +*/ +typedef struct lua_Debug lua_Debug; + + +/* +** Functions to be called by the debugger in specific events +*/ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); /* @@ -446,12 +456,6 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx); #define LUA_MASKLINE (1 << LUA_HOOKLINE) #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); From f344a49755ef16f02595e289c30cfdb0616f61c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E5=A4=A7=E9=BE=99?= <6268385+dingdalong@users.noreply.github.com> Date: Fri, 17 Feb 2023 14:36:32 +0800 Subject: [PATCH 475/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dclient=5Fnumber?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E9=94=99=E8=AF=AF=20(#1704)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 75cd6e54e..ee4386376 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -90,6 +90,7 @@ function gateserver.start(handler) MSG.more = dispatch_queue function MSG.open(fd, msg) + client_number = client_number + 1 if client_number >= maxclient then socketdriver.shutdown(fd) return @@ -98,7 +99,6 @@ function gateserver.start(handler) socketdriver.nodelay(fd) end connection[fd] = true - client_number = client_number + 1 handler.connect(fd, msg) end From 6fe520a7cb260037b3a2bc68bd76c3fc49d87bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E5=A4=A7=E9=BE=99?= <6268385+dingdalong@users.noreply.github.com> Date: Sat, 18 Feb 2023 00:57:39 +0800 Subject: [PATCH 476/565] =?UTF-8?q?=20cluster.open=E6=B7=BB=E5=8A=A0maxcli?= =?UTF-8?q?ent=E5=8F=82=E6=95=B0=20(#1705)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/cluster.lua | 6 +++--- service/clusterd.lua | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index e3437b43b..025ea016f 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -75,11 +75,11 @@ function cluster.send(node, address, ...) end end -function cluster.open(port) +function cluster.open(port, maxclient) if type(port) == "string" then - return skynet.call(clusterd, "lua", "listen", port) + return skynet.call(clusterd, "lua", "listen", port, nil, maxclient) else - return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port, maxclient) end end diff --git a/service/clusterd.lua b/service/clusterd.lua index 20faedfa0..44729966a 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -142,17 +142,17 @@ function command.reload(source, config) skynet.ret(skynet.pack(nil)) end -function command.listen(source, addr, port) +function command.listen(source, addr, port, maxclient) local gate = skynet.newservice("gate") if port == nil then local address = assert(node_address[addr], addr .. " is down") addr, port = string.match(address, "(.+):([^:]+)$") port = tonumber(port) assert(port ~= 0) - skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.call(gate, "lua", "open", { address = addr, port = port, maxclient = maxclient }) skynet.ret(skynet.pack(addr, port)) else - local realaddr, realport = skynet.call(gate, "lua", "open", { address = addr, port = port }) + local realaddr, realport = skynet.call(gate, "lua", "open", { address = addr, port = port, maxclient = maxclient }) skynet.ret(skynet.pack(realaddr, realport)) end end From 1e45d4a9be6582b534a6afca5e7dd5ec5275fbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Wed, 15 Mar 2023 12:44:08 +0800 Subject: [PATCH 477/565] =?UTF-8?q?=E6=B7=BB=E5=8A=A0get=5Fprotocol?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20(#1712)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加get_protocol接口 * 添加_proto字段 --------- Co-authored-by: zixun --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 73a4d3e7b..107b5a37c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -45,6 +45,7 @@ local skynet = { -- code cache skynet.cache = require "skynet.codecache" +skynet._proto = proto function skynet.register_protocol(class) local name = class.name From b9a5cec19156f87b21321d462ffdc79acc5c36b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Tue, 21 Mar 2023 21:37:03 +0800 Subject: [PATCH 478/565] update sproto (#1720) Co-authored-by: zixun --- lualib-src/sproto/README.md | 43 ++++++++++++++++++++++++++++++++++--- lualib-src/sproto/lsproto.c | 18 +++++++++------- lualib-src/sproto/sproto.c | 25 +++++++++++++-------- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index e5011fc90..7a2ab9771 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -104,7 +104,7 @@ local sprotocore = require "sproto.core" -- optional * `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.parse(schema)` creates 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. @@ -217,7 +217,7 @@ Types * **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. -* **double** : double, floating-point number. +* **double** : double precision floating-point number, satisfy [the IEEE 754 standard](https://en.wikipedia.org/wiki/Double-precision_floating-point_format). * **boolean** : true or false You can add * before the typename to declare an array. @@ -230,7 +230,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. When you need decimal, you can specify the fixed-point presision. +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 precision. **NOTE** : `double` is supported now. @@ -263,6 +263,7 @@ For integer array, an additional byte (4 or 8) to indicate the value is 32bit or 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. +Notice more: all examples are tested in `test_wire_protocol.lua`, update `test_wire_protocol.lua` when update examples. ``` .Person { @@ -277,6 +278,9 @@ Notice: If the tag is not declared in schema, the decoder will simply ignore the bools 1 : *boolean number 2 : integer bignumber 3 : integer + double 4 : double + doubles 5 : *double + fpn 6 : integer(2) } ``` @@ -405,6 +409,39 @@ A0 86 01 00 (100000, 32bit integer) 00 1C F4 AB FD FF FF FF (-10000000000, 64bit integer) ``` +Example 7: +``` +data { + double = 0.01171875, + doubles = {0.01171875, 23, 4} +} + +03 00 (fn = 3) +07 00 (skip id = 3) +00 00 (id = 4, value in data part) +00 00 (id = 5, value in data part) + +08 00 00 00 (sizeof number, data part) +00 00 00 00 00 00 88 3f (0.01171875, 64bit double) + +19 00 00 00 (sizeof doubles) +08 (sizeof double) +00 00 00 00 00 00 88 3f (0.01171875, 64bit double) +00 00 00 00 00 00 37 40 (23, 64bit double) +00 00 00 00 00 00 10 40 (4, 64bit double) +``` + +Example 8: +``` +data { + fpn = 1.82, +} + +02 00 (fn = 2) +0b 00 (skip id = 5) +6e 01 (id = 6, value = 0x16e/2 - 1 = 182) +``` + 0 Packing ======= diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 605ec0c1f..74b5397ef 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -84,14 +84,16 @@ lua_seti(lua_State *L, int index, lua_Integer n) { #if defined(SPROTO_WEAK_TYPE) static int64_t tointegerx (lua_State *L, int idx, int *isnum) { - int64_t v; - if (lua_isnumber(L, idx)) { - v = (int64_t)(round(lua_tonumber(L, idx))); - if (isnum) *isnum = 1; - return v; - } else { - return lua_tointegerx(L, idx, isnum); + int _isnum = 0; + int64_t v = lua_tointegerx(L, idx, &_isnum); + if (!_isnum){ + double num = lua_tonumberx(L, idx, &_isnum); + if(_isnum) { + v = (int64_t)llround(num); + } } + if(isnum) *isnum = _isnum; + return v; } static int @@ -613,7 +615,7 @@ getbuffer(lua_State *L, int index, size_t *sz) { /* lightuserdata sproto_type string source / (lightuserdata , integer) - return table + return table, sz(decoded bytes) */ static int ldecode(lua_State *L) { diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 47d1527ba..edaf37f69 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -898,7 +898,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz buffer = encode_integer_array(cb,args,buffer,size, &noarray); if (buffer == NULL) return -1; - + if (noarray) { return 0; } @@ -1088,26 +1088,33 @@ expand64(uint32_t v) { return value; } +static int +decode_empty_array(sproto_callback cb, struct sproto_arg *args) { + // It's empty array, call cb with index == -1 to create the empty array. + args->index = -1; + args->value = NULL; + args->length = 0; + return cb(args); +} + static int 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; - } + return decode_empty_array(cb, args); + } stream += SIZEOF_LENGTH; switch (type) { case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { + if (--sz == 0) { + // An empty array but with a len prefix + return decode_empty_array(cb, args); + } int len = *stream; ++stream; - --sz; if (len == SIZEOF_INT32) { if (sz % SIZEOF_INT32 != 0) return -1; From 223bd599ba057b34a9e2be4d4d73ef2317e55b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Wed, 29 Mar 2023 10:42:31 +0800 Subject: [PATCH 479/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=20?= =?UTF-8?q?callback=E5=AF=BC=E8=87=B4=E7=9A=84crash=E9=97=AE=E9=A2=98=20(#?= =?UTF-8?q?1726)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix set callback in resolve _cb * 调整注释 * 使用预处理callback 来解决引用问题 * add cb_pre and forward_pre * delete precb_context * format tab --------- Co-authored-by: zixun --- lualib-src/lua-skynet.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index dc3765bb6..811a43920 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -94,25 +94,47 @@ forward_cb(struct skynet_context * context, void * ud, int type, int session, ui return 1; } +static void +clear_last_context(lua_State *L) { + if (lua_getfield(L, LUA_REGISTRYINDEX, "callback_context") == LUA_TUSERDATA) { + lua_pushnil(L); + lua_setiuservalue(L, -2, 2); + } + lua_pop(L, 1); +} + +static int +_cb_pre(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { + struct callback_context *cb_ctx = (struct callback_context *)ud; + clear_last_context(cb_ctx->L); + skynet_callback(context, ud, _cb); + return _cb(context, cb_ctx, type, session, source, msg, sz); +} + +static int +_forward_pre(struct skynet_context *context, void *ud, int type, int session, uint32_t source, const void *msg, size_t sz) { + struct callback_context *cb_ctx = (struct callback_context *)ud; + clear_last_context(cb_ctx->L); + skynet_callback(context, ud, forward_cb); + return forward_cb(context, cb_ctx, type, session, source, msg, sz); +} + static int 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); lua_settop(L,1); - struct callback_context *cb_ctx = (struct callback_context *)lua_newuserdata(L, sizeof(*cb_ctx)); + struct callback_context * cb_ctx = (struct callback_context *)lua_newuserdatauv(L, sizeof(*cb_ctx), 2); cb_ctx->L = lua_newthread(L); lua_pushcfunction(cb_ctx->L, traceback); - lua_setuservalue(L, -2); + lua_setiuservalue(L, -2, 1); + lua_getfield(L, LUA_REGISTRYINDEX, "callback_context"); + lua_setiuservalue(L, -2, 2); lua_setfield(L, LUA_REGISTRYINDEX, "callback_context"); lua_xmove(L, cb_ctx->L, 1); - if (forward) { - skynet_callback(context, cb_ctx, forward_cb); - } else { - skynet_callback(context, cb_ctx, _cb); - } - + skynet_callback(context, cb_ctx, (forward)?(_forward_pre):(_cb_pre)); return 0; } From aedfb9288dc4d25794b232d491242d1ab907766b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Mar 2023 16:25:49 +0800 Subject: [PATCH 480/565] See #1529 --- 3rd/lua/lauxlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index b922d9358..37c1f3f75 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1237,7 +1237,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lua_State * eL; int err; const void * oldv; - if (level == CACHE_OFF) { + if (level == CACHE_OFF || filename == NULL) { return luaL_loadfilex_(L, filename, mode); } proto = load_proto(filename); From d56cf80f95f7e2d49e635d0c226b39679ac582be Mon Sep 17 00:00:00 2001 From: twinklinggu <61661343+twinklinggu@users.noreply.github.com> Date: Fri, 14 Apr 2023 17:36:23 +0800 Subject: [PATCH 481/565] Fix socket not closed when clusteragent exit (#1741) Co-authored-by: twinklinggu --- service/clusteragent.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 3797c04f9..b0b8c2b8d 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -135,7 +135,7 @@ skynet.start(function() skynet.dispatch("lua", function(_,source, cmd, ...) if cmd == "exit" then - socket.close(fd) + socket.close_fd(fd) skynet.exit() elseif cmd == "namechange" then register_name = new_register_name() From 8b55660274d7684de761e8634f2b1b868c451807 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 May 2023 19:08:11 +0800 Subject: [PATCH 482/565] Update lua 5.4.6 rc1 --- 3rd/lua/lcode.c | 75 ++-- 3rd/lua/lcorolib.c | 4 +- 3rd/lua/ldebug.c | 31 +- 3rd/lua/ldo.c | 14 +- 3rd/lua/ldump.c | 8 +- 3rd/lua/llex.c | 2 +- 3rd/lua/llimits.h | 21 +- 3rd/lua/lopcodes.h | 2 +- 3rd/lua/loslib.c | 38 +- 3rd/lua/lparser.c | 8 +- 3rd/lua/lstate.c | 10 +- 3rd/lua/lstrlib.c | 2 +- 3rd/lua/ltable.c | 2 + 3rd/lua/lua.c | 2 +- 3rd/lua/lua.h | 11 +- 3rd/lua/luac.c | 723 ++++++++++++++++++++++++++++++++++++ 3rd/lua/luaconf.h | 6 + 3rd/lua/lundump.c | 2 + 3rd/lua/lvm.c | 4 + 3rd/lua/makefile | 9 +- service-src/service_snlua.c | 2 +- 21 files changed, 876 insertions(+), 100 deletions(-) create mode 100644 3rd/lua/luac.c diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 911dbd5f1..1a371ca94 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1351,6 +1351,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, } +/* +** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { + lua_assert(baser <= opr && + ((baser == OPR_ADD && opr <= OPR_SHR) || + (baser == OPR_LT && opr <= OPR_LE))); + return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); +} + + +/* +** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode unopr2op (UnOpr opr) { + return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + + cast_int(OP_UNM)); +} + + +/* +** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) +*/ +l_sinline TMS binopr2TM (BinOpr opr) { + lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); + return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); +} + + /* ** Emit code for unary expressions that "produce values" ** (everything but 'not'). @@ -1389,15 +1418,15 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, ** Emit code for binary expressions that "produce values" over ** two registers. */ -static void codebinexpval (FuncState *fs, OpCode op, +static void codebinexpval (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { + OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ /* 'e1' must be already in a register or it is a constant */ lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); - finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, - cast(TMS, (op - OP_ADD) + TM_ADD)); + finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); } @@ -1418,9 +1447,9 @@ static void codebini (FuncState *fs, OpCode op, */ static void codebinK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = cast(TMS, opr + TM_ADD); + TMS event = binopr2TM(opr); int v2 = e2->u.info; /* K index */ - OpCode op = cast(OpCode, opr + OP_ADDK); + OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); } @@ -1457,10 +1486,9 @@ static void swapexps (expdesc *e1, expdesc *e2) { */ static void codebinNoK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - OpCode op = cast(OpCode, opr + OP_ADD); if (flip) swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ + codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ } @@ -1490,7 +1518,7 @@ static void codecommutative (FuncState *fs, BinOpr op, flip = 1; } if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); + codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD); else codearith(fs, op, e1, e2, flip, line); } @@ -1518,25 +1546,27 @@ static void codebitwise (FuncState *fs, BinOpr opr, ** Emit code for order comparisons. When using an immediate operand, ** 'isfloat' tells whether the original value was a float. */ -static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { +static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; int im; int isfloat = 0; + OpCode op; if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); r2 = im; - op = cast(OpCode, (op - OP_LT) + OP_LTI); + op = binopr2op(opr, OPR_LT, OP_LTI); } else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = im; - op = (op == OP_LT) ? OP_GTI : OP_GEI; + op = binopr2op(opr, OPR_LT, OP_GTI); } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); + op = binopr2op(opr, OPR_LT, OP_LT); } freeexps(fs, e1, e2); e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); @@ -1579,16 +1609,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* ** Apply prefix operation 'op' to expression 'e'. */ -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { +void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; luaK_dischargevars(fs, e); - switch (op) { + switch (opr) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, op + LUA_OPUNM, e, &ef)) + if (constfolding(fs, opr + LUA_OPUNM, e, &ef)) break; /* else */ /* FALLTHROUGH */ case OPR_LEN: - codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); + codeunexpval(fs, unopr2op(opr), e, line); break; case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); @@ -1718,30 +1748,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr, /* coded as (r1 >> -I) */; } else /* regular case (two registers) */ - codebinexpval(fs, OP_SHL, e1, e2, line); + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_SHR: { if (isSCint(e2)) codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ else /* regular case (two registers) */ - codebinexpval(fs, OP_SHR, e1, e2, line); + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { codeeq(fs, opr, e1, e2); break; } - case OPR_LT: case OPR_LE: { - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - codeorder(fs, op, e1, e2); - break; - } case OPR_GT: case OPR_GE: { /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); swapexps(e1, e2); - codeorder(fs, op, e1, e2); + opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); + } /* FALLTHROUGH */ + case OPR_LT: case OPR_LE: { + codeorder(fs, opr, e1, e2); break; } default: lua_assert(0); diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index 40b880b14..c64adf08a 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -76,7 +76,7 @@ static int luaB_auxwrap (lua_State *L) { if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ - stat = lua_resetthread(co, L); /* close its tbc variables */ + stat = lua_closethread(co, L); /* close its tbc variables */ lua_assert(stat != LUA_OK); lua_xmove(co, L, 1); /* move error message to the caller */ } @@ -172,7 +172,7 @@ static int luaB_close (lua_State *L) { int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co, L); + status = lua_closethread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 3fae5cf25..28b1caabf 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -656,18 +656,19 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, /* -** Check whether pointer 'o' points to some value in the stack -** frame of the current function. Because 'o' may not point to a -** value in this stack, we cannot compare it with the region -** boundaries (undefined behaviour in ISO C). +** Check whether pointer 'o' points to some value in the stack frame of +** the current function and, if so, returns its index. Because 'o' may +** not point to a value in this stack, we cannot compare it with the +** region boundaries (undefined behavior in ISO C). */ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId pos; - for (pos = ci->func.p + 1; pos < ci->top.p; pos++) { - if (o == s2v(pos)) - return 1; +static int instack (CallInfo *ci, const TValue *o) { + int pos; + StkId base = ci->func.p + 1; + for (pos = 0; base + pos < ci->top.p; pos++) { + if (o == s2v(base + pos)) + return pos; } - return 0; /* not found */ + return -1; /* not found */ } @@ -708,9 +709,11 @@ static const char *varinfo (lua_State *L, const TValue *o) { const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func.p + 1)), &name); + if (!kind) { /* not an upvalue? */ + int reg = instack(ci, o); /* try a register */ + if (reg >= 0) /* is 'o' a register? */ + kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name); + } } return formatvarinfo(L, kind, name); } @@ -845,7 +848,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) { if (p->lineinfo == NULL) /* no debug information? */ return 0; if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ - int delta = 0; /* line diference */ + int delta = 0; /* line difference */ int pc = oldpc; for (;;) { int lineinfo = p->lineinfo[++pc]; diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index c30cde76f..2a0017ca6 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -299,17 +299,13 @@ static int stackinuse (lua_State *L) { */ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int nsize = inuse * 2; /* proposed new size */ - int max = inuse * 3; /* maximum "reasonable" size */ - if (max > LUAI_MAXSTACK) { - max = LUAI_MAXSTACK; /* respect stack limit */ - if (nsize > LUAI_MAXSTACK) - nsize = LUAI_MAXSTACK; - } + int max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3; /* if thread is currently not handling a stack overflow and its size is larger than maximum "reasonable" size, shrink it */ - if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) + if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) { + int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2; luaD_reallocstack(L, nsize, 0); /* ok if that fails */ + } else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ @@ -629,7 +625,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** check the stack before doing anything else. 'luaD_precall' already ** does that. */ -l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { +l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { CallInfo *ci; L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index f848b669c..f231691b7 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -55,8 +56,11 @@ static void dumpByte (DumpState *D, int y) { } -/* dumpInt Buff Size */ -#define DIBS ((sizeof(size_t) * 8 / 7) + 1) +/* +** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" +** rounds up the division.) +*/ +#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) static void dumpSize (DumpState *D, size_t x) { lu_byte buff[DIBS]; diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index b0dc0acc2..5fc39a5cd 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -128,7 +128,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { ** ensuring there is only one copy of each unique string. The table ** here is used as a set: the string enters as the key, while its value ** is irrelevant. We use the string itself as the value only because it -** is a TValue readly available. Later, the code generation can change +** is a TValue readily available. Later, the code generation can change ** this value. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 52a32f92e..1c826f7be 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -71,11 +71,24 @@ typedef signed char ls_byte; /* -** conversion of pointer to unsigned integer: -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value +** conversion of pointer to unsigned integer: this is for hashing only; +** there is no problem if the integer cannot hold the whole pointer +** value. (In strict ISO C this may cause undefined behavior, but no +** actual machine seems to bother.) */ -#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(UINTPTR_MAX) /* even in C99 this type is optional */ +#define L_P2I uintptr_t +#else /* no 'intptr'? */ +#define L_P2I uintmax_t /* use the largest available integer */ +#endif +#else /* C89 option */ +#define L_P2I size_t +#endif + +#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX)) diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 7c2745159..4c5514539 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -21,7 +21,7 @@ iABC C(8) | B(8) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | -isJ sJ(25) | Op(7) | +isJ sJ (signed)(25) | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 7eb05cafd..ad5a92768 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -30,23 +30,14 @@ */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ -/* 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" /* two-char options */ - -/* options for Windows */ -#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#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 -#elif defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ + "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ +#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */ +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" #else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 +#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ + "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ #endif #endif /* } */ @@ -138,21 +129,14 @@ /* }================================================================== */ -/* -** Despite claiming to be ISO, the C library in some Apple platforms -** does not implement 'system'. -*/ -#if !defined(l_system) && defined(__APPLE__) /* { */ -#include "TargetConditionals.h" -#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV -#define l_system(cmd) ((cmd) == NULL ? 0 : -1) -#endif -#endif /* } */ - #if !defined(l_system) +#if defined(LUA_USE_IOS) +/* Despite claiming to be ISO C, iOS does not implement 'system'. */ +#define l_system(cmd) ((cmd) == NULL ? 0 : -1) +#else #define l_system(cmd) system(cmd) /* default definition */ #endif - +#endif static int os_execute (lua_State *L) { diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 24668c248..b745f236f 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -521,12 +521,12 @@ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { /* ** Solves the goto at index 'g' to given 'label' and removes it -** from the list of pending goto's. +** from the list of pending gotos. ** If it jumps into the scope of some variable, raises an error. */ static void solvegoto (LexState *ls, int g, Labeldesc *label) { int i; - Labellist *gl = &ls->dyd->gt; /* list of goto's */ + Labellist *gl = &ls->dyd->gt; /* list of gotos */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ @@ -580,7 +580,7 @@ static int newgotoentry (LexState *ls, TString *name, int line, int pc) { /* ** Solves forward jumps. Check whether new label 'lb' matches any ** pending gotos in current block and solves them. Return true -** if any of the goto's need to close upvalues. +** if any of the gotos need to close upvalues. */ static int solvegotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; @@ -601,7 +601,7 @@ static int solvegotos (LexState *ls, Labeldesc *lb) { /* ** Create a new label with the given 'name' at the given 'line'. ** 'last' tells whether label is the last non-op statement in its -** block. Solves all pending goto's to this new label and adds +** block. Solves all pending gotos to this new label and adds ** a close instruction if necessary. ** Returns true iff it added a close instruction. */ diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index c1e6e531e..e8668c401 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -339,7 +339,7 @@ int luaE_resetthread (lua_State *L, int status) { } -LUA_API int lua_resetthread (lua_State *L, lua_State *from) { +LUA_API int lua_closethread (lua_State *L, lua_State *from) { int status; lua_lock(L); L->nCcalls = (from) ? getCcalls(from) : 0; @@ -349,6 +349,14 @@ LUA_API int lua_resetthread (lua_State *L, lua_State *from) { } +/* +** Deprecated! Use 'lua_closethread' instead. +*/ +LUA_API int lua_resetthread (lua_State *L) { + return lua_closethread(L, NULL); +} + + LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 0b4fdbb7b..03167161d 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -570,7 +570,7 @@ static const char *match_capture (MatchState *ms, const char *s, int l) { static const char *match (MatchState *ms, const char *s, const char *p) { if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ + init: /* using goto to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ switch (*p) { case '(': { /* start capture */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 7af02a612..2930ee0d4 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -259,9 +259,11 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) { size |= (size >> 2); size |= (size >> 4); size |= (size >> 8); +#if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ size |= (size >> 16); #if (UINT_MAX >> 30) > 3 size |= (size >> 32); /* unsigned int has more than 32 bits */ +#endif #endif size++; lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 715430a0d..0ff884545 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -666,7 +666,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } - lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */ + lua_gc(L, LUA_GCSTOP); /* stop GC while building state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 5aee8cefb..9ec1894dc 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,14 +18,14 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "5" +#define LUA_VERSION_RELEASE "6" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 6) #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-2022 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -163,7 +163,8 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L, lua_State *from); +LUA_API int (lua_closethread) (lua_State *L, lua_State *from); +LUA_API int (lua_resetthread) (lua_State *L); /* Deprecated! */ LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); @@ -500,7 +501,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2022 Lua.org, PUC-Rio. +* Copyright (C) 1994-2023 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 new file mode 100644 index 000000000..5f4a141bd --- /dev/null +++ b/3rd/lua/luac.c @@ -0,0 +1,723 @@ +/* +** $Id: luac.c $ +** 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 + +#include "lua.h" +#include "lauxlib.h" + +#include "ldebug.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lopnames.h" +#include "lstate.h" +#include "lundump.h" + +static void PrintFunction(const Proto* f, int full); +#define luaU_print PrintFunction + +#define PROGNAME "luac" /* default program name */ +#define OUTPUT PROGNAME ".out" /* default output file */ + +static int listing=0; /* list bytecodes? */ +static int dumping=1; /* dump bytecodes? */ +static int stripping=0; /* strip debug information? */ +static char Output[]={ OUTPUT }; /* default output file name */ +static const char* output=Output; /* actual output file name */ +static const char* progname=PROGNAME; /* actual program name */ +static TString **tmname; + +static void fatal(const char* message) +{ + fprintf(stderr,"%s: %s\n",progname,message); + exit(EXIT_FAILURE); +} + +static void cannot(const char* what) +{ + fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); + exit(EXIT_FAILURE); +} + +static void usage(const char* message) +{ + if (*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 'name' (default is \"%s\")\n" + " -p parse only\n" + " -s strip debug information\n" + " -v show version information\n" + " -- stop handling options\n" + " - stop handling options and process stdin\n" + ,progname,Output); + exit(EXIT_FAILURE); +} + +#define IS(s) (strcmp(argv[i],s)==0) + +static int doargs(int argc, char* argv[]) +{ + int i; + int version=0; + if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; + for (i=1; itop.p+(i))) + +static const Proto* combine(lua_State* L, int n) +{ + if (n==1) + return toproto(L,-1); + else + { + Proto* f; + int i=n; + if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); + f=toproto(L,-1); + for (i=0; ip[i]=toproto(L,i-n-1); + if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; + } + return f; + } +} + +static int writer(lua_State* L, const void* p, size_t size, void* u) +{ + UNUSED(L); + return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); +} + +static int pmain(lua_State* L) +{ + int argc=(int)lua_tointeger(L,1); + char** argv=(char**)lua_touserdata(L,2); + const Proto* f; + int i; + tmname=G(L)->tmname; + if (!lua_checkstack(L,argc)) fatal("too many input files"); + for (i=0; i1); + if (dumping) + { + FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); + if (D==NULL) cannot("open"); + lua_lock(L); + luaU_dump(L,f,writer,D,stripping); + lua_unlock(L); + if (ferror(D)) cannot("write"); + if (fclose(D)) cannot("close"); + } + return 0; +} + +int main(int argc, char* argv[]) +{ + lua_State* L; + int i=doargs(argc,argv); + argc-=i; argv+=i; + if (argc<=0) usage("no input files given"); + L=luaL_newstate(); + if (L==NULL) fatal("cannot create state: not enough memory"); + lua_pushcfunction(L,&pmain); + lua_pushinteger(L,argc); + lua_pushlightuserdata(L,argv); + if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); + lua_close(L); + return EXIT_SUCCESS; +} + +/* +** print bytecodes +*/ + +#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") +#define VOID(p) ((const void*)(p)) +#define eventname(i) (getstr(tmname[i])) + +static void PrintString(const TString* ts) +{ + const char* s=getstr(ts); + size_t i,n=tsslen(ts); + printf("\""); + for (i=0; ik[i]; + switch (ttypetag(o)) + { + case LUA_VNIL: + printf("N"); + break; + case LUA_VFALSE: + case LUA_VTRUE: + printf("B"); + break; + case LUA_VNUMFLT: + printf("F"); + break; + case LUA_VNUMINT: + printf("I"); + break; + case LUA_VSHRSTR: + case LUA_VLNGSTR: + printf("S"); + break; + default: /* cannot happen */ + printf("?%d",ttypetag(o)); + break; + } + printf("\t"); +} + +static void PrintConstant(const Proto* f, int i) +{ + const TValue* o=&f->k[i]; + switch (ttypetag(o)) + { + case LUA_VNIL: + printf("nil"); + break; + case LUA_VFALSE: + printf("false"); + break; + case LUA_VTRUE: + printf("true"); + break; + case LUA_VNUMFLT: + { + char buff[100]; + sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); + printf("%s",buff); + if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); + break; + } + case LUA_VNUMINT: + printf(LUA_INTEGER_FMT,ivalue(o)); + break; + case LUA_VSHRSTR: + case LUA_VLNGSTR: + PrintString(tsvalue(o)); + break; + default: /* cannot happen */ + printf("?%d",ttypetag(o)); + break; + } +} + +#define COMMENT "\t; " +#define EXTRAARG GETARG_Ax(code[pc+1]) +#define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) +#define ISK (isk ? "k" : "") + +static void PrintCode(const Proto* f) +{ + const Instruction* code=f->code; + int pc,n=f->sizecode; + for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); + printf("%-9s\t",opnames[o]); + switch (o) + { + case OP_MOVE: + printf("%d %d",a,b); + break; + case OP_LOADI: + printf("%d %d",a,sbx); + break; + case OP_LOADF: + printf("%d %d",a,sbx); + break; + case OP_LOADK: + printf("%d %d",a,bx); + printf(COMMENT); PrintConstant(f,bx); + break; + case OP_LOADKX: + printf("%d",a); + printf(COMMENT); PrintConstant(f,EXTRAARG); + break; + case OP_LOADFALSE: + printf("%d",a); + break; + case OP_LFALSESKIP: + printf("%d",a); + break; + case OP_LOADTRUE: + printf("%d",a); + break; + case OP_LOADNIL: + printf("%d %d",a,b); + printf(COMMENT "%d out",b+1); + break; + case OP_GETUPVAL: + printf("%d %d",a,b); + printf(COMMENT "%s",UPVALNAME(b)); + break; + case OP_SETUPVAL: + printf("%d %d",a,b); + printf(COMMENT "%s",UPVALNAME(b)); + break; + case OP_GETTABUP: + printf("%d %d %d",a,b,c); + printf(COMMENT "%s",UPVALNAME(b)); + printf(" "); PrintConstant(f,c); + break; + case OP_GETTABLE: + printf("%d %d %d",a,b,c); + break; + case OP_GETI: + printf("%d %d %d",a,b,c); + break; + case OP_GETFIELD: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SETTABUP: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT "%s",UPVALNAME(a)); + printf(" "); PrintConstant(f,b); + if (isk) { printf(" "); PrintConstant(f,c); } + break; + case OP_SETTABLE: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_SETI: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_SETFIELD: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT); PrintConstant(f,b); + if (isk) { printf(" "); PrintConstant(f,c); } + break; + case OP_NEWTABLE: + printf("%d %d %d",a,b,c); + printf(COMMENT "%d",c+EXTRAARGC); + break; + case OP_SELF: + printf("%d %d %d%s",a,b,c,ISK); + if (isk) { printf(COMMENT); PrintConstant(f,c); } + break; + case OP_ADDI: + printf("%d %d %d",a,b,sc); + break; + case OP_ADDK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SUBK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_MULK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_MODK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_POWK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_DIVK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_IDIVK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BANDK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BORK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_BXORK: + printf("%d %d %d",a,b,c); + printf(COMMENT); PrintConstant(f,c); + break; + case OP_SHRI: + printf("%d %d %d",a,b,sc); + break; + case OP_SHLI: + printf("%d %d %d",a,b,sc); + break; + case OP_ADD: + printf("%d %d %d",a,b,c); + break; + case OP_SUB: + printf("%d %d %d",a,b,c); + break; + case OP_MUL: + printf("%d %d %d",a,b,c); + break; + case OP_MOD: + printf("%d %d %d",a,b,c); + break; + case OP_POW: + printf("%d %d %d",a,b,c); + break; + case OP_DIV: + printf("%d %d %d",a,b,c); + break; + case OP_IDIV: + printf("%d %d %d",a,b,c); + break; + case OP_BAND: + printf("%d %d %d",a,b,c); + break; + case OP_BOR: + printf("%d %d %d",a,b,c); + break; + case OP_BXOR: + printf("%d %d %d",a,b,c); + break; + case OP_SHL: + printf("%d %d %d",a,b,c); + break; + case OP_SHR: + printf("%d %d %d",a,b,c); + break; + case OP_MMBIN: + printf("%d %d %d",a,b,c); + printf(COMMENT "%s",eventname(c)); + break; + case OP_MMBINI: + printf("%d %d %d %d",a,sb,c,isk); + printf(COMMENT "%s",eventname(c)); + if (isk) printf(" flip"); + break; + case OP_MMBINK: + printf("%d %d %d %d",a,b,c,isk); + printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); + if (isk) printf(" flip"); + break; + case OP_UNM: + printf("%d %d",a,b); + break; + case OP_BNOT: + printf("%d %d",a,b); + break; + case OP_NOT: + printf("%d %d",a,b); + break; + case OP_LEN: + printf("%d %d",a,b); + break; + case OP_CONCAT: + printf("%d %d",a,b); + break; + case OP_CLOSE: + printf("%d",a); + break; + case OP_TBC: + printf("%d",a); + break; + case OP_JMP: + printf("%d",GETARG_sJ(i)); + printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); + break; + case OP_EQ: + printf("%d %d %d",a,b,isk); + break; + case OP_LT: + printf("%d %d %d",a,b,isk); + break; + case OP_LE: + printf("%d %d %d",a,b,isk); + break; + case OP_EQK: + printf("%d %d %d",a,b,isk); + printf(COMMENT); PrintConstant(f,b); + break; + case OP_EQI: + printf("%d %d %d",a,sb,isk); + break; + case OP_LTI: + printf("%d %d %d",a,sb,isk); + break; + case OP_LEI: + printf("%d %d %d",a,sb,isk); + break; + case OP_GTI: + printf("%d %d %d",a,sb,isk); + break; + case OP_GEI: + printf("%d %d %d",a,sb,isk); + break; + case OP_TEST: + printf("%d %d",a,isk); + break; + case OP_TESTSET: + printf("%d %d %d",a,b,isk); + break; + case OP_CALL: + printf("%d %d %d",a,b,c); + printf(COMMENT); + if (b==0) printf("all in "); else printf("%d in ",b-1); + if (c==0) printf("all out"); else printf("%d out",c-1); + break; + case OP_TAILCALL: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT "%d in",b-1); + break; + case OP_RETURN: + printf("%d %d %d%s",a,b,c,ISK); + printf(COMMENT); + if (b==0) printf("all out"); else printf("%d out",b-1); + break; + case OP_RETURN0: + break; + case OP_RETURN1: + printf("%d",a); + break; + case OP_FORLOOP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc-bx+2); + break; + case OP_FORPREP: + printf("%d %d",a,bx); + printf(COMMENT "exit to %d",pc+bx+3); + break; + case OP_TFORPREP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc+bx+2); + break; + case OP_TFORCALL: + printf("%d %d",a,c); + break; + case OP_TFORLOOP: + printf("%d %d",a,bx); + printf(COMMENT "to %d",pc-bx+2); + break; + case OP_SETLIST: + printf("%d %d %d",a,b,c); + if (isk) printf(COMMENT "%d",c+EXTRAARGC); + break; + case OP_CLOSURE: + printf("%d %d",a,bx); + printf(COMMENT "%p",VOID(f->p[bx])); + break; + case OP_VARARG: + printf("%d %d",a,c); + printf(COMMENT); + if (c==0) printf("all out"); else printf("%d out",c-1); + break; + case OP_VARARGPREP: + printf("%d",a); + break; + case OP_EXTRAARG: + printf("%d",ax); + break; +#if 0 + default: + printf("%d %d %d",a,b,c); + printf(COMMENT "not handled"); + break; +#endif + } + printf("\n"); + } +} + + +#define SS(x) ((x==1)?"":"s") +#define S(x) (int)(x),SS(x) + +static void PrintHeader(const Proto* f) +{ + const char* s=f->source ? getstr(f->source) : "=?"; + if (*s=='@' || *s=='=') + s++; + else if (*s==LUA_SIGNATURE[0]) + s="(bstring)"; + else + s="(string)"; + printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", + (f->linedefined==0)?"main":"function",s, + f->linedefined,f->lastlinedefined, + S(f->sizecode),VOID(f)); + printf("%d%s param%s, %d slot%s, %d upvalue%s, ", + (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), + S(f->maxstacksize),S(f->sizeupvalues)); + printf("%d local%s, %d constant%s, %d function%s\n", + S(f->sizelocvars),S(f->sizek),S(f->sizep)); +} + +static void PrintDebug(const Proto* f) +{ + int i,n; + n=f->sizek; + printf("constants (%d) for %p:\n",n,VOID(f)); + for (i=0; isizelocvars; + 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); + } + n=f->sizeupvalues; + printf("upvalues (%d) for %p:\n",n,VOID(f)); + for (i=0; iupvalues[i].instack,f->upvalues[i].idx); + } +} + +static void PrintFunction(const Proto* f, int full) +{ + 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 e4650fbce..137103ede 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -70,6 +70,12 @@ #endif +#if defined(LUA_USE_IOS) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN +#endif + + /* @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. */ diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index aba93f828..02aed64fb 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -248,6 +248,8 @@ static void loadDebug (LoadState *S, Proto *f) { f->locvars[i].endpc = loadInt(S); } n = loadInt(S); + if (n != 0) /* does it have debug information? */ + n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) f->upvalues[i].name = loadStringN(S, f); } diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 8aab051e6..63e5f6fc3 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1412,6 +1412,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MODK) { + savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_mod, luaV_modf); vmbreak; } @@ -1424,6 +1425,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_IDIVK) { + savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_idiv, luai_numidiv); vmbreak; } @@ -1472,6 +1474,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MOD) { + savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_mod, luaV_modf); vmbreak; } @@ -1484,6 +1487,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_IDIV) { /* floor division */ + savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_idiv, luai_numidiv); vmbreak; } diff --git a/3rd/lua/makefile b/3rd/lua/makefile index fd386f65c..205cb37af 100644 --- a/3rd/lua/makefile +++ b/3rd/lua/makefile @@ -20,7 +20,7 @@ SYSCFLAGS= SYSLDFLAGS= SYSLIBS= -MYCFLAGS=-I../../skynet-src -g +MYCFLAGS= -I../../skynet-src -g MYLDFLAGS= MYLIBS= MYOBJS= @@ -30,7 +30,7 @@ CMCFLAGS= # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris +PLATS= guess aix bsd c89 freebsd generic ios linux linux-readline 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 @@ -44,7 +44,7 @@ LUAC_T= luac LUAC_O= luac.o ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) -ALL_T= $(LUA_A) $(LUA_T) +ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) ALL_A= $(LUA_A) # Targets start here. @@ -117,6 +117,9 @@ FreeBSD NetBSD OpenBSD freebsd: generic: $(ALL) +ios: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_IOS" + Linux linux: linux-noreadline linux-noreadline: diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 8794def0e..43dd9c58c 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -233,7 +233,7 @@ static int luaB_auxwrap (lua_State *L) { if (r < 0) { int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) - lua_resetthread(co, L); /* close variables in case of errors */ + lua_closethread(co, L); /* close variables in case of errors */ if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); From a5af5f6ba7213e912150d061eb276adee63f26ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 16 May 2023 15:59:43 +0800 Subject: [PATCH 483/565] Overload socket_read(), See #1750 --- lualib/skynet/socketchannel.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 251ecc28e..871334c2a 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -41,7 +41,17 @@ function socket_channel.channel(desc) __nodelay = desc.nodelay, __overload_notify = desc.overload, __overload = false, + __socket_meta = channel_socket_meta, } + if desc.socket_read or desc.socket_readline then + c.__socket_meta = { + __index = { + read = desc.socket_read or channel_socket.read, + readline = desc.socket_readline or channel_socket.readline, + }, + __gc = channel_socket_meta.__gc + } + end return setmetatable(c, channel_meta) end @@ -319,7 +329,7 @@ local function connect_once(self) skynet.yield() end - self.__sock = setmetatable( {fd} , channel_socket_meta ) + self.__sock = setmetatable( {fd} , self.__socket_meta ) self.__dispatch_thread = skynet.fork(function() if self.__sock then -- self.__sock can be false (socket closed) if error during connecting, See #1513 From 4893fa39eefa6e47dd5d400fde6c2521e93eab53 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 16 May 2023 16:18:19 +0800 Subject: [PATCH 484/565] remove unused code --- service/clusteragent.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index b0b8c2b8d..541b3f5d6 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -1,5 +1,4 @@ local skynet = require "skynet" -local sc = require "skynet.socketchannel" local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" local ignoreret = skynet.ignoreret From 2f9d0c805ac694f9d8358f759fdf3da5ad35a47d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Jun 2023 23:05:40 +0800 Subject: [PATCH 485/565] include stdint.h for uintptr_t --- skynet-src/atomic.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index 0ce185e3c..8e3a9e406 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -1,11 +1,11 @@ #ifndef SKYNET_ATOMIC_H #define SKYNET_ATOMIC_H -#ifdef __STDC_NO_ATOMICS__ - #include #include +#ifdef __STDC_NO_ATOMICS__ + #define ATOM_INT volatile int #define ATOM_POINTER volatile uintptr_t #define ATOM_SIZET volatile size_t From 69420bdfde087e0d24eba8ec55c14296db4ef9a4 Mon Sep 17 00:00:00 2001 From: Jin Xiao <996442717qqcom@gmail.com> Date: Tue, 27 Jun 2023 16:28:21 +0800 Subject: [PATCH 486/565] upgrade lpeg to 1.1.0 (#1766) * upgrade lpeg to 1.1.0 * fix lpeg compiler error --- 3rd/lpeg/HISTORY | 11 +- 3rd/lpeg/README.md | 4 + 3rd/lpeg/lpcap.c | 227 ++++++++++++++++---------- 3rd/lpeg/lpcap.h | 39 ++++- 3rd/lpeg/lpcode.c | 399 +++++++++++++++++++++++++-------------------- 3rd/lpeg/lpcode.h | 8 +- 3rd/lpeg/lpcset.c | 110 +++++++++++++ 3rd/lpeg/lpcset.h | 30 ++++ 3rd/lpeg/lpeg.html | 365 ++++++++++++++++++++--------------------- 3rd/lpeg/lpprint.c | 140 +++++++++++----- 3rd/lpeg/lpprint.h | 10 +- 3rd/lpeg/lptree.c | 212 +++++++++++++++++------- 3rd/lpeg/lptree.h | 26 ++- 3rd/lpeg/lptypes.h | 33 ++-- 3rd/lpeg/lpvm.c | 155 +++++++++++++----- 3rd/lpeg/lpvm.h | 51 ++++-- 3rd/lpeg/makefile | 16 +- 3rd/lpeg/re.html | 86 ++++------ 3rd/lpeg/re.lua | 31 ++-- 3rd/lpeg/test.lua | 170 +++++++++++++++++-- Makefile | 2 +- 21 files changed, 1389 insertions(+), 736 deletions(-) create mode 100644 3rd/lpeg/README.md create mode 100644 3rd/lpeg/lpcset.c create mode 100644 3rd/lpeg/lpcset.h diff --git a/3rd/lpeg/HISTORY b/3rd/lpeg/HISTORY index 66a8e14db..a25070981 100644 --- a/3rd/lpeg/HISTORY +++ b/3rd/lpeg/HISTORY @@ -1,4 +1,13 @@ -HISTORY for LPeg 1.0.2 +HISTORY for LPeg 1.1.0 + +* Changes from version 1.0.2 to 1.1.0 + --------------------------------- + + accumulator capture + + UTF-8 ranges + + Larger limit for number of rules in a grammar + + Larger limit for number of captures in a match + + bug fixes + + other small improvements * Changes from version 1.0.1 to 1.0.2 --------------------------------- diff --git a/3rd/lpeg/README.md b/3rd/lpeg/README.md new file mode 100644 index 000000000..65ac1eb5a --- /dev/null +++ b/3rd/lpeg/README.md @@ -0,0 +1,4 @@ +# LPeg - Parsing Expression Grammars For Lua + +For more information, +see [Lpeg](//www.inf.puc-rio.br/~roberto/lpeg/). diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c index b332fde49..f13ecf4d8 100644 --- a/3rd/lpeg/lpcap.c +++ b/3rd/lpeg/lpcap.c @@ -1,27 +1,39 @@ -/* -** $Id: lpcap.c $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ #include "lua.h" #include "lauxlib.h" #include "lpcap.h" +#include "lpprint.h" #include "lptypes.h" -#define captype(cap) ((cap)->kind) +#define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v) -#define isclosecap(cap) (captype(cap) == Cclose) +#define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) -#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 skipclose(cs,head) \ + if (isopencap(head)) { assert(isclosecap(cs->cap)); cs->cap++; } -#define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) +/* +** Return the size of capture 'cap'. If it is an open capture, 'close' +** must be its corresponding close. +*/ +static Index_t capsize (Capture *cap, Capture *close) { + if (isopencap(cap)) { + assert(isclosecap(close)); + return close->index - cap->index; + } + else + return cap->siz - 1; +} + + +static Index_t closesize (CapState *cs, Capture *head) { + return capsize(head, cs->cap); +} /* @@ -44,35 +56,40 @@ static int pushcapture (CapState *cs); /* ** Goes back in a list of captures looking for an open capture -** corresponding to a close +** corresponding to a close one. */ 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)) + else if (isopencap(cap)) if (n-- == 0) return cap; } } /* -** Go to the next capture +** Go to the next capture at the same level. */ static void nextcap (CapState *cs) { Capture *cap = cs->cap; - if (!isfullcap(cap)) { /* not a single capture? */ + if (isopencap(cap)) { /* must look for a close? */ int n = 0; /* number of opens waiting a close */ for (;;) { /* look for corresponding close */ cap++; - if (isclosecap(cap)) { + if (isopencap(cap)) n++; + else if (isclosecap(cap)) if (n-- == 0) break; - } - else if (!isfullcap(cap)) n++; } + cs->cap = cap + 1; /* + 1 to skip last close */ + } + else { + Capture *next; + for (next = cap + 1; capinside(cap, next); next++) + ; /* skip captures inside current one */ + cs->cap = next; } - cs->cap = cap + 1; /* + 1 to skip last close (or entire single capture) */ } @@ -84,22 +101,17 @@ static void nextcap (CapState *cs) { ** 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; + Capture *head = cs->cap++; /* original capture */ + int n = 0; /* number of pushed subvalues */ + /* repeat for all nested patterns */ + while (capinside(head, cs->cap)) + n += pushcapture(cs); + if (addextra || n == 0) { /* need extra? */ + lua_pushlstring(cs->L, cs->s + head->index, closesize(cs, head)); + n++; } + skipclose(cs, head); + return n; } @@ -113,18 +125,44 @@ static void pushonenestedvalue (CapState *cs) { } +/* +** Checks whether group 'grp' is visible to 'ref', that is, 'grp' is +** not nested inside a full capture that does not contain 'ref'. (We +** only need to care for full captures because the search at 'findback' +** skips open-end blocks; so, if 'grp' is nested in a non-full capture, +** 'ref' is also inside it.) To check this, we search backward for the +** inner full capture enclosing 'grp'. A full capture cannot contain +** non-full captures, so a close capture means we cannot be inside a +** full capture anymore. +*/ +static int capvisible (CapState *cs, Capture *grp, Capture *ref) { + Capture *cap = grp; + int i = MAXLOP; /* maximum distance for an 'open' */ + while (i-- > 0 && cap-- > cs->ocap) { + if (isclosecap(cap)) + return 1; /* can stop the search */ + else if (grp->index - cap->index >= UCHAR_MAX) + return 1; /* can stop the search */ + else if (capinside(cap, grp)) /* is 'grp' inside cap? */ + return capinside(cap, ref); /* ok iff cap also contains 'ref' */ + } + return 1; /* 'grp' is not inside any capture */ +} + + /* ** Try to find a named group capture with the name given at the top of -** the stack; goes backward from 'cap'. +** the stack; goes backward from 'ref'. */ -static Capture *findback (CapState *cs, Capture *cap) { +static Capture *findback (CapState *cs, Capture *ref) { lua_State *L = cs->L; + Capture *cap = ref; 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) { + else if (capinside(cap, ref)) + continue; /* enclosing captures are not visible to 'ref' */ + if (captype(cap) == Cgroup && capvisible(cs, cap, ref)) { getfromktable(cs, cap->idx); /* get group name */ if (lp_equal(L, -2, -1)) { /* right group? */ lua_pop(L, 2); /* remove reference name and group name */ @@ -158,11 +196,10 @@ static int backrefcap (CapState *cs) { */ static int tablecap (CapState *cs) { lua_State *L = cs->L; + Capture *head = cs->cap++; int n = 0; lua_newtable(L); - if (isfullcap(cs->cap++)) - return 1; /* table is empty */ - while (!isclosecap(cs->cap)) { + while (capinside(head, cs->cap)) { if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */ pushluaval(cs); /* push group name */ pushonenestedvalue(cs); @@ -176,7 +213,7 @@ static int tablecap (CapState *cs) { n += k; } } - cs->cap++; /* skip close entry */ + skipclose(cs, head); return 1; /* number of values pushed (only the table) */ } @@ -203,20 +240,20 @@ static int querycap (CapState *cs) { 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)? */ + Capture *head = cs->cap++; + int idx = head->idx; + if (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)) { + while (capinside(head, 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 */ + skipclose(cs, head); return 1; /* only accumulator left on the stack */ } @@ -234,6 +271,22 @@ static int functioncap (CapState *cs) { } +/* +** Accumulator capture +*/ +static int accumulatorcap (CapState *cs) { + lua_State *L = cs->L; + int n; + if (lua_gettop(L) < cs->firstcap) + luaL_error(L, "no previous value for accumulator capture"); + pushluaval(cs); /* push function */ + lua_insert(L, -2); /* previous value becomes first argument */ + n = pushnestedvalues(cs, 0); /* push nested captures */ + lua_call(L, n + 1, 1); /* call function */ + return 0; /* did not add any extra value */ +} + + /* ** Select capture */ @@ -283,7 +336,7 @@ int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { assert(captype(open) == Cgroup); id = finddyncap(open, close); /* get first dynamic capture argument */ close->kind = Cclose; /* closes the group */ - close->s = s; + close->index = s - cs->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 */ @@ -313,8 +366,8 @@ typedef struct StrAux { 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 */ + Index_t idx; /* starts here */ + Index_t siz; /* with this size */ } s; } u; } StrAux; @@ -329,24 +382,23 @@ typedef struct StrAux { */ static int getstrcaps (CapState *cs, StrAux *cps, int n) { int k = n++; + Capture *head = cs->cap++; 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++; - } + cps[k].u.s.idx = head->index; /* starts here */ + while (capinside(head, cs->cap)) { + 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 */ + cps[k].u.s.siz = closesize(cs, head); + skipclose(cs, head); return n; } @@ -368,7 +420,7 @@ static void stringcap (luaL_Buffer *b, CapState *cs) { 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 */ + for (i = 0; i < len; i++) { /* traverse format string */ 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? */ @@ -378,7 +430,7 @@ static void stringcap (luaL_Buffer *b, CapState *cs) { 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); + luaL_addlstring(b, cs->s + cps[l].u.s.idx, cps[l].u.s.siz); else { Capture *curr = cs->cap; cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */ @@ -395,22 +447,20 @@ static void stringcap (luaL_Buffer *b, CapState *cs) { ** 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 */ + const char *curr = cs->s + cs->cap->index; + Capture *head = cs->cap++; + while (capinside(head, cs->cap)) { + Capture *cap = cs->cap; + const char *caps = cs->s + cap->index; + luaL_addlstring(b, curr, caps - curr); /* add text up to capture */ + if (addonestring(b, cs, "replacement")) + curr = caps + capsize(cap, cs->cap - 1); /* continue after match */ + else /* no capture value */ + curr = caps; /* keep original text in final result */ } - cs->cap++; /* go to next capture */ + /* add last piece of text */ + luaL_addlstring(b, curr, cs->s + head->index + closesize(cs, head) - curr); + skipclose(cs, head); } @@ -426,13 +476,16 @@ static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { case Csubst: substcap(b, cs); /* add capture directly to buffer */ return 1; + case Cacc: /* accumulator capture? */ + return luaL_error(cs->L, "invalid context for an accumulator capture"); 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)); + return luaL_error(L, "invalid %s value (a %s)", + what, luaL_typename(L, -1)); luaL_addvalue(b); } return n; @@ -458,7 +511,7 @@ static int pushcapture (CapState *cs) { return luaL_error(L, "subcapture nesting too deep"); switch (captype(cs->cap)) { case Cposition: { - lua_pushinteger(L, cs->cap->s - cs->s + 1); + lua_pushinteger(L, cs->cap->index + 1); cs->cap++; res = 1; break; @@ -516,6 +569,7 @@ static int pushcapture (CapState *cs) { case Cbackref: res = backrefcap(cs); break; case Ctable: res = tablecap(cs); break; case Cfunction: res = functioncap(cs); break; + case Cacc: res = accumulatorcap(cs); break; case Cnum: res = numcap(cs); break; case Cquery: res = querycap(cs); break; case Cfold: res = foldcap(cs); break; @@ -529,7 +583,7 @@ static int pushcapture (CapState *cs) { /* ** 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' +** 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.) @@ -537,13 +591,16 @@ static int pushcapture (CapState *cs) { int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop)); int n = 0; + /* printcaplist(capture); */ if (!isclosecap(capture)) { /* is there any capture? */ CapState cs; cs.ocap = cs.cap = capture; cs.L = L; cs.reclevel = 0; cs.s = s; cs.valuecached = 0; cs.ptop = ptop; + cs.firstcap = lua_gettop(L) + 1; /* where first value (if any) will go */ do { /* collect their values */ n += pushcapture(&cs); } while (!isclosecap(cs.cap)); + assert(lua_gettop(L) - cs.firstcap == n - 1); } if (n == 0) { /* no capture values? */ lua_pushinteger(L, r - s + 1); /* return only end position */ diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h index dc10d6969..abbd55371 100644 --- a/3rd/lpeg/lpcap.h +++ b/3rd/lpeg/lpcap.h @@ -1,6 +1,3 @@ -/* -** $Id: lpcap.h $ -*/ #if !defined(lpcap_h) #define lpcap_h @@ -19,6 +16,7 @@ typedef enum CapKind { Csimple, /* next node is pattern */ Ctable, /* next node is pattern */ Cfunction, /* ktable[key] is function; next node is pattern */ + Cacc, /* 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 */ @@ -29,8 +27,20 @@ typedef enum CapKind { } CapKind; +/* +** An unsigned integer large enough to index any subject entirely. +** It can be size_t, but that will double the size of the array +** of captures in a 64-bit machine. +*/ +#if !defined(Index_t) +typedef uint Index_t; +#endif + +#define MAXINDT (~(Index_t)0) + + typedef struct Capture { - const char *s; /* subject position */ + Index_t index; /* subject position */ 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) */ @@ -41,13 +51,32 @@ typedef struct CapState { Capture *cap; /* current capture */ Capture *ocap; /* (original) capture list */ lua_State *L; - int ptop; /* index of last argument to 'match' */ + int ptop; /* stack index of last argument to 'match' */ + int firstcap; /* stack index of first capture pushed in the stack */ const char *s; /* original string */ int valuecached; /* value stored in cache slot */ int reclevel; /* recursion level */ } CapState; +#define captype(cap) ((cap)->kind) + +#define isclosecap(cap) (captype(cap) == Cclose) +#define isopencap(cap) ((cap)->siz == 0) + +/* true if c2 is (any number of levels) inside c1 */ +#define capinside(c1,c2) \ + (isopencap(c1) ? !isclosecap(c2) \ + : (c2)->index < (c1)->index + (c1)->siz - 1) + + +/** +** Maximum number of captures to visit when looking for an 'open'. +*/ +#define MAXLOP 20 + + + 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); diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c index 392345972..f3b8ae365 100644 --- a/3rd/lpeg/lpcode.c +++ b/3rd/lpeg/lpcode.c @@ -1,7 +1,3 @@ -/* -** $Id: lpcode.c $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ #include @@ -11,6 +7,7 @@ #include "lptypes.h" #include "lpcode.h" +#include "lpcset.h" /* signals a "no-instruction */ @@ -26,61 +23,13 @@ static const Charset fullset_ = static const Charset *fullset = &fullset_; + /* ** {====================================================== ** Analysis and some optimizations ** ======================================================= */ -/* -** 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; /* number of characters in the set */ - int 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) { /* 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) { /* 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) { /* 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; - } - } - 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 @@ -89,42 +38,12 @@ 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; -} - static int cs_disjoint (const Charset *cs1, const Charset *cs2) { loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) return 1; } -/* -** 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) { - 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 characters to the set */ - return 1; - } - default: return 0; - } -} - - /* ** Visit a TCall node taking care to stop recursion. If node not yet ** visited, return 'f(sib2(tree))', otherwise return 'def' (default @@ -196,7 +115,7 @@ int hascaptures (TTree *tree) { int checkaux (TTree *tree, int pred) { tailcall: switch (tree->tag) { - case TChar: case TSet: case TAny: + case TChar: case TSet: case TAny: case TUTFR: case TFalse: case TOpenCall: return 0; /* not nullable */ case TRep: case TTrue: @@ -220,7 +139,7 @@ int checkaux (TTree *tree, int pred) { if (checkaux(sib2(tree), pred)) return 1; /* else return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; - case TCapture: case TGrammar: case TRule: + case TCapture: case TGrammar: case TRule: case TXInfo: /* return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; case TCall: /* return checkaux(sib2(tree), pred); */ @@ -239,11 +158,13 @@ int fixedlen (TTree *tree) { switch (tree->tag) { case TChar: case TSet: case TAny: return len + 1; + case TUTFR: + return (tree->cap == sib1(tree)->cap) ? len + tree->cap : -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: + case TCapture: case TRule: case TGrammar: case TXInfo: /* return fixedlen(sib1(tree)); */ tree = sib1(tree); goto tailcall; case TCall: { @@ -294,18 +215,21 @@ int fixedlen (TTree *tree) { static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { tailcall: switch (tree->tag) { - case TChar: case TSet: case TAny: { + case TChar: case TSet: case TAny: case TFalse: { tocharset(tree, firstset); return 0; } + case TUTFR: { + int c; + clearset(firstset->cs); /* erase all chars */ + for (c = tree->key; c <= sib1(tree)->key; c++) + setchar(firstset->cs, c); + return 0; + } case TTrue: { loopset(i, firstset->cs[i] = follow->cs[i]); return 1; /* accepts the empty string */ } - case TFalse: { - loopset(i, firstset->cs[i] = 0); - return 0; - } case TChoice: { Charset csaux; int e1 = getfirst(sib1(tree), follow, firstset); @@ -334,7 +258,7 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { loopset(i, firstset->cs[i] |= follow->cs[i]); return 1; /* accept the empty string */ } - case TCapture: case TGrammar: case TRule: { + case TCapture: case TGrammar: case TRule: case TXInfo: { /* return getfirst(sib1(tree), follow, firstset); */ tree = sib1(tree); goto tailcall; } @@ -356,9 +280,8 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { if (tocharset(sib1(tree), firstset)) { cs_complement(firstset); return 1; - } - /* else go through */ - } + } /* else */ + } /* FALLTHROUGH */ case TBehind: { /* instruction gives no new information */ /* call 'getfirst' only to check for math-time captures */ int e = getfirst(sib1(tree), follow, firstset); @@ -380,9 +303,9 @@ static int headfail (TTree *tree) { case TChar: case TSet: case TAny: case TFalse: return 1; case TTrue: case TRep: case TRunTime: case TNot: - case TBehind: + case TBehind: case TUTFR: return 0; - case TCapture: case TGrammar: case TRule: case TAnd: + case TCapture: case TGrammar: case TRule: case TXInfo: case TAnd: tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */ case TCall: tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */ @@ -407,7 +330,7 @@ static int headfail (TTree *tree) { static int needfollow (TTree *tree) { tailcall: switch (tree->tag) { - case TChar: case TSet: case TAny: + case TChar: case TSet: case TAny: case TUTFR: case TFalse: case TTrue: case TAnd: case TNot: case TRunTime: case TGrammar: case TCall: case TBehind: return 0; @@ -418,7 +341,7 @@ static int needfollow (TTree *tree) { case TSeq: tree = sib2(tree); goto tailcall; default: assert(0); return 0; - } + } } /* }====================================================== */ @@ -437,10 +360,11 @@ static int needfollow (TTree *tree) { */ int sizei (const Instruction *i) { switch((Opcode)i->i.code) { - case ISet: case ISpan: return CHARSETINSTSIZE; - case ITestSet: return CHARSETINSTSIZE + 1; + case ISet: case ISpan: return 1 + i->i.aux2.set.size; + case ITestSet: return 2 + i->i.aux2.set.size; case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall: case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit: + case IUTFR: return 2; default: return 1; } @@ -468,23 +392,67 @@ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl); -void realloccode (lua_State *L, Pattern *p, int nsize) { +static void finishrelcode (lua_State *L, Pattern *p, Instruction *block, + int size) { + if (block == NULL) + luaL_error(L, "not enough memory"); + block->codesize = size; + p->code = (Instruction *)block + 1; +} + + +/* +** Initialize array 'p->code' +*/ +static void newcode (lua_State *L, Pattern *p, int size) { void *ud; + Instruction *block; 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; + size++; /* slot for 'codesize' */ + block = (Instruction*) f(ud, NULL, 0, size * sizeof(Instruction)); + finishrelcode(L, p, block, size); } -static int nextinstruction (CompileState *compst) { - int size = compst->p->codesize; - if (compst->ncode >= size) - realloccode(compst->L, compst->p, size * 2); - return compst->ncode++; +void freecode (lua_State *L, Pattern *p) { + if (p->code != NULL) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + uint osize = p->code[-1].codesize; + f(ud, p->code - 1, osize * sizeof(Instruction), 0); /* free block */ + } +} + + +/* +** Assume that 'nsize' is not zero and that 'p->code' already exists. +*/ +static void realloccode (lua_State *L, Pattern *p, int nsize) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + Instruction *block = p->code - 1; + uint osize = block->codesize; + nsize++; /* add the 'codesize' slot to size */ + block = (Instruction*) f(ud, block, osize * sizeof(Instruction), + nsize * sizeof(Instruction)); + finishrelcode(L, p, block, nsize); +} + + +/* +** Add space for an instruction with 'n' slots and return its index. +*/ +static int nextinstruction (CompileState *compst, int n) { + int size = compst->p->code[-1].codesize - 1; + int ncode = compst->ncode; + if (ncode > size - n) { + uint nsize = size + (size >> 1) + n; + if (nsize >= INT_MAX) + luaL_error(compst->L, "pattern code too large"); + realloccode(compst->L, compst->p, nsize); + } + compst->ncode = ncode + n; + return ncode; } @@ -492,9 +460,9 @@ static int nextinstruction (CompileState *compst) { static int addinstruction (CompileState *compst, Opcode op, int aux) { - int i = nextinstruction(compst); + int i = nextinstruction(compst, 1); getinstr(compst, i).i.code = op; - getinstr(compst, i).i.aux = aux; + getinstr(compst, i).i.aux1 = aux; return i; } @@ -518,6 +486,16 @@ static void setoffset (CompileState *compst, int instruction, int offset) { } +static void codeutfr (CompileState *compst, TTree *tree) { + int i = addoffsetinst(compst, IUTFR); + int to = sib1(tree)->u.n; + assert(sib1(tree)->tag == TXInfo); + getinstr(compst, i + 1).offset = tree->u.n; + getinstr(compst, i).i.aux1 = to & 0xff; + getinstr(compst, i).i.aux2.key = to >> 8; +} + + /* ** Add a capture instruction: ** 'op' is the capture instruction; 'cap' the capture kind; @@ -527,7 +505,7 @@ static void setoffset (CompileState *compst, int instruction, int 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; + getinstr(compst, i).i.aux2.key = key; return i; } @@ -560,7 +538,7 @@ static void jumptohere (CompileState *compst, int instruction) { */ static void codechar (CompileState *compst, int c, int tt) { if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && - getinstr(compst, tt).i.aux == c) + getinstr(compst, tt).i.aux1 == c) addinstruction(compst, IAny, 0); else addinstruction(compst, IChar, c); @@ -568,45 +546,63 @@ static void codechar (CompileState *compst, int c, int tt) { /* -** Add a charset posfix to an instruction +** Add a charset posfix to an instruction. */ -static void addcharset (CompileState *compst, const byte *cs) { - int p = gethere(compst); +static void addcharset (CompileState *compst, int inst, charsetinfo *info) { + int p; + Instruction *I = &getinstr(compst, inst); + byte *charset; + int isize = instsize(info->size); /* size in instructions */ 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; + I->i.aux2.set.offset = info->offset * 8; /* offset in bits */ + I->i.aux2.set.size = isize; + I->i.aux1 = info->deflt; + p = nextinstruction(compst, isize); /* space for charset */ + charset = getinstr(compst, p).buff; /* charset buffer */ + for (i = 0; i < isize * (int)sizeof(Instruction); i++) + charset[i] = getbytefromcharset(info, i); /* copy the buffer */ +} + + +/* +** Check whether charset 'info' is dominated by instruction 'p' +*/ +static int cs_equal (Instruction *p, charsetinfo *info) { + if (p->i.code != ITestSet) + return 0; + else if (p->i.aux2.set.offset != info->offset * 8 || + p->i.aux2.set.size != instsize(info->size) || + p->i.aux1 != info->deflt) + return 0; + else { + int i; + for (i = 0; i < instsize(info->size) * (int)sizeof(Instruction); i++) { + if ((p + 2)->buff[i] != getbytefromcharset(info, i)) + return 0; } - default: addinstruction(compst, op, c); break; } + return 1; } /* -** code a test set, optimizing unit sets for ITestChar, "complete" +** Code a char set, using IAny when instruction is dominated by an +** equivalent test. +*/ +static void codecharset (CompileState *compst, TTree *tree, int tt) { + charsetinfo info; + tree2cset(tree, &info); + if (tt >= 0 && cs_equal(&getinstr(compst, tt), &info)) + addinstruction(compst, IAny, 0); + else { + int i = addinstruction(compst, ISet, 0); + addcharset(compst, i, &info); + } +} + + +/* +** 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.) @@ -614,22 +610,22 @@ static void codecharset (CompileState *compst, const byte *cs, int tt) { 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); + charsetinfo info; + Opcode op = charsettype(cs->cs, &info); 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; + getinstr(compst, i).i.aux1 = info.offset; return i; } - case ISet: { + default: { /* regular set */ int i = addoffsetinst(compst, ITestSet); - addcharset(compst, cs->cs); + addcharset(compst, i, &info); + assert(op == ISet); return i; } - default: assert(0); return 0; } } } @@ -665,11 +661,11 @@ static void codebehind (CompileState *compst, TTree *tree) { /* ** Choice; optimizations: -** - 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, +** - 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, either because p1 will accept +** (headfail) or because it is not in first(p2) (disjoint). +** (The second case 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 reuse ** the Choice already active in the stack. @@ -686,7 +682,7 @@ static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, int jmp = NOINST; codegen(compst, p1, 0, test, fl); if (!emptyp2) - jmp = addoffsetinst(compst, IJmp); + jmp = addoffsetinst(compst, IJmp); jumptohere(compst, test); codegen(compst, p2, opt, NOINST, fl); jumptohere(compst, jmp); @@ -697,7 +693,7 @@ static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, codegen(compst, p1, 1, NOINST, fullset); } else { - /* == + /* == test(first(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ int pcommit; int test = codetestset(compst, &cs1, e1); @@ -764,9 +760,53 @@ static void coderuntime (CompileState *compst, TTree *tree, int tt) { } +/* +** Create a jump to 'test' and fix 'test' to jump to next instruction +*/ +static void closeloop (CompileState *compst, int test) { + int jmp = addoffsetinst(compst, IJmp); + jumptohere(compst, test); + jumptothere(compst, jmp, test); +} + + +/* +** Try repetition of charsets: +** For an empty set, repetition of fail is a no-op; +** For any or char, code a tight loop; +** For generic charset, use a span instruction. +*/ +static int coderepcharset (CompileState *compst, TTree *tree) { + switch (tree->tag) { + case TFalse: return 1; /* 'fail*' is a no-op */ + case TAny: { /* L1: testany -> L2; any; jmp L1; L2: */ + int test = addoffsetinst(compst, ITestAny); + addinstruction(compst, IAny, 0); + closeloop(compst, test); + return 1; + } + case TChar: { /* L1: testchar c -> L2; any; jmp L1; L2: */ + int test = addoffsetinst(compst, ITestChar); + getinstr(compst, test).i.aux1 = tree->u.n; + addinstruction(compst, IAny, 0); + closeloop(compst, test); + return 1; + } + case TSet: { /* regular set */ + charsetinfo info; + int i = addinstruction(compst, ISpan, 0); + tree2cset(tree, &info); + addcharset(compst, i, &info); + return 1; + } + default: return 0; /* not a charset */ + } +} + + /* ** Repetion; optimizations: -** When pattern is a charset, can use special instruction ISpan. +** When pattern is a charset, use special code. ** 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 @@ -776,21 +816,14 @@ static void coderuntime (CompileState *compst, TTree *tree, int tt) { */ 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 { + if (!coderepcharset(compst, tree)) { + Charset st; 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, 0, test, fullset); - jmp = addoffsetinst(compst, IJmp); - jumptohere(compst, test); - jumptothere(compst, jmp, test); + closeloop(compst, test); } else { /* test(fail(p1)) -> L2; choice L2; L1:

; partialcommit L1; L2: */ @@ -847,7 +880,7 @@ static void correctcalls (CompileState *compst, int *positions, 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 n = code[i].i.aux2.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 ? */ @@ -874,8 +907,10 @@ static void codegrammar (CompileState *compst, TTree *grammar) { int start = gethere(compst); /* here starts the initial rule */ jumptohere(compst, firstcall); for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { + TTree *r = sib1(rule); + assert(r->tag == TXInfo); positions[rulenumber++] = gethere(compst); /* save rule position */ - codegen(compst, sib1(rule), 0, NOINST, fullset); /* code rule */ + codegen(compst, sib1(r), 0, NOINST, fullset); /* code rule */ addinstruction(compst, IRet, 0); } assert(rule->tag == TTrue); @@ -886,8 +921,8 @@ static void codegrammar (CompileState *compst, TTree *grammar) { 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); + assert(sib1(sib2(call))->tag == TXInfo); + getinstr(compst, c).i.aux2.key = sib1(sib2(call))->u.n; /* rule number */ } @@ -922,9 +957,10 @@ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, 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 TSet: codecharset(compst, tree, tt); break; case TTrue: break; case TFalse: addinstruction(compst, IFail, 0); break; + case TUTFR: codeutfr(compst, tree); 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; @@ -971,7 +1007,7 @@ static void peephole (CompileState *compst) { 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 */ + code[i + 1].i.code = IEmpty; /* 'no-op' for target position */ break; } case ICommit: case IPartialCommit: @@ -996,12 +1032,13 @@ static void peephole (CompileState *compst) { /* -** Compile a pattern +** Compile a pattern. 'size' is the size of the pattern's tree, +** which gives a hint for the size of the final code. */ -Instruction *compile (lua_State *L, Pattern *p) { +Instruction *compile (lua_State *L, Pattern *p, uint size) { CompileState compst; compst.p = p; compst.ncode = 0; compst.L = L; - realloccode(L, p, 2); /* minimum initial size */ + newcode(L, p, size/2u + 2); /* set initial size */ codegen(&compst, p->tree, 0, NOINST, fullset); addinstruction(&compst, IEnd, 0); realloccode(L, p, compst.ncode); /* set final size */ diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h index 34ee27637..10c2ced5b 100644 --- a/3rd/lpeg/lpcode.h +++ b/3rd/lpeg/lpcode.h @@ -1,6 +1,3 @@ -/* -** $Id: lpcode.h $ -*/ #if !defined(lpcode_h) #define lpcode_h @@ -11,13 +8,12 @@ #include "lptree.h" #include "lpvm.h" -int tocharset (TTree *tree, Charset *cs); int checkaux (TTree *tree, int pred); int fixedlen (TTree *tree); int hascaptures (TTree *tree); int lp_gc (lua_State *L); -Instruction *compile (lua_State *L, Pattern *p); -void realloccode (lua_State *L, Pattern *p, int nsize); +Instruction *compile (lua_State *L, Pattern *p, uint size); +void freecode (lua_State *L, Pattern *p); int sizei (const Instruction *i); diff --git a/3rd/lpeg/lpcset.c b/3rd/lpeg/lpcset.c new file mode 100644 index 000000000..2dcffd9a4 --- /dev/null +++ b/3rd/lpeg/lpcset.c @@ -0,0 +1,110 @@ + +#include "lptypes.h" +#include "lpcset.h" + + +/* +** Add to 'c' the index of the (only) bit set in byte 'b' +*/ +static int onlybit (int c, int b) { + if ((b & 0xF0) != 0) { c += 4; b >>= 4; } + if ((b & 0x0C) != 0) { c += 2; b >>= 2; } + if ((b & 0x02) != 0) { c += 1; } + return c; +} + + +/* +** Check whether a charset is empty (returns IFail), singleton (IChar), +** full (IAny), or none of those (ISet). When singleton, 'info.offset' +** returns which character it is. When generic set, 'info' returns +** information about its range. +*/ +Opcode charsettype (const byte *cs, charsetinfo *info) { + int low0, low1, high0, high1; + for (low1 = 0; low1 < CHARSETSIZE && cs[low1] == 0; low1++) + /* find lowest byte with a 1-bit */; + if (low1 == CHARSETSIZE) + return IFail; /* no characters in set */ + for (high1 = CHARSETSIZE - 1; cs[high1] == 0; high1--) + /* find highest byte with a 1-bit; low1 is a sentinel */; + if (low1 == high1) { /* only one byte with 1-bits? */ + int b = cs[low1]; + if ((b & (b - 1)) == 0) { /* does byte has only one 1-bit? */ + info->offset = onlybit(low1 * BITSPERCHAR, b); /* get that bit */ + return IChar; /* single character */ + } + } + for (low0 = 0; low0 < CHARSETSIZE && cs[low0] == 0xFF; low0++) + /* find lowest byte with a 0-bit */; + if (low0 == CHARSETSIZE) + return IAny; /* set has all bits set */ + for (high0 = CHARSETSIZE - 1; cs[high0] == 0xFF; high0--) + /* find highest byte with a 0-bit; low0 is a sentinel */; + if (high1 - low1 <= high0 - low0) { /* range of 1s smaller than of 0s? */ + info->offset = low1; + info->size = high1 - low1 + 1; + info->deflt = 0; /* all discharged bits were 0 */ + } + else { + info->offset = low0; + info->size = high0 - low0 + 1; + info->deflt = 0xFF; /* all discharged bits were 1 */ + } + info->cs = cs + info->offset; + return ISet; +} + + +/* +** Get a byte from a compact charset. If index is inside the charset +** range, get the byte from the supporting charset (correcting it +** by the offset). Otherwise, return the default for the set. +*/ +byte getbytefromcharset (const charsetinfo *info, int index) { + if (index < info->size) + return info->cs[index]; + else return info->deflt; +} + + +/* +** If 'tree' is a 'char' pattern (TSet, TChar, TAny, TFalse), convert it +** into a charset and return 1; else return 0. +*/ +int tocharset (TTree *tree, Charset *cs) { + switch (tree->tag) { + case TChar: { /* only one char */ + assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX); + clearset(cs->cs); /* erase all chars */ + setchar(cs->cs, tree->u.n); /* add that one */ + return 1; + } + case TAny: { + fillset(cs->cs, 0xFF); /* add all characters to the set */ + return 1; + } + case TFalse: { + clearset(cs->cs); /* empty set */ + return 1; + } + case TSet: { /* fill set */ + int i; + fillset(cs->cs, tree->u.set.deflt); + for (i = 0; i < tree->u.set.size; i++) + cs->cs[tree->u.set.offset + i] = treebuffer(tree)[i]; + return 1; + } + default: return 0; + } +} + + +void tree2cset (TTree *tree, charsetinfo *info) { + assert(tree->tag == TSet); + info->offset = tree->u.set.offset; + info->size = tree->u.set.size; + info->deflt = tree->u.set.deflt; + info->cs = treebuffer(tree); +} + diff --git a/3rd/lpeg/lpcset.h b/3rd/lpeg/lpcset.h new file mode 100644 index 000000000..b69fef900 --- /dev/null +++ b/3rd/lpeg/lpcset.h @@ -0,0 +1,30 @@ + +#if !defined(lpset_h) +#define lpset_h + +#include "lpcset.h" +#include "lpcode.h" +#include "lptree.h" + + +/* +** Extra information for the result of 'charsettype'. When result is +** IChar, 'offset' is the character. When result is ISet, 'cs' is the +** supporting bit array (with offset included), 'offset' is the offset +** (in bytes), 'size' is the size (in bytes), and 'delt' is the default +** value for bytes outside the set. +*/ +typedef struct { + const byte *cs; + int offset; + int size; + int deflt; +} charsetinfo; + + +int tocharset (TTree *tree, Charset *cs); +Opcode charsettype (const byte *cs, charsetinfo *info); +byte getbytefromcharset (const charsetinfo *info, int index); +void tree2cset (TTree *tree, charsetinfo *info); + +#endif diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html index 8b9f59c2a..b31d57599 100644 --- a/3rd/lpeg/lpeg.html +++ b/3rd/lpeg/lpeg.html @@ -1,28 +1,27 @@ - + "//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> + LPeg - Parsing Expression Grammars For Lua -

LPeg
- Parsing Expression Grammars For Lua, version 1.0 + Parsing Expression Grammars For Lua, version 1.1
@@ -56,16 +55,16 @@

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 +talk about LPeg given at the III Lua Workshop.)

@@ -107,6 +106,9 @@

Introduction

Matches any character in string (Set) lpeg.R("xy") Matches any character between x and y (Range) +lpeg.utfR(cp1, cp2) + Matches an UTF-8 code point between cp1 and + cp2 patt^n Matches at least n repetitions of patt patt^-n @@ -142,7 +144,7 @@

Introduction

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, +(This module is 270 lines of Lua code, and of course it uses LPeg to parse regular expressions and translate them to regular LPeg patterns.)

@@ -164,7 +166,7 @@

lpeg.match (pattern, subject [, init]) An optional numeric argument init makes the match start at that position in the subject string. -As usual in Lua libraries, +As in the Lua standard libraries, a negative value counts from the end.

@@ -188,9 +190,9 @@

lpeg.type (value)

Otherwise returns nil.

-

lpeg.version ()

+

lpeg.version

-Returns a string with the running version of LPeg. +A string (not a function) with the running version of LPeg.

lpeg.setmaxstack (max)

@@ -329,6 +331,15 @@

lpeg.S (string)

+

lpeg.utfR (cp1, cp2)

+

+Returns a pattern that matches a valid UTF-8 byte sequence +representing a code point in the range [cp1, cp2]. +The range is limited by the natural Unicode limit of 0x10FFFF, +but may include surrogates. +

+ +

lpeg.V (v)

This operation creates a non-terminal (a variable) @@ -433,7 +444,8 @@

patt1 + patt2

patt1 - patt2

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

@@ -597,17 +609,17 @@

Captures

lpeg.Carg(n) the value of the nth extra argument to lpeg.match (matches the empty string) -lpeg.Cb(name) +lpeg.Cb(key) the values produced by the previous - group capture named name + group capture named key (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]) + folding capture (deprecated) +lpeg.Cg(patt [, key]) the values produced by patt, - optionally tagged with name + optionally tagged with key lpeg.Cp() the current position (matches the empty string) lpeg.Cs(patt) @@ -627,6 +639,11 @@

Captures

patt / function the returns of function applied to the captures of patt +patt % function + produces no value; + it accummulates the captures from patt + into the previous capture through function + lpeg.Cmt(patt, function) the returns of function applied to the captures of patt; the application is done at match time @@ -652,10 +669,10 @@

Captures

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.) +it is not specified whether LPeg will call func.) Therefore, captures should avoid side effects. Moreover, -most captures cannot affect the way a pattern matches a subject. +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, @@ -684,24 +701,25 @@

lpeg.Carg (n)

-

lpeg.Cb (name)

+

lpeg.Cb (key)

Creates a back capture. This pattern matches the empty string and produces the values produced by the most recent -group capture named name -(where name can be any Lua value). +group capture named key +(where key can be any Lua value).

Most recent means the last complete outermost -group capture with the given name. +group capture with the given key. A Complete capture means that the entire pattern -corresponding to the capture has matched. +corresponding to the capture has matched; +in other words, the back capture is not nested inside the group. An Outermost capture means that the capture is not inside -another complete capture. +another complete capture that does not contain the back capture itself.

@@ -722,61 +740,21 @@

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

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
-
+This construction is deprecated; +use an accumulator pattern instead. +In general, a fold like +lpeg.Cf(p1 * p2^0, func) +can be translated to +(p1 * (p2 % func)^0). -

lpeg.Cg (patt [, name])

+

lpeg.Cg (patt [, key])

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 +The group may be anonymous (if no key is given) +or named with the given key (which can be any non-nil Lua value).

@@ -822,7 +800,7 @@

lpeg.Ct (patt)

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. +with the group key as its key. The captured value is only the table.

@@ -878,6 +856,98 @@

patt / function

+

patt % function

+

+Creates an accumulator capture. +This pattern behaves similarly to a +function capture, +with the following differences: +The last captured value before patt +is added as a first argument to the call; +the return of the function is adjusted to one single value; +that value replaces the last captured value. +Note that the capture itself produces no values; +it only changes the value of its previous capture. +

+ +

+As an example, +let us consider the problem of adding a list of numbers. +

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

+First, the initial number captures a number; +that first capture will play the role of an accumulator. +Then, each time the sequence comma-number +matches inside the loop there is an accumulator capture: +It calls add with the current value of the +accumulator—which is the last captured value, created by the +first number— and the value of the new number, +and the result of the call (the sum of the two numbers) +replaces the value of the accumulator. +At the end of the match, +the accumulator with all sums is the final value. +

+ +

+As another example, +consider the following code fragment: +

+
+local name = lpeg.C(lpeg.R("az")^1)
+local p = name * (lpeg.P("^") % string.upper)^-1
+print(p:match("count"))    --> count
+print(p:match("count^"))   --> COUNT
+
+

+In the match against "count", +as there is no "^", +the optional accumulator capture does not match; +so, the match results in its sole capture, a name. +In the match against "count^", +the accumulator capture matches, +so the function string.upper +is called with the previous captured value (created by name) +plus the string "^"; +the function ignores its second argument and returns the first argument +changed to upper case; +that value then becomes the first and only +capture value created by the match. +

+ +

+Due to the nature of this capture, +you should avoid using it in places where it is not clear +what is the "previous" capture, +such as directly nested in a string capture +or a numbered capture. +(Note that these captures may not need to evaluate +all their subcaptures to compute their results.) +Moreover, due to implementation details, +you should not use this capture directly nested in a +substitution capture. +You should also avoid a direct nesting of this capture inside +a folding capture (deprecated), +as the folding will try to fold each individual accumulator capture. +A simple and effective way to avoid all these issues is +to enclose the whole accumulation composition +(including the capture that generates the initial value) +into an anonymous group capture. +

+ +

lpeg.Cmt(patt, function)

Creates a match-time capture. @@ -930,9 +1000,9 @@

Using a Pattern

-- 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 +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 @@ -957,19 +1027,18 @@

Name-value lists

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" } +local pair = name * "=" * space * name * sep^-1 +local list = lpeg.Ct("") * (pair % rawset)^0 +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. +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 +then for each 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. @@ -1003,7 +1072,7 @@

Splitting a string

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, +To avoid this problem, we can collect these values in a table:

@@ -1039,7 +1108,7 @@ 

Searching for a pattern

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

@@ -1048,27 +1117,27 @@

Searching for a pattern

we can add position captures to the pattern:

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

Another option for the search is like this:

-local I = lpeg.Cp()
+local Cp = lpeg.Cp()
 function anywhere (p)
-  return (1 - lpeg.P(p))^0 * I * p * I
+  return (1 - lpeg.P(p))^0 * Cp * p * Cp
 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). +and then matches p plus appropriate captures.

@@ -1163,91 +1232,6 @@

Comma-Separated Values (CSV)

-

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 [=*[ @@ -1347,7 +1331,7 @@

Arithmetic expressions

end -- small example -print(evalExp"3 + 5*9 / (1+1) - 12") --> 13.5 +print(evalExp"3 + 5*9 / (1+1) - 12") --> 13.5

@@ -1369,16 +1353,16 @@

Arithmetic expressions

-- 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); + Exp = V"Term" * (TermOp * V"Term" % eval)^0; + Term = V"Factor" * (FactorOp * V"Factor" % eval)^0; Factor = Number / tonumber + Open * V"Exp" * Close; } -- small example -print(lpeg.match(G, "3 + 5*9 / (1+1) - 12")) --> 13.5 +print(lpeg.match(G, "3 + 5*9 / (1+1) - 12")) --> 13.5

-Note the use of the fold (accumulator) capture. +Note the use of the accumulator capture. To compute the value of an expression, the accumulator starts with the value of the first term, and then applies eval over @@ -1391,13 +1375,20 @@

Arithmetic expressions

Download

LPeg -source code.

+source code.

+ +

+Probably, the easiest way to install LPeg is with +LuaRocks. +If you have LuaRocks installed, +the following command is all you need to install LPeg: +

$ luarocks install lpeg

License

-Copyright © 2007-2019 Lua.org, PUC-Rio. +Copyright © 2007-2023 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, diff --git a/3rd/lpeg/lpprint.c b/3rd/lpeg/lpprint.c index df62cbeed..da902e6f7 100644 --- a/3rd/lpeg/lpprint.c +++ b/3rd/lpeg/lpprint.c @@ -1,7 +1,3 @@ -/* -** $Id: lpprint.c $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ #include #include @@ -27,7 +23,7 @@ void printcharset (const byte *st) { printf("["); for (i = 0; i <= UCHAR_MAX; i++) { int first = i; - while (testchar(st, i) && i <= UCHAR_MAX) i++; + while (i <= UCHAR_MAX && testchar(st, i)) i++; if (i - 1 == first) /* unary range? */ printf("(%02x)", first); else if (i - 1 > first) /* non-empty range? */ @@ -37,10 +33,34 @@ void printcharset (const byte *st) { } +static void printIcharset (const Instruction *inst, const byte *buff) { + byte cs[CHARSETSIZE]; + int i; + printf("(%02x-%d) ", inst->i.aux2.set.offset, inst->i.aux2.set.size); + clearset(cs); + for (i = 0; i < CHARSETSIZE * 8; i++) { + if (charinset(inst, buff, i)) + setchar(cs, i); + } + printcharset(cs); +} + + +static void printTcharset (TTree *tree) { + byte cs[CHARSETSIZE]; + int i; + printf("(%02x-%d) ", tree->u.set.offset, tree->u.set.size); + fillset(cs, tree->u.set.deflt); + for (i = 0; i < tree->u.set.size; i++) + cs[tree->u.set.offset + i] = treebuffer(tree)[i]; + printcharset(cs); +} + + static const char *capkind (int kind) { const char *const modes[] = { "close", "position", "constant", "backref", - "argument", "simple", "table", "function", + "argument", "simple", "table", "function", "accumulator", "query", "string", "num", "substitution", "fold", "runtime", "group"}; return modes[kind]; @@ -56,41 +76,46 @@ void printinst (const Instruction *op, const Instruction *p) { const char *const names[] = { "any", "char", "set", "testany", "testchar", "testset", - "span", "behind", + "span", "utf-range", "behind", "ret", "end", "choice", "jmp", "call", "open_call", "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", - "fullcapture", "opencapture", "closecapture", "closeruntime" + "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); + printf("'%c' (%02x)", p->i.aux1, p->i.aux1); break; } case ITestChar: { - printf("'%c'", p->i.aux); printjmp(op, p); + printf("'%c' (%02x)", p->i.aux1, p->i.aux1); printjmp(op, p); + break; + } + case IUTFR: { + printf("%d - %d", p[1].offset, utf_to(p)); break; } case IFullCapture: { printf("%s (size = %d) (idx = %d)", - capkind(getkind(p)), getoff(p), p->i.key); + capkind(getkind(p)), getoff(p), p->i.aux2.key); break; } case IOpenCapture: { - printf("%s (idx = %d)", capkind(getkind(p)), p->i.key); + printf("%s (idx = %d)", capkind(getkind(p)), p->i.aux2.key); break; } case ISet: { - printcharset((p+1)->buff); + printIcharset(p, (p+1)->buff); break; } case ITestSet: { - printcharset((p+2)->buff); printjmp(op, p); + printIcharset(p, (p+2)->buff); printjmp(op, p); break; } case ISpan: { - printcharset((p+1)->buff); + printIcharset(p, (p+1)->buff); break; } case IOpenCall: { @@ -98,7 +123,7 @@ void printinst (const Instruction *op, const Instruction *p) { break; } case IBehind: { - printf("%d", p->i.aux); + printf("%d", p->i.aux1); break; } case IJmp: case ICall: case ICommit: case IChoice: @@ -112,8 +137,9 @@ void printinst (const Instruction *op, const Instruction *p) { } -void printpatt (Instruction *p, int n) { +void printpatt (Instruction *p) { Instruction *op = p; + uint n = op[-1].codesize - 1; while (p < op + n) { printinst(op, p); p += sizei(p); @@ -121,20 +147,39 @@ void printpatt (Instruction *p, int n) { } -#if defined(LPEG_DEBUG) -static void printcap (Capture *cap) { - printf("%s (idx: %d - size: %d) -> %p\n", - capkind(cap->kind), cap->idx, cap->siz, cap->s); +static void printcap (Capture *cap, int ident) { + while (ident--) printf(" "); + printf("%s (idx: %d - size: %d) -> %lu (%p)\n", + capkind(cap->kind), cap->idx, cap->siz, (long)cap->index, (void*)cap); +} + + +/* +** Print a capture and its nested captures +*/ +static Capture *printcap2close (Capture *cap, int ident) { + Capture *head = cap++; + printcap(head, ident); /* print head capture */ + while (capinside(head, cap)) + cap = printcap2close(cap, ident + 2); /* print nested captures */ + if (isopencap(head)) { + assert(isclosecap(cap)); + printcap(cap++, ident); /* print and skip close capture */ + } + return cap; } -void printcaplist (Capture *cap, Capture *limit) { +void printcaplist (Capture *cap) { + { /* for debugging, print first a raw list of captures */ + Capture *c = cap; + while (c->index != MAXINDT) { printcap(c, 0); c++; } + } printf(">======\n"); - for (; cap->s && (limit == NULL || cap < limit); cap++) - printcap(cap); + while (!isclosecap(cap)) + cap = printcap2close(cap, 0); printf("=======\n"); } -#endif /* }====================================================== */ @@ -147,11 +192,11 @@ void printcaplist (Capture *cap, Capture *limit) { static const char *tagnames[] = { "char", "set", "any", - "true", "false", + "true", "false", "utf8.range", "rep", "seq", "choice", "not", "and", - "call", "opencall", "rule", "grammar", + "call", "opencall", "rule", "xinfo", "grammar", "behind", "capture", "run-time" }; @@ -159,6 +204,7 @@ static const char *tagnames[] = { void printtree (TTree *tree, int ident) { int i; + int sibs = numsiblings[tree->tag]; for (i = 0; i < ident; i++) printf(" "); printf("%s", tagnames[tree->tag]); switch (tree->tag) { @@ -171,29 +217,38 @@ void printtree (TTree *tree, int ident) { break; } case TSet: { - printcharset(treebuffer(tree)); + printTcharset(tree); printf("\n"); break; } + case TUTFR: { + assert(sib1(tree)->tag == TXInfo); + printf(" %d (%02x %d) - %d (%02x %d) \n", + tree->u.n, tree->key, tree->cap, + sib1(tree)->u.n, sib1(tree)->key, sib1(tree)->cap); + break; + } case TOpenCall: case TCall: { - assert(sib2(tree)->tag == TRule); - printf(" key: %d (rule: %d)\n", tree->key, sib2(tree)->cap); + assert(sib1(sib2(tree))->tag == TXInfo); + printf(" key: %d (rule: %d)\n", tree->key, sib1(sib2(tree))->u.n); break; } case TBehind: { printf(" %d\n", tree->u.n); - printtree(sib1(tree), ident + 2); break; } case TCapture: { printf(" kind: '%s' key: %d\n", capkind(tree->cap), tree->key); - 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 */ + printf(" key: %d\n", tree->key); + sibs = 1; /* do not print 'sib2' (next rule) as a sibling */ + break; + } + case TXInfo: { + printf(" n: %d\n", tree->u.n); + break; } case TGrammar: { TTree *rule = sib1(tree); @@ -203,18 +258,17 @@ void printtree (TTree *tree, int ident) { rule = sib2(rule); } assert(rule->tag == TTrue); /* sentinel */ + sibs = 0; /* siblings already handled */ break; } - default: { - int sibs = numsiblings[tree->tag]; + default: printf("\n"); - if (sibs >= 1) { - printtree(sib1(tree), ident + 2); - if (sibs >= 2) - printtree(sib2(tree), ident + 2); - } break; - } + } + if (sibs >= 1) { + printtree(sib1(tree), ident + 2); + if (sibs >= 2) + printtree(sib2(tree), ident + 2); } } diff --git a/3rd/lpeg/lpprint.h b/3rd/lpeg/lpprint.h index 15ef121d7..e8e04e872 100644 --- a/3rd/lpeg/lpprint.h +++ b/3rd/lpeg/lpprint.h @@ -1,7 +1,3 @@ -/* -** $Id: lpprint.h $ -*/ - #if !defined(lpprint_h) #define lpprint_h @@ -13,11 +9,11 @@ #if defined(LPEG_DEBUG) -void printpatt (Instruction *p, int n); +void printpatt (Instruction *p); 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 printcaplist (Capture *cap); void printinst (const Instruction *op, const Instruction *p); #else @@ -26,7 +22,7 @@ void printinst (const Instruction *op, const Instruction *p); 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) \ +#define printpatt(p) \ luaL_error(L, "function only implemented in debug mode") #endif diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index 5c8de9477..475b0c36b 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -1,7 +1,3 @@ -/* -** $Id: lptree.c $ -** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ #include #include @@ -16,16 +12,17 @@ #include "lpcode.h" #include "lpprint.h" #include "lptree.h" +#include "lpcset.h" /* number of siblings for each tree */ const byte numsiblings[] = { 0, 0, 0, /* char, set, any */ - 0, 0, /* true, false */ - 1, /* rep */ + 0, 0, 0, /* true, false, utf-range */ + 1, /* acc */ 2, 2, /* seq, choice */ 1, 1, /* not, and */ - 0, 0, 2, 1, /* call, opencall, rule, grammar */ + 0, 0, 2, 1, 1, /* call, opencall, rule, prerule, grammar */ 1, /* behind */ 1, 1 /* capture, runtime capture */ }; @@ -338,7 +335,7 @@ static Pattern *getpattern (lua_State *L, int idx) { static int getsize (lua_State *L, int idx) { - return (lua_rawlen(L, idx) - sizeof(Pattern)) / sizeof(TTree) + 1; + return (lua_rawlen(L, idx) - offsetof(Pattern, tree)) / sizeof(TTree); } @@ -351,18 +348,18 @@ static TTree *gettree (lua_State *L, int idx, int *len) { /* -** 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.) +** create a pattern followed by a tree with 'len' nodes. 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); + size_t size = offsetof(Pattern, tree) + len * sizeof(TTree); 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; + p->code = NULL; return p->tree; } @@ -374,17 +371,44 @@ static TTree *newleaf (lua_State *L, int tag) { } -static TTree *newcharset (lua_State *L) { - TTree *tree = newtree(L, bytes2slots(CHARSETSIZE) + 1); - tree->tag = TSet; - loopset(i, treebuffer(tree)[i] = 0); - return tree; +/* +** Create a tree for a charset, optimizing for special cases: empty set, +** full set, and singleton set. +*/ +static TTree *newcharset (lua_State *L, byte *cs) { + charsetinfo info; + Opcode op = charsettype(cs, &info); + switch (op) { + case IFail: return newleaf(L, TFalse); /* empty set */ + case IAny: return newleaf(L, TAny); /* full set */ + case IChar: { /* singleton set */ + TTree *tree =newleaf(L, TChar); + tree->u.n = info.offset; + return tree; + } + default: { /* regular set */ + int i; + int bsize = /* tree size in bytes */ + (int)offsetof(TTree, u.set.bitmap) + info.size; + TTree *tree = newtree(L, bytes2slots(bsize)); + assert(op == ISet); + tree->tag = TSet; + tree->u.set.offset = info.offset; + tree->u.set.size = info.size; + tree->u.set.deflt = info.deflt; + for (i = 0; i < info.size; i++) { + assert(&treebuffer(tree)[i] < (byte*)tree + bsize); + treebuffer(tree)[i] = cs[info.offset + i]; + } + return tree; + } + } } /* -** add to tree a sequence where first sibling is 'sib' (with size -** 'sibsize'); returns position for second sibling +** Add to tree a sequence where first sibling is 'sib' (with size +** 'sibsize'); return position for second sibling. */ static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { tree->tag = TSeq; tree->u.ps = sibsize + 1; @@ -553,8 +577,8 @@ static int lp_choice (lua_State *L) { 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]); + loopset(i, st1.cs[i] |= st2.cs[i]); + newcharset(L, st1.cs); } else if (nofail(t1) || t2->tag == TFalse) lua_pushvalue(L, 1); /* true / x => true, x / false => x */ @@ -630,8 +654,8 @@ static int lp_sub (lua_State *L) { 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]); + loopset(i, st1.cs[i] &= ~st2.cs[i]); + newcharset(L, st1.cs); } else { TTree *tree = newtree(L, 2 + s1 + s2); @@ -649,11 +673,13 @@ static int lp_sub (lua_State *L) { static int lp_set (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - TTree *tree = newcharset(L); + byte buff[CHARSETSIZE]; + clearset(buff); while (l--) { - setchar(treebuffer(tree), (byte)(*s)); + setchar(buff, (byte)(*s)); s++; } + newcharset(L, buff); return 1; } @@ -661,14 +687,68 @@ static int lp_set (lua_State *L) { static int lp_range (lua_State *L) { int arg; int top = lua_gettop(L); - TTree *tree = newcharset(L); + byte buff[CHARSETSIZE]; + clearset(buff); 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); + setchar(buff, c); + } + newcharset(L, buff); + return 1; +} + + +/* +** Fills a tree node with basic information about the UTF-8 code point +** 'cpu': its value in 'n', its length in 'cap', and its first byte in +** 'key' +*/ +static void codeutftree (lua_State *L, TTree *t, lua_Unsigned cpu, int arg) { + int len, fb, cp; + cp = (int)cpu; + if (cp <= 0x7f) { /* one byte? */ + len = 1; + fb = cp; + } else if (cp <= 0x7ff) { + len = 2; + fb = 0xC0 | (cp >> 6); + } else if (cp <= 0xffff) { + len = 3; + fb = 0xE0 | (cp >> 12); + } + else { + luaL_argcheck(L, cpu <= 0x10ffffu, arg, "invalid code point"); + len = 4; + fb = 0xF0 | (cp >> 18); + } + t->u.n = cp; + t->cap = len; + t->key = fb; +} + + +static int lp_utfr (lua_State *L) { + lua_Unsigned from = (lua_Unsigned)luaL_checkinteger(L, 1); + lua_Unsigned to = (lua_Unsigned)luaL_checkinteger(L, 2); + luaL_argcheck(L, from <= to, 2, "empty range"); + if (to <= 0x7f) { /* ascii range? */ + uint f; + byte buff[CHARSETSIZE]; /* code it as a regular charset */ + clearset(buff); + for (f = (int)from; f <= to; f++) + setchar(buff, f); + newcharset(L, buff); + } + else { /* multi-byte utf-8 range */ + TTree *tree = newtree(L, 2); + tree->tag = TUTFR; + codeutftree(L, tree, from, 1); + sib1(tree)->tag = TXInfo; + codeutftree(L, sib1(tree), to, 2); } return 1; } @@ -763,11 +843,18 @@ static int lp_divcapture (lua_State *L) { tree->key = n; return 1; } - default: return luaL_argerror(L, 2, "invalid replacement value"); + default: + return luaL_error(L, "unexpected %s as 2nd operand to LPeg '/'", + luaL_typename(L, 2)); } } +static int lp_acccapture (lua_State *L) { + return capture_aux(L, Cacc, 2); +} + + static int lp_substcapture (lua_State *L) { return capture_aux(L, Csubst, 0); } @@ -906,7 +993,7 @@ static int collectrules (lua_State *L, int arg, int *totalsize) { 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 */ + size = 3 + getsize(L, postab + 2); /* TGrammar + TRule + TXInfo + rule */ lua_pushnil(L); /* prepare to traverse grammar table */ while (lua_next(L, arg) != 0) { if (lua_tonumber(L, -2) == 1 || @@ -920,11 +1007,11 @@ static int collectrules (lua_State *L, int arg, int *totalsize) { 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 */ + size += 2 + getsize(L, -1); /* add 'TRule + TXInfo + rule' to size */ lua_pushvalue(L, -2); /* push key (for next lua_next) */ n++; } - *totalsize = size + 1; /* TTrue to finish list of rules */ + *totalsize = size + 1; /* space for 'TTrue' finishing list of rules */ return n; } @@ -936,11 +1023,13 @@ static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { int ridx = frule + 2*i + 1; /* index of i-th rule */ int rulesize; TTree *rn = gettree(L, ridx, &rulesize); + TTree *pr = sib1(nd); /* points to rule's prerule */ nd->tag = TRule; 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 */ + pr->tag = TXInfo; + pr->u.n = i; /* rule number */ + nd->u.ps = rulesize + 2; /* point to next rule */ + memcpy(sib1(pr), rn, rulesize * sizeof(TTree)); /* copy rule */ mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ nd = sib2(nd); /* move to next rule */ } @@ -976,7 +1065,7 @@ static int checkloops (TTree *tree) { ** 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) { +static int verifyerror (lua_State *L, unsigned short *passed, int npassed) { int i, j; for (i = npassed - 1; i >= 0; i--) { /* search for a repetition */ for (j = i - 1; j >= 0; j--) { @@ -1001,12 +1090,12 @@ static int verifyerror (lua_State *L, int *passed, int 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, - int nb) { +static int verifyrule (lua_State *L, TTree *tree, unsigned short *passed, + int npassed, int nb) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: - case TFalse: + case TFalse: case TUTFR: return nb; /* cannot pass from here */ case TTrue: case TBehind: /* look-behind cannot have calls */ @@ -1014,7 +1103,7 @@ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, case TNot: case TAnd: case TRep: /* return verifyrule(L, sib1(tree), passed, npassed, 1); */ tree = sib1(tree); nb = 1; goto tailcall; - case TCapture: case TRunTime: + case TCapture: case TRunTime: case TXInfo: /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ tree = sib1(tree); goto tailcall; case TCall: @@ -1030,10 +1119,10 @@ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TRule: - if (npassed >= MAXRULES) - return verifyerror(L, passed, npassed); + if (npassed >= MAXRULES) /* too many steps? */ + return verifyerror(L, passed, npassed); /* error */ else { - passed[npassed++] = tree->key; + passed[npassed++] = tree->key; /* add rule to path */ /* return verifyrule(L, sib1(tree), passed, npassed); */ tree = sib1(tree); goto tailcall; } @@ -1045,7 +1134,7 @@ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, static void verifygrammar (lua_State *L, TTree *grammar) { - int passed[MAXRULES]; + unsigned short passed[MAXRULES]; TTree *rule; /* check left-recursive rules */ for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { @@ -1105,7 +1194,7 @@ static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { 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); + return compile(L, p, getsize(L, idx)); } @@ -1128,7 +1217,7 @@ static int lp_printcode (lua_State *L) { printktable(L, 1); if (p->code == NULL) /* not compiled yet? */ prepcompile(L, p, 1); - printpatt(p->code, p->codesize); + printpatt(p->code); return 0; } @@ -1164,9 +1253,10 @@ static int lp_match (lua_State *L) { const char *s = luaL_checklstring(L, SUBJIDX, &l); size_t i = initposition(L, l); int ptop = lua_gettop(L); + luaL_argcheck(L, l < MAXINDT, SUBJIDX, "subject too long"); lua_pushnil(L); /* initialize subscache */ lua_pushlightuserdata(L, capture); /* initialize caplistidx */ - lua_getuservalue(L, 1); /* initialize penvidx */ + lua_getuservalue(L, 1); /* initialize ktableidx */ r = match(L, s, s + i, s + l, code, capture, ptop); if (r == NULL) { lua_pushnil(L); @@ -1195,12 +1285,6 @@ static int lp_setmax (lua_State *L) { } -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"); @@ -1212,16 +1296,22 @@ static int lp_type (lua_State *L) { int lp_gc (lua_State *L) { Pattern *p = getpattern(L, 1); - realloccode(L, p, 0); /* delete code block */ + freecode(L, p); /* delete code block */ return 0; } +/* +** Create a charset representing a category of characters, given by +** the predicate 'catf'. +*/ 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); + int c; + byte buff[CHARSETSIZE]; + clearset(buff); + for (c = 0; c <= UCHAR_MAX; c++) + if (catf(c)) setchar(buff, c); + newcharset(L, buff); lua_setfield(L, -2, catname); } @@ -1269,8 +1359,9 @@ static struct luaL_Reg pattreg[] = { {"P", lp_P}, {"S", lp_set}, {"R", lp_range}, + {"utfR", lp_utfr}, {"locale", lp_locale}, - {"version", lp_version}, + {"version", NULL}, {"setmaxstack", lp_setmax}, {"type", lp_type}, {NULL, NULL} @@ -1284,6 +1375,7 @@ static struct luaL_Reg metareg[] = { {"__gc", lp_gc}, {"__len", lp_and}, {"__div", lp_divcapture}, + {"__mod", lp_acccapture}, {"__unm", lp_not}, {"__sub", lp_sub}, {NULL, NULL} @@ -1299,6 +1391,8 @@ int luaopen_lpeg (lua_State *L) { luaL_newlib(L, pattreg); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); + lua_pushliteral(L, "LPeg " VERSION); + lua_setfield(L, -2, "version"); return 1; } diff --git a/3rd/lpeg/lptree.h b/3rd/lpeg/lptree.h index 25906d5f4..c7887412b 100644 --- a/3rd/lpeg/lptree.h +++ b/3rd/lpeg/lptree.h @@ -1,12 +1,9 @@ -/* -** $Id: lptree.h $ -*/ #if !defined(lptree_h) #define lptree_h -#include "lptypes.h" +#include "lptypes.h" /* @@ -14,10 +11,13 @@ */ typedef enum TTag { TChar = 0, /* 'n' = char */ - TSet, /* the set is stored in next CHARSETSIZE bytes */ + TSet, /* the set is encoded in 'u.set' and the next 'u.set.size' bytes */ TAny, TTrue, TFalse, + TUTFR, /* range of UTF-8 codepoints; 'n' has initial codepoint; + 'cap' has length; 'key' has first byte; + extra info is similar for end codepoint */ TRep, /* 'sib1'* */ TSeq, /* 'sib1' 'sib2' */ TChoice, /* 'sib1' / 'sib2' */ @@ -26,8 +26,9 @@ typedef enum TTag { 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 */ + 'sib1' is rule's pattern pre-rule; 'sib2' is next rule; + extra info 'n' is rule's sequential number */ + TXInfo, /* extra info */ 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'); @@ -51,17 +52,26 @@ typedef struct TTree { union { int ps; /* occasional second child */ int n; /* occasional counter */ + struct { + byte offset; /* compact set offset (in bytes) */ + byte size; /* compact set size (in bytes) */ + byte deflt; /* default value */ + byte bitmap[1]; /* bitmap (open array) */ + } set; /* for compact sets */ } u; } TTree; +/* access to charset */ +#define treebuffer(t) ((t)->u.set.bitmap) + + /* ** 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; diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index 1d9d59f6b..3f860b973 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -1,7 +1,6 @@ /* -** $Id: lptypes.h $ ** LPeg - PEG pattern matching for Lua -** Copyright 2007-2019, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ @@ -11,11 +10,12 @@ #include #include +#include #include "lua.h" -#define VERSION "1.0.2" +#define VERSION "1.1.0" #define PATTERN_T "lpeg-pattern" @@ -37,6 +37,8 @@ #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) +typedef size_t lua_Unsigned; + #endif @@ -51,9 +53,9 @@ #endif -/* maximum number of rules in a grammar (limited by 'unsigned char') */ +/* maximum number of rules in a grammar (limited by 'unsigned short') */ #if !defined(MAXRULES) -#define MAXRULES 250 +#define MAXRULES 1000 #endif @@ -81,6 +83,8 @@ typedef unsigned char byte; +typedef unsigned int uint; + #define BITSPERCHAR 8 @@ -96,11 +100,11 @@ typedef struct Charset { #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } -/* access to charset */ -#define treebuffer(t) ((byte *)((t) + 1)) +#define fillset(s,c) memset(s,c,CHARSETSIZE) +#define clearset(s) fillset(s,0) /* number of slots needed for 'n' bytes */ -#define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1) +#define bytes2slots(n) (((n) - 1u) / (uint)sizeof(TTree) + 1u) /* set 'b' bit in charset 'cs' */ #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) @@ -110,8 +114,8 @@ typedef struct Charset { ** 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 getkind(op) ((op)->i.aux1 & 0xF) +#define getoff(op) (((op)->i.aux1 >> 4) & 0xF) #define joinkindoff(k,o) ((k) | ((o) << 4)) #define MAXOFF 0xF @@ -126,19 +130,20 @@ typedef struct Charset { #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 instructions) for l bytes (l > 0) */ +#define instsize(l) ((int)(((l) + (uint)sizeof(Instruction) - 1u) \ + / (uint)sizeof(Instruction))) /* size (in elements) for a ISet instruction */ -#define CHARSETINSTSIZE instsize(CHARSETSIZE) +#define CHARSETINSTSIZE (1 + 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)))) +#define testchar(st,c) ((((uint)(st)[((c) >> 3)]) >> ((c) & 7)) & 1) #endif diff --git a/3rd/lpeg/lpvm.c b/3rd/lpeg/lpvm.c index 737418c47..0a2fde4f5 100644 --- a/3rd/lpeg/lpvm.c +++ b/3rd/lpeg/lpvm.c @@ -1,7 +1,3 @@ -/* -** $Id: lpvm.c $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ #include #include @@ -24,7 +20,46 @@ #define getoffset(p) (((p) + 1)->offset) -static const Instruction giveup = {{IGiveup, 0, 0}}; +static const Instruction giveup = {{IGiveup, 0, {0}}}; + + +int charinset (const Instruction *i, const byte *buff, uint c) { + c -= i->i.aux2.set.offset; + if (c >= ((uint)i->i.aux2.set.size /* size in instructions... */ + * (uint)sizeof(Instruction) /* in bytes... */ + * 8u)) /* in bits */ + return i->i.aux1; /* out of range; return default value */ + return testchar(buff, c); +} + + +/* +** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. +*/ +static const char *utf8_decode (const char *o, int *val) { + static const uint limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFFu}; + const unsigned char *s = (const unsigned char *)o; + uint c = s[0]; /* first byte */ + uint 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 > 0x10FFFFu || res <= limits[count]) + return NULL; /* invalid byte sequence */ + s += count; /* skip continuation bytes read */ + } + *val = res; + return (const char *)s + 1; /* +1 to include first byte */ +} /* @@ -51,16 +86,25 @@ typedef struct Stack { ** array, it is simpler to ensure the array always has at least one free ** slot upfront and check its size later.) */ + +/* new size in number of elements cannot overflow integers, and new + size in bytes cannot overflow size_t. */ +#define MAXNEWSIZE \ + (((size_t)INT_MAX) <= (~(size_t)0 / sizeof(Capture)) ? \ + ((size_t)INT_MAX) : (~(size_t)0 / sizeof(Capture))) + static Capture *growcap (lua_State *L, Capture *capture, int *capsize, int captop, int n, int ptop) { if (*capsize - captop > n) return capture; /* no need to grow array */ else { /* must grow */ Capture *newc; - int newsize = captop + n + 1; /* minimum size needed */ - if (newsize < INT_MAX/((int)sizeof(Capture) * 2)) - newsize *= 2; /* twice that size, if not too big */ - else if (newsize >= INT_MAX/((int)sizeof(Capture))) + uint newsize = captop + n + 1; /* minimum size needed */ + if (newsize < (MAXNEWSIZE / 3) * 2) + newsize += newsize / 2; /* 1.5 that size, if not too big */ + else if (newsize < (MAXNEWSIZE / 9) * 8) + newsize += newsize / 8; /* else, try 9/8 that size */ + else luaL_error(L, "too many captures"); newc = (Capture *)lua_newuserdata(L, newsize * sizeof(Capture)); memcpy(newc, capture, captop * sizeof(Capture)); @@ -125,7 +169,7 @@ static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { ** value, 'n' is the number of values (at least 1). The open group ** capture is already in 'capture', before the place for the new entries. */ -static void adddyncaptures (const char *s, Capture *capture, int n, int fd) { +static void adddyncaptures (Index_t index, Capture *capture, int n, int fd) { int i; assert(capture[-1].kind == Cgroup && capture[-1].siz == 0); capture[-1].idx = 0; /* make group capture an anonymous group */ @@ -133,11 +177,11 @@ static void adddyncaptures (const char *s, Capture *capture, int n, int fd) { capture[i].kind = Cruntime; capture[i].siz = 1; /* mark it as closed */ capture[i].idx = fd + i; /* stack index of capture value */ - capture[i].s = s; + capture[i].index = index; } capture[n].kind = Cclose; /* close group */ capture[n].siz = 1; - capture[n].s = s; + capture[n].index = index; } @@ -154,6 +198,32 @@ static int removedyncap (lua_State *L, Capture *capture, } +/* +** Find the corresponding 'open' capture before 'cap', when that capture +** can become a full capture. If a full capture c1 is followed by an +** empty capture c2, there is no way to know whether c2 is inside +** c1. So, full captures can enclose only captures that start *before* +** its end. +*/ +static Capture *findopen (Capture *cap, Index_t currindex) { + int i; + cap--; /* check last capture */ + /* Must it be inside current one, but starts where current one ends? */ + if (!isopencap(cap) && cap->index == currindex) + return NULL; /* current one cannot be a full capture */ + /* else, look for an 'open' capture */ + for (i = 0; i < MAXLOP; i++, cap--) { + if (currindex - cap->index >= UCHAR_MAX) + return NULL; /* capture too long for a full capture */ + else if (isopencap(cap)) /* open capture? */ + return cap; /* that's the one to be closed */ + else if (cap->kind == Cclose) + return NULL; /* a full capture should not nest a non-full one */ + } + return NULL; /* not found within allowed search limit */ +} + + /* ** Opcode interpreter */ @@ -181,7 +251,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IEnd: { assert(stack == getstackbase(L, ptop) + 1); capture[captop].kind = Cclose; - capture[captop].s = NULL; + capture[captop].index = MAXINDT; return s; } case IGiveup: { @@ -198,47 +268,58 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, else goto fail; continue; } + case IUTFR: { + int codepoint; + if (s >= e) + goto fail; + s = utf8_decode (s, &codepoint); + if (s && p[1].offset <= codepoint && codepoint <= utf_to(p)) + p += 2; + 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++; } + if ((byte)*s == p->i.aux1 && s < e) { p++; s++; } else goto fail; continue; } case ITestChar: { - if ((byte)*s == p->i.aux && s < e) p += 2; + if ((byte)*s == p->i.aux1 && 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++; } + uint c = (byte)*s; + if (charinset(p, (p+1)->buff, c) && s < e) + { p += 1 + p->i.aux2.set.size; s++; } else goto fail; continue; } case ITestSet: { - int c = (byte)*s; - if (testchar((p + 2)->buff, c) && s < e) - p += 1 + CHARSETINSTSIZE; + uint c = (byte)*s; + if (charinset(p, (p + 2)->buff, c) && s < e) + p += 2 + p->i.aux2.set.size; else p += getoffset(p); continue; } case IBehind: { - int n = p->i.aux; + int n = p->i.aux1; 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; + uint c = (byte)*s; + if (!charinset(p, (p+1)->buff, c)) break; } - p += CHARSETINSTSIZE; + p += 1 + p->i.aux2.set.size; continue; } case IJmp: { @@ -280,6 +361,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IBackCommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); s = (--stack)->s; + if (ndyncap > 0) /* are there matchtime captures? */ + ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; p += getoffset(p); continue; @@ -287,7 +370,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IFailTwice: assert(stack > getstackbase(L, ptop)); stack--; - /* go through */ + /* FALLTHROUGH */ case IFail: fail: { /* pattern failed: try to backtrack */ do { /* remove pending calls */ @@ -326,38 +409,36 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, luaL_error(L, "too many results in match-time capture"); /* add new captures + close group to 'capture' list */ capture = growcap(L, capture, &capsize, captop, n + 1, ptop); - adddyncaptures(s, capture + captop, n, fr); + adddyncaptures(s - o, capture + captop, n, fr); captop += n + 1; /* new captures + close group */ } p++; continue; } case ICloseCapture: { - const char *s1 = s; + Capture *open = findopen(capture + captop, s - o); 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; + if (open) { /* if possible, turn capture into a full capture */ + open->siz = (s - o) - open->index + 1; p++; continue; } - else { + else { /* must create a close capture */ capture[captop].siz = 1; /* mark entry as closed */ - capture[captop].s = s; + capture[captop].index = s - o; goto pushcapture; } } case IOpenCapture: capture[captop].siz = 0; /* mark entry as open */ - capture[captop].s = s; + capture[captop].index = s - o; goto pushcapture; case IFullCapture: capture[captop].siz = getoff(p) + 1; /* save capture size */ - capture[captop].s = s - getoff(p); + capture[captop].index = s - o - getoff(p); /* goto pushcapture; */ pushcapture: { - capture[captop].idx = p->i.key; + capture[captop].idx = p->i.aux2.key; capture[captop].kind = getkind(p); captop++; capture = growcap(L, capture, &capsize, captop, 0, ptop); diff --git a/3rd/lpeg/lpvm.h b/3rd/lpeg/lpvm.h index 69ec33dce..684f0c9a0 100644 --- a/3rd/lpeg/lpvm.h +++ b/3rd/lpeg/lpvm.h @@ -1,6 +1,3 @@ -/* -** $Id: lpvm.h $ -*/ #if !defined(lpvm_h) #define lpvm_h @@ -8,16 +5,24 @@ #include "lpcap.h" +/* +** About Character sets in instructions: a set is a bit map with an +** initial offset, in bits, and a size, in number of instructions. +** aux1 has the default value for the bits outsize that range. +*/ + + /* Virtual Machine's instructions */ typedef enum Opcode { IAny, /* if no char, fail */ - IChar, /* if char != aux, fail */ - ISet, /* if char not in buff, fail */ + IChar, /* if char != aux1, fail */ + ISet, /* if char not in set, 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) */ + ITestChar, /* if char != aux1, jump to 'offset' */ + ITestSet, /* if char not in set, jump to 'offset' */ + ISpan, /* read a span of chars in set */ + IUTFR, /* if codepoint not in range [offset, utf_to], fail */ + IBehind, /* walk back 'aux1' characters (fail if not possible) */ IRet, /* return from a rule */ IEnd, /* end of pattern */ IChoice, /* stack a choice; next fail will jump to 'offset' */ @@ -26,30 +31,46 @@ typedef enum Opcode { 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' */ + IBackCommit, /* backtrack like "fail" 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 + ICloseRunTime, + IEmpty /* to fill empty slots left by optimizations */ } Opcode; - +/* +** All array of instructions has a 'codesize' as its first element +** and is referred by a pointer to its second element, which is the +** first actual opcode. +*/ typedef union Instruction { struct Inst { byte code; - byte aux; - short key; + byte aux1; + union { + short key; + struct { + byte offset; + byte size; + } set; + } aux2; } i; int offset; + uint codesize; byte buff[1]; } Instruction; -void printpatt (Instruction *p, int n); +/* extract 24-bit value from an instruction */ +#define utf_to(inst) (((inst)->i.aux2.key << 8) | (inst)->i.aux1) + + +int charinset (const Instruction *i, const byte *buff, uint c); const char *match (lua_State *L, const char *o, const char *s, const char *e, Instruction *op, Capture *capture, int ptop); diff --git a/3rd/lpeg/makefile b/3rd/lpeg/makefile index 1e32195a8..32485a700 100644 --- a/3rd/lpeg/makefile +++ b/3rd/lpeg/makefile @@ -2,7 +2,7 @@ LIBNAME = lpeg LUADIR = ../lua/ COPT = -O2 -DNDEBUG -# COPT = -g +# COPT = -O0 -DLPEG_DEBUG -g CWARNS = -Wall -Wextra -pedantic \ -Waggregate-return \ @@ -11,21 +11,24 @@ CWARNS = -Wall -Wextra -pedantic \ -Wdisabled-optimization \ -Wpointer-arith \ -Wshadow \ + -Wredundant-decls \ -Wsign-compare \ -Wundef \ -Wwrite-strings \ -Wbad-function-cast \ -Wdeclaration-after-statement \ -Wmissing-prototypes \ + -Wmissing-declarations \ -Wnested-externs \ -Wstrict-prototypes \ + -Wc++-compat \ # -Wunreachable-code \ CFLAGS = $(CWARNS) $(COPT) -std=c99 -I$(LUADIR) -fPIC CC = gcc -FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o +FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o lpcset.o # For Linux linux: @@ -48,8 +51,9 @@ clean: 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 +lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h lpcset.h +lpcset.o: lpcset.c lptypes.h lpcset.h lpcode.h lptree.h lpvm.h lpcap.h +lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h lpcode.h +lptree.o: lptree.c lptypes.h lpcap.h lpcode.h lptree.h lpvm.h lpprint.h \ + lpcset.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 index ad60d509e..c8d1bc8db 100644 --- a/3rd/lpeg/re.html +++ b/3rd/lpeg/re.html @@ -1,22 +1,21 @@ + "//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> LPeg.re - Regex syntax for LPEG -

@@ -62,6 +61,20 @@

The re Module

+ + + + + + + + + + + + + + @@ -70,22 +83,15 @@

The re Module

+ - - - - - - - - - + + @@ -94,16 +100,13 @@

The re Module

- - - - - +(deprecated) +
SyntaxDescription
( p ) grouping
& p and predicate
! p not predicate
p1 p2 concatenation
p1 / p2 ordered choice
p ? optional match
p * zero or more repetitions
p + one or more repetitions
p^numexactly num repetitions
p^+numat least num repetitions
p^-numat most num repetitions
(name <- p)+ grammar
'string' literal string
"string" literal string
[class] character class
pattern 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
=name back reference
p -> 'string' string capture
p -> "string" string capture
p -> num numbered capture
p => name match-time capture equivalent to lpeg.Cmt(p, defs[name])
p ~> name fold capture -equivalent to lpeg.Cf(p, defs[name])
& p and predicate
! p not predicate
p1 p2 concatenation
p1 / p2 ordered choice
(name <- p)+ grammar
p >> name accumulator capture +equivalent to (p % defs[name])

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

@@ -200,9 +203,10 @@

A complete simple program

--> the number is odd -- returns the first numeral in a string -print(re.match("the number 423 is odd", "s <- {%d+} / . s")) +print(re.match("the number 423 is odd", "s <- {%d+} / . s")) --> 423 +-- substitutes a dot for each vowel in a string print(re.gsub("hello World", "[aeiou]", ".")) --> h.ll. W.rld @@ -329,7 +333,7 @@

Indented blocks

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

Patterns

suffix <- primary S (([+*?] / '^' [+-]? num / '->' S (string / '{}' / name) + / '>>' S name / '=>' S name) S)* primary <- '(' exp ')' / string / class / defined @@ -423,6 +428,7 @@

Patterns

/ '=' name / '{}' / '{~' exp '~}' + / '{|' exp '|}' / '{' exp '}' / '.' / name S !arrow @@ -436,7 +442,7 @@

Patterns

range <- . '-' [^]] S <- (%s / '--' [^%nl]*)* -- spaces and comments -name <- [A-Za-z][A-Za-z0-9_]* +name <- [A-Za-z_][A-Za-z0-9_]* arrow <- '<-' num <- [0-9]+ string <- '"' [^"]* '"' / "'" [^']* "'" @@ -452,37 +458,9 @@

Patterns

License

-Copyright © 2008-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 "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. -

+This module is part of the LPeg package and shares +its license. -

-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/lpeg/re.lua b/3rd/lpeg/re.lua index 3bb8af7d4..1fb9fa9ec 100644 --- a/3rd/lpeg/re.lua +++ b/3rd/lpeg/re.lua @@ -1,4 +1,7 @@ --- $Id: re.lua $ +-- +-- Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) +-- written by Roberto Ierusalimschy +-- -- imported functions and modules local tonumber, type, print, error = tonumber, type, print, error @@ -10,14 +13,14 @@ local m = require"lpeg" -- on 'mm' local mm = m --- pattern's metatable +-- patterns' metatable local mt = getmetatable(mm.P(0)) +local version = _VERSION -- No more global accesses after this point -local version = _VERSION -if version == "Lua 5.2" then _ENV = nil end +_ENV = nil -- does no harm in Lua 5.1 local any = m.P(1) @@ -142,7 +145,7 @@ local item = (defined + Range + m.C(any)) / m.P local Class = "[" * (m.C(m.P"^"^-1)) -- optional complement symbol - * m.Cf(item * (item - "]")^0, mt.__add) / + * (item * ((item % mt.__add) - "]")^0) / function (c, p) return c == "^" and any - p or p end * "]" @@ -168,13 +171,13 @@ 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) + + m.V"Seq" * ("/" * S * m.V"Seq" % mt.__add)^0 ); + Seq = (m.Cc(m.P"") * (m.V"Prefix" % mt.__mul)^0) * (#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 * + Suffix = 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) @@ -185,10 +188,11 @@ local exp = m.P{ "Exp", + m.P"{}" * m.Cc(nil, m.Ct) + defwithfunc(mt.__div) ) - + "=>" * S * defwithfunc(m.Cmt) - + "~>" * S * defwithfunc(m.Cf) - ) * S - )^0, function (a,b,f) return f(a,b) end ); + + "=>" * S * defwithfunc(mm.Cmt) + + ">>" * S * defwithfunc(mt.__mod) + + "~>" * S * defwithfunc(mm.Cf) + ) % function (a,b,f) return f(a,b) end * S + )^0; Primary = "(" * m.V"Exp" * ")" + String / mm.P + Class @@ -204,8 +208,7 @@ local exp = m.P{ "Exp", + (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 + ((m.V"Definition" / firstdef) * (m.V"Definition" % adddef)^0) / mm.P } local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error) diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua index 8f9f5745d..d0b82da48 100755 --- a/3rd/lpeg/test.lua +++ b/3rd/lpeg/test.lua @@ -1,7 +1,5 @@ #!/usr/bin/env lua --- $Id: test.lua $ - -- require"strict" -- just to be pedantic local m = require"lpeg" @@ -48,8 +46,8 @@ end print"General tests for LPeg library" -assert(type(m.version()) == "string") -print("version " .. m.version()) +assert(type(m.version) == "string") +print(m.version) assert(m.type("alo") ~= "pattern") assert(m.type(io.input) ~= "pattern") assert(m.type(m.P"alo") == "pattern") @@ -70,6 +68,8 @@ 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(1)^0, "abcd") == 5) +assert(m.match(m.S("")^0, "abcd") == 1) -- tests for locale do @@ -119,6 +119,8 @@ 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") +eqcharset(m.S("\0\255"), m.P"\0" + "\255") -- charset extremes + local word = alpha^1 * (1 - alpha)^0 assert((word^0 * -1):match"alo alo") @@ -406,7 +408,7 @@ assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc') do - -- large dynamic Cc + print "testing large dynamic Cc" local lim = 2^16 - 1 local c = 0 local function seq (n) @@ -491,8 +493,8 @@ local function f_term (v1, op, v2, d) 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); + Exp = V"Factor" * (FactorOp * V"Factor" % f_factor)^0; + Factor = V"Term" * (TermOp * V"Term" % f_term)^0; Term = Number / tonumber + Open * V"Exp" * Close; } @@ -864,6 +866,7 @@ 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) +assert(m.match(m.Cc(0) * (m.C(1) % f)^0, "alo alo") == 7) t = {m.match(m.Cf(m.Cc(1,2,3), error), "")} checkeq(t, {1}) @@ -873,7 +876,7 @@ t = p:match("a=b;c=du;xux=yuy;") checkeq(t, {a="b", c="du", xux="yuy"}) --- errors in accumulator capture +-- errors in fold capture -- no initial capture checkerr("no initial value", m.match, m.Cf(m.P(5), print), 'aaaaaa') @@ -881,8 +884,14 @@ checkerr("no initial value", m.match, m.Cf(m.P(5), print), 'aaaaaa') checkerr("no initial value", m.match, m.Cf(m.P(500), print), string.rep('a', 600)) --- nested capture produces no initial value -checkerr("no initial value", m.match, m.Cf(m.P(1) / {}, print), "alo") + +-- errors in accumulator capture + +-- no initial capture +checkerr("no previous value", m.match, m.P(5) % print, 'aaaaaa') +-- no initial capture (very long match forces fold to be a pair open-close) +checkerr("no previous value", m.match, m.P(500) % print, + string.rep('a', 600)) -- tests for loop checker @@ -985,10 +994,10 @@ for i = 1, 10 do assert(p:match("aaaaaaaaaaa") == 11 - i + 1) end -print"+" --- tests for back references +print "testing back references" + 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') @@ -996,6 +1005,35 @@ p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) t = p:match("ab") checkeq(t, {"a", "b"}) + +do + -- some basic cases + assert(m.match(m.Cg(m.Cc(3), "a") * m.Cb("a"), "a") == 3) + assert(m.match(m.Cg(m.C(1), 133) * m.Cb(133), "X") == "X") + + -- first reference to 'x' should not see the group enclosing it + local p = m.Cg(m.Cb('x'), 'x') * m.Cb('x') + checkerr("back reference 'x' not found", m.match, p, '') + + local p = m.Cg(m.Cb('x') * m.C(1), 'x') * m.Cb('x') + checkerr("back reference 'x' not found", m.match, p, 'abc') + + -- reference to 'x' should not see the group enclosed in another capture + local s = string.rep("a", 30) + local p = (m.C(1)^-4 * m.Cg(m.C(1), 'x')) / {} * m.Cb('x') + checkerr("back reference 'x' not found", m.match, p, s) + + local p = (m.C(1)^-20 * m.Cg(m.C(1), 'x')) / {} * m.Cb('x') + checkerr("back reference 'x' not found", m.match, p, s) + + -- second reference 'k' should refer to 10 and first ref. 'k' + p = m.Cg(m.Cc(20), 'k') * m.Cg(m.Cc(10) * m.Cb('k') * m.C(1), 'k') + * (m.Cb('k') / function (a,b,c) return a*10 + b + tonumber(c) end) + -- 10 * 10 (Cc) + 20 (Cb) + 7 (C) == 127 + assert(p:match("756") == 127) + +end + p = m.P(true) for i = 1, 10 do p = p * m.Cg(1, i) end for i = 1, 10 do @@ -1032,6 +1070,17 @@ local function id (s, i, ...) return true, ... end +do -- run-time capture in an end predicate (should discard its value) + local x = 0 + function foo (s, i) + x = x + 1 + return true, x + end + + local p = #(m.Cmt("", foo) * "xx") * m.Cmt("", foo) + assert(p:match("xx") == 2) +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") @@ -1156,7 +1205,7 @@ end -- bug in 1.0: problems with math-times returning too many captures -do +if _VERSION >= "Lua 5.2" then local lim = 2^11 - 10 local res = {m.match(manyCmt(lim), "a")} assert(#res == lim and res[1] == lim - 1 and res[lim] == 0) @@ -1171,9 +1220,91 @@ t = {p:match('abacc')} checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) +do print"testing large grammars" + local lim = 1000 -- number of rules + local t = {} + + for i = 3, lim do + t[i] = m.V(i - 1) -- each rule calls previous one + end + t[1] = m.V(lim) -- start on last rule + t[2] = m.C("alo") -- final rule + + local P = m.P(t) -- build grammar + assert(P:match("alo") == "alo") + + t[#t + 1] = m.P("x") -- one more rule... + checkerr("too many rules", m.P, t) +end + + +print "testing UTF-8 ranges" + +do -- a few typical UTF-8 ranges + local p = m.utfR(0x410, 0x44f)^1 / "cyr: %0" + + m.utfR(0x4e00, 0x9fff)^1 / "cjk: %0" + + m.utfR(0x1F600, 0x1F64F)^1 / "emot: %0" + + m.utfR(0, 0x7f)^1 / "ascii: %0" + + m.utfR(0, 0x10ffff) / "other: %0" + + p = m.Ct(p^0) * -m.P(1) + + local cyr = "ждюя" + local emot = "\240\159\152\128\240\159\153\128" -- 😀🙀 + local cjk = "专举乸" + local ascii = "alo" + local last = "\244\143\191\191" -- U+10FFFF + + local s = cyr .. "—" .. emot .. "—" .. cjk .. "—" .. ascii .. last + t = (p:match(s)) + + assert(t[1] == "cyr: " .. cyr and t[2] == "other: —" and + t[3] == "emot: " .. emot and t[4] == "other: —" and + t[5] == "cjk: " .. cjk and t[6] == "other: —" and + t[7] == "ascii: " .. ascii and t[8] == "other: " .. last and + t[9] == nil) + + -- failing UTF-8 matches and borders + assert(not m.match(m.utfR(10, 0x2000), "\9")) + assert(not m.match(m.utfR(10, 0x2000), "\226\128\129")) + assert(m.match(m.utfR(10, 0x2000), "\10") == 2) + assert(m.match(m.utfR(10, 0x2000), "\226\128\128") == 4) +end + + +do -- valid and invalid code points + local p = m.utfR(0, 0x10ffff)^0 + assert(p:match("汉字\128") == #"汉字" + 1) + assert(p:match("\244\159\191") == 1) + assert(p:match("\244\159\191\191") == 1) + assert(p:match("\255") == 1) + + -- basic errors + checkerr("empty range", m.utfR, 1, 0) + checkerr("invalid code point", m.utfR, 1, 0x10ffff + 1) +end + + +do -- back references (fixed width) + -- match a byte after a CJK point + local p = m.B(m.utfR(0x4e00, 0x9fff)) * m.C(1) + p = m.P{ p + m.P(1) * m.V(1) } -- search for 'p' + assert(p:match("ab д 专X x") == "X") + + -- match a byte after a hebrew point + local p = m.B(m.utfR(0x5d0, 0x5ea)) * m.C(1) + p = m.P(#"ש") * p + assert(p:match("שX") == "X") + + checkerr("fixed length", m.B, m.utfR(0, 0x10ffff)) +end + + + ------------------------------------------------------------------- -- Tests for 're' module ------------------------------------------------------------------- +print"testing 're' module" local re = require "re" @@ -1307,6 +1438,12 @@ e = compile([[ e = compile("{[0-9]+'.'?[0-9]*} -> sin", math) assert(e:match("2.34") == math.sin(2.34)) +e = compile("'pi' -> math", _G) +assert(e:match("pi") == math.pi) + +e = compile("[ ]* 'version' -> _VERSION", _G) +assert(e:match(" version") == _VERSION) + function eq (_, _, a, b) return a == b end @@ -1380,6 +1517,13 @@ c = re.compile([[ ]], {tonumber = tonumber, add = function (a,b) return a + b end}) assert(c:match("3 401 50") == 3 + 401 + 50) +-- test for accumulator captures +c = re.compile([[ + S <- number (%s+ number >> add)* + number <- %d+ -> tonumber +]], {tonumber = tonumber, add = function (a,b) return a + b end}) +assert(c:match("3 401 50") == 3 + 401 + 50) + -- tests for look-ahead captures x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} checkeq(x, {"", "alo", ""}) diff --git a/Makefile b/Makefile index 337ed3e6a..daab6d1e0 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L$(TLS_LIB) -I$(TLS_INC) $^ -o $@ -lssl -$(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) +$(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 3rd/lpeg/lpcset.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ clean : From 272af34736749e90abda04a7ad81cc963ad77bfd Mon Sep 17 00:00:00 2001 From: yzj <71420106+yzj12138@users.noreply.github.com> Date: Thu, 6 Jul 2023 17:44:02 +0800 Subject: [PATCH 487/565] =?UTF-8?q?=E4=BF=AE=E6=94=B9delete=E4=BD=BF?= =?UTF-8?q?=E7=94=A8send=5Fcommand=20(#1770)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xingfan.yzj --- lualib/skynet/db/mongo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 958f96732..451e6059c 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -457,7 +457,7 @@ function mongo_collection:raw_safe_update(update) end function mongo_collection:delete(query, single) - self.database:runCommand("delete", self.name, "deletes", {bson_encode({ + self.database:send_command("delete", self.name, "deletes", {bson_encode({ q = query, limit = single and 1 or 0, })}) From b402837628d8e46674fd8ad6875f1a67e4217a46 Mon Sep 17 00:00:00 2001 From: Whislly Date: Fri, 7 Jul 2023 15:14:37 +0800 Subject: [PATCH 488/565] =?UTF-8?q?=E8=81=9A=E5=90=88=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=B8=B8=E6=A0=87=E5=AF=B9=E8=B1=A1=20(#1769?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 聚合方法返回游标对象 * 使用table.move()替换for循环拷贝 * 改成使用类似继承的方法 1、返回aggregate_cursor不修改mongo_cursor,直接拷贝mongo_cursor的sort、skip、limit、next、close方法 2、修改了mongo_cursor:count,不再使用unpack 3、aggregate_cursor:count和aggregate_cursor:hasNext方法里options为nil时,不使用unpack 4、mongo_collection:aggregate方法里拷贝了pipeline和options --- lualib/skynet/db/mongo.lua | 156 +++++++++++++++++++++++++++++++++---- 1 file changed, 139 insertions(+), 17 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 451e6059c..84fa445cf 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -28,6 +28,11 @@ local cursor_meta = { __index = mongo_cursor, } +local aggregate_cursor = {} +local aggregate_cursor_meta = { + __index = aggregate_cursor, +} + local mongo_client = {} local client_meta = { @@ -505,7 +510,6 @@ function mongo_collection:find(query, projection) __data = nil, __cursor = nil, __document = {}, - __flags = 0, __skip = 0, __limit = 0, __sort = empty_bson, @@ -544,18 +548,13 @@ function mongo_cursor:limit(amount) end function mongo_cursor:count(with_limit_and_skip) - local cmd = { - 'count', self.__collection.name, - 'query', self.__query, - } + local ret 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 + ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query, + 'limit', self.__limit, 'skip', self.__skip) + else + ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query) end - local ret = self.__collection.database:runCommand(table.unpack(cmd)) assert(ret and ret.ok == 1) return ret.n end @@ -667,12 +666,27 @@ end -- @return function mongo_collection:aggregate(pipeline, options) assert(pipeline) - local cmd = {"aggregate", self.name, "pipeline", pipeline} - for k, v in pairs(options) do - table.insert(cmd, k) - table.insert(cmd, v) + local options_cmd + if options then + options_cmd = {} + for k, v in pairs(options) do + table.insert(options_cmd, k) + table.insert(options_cmd, v) + end end - return self.database:runCommand(table.unpack(cmd)) + local len = #pipeline + return setmetatable( { + __collection = self, + __pipeline = table.move(pipeline, 1, len, 1, {}), + __pipeline_len = len, + __options = options_cmd, + __ptr = nil, + __data = nil, + __cursor = nil, + __document = {}, + __skip = 0, + __limit = 0, + } , aggregate_cursor_meta) end function mongo_cursor:hasNext() @@ -703,7 +717,7 @@ function mongo_cursor:hasNext() self.__document = nil self.__data = nil self.__cursor = nil - error(response["$err"] or "Reply from mongod error") + error(response["errmsg"] or "Reply from mongod error") end local cursor = response.cursor @@ -754,4 +768,112 @@ function mongo_cursor:close() end end +local function format_pipeline(self, with_limit_and_skip, is_count) + local len = self.__pipeline_len + if self.__sort and not is_count then + len = len + 1 + self.__pipeline[len] = { ["$sort"] = self.__sort } + end + if with_limit_and_skip then + if self.__skip > 0 then + len = len + 1 + self.__pipeline[len] = { ["$skip"] = self.__skip } + end + if self.__limit > 0 then + len = len + 1 + self.__pipeline[len] = { ["$limit"] = self.__limit } + end + end + if is_count then + len = len + 1 + self.__pipeline[len] = { ["$count"] = "__count" } + end + for i = #self.__pipeline, len + 1, -1 do + table.remove(self.__pipeline, i) + end + return self.__pipeline +end + +function aggregate_cursor:count(with_limit_and_skip) + local ret + local name = self.__collection.name + local database = self.__collection.database + if self.__options then + ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, with_limit_and_skip, true), + table.unpack(self.__options)) + else + ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, with_limit_and_skip, true), + "cursor", empty_bson) + end + if ret.ok ~= 1 then + error(ret["errmsg"] or "Reply from mongod error") + end + return ret.cursor.firstBatch[1].__count +end + +function aggregate_cursor:hasNext() + if self.__ptr == nil then + if self.__document == nil then + return false + end + local ret + local name = self.__collection.name + local database = self.__collection.database + if self.__data == nil then + if self.__options then + ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, true), table.unpack(self.__options)) + else + ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, true), "cursor", empty_bson) + end + else + if self.__cursor and self.__cursor > 0 then + ret = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name) + else + -- no more + self.__document = nil + self.__data = nil + return false + end + end + + if ret.ok ~= 1 then + self.__document = nil + self.__data = nil + self.__cursor = nil + error(ret["errmsg"] or "Reply from mongod error") + end + + local cursor = ret.cursor + self.__document = cursor.firstBatch or cursor.nextBatch + self.__data = ret + self.__ptr = 1 + self.__cursor = cursor.id + + local limit = self.__limit + if cursor.id > 0 and limit > 0 then + limit = limit - #self.__document + if limit <= 0 then + -- reach limit + self:close() + end + + self.__limit = limit + end + + if cursor.id == 0 and #self.__document == 0 then -- nomore + return false + end + + return true + end + + return true +end + +aggregate_cursor.sort = mongo_cursor.sort +aggregate_cursor.skip = mongo_cursor.skip +aggregate_cursor.limit = mongo_cursor.limit +aggregate_cursor.next = mongo_cursor.next +aggregate_cursor.close = mongo_cursor.close + return mongo From 074426e2735203134f712f37ef292c146612a408 Mon Sep 17 00:00:00 2001 From: Whislly Date: Fri, 7 Jul 2023 20:11:36 +0800 Subject: [PATCH 489/565] =?UTF-8?q?pipline=E6=96=B9=E6=B3=95=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20(#1771)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、增加sort_statge、skip_statge、limit_statge、count_statge替换临时table 2、改成for i = 1, 2 do self.__pipeline[len + i] = nil end清除老数据 --- lualib/skynet/db/mongo.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 84fa445cf..51a9ee2df 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -768,29 +768,34 @@ function mongo_cursor:close() end end +local sort_stage = { ["$sort"] = true } +local skip_stage = { ["$skip"] = 0 } +local limit_stage = { ["$limit"] = 0 } +local count_stage = { ["$count"] = "__count" } local function format_pipeline(self, with_limit_and_skip, is_count) local len = self.__pipeline_len if self.__sort and not is_count then len = len + 1 - self.__pipeline[len] = { ["$sort"] = self.__sort } + sort_stage["$sort"] = self.__sort + self.__pipeline[len] = sort_stage end if with_limit_and_skip then if self.__skip > 0 then len = len + 1 - self.__pipeline[len] = { ["$skip"] = self.__skip } + skip_stage["$skip"] = self.__skip + self.__pipeline[len] = skip_stage end if self.__limit > 0 then len = len + 1 - self.__pipeline[len] = { ["$limit"] = self.__limit } + limit_stage["$limit"] = self.__limit + self.__pipeline[len] = limit_stage end end if is_count then len = len + 1 - self.__pipeline[len] = { ["$count"] = "__count" } - end - for i = #self.__pipeline, len + 1, -1 do - table.remove(self.__pipeline, i) + self.__pipeline[len] = count_stage end + for i = 1, 2 do self.__pipeline[len + i] = nil end return self.__pipeline end From c6a8976bc3adeb4213e2e0060cb4ac543c004544 Mon Sep 17 00:00:00 2001 From: Whislly Date: Mon, 10 Jul 2023 12:29:16 +0800 Subject: [PATCH 490/565] =?UTF-8?q?findOne=E4=BC=98=E5=8C=96=20(#1772)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、直接调用runCommand方法 2、增加"limit": 1参数,限制只返回一条数据 --- lualib/skynet/db/mongo.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 51a9ee2df..325a68c9a 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -494,11 +494,12 @@ function mongo_collection:raw_safe_delete(delete) end function mongo_collection:findOne(query, projection) - local cursor = self:find(query, projection) - if cursor:hasNext() then - return cursor:next() + local r = self.database:runCommand("find", self.name, "filter", query and bson_encode(query) or empty_bson, + "limit", 1, "projection", projection and bson_encode(projection) or empty_bson) + if r.ok ~= 1 then + error(r.errmsg or "Reply from mongod error") end - return nil + return r.cursor.firstBatch[1] end function mongo_collection:find(query, projection) From bb90b64c3f0d8f54d7b5df322224b85a49bf7aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 19 Jul 2023 20:01:41 +0800 Subject: [PATCH 491/565] =?UTF-8?q?websocket=E6=B7=BB=E5=8A=A0is=5Fclose?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20(#1781)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * websocket添加is_close方法 * 代码优化 --- lualib/http/websocket.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index a7e62ea7b..63e04f127 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -9,6 +9,15 @@ local socket_error = sockethelper.socket_error local GLOBAL_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" local MAX_FRAME_SIZE = 256 * 1024 -- max frame is 256K +local assert = assert +local pairs = pairs +local error = error +local string = string +local xpcall = xpcall +local debug = debug +local table = table +local tonumber = tonumber + local M = {} @@ -526,5 +535,6 @@ function M.close(id, code ,reason) end end +M.is_close = _isws_closed return M From 93ea565bcb1754fdac3c0b6219c24206cadb9851 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 31 Jul 2023 10:19:40 +0800 Subject: [PATCH 492/565] Improve README, See #1785 --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5514756d1..d8b194062 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ ## ![skynet logo](https://github.com/cloudwu/skynet/wiki/image/skynet_metro.jpg) -Skynet is a lightweight online game framework which can be used in many other fields. +Skynet is a multi-user Lua framework supporting the actor model, often used in games. + +[It is heavily used in the Chinese game industry](https://github.com/cloudwu/skynet/wiki/Uses), but is also now spreading to other industries, and to English-centric developers. To visit related sites, visit the Chinese pages using something like Google or Deepl translate. + +The community is friendly and almost contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or sumbit issues in English. ## Build @@ -38,5 +42,5 @@ Official Lua versions can also be used as long as the Makefile is edited. ## How To Use -* Read Wiki for documents https://github.com/cloudwu/skynet/wiki -* The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ +* Read Wiki for documents https://github.com/cloudwu/skynet/wiki (Written in both English and Chinese) +* The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ (In Chinese, but you can visit them using something like Google or Deepl translate.) From c178c398b984a3da39f7398f3bfa8bdc772b8e91 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 31 Jul 2023 21:31:21 +0800 Subject: [PATCH 493/565] don't report error when session == 0 --- lualib/skynet.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 107b5a37c..9b18467af 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -455,7 +455,10 @@ function skynet.killthread(thread) if addr then session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil - c.send(addr, skynet.PTYPE_ERROR, session_coroutine_id[co], "") + local session = session_coroutine_id[co] + if session > 0 then + c.send(addr, skynet.PTYPE_ERROR, session, "") + end session_coroutine_id[co] = nil end if watching_session[session] then From 172278bc8f8bae8d81593ca18ce763fdbb812abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 28 Sep 2023 13:48:35 +0800 Subject: [PATCH 494/565] check conflict (#1798) * rewind check conflict * Use auxsend instead of c.send --- lualib/skynet.lua | 109 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 9b18467af..1585c391b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -69,6 +69,101 @@ local watching_session = {} local error_queue = {} local fork_queue = { h = 1, t = 0 } +local auxsend, auxtimeout +do ---- avoid session rewind conflict + local csend = c.send + local cintcommand = c.intcommand + local dangerzone + local dangerzone_size = 0x1000 + local dangerzone_low = 0x70000000 + local dangerzone_up = dangerzone_low + dangerzone_size + + local set_checkrewind -- set auxsend and auxtimeout for safezone + local set_checkconflict -- set auxsend and auxtimeout for dangerzone + + local function reset_dangerzone(session) + dangerzone_up = session + dangerzone_low = session + dangerzone = { [session] = true } + for s in pairs(session_id_coroutine) do + if s < dangerzone_low then + dangerzone_low = s + elseif s > dangerzone_up then + dangerzone_up = s + end + dangerzone[s] = true + end + dangerzone_low = dangerzone_low - dangerzone_size + end + + -- in dangerzone, we should check if the next session already exist. + local function checkconflict(session) + local next_session = session + 1 + if next_session > dangerzone_up then + -- leave dangerzone + reset_dangerzone(session) + assert(next_session > dangerzone_up) + set_checkrewind() + return + end + while true do + if not dangerzone[next_session] then + return + end + if not session_id_coroutine[next_session] then + reset_dangerzone(session) + return + end + -- skip the session already exist. + next_session = c.genid() + 1 + end + end + + local function auxsend_checkconflict(addr, proto, msg, sz) + local session = csend(addr, proto, nil, msg, sz) + checkconflict(session) + return session + end + + local function auxtimeout_checkconflict(timeout) + local session = cintcommand("TIMEOUT", timeout) + checkconflict(session) + return session + end + + local function auxsend_checkrewind(addr, proto, msg, sz) + local session = csend(addr, proto, nil, msg, sz) + if session and session > dangerzone_low and session < dangerzone_up then + -- enter dangerzone + set_checkconflict(session) + end + return session + end + + local function auxtimeout_checkrewind(timeout) + local session = cintcommand("TIMEOUT", timeout) + if session and session > dangerzone_low and session < dangerzone_up then + -- enter dangerzone + set_checkconflict(session) + end + return session + end + + set_checkrewind = function() + auxsend = auxsend_checkrewind + auxtimeout = auxtimeout_checkrewind + end + + set_checkconflict = function(session) + reset_dangerzone(session) + auxsend = auxsend_checkconflict + auxtimeout = auxtimeout_checkconflict + end + + -- in safezone at the beginning + set_checkrewind() +end + do ---- request/select local function send_requests(self) local sessions = {} @@ -85,7 +180,7 @@ do ---- request/select c.trace(tag, "call", 4) c.send(addr, skynet.PTYPE_TRACE, 0, tag) end - local session = c.send(addr, p.id , nil , p.pack(tunpack(req, 3, req.n))) + local session = auxsend(addr, p.id , p.pack(tunpack(req, 3, req.n))) if session == nil then err = err or {} err[#err+1] = req @@ -188,7 +283,7 @@ do ---- request/select self._error = send_requests(self) self._resp = {} if timeout then - self._timeout = c.intcommand("TIMEOUT",timeout) + self._timeout = auxtimeout(timeout) session_id_coroutine[self._timeout] = self._thread end @@ -373,7 +468,7 @@ end skynet.trace_timeout(false) -- turn off by default function skynet.timeout(ti, func) - local session = c.intcommand("TIMEOUT",ti) + local session = auxtimeout(ti) assert(session) local co = co_create_for_timeout(func, ti) assert(session_id_coroutine[session] == nil) @@ -392,7 +487,7 @@ local function suspend_sleep(session, token) end function skynet.sleep(ti, token) - local session = c.intcommand("TIMEOUT",ti) + local session = auxtimeout(ti) assert(session) token = token or coroutine.running() local succ, ret = suspend_sleep(session, token) @@ -605,7 +700,7 @@ function skynet.call(addr, typename, ...) end local p = proto[typename] - local session = c.send(addr, p.id , nil , p.pack(...)) + local session = auxsend(addr, p.id , p.pack(...)) if session == nil then error("call to invalid address " .. skynet.address(addr)) end @@ -619,7 +714,7 @@ function skynet.rawcall(addr, typename, msg, sz) c.send(addr, skynet.PTYPE_TRACE, 0, tag) end local p = proto[typename] - local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address") + local session = assert(auxsend(addr, p.id , msg, sz), "call to invalid address") return yield_call(addr, session) end @@ -627,7 +722,7 @@ function skynet.tracecall(tag, addr, typename, msg, sz) c.trace(tag, "tracecall begin") c.send(addr, skynet.PTYPE_TRACE, 0, tag) local p = proto[typename] - local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address") + local session = assert(auxsend(addr, p.id , msg, sz), "call to invalid address") local msg, sz = yield_call(addr, session) c.trace(tag, "tracecall end") return msg, sz From f3f7e725e8cccd6f869402b8820ad88ef4329af1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 28 Sep 2023 14:40:02 +0800 Subject: [PATCH 495/565] check session == nil --- lualib/skynet.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 1585c391b..9cb7c7487 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -98,6 +98,9 @@ do ---- avoid session rewind conflict -- in dangerzone, we should check if the next session already exist. local function checkconflict(session) + if session == nil then + return + end local next_session = session + 1 if next_session > dangerzone_up then -- leave dangerzone From 5ec02a4a2c5bb42e2f7c9165f6132e4252e6f8a0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 28 Sep 2023 20:15:29 +0800 Subject: [PATCH 496/565] Add a rare case --- lualib/skynet.lua | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 9cb7c7487..b26b45478 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -107,18 +107,23 @@ do ---- avoid session rewind conflict reset_dangerzone(session) assert(next_session > dangerzone_up) set_checkrewind() - return - end - while true do - if not dangerzone[next_session] then - return - end - if not session_id_coroutine[next_session] then - reset_dangerzone(session) - return + else + while true do + if not dangerzone[next_session] then + break + end + if not session_id_coroutine[next_session] then + reset_dangerzone(session) + break + end + -- skip the session already exist. + next_session = c.genid() + 1 end - -- skip the session already exist. - next_session = c.genid() + 1 + end + -- session will rewind after 0x7fffffff + if next_session == 0x80000000 and dangerzone[1] then + assert(c.genid() == 1) + return checkconflict(1) end end @@ -136,7 +141,7 @@ do ---- avoid session rewind conflict local function auxsend_checkrewind(addr, proto, msg, sz) local session = csend(addr, proto, nil, msg, sz) - if session and session > dangerzone_low and session < dangerzone_up then + if session and session > dangerzone_low and session <= dangerzone_up then -- enter dangerzone set_checkconflict(session) end @@ -145,7 +150,7 @@ do ---- avoid session rewind conflict local function auxtimeout_checkrewind(timeout) local session = cintcommand("TIMEOUT", timeout) - if session and session > dangerzone_low and session < dangerzone_up then + if session and session > dangerzone_low and session <= dangerzone_up then -- enter dangerzone set_checkconflict(session) end From adf0cc5a6fb1ce32bdd4890c0df5029aaeba627c Mon Sep 17 00:00:00 2001 From: HYbutterfly <707298413@qq.com> Date: Fri, 27 Oct 2023 17:16:48 +0800 Subject: [PATCH 497/565] check enablessl == "true" (#1815) --- service/bootstrap.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 69802b492..02d19dd03 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -42,7 +42,7 @@ skynet.start(function() skynet.newservice "service_mgr" local enablessl = skynet.getenv "enablessl" - if enablessl then + if enablessl == "true" then service.new("ltls_holder", function () local c = require "ltls.init.c" c.constructor() From 0e6423484fe04feece061309bddfd5182f3f06f1 Mon Sep 17 00:00:00 2001 From: ykxpb Date: Mon, 30 Oct 2023 18:15:27 +0800 Subject: [PATCH 498/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20openssl3=20depreca?= =?UTF-8?q?ted=20warning=20ERR=5Fload=5FBIO=5Fstrings=20(#1816)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/ltls.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 28488de04..bf3c3c9ac 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -411,7 +411,9 @@ ltls_init_constructor(lua_State* L) { if(!TLS_IS_INIT) { SSL_library_init(); SSL_load_error_strings(); +#if OPENSSL_VERSION_NUMBER < 0x30000000L ERR_load_BIO_strings(); +#endif OpenSSL_add_all_algorithms(); } #endif From f7453973f8e968fb8a61f86c6eeca31afb41c44f Mon Sep 17 00:00:00 2001 From: shencyx Date: Tue, 7 Nov 2023 11:52:58 +0800 Subject: [PATCH 499/565] =?UTF-8?q?=E9=81=BF=E5=85=8D=20sharedata=20?= =?UTF-8?q?=E8=AF=AF=E7=94=A8=E5=A4=A7=E4=BA=8E32=E4=BD=8D=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E5=81=9A=20key=20=E6=97=B6,=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83=20(#1820?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: shencyx --- lualib-src/lua-sharedata.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 8e329f9d1..d52e294f3 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -306,6 +306,7 @@ convtable(lua_State *L) { for (i=0;ihash[i].valuetype = VALUETYPE_NIL; tbl->hash[i].nocolliding = 0; + tbl->hash[i].next = -1; } tbl->sizehash = sizehash; From f97c72d341c820ca9ed0ec0d49f654b0e783d934 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Nov 2023 15:13:08 +0800 Subject: [PATCH 500/565] Release 1.7.0 --- HISTORY.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 5fe4b1441..846a470f6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,11 @@ +v1.7.0 (2023-11-13) +----------- +* Update Lua to 5.4.6 +* Update lpeg to 1.1.0 +* Improve mongo driver +* Fix service session rewind issue +* Add websocket.is_closed + v1.6.0 (2022-11-16) ----------- * Update Lua to 5.4.4 (github Nov 8, 2022) From 8b5bbaabd9c2d165f92101b762da788ae3bd329e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Nov 2023 15:16:53 +0800 Subject: [PATCH 501/565] Update lua version number --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8b194062..540ef8b68 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.4.4 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.6 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. From 6249062c2de121a660a144d6b4db2f8e2ef48359 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Wed, 15 Nov 2023 22:11:24 +0800 Subject: [PATCH 502/565] Update httpc.request (#1822) --- lualib/http/httpc.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 058e28a48..e3a630707 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -114,6 +114,8 @@ function httpc.request(method, hostname, url, recvheader, header, content) close_interface(interface, fd) if ok then return statuscode, body + elseif statuscode == 200 then + error(body) else error(statuscode) end From 89b47f93723d03f4dd275a9dbab52b267a40dd89 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Fri, 17 Nov 2023 19:49:42 +0800 Subject: [PATCH 503/565] modify httpc.request (#1823) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modify httpc.request 这样可能更好点 * modify httpc.request --- lualib/http/httpc.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index e3a630707..78cab15ed 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -114,10 +114,8 @@ function httpc.request(method, hostname, url, recvheader, header, content) close_interface(interface, fd) if ok then return statuscode, body - elseif statuscode == 200 then - error(body) else - error(statuscode) + error(body or statuscode) end end From 4b0937cc19f9af37e0eb917582ead930c0566bf2 Mon Sep 17 00:00:00 2001 From: felove <826279291@qq.com> Date: Fri, 1 Dec 2023 11:28:59 +0800 Subject: [PATCH 504/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmysql.execute?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E9=95=BF=E5=8F=82=E6=95=B0=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=B8=BAnil=E6=97=B6=E7=9A=84NULL=E4=BD=8D?= =?UTF-8?q?=E5=9B=BE=E9=94=99=E8=AF=AF=20(#1829)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: wfl --- lualib/skynet/db/mysql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index a61afd5fa..44b58a650 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -584,7 +584,7 @@ local function _compose_stmt_execute(self, stmt, cursor_type, args) for i = 1, null_count do local byte = 0 for j = 0, 7 do - if field_index < arg_num then + if field_index <= arg_num then if args[field_index] == nil then byte = byte | (1 << j) else From 30d967ea05bb7e5bb57c34560de07be68b94a729 Mon Sep 17 00:00:00 2001 From: kuzhu Date: Mon, 4 Dec 2023 16:28:30 +0800 Subject: [PATCH 505/565] add get_sender function to outside (#1832) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add get_sender function to outside 目的是用sender结合skynet.select的超时特性,实现一个超时的cluster.call,所以希望这里能够把get_sender的接口加上 * Update cluster.lua 改为cluster.get_sender = get_sender --- lualib/skynet/cluster.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 025ea016f..10635398e 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -55,6 +55,8 @@ local function get_sender(node) return s end +cluster.get_sender = get_sender + function cluster.call(node, address, ...) -- skynet.pack(...) will free by cluster.core.packrequest local s = sender[node] From e66e892ee55e3ad7106d15791322aa26a27a35ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Wed, 13 Dec 2023 11:53:08 +0800 Subject: [PATCH 506/565] fix read_handshake (#1839) Co-authored-by: zixun --- lualib/http/websocket.lua | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 63e04f127..d95cd95d0 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -76,6 +76,27 @@ local function write_handshake(self, host, url, header) end end +local function reader_with_payload(self, payload) + local sz_payload = #payload + if sz_payload == 0 then + return + end + local read = self.read + function self.read (sz) + if sz == nil or sz == sz_payload then + self.read = read + return payload + end + if sz < sz_payload then + local ret = payload:sub(1, sz) + payload = payload:sub(sz + 1) + sz_payload = #payload + return ret + end + self.read = read + return payload .. read(sz - sz_payload) + end +end local function read_handshake(self, upgrade_ops) local header, method, url @@ -83,10 +104,11 @@ local function read_handshake(self, upgrade_ops) header, method, url = upgrade_ops.header, upgrade_ops.method, upgrade_ops.url else local tmpline = {} - local header_body = internal.recvheader(self.read, tmpline, "") - if not header_body then + local payload = internal.recvheader(self.read, tmpline, "") + if not payload then return 413 end + reader_with_payload(self, payload) local request = assert(tmpline[1]) local httpver From 1f93f4864fd22c0ef3fc39dc55f746ef49780dae Mon Sep 17 00:00:00 2001 From: Zoro <14868554+zorocn@users.noreply.github.com> Date: Thu, 14 Dec 2023 21:04:43 +0800 Subject: [PATCH 507/565] fix write_handshake (#1841) Signed-off-by: zorocn --- lualib/http/websocket.lua | 49 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index d95cd95d0..431fc0ecb 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -33,6 +33,27 @@ local function _isws_closed(id) return not ws_pool[id] end +local function reader_with_payload(self, payload) + local sz_payload = #payload + if sz_payload == 0 then + return + end + local read = self.read + function self.read (sz) + if sz == nil or sz == sz_payload then + self.read = read + return payload + end + if sz < sz_payload then + local ret = payload:sub(1, sz) + payload = payload:sub(sz + 1) + sz_payload = #payload + return ret + end + self.read = read + return payload .. read(sz - sz_payload) + end +end local function write_handshake(self, host, url, header) local key = crypt.base64encode(crypt.randomkey()..crypt.randomkey()) @@ -50,11 +71,11 @@ local function write_handshake(self, host, url, header) end local recvheader = {} - local code, body = internal.request(self, "GET", host, url, recvheader, request_header) + local code, payload = internal.request(self, "GET", host, url, recvheader, request_header) if code ~= 101 then - error(string.format("websocket handshake error: code[%s] info:%s", code, body)) + error(string.format("websocket handshake error: code[%s] info:%s", code, payload)) end - assert(body == "") -- todo: M.read may need handle it + reader_with_payload(self, payload) if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then error("websocket handshake upgrade must websocket") @@ -76,28 +97,6 @@ local function write_handshake(self, host, url, header) end end -local function reader_with_payload(self, payload) - local sz_payload = #payload - if sz_payload == 0 then - return - end - local read = self.read - function self.read (sz) - if sz == nil or sz == sz_payload then - self.read = read - return payload - end - if sz < sz_payload then - local ret = payload:sub(1, sz) - payload = payload:sub(sz + 1) - sz_payload = #payload - return ret - end - self.read = read - return payload .. read(sz - sz_payload) - end -end - local function read_handshake(self, upgrade_ops) local header, method, url if upgrade_ops then From 403068740061b6e9ddd16f5f3d2f2db719b361b6 Mon Sep 17 00:00:00 2001 From: robot Date: Wed, 20 Dec 2023 13:07:38 +0800 Subject: [PATCH 508/565] =?UTF-8?q?=E5=9C=A8=E8=BF=9B=E8=A1=8Chttps?= =?UTF-8?q?=E6=8F=A1=E6=89=8B=E6=97=B6=EF=BC=8C=E4=BC=9A=E6=9C=89=E5=B0=8F?= =?UTF-8?q?=E6=A6=82=E7=8E=87=E5=9C=A8=E8=BF=9B=E8=A1=8C=E5=AF=86=E9=92=A5?= =?UTF-8?q?=E5=8D=8F=E5=95=86=E5=89=8D=EF=BC=8Ctcp=E6=B5=81=E5=8D=A1?= =?UTF-8?q?=E4=BD=8F=EF=BC=8C=E5=AF=BC=E8=87=B3=E4=B8=8D=E8=83=BD=E6=9C=89?= =?UTF-8?q?=E6=95=88timeout=E8=A1=8C=E4=B8=BA=20(#1842)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/http/httpc.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 78cab15ed..bc18dad0c 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -83,9 +83,6 @@ local function connect(host, timeout) end -- print("protocol hostname port", protocol, hostname, port) local interface = gen_interface(protocol, fd, hostname) - if interface.init then - interface.init() - end if timeout then skynet.timeout(timeout, function() if not interface.finish then @@ -93,6 +90,9 @@ local function connect(host, timeout) end end) end + if interface.init then + interface.init() + end return fd, interface, host end From d9c30f9119e015f48c200dbdcffba18c7e2a1e01 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 27 Dec 2023 21:02:25 +0800 Subject: [PATCH 509/565] fix #1845 --- lualib/skynet/socket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index 6ec3d6bad..b15dd024b 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -330,7 +330,7 @@ function socket.read(id, sz) if ret then return ret end - if not s.connected then + if s.closing or not s.connected then return false, driver.readall(s.buffer, s.pool) end From 9e000da81f660ac6bd5363be11c6f0ccd3548927 Mon Sep 17 00:00:00 2001 From: yzj12138 <71420106+yzj12138@users.noreply.github.com> Date: Wed, 3 Jan 2024 18:14:17 +0800 Subject: [PATCH 510/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8Csky?= =?UTF-8?q?net.exit=E5=90=8E=E5=88=AB=E7=9A=84=E6=9C=8D=E5=8A=A1call?= =?UTF-8?q?=E6=9C=89=E6=A6=82=E7=8E=87=E5=8D=A1=E6=AD=BB=20(#1846)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xingfan.yzj --- lualib/skynet.lua | 4 +++- skynet-src/skynet_server.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index b26b45478..84c649e85 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -652,7 +652,9 @@ function skynet.exit() for address in pairs(tmp) do c.send(address, skynet.PTYPE_ERROR, 0, "") end - c.callback(function() end) + c.callback(function(prototype, msg, sz, session, source) + c.send(source, skynet.PTYPE_ERROR, session, "") + end) c.command("EXIT") -- quit service coroutine_yield "QUIT" diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4cc726e34..9f2a3593b 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -118,7 +118,7 @@ drop_message(struct skynet_message *msg, void *ud) { uint32_t source = d->handle; assert(source); // report error to the message source - skynet_send(NULL, source, msg->source, PTYPE_ERROR, 0, NULL, 0); + skynet_send(NULL, source, msg->source, PTYPE_ERROR, msg->session, NULL, 0); } struct skynet_context * From fb97668f9e2faba27d68a692335ed7ae00164c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:05:28 +0800 Subject: [PATCH 511/565] fix word err (#1847) --- service-src/service_gate.c | 2 +- service-src/service_harbor.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 35fa3ea63..41e06f1cc 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -147,7 +147,7 @@ _ctrl(struct gate * g, const void * msg, int sz) { } return; } - skynet_error(ctx, "[gate] Unkown command : %s", command); + skynet_error(ctx, "[gate] Unknown command : %s", command); } static void diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index f10c13020..922974639 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -680,7 +680,7 @@ mainloop(struct skynet_context * context, void * ud, int type, int session, uint if (id) { report_harbor_down(h,id); } else { - skynet_error(context, "Unkown fd (%d) closed", message->id); + skynet_error(context, "Unknown fd (%d) closed", message->id); } break; } From b309d82538a7690e720a1f785f0c2107c38ab1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Sat, 6 Jan 2024 16:38:14 +0800 Subject: [PATCH 512/565] fix word err (#1848) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix word err * fix word err * 增加mongo update 测试 --- lualib/skynet/socket.lua | 2 +- test/testmongodb.lua | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index b15dd024b..e047ffff9 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -154,7 +154,7 @@ socket_message[5] = function(id, _, err) return end if s.callback then - skynet.error("socket: accpet error:", err) + skynet.error("socket: accept error:", err) return end if s.connected then diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 89033e994..662cc3057 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -7,6 +7,11 @@ if port then port = math.tointeger(port) end +host = '127.0.0.1' +port = 27017 +username = "admin" +password = 123456 +db_name = "admin" -- print(host, port, db_name, username, password) local function _create_client() @@ -19,7 +24,7 @@ local function _create_client() ) end -function test_auth() +local function test_auth() local ok, err, ret local c = mongo.client( { @@ -39,7 +44,7 @@ function test_auth() assert(ok and ret and ret.n == 1, err) end -function test_insert_without_index() +local function test_insert_without_index() local ok, err, ret local c = _create_client() local db = c[db_name] @@ -54,7 +59,7 @@ function test_insert_without_index() assert(ok and ret and ret.n == 1, err) end -function test_insert_with_index() +local function test_insert_with_index() local ok, err, ret local c = _create_client() local db = c[db_name] @@ -71,7 +76,7 @@ function test_insert_with_index() assert(ok == false and string.find(err, "duplicate key error")) end -function test_find_and_remove() +local function test_find_and_remove() local ok, err, ret local c = _create_client() local db = c[db_name] @@ -117,7 +122,7 @@ function test_find_and_remove() assert(ret == nil) end -function test_runcommand() +local function test_runcommand() local ok, err, ret local c = _create_client() local db = c[db_name] @@ -148,7 +153,7 @@ function test_runcommand() assert(ret and ret.cursor.firstBatch[1].test_key2_total == 6) end -function test_expire_index() +local function test_expire_index() local ok, err, ret local c = _create_client() local db = c[db_name] @@ -221,6 +226,26 @@ local function test_safe_batch_delete() assert((length - del_num) == ret:count(), "test safe batch delete failed") end +local function test_safe_update() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:drop() + + db.testcoll:safe_insert({test_key = 100, test_value = "hello mongo"}) + + db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local query = {test_key = 100} + local update = {test_value = "hi mongo"} + ok, err = db.testcoll:safe_update(query, {['$set'] = update}) + assert(ok, err) + + ret = db.testcoll:findOne(query) + assert(ret.test_value == "hi mongo") +end + skynet.start(function() if username then print("Test auth") @@ -240,5 +265,7 @@ skynet.start(function() test_safe_batch_insert() print("test safe batch delete") test_safe_batch_delete() + print("test_safe_update") + test_safe_update() print("mongodb test finish."); end) From 6bdc8b8608dcfc5e00070e331b284a801ee3b80d Mon Sep 17 00:00:00 2001 From: t0350 Date: Fri, 12 Jan 2024 18:10:36 +0800 Subject: [PATCH 513/565] bugfix, parsing http array form (#1852) --- lualib/http/url.lua | 12 +++++++++++- test/testhttp.lua | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lualib/http/url.lua b/lualib/http/url.lua index ae74b0993..72a9c7e5e 100644 --- a/lualib/http/url.lua +++ b/lualib/http/url.lua @@ -20,7 +20,17 @@ end function url.parse_query(q) local r = {} for k,v in q:gmatch "(.-)=([^&]*)&?" do - r[decode(k)] = decode(v) + local dk, dv = decode(k), decode(v) + local oldv = r[dk] + if oldv then + if type(oldv) ~= "table" then + r[dk] = {oldv, dv} + else + oldv[#oldv+1] = dv + end + else + r[dk] = dv + end end return r end diff --git a/test/testhttp.lua b/test/testhttp.lua index 5391574f9..9c0ce789a 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local httpc = require "http.httpc" +local httpurl = require "http.url" local dns = require "skynet.dns" local function http_test(protocol) @@ -44,11 +45,25 @@ local function http_head_test() end end +local function http_url_test() + local url = "http://baidu.com/get?k1=1&k2=2&k4=a%20space&k5=b%20space&k5=b%20space&k5=b%20space" + local path, query = httpurl.parse(url) + print("url", path, query) + local qret = httpurl.parse_query(query) + for k, v in pairs(qret) do + print(k, v) + end + assert(#qret["k5"] == 3) + assert(qret[1] == qret[2]) + assert(qret[1] == qret[3]) +end + local function main() dns.server() http_stream_test() http_head_test() + http_url_test() http_test("http") if not pcall(require,"ltls.c") then From d0b11c7955395fc79671b1bdfc5f068b0a322777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:57:26 +0800 Subject: [PATCH 514/565] remove unused local variable (#1864) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix word err * fix word err * 增加mongo update 测试 * remove unused local variable --- examples/login/client.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/login/client.lua b/examples/login/client.lua index dfb51a894..b369a736a 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -79,7 +79,6 @@ local function encode_token(token) end local etoken = crypt.desencode(secret, encode_token(token)) -local b = crypt.base64encode(etoken) writeline(fd, crypt.base64encode(etoken)) local result = readline() From 6f24b08f02faf8bdb924b576208d0d0a53a51236 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Fri, 2 Feb 2024 15:58:57 +0800 Subject: [PATCH 515/565] =?UTF-8?q?mongo=20=E6=94=AF=E6=8C=81=20hint=20(#1?= =?UTF-8?q?865)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mongo 支持 hint mongodb查询有时不会命中最优索引,所以支持下 hint 使用 --- lualib/skynet/db/mongo.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 325a68c9a..30f441b9f 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -511,9 +511,10 @@ function mongo_collection:find(query, projection) __data = nil, __cursor = nil, __document = {}, + __sort = empty_bson, __skip = 0, __limit = 0, - __sort = empty_bson, + __hint = nil } , cursor_meta) end @@ -548,6 +549,11 @@ function mongo_cursor:limit(amount) return self end +function mongo_cursor:hint(indexName) + self.__hint = indexName + return self +end + function mongo_cursor:count(with_limit_and_skip) local ret if with_limit_and_skip then @@ -690,6 +696,13 @@ function mongo_collection:aggregate(pipeline, options) } , aggregate_cursor_meta) end +local function add_hint(self) + local h = self.__hint + if h then + return "hint", h + end +end + function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then @@ -701,7 +714,7 @@ function mongo_cursor:hasNext() if self.__data == nil then local name = self.__collection.name response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort, - "skip", self.__skip, "limit", self.__limit, "projection", self.__projection) + "skip", self.__skip, "limit", self.__limit, "projection", self.__projection, add_hint(self)) else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name From 267cf5384ceffe444a3d1bd4eb4dc903d34ab51e Mon Sep 17 00:00:00 2001 From: emmanuel <154705254+codesmith-emmy@users.noreply.github.com> Date: Tue, 13 Feb 2024 02:20:44 +0100 Subject: [PATCH 516/565] Update README.md (#1870) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 540ef8b68..d7ae55c1f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Skynet is a multi-user Lua framework supporting the actor model, often used in g [It is heavily used in the Chinese game industry](https://github.com/cloudwu/skynet/wiki/Uses), but is also now spreading to other industries, and to English-centric developers. To visit related sites, visit the Chinese pages using something like Google or Deepl translate. -The community is friendly and almost contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or sumbit issues in English. +The community is friendly and almost all contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or sumbit issues in English. ## Build From bd5f12b959a52ffff26b305a986ce53610192444 Mon Sep 17 00:00:00 2001 From: coldskycoldsky Date: Fri, 1 Mar 2024 14:08:27 +0800 Subject: [PATCH 517/565] =?UTF-8?q?fix:=20=E5=BD=93=20mysql=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=97=A5=E6=9C=9F=E7=B1=BB=E5=9E=8B(timestam?= =?UTF-8?q?p,=20datetime)=E7=9A=84=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=80=BC=E6=B2=A1=E6=9C=89=20"=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E9=83=A8=E5=88=86=EF=BC=88=E6=97=B6=E5=88=86=E7=A7=92=EF=BC=89?= =?UTF-8?q?"=20=E6=97=B6=EF=BC=8C=E6=AF=94=E5=A6=82=20"2019-01-01"=20(#187?= =?UTF-8?q?9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 数据的长度是4(年2,月1, 日1), 而不是预期的7(年2,月1, 日1, 时1, 分1, 秒1) --- lualib/skynet/db/mysql.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 44b58a650..afa6e2292 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -841,6 +841,9 @@ local function _get_datetime(data, pos) if len == 7 then year, month, day, hour, minute, second, pos = string.unpack(" Date: Thu, 7 Mar 2024 10:13:22 +0800 Subject: [PATCH 518/565] =?UTF-8?q?mongodb=20=E6=9F=A5=E8=AF=A2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20maxTimeMS=20(#1882)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 查询增加 maxTimeMS * Update mongo.lua --- lualib/skynet/db/mongo.lua | 52 +++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 30f441b9f..3ce55341d 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -511,10 +511,7 @@ function mongo_collection:find(query, projection) __data = nil, __cursor = nil, __document = {}, - __sort = empty_bson, - __skip = 0, - __limit = 0, - __hint = nil + __sort = empty_bson } , cursor_meta) end @@ -554,15 +551,47 @@ function mongo_cursor:hint(indexName) return self end +function mongo_cursor:maxTimeMS(ms) + self.__maxTimeMS = ms + return self +end + +local opt_func = {} + +local function opt_define(name) + local key = "__" .. name + opt_func[name] = function (self, ...) + local v = self[key] + if v ~= nil then + return name, v, ... + else + return ... + end + end +end + +opt_define "skip" +opt_define "limit" +opt_define "hint" +opt_define "maxTimeMS" + +local function add_opt(self, opt, ...) + if opt == nil then + return + end + return opt_func[opt](self, add_opt(self, ...)) +end + function mongo_cursor:count(with_limit_and_skip) local ret if with_limit_and_skip then ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query, - 'limit', self.__limit, 'skip', self.__skip) + add_opt(self, "skip", "limit", "hint", "maxTimeMS")) else - ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query) + ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query, + add_opt(self, "hint", "maxTimeMS")) end - assert(ret and ret.ok == 1) + assert(ret.ok == 1, ret.errmsg) return ret.n end @@ -696,13 +725,6 @@ function mongo_collection:aggregate(pipeline, options) } , aggregate_cursor_meta) end -local function add_hint(self) - local h = self.__hint - if h then - return "hint", h - end -end - function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then @@ -714,7 +736,7 @@ function mongo_cursor:hasNext() if self.__data == nil then local name = self.__collection.name response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort, - "skip", self.__skip, "limit", self.__limit, "projection", self.__projection, add_hint(self)) + "projection", self.__projection, add_opt(self, "skip", "limit", "hint", "maxTimeMS")) else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name From 3bbb62e67324043be143b991bbc3867a8dc8adf6 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Thu, 7 Mar 2024 19:20:51 +0800 Subject: [PATCH 519/565] Update mongo.lua (#1884) --- lualib/skynet/db/mongo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 3ce55341d..8f5430bb5 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -763,7 +763,7 @@ function mongo_cursor:hasNext() self.__cursor = cursor.id local limit = self.__limit - if cursor.id > 0 and limit > 0 then + if limit and limit > 0 and cursor.id > 0 then limit = limit - #self.__document if limit <= 0 then -- reach limit From 72688f5771781bfc47c1b321bc86ed1646b68146 Mon Sep 17 00:00:00 2001 From: iamwlj Date: Mon, 18 Mar 2024 18:54:12 +0800 Subject: [PATCH 520/565] Update msgserver.lua (#1889) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用本地变量,不然本地变量`request_handler`未使用 --- 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 5ce9c0e76..bf5b3d8bc 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -262,7 +262,7 @@ function server.start(conf) if p == nil then p = { fd } u.response[session] = p - local ok, result = pcall(conf.request_handler, u.username, message) + local ok, result = pcall(request_handler, u.username, message) -- NOTICE: YIELD here, socket may close. result = result or "" if not ok then From 9fd43778e2c5973e234b2de1f931f4c45b2f63c6 Mon Sep 17 00:00:00 2001 From: iamwlj Date: Mon, 18 Mar 2024 18:58:54 +0800 Subject: [PATCH 521/565] Update skynet_main.c (#1890) Putting the relevant code together makes it easier to understand --- skynet-src/skynet_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index a96c312d9..5cbcc7e99 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -151,6 +151,7 @@ main(int argc, char *argv[]) { return 1; } _init_env(L); + lua_close(L); config.thread = optint("thread",8); config.module_path = optstring("cpath","./cservice/?.so"); @@ -161,8 +162,6 @@ main(int argc, char *argv[]) { config.logservice = optstring("logservice", "logger"); config.profile = optboolean("profile", 1); - lua_close(L); - skynet_start(&config); skynet_globalexit(); From cde8f75d68106006406704252e5d7cbd0bfc398e Mon Sep 17 00:00:00 2001 From: iamwlj Date: Mon, 18 Mar 2024 22:54:39 +0800 Subject: [PATCH 522/565] Update bootstrap.lua (#1891) 1. local var `harbor' not used 2. ` local skynet = require "skynet" require "skynet.manager" ` can be simplified as `local skynet = require "skynet.manager"` --- service/bootstrap.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 02d19dd03..9d762f1cf 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,7 +1,5 @@ -local skynet = require "skynet" -local harbor = require "skynet.harbor" local service = require "skynet.service" -require "skynet.manager" -- import skynet.launch, ... +local skynet = require "skynet.manager" -- import skynet.launch, ... skynet.start(function() local standalone = skynet.getenv "standalone" From 43225367fbdd7034b92138c72122d0f383626a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:36:11 +0800 Subject: [PATCH 523/565] =?UTF-8?q?fix=20websocket=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=BF=9E=E6=8E=A5=E8=BF=87=E7=A8=8B=E4=B8=AD=E6=8F=A1?= =?UTF-8?q?=E6=89=8B=E5=A4=B1=E8=B4=A5=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F=20(#1899)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix word err * fix word err * 增加mongo update 测试 * remove unused local variable * fix websocket客户端连接过程中握手失败导致内存泄漏 --- lualib/http/websocket.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 431fc0ecb..163d3d8b2 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -14,6 +14,7 @@ local pairs = pairs local error = error local string = string local xpcall = xpcall +local pcall = pcall local debug = debug local table = table local tonumber = tonumber @@ -481,7 +482,12 @@ function M.connect(url, header, timeout) local socket_id = sockethelper.connect(host_addr, host_port, timeout) local ws_obj = _new_client_ws(socket_id, protocol, hostname) ws_obj.addr = host - write_handshake(ws_obj, host_addr, uri, header) + + local is_ok,err = pcall(write_handshake, ws_obj, host_addr, uri, header) + if not is_ok then + _close_websocket(ws_obj) + error(err) + end return socket_id end From 1d47bfa0b694c64e53ed977ce8341ebf94845615 Mon Sep 17 00:00:00 2001 From: yzj12138 <71420106+yzj12138@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:01:46 +0800 Subject: [PATCH 524/565] =?UTF-8?q?session=E6=88=96=E8=80=85source?= =?UTF-8?q?=E4=B8=BA0=E6=97=B6=E4=B8=8D=E5=9B=9E=E6=B6=88=E6=81=AF=20(#189?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xingfan.yzj --- lualib/skynet.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 84c649e85..64b5b81db 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -653,7 +653,9 @@ function skynet.exit() c.send(address, skynet.PTYPE_ERROR, 0, "") end c.callback(function(prototype, msg, sz, session, source) - c.send(source, skynet.PTYPE_ERROR, session, "") + if session ~= 0 and source ~= 0 then + c.send(source, skynet.PTYPE_ERROR, session, "") + end end) c.command("EXIT") -- quit service From 22df64244a73312daa8c75b239230efe54a4e54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:40:41 +0800 Subject: [PATCH 525/565] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E5=87=BA?= =?UTF-8?q?=E9=94=99=E6=8A=9B=E5=87=BA=E6=9B=B4=E8=AF=A6=E7=BB=86=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=20(#1900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 优化http出错抛出更详细的错误信息 * 优化http err_info写法 --- lualib/http/httpc.lua | 4 ++++ lualib/http/httpd.lua | 5 +++++ lualib/http/internal.lua | 6 ++++++ lualib/http/sockethelper.lua | 35 +++++++++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index bc18dad0c..545956486 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -2,8 +2,12 @@ local skynet = require "skynet" local socket = require "http.sockethelper" local internal = require "http.internal" local dns = require "skynet.dns" + local string = string local table = table +local pcall = pcall +local error = error +local pairs = pairs local httpc = {} diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 1575f5a64..fb3fb2e88 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -2,6 +2,11 @@ local internal = require "http.internal" local string = string local type = type +local assert = assert +local tonumber = tonumber +local pcall = pcall +local ipairs = ipairs +local pairs = pairs local httpd = {} diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 204767be5..a34b7df6b 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,5 +1,11 @@ local table = table local type = type +local string = string +local tonumber = tonumber +local pcall = pcall +local assert = assert +local error = error +local pairs = pairs local M = {} diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 10b323461..9f0a6d03c 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,11 +1,26 @@ local socket = require "skynet.socket" local skynet = require "skynet" +local coroutine = coroutine +local error = error +local tostring = tostring + local readbytes = socket.read local writebytes = socket.write local sockethelper = {} -local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end }) +local socket_error = setmetatable({} , { + __tostring = function(self) + local info = self.err_info + self.err_info = nil + return info or "[Socket Error]" + end, + + __call = function (self, info) + self.err_info = "[Socket Error] : " .. tostring(info) + return self + end +}) sockethelper.socket_error = socket_error @@ -27,7 +42,7 @@ local function preread(fd, str) if ret then return str .. ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -36,7 +51,7 @@ local function preread(fd, str) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -51,7 +66,7 @@ function sockethelper.readfunc(fd, pre) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -62,24 +77,27 @@ function sockethelper.writefunc(fd) return function(content) local ok = writebytes(fd, content) if not ok then - error(socket_error) + error(socket_error("write failed fd = " .. fd)) end end end function sockethelper.connect(host, port, timeout) - local fd + local fd, err + local is_time_out = false if timeout then + is_time_out = true local drop_fd local co = coroutine.running() -- asynchronous connect skynet.fork(function() - fd = socket.open(host, port) + fd, err = 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. + is_time_out = false skynet.wakeup(co) end end) @@ -89,13 +107,14 @@ function sockethelper.connect(host, port, timeout) drop_fd = true end else + is_time_out = false -- block connect fd = socket.open(host, port) end if fd then return fd end - error(socket_error) + error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. timeout .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) end function sockethelper.close(fd) From 18a6dc57861f2c6535c7a2d17e6df6d6f6261ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:51:03 +0800 Subject: [PATCH 526/565] typo rename faild to failed (#1901) --- HISTORY.md | 2 +- lualib-src/ltls.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 846a470f6..ef8c3a50c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -352,7 +352,7 @@ v0.6.0 (2014-8-18) * 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 +* skynet.newservice throw error when lanuch failed * 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 diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index bf3c3c9ac..efac8bbce 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -35,18 +35,18 @@ static void _init_bio(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { tls_p->ssl = SSL_new(ctx_p->ctx); if(!tls_p->ssl) { - luaL_error(L, "SSL_new faild"); + luaL_error(L, "SSL_new failed"); } tls_p->in_bio = BIO_new(BIO_s_mem()); if(!tls_p->in_bio) { - luaL_error(L, "new in bio faild"); + luaL_error(L, "new in bio failed"); } BIO_set_mem_eof_return(tls_p->in_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ tls_p->out_bio = BIO_new(BIO_s_mem()); if(!tls_p->out_bio) { - luaL_error(L, "new out bio faild"); + luaL_error(L, "new out bio failed"); } BIO_set_mem_eof_return(tls_p->out_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ @@ -330,7 +330,7 @@ lnew_ctx(lua_State* L) { unsigned int err = ERR_get_error(); char buf[256]; ERR_error_string_n(err, buf, sizeof(buf)); - luaL_error(L, "SSL_CTX_new client faild. %s\n", buf); + luaL_error(L, "SSL_CTX_new client failed. %s\n", buf); } if(luaL_newmetatable(L, "_TLS_SSLCTX_METATABLE_")) { From caf513e6c0c68cfa7d61e1b294de7be693d94df0 Mon Sep 17 00:00:00 2001 From: t0350 Date: Tue, 23 Apr 2024 15:32:36 +0800 Subject: [PATCH 527/565] make task/uniqtask consistent (#1902) --- lualib/skynet.lua | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 64b5b81db..f983b2b39 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -1076,10 +1076,20 @@ function skynet.stat(what) return c.intcommand("STAT", what) end +local function task_traceback(co) + if co == "BREAK" then + return co + elseif timeout_traceback and timeout_traceback[co] then + return timeout_traceback[co] + else + return traceback(co) + end +end + function skynet.task(ret) if ret == nil then local t = 0 - for session,co in pairs(session_id_coroutine) do + for _,co in pairs(session_id_coroutine) do if co ~= "BREAK" then t = t + 1 end @@ -1097,23 +1107,13 @@ function skynet.task(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do local key = string.format("%s session: %d", tostring(co), session) - if co == "BREAK" then - ret[key] = "BREAK" - elseif timeout_traceback and timeout_traceback[co] then - ret[key] = timeout_traceback[co] - else - ret[key] = traceback(co) - end + ret[key] = task_traceback(co) end return elseif tt == "number" then local co = session_id_coroutine[ret] if co then - if co == "BREAK" then - return "BREAK" - else - return traceback(co) - end + return task_traceback(co) else return "No session" end @@ -1130,7 +1130,7 @@ end function skynet.uniqtask() local stacks = {} for session, co in pairs(session_id_coroutine) do - local stack = traceback(co) + local stack = task_traceback(co) local info = stacks[stack] or {count = 0, sessions = {}} info.count = info.count + 1 if info.count < 10 then From 1a0041982b43d1665a82d63b873348be67b68c85 Mon Sep 17 00:00:00 2001 From: chengf2018 <38105186+chengf2018@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:16:17 +0800 Subject: [PATCH 528/565] fix annotation (#1903) --- skynet-src/socket_server.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index e21a17415..a3cf96338 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -192,20 +192,20 @@ struct request_udp { /* The first byte is TYPE - - S Start socket + R Resume socket + S Pause socket B Bind socket L Listen socket K Close socket O Connect to (Open) - X Exit + X Exit socket thread + W Enable write D Send package (high) P Send package (low) A Send UDP package + C set udp address T Set opt U Create UDP socket - C set udp address - Q query info */ struct request_package { @@ -1372,6 +1372,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { int type = header[0]; int len = header[1]; block_readpipe(fd, buffer, len); + // ctrl command only exist in local fd, so don't worry about endian. switch (type) { case 'R': From 267e4ad44d042b44c60a7afde8d6df448169be89 Mon Sep 17 00:00:00 2001 From: yzj12138 <71420106+yzj12138@users.noreply.github.com> Date: Thu, 9 May 2024 18:06:47 +0800 Subject: [PATCH 529/565] =?UTF-8?q?skynet=E4=BD=BF=E7=94=A8=E5=8E=9F?= =?UTF-8?q?=E7=94=9Ferror=EF=BC=8C=E9=98=B2=E6=AD=A2=E7=BB=99=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20(#1906)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xingfan.yzj --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index f983b2b39..e1322965a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -4,6 +4,7 @@ local skynet_require = require "skynet.require" local tostring = tostring local coroutine = coroutine local assert = assert +local error = error local pairs = pairs local pcall = pcall local table = table From 89a821ced05f163b07b9e74193ad1a9337ba6205 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 May 2024 23:17:38 +0800 Subject: [PATCH 530/565] Avoid to use je_malloc_usable_size, See #1907 --- skynet-src/malloc_hook.c | 90 ++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 44c718302..9f532b386 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -24,10 +24,12 @@ struct mem_data { }; struct mem_cookie { + size_t size; uint32_t handle; #ifdef MEMORY_CHECK uint32_t dogtag; #endif + uint32_t cookie_size; // should be the last }; #define SLOT_SIZE 0x10000 @@ -86,37 +88,42 @@ update_xmalloc_stat_free(uint32_t handle, size_t __n) { } inline static void* -fill_prefix(char* ptr) { +fill_prefix(char* ptr, size_t sz, uint32_t cookie_size) { uint32_t handle = skynet_current_handle(); - size_t size = je_malloc_usable_size(ptr); - struct mem_cookie *p = (struct mem_cookie *)(ptr + size - sizeof(struct mem_cookie)); - memcpy(&p->handle, &handle, sizeof(handle)); + struct mem_cookie *p = (struct mem_cookie *)ptr; + char * ret = ptr + cookie_size; + p->size = sz; + p->handle = handle; #ifdef MEMORY_CHECK - uint32_t dogtag = MEMORY_ALLOCTAG; - memcpy(&p->dogtag, &dogtag, sizeof(dogtag)); + p->dogtag = MEMORY_ALLOCTAG; #endif - update_xmalloc_stat_alloc(handle, size); - return ptr; + update_xmalloc_stat_alloc(handle, sz); + memcpy(ret - sizeof(uint32_t), &cookie_size, sizeof(cookie_size)); + return ret; +} + +inline static uint32_t +get_cookie_size(char *ptr) { + uint32_t cookie_size; + memcpy(&cookie_size, ptr - sizeof(cookie_size), sizeof(cookie_size)); + return cookie_size; } inline static void* clean_prefix(char* ptr) { - size_t size = je_malloc_usable_size(ptr); - struct mem_cookie *p = (struct mem_cookie *)(ptr + size - sizeof(struct mem_cookie)); - uint32_t handle; - memcpy(&handle, &p->handle, sizeof(handle)); + uint32_t cookie_size = get_cookie_size(ptr); + struct mem_cookie *p = (struct mem_cookie *)(ptr - cookie_size); + uint32_t handle = p->handle; #ifdef MEMORY_CHECK - uint32_t dogtag; - memcpy(&dogtag, &p->dogtag, sizeof(dogtag)); + uint32_t dogtag = p->dogtag; if (dogtag == MEMORY_FREETAG) { fprintf(stderr, "xmalloc: double free in :%08x\n", handle); } assert(dogtag == MEMORY_ALLOCTAG); // memory out of bounds - dogtag = MEMORY_FREETAG; - memcpy(&p->dogtag, &dogtag, sizeof(dogtag)); + p->dogtag = MEMORY_FREETAG; #endif - update_xmalloc_stat_free(handle, size); - return ptr; + update_xmalloc_stat_free(handle, p->size); + return p; } static void malloc_oom(size_t size) { @@ -185,7 +192,7 @@ void * skynet_malloc(size_t size) { void* ptr = je_malloc(size + PREFIX_SIZE); if(!ptr) malloc_oom(size); - return fill_prefix(ptr); + return fill_prefix(ptr, size, PREFIX_SIZE); } void * @@ -193,9 +200,10 @@ skynet_realloc(void *ptr, size_t size) { if (ptr == NULL) return skynet_malloc(size); void* rawptr = clean_prefix(ptr); - void *newptr = je_realloc(rawptr, size+PREFIX_SIZE); + uint32_t cookie_size = get_cookie_size(ptr); + void *newptr = je_realloc(rawptr, size+cookie_size); if(!newptr) malloc_oom(size); - return fill_prefix(newptr); + return fill_prefix(newptr, size, cookie_size); } void @@ -206,31 +214,51 @@ skynet_free(void *ptr) { } void * -skynet_calloc(size_t nmemb,size_t size) { - void* ptr = je_calloc(nmemb + ((PREFIX_SIZE+size-1)/size), size ); - if(!ptr) malloc_oom(size); - return fill_prefix(ptr); +skynet_calloc(size_t nmemb, size_t size) { + uint32_t cookie_n = (PREFIX_SIZE+size-1)/size; + uint32_t n = nmemb + cookie_n; + void* ptr = je_calloc(n, size); + if(!ptr) malloc_oom(n * size); + return fill_prefix(ptr, n * size, cookie_n * size); +} + +static inline uint32_t +alignment_cookie_size(size_t alignment) { + if (alignment >= PREFIX_SIZE) + return alignment; + switch (alignment) { + case 4 : + return (PREFIX_SIZE + 3) / 4 * 4; + case 8 : + return (PREFIX_SIZE + 7) / 8 * 8; + case 16 : + return (PREFIX_SIZE + 15) / 16 * 16; + } + return (PREFIX_SIZE + alignment - 1) / alignment * alignment; } void * skynet_memalign(size_t alignment, size_t size) { - void* ptr = je_memalign(alignment, size + PREFIX_SIZE); + uint32_t cookie_size = alignment_cookie_size(alignment); + void* ptr = je_memalign(alignment, size + cookie_size); if(!ptr) malloc_oom(size); - return fill_prefix(ptr); + return fill_prefix(ptr, size, cookie_size); } void * skynet_aligned_alloc(size_t alignment, size_t size) { - void* ptr = je_aligned_alloc(alignment, size + (size_t)((PREFIX_SIZE + alignment -1) & ~(alignment-1))); + uint32_t cookie_size = alignment_cookie_size(alignment); + void* ptr = je_aligned_alloc(alignment, size + cookie_size); if(!ptr) malloc_oom(size); - return fill_prefix(ptr); + return fill_prefix(ptr, size, cookie_size); } int skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { - int err = je_posix_memalign(memptr, alignment, size + PREFIX_SIZE); + uint32_t cookie_size = alignment_cookie_size(alignment); + int err = je_posix_memalign(memptr, alignment, size + cookie_size); if (err) malloc_oom(size); - fill_prefix(*memptr); + fill_prefix(*memptr, size, cookie_size); return err; } From 8f7e9cc823d0c503c0c80ff9336c4c3b8334962b Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Fri, 10 May 2024 07:27:26 +0900 Subject: [PATCH 531/565] docs: update README.md (#1908) sumbit -> submit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d7ae55c1f..139b10df5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Skynet is a multi-user Lua framework supporting the actor model, often used in g [It is heavily used in the Chinese game industry](https://github.com/cloudwu/skynet/wiki/Uses), but is also now spreading to other industries, and to English-centric developers. To visit related sites, visit the Chinese pages using something like Google or Deepl translate. -The community is friendly and almost all contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or sumbit issues in English. +The community is friendly and almost all contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or submit issues in English. ## Build From 45b407481d5a9f9abcf3504ddb07549c08a33313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Fri, 17 May 2024 12:26:42 +0800 Subject: [PATCH 532/565] fix socketchannel mem leakage (#1921) --- lualib/skynet/socketchannel.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 871334c2a..24e8de75f 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -60,6 +60,10 @@ local function close_channel_socket(self) if self.__sock then local so = self.__sock self.__sock = false + if self.__wait_response then + skynet.wakeup(self.__wait_response) + self.__wait_response = nil + end -- never raise error pcall(socket.close,so[1]) end @@ -128,7 +132,7 @@ local function dispatch_by_session(self) end local function pop_response(self) - while true do + while self.__sock do local func,co = table.remove(self.__request, 1), table.remove(self.__thread, 1) if func then return func, co From c226ea3f1f0faa17aa600734179cc1d67776e864 Mon Sep 17 00:00:00 2001 From: King <260090656@qq.com> Date: Mon, 20 May 2024 15:56:55 +0800 Subject: [PATCH 533/565] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=93=E5=BC=80DEB?= =?UTF-8?q?UG=5FLOG=E6=97=B6=EF=BC=8C=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= =?UTF-8?q?=20(#1922)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service-src/service_snlua.c | 1 + 1 file changed, 1 insertion(+) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 43dd9c58c..462fea82f 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -188,6 +188,7 @@ timing_resume(lua_State *L, int co_index, int n) { if (timing_enable(L, co_index, &start_time)) { start_time = get_time(); #ifdef DEBUG_LOG + double ti = diff_time(start_time); fprintf(stderr, "PROFILE [%p] resume %lf\n", co, ti); #endif lua_pushvalue(L, co_index); From ccd923c44f2a50d5cc761f6f16787b8d0a6cf6a0 Mon Sep 17 00:00:00 2001 From: mippo520 <2570916196@qq.com> Date: Mon, 27 May 2024 18:31:51 +0800 Subject: [PATCH 534/565] =?UTF-8?q?skynet.wait=E4=B8=AD=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84session=E5=A2=9E=E5=8A=A0=E5=9B=9E=E7=BB=95=E5=88=A4?= =?UTF-8?q?=E6=96=AD=20(#1931)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * skynet.wait中使用的session增加回绕判断 * 去除session判空 --------- Co-authored-by: mippo --- lualib/skynet.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e1322965a..5b5762c31 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -70,7 +70,7 @@ local watching_session = {} local error_queue = {} local fork_queue = { h = 1, t = 0 } -local auxsend, auxtimeout +local auxsend, auxtimeout, auxwait do ---- avoid session rewind conflict local csend = c.send local cintcommand = c.intcommand @@ -140,6 +140,12 @@ do ---- avoid session rewind conflict return session end + local function auxwait_checkconflict() + local session = c.genid() + checkconflict(session) + return session + end + local function auxsend_checkrewind(addr, proto, msg, sz) local session = csend(addr, proto, nil, msg, sz) if session and session > dangerzone_low and session <= dangerzone_up then @@ -158,15 +164,26 @@ do ---- avoid session rewind conflict return session end + local function auxwait_checkrewind() + local session = c.genid() + if session > dangerzone_low and session <= dangerzone_up then + -- enter dangerzone + set_checkconflict(session) + end + return session + end + set_checkrewind = function() auxsend = auxsend_checkrewind auxtimeout = auxtimeout_checkrewind + auxwait = auxwait_checkrewind end set_checkconflict = function(session) reset_dangerzone(session) auxsend = auxsend_checkconflict auxtimeout = auxtimeout_checkconflict + auxwait = auxwait_checkconflict end -- in safezone at the beginning @@ -516,7 +533,7 @@ function skynet.yield() end function skynet.wait(token) - local session = c.genid() + local session = auxwait() token = token or coroutine.running() suspend_sleep(session, token) sleep_session[token] = nil From dc1980dfc83e5616987c642ffad450820ce0b381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Tue, 4 Jun 2024 11:19:10 +0800 Subject: [PATCH 535/565] fix attempt to concatenate a nil value (local timeout) (#1936) --- lualib/http/sockethelper.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 9f0a6d03c..73b058228 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -114,7 +114,7 @@ function sockethelper.connect(host, port, timeout) if fd then return fd end - error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. timeout .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) + error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. tostring(timeout) .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) end function sockethelper.close(fd) From f214c68ff3a630cbbf8a25a2eec2a93e7e05c191 Mon Sep 17 00:00:00 2001 From: anonymou3 Date: Wed, 19 Jun 2024 14:16:41 +0800 Subject: [PATCH 536/565] The listen function supports passing backlog parameters (#1941) Co-authored-by: anonymou3 --- 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 ee4386376..0f20ba387 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -43,7 +43,7 @@ function gateserver.start(handler) maxclient = conf.maxclient or 1024 nodelay = conf.nodelay skynet.error(string.format("Listen on %s:%d", address, port)) - socket = socketdriver.listen(address, port) + socket = socketdriver.listen(address, port, conf.backlog) listen_context.co = coroutine.running() listen_context.fd = socket skynet.wait(listen_context.co) From aa20f7642b37a507b21ec3e9730758498aa1cd9a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 24 Jun 2024 23:15:20 +0800 Subject: [PATCH 537/565] cleanup --- skynet-src/malloc_hook.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 9f532b386..9ec897557 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -199,8 +199,8 @@ void * skynet_realloc(void *ptr, size_t size) { if (ptr == NULL) return skynet_malloc(size); - void* rawptr = clean_prefix(ptr); uint32_t cookie_size = get_cookie_size(ptr); + void* rawptr = clean_prefix(ptr); void *newptr = je_realloc(rawptr, size+cookie_size); if(!newptr) malloc_oom(size); return fill_prefix(newptr, size, cookie_size); @@ -216,10 +216,9 @@ skynet_free(void *ptr) { void * skynet_calloc(size_t nmemb, size_t size) { uint32_t cookie_n = (PREFIX_SIZE+size-1)/size; - uint32_t n = nmemb + cookie_n; - void* ptr = je_calloc(n, size); - if(!ptr) malloc_oom(n * size); - return fill_prefix(ptr, n * size, cookie_n * size); + void* ptr = je_calloc(nmemb + cookie_n, size); + if(!ptr) malloc_oom(nmemb * size); + return fill_prefix(ptr, nmemb * size, cookie_n * size); } static inline uint32_t From afb50a0587d3758f02a34e5fe6a1da9e77ebe32e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Jun 2024 08:41:59 +0800 Subject: [PATCH 538/565] update lua 5.4.7 --- 3rd/lua/lapi.c | 4 +- 3rd/lua/lauxlib.c | 28 ++++-- 3rd/lua/lcode.c | 35 ++++---- 3rd/lua/lcode.h | 3 - 3rd/lua/ldebug.c | 218 ++++++++++++++++++++++++++------------------- 3rd/lua/ldebug.h | 1 + 3rd/lua/ldo.c | 10 ++- 3rd/lua/ldo.h | 1 - 3rd/lua/lgc.c | 20 +++-- 3rd/lua/liolib.c | 27 ++++-- 3rd/lua/lmathlib.c | 31 +++++-- 3rd/lua/loadlib.c | 9 -- 3rd/lua/lobject.c | 2 +- 3rd/lua/lobject.h | 18 ++-- 3rd/lua/lopcodes.h | 8 +- 3rd/lua/loslib.c | 2 + 3rd/lua/lparser.c | 12 +-- 3rd/lua/lstate.c | 6 +- 3rd/lua/lstate.h | 3 +- 3rd/lua/lstring.c | 13 +-- 3rd/lua/ltable.c | 39 +++++--- 3rd/lua/ltable.h | 2 - 3rd/lua/ltests.c | 12 ++- 3rd/lua/ltm.h | 5 +- 3rd/lua/lua.c | 17 +++- 3rd/lua/lua.h | 8 +- 3rd/lua/luaconf.h | 9 ++ 3rd/lua/lundump.c | 4 +- 3rd/lua/lundump.h | 3 +- 3rd/lua/lvm.c | 78 ++++++++-------- 3rd/lua/onelua.c | 20 ++++- 31 files changed, 388 insertions(+), 260 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 25184ece5..90f1fccd3 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -417,9 +417,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { o = index2value(L, idx); /* previous call may reallocate the stack */ } if (len != NULL) - *len = vslen(o); + *len = tsslen(tsvalue(o)); lua_unlock(L); - return svalue(o); + return getstr(tsvalue(o)); } diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 37c1f3f75..00fa50b60 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -80,6 +80,7 @@ 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, LUA_LOADED_TABLE); + luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */ if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ @@ -249,11 +250,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { return 1; } else { + const char *msg; luaL_pushfail(L); + msg = (en != 0) ? strerror(en) : "(no extra info)"; if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); + lua_pushfstring(L, "%s: %s", fname, msg); else - lua_pushstring(L, strerror(en)); + lua_pushstring(L, msg); lua_pushinteger(L, en); return 3; } @@ -732,9 +735,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); + int err = errno; const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + if (err != 0) + lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err)); + else + lua_pushfstring(L, "cannot %s %s", what, filename); lua_remove(L, fnameindex); return LUA_ERRFILE; } @@ -787,6 +793,7 @@ LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, } else { lua_pushfstring(L, "@%s", filename); + errno = 0; lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } @@ -796,6 +803,7 @@ LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, if (c == LUA_SIGNATURE[0]) { /* binary file? */ lf.n = 0; /* remove possible newline */ if (filename) { /* "real" file? */ + errno = 0; lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); skipcomment(lf.f, &c); /* re-read initial portion */ @@ -803,6 +811,7 @@ LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ + errno = 0; status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ @@ -933,7 +942,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ - if (l->func == NULL) /* place holder? */ + if (l->func == NULL) /* placeholder? */ lua_pushboolean(L, 0); else { int i; @@ -1025,9 +1034,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { } +/* +** Standard panic funcion just prints an error message. The test +** with 'lua_type' avoids possible memory errors in 'lua_tostring'. +*/ static int panic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); return 0; /* return to Lua to abort */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 1a371ca94..87616140e 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -415,7 +415,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { /* ** Format and emit an 'iAsBx' instruction. */ -int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { +static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { unsigned int b = bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); @@ -671,7 +671,7 @@ static int fitsBx (lua_Integer i) { void luaK_int (FuncState *fs, int reg, lua_Integer i) { if (fitsBx(i)) - luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); } @@ -680,7 +680,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) - luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); + codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); } @@ -776,7 +776,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { break; } case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.ridx; + int temp = e->u.var.ridx; + e->u.info = temp; /* (can't do a direct assignment; values overlap) */ e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -1025,7 +1026,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { ** in the range of R/K indices). ** Returns 1 iff expression is K. */ -int luaK_exp2RK (FuncState *fs, expdesc *e) { +static int exp2RK (FuncState *fs, expdesc *e) { if (luaK_exp2K(fs, e)) return 1; else { /* not a constant in the right range: put it in a register */ @@ -1037,7 +1038,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { static void codeABRK (FuncState *fs, OpCode o, int a, int b, expdesc *ec) { - int k = luaK_exp2RK(fs, ec); + int k = exp2RK(fs, ec); luaK_codeABCk(fs, o, a, b, ec->u.info, k); } @@ -1215,7 +1216,7 @@ static void codenot (FuncState *fs, expdesc *e) { /* -** Check whether expression 'e' is a small literal string +** Check whether expression 'e' is a short literal string */ static int isKstr (FuncState *fs, expdesc *e) { return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && @@ -1225,7 +1226,7 @@ static int isKstr (FuncState *fs, expdesc *e) { /* ** Check whether expression 'e' is a literal integer. */ -int luaK_isKint (expdesc *e) { +static int isKint (expdesc *e) { return (e->k == VKINT && !hasjumps(e)); } @@ -1235,7 +1236,7 @@ int luaK_isKint (expdesc *e) { ** proper range to fit in register C */ static int isCint (expdesc *e) { - return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); + return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); } @@ -1244,7 +1245,7 @@ static int isCint (expdesc *e) { ** proper range to fit in register sC */ static int isSCint (expdesc *e) { - return luaK_isKint(e) && fitsC(e->u.ival); + return isKint(e) && fitsC(e->u.ival); } @@ -1283,15 +1284,17 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { - t->u.ind.t = t->u.info; /* upvalue index */ - t->u.ind.idx = k->u.info; /* literal string */ + int temp = t->u.info; /* upvalue index */ + lua_assert(isKstr(fs, k)); + t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ + t->u.ind.idx = k->u.info; /* literal short string */ t->k = VINDEXUP; } else { /* register index of the table */ t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ + t->u.ind.idx = k->u.info; /* literal short string */ t->k = VINDEXSTR; } else if (isCint(k)) { @@ -1459,7 +1462,7 @@ static void codebinK (FuncState *fs, BinOpr opr, */ static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int line, TMS event) { - if (!luaK_isKint(e2)) + if (!isKint(e2)) return 0; /* not an integer constant */ else { lua_Integer i2 = e2->u.ival; @@ -1592,7 +1595,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { op = OP_EQI; r2 = im; /* immediate operand */ } - else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ + else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } @@ -1658,7 +1661,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } case OPR_EQ: case OPR_NE: { if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); + exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 326582445..0b971fc43 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -61,10 +61,8 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); -LUAI_FUNC int luaK_isKint (expdesc *e); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); @@ -76,7 +74,6 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 28b1caabf..591b3528a 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -31,7 +31,7 @@ -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) +#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL) static const char *funcnamefromcall (lua_State *L, CallInfo *ci, @@ -254,7 +254,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { + if (!LuaClosure(cl)) { ar->source = "=[C]"; ar->srclen = LL("=[C]"); ar->linedefined = -1; @@ -288,29 +288,31 @@ static int nextline (const Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { + if (!LuaClosure(f)) { setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { - int i; - TValue v; const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); - setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - if (!p->is_vararg) /* regular function? */ - i = 0; /* consider all instructions */ - else { /* vararg function */ - lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); - currentline = nextline(p, currentline, 0); - i = 1; /* skip first instruction (OP_VARARGPREP) */ - } - for (; i < p->sizelineinfo; i++) { /* for each instruction */ - currentline = nextline(p, currentline, i); /* get its line */ - luaH_setint(L, t, currentline, &v); /* table[line] = true */ + if (p->lineinfo != NULL) { /* proto with debug information? */ + int i; + TValue v; + setbtvalue(&v); /* boolean 'true' to be the value of all indices */ + if (!p->is_vararg) /* regular function? */ + i = 0; /* consider all instructions */ + else { /* vararg function */ + lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); + currentline = nextline(p, currentline, 0); + i = 1; /* skip first instruction (OP_VARARGPREP) */ + } + for (; i < p->sizelineinfo; i++) { /* for each instruction */ + currentline = nextline(p, currentline, i); /* get its line */ + luaH_setint(L, t, currentline, &v); /* table[line] = true */ + } } } } @@ -339,7 +341,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } case 'u': { ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { + if (!LuaClosure(f)) { ar->isvararg = 1; ar->nparams = 0; } @@ -417,40 +419,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name); - - -/* -** Find a "name" for the constant 'c'. -*/ -static void kname (const Proto *p, int c, const char **name) { - TValue *kvalue = &p->k[c]; - *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; -} - - -/* -** Find a "name" for the register 'c'. -*/ -static void rname (const Proto *p, int pc, int c, const char **name) { - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (!(what && *what == 'c')) /* did not find a constant name? */ - *name = "?"; -} - - -/* -** Find a "name" for a 'C' value in an RK instruction. -*/ -static void rkname (const Proto *p, int pc, Instruction i, const char **name) { - int c = GETARG_C(i); /* key index */ - if (GETARG_k(i)) /* is 'c' a constant? */ - kname(p, c, name); - else /* 'c' is a register */ - rname(p, pc, c, name); -} - static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ @@ -509,28 +477,29 @@ static int findsetreg (const Proto *p, int lastpc, int reg) { /* -** Check whether table being indexed by instruction 'i' is the -** environment '_ENV' +** Find a "name" for the constant 'c'. */ -static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { - int t = GETARG_B(i); /* table index */ - const char *name; /* name of indexed variable */ - if (isup) /* is an upvalue? */ - name = upvalname(p, t); - else - getobjname(p, pc, t, &name); - return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +static const char *kname (const Proto *p, int index, const char **name) { + TValue *kvalue = &p->k[index]; + if (ttisstring(kvalue)) { + *name = getstr(tsvalue(kvalue)); + return "constant"; + } + else { + *name = "?"; + return NULL; + } } -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); +static const char *basicgetobjname (const Proto *p, int *ppc, int reg, + const char **name) { + int pc = *ppc; + *name = luaF_getlocalname(p, reg + 1, pc); if (*name) /* is a local? */ return "local"; /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); + *ppc = pc = findsetreg(p, pc, reg); if (pc != -1) { /* could find instruction? */ Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); @@ -538,18 +507,80 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, case OP_MOVE: { int b = GETARG_B(i); /* move from 'b' to 'a' */ if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ + return basicgetobjname(p, ppc, b, name); /* get name for 'b' */ break; } + case OP_GETUPVAL: { + *name = upvalname(p, GETARG_B(i)); + return "upvalue"; + } + case OP_LOADK: return kname(p, GETARG_Bx(i), name); + case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name); + default: break; + } + } + return NULL; /* could not find reasonable name */ +} + + +/* +** Find a "name" for the register 'c'. +*/ +static void rname (const Proto *p, int pc, int c, const char **name) { + const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */ + if (!(what && *what == 'c')) /* did not find a constant name? */ + *name = "?"; +} + + +/* +** Find a "name" for a 'C' value in an RK instruction. +*/ +static void rkname (const Proto *p, int pc, Instruction i, const char **name) { + int c = GETARG_C(i); /* key index */ + if (GETARG_k(i)) /* is 'c' a constant? */ + kname(p, c, name); + else /* 'c' is a register */ + rname(p, pc, c, name); +} + + +/* +** Check whether table being indexed by instruction 'i' is the +** environment '_ENV' +*/ +static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) { + int t = GETARG_B(i); /* table index */ + const char *name; /* name of indexed variable */ + if (isup) /* is 't' an upvalue? */ + name = upvalname(p, t); + else /* 't' is a register */ + basicgetobjname(p, &pc, t, &name); + return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +} + + +/* +** Extend 'basicgetobjname' to handle table accesses +*/ +static const char *getobjname (const Proto *p, int lastpc, int reg, + const char **name) { + const char *kind = basicgetobjname(p, &lastpc, reg, name); + if (kind != NULL) + return kind; + else if (lastpc != -1) { /* could find instruction? */ + Instruction i = p->code[lastpc]; + OpCode op = GET_OPCODE(i); + switch (op) { case OP_GETTABUP: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 1); + return isEnv(p, lastpc, i, 1); } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ - rname(p, pc, k, name); - return gxf(p, pc, i, 0); + rname(p, lastpc, k, name); + return isEnv(p, lastpc, i, 0); } case OP_GETI: { *name = "integer index"; @@ -558,24 +589,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, case OP_GETFIELD: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 0); - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); - return "constant"; - } - break; + return isEnv(p, lastpc, i, 0); } case OP_SELF: { - rkname(p, pc, i, name); + rkname(p, lastpc, i, name); return "method"; } default: break; /* go through to return NULL */ @@ -627,7 +644,7 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p, default: return NULL; /* cannot find a reasonable name */ } - *name = getstr(G(L)->tmname[tm]) + 2; + *name = getshrstr(G(L)->tmname[tm]) + 2; return "metamethod"; } @@ -865,6 +882,28 @@ static int changedline (const Proto *p, int oldpc, int newpc) { } +/* +** Traces Lua calls. If code is running the first instruction of a function, +** and function is not vararg, and it is not coming from an yield, +** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall' +** after adjusting its variable arguments; otherwise, they could call +** a line/count hook before the call hook. Functions coming from +** an yield already called 'luaD_hookcall' before yielding.) +*/ +int luaG_tracecall (lua_State *L) { + CallInfo *ci = L->ci; + Proto *p = ci_func(ci)->p; + ci->u.l.trap = 1; /* ensure hooks will be checked */ + if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ + if (p->is_vararg) + return 0; /* hooks will start at VARARGPREP instruction */ + else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */ + luaD_hookcall(L, ci); /* check 'call' hook */ + } + return 1; /* keep 'trap' on */ +} + + /* ** Traces the execution of a Lua function. Called before the execution ** of each opcode, when debug is on. 'L->oldpc' stores the last @@ -888,12 +927,12 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { } pc++; /* reference is always next instruction */ ci->u.l.savedpc = pc; /* save 'pc' */ - counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return 1; /* no line hook and count != 0; nothing to be done now */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } @@ -915,7 +954,6 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { 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 */ luaD_throw(L, LUA_YIELD); } diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 2c3074c61..2bfce3cb5 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -58,6 +58,7 @@ 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 int luaG_traceexec (lua_State *L, const Instruction *pc); +LUAI_FUNC int luaG_tracecall (lua_State *L); #endif diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 2a0017ca6..ea0529507 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -409,7 +409,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ -StkId luaD_tryfuncTM (lua_State *L, StkId func) { +static StkId tryfuncTM (lua_State *L, StkId func) { const TValue *tm; StkId p; checkstackGCp(L, 1, func); /* space for metamethod */ @@ -568,7 +568,7 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, return -1; } default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = tryfuncTM(L, func); /* try to get '__call' metamethod */ /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ narg1++; goto retry; /* try again */ @@ -609,7 +609,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { return ci; } default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = tryfuncTM(L, func); /* try to get '__call' metamethod */ /* return luaD_precall(L, func, nresults); */ goto retry; /* try again with metamethod */ } @@ -792,6 +792,10 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) { /* yielded inside a hook? */ + /* undo increment made by 'luaG_traceexec': instruction was not + executed yet */ + lua_assert(ci->callstatus & CIST_HOOKYIELD); + ci->u.l.savedpc--; L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ } diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 1aa446ad0..56008ab30 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -71,7 +71,6 @@ LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 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 StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index fe376fe6b..c86e101c7 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -542,10 +542,12 @@ 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); + TString *smode; markobjectN(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - (cast_void(weakkey = strchr(svalue(mode), 'k')), - cast_void(weakvalue = strchr(svalue(mode), 'v')), + if (mode && ttisshrstring(mode) && /* is there a weak mode? */ + (cast_void(smode = tsvalue(mode)), + cast_void(weakkey = strchr(getshrstr(smode), 'k')), + cast_void(weakvalue = strchr(getshrstr(smode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); @@ -638,7 +640,9 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last.p + EXTRA_STACK; o++) + if (!g->gcemergency) + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ + for (o = th->top.p; o < th->stack_last.p + EXTRA_STACK; o++) setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -646,8 +650,6 @@ static int traversethread (global_State *g, lua_State *th) { g->twups = th; } } - else if (!g->gcemergency) - luaD_shrinkstack(th); /* do not change stack in emergency cycle */ return 1 + stacksize(th); } @@ -1415,7 +1417,7 @@ static void stepgenfull (lua_State *L, global_State *g) { setminordebt(g); } else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalbytes(g); /* first estimate */; + g->GCestimate = gettotalbytes(g); /* first estimate */ entersweep(L); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); @@ -1610,7 +1612,7 @@ static lu_mem singlestep (lua_State *L) { case GCSenteratomic: { work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; + g->GCestimate = gettotalbytes(g); /* first estimate */ break; } case GCSswpallgc: { /* sweep "regular" objects */ @@ -1716,6 +1718,8 @@ static void fullinc (lua_State *L, global_State *g) { 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(GCSpropagate)); /* start new cycle */ + g->gcstate = GCSenteratomic; /* go straight to atomic phase */ luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ /* estimate must be correct after a full GC cycle */ lua_assert(g->GCestimate == gettotalbytes(g)); diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index b08397da4..c5075f3e7 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -245,8 +245,8 @@ static int f_gc (lua_State *L) { */ static int io_fclose (lua_State *L) { LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); + errno = 0; + return luaL_fileresult(L, (fclose(p->f) == 0), NULL); } @@ -272,6 +272,7 @@ static int io_open (lua_State *L) { LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); + errno = 0; p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -292,6 +293,7 @@ static int io_popen (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); + errno = 0; p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; @@ -300,6 +302,7 @@ static int io_popen (lua_State *L) { static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); + errno = 0; p->f = tmpfile(); return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } @@ -567,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; int n, success; clearerr(f); + errno = 0; if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); n = first + 1; /* to return 1 result */ @@ -660,6 +664,7 @@ static int io_readline (lua_State *L) { static int g_write (lua_State *L, FILE *f, int arg) { int nargs = lua_gettop(L) - arg; int status = 1; + errno = 0; for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ @@ -678,7 +683,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { } if (l_likely(status)) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); + else + return luaL_fileresult(L, status, NULL); } @@ -703,6 +709,7 @@ static int f_seek (lua_State *L) { l_seeknum offset = (l_seeknum)p3; luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); + errno = 0; op = l_fseek(f, offset, mode[op]); if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ @@ -719,19 +726,25 @@ 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], (size_t)sz); + int res; + errno = 0; + res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); + FILE *f = getiofile(L, IO_OUTPUT); + errno = 0; + return luaL_fileresult(L, fflush(f) == 0, NULL); } static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); + FILE *f = tofile(L); + errno = 0; + return luaL_fileresult(L, fflush(f) == 0, NULL); } @@ -773,7 +786,7 @@ static const luaL_Reg meth[] = { ** metamethods for file handles */ static const luaL_Reg metameth[] = { - {"__index", NULL}, /* place holder */ + {"__index", NULL}, /* placeholder */ {"__gc", f_gc}, {"__close", f_gc}, {"__tostring", f_tostring}, diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index d0b1e1e5d..438106348 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -249,6 +249,15 @@ static int math_type (lua_State *L) { ** =================================================================== */ +/* +** This code uses lots of shifts. ANSI C does not allow shifts greater +** than or equal to the width of the type being shifted, so some shifts +** are written in convoluted ways to match that restriction. For +** preprocessor tests, it assumes a width of 32 bits, so the maximum +** shift there is 31 bits. +*/ + + /* number of binary digits in the mantissa of a float */ #define FIGS l_floatatt(MANT_DIG) @@ -271,16 +280,19 @@ static int math_type (lua_State *L) { /* 'long' has at least 64 bits */ #define Rand64 unsigned long +#define SRand64 long #elif !defined(LUA_USE_C89) && defined(LLONG_MAX) /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long +#define SRand64 long long #elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 /* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned +#define SRand64 lua_Integer #endif @@ -319,23 +331,30 @@ static Rand64 nextrand (Rand64 *state) { } -/* must take care to not shift stuff by more than 63 slots */ - - /* ** Convert bits from a random integer into a float in the ** interval [0,1), getting the higher FIG bits from the ** random unsigned integer and converting that to a float. +** Some old Microsoft compilers cannot cast an unsigned long +** to a floating-point number, so we use a signed long as an +** intermediary. When lua_Number is float or double, the shift ensures +** that 'sx' is non negative; in that case, a good compiler will remove +** the correction. */ /* must throw out the extra (64 - FIGS) bits */ #define shift64_FIG (64 - FIGS) -/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ +/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */ #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) static lua_Number I2d (Rand64 x) { - return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; + SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG); + lua_Number res = (lua_Number)(sx) * scaleFIG; + if (sx < 0) + res += l_mathop(1.0); /* correct the two's complement if negative */ + lua_assert(0 <= res && res < 1); + return res; } /* convert a 'Rand64' to a 'lua_Unsigned' */ @@ -471,8 +490,6 @@ static lua_Number I2d (Rand64 x) { #else /* 32 < FIGS <= 64 */ -/* must take care to not shift stuff by more than 31 slots */ - /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index d792dffaa..6d289fceb 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -24,15 +24,6 @@ #include "lualib.h" -/* -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif - - /* ** LUA_CSUBSEP is the character that replaces dots in submodule names ** when searching for a C loader. diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index f73ffc6d9..9cfa5227e 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -542,7 +542,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(&buff); /* empty buffer into the stack */ lua_assert(buff.pushed == 1); - return svalue(s2v(L->top.p - 1)); + return getstr(tsvalue(s2v(L->top.p - 1))); } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index aeaa9d563..fa15a01df 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -386,7 +386,7 @@ typedef struct GCObject { typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings */ + lu_byte shrlen; /* length for short strings, 0xFF for long strings */ unsigned int hash; size_t id; /* id for short strings */ union { @@ -399,19 +399,17 @@ typedef struct TString { /* -** Get the actual string (array of bytes) from a 'TString'. +** Get the actual string (array of bytes) from a 'TString'. (Generic +** version and specialized versions for long and short strings.) */ -#define getstr(ts) ((ts)->contents) +#define getstr(ts) ((ts)->contents) +#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) +#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) -/* 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_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) - -/* get string length from 'TValue *o' */ -#define vslen(o) tsslen(tsvalue(o)) +#define tsslen(s) \ + ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) /* }================================================================== */ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 4c5514539..46911cac1 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -210,15 +210,15 @@ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ OP_SETUPVAL,/* A B UpValue[B] := R[A] */ -OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */ +OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */ OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */ OP_GETI,/* A B C R[A] := R[B][C] */ -OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */ +OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */ -OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */ +OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */ OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ OP_SETI,/* A B C R[A][B] := RK(C) */ -OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */ +OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */ OP_NEWTABLE,/* A B C k R[A] := {} */ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index ad5a92768..ba80d72c4 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -155,6 +155,7 @@ static int os_execute (lua_State *L) { static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); + errno = 0; return luaL_fileresult(L, remove(filename) == 0, filename); } @@ -162,6 +163,7 @@ static int os_remove (lua_State *L) { static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); + errno = 0; return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index b745f236f..2b888c7cf 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1022,10 +1022,11 @@ static int explist (LexState *ls, expdesc *v) { } -static void funcargs (LexState *ls, expdesc *f, int line) { +static void funcargs (LexState *ls, expdesc *f) { FuncState *fs = ls->fs; expdesc args; int base, nparams; + int line = ls->linenumber; switch (ls->t.token) { case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); @@ -1063,8 +1064,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ + fs->freereg = base+1; /* call removes function and arguments and leaves + one result (unless changed later) */ } @@ -1103,7 +1104,6 @@ static void suffixedexp (LexState *ls, expdesc *v) { /* suffixedexp -> primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ FuncState *fs = ls->fs; - int line = ls->linenumber; primaryexp(ls, v); for (;;) { switch (ls->t.token) { @@ -1123,12 +1123,12 @@ static void suffixedexp (LexState *ls, expdesc *v) { luaX_next(ls); codename(ls, &key); luaK_self(fs, v, &key); - funcargs(ls, v, line); + funcargs(ls, v); break; } case '(': case TK_STRING: case '{': { /* funcargs */ luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); + funcargs(ls, v); break; } default: return; diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index e8668c401..b0db02602 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -119,7 +119,7 @@ CallInfo *luaE_extendCI (lua_State *L) { /* ** free all CallInfo structures not in use by a thread */ -void luaE_freeCI (lua_State *L) { +static void freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; @@ -204,7 +204,7 @@ static void freestack (lua_State *L) { if (L->stack.p == NULL) return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); + freeCI(L); lua_assert(L->nci == 0); luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ } @@ -432,7 +432,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { void luaE_warnerror (lua_State *L, const char *where) { TValue *errobj = s2v(L->top.p - 1); /* error object */ const char *msg = (ttisstring(errobj)) - ? svalue(errobj) + ? getstr(tsvalue(errobj)) : "error object is not a string"; /* produce warning "error in %s (%s)" (where, msg) */ luaE_warning(L, "error in ", 1); diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index e2763073e..2a5db55f4 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -181,7 +181,7 @@ struct CallInfo { union { struct { /* only for Lua functions */ const Instruction *savedpc; - volatile l_signalT trap; + volatile l_signalT trap; /* function is tracing lines/counts */ int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ @@ -395,7 +395,6 @@ union GCUnion { 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); LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index bc7a0ba82..95b63b025 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -39,7 +39,7 @@ int luaS_eqlngstr (TString *a, TString *b) { lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); return (a == b) || /* same instance or... */ ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ + (memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */ } int luaS_eqshrstr (TString *a, TString *b) { @@ -76,7 +76,7 @@ unsigned int luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; - ts->hash = luaS_hash(getstr(ts), len, ts->hash); + ts->hash = luaS_hash(getlngstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -201,6 +201,7 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { TString *luaS_createlngstrobj (lua_State *L, size_t l) { TString *ts = createstrobj(L, l, LUA_VLNGSTR, STRSEED); ts->u.lnglen = l; + ts->shrlen = 0xFF; /* signals that it is a long string */ return ts; } @@ -237,7 +238,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString **list = &tb->hash[lmod(h, tb->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)) { + if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ @@ -250,8 +251,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, l, LUA_VSHRSTR, h); - memcpy(getstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); + memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; tb->nuse++; @@ -267,10 +268,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) + if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString)))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); - memcpy(getstr(ts), str, l * sizeof(char)); + memcpy(getlngstr(ts), str, l * sizeof(char)); return ts; } } diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 2930ee0d4..591d036fc 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -254,7 +254,7 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) { return t->alimit; /* this is the size */ else { unsigned int size = t->alimit; - /* compute the smallest power of 2 not smaller than 'n' */ + /* compute the smallest power of 2 not smaller than 'size' */ size |= (size >> 1); size |= (size >> 2); size |= (size >> 4); @@ -664,7 +664,8 @@ static Node *getfreepos (Table *t) { ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ -void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { +static void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value) { Node *mp; TValue aux; if (l_unlikely(isshared(t))) @@ -725,22 +726,36 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { /* ** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). +** directly from the array part. Otherwise, if 'alimit' is not +** the real size of the array, the key still can be in the array part. +** In this case, do the "Xmilia trick" to check whether 'key-1' is +** smaller than the real size. +** The trick works as follow: let 'p' be an integer such that +** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. +** That is, 2^(p+1) is the real size of the array, and 'p' is the highest +** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. +** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will +** have the 'p' bit cleared. If the key is outside the array, that is, +** 'key-1 >= 2^(p+1)', then 'res' will have some bit on higher than 'p', +** therefore it will be larger or equal to 'alimit', and the check +** will fail. If 'key-1 < 2^(p+1)', then 'res' has no bit on higher than +** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller +** than 2^p, therefore smaller than 'alimit', and the check succeeds. +** As special cases, when 'alimit' is 0 the condition is trivially false, +** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. +** If key is 0 or negative, 'res' will have its higher bit on, so that +** if cannot be smaller than alimit. */ const TValue *luaH_getint (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ + lua_Unsigned alimit = t->alimit; + if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ return &t->array[key - 1]; - else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (l_castS2U(key) == t->alimit + 1 || - l_castS2U(key) - 1u < luaH_realasize(t))) { + else if (!isrealasize(t) && /* key still may be in the array part? */ + (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { t->alimit = cast_uint(key); /* probably '#t' is here now */ return &t->array[key - 1]; } - else { + else { /* key is not in the array part; check the hash */ Node *n = hashint(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisinteger(n) && keyival(n) == key) diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 75dd9e26e..8e6890342 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -41,8 +41,6 @@ LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 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 void luaH_newkey (lua_State *L, Table *t, const TValue *key, - TValue *value); LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, diff --git a/3rd/lua/ltests.c b/3rd/lua/ltests.c index 4a0a6af1f..a27cdb079 100644 --- a/3rd/lua/ltests.c +++ b/3rd/lua/ltests.c @@ -73,8 +73,9 @@ static void badexit (const char *fmt, const char *s1, const char *s2) { static int tpanic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", msg, NULL), 0); /* do not return to Lua */ @@ -1533,7 +1534,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_newthread(L1); } else if EQ("resetthread") { - lua_pushinteger(L1, lua_resetthread(L1, L)); + lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */ } else if EQ("newuserdata") { lua_newuserdata(L1, getnum); @@ -1649,6 +1650,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int nres; status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); } + else if EQ("traceback") { + const char *msg = getstring; + int level = getnum; + luaL_traceback(L1, L1, msg, level); + } else if EQ("return") { int n = getnum; if (L1 != L) { diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index c309e2ae1..73b833c60 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -9,7 +9,6 @@ #include "lobject.h" -#include "lstate.h" /* @@ -96,8 +95,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci, + struct CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 0ff884545..6da331f11 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -115,12 +115,13 @@ static void l_message (const char *pname, const char *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'. +** message on the top of the stack. */ static int report (lua_State *L, int status) { if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); + if (msg == NULL) + msg = "(error message not a string)"; l_message(progname, msg); lua_pop(L, 1); /* remove message */ } @@ -210,12 +211,17 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* ** Receives 'globname[=modname]' and runs 'globname = require(modname)'. +** If there is no explicit modname and globname contains a '-', cut +** the suffix after '-' (the "version") to make the global name. */ static int dolibrary (lua_State *L, char *globname) { int status; + char *suffix = NULL; char *modname = strchr(globname, '='); - if (modname == NULL) /* no explicit name? */ + if (modname == NULL) { /* no explicit name? */ modname = globname; /* module name is equal to global name */ + suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */ + } else { *modname = '\0'; /* global name ends here */ modname++; /* module name starts after the '=' */ @@ -223,8 +229,11 @@ static int dolibrary (lua_State *L, char *globname) { lua_getglobal(L, "require"); lua_pushstring(L, modname); status = docall(L, 1, 1); /* call 'require(modname)' */ - if (status == LUA_OK) + if (status == LUA_OK) { + if (suffix != NULL) /* is there a suffix mark? */ + *suffix = '\0'; /* remove suffix from global name */ lua_setglobal(L, globname); /* globname = require(modname) */ + } return report(L, status); } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 9ec1894dc..5a9a00ad1 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -18,14 +18,14 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "6" +#define LUA_VERSION_RELEASE "7" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 6) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 7) #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-2023 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2024 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -501,7 +501,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2023 Lua.org, PUC-Rio. +* Copyright (C) 1994-2024 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 137103ede..33bb580d1 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -257,6 +257,15 @@ #endif + +/* +** LUA_IGMARK is a mark to ignore all after it when building the +** module name (e.g., used to build the luaopen_ function name). +** Typically, the suffix after the mark is the module version, +** as in "mod-v1.2.so". +*/ +#define LUA_IGMARK "-" + /* }================================================================== */ diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 02aed64fb..e8d92a853 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -81,7 +81,7 @@ static size_t loadUnsigned (LoadState *S, size_t limit) { static size_t loadSize (LoadState *S) { - return loadUnsigned(S, ~(size_t)0); + return loadUnsigned(S, MAX_SIZET); } @@ -122,7 +122,7 @@ static TString *loadStringN (LoadState *S, Proto *p) { ts = luaS_createlngstrobj(L, size); /* create string */ setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ luaD_inctop(L); - loadVector(S, getstr(ts), size); /* load directly in final place */ + loadVector(S, getlngstr(ts), size); /* load directly in final place */ L->top.p--; /* pop string */ } luaC_objbarrier(L, p, ts); diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index f3748a998..a97676ca1 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -21,8 +21,7 @@ /* ** Encode major-minor version in one byte, one nibble for each */ -#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_VERSION (((LUA_VERSION_NUM / 100) * 16) + LUA_VERSION_NUM % 100) #define LUAC_FORMAT 0 /* this is the official format */ diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 63e5f6fc3..2a7207fd0 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -91,8 +91,10 @@ static int l_strton (const TValue *obj, TValue *result) { lua_assert(obj != result); if (!cvt2num(obj)) /* is object not a string? */ return 0; - else - return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); + else { + TString *st = tsvalue(obj); + return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); + } } @@ -368,30 +370,32 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* -** Compare two strings 'ls' x 'rs', returning an integer less-equal- -** -greater than zero if 'ls' is less-equal-greater than 'rs'. +** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- +** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. ** 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. +** and it uses 'strcoll' (to respect locales) for each segment +** of the strings. Note that segments can compare equal but still +** have different lengths. */ -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = tsslen(ls); - const char *r = getstr(rs); - size_t lr = tsslen(rs); +static int l_strcmp (const TString *ts1, const TString *ts2) { + const char *s1 = getstr(ts1); + size_t rl1 = tsslen(ts1); /* real length */ + const char *s2 = getstr(ts2); + size_t rl2 = tsslen(ts2); for (;;) { /* for each segment */ - int temp = strcoll(l, r); + int temp = strcoll(s1, s2); 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 less 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; + size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ + size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ + if (zl2 == rl2) /* 's2' is finished? */ + return (zl1 == rl1) ? 0 : 1; /* check 's1' */ + else if (zl1 == rl1) /* 's1' is finished? */ + return -1; /* 's1' is less than 's2' ('s2' is not finished) */ + /* both strings longer than 'zl'; go on comparing after the '\0' */ + zl1++; zl2++; + s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; } } } @@ -626,8 +630,9 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { - size_t l = vslen(s2v(top - n)); /* length of string being copied */ - memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); + TString *st = tsvalue(s2v(top - n)); + size_t l = tsslen(st); /* length of string being copied */ + memcpy(buff + tl, getstr(st), l * sizeof(char)); tl += l; } while (--n > 0); } @@ -653,12 +658,12 @@ 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(s2v(top - 1)); + size_t tl = tsslen(tsvalue(s2v(top - 1))); TString *ts; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { - size_t l = vslen(s2v(top - n - 1)); - if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { + size_t l = tsslen(tsvalue(s2v(top - n - 1))); + if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); } @@ -671,7 +676,7 @@ void luaV_concat (lua_State *L, int total) { } else { /* long string; copy strings directly to final result */ ts = luaS_createlngstrobj(L, tl); - copy2buff(top, n, getstr(ts)); + copy2buff(top, n, getlngstr(ts)); } setsvalue2s(L, top - n, ts); /* create result */ } @@ -1157,18 +1162,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { startfunc: trap = L->hookmask; returning: /* trap already set */ - cl = clLvalue(s2v(ci->func.p)); + cl = ci_func(ci); k = cl->p->k; pc = ci->u.l.savedpc; - if (l_unlikely(trap)) { - if (pc == cl->p->code) { /* first instruction (not resuming)? */ - if (cl->p->is_vararg) - trap = 0; /* hooks will start after VARARGPREP instruction */ - else /* check 'call' hook */ - luaD_hookcall(L, ci); - } - ci->u.l.trap = 1; /* assume trap is on, for now */ - } + if (l_unlikely(trap)) + trap = luaG_tracecall(L); base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { @@ -1255,7 +1253,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TString *key = tsvalue(rc); /* key must be a short string */ if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } @@ -1298,7 +1296,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *rb = vRB(i); TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TString *key = tsvalue(rc); /* key must be a short string */ if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } @@ -1311,7 +1309,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ + TString *key = tsvalue(rb); /* key must be a short string */ if (luaV_fastset(L, upval, key, slot, luaH_getshortstr)) { luaV_finishfastset(L, upval, slot, rc); } @@ -1354,7 +1352,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ + TString *key = tsvalue(rb); /* key must be a short string */ if (luaV_fastset(L, s2v(ra), key, slot, luaH_getshortstr)) { luaV_finishfastset(L, s2v(ra), slot, rc); } diff --git a/3rd/lua/onelua.c b/3rd/lua/onelua.c index 3c605981f..2a4349612 100644 --- a/3rd/lua/onelua.c +++ b/3rd/lua/onelua.c @@ -1,5 +1,14 @@ /* -* one.c -- Lua core, libraries, and interpreter in a single file +** Lua core, libraries, and interpreter in a single file. +** Compiling just this file generates a complete Lua stand-alone +** program: +** +** $ gcc -O2 -std=c99 -o lua onelua.c -lm +** +** or +** +** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm +** */ /* default is to build the full interpreter */ @@ -11,8 +20,12 @@ #endif #endif -/* choose suitable platform-specific features */ -/* some of these may need extra libraries such as -ldl -lreadline -lncurses */ + +/* +** Choose suitable platform-specific features. Default is no +** platform-specific features. Some of these options may need extra +** libraries such as -ldl -lreadline -lncurses +*/ #if 0 #define LUA_USE_LINUX #define LUA_USE_MACOSX @@ -20,6 +33,7 @@ #define LUA_ANSI #endif + /* no need to change anything below this line ----------------------------- */ #include "lprefix.h" From 41928512f009ea8041f18cd790fb8d4daa995347 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Jun 2024 13:05:27 +0800 Subject: [PATCH 539/565] Use getshrstr() --- 3rd/lua/lstring.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 95b63b025..b6f318a49 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -45,8 +45,7 @@ int luaS_eqlngstr (TString *a, TString *b) { int luaS_eqshrstr (TString *a, TString *b) { int r; lu_byte len = a->shrlen; - lua_assert(b->tt == LUA_VSHRSTR); - r = len == b->shrlen && (memcmp(getstr(a), getstr(b), len) == 0); + r = len == b->shrlen && (memcmp(getshrstr(a), getshrstr(b), len) == 0); if (r) { if (a->id < b->id) { a->id = b->id; From 64a0ed4bfaf5ef9ca5728f2cfea3271bbe07f901 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 27 Jun 2024 08:31:30 +0800 Subject: [PATCH 540/565] Try to fix #1948 --- service/clusteragent.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 541b3f5d6..bae068a0f 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -9,11 +9,10 @@ gate = tonumber(gate) fd = tonumber(fd) local large_request = {} -local inquery_name = {} local register_name_mt = { __index = function(self, name) - local waitco = inquery_name[name] + local waitco = self.__inquery_name[name] if waitco then local co=coroutine.running() table.insert(waitco, co) @@ -21,12 +20,12 @@ local register_name_mt = { __index = return rawget(self, name) else waitco = {} - inquery_name[name] = waitco + self.__inquery_name[name] = waitco local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' if addr then self[name] = addr end - inquery_name[name] = nil + self.__inquery_name[name] = nil for _, co in ipairs(waitco) do skynet.wakeup(co) end @@ -36,7 +35,7 @@ local register_name_mt = { __index = } local function new_register_name() - return setmetatable({}, register_name_mt) + return setmetatable({ __inquery_name = {} }, register_name_mt) end local register_name = new_register_name() From 059fd5cc003bde82928b1e248ea6043ab4b12247 Mon Sep 17 00:00:00 2001 From: hong Date: Fri, 28 Jun 2024 05:32:01 +0800 Subject: [PATCH 541/565] new_register_name() (#1949) --- service/clusteragent.lua | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index bae068a0f..458ed7222 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -9,36 +9,43 @@ gate = tonumber(gate) fd = tonumber(fd) local large_request = {} +local inquery_name = {} +local register_name local register_name_mt = { __index = function(self, name) - local waitco = self.__inquery_name[name] + local waitco = inquery_name[name] if waitco then - local co=coroutine.running() + local co = coroutine.running() table.insert(waitco, co) skynet.wait(co) - return rawget(self, name) + return rawget(register_name, name) else waitco = {} - self.__inquery_name[name] = waitco - local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' - if addr then - self[name] = addr - end - self.__inquery_name[name] = nil - for _, co in ipairs(waitco) do - skynet.wakeup(co) + inquery_name[name] = waitco + + while true do + local ctx = register_name + local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' + if addr then + ctx[name] = addr + end + if ctx == register_name then + inquery_name[name] = nil + for _, co in ipairs(waitco) do + skynet.wakeup(co) + end + return addr + end end - return addr end end } local function new_register_name() - return setmetatable({ __inquery_name = {} }, register_name_mt) + register_name = setmetatable({}, register_name_mt) end - -local register_name = new_register_name() +new_register_name() local tracetag @@ -136,7 +143,7 @@ skynet.start(function() socket.close_fd(fd) skynet.exit() elseif cmd == "namechange" then - register_name = new_register_name() + new_register_name() else skynet.error(string.format("Invalid command %s from %s", cmd, skynet.address(source))) end From 2cfe36a8092330dee90d7ea2f3209c1105c72e4e Mon Sep 17 00:00:00 2001 From: hong Date: Sat, 6 Jul 2024 12:59:33 +0800 Subject: [PATCH 542/565] refix register_name_mt (#1954) --- service/clusteragent.lua | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 458ed7222..742e80830 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -24,20 +24,15 @@ local register_name_mt = { __index = waitco = {} inquery_name[name] = waitco - while true do - local ctx = register_name - local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' - if addr then - ctx[name] = addr - end - if ctx == register_name then - inquery_name[name] = nil - for _, co in ipairs(waitco) do - skynet.wakeup(co) - end - return addr - end + local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' + if addr then + register_name[name] = addr + end + inquery_name[name] = nil + for _, co in ipairs(waitco) do + skynet.wakeup(co) end + return addr end end } From 027e5990378ece9e5098ed5294920dae5503b6f2 Mon Sep 17 00:00:00 2001 From: Bruce Date: Sun, 21 Jul 2024 07:45:21 +0800 Subject: [PATCH 543/565] Avoid different function declarations when build as windows DLL (#1959) --- 3rd/lua/lauxlib.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 00fa50b60..d698f7eae 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1150,9 +1150,6 @@ init(void) { CC.L = luaL_newstate(); } - -void luaL_initcodecache(void); - LUALIB_API void luaL_initcodecache(void) { SPIN_INIT(&CC); @@ -1296,8 +1293,6 @@ cache_clear(lua_State *L) { return 0; } -int luaopen_cache(lua_State *L); - LUAMOD_API int luaopen_cache(lua_State *L) { luaL_Reg l[] = { { "clear", cache_clear }, From b519c53fe4986ad999097340789f8bc0c1d8dcfe Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Jul 2024 00:52:19 +0800 Subject: [PATCH 544/565] Try to fix #1958 --- service/clusteragent.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/clusteragent.lua b/service/clusteragent.lua index 742e80830..09b444b0e 100644 --- a/service/clusteragent.lua +++ b/service/clusteragent.lua @@ -131,7 +131,8 @@ skynet.start(function() dispatch = dispatch_request, } -- fd can write, but don't read fd, the data package will forward from gate though client protocol. - skynet.call(gate, "lua", "forward", fd) + -- forward may fail, see https://github.com/cloudwu/skynet/issues/1958 + pcall(skynet.call,gate, "lua", "forward", fd) skynet.dispatch("lua", function(_,source, cmd, ...) if cmd == "exit" then From 89372a257dd843da5170dcc6ebb54ddc6e321274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91?= <273461474@qq.com> Date: Wed, 24 Jul 2024 11:55:05 +0800 Subject: [PATCH 545/565] https set extension:server_name (#1960) Co-authored-by: root --- lualib-src/ltls.c | 10 ++++++++++ lualib/http/httpc.lua | 2 +- lualib/http/tlshelper.lua | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index efac8bbce..3fd4a0a43 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -267,6 +267,15 @@ _ltls_context_write(lua_State* L) { return 1; } +static int +_lset_ext_host_name(lua_State* L) { + struct tls_context* tls_p = _check_context(L, 1); + size_t slen = 0; + char* host = (char*)lua_tolstring(L, 2, &slen); + int ret = SSL_set_tlsext_host_name(tls_p->ssl, host); + lua_pushinteger(L, ret); + return 1; +} static int _lctx_gc(lua_State* L) { @@ -378,6 +387,7 @@ lnew_tls(lua_State* L) { {"handshake", _ltls_context_handshake}, {"read", _ltls_context_read}, {"write", _ltls_context_write}, + {"set_ext_host_name", _lset_ext_host_name}, {NULL, NULL}, }; luaL_newlib(L, l); diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 545956486..e9cb775c4 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -95,7 +95,7 @@ local function connect(host, timeout) end) end if interface.init then - interface.init() + interface.init((header and header.Host) or host) end return fd, interface, host end diff --git a/lualib/http/tlshelper.lua b/lualib/http/tlshelper.lua index 359c423a6..a81405ca8 100644 --- a/lualib/http/tlshelper.lua +++ b/lualib/http/tlshelper.lua @@ -6,7 +6,8 @@ local tlshelper = {} function tlshelper.init_requestfunc(fd, tls_ctx) local readfunc = socket.readfunc(fd) local writefunc = socket.writefunc(fd) - return function () + return function (hostname) + tls_ctx:set_ext_host_name(hostname) local ds1 = tls_ctx:handshake() writefunc(ds1) while not tls_ctx:finished() do From c2b391ed423a016efa930f262091d5723469d30f Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Sat, 27 Jul 2024 00:42:15 +0800 Subject: [PATCH 546/565] Update httpc.lua (#1962) --- 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 e9cb775c4..057aa61a9 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -95,7 +95,7 @@ local function connect(host, timeout) end) end if interface.init then - interface.init((header and header.Host) or host) + interface.init(host) end return fd, interface, host end From 76d73c295f229b853f5e418a5f929e3e26302a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91?= <273461474@qq.com> Date: Wed, 7 Aug 2024 10:54:03 +0800 Subject: [PATCH 547/565] =?UTF-8?q?debug=5Fconsole=20=E5=A2=9E=E5=8A=A0htt?= =?UTF-8?q?p=20post=EF=BC=8C=E9=81=BF=E5=85=8Dget=20url=E8=BD=AC=E4=B9=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#1964)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加http post,避免get url转义问题 --- service/debug_console.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index ea6379118..4c4fb7799 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -104,7 +104,13 @@ local function console_main_loop(stdin, print, addr) local cmdline = url:sub(2):gsub("/"," ") docmd(cmdline, print, stdin) break + elseif cmdline:sub(1,5) == "POST " then + -- http post + local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) + docmd(body, print, stdin) + break end + if cmdline ~= "" then docmd(cmdline, print, stdin) end From 4dfdeb146e43dcd82d6586399625f439e0c802aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91?= <273461474@qq.com> Date: Wed, 7 Aug 2024 11:59:27 +0800 Subject: [PATCH 548/565] =?UTF-8?q?debug=5Fconsole=E5=A2=9E=E5=8A=A0getenv?= =?UTF-8?q?,setenv=E5=91=BD=E4=BB=A4=20(#1965)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 方便获取配置文件中信息 --- service/debug_console.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index 4c4fb7799..c2527f11a 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -476,3 +476,12 @@ function COMMAND.profactive(flag) local active = memory.profactive() return "heap profilling is ".. (active and "active" or "deactive") end + +function COMMAND.getenv(name) + local value = skynet.getenv(name) + return {[name]=tostring(value)} +end + +function COMMAND.setenv(name,value) + return skynet.setenv(name,value) +end From bcf97ecd1be81f0c650b6be15724b89003470538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91?= <273461474@qq.com> Date: Wed, 7 Aug 2024 14:14:04 +0800 Subject: [PATCH 549/565] =?UTF-8?q?debug=5Fconsole=20help=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=A2=9E=E5=8A=A0getenv,setenv=E4=BF=A1=E6=81=AF=20(#?= =?UTF-8?q?1966)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/debug_console.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index c2527f11a..6a70b6bae 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -172,6 +172,8 @@ function COMMAND.help() dumpheap = "dumpheap : dump heap profilling", killtask = "killtask address threadname : threadname listed by task", dbgcmd = "run address debug command", + getenv = "getenv name : skynet.getenv(name)", + setenv = "setenv name value: skynet.setenv(name,value)", } end From 839570ce3f6025ec088798e8a227d54aa406a718 Mon Sep 17 00:00:00 2001 From: huojicha <4695954+huojicha@users.noreply.github.com> Date: Thu, 8 Aug 2024 02:34:15 +0800 Subject: [PATCH 550/565] =?UTF-8?q?1.=E6=8F=90=E4=BE=9B=E6=94=AF=E6=8C=81i?= =?UTF-8?q?pv6=E7=9A=84udp=20client=E5=92=8Cserver=E6=8E=A5=E5=8F=A3,=20?= =?UTF-8?q?=E5=8F=AF=E6=94=AF=E6=8C=81ipv6=20(#1967)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1.提供支持ipv6的udp client和server接口, 可支持ipv6 * remove chinese comments --- lualib-src/lua-socket.c | 40 ++++++++++++ lualib/skynet/socket.lua | 12 ++++ skynet-src/skynet_socket.c | 12 ++++ skynet-src/skynet_socket.h | 2 + skynet-src/socket_server.c | 122 ++++++++++++++++++++++++++++++++++++- skynet-src/socket_server.h | 6 ++ test/testudp.lua | 27 +++++++- 7 files changed, 218 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index c95273721..7e22d2ed7 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -675,6 +675,44 @@ ludp_connect(lua_State *L) { return 0; } +static int +ludp_dial(lua_State *L){ + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + size_t sz = 0; + const char * addr = luaL_checklstring(L, 1, &sz); + char tmp[sz]; + int port = 0; + const char * host = address_port(L, tmp, addr, 2, &port); + + int id = skynet_socket_udp_dial(ctx, host, port); + if (id < 0){ + return luaL_error(L, "udp dial host failed"); + } + + lua_pushinteger(L, id); + return 1; +} + +static int +ludp_listen(lua_State *L){ + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + size_t sz = 0; + const char * addr = luaL_checklstring(L, 1, &sz); + char tmp[sz]; + + int port = 0; + const char * host = address_port(L, tmp, addr, 2, &port); + + int id = skynet_socket_udp_listen(ctx, host, port); + if (id < 0){ + return luaL_error(L, "udp listen host failed"); + } + + lua_pushinteger(L, id); + return 1; +} + + static int ludp_send(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); @@ -849,6 +887,8 @@ luaopen_skynet_socketdriver(lua_State *L) { { "nodelay", lnodelay }, { "udp", ludp }, { "udp_connect", ludp_connect }, + { "udp_dial", ludp_dial}, + { "udp_listen", ludp_listen}, { "udp_send", ludp_send }, { "udp_address", ludp_address }, { "resolve", lresolve }, diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e047ffff9..b4c0fb271 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -471,6 +471,18 @@ function socket.udp_connect(id, addr, port, callback) driver.udp_connect(id, addr, port) end +function socket.udp_listen(addr, port, callback) + local id = driver.udp_listen(addr, port) + create_udp_object(id, callback) + return id +end + +function socket.udp_dial(addr, port, callback) + local id = driver.udp_dial(addr, port) + create_udp_object(id, callback) + return id +end + socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) socket.netstat = assert(driver.info) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 7ba7a6837..d7edc51e6 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -180,6 +180,18 @@ skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { return socket_server_udp(SOCKET_SERVER, source, addr, port); } +int +skynet_socket_udp_dial(struct skynet_context *ctx, const char * addr, int port){ + uint32_t source = skynet_context_handle(ctx); + return socket_server_udp_dial(SOCKET_SERVER, source, addr, port); +} + +int +skynet_socket_udp_listen(struct skynet_context *ctx, const char * addr, int port){ + uint32_t source = skynet_context_handle(ctx); + return socket_server_udp_listen(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); diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index dd66f834c..40d7e380c 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -40,6 +40,8 @@ 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_dial(struct skynet_context *ctx, const char * addr, int port); +int skynet_socket_udp_listen(struct skynet_context *ctx, const char * addr, int port); int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index a3cf96338..72f2f96b8 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -190,6 +190,13 @@ struct request_udp { uintptr_t opaque; }; +struct request_dial_udp { + int id; + int fd; + uintptr_t opaque; + uint8_t address[UDP_ADDRESS_SIZE]; +}; + /* The first byte is TYPE R Resume socket @@ -204,6 +211,7 @@ struct request_udp { P Send package (low) A Send UDP package C set udp address + N client dial to UDP host port T Set opt U Create UDP socket */ @@ -222,6 +230,7 @@ struct request_package { struct request_setopt setopt; struct request_udp udp; struct request_setudp set_udp; + struct request_dial_udp dial_udp; } u; uint8_t dummy[256]; }; @@ -1329,6 +1338,30 @@ set_udp_address(struct socket_server *ss, struct request_setudp *request, struct return -1; } +static int +dial_udp_socket(struct socket_server *ss, struct request_dial_udp *request, struct socket_message *result){ + int id = request->id; + int protocol = request->address[0]; + + struct socket *ns = new_fd(ss, id, request->fd, protocol, request->opaque, true); + if (ns == NULL){ + close(request->fd); + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + return -1; + } + + if (protocol == PROTOCOL_UDP){ + memcpy(ns->p.udp_address, request->address, 1 + 2 + 4); + } else { + memcpy(ns->p.udp_address, request->address, 1 + 2 + 16); + } + + ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); + + ATOM_FDEC(&ns->udpconnecting); + return -1; +} + static inline void inc_sending_ref(struct socket *s, int id) { if (s->protocol != PROTOCOL_TCP) @@ -1372,7 +1405,6 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { int type = header[0]; int len = header[1]; block_readpipe(fd, buffer, len); - // ctrl command only exist in local fd, so don't worry about endian. switch (type) { case 'R': @@ -1409,6 +1441,8 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { } case 'C': return set_udp_address(ss, (struct request_setudp *)buffer, result); + case 'N': + return dial_udp_socket(ss, (struct request_dial_udp *)buffer, result); case 'T': setopt_socket(ss, (struct request_setopt *)buffer); return -1; @@ -2116,6 +2150,92 @@ socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, return id; } +int +socket_server_udp_listen(struct socket_server *ss, uintptr_t opaque, const char* addr, int port){ + int fd; + if (port == 0){ + return -1; + } + + int family; + // bind + fd = do_bind(addr, port, IPPROTO_UDP, &family); + if (fd < 0) { + return -1; + } + + sp_nonblocking(fd); + + 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; +} + +int +socket_server_udp_dial(struct socket_server *ss, uintptr_t opaque, 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; + } + + 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 fd = socket(ai_list->ai_family, SOCK_DGRAM, 0); + if (fd < 0){ + return -1; + } + + sp_nonblocking(fd); + int id = reserve_id(ss); + if (id < 0){ + close(fd); + return -1; + } + + struct request_package request; + request.u.dial_udp.id = id; + request.u.dial_udp.fd = fd; + request.u.dial_udp.opaque = opaque; + + + int addrsz = gen_udp_address(protocol, (union sockaddr_all *)ai_list->ai_addr, request.u.dial_udp.address); + + freeaddrinfo( ai_list ); + + send_request(ss, &request, 'N', sizeof(request.u.dial_udp) - sizeof(request.u.dial_udp.address) + addrsz); + return id; +} + int socket_server_udp_send(struct socket_server *ss, const struct socket_udp_address *addr, struct socket_sendbuffer *buf) { int id = buf->id; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index beb628ca9..4167cb25b 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -57,6 +57,12 @@ struct socket_udp_address; 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); + +// create an udp client socket handle, and connect to server addr, return id when success +int socket_server_udp_dial(struct socket_server *ss, uintptr_t opaque, const char* addr, int port); +// create an udp server socket handle, and bind the host port, return id when success +int socket_server_udp_listen(struct socket_server *ss, uintptr_t opaque, 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 int socket_server_udp_send(struct socket_server *, const struct socket_udp_address *, struct socket_sendbuffer *buffer); diff --git a/test/testudp.lua b/test/testudp.lua index e62f40674..e49121ea8 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -4,14 +4,14 @@ local socket = require "skynet.socket" local function server() local host host = socket.udp(function(str, from) - print("server recv", str, socket.udp_address(from)) + print("server v4 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(str, from) - print("client recv", str, socket.udp_address(from)) + print("client v4 recv", str, socket.udp_address(from)) end) socket.udp_connect(c, "127.0.0.1", 8765) for i=1,20 do @@ -19,7 +19,30 @@ local function client() end end +local function server_v6() + local server + server = socket.udp_listen("::1", 8766, function(str, from) + print(string.format("server_v6 recv str:%s from:%s", str, socket.udp_address(from))) + socket.sendto(server, from, "OK " .. str) + end) -- bind an address + print("create server succeed. "..server) + return server +end + +local function client_v6() + local c = socket.udp_dial("::1", 8766, function(str, from) + print(string.format("client recv v6 response str:%s from:%s", str, socket.udp_address(from))) + end) + + print("create client succeed. "..c) + for i=1,20 do + socket.write(c, "hello " .. i) -- write to the address by udp_connect binding + end +end + skynet.start(function() skynet.fork(server) skynet.fork(client) + skynet.fork(server_v6) + skynet.fork(client_v6) end) From b1bb84ddbdbe78055bd336ef225f9633a9b947d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91?= <273461474@qq.com> Date: Fri, 30 Aug 2024 12:33:30 +0800 Subject: [PATCH 551/565] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=91=8A=E8=AD=A6=20(#1973)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update loader.lua去除语法检查告警 * Update mysql.lua去除语法检查告警 --- lualib/loader.lua | 4 ++-- lualib/skynet/db/mysql.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/loader.lua b/lualib/loader.lua index c18597e80..bf0b0577a 100644 --- a/lualib/loader.lua +++ b/lualib/loader.lua @@ -25,8 +25,8 @@ if not main then end LUA_SERVICE = nil -package.path , LUA_PATH = LUA_PATH -package.cpath , LUA_CPATH = LUA_CPATH +package.path , LUA_PATH = LUA_PATH, nil +package.cpath , LUA_CPATH = LUA_CPATH, nil local service_path = string.match(pattern, "(.*/)[^/?]+$") diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index afa6e2292..94b9b8f0c 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -820,7 +820,7 @@ end local function _prepare_resp(self, sql) return function(sock) - return read_prepare_result(self, sock, sql) + return read_prepare_result(self, sock) end end From 8900360fab6b449d63ab9d5efcac2d4088d5e060 Mon Sep 17 00:00:00 2001 From: JulyWind <707298413@qq.com> Date: Sat, 14 Sep 2024 17:37:01 +0800 Subject: [PATCH 552/565] fix client recv_package (#1977) --- examples/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/client.lua b/examples/client.lua index d0e5d1cd3..ad1131ec9 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -45,7 +45,7 @@ local function recv_package(last) if r == "" then error "Server closed" end - return unpack_package(last .. r) + return recv_package(last .. r) end local session = 0 From 270ed6e1981693bfbd58fe114f3cdea72c3d5f12 Mon Sep 17 00:00:00 2001 From: lcw4u <131836350+lcw4u@users.noreply.github.com> Date: Thu, 24 Oct 2024 09:40:49 +0800 Subject: [PATCH 553/565] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=94=99=E8=AF=AF=E6=8F=8F=E8=BF=B0=20(#1991)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 668bb1f1c..a1f322023 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -114,7 +114,7 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz, int is_push) if (name == NULL) { luaL_error(L, "name is not a string, it's a %s", lua_typename(L, lua_type(L, 1))); } else { - luaL_error(L, "name is too long %s", name); + luaL_error(L, "name length is invalid, must be between 1 and 255 characters: %s", name); } } From b5403a6866aef5a8390946b7793257548d0ecfa5 Mon Sep 17 00:00:00 2001 From: Bob Conan Date: Fri, 15 Nov 2024 17:34:04 -0600 Subject: [PATCH 554/565] Updated HISTORY.md, fix typo(s) (#1999) --- HISTORY.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index ef8c3a50c..d4dcefd7f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -92,7 +92,7 @@ v1.1.0-rc (2017-7-18) * httpc : Add httpc.timeout * mongo driver : sort support multi-key * bson : Check utf8 string -* bson : No longer support numberic key +* bson : No longer support numeric key * daemon mode: Can output the error messages * sproto : Support decimal number * sproto: Support binary type @@ -136,7 +136,7 @@ v1.0.0-rc3 (2016-5-9) * skynet.getenv can return empty string * Add lua VM memory warning * lua VM support memory limit -* skynet.pcall suport varargs +* skynet.pcall support varargs * Bugfix : Global name query * Bugfix : snax.queryglobal @@ -236,7 +236,7 @@ v1.0.0-alpha6 (2015-5-18) v1.0.0-alpha5 (2015-4-27) ----------- -* merge lua 5.3 offical bugfix +* merge lua 5.3 official bugfix * improve sproto rpc api * fix a deadlock bug when service retire * improve cluster config reload @@ -412,7 +412,7 @@ v0.4.0 (2014-6-30) * 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. +* Redesign harbor/master/dummy, remove lots of C code and rewrite in lua. * Remove block connect api, queue sending message during connecting now. * Add skynet.time() @@ -452,8 +452,8 @@ v0.2.0 (2014-5-12) * Add some snax api, snax.uniqueservice (etc.) , use independent protocol `PTYPE_SNAX` . * Add bootstrap lua script , remove some code in C . * Use a lua loader to load lua service code (and set the lua environment), remove some code in C. -* Support preload a file before each lua serivce start. -* Add datacenter serivce. +* Support preload a file before each lua service start. +* Add datacenter service. * Add multicast api. * Remove skynet.blockcall , simplify the implement of message queue. * When dropping message queue (at service exit) , dispatcher will post an error back to the source of each message. From 4c8d42153ec5adc722eac0213e383ab6703bd337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:23:42 +0800 Subject: [PATCH 555/565] throw it out err (#2003) --- lualib/http/websocket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index 163d3d8b2..e0ff85c0c 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -450,7 +450,7 @@ function M.accept(socket_id, handle, protocol, addr, options) if closed then try_handle(ws_obj, "close") else - try_handle(ws_obj, "error") + try_handle(ws_obj, "error", err) end else -- error(err) From 2fc8e869258ee07b2049bce1975d9b51c4750246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Sat, 7 Dec 2024 15:20:19 +0800 Subject: [PATCH 556/565] fix websocket accept mem leakage (#2002) * fix websocket accept mem leakage * fix websocket accept mem leakage * fix websocket accept mem leakage * fix websocket accept mem leakage --- lualib/http/websocket.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index e0ff85c0c..8279c7d92 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -428,16 +428,25 @@ end -- connect / handshake / message / ping / pong / close / error function M.accept(socket_id, handle, protocol, addr, options) if not (options and options.upgrade) then - socket.start(socket_id) + local isok, err = socket.start(socket_id) + if not isok then + return false, err + end end protocol = protocol or "ws" local ws_obj = _new_server_ws(socket_id, handle, protocol) ws_obj.addr = addr local on_warning = handle and handle["warning"] if on_warning then - socket.warning(socket_id, function (id, sz) + local isok = pcall(socket.warning, socket_id, function(id, sz) on_warning(ws_obj, sz) end) + if not isok then + if not _isws_closed(socket_id) then + _close_websocket(ws_obj) + end + return false, "connect is closed " .. socket_id + end end local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj, options) From cada3f5cb0f438f1698051c43fa0f29043645c29 Mon Sep 17 00:00:00 2001 From: 851773277 <851773277@qq.com> Date: Mon, 9 Dec 2024 19:51:37 +0800 Subject: [PATCH 557/565] =?UTF-8?q?datacenterd=20query=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=97=B6,=E5=BD=93db=E4=B8=BAnil=E6=97=B6,?= =?UTF-8?q?=E7=BB=88=E6=AD=A2=E9=80=92=E5=BD=92=20(#2006)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/datacenterd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index 0c355186f..4fee2e0d6 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -6,7 +6,7 @@ local wait_queue = {} local mode = {} local function query(db, key, ...) - if key == nil then + if db == nil or key == nil then return db else return query(db[key], ...) From 88df42a60fbe2007524b04e425941df8270dda8a Mon Sep 17 00:00:00 2001 From: navi <87298770+BMingSY@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:29:48 +0800 Subject: [PATCH 558/565] =?UTF-8?q?fix=20message=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E8=BF=87=E5=A4=9A=E5=AF=BC=E8=87=B4int=E6=BA=A2=E5=87=BA=20(#2?= =?UTF-8?q?014)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 角弓 --- skynet-src/skynet_server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 9f2a3593b..a07f51ff9 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -52,7 +52,7 @@ struct skynet_context { uint32_t handle; int session_id; ATOM_INT ref; - int message_count; + size_t message_count; bool init; bool endless; bool profile; @@ -574,7 +574,7 @@ cmd_stat(struct skynet_context * context, const char * param) { strcpy(context->result, "0"); } } else if (strcmp(param, "message") == 0) { - sprintf(context->result, "%d", context->message_count); + sprintf(context->result, "%zu", context->message_count); } else { context->result[0] = '\0'; } From 7c3942cdbde844d5b323ab01ac9eadf99675e370 Mon Sep 17 00:00:00 2001 From: sundream Date: Sun, 29 Dec 2024 09:24:21 +0800 Subject: [PATCH 559/565] fix(rediscluster): maybe too many connection when MOVED (#2016) --- lualib/skynet/db/redis/cluster.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/db/redis/cluster.lua b/lualib/skynet/db/redis/cluster.lua index d7ab6468b..697befba5 100644 --- a/lualib/skynet/db/redis/cluster.lua +++ b/lualib/skynet/db/redis/cluster.lua @@ -121,8 +121,8 @@ function rediscluster:initialize_slots_cache() self:set_node_name(slave_node) table.insert(master_node.slaves,slave_node) end + table.insert(self.nodes,master_node) for slot=tonumber(result[1]),tonumber(result[2]) do - table.insert(self.nodes,master_node) self.slots[slot] = master_node end end From 474f04e8640337193c425a635df60df82aaa684a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:26:40 +0800 Subject: [PATCH 560/565] fix socketchannel closed thread not wakeup (#2020) --- lualib/skynet/socketchannel.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 24e8de75f..b7646fd58 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -116,6 +116,11 @@ local function dispatch_by_session(self) end skynet.wakeup(co) end + if not self.__sock then + -- closed + wakeup_all(self, "channel_closed") + break + end else self.__thread[session] = nil skynet.error("socket: unknown session :", session) @@ -211,6 +216,11 @@ local function dispatch_by_order(self) self.__result_data[co] = result_data end skynet.wakeup(co) + if not self.__sock then + -- closed + wakeup_all(self, "channel_closed") + break + end else close_channel_socket(self) local errmsg From e1c28ed13ea5d5b7d9419a33fb581213c9424e15 Mon Sep 17 00:00:00 2001 From: "Jason N. White" Date: Tue, 31 Dec 2024 18:53:34 -0600 Subject: [PATCH 561/565] Update LICENSE, fix license year (#2021) Signed-off-by: JasonnnW3000 --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 3f8241a62..72bf0b961 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2022 codingnow.com +Copyright (c) 2012-2025 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 ba64be6f9fa044933c77de1317f466afbade8eaa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Jan 2025 09:50:26 +0800 Subject: [PATCH 562/565] Update history for v1.8.0 release --- 3rd/lua/README | 2 +- HISTORY.md | 15 +++++++++++++++ README.md | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index 6a0ac4546..8f21986f1 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,4 +1,4 @@ -This is a modify version of lua 5.4.3 . +This is a modify version of lua 5.4.7 . For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/HISTORY.md b/HISTORY.md index d4dcefd7f..a4b92692b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,18 @@ +v1.8.0 (2025-1-14) +----------- +* Update Lua to 5.4.7 +* service sessions can be rewind +* Improve: udp (ipv6 support) +* Improve: debug console +* Improve: http +* Improve: mongo driver +* Improve: mysql driver +* Bugfix: socketchannel +* Bugfix: cluster +* Bugfix: ssl +* Bugfix: websocket +* Bugfix: redis cluster driver + v1.7.0 (2023-11-13) ----------- * Update Lua to 5.4.6 diff --git a/README.md b/README.md index 139b10df5..5dd750d79 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.4.6 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. +Skynet now uses a modified version of lua 5.4.7 ( https://github.com/ejoy/lua/tree/skynet54 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. From ad96b6c736a98d644da46381cefadb9f2724bc2c Mon Sep 17 00:00:00 2001 From: wanli <522256691@qq.com> Date: Thu, 6 Mar 2025 12:02:32 +0800 Subject: [PATCH 563/565] =?UTF-8?q?*=20=E5=A2=9E=E5=8A=A0mysql=E4=BD=BF?= =?UTF-8?q?=E7=94=A8execute=E6=96=B9=E6=B3=95=E8=A7=A3=E6=9E=90date?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E6=95=B0=E6=8D=AE=20(#2035)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * * 增加mysql使用execute方法解析date格式数据 * 优化mysql模块解析date失败时处理方式 --------- Co-authored-by: test --- lualib/skynet/db/mysql.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index 94b9b8f0c..e622e176c 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -852,6 +852,20 @@ local function _get_datetime(data, pos) return value, pos end +local function _get_date(data, pos) + local len, year, month, day + local value + len, pos = _from_length_coded_bin(data, pos) + if len == 4 then + year, month, day, pos = string.unpack(" Date: Thu, 6 Mar 2025 12:03:30 +0800 Subject: [PATCH 564/565] allow `t[nil] => nil` in table returned by sharedata (#2037) --- lualib-src/lua-sharedata.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index d52e294f3..ac72127d3 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -540,7 +540,9 @@ lindexconf(lua_State *L) { int keytype; size_t sz = 0; const char * str = NULL; - if (kt == LUA_TNUMBER) { + if (kt == LUA_TNIL) { + return 0; + } else if (kt == LUA_TNUMBER) { if (!lua_isinteger(L, 2)) { return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2)); } From 1ba381a1c209ae6eea8ecec0fffac90a3340f350 Mon Sep 17 00:00:00 2001 From: fanyh <8852086@qq.com> Date: Tue, 18 Mar 2025 20:14:45 +0800 Subject: [PATCH 565/565] skynet_error output abnormal log add error tag (#2039) --- skynet-src/skynet_monitor.c | 2 +- skynet-src/skynet_server.c | 18 +++++++++--------- skynet-src/skynet_socket.c | 2 +- skynet-src/socket_server.c | 22 +++++++++++----------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/skynet-src/skynet_monitor.c b/skynet-src/skynet_monitor.c index 48def95b3..0b0cebfa0 100644 --- a/skynet-src/skynet_monitor.c +++ b/skynet-src/skynet_monitor.c @@ -39,7 +39,7 @@ skynet_monitor_check(struct skynet_monitor *sm) { if (sm->version == sm->check_version) { if (sm->destination) { skynet_context_endless(sm->destination); - skynet_error(NULL, "A message from [ :%08x ] to [ :%08x ] maybe in an endless loop (version = %d)", sm->source , sm->destination, sm->version); + skynet_error(NULL, "error: A message from [ :%08x ] to [ :%08x ] maybe in an endless loop (version = %d)", sm->source , sm->destination, sm->version); } } else { sm->check_version = sm->version; diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index a07f51ff9..6d6d67286 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -170,7 +170,7 @@ skynet_context_new(const char * name, const char *param) { } return ret; } else { - skynet_error(ctx, "FAILED launch %s", name); + skynet_error(ctx, "error: launch %s FAILED", name); uint32_t handle = ctx->handle; skynet_context_release(ctx); skynet_handle_retire(handle); @@ -324,7 +324,7 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue } int overload = skynet_mq_overload(q); if (overload) { - skynet_error(ctx, "May overload, message queue length = %d", overload); + skynet_error(ctx, "error: May overload, message queue length = %d", overload); } skynet_monitor_trigger(sm, msg.source , handle); @@ -370,7 +370,7 @@ skynet_queryname(struct skynet_context * context, const char * name) { case '.': return skynet_handle_findname(name + 1); } - skynet_error(context, "Don't support query global name %s",name); + skynet_error(context, "error: Don't support query global name %s",name); return 0; } @@ -413,7 +413,7 @@ cmd_reg(struct skynet_context * context, const char * param) { } else if (param[0] == '.') { return skynet_handle_namehandle(context->handle, param + 1); } else { - skynet_error(context, "Can't register global name %s in C", param); + skynet_error(context, "error: Can't register global name %s in C", param); return NULL; } } @@ -446,7 +446,7 @@ cmd_name(struct skynet_context * context, const char * param) { if (name[0] == '.') { return skynet_handle_namehandle(handle_id, name + 1); } else { - skynet_error(context, "Can't set global name %s in C", name); + skynet_error(context, "error: Can't set global name %s in C", name); } return NULL; } @@ -465,7 +465,7 @@ tohandle(struct skynet_context * context, const char * param) { } else if (param[0] == '.') { handle = skynet_handle_findname(param+1); } else { - skynet_error(context, "Can't convert %s to handle",param); + skynet_error(context, "error: Can't convert %s to handle",param); } return handle; @@ -700,7 +700,7 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da 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_error(context, "error: The message to %x is too large", destination); if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } @@ -714,7 +714,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati if (destination == 0) { if (data) { - skynet_error(context, "Destination address can't be 0"); + skynet_error(context, "error: Destination address can't be 0"); skynet_free(data); return -1; } @@ -761,7 +761,7 @@ skynet_sendname(struct skynet_context * context, uint32_t source, const char * a } } else { if ((sz & MESSAGE_TYPE_MASK) != sz) { - skynet_error(context, "The message to %s is too large", addr); + skynet_error(context, "error: The message to %s is too large", addr); if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index d7edc51e6..df44bd251 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -107,7 +107,7 @@ skynet_socket_poll() { forward_message(SKYNET_SOCKET_TYPE_WARNING, false, &result); break; default: - skynet_error(NULL, "Unknown socket message type %d.",type); + skynet_error(NULL, "error: Unknown socket message type %d.",type); return -1; } if (more) { diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 72f2f96b8..a6e83880a 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -392,17 +392,17 @@ socket_server_create(uint64_t time) { int fd[2]; poll_fd efd = sp_create(); if (sp_invalid(efd)) { - skynet_error(NULL, "socket-server: create event pool failed."); + skynet_error(NULL, "socket-server error: create event pool failed."); return NULL; } if (pipe(fd)) { sp_release(efd); - skynet_error(NULL, "socket-server: create socket pair failed."); + skynet_error(NULL, "socket-server error: create socket pair failed."); return NULL; } if (sp_add(efd, fd[0], NULL)) { // add recvctrl_fd to event poll - skynet_error(NULL, "socket-server: can't add server fd to event pool."); + skynet_error(NULL, "socket-server error: can't add server fd to event pool."); close(fd[0]); close(fd[1]); sp_release(efd); @@ -796,7 +796,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp->udp_address, &sa); if (sasz == 0) { - skynet_error(NULL, "socket-server : udp (%d) type mismatch.", s->id); + skynet_error(NULL, "socket-server : udp (%d) error: type mismatch.", s->id); drop_udp(ss, s, list, tmp); return -1; } @@ -1032,7 +1032,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return -1; } if (type == SOCKET_TYPE_PLISTEN || type == SOCKET_TYPE_LISTEN) { - skynet_error(NULL, "socket-server: write to listen fd %d.", id); + skynet_error(NULL, "socket-server error: write to listen fd %d.", id); so.free_func((void *)request->buffer); return -1; } @@ -1048,7 +1048,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { // udp type mismatch, just drop it. - skynet_error(NULL, "socket-server: udp socket (%d) type mismatch.", id); + skynet_error(NULL, "socket-server: udp socket (%d) error: type mismatch.", id); so.free_func((void *)request->buffer); return -1; } @@ -1450,7 +1450,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { add_udp_socket(ss, (struct request_udp *)buffer); return -1; default: - skynet_error(NULL, "socket-server: Unknown ctrl %c.",type); + skynet_error(NULL, "socket-server error: Unknown ctrl %c.",type); return -1; }; @@ -1729,7 +1729,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int ss->event_n = 0; int err = errno; if (err != EINTR) { - skynet_error(NULL, "socket-server: %s", strerror(err)); + skynet_error(NULL, "socket-server error: %s", strerror(err)); } continue; } @@ -1756,7 +1756,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int break; } case SOCKET_TYPE_INVALID: - skynet_error(NULL, "socket-server: invalid socket"); + skynet_error(NULL, "socket-server error: invalid socket"); break; default: if (e->read) { @@ -1840,7 +1840,7 @@ 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) { - skynet_error(NULL, "socket-server : Invalid addr %s.",addr); + skynet_error(NULL, "socket-server error: Invalid addr %s.",addr); return -1; } int id = reserve_id(ss); @@ -1896,7 +1896,7 @@ socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, s->p.udp_address, &sa); if (sasz == 0) { - skynet_error(NULL, "socket-server : set udp (%d) address first.", id); + skynet_error(NULL, "socket-server : set udp (%d) error: address first.", id); socket_unlock(&l); so.free_func((void *)buf->buffer); return -1;