diff --git a/.gitignore b/.gitignore index 9c452a72..56919127 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,11 @@ *.o *.a -skynet +/skynet +/skynet.pid 3rd/lua/lua 3rd/lua/luac -cservice -luaclib +/cservice +/luaclib *.so *.dSYM +.DS_Store diff --git a/3rd/jemalloc b/3rd/jemalloc index 3541a904..04380e79 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 3541a904d6fb949f3f0aea05418ccce7cbd4b705 +Subproject commit 04380e79f1e2428bd0ad000bbc6e3d2dfc6b66a5 diff --git a/3rd/lpeg/HISTORY b/3rd/lpeg/HISTORY new file mode 100644 index 00000000..0c10edd0 --- /dev/null +++ b/3rd/lpeg/HISTORY @@ -0,0 +1,96 @@ +HISTORY for LPeg 1.0 + +* Changes from version 0.12 to 1.0 + --------------------------------- + + group "names" can be any Lua value + + some bugs fixed + + other small improvements + +* Changes from version 0.11 to 0.12 + --------------------------------- + + no "unsigned short" limit for pattern sizes + + mathtime captures considered nullable + + some bugs fixed + +* Changes from version 0.10 to 0.11 + ------------------------------- + + complete reimplementation of the code generator + + new syntax for table captures + + new functions in module 're' + + other small improvements + +* Changes from version 0.9 to 0.10 + ------------------------------- + + backtrack stack has configurable size + + better error messages + + Notation for non-terminals in 're' back to A instead o + + experimental look-behind pattern + + support for external extensions + + works with Lua 5.2 + + consumes less C stack + + - "and" predicates do not keep captures + +* Changes from version 0.8 to 0.9 + ------------------------------- + + The accumulator capture was replaced by a fold capture; + programs that used the old 'lpeg.Ca' will need small changes. + + Some support for character classes from old C locales. + + A new named-group capture. + +* Changes from version 0.7 to 0.8 + ------------------------------- + + New "match-time" capture. + + New "argument capture" that allows passing arguments into the pattern. + + Better documentation for 're'. + + Several small improvements for 're'. + + The 're' module has an incompatibility with previous versions: + now, any use of a non-terminal must be enclosed in angle brackets + (like ). + +* Changes from version 0.6 to 0.7 + ------------------------------- + + Several improvements in module 're': + - better documentation; + - support for most captures (all but accumulator); + - limited repetitions p{n,m}. + + Small improvements in efficiency. + + Several small bugs corrected (special thanks to Hans Hagen + and Taco Hoekwater). + +* Changes from version 0.5 to 0.6 + ------------------------------- + + Support for non-numeric indices in grammars. + + Some bug fixes (thanks to the luatex team). + + Some new optimizations; (thanks to Mike Pall). + + A new page layout (thanks to Andre Carregal). + + Minimal documentation for module 're'. + +* Changes from version 0.4 to 0.5 + ------------------------------- + + Several optimizations. + + lpeg.P now accepts booleans. + + Some new examples. + + A proper license. + + Several small improvements. + +* Changes from version 0.3 to 0.4 + ------------------------------- + + Static check for loops in repetitions and grammars. + + Removed label option in captures. + + The implementation of captures uses less memory. + +* Changes from version 0.2 to 0.3 + ------------------------------- + + User-defined patterns in Lua. + + Several new captures. + +* Changes from version 0.1 to 0.2 + ------------------------------- + + Several small corrections. + + Handles embedded zeros like any other character. + + Capture "name" can be any Lua value. + + Unlimited number of captures. + + Match gets an optional initial position. + +(end of HISTORY) diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c new file mode 100644 index 00000000..c9085de0 --- /dev/null +++ b/3rd/lpeg/lpcap.c @@ -0,0 +1,537 @@ +/* +** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include "lua.h" +#include "lauxlib.h" + +#include "lpcap.h" +#include "lptypes.h" + + +#define captype(cap) ((cap)->kind) + +#define isclosecap(cap) (captype(cap) == Cclose) + +#define closeaddr(c) ((c)->s + (c)->siz - 1) + +#define isfullcap(cap) ((cap)->siz != 0) + +#define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v) + +#define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) + + + +/* +** Put at the cache for Lua values the value indexed by 'v' in ktable +** of the running pattern (if it is not there yet); returns its index. +*/ +static int updatecache (CapState *cs, int v) { + int idx = cs->ptop + 1; /* stack index of cache for Lua values */ + if (v != cs->valuecached) { /* not there? */ + getfromktable(cs, v); /* get value from 'ktable' */ + lua_replace(cs->L, idx); /* put it at reserved stack position */ + cs->valuecached = v; /* keep track of what is there */ + } + return idx; +} + + +static int pushcapture (CapState *cs); + + +/* +** Goes back in a list of captures looking for an open capture +** corresponding to a close +*/ +static Capture *findopen (Capture *cap) { + int n = 0; /* number of closes waiting an open */ + for (;;) { + cap--; + if (isclosecap(cap)) n++; /* one more open to skip */ + else if (!isfullcap(cap)) + if (n-- == 0) return cap; + } +} + + +/* +** Go to the next capture +*/ +static void nextcap (CapState *cs) { + Capture *cap = cs->cap; + if (!isfullcap(cap)) { /* not a single capture? */ + int n = 0; /* number of opens waiting a close */ + for (;;) { /* look for corresponding close */ + cap++; + if (isclosecap(cap)) { + if (n-- == 0) break; + } + else if (!isfullcap(cap)) n++; + } + } + cs->cap = cap + 1; /* + 1 to skip last close (or entire single capture) */ +} + + +/* +** Push on the Lua stack all values generated by nested captures inside +** the current capture. Returns number of values pushed. 'addextra' +** makes it push the entire match after all captured values. The +** entire match is pushed also if there are no other nested values, +** so the function never returns zero. +*/ +static int pushnestedvalues (CapState *cs, int addextra) { + Capture *co = cs->cap; + if (isfullcap(cs->cap++)) { /* no nested captures? */ + lua_pushlstring(cs->L, co->s, co->siz - 1); /* push whole match */ + return 1; /* that is it */ + } + else { + int n = 0; + while (!isclosecap(cs->cap)) /* repeat for all nested patterns */ + n += pushcapture(cs); + if (addextra || n == 0) { /* need extra? */ + lua_pushlstring(cs->L, co->s, cs->cap->s - co->s); /* push whole match */ + n++; + } + cs->cap++; /* skip close entry */ + return n; + } +} + + +/* +** Push only the first value generated by nested captures +*/ +static void pushonenestedvalue (CapState *cs) { + int n = pushnestedvalues(cs, 0); + if (n > 1) + lua_pop(cs->L, n - 1); /* pop extra values */ +} + + +/* +** Try to find a named group capture with the name given at the top of +** the stack; goes backward from 'cap'. +*/ +static Capture *findback (CapState *cs, Capture *cap) { + lua_State *L = cs->L; + while (cap-- > cs->ocap) { /* repeat until end of list */ + if (isclosecap(cap)) + cap = findopen(cap); /* skip nested captures */ + else if (!isfullcap(cap)) + continue; /* opening an enclosing capture: skip and get previous */ + if (captype(cap) == Cgroup) { + getfromktable(cs, cap->idx); /* get group name */ + if (lp_equal(L, -2, -1)) { /* right group? */ + lua_pop(L, 2); /* remove reference name and group name */ + return cap; + } + else lua_pop(L, 1); /* remove group name */ + } + } + luaL_error(L, "back reference '%s' not found", lua_tostring(L, -1)); + return NULL; /* to avoid warnings */ +} + + +/* +** Back-reference capture. Return number of values pushed. +*/ +static int backrefcap (CapState *cs) { + int n; + Capture *curr = cs->cap; + pushluaval(cs); /* reference name */ + cs->cap = findback(cs, curr); /* find corresponding group */ + n = pushnestedvalues(cs, 0); /* push group's values */ + cs->cap = curr + 1; + return n; +} + + +/* +** Table capture: creates a new table and populates it with nested +** captures. +*/ +static int tablecap (CapState *cs) { + lua_State *L = cs->L; + int n = 0; + lua_newtable(L); + if (isfullcap(cs->cap++)) + return 1; /* table is empty */ + while (!isclosecap(cs->cap)) { + if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */ + pushluaval(cs); /* push group name */ + pushonenestedvalue(cs); + lua_settable(L, -3); + } + else { /* not a named group */ + int i; + int k = pushcapture(cs); + for (i = k; i > 0; i--) /* store all values into table */ + lua_rawseti(L, -(i + 1), n + i); + n += k; + } + } + cs->cap++; /* skip close entry */ + return 1; /* number of values pushed (only the table) */ +} + + +/* +** Table-query capture +*/ +static int querycap (CapState *cs) { + int idx = cs->cap->idx; + pushonenestedvalue(cs); /* get nested capture */ + lua_gettable(cs->L, updatecache(cs, idx)); /* query cap. value at table */ + if (!lua_isnil(cs->L, -1)) + return 1; + else { /* no value */ + lua_pop(cs->L, 1); /* remove nil */ + return 0; + } +} + + +/* +** Fold capture +*/ +static int foldcap (CapState *cs) { + int n; + lua_State *L = cs->L; + int idx = cs->cap->idx; + if (isfullcap(cs->cap++) || /* no nested captures? */ + isclosecap(cs->cap) || /* no nested captures (large subject)? */ + (n = pushcapture(cs)) == 0) /* nested captures with no values? */ + return luaL_error(L, "no initial value for fold capture"); + if (n > 1) + lua_pop(L, n - 1); /* leave only one result for accumulator */ + while (!isclosecap(cs->cap)) { + lua_pushvalue(L, updatecache(cs, idx)); /* get folding function */ + lua_insert(L, -2); /* put it before accumulator */ + n = pushcapture(cs); /* get next capture's values */ + lua_call(L, n + 1, 1); /* call folding function */ + } + cs->cap++; /* skip close entry */ + return 1; /* only accumulator left on the stack */ +} + + +/* +** Function capture +*/ +static int functioncap (CapState *cs) { + int n; + int top = lua_gettop(cs->L); + pushluaval(cs); /* push function */ + n = pushnestedvalues(cs, 0); /* push nested captures */ + lua_call(cs->L, n, LUA_MULTRET); /* call function */ + return lua_gettop(cs->L) - top; /* return function's results */ +} + + +/* +** Select capture +*/ +static int numcap (CapState *cs) { + int idx = cs->cap->idx; /* value to select */ + if (idx == 0) { /* no values? */ + nextcap(cs); /* skip entire capture */ + return 0; /* no value produced */ + } + else { + int n = pushnestedvalues(cs, 0); + if (n < idx) /* invalid index? */ + return luaL_error(cs->L, "no capture '%d'", idx); + else { + lua_pushvalue(cs->L, -(n - idx + 1)); /* get selected capture */ + lua_replace(cs->L, -(n + 1)); /* put it in place of 1st capture */ + lua_pop(cs->L, n - 1); /* remove other captures */ + return 1; + } + } +} + + +/* +** Return the stack index of the first runtime capture in the given +** list of captures (or zero if no runtime captures) +*/ +int finddyncap (Capture *cap, Capture *last) { + for (; cap < last; cap++) { + if (cap->kind == Cruntime) + return cap->idx; /* stack position of first capture */ + } + return 0; /* no dynamic captures in this segment */ +} + + +/* +** Calls a runtime capture. Returns number of captures removed by +** the call, including the initial Cgroup. (Captures to be added are +** on the Lua stack.) +*/ +int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { + int n, id; + lua_State *L = cs->L; + int otop = lua_gettop(L); + Capture *open = findopen(close); + assert(captype(open) == Cgroup); + id = finddyncap(open, close); /* get first dynamic capture argument */ + close->kind = Cclose; /* closes the group */ + close->s = s; + cs->cap = open; cs->valuecached = 0; /* prepare capture state */ + luaL_checkstack(L, 4, "too many runtime captures"); + pushluaval(cs); /* push function to be called */ + lua_pushvalue(L, SUBJIDX); /* push original subject */ + lua_pushinteger(L, s - cs->s + 1); /* push current position */ + n = pushnestedvalues(cs, 0); /* push nested captures */ + lua_call(L, n + 2, LUA_MULTRET); /* call dynamic function */ + if (id > 0) { /* are there old dynamic captures to be removed? */ + int i; + for (i = id; i <= otop; i++) + lua_remove(L, id); /* remove old dynamic captures */ + *rem = otop - id + 1; /* total number of dynamic captures removed */ + } + else + *rem = 0; /* no dynamic captures removed */ + return close - open; /* number of captures of all kinds removed */ +} + + +/* +** Auxiliary structure for substitution and string captures: keep +** information about nested captures for future use, avoiding to push +** string results into Lua +*/ +typedef struct StrAux { + int isstring; /* whether capture is a string */ + union { + Capture *cp; /* if not a string, respective capture */ + struct { /* if it is a string... */ + const char *s; /* ... starts here */ + const char *e; /* ... ends here */ + } s; + } u; +} StrAux; + +#define MAXSTRCAPS 10 + +/* +** Collect values from current capture into array 'cps'. Current +** capture must be Cstring (first call) or Csimple (recursive calls). +** (In first call, fills %0 with whole match for Cstring.) +** Returns number of elements in the array that were filled. +*/ +static int getstrcaps (CapState *cs, StrAux *cps, int n) { + int k = n++; + cps[k].isstring = 1; /* get string value */ + cps[k].u.s.s = cs->cap->s; /* starts here */ + if (!isfullcap(cs->cap++)) { /* nested captures? */ + while (!isclosecap(cs->cap)) { /* traverse them */ + if (n >= MAXSTRCAPS) /* too many captures? */ + nextcap(cs); /* skip extra captures (will not need them) */ + else if (captype(cs->cap) == Csimple) /* string? */ + n = getstrcaps(cs, cps, n); /* put info. into array */ + else { + cps[n].isstring = 0; /* not a string */ + cps[n].u.cp = cs->cap; /* keep original capture */ + nextcap(cs); + n++; + } + } + cs->cap++; /* skip close */ + } + cps[k].u.s.e = closeaddr(cs->cap - 1); /* ends here */ + return n; +} + + +/* +** add next capture value (which should be a string) to buffer 'b' +*/ +static int addonestring (luaL_Buffer *b, CapState *cs, const char *what); + + +/* +** String capture: add result to buffer 'b' (instead of pushing +** it into the stack) +*/ +static void stringcap (luaL_Buffer *b, CapState *cs) { + StrAux cps[MAXSTRCAPS]; + int n; + size_t len, i; + const char *fmt; /* format string */ + fmt = lua_tolstring(cs->L, updatecache(cs, cs->cap->idx), &len); + n = getstrcaps(cs, cps, 0) - 1; /* collect nested captures */ + for (i = 0; i < len; i++) { /* traverse them */ + if (fmt[i] != '%') /* not an escape? */ + luaL_addchar(b, fmt[i]); /* add it to buffer */ + else if (fmt[++i] < '0' || fmt[i] > '9') /* not followed by a digit? */ + luaL_addchar(b, fmt[i]); /* add to buffer */ + else { + int l = fmt[i] - '0'; /* capture index */ + if (l > n) + luaL_error(cs->L, "invalid capture index (%d)", l); + else if (cps[l].isstring) + luaL_addlstring(b, cps[l].u.s.s, cps[l].u.s.e - cps[l].u.s.s); + else { + Capture *curr = cs->cap; + cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */ + if (!addonestring(b, cs, "capture")) + luaL_error(cs->L, "no values in capture index %d", l); + cs->cap = curr; /* continue from where it stopped */ + } + } + } +} + + +/* +** Substitution capture: add result to buffer 'b' +*/ +static void substcap (luaL_Buffer *b, CapState *cs) { + const char *curr = cs->cap->s; + if (isfullcap(cs->cap)) /* no nested captures? */ + luaL_addlstring(b, curr, cs->cap->siz - 1); /* keep original text */ + else { + cs->cap++; /* skip open entry */ + while (!isclosecap(cs->cap)) { /* traverse nested captures */ + const char *next = cs->cap->s; + luaL_addlstring(b, curr, next - curr); /* add text up to capture */ + if (addonestring(b, cs, "replacement")) + curr = closeaddr(cs->cap - 1); /* continue after match */ + else /* no capture value */ + curr = next; /* keep original text in final result */ + } + luaL_addlstring(b, curr, cs->cap->s - curr); /* add last piece of text */ + } + cs->cap++; /* go to next capture */ +} + + +/* +** Evaluates a capture and adds its first value to buffer 'b'; returns +** whether there was a value +*/ +static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { + switch (captype(cs->cap)) { + case Cstring: + stringcap(b, cs); /* add capture directly to buffer */ + return 1; + case Csubst: + substcap(b, cs); /* add capture directly to buffer */ + return 1; + default: { + lua_State *L = cs->L; + int n = pushcapture(cs); + if (n > 0) { + if (n > 1) lua_pop(L, n - 1); /* only one result */ + if (!lua_isstring(L, -1)) + luaL_error(L, "invalid %s value (a %s)", what, luaL_typename(L, -1)); + luaL_addvalue(b); + } + return n; + } + } +} + + +/* +** Push all values of the current capture into the stack; returns +** number of values pushed +*/ +static int pushcapture (CapState *cs) { + lua_State *L = cs->L; + luaL_checkstack(L, 4, "too many captures"); + switch (captype(cs->cap)) { + case Cposition: { + lua_pushinteger(L, cs->cap->s - cs->s + 1); + cs->cap++; + return 1; + } + case Cconst: { + pushluaval(cs); + cs->cap++; + return 1; + } + case Carg: { + int arg = (cs->cap++)->idx; + if (arg + FIXEDARGS > cs->ptop) + return luaL_error(L, "reference to absent extra argument #%d", arg); + lua_pushvalue(L, arg + FIXEDARGS); + return 1; + } + case Csimple: { + int k = pushnestedvalues(cs, 1); + lua_insert(L, -k); /* make whole match be first result */ + return k; + } + case Cruntime: { + lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */ + return 1; + } + case Cstring: { + luaL_Buffer b; + luaL_buffinit(L, &b); + stringcap(&b, cs); + luaL_pushresult(&b); + return 1; + } + case Csubst: { + luaL_Buffer b; + luaL_buffinit(L, &b); + substcap(&b, cs); + luaL_pushresult(&b); + return 1; + } + case Cgroup: { + if (cs->cap->idx == 0) /* anonymous group? */ + return pushnestedvalues(cs, 0); /* add all nested values */ + else { /* named group: add no values */ + nextcap(cs); /* skip capture */ + return 0; + } + } + case Cbackref: return backrefcap(cs); + case Ctable: return tablecap(cs); + case Cfunction: return functioncap(cs); + case Cnum: return numcap(cs); + case Cquery: return querycap(cs); + case Cfold: return foldcap(cs); + default: assert(0); return 0; + } +} + + +/* +** Prepare a CapState structure and traverse the entire list of +** captures in the stack pushing its results. 's' is the subject +** string, 'r' is the final position of the match, and 'ptop' +** the index in the stack where some useful values were pushed. +** Returns the number of results pushed. (If the list produces no +** results, push the final position of the match.) +*/ +int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { + Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop)); + int n = 0; + if (!isclosecap(capture)) { /* is there any capture? */ + CapState cs; + cs.ocap = cs.cap = capture; cs.L = L; + cs.s = s; cs.valuecached = 0; cs.ptop = ptop; + do { /* collect their values */ + n += pushcapture(&cs); + } while (!isclosecap(cs.cap)); + } + if (n == 0) { /* no capture values? */ + lua_pushinteger(L, r - s + 1); /* return only end position */ + n = 1; + } + return n; +} + + diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h new file mode 100644 index 00000000..6133df2a --- /dev/null +++ b/3rd/lpeg/lpcap.h @@ -0,0 +1,56 @@ +/* +** $Id: lpcap.h,v 1.3 2016/09/13 17:45:58 roberto Exp $ +*/ + +#if !defined(lpcap_h) +#define lpcap_h + + +#include "lptypes.h" + + +/* kinds of captures */ +typedef enum CapKind { + Cclose, /* not used in trees */ + Cposition, + Cconst, /* ktable[key] is Lua constant */ + Cbackref, /* ktable[key] is "name" of group to get capture */ + Carg, /* 'key' is arg's number */ + Csimple, /* next node is pattern */ + Ctable, /* next node is pattern */ + Cfunction, /* ktable[key] is function; next node is pattern */ + Cquery, /* ktable[key] is table; next node is pattern */ + Cstring, /* ktable[key] is string; next node is pattern */ + Cnum, /* numbered capture; 'key' is number of value to return */ + Csubst, /* substitution capture; next node is pattern */ + Cfold, /* ktable[key] is function; next node is pattern */ + Cruntime, /* not used in trees (is uses another type for tree) */ + Cgroup /* ktable[key] is group's "name" */ +} CapKind; + + +typedef struct Capture { + const char *s; /* 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) */ +} Capture; + + +typedef struct CapState { + Capture *cap; /* current capture */ + Capture *ocap; /* (original) capture list */ + lua_State *L; + int ptop; /* index of last argument to 'match' */ + const char *s; /* original string */ + int valuecached; /* value stored in cache slot */ +} CapState; + + +int runtimecap (CapState *cs, Capture *close, const char *s, int *rem); +int getcaptures (lua_State *L, const char *s, const char *r, int ptop); +int finddyncap (Capture *cap, Capture *last); + +#endif + + diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c new file mode 100644 index 00000000..2722d716 --- /dev/null +++ b/3rd/lpeg/lpcode.c @@ -0,0 +1,1014 @@ +/* +** $Id: lpcode.c,v 1.24 2016/09/15 17:46:13 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include + + +#include "lua.h" +#include "lauxlib.h" + +#include "lptypes.h" +#include "lpcode.h" + + +/* signals a "no-instruction */ +#define NOINST -1 + + + +static const Charset fullset_ = + {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + +static const Charset *fullset = &fullset_; + +/* +** {====================================================== +** Analysis and some optimizations +** ======================================================= +*/ + +/* +** Check whether a charset is empty (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 +*/ +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 +** value) +*/ +static int callrecursive (TTree *tree, int f (TTree *t), int def) { + int key = tree->key; + assert(tree->tag == TCall); + assert(sib2(tree)->tag == TRule); + if (key == 0) /* node already visited? */ + return def; /* return default value */ + else { /* first visit */ + int result; + tree->key = 0; /* mark call as already visited */ + result = f(sib2(tree)); /* go to called rule */ + tree->key = key; /* restore tree */ + return result; + } +} + + +/* +** Check whether a pattern tree has captures +*/ +int hascaptures (TTree *tree) { + tailcall: + switch (tree->tag) { + case TCapture: case TRunTime: + return 1; + case TCall: + return callrecursive(tree, hascaptures, 0); + case TRule: /* do not follow siblings */ + tree = sib1(tree); goto tailcall; + case TOpenCall: assert(0); + default: { + switch (numsiblings[tree->tag]) { + case 1: /* return hascaptures(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case 2: + if (hascaptures(sib1(tree))) + return 1; + /* else return hascaptures(sib2(tree)); */ + tree = sib2(tree); goto tailcall; + default: assert(numsiblings[tree->tag] == 0); return 0; + } + } + } +} + + +/* +** Checks how a pattern behaves regarding the empty string, +** in one of two different ways: +** A pattern is *nullable* if it can match without consuming any character; +** A pattern is *nofail* if it never fails for any string +** (including the empty string). +** The difference is only for predicates and run-time captures; +** for other patterns, the two properties are equivalent. +** (With predicates, &'a' is nullable but not nofail. Of course, +** nofail => nullable.) +** These functions are all convervative in the following way: +** p is nullable => nullable(p) +** nofail(p) => p cannot fail +** The function assumes that TOpenCall is not nullable; +** this will be checked again when the grammar is fixed. +** Run-time captures can do whatever they want, so the result +** is conservative. +*/ +int checkaux (TTree *tree, int pred) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + case TFalse: case TOpenCall: + return 0; /* not nullable */ + case TRep: case TTrue: + return 1; /* no fail */ + case TNot: case TBehind: /* can match empty, but can fail */ + if (pred == PEnofail) return 0; + else return 1; /* PEnullable */ + case TAnd: /* can match empty; fail iff body does */ + if (pred == PEnullable) return 1; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TRunTime: /* can fail; match empty iff body does */ + if (pred == PEnofail) return 0; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TSeq: + if (!checkaux(sib1(tree), pred)) return 0; + /* else return checkaux(sib2(tree), pred); */ + tree = sib2(tree); goto tailcall; + case TChoice: + if (checkaux(sib2(tree), pred)) return 1; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TCapture: case TGrammar: case TRule: + /* return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TCall: /* return checkaux(sib2(tree), pred); */ + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + } +} + + +/* +** number of characters to match a pattern (or -1 if variable) +*/ +int fixedlen (TTree *tree) { + int len = 0; /* to accumulate in tail calls */ + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + return len + 1; + case TFalse: case TTrue: case TNot: case TAnd: case TBehind: + return len; + case TRep: case TRunTime: case TOpenCall: + return -1; + case TCapture: case TRule: case TGrammar: + /* return fixedlen(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case TCall: { + int n1 = callrecursive(tree, fixedlen, -1); + if (n1 < 0) + return -1; + else + return len + n1; + } + case TSeq: { + int n1 = fixedlen(sib1(tree)); + if (n1 < 0) + return -1; + /* else return fixedlen(sib2(tree)) + len; */ + len += n1; tree = sib2(tree); goto tailcall; + } + case TChoice: { + int n1 = fixedlen(sib1(tree)); + int n2 = fixedlen(sib2(tree)); + if (n1 != n2 || n1 < 0) + return -1; + else + return len + n1; + } + default: assert(0); return 0; + }; +} + + +/* +** Computes the 'first set' of a pattern. +** The result is a conservative aproximation: +** match p ax -> x (for some x) ==> a belongs to first(p) +** or +** a not in first(p) ==> match p ax -> fail (for all x) +** +** The set 'follow' is the first set of what follows the +** pattern (full set if nothing follows it). +** +** The function returns 0 when this resulting set can be used for +** test instructions that avoid the pattern altogether. +** A non-zero return can happen for two reasons: +** 1) match p '' -> '' ==> return has bit 1 set +** (tests cannot be used because they would always fail for an empty input); +** 2) there is a match-time capture ==> return has bit 2 set +** (optimizations should not bypass match-time captures). +*/ +static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: { + tocharset(tree, firstset); + return 0; + } + case TTrue: { + loopset(i, firstset->cs[i] = follow->cs[i]); + return 1; /* 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); + int e2 = getfirst(sib2(tree), follow, &csaux); + loopset(i, firstset->cs[i] |= csaux.cs[i]); + return e1 | e2; + } + case TSeq: { + if (!nullable(sib1(tree))) { + /* when p1 is not nullable, p2 has nothing to contribute; + return getfirst(sib1(tree), fullset, firstset); */ + tree = sib1(tree); follow = fullset; goto tailcall; + } + else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */ + Charset csaux; + int e2 = getfirst(sib2(tree), follow, &csaux); + int e1 = getfirst(sib1(tree), &csaux, firstset); + if (e1 == 0) return 0; /* 'e1' ensures that first can be used */ + else if ((e1 | e2) & 2) /* one of the children has a matchtime? */ + return 2; /* pattern has a matchtime capture */ + else return e2; /* else depends on 'e2' */ + } + } + case TRep: { + getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] |= follow->cs[i]); + return 1; /* accept the empty string */ + } + case TCapture: case TGrammar: case TRule: { + /* return getfirst(sib1(tree), follow, firstset); */ + tree = sib1(tree); goto tailcall; + } + case TRunTime: { /* function invalidates any follow info. */ + int e = getfirst(sib1(tree), fullset, firstset); + if (e) return 2; /* function is not "protected"? */ + else return 0; /* pattern inside capture ensures first can be used */ + } + case TCall: { + /* return getfirst(sib2(tree), follow, firstset); */ + tree = sib2(tree); goto tailcall; + } + case TAnd: { + int e = getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] &= follow->cs[i]); + return e; + } + case TNot: { + if (tocharset(sib1(tree), firstset)) { + cs_complement(firstset); + return 1; + } + /* else go through */ + } + case TBehind: { /* instruction gives no new information */ + /* call 'getfirst' only to check for math-time captures */ + int e = getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */ + return e | 1; /* always can accept the empty string */ + } + default: assert(0); return 0; + } +} + + +/* +** If 'headfail(tree)' true, then 'tree' can fail only depending on the +** next character of the subject. +*/ +static int headfail (TTree *tree) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: case TFalse: + return 1; + case TTrue: case TRep: case TRunTime: case TNot: + case TBehind: + return 0; + case TCapture: case TGrammar: case TRule: case TAnd: + tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */ + case TCall: + tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */ + case TSeq: + if (!nofail(sib2(tree))) return 0; + /* else return headfail(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case TChoice: + if (!headfail(sib1(tree))) return 0; + /* else return headfail(sib2(tree)); */ + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + } +} + + +/* +** Check whether the code generation for the given tree can benefit +** from a follow set (to avoid computing the follow set when it is +** not needed) +*/ +static int needfollow (TTree *tree) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + case TFalse: case TTrue: case TAnd: case TNot: + case TRunTime: case TGrammar: case TCall: case TBehind: + return 0; + case TChoice: case TRep: + return 1; + case TCapture: + tree = sib1(tree); goto tailcall; + case TSeq: + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + } +} + +/* }====================================================== */ + + + +/* +** {====================================================== +** Code generation +** ======================================================= +*/ + + +/* +** size of an instruction +*/ +int sizei (const Instruction *i) { + switch((Opcode)i->i.code) { + case ISet: case ISpan: return CHARSETINSTSIZE; + case ITestSet: return CHARSETINSTSIZE + 1; + case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall: + case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit: + return 2; + default: return 1; + } +} + + +/* +** state for the compiler +*/ +typedef struct CompileState { + Pattern *p; /* pattern being compiled */ + int ncode; /* next position in p->code to be filled */ + lua_State *L; +} CompileState; + + +/* +** code generation is recursive; 'opt' indicates that the code is being +** generated as the last thing inside an optional pattern (so, if that +** code is optional too, it can reuse the 'IChoice' already in place for +** the outer pattern). 'tt' points to a previous test protecting this +** code (or NOINST). 'fl' is the follow set of the pattern. +*/ +static void codegen (CompileState *compst, TTree *tree, int opt, int tt, + const Charset *fl); + + +void realloccode (lua_State *L, Pattern *p, int nsize) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + void *newblock = f(ud, p->code, p->codesize * sizeof(Instruction), + nsize * sizeof(Instruction)); + if (newblock == NULL && nsize > 0) + luaL_error(L, "not enough memory"); + p->code = (Instruction *)newblock; + p->codesize = nsize; +} + + +static int nextinstruction (CompileState *compst) { + int size = compst->p->codesize; + if (compst->ncode >= size) + realloccode(compst->L, compst->p, size * 2); + return compst->ncode++; +} + + +#define getinstr(cs,i) ((cs)->p->code[i]) + + +static int addinstruction (CompileState *compst, Opcode op, int aux) { + int i = nextinstruction(compst); + getinstr(compst, i).i.code = op; + getinstr(compst, i).i.aux = aux; + return i; +} + + +/* +** Add an instruction followed by space for an offset (to be set later) +*/ +static int addoffsetinst (CompileState *compst, Opcode op) { + int i = addinstruction(compst, op, 0); /* instruction */ + addinstruction(compst, (Opcode)0, 0); /* open space for offset */ + assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2); + return i; +} + + +/* +** Set the offset of an instruction +*/ +static void setoffset (CompileState *compst, int instruction, int offset) { + getinstr(compst, instruction + 1).offset = offset; +} + + +/* +** Add a capture instruction: +** 'op' is the capture instruction; 'cap' the capture kind; +** 'key' the key into ktable; 'aux' is the optional capture offset +** +*/ +static int addinstcap (CompileState *compst, Opcode op, int cap, int key, + int aux) { + int i = addinstruction(compst, op, joinkindoff(cap, aux)); + getinstr(compst, i).i.key = key; + return i; +} + + +#define gethere(compst) ((compst)->ncode) + +#define target(code,i) ((i) + code[i + 1].offset) + + +/* +** Patch 'instruction' to jump to 'target' +*/ +static void jumptothere (CompileState *compst, int instruction, int target) { + if (instruction >= 0) + setoffset(compst, instruction, target - instruction); +} + + +/* +** Patch 'instruction' to jump to current position +*/ +static void jumptohere (CompileState *compst, int instruction) { + jumptothere(compst, instruction, gethere(compst)); +} + + +/* +** Code an IChar instruction, or IAny if there is an equivalent +** test dominating it +*/ +static void codechar (CompileState *compst, int c, int tt) { + if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && + getinstr(compst, tt).i.aux == c) + addinstruction(compst, IAny, 0); + else + addinstruction(compst, IChar, c); +} + + +/* +** Add a charset posfix to an instruction +*/ +static void addcharset (CompileState *compst, const byte *cs) { + int p = gethere(compst); + int i; + for (i = 0; i < (int)CHARSETINSTSIZE - 1; i++) + nextinstruction(compst); /* space for buffer */ + /* fill buffer with charset */ + loopset(j, getinstr(compst, p).buff[j] = cs[j]); +} + + +/* +** code a char set, optimizing unit sets for IChar, "complete" +** sets for IAny, and empty sets for IFail; also use an IAny +** when instruction is dominated by an equivalent test. +*/ +static void codecharset (CompileState *compst, const byte *cs, int tt) { + int c = 0; /* (=) to avoid warnings */ + Opcode op = charsettype(cs, &c); + switch (op) { + case IChar: codechar(compst, c, tt); break; + case ISet: { /* non-trivial set? */ + if (tt >= 0 && getinstr(compst, tt).i.code == ITestSet && + cs_equal(cs, getinstr(compst, tt + 2).buff)) + addinstruction(compst, IAny, 0); + else { + addinstruction(compst, ISet, 0); + addcharset(compst, cs); + } + break; + } + default: addinstruction(compst, op, c); break; + } +} + + +/* +** code a test set, optimizing unit sets for ITestChar, "complete" +** sets for ITestAny, and empty sets for IJmp (always fails). +** 'e' is true iff test should accept the empty string. (Test +** instructions in the current VM never accept the empty string.) +*/ +static int codetestset (CompileState *compst, Charset *cs, int e) { + if (e) return NOINST; /* no test */ + else { + int c = 0; + Opcode op = charsettype(cs->cs, &c); + switch (op) { + case IFail: return addoffsetinst(compst, IJmp); /* always jump */ + case IAny: return addoffsetinst(compst, ITestAny); + case IChar: { + int i = addoffsetinst(compst, ITestChar); + getinstr(compst, i).i.aux = c; + return i; + } + case ISet: { + int i = addoffsetinst(compst, ITestSet); + addcharset(compst, cs->cs); + return i; + } + default: assert(0); return 0; + } + } +} + + +/* +** Find the final destination of a sequence of jumps +*/ +static int finaltarget (Instruction *code, int i) { + while (code[i].i.code == IJmp) + i = target(code, i); + return i; +} + + +/* +** final label (after traversing any jumps) +*/ +static int finallabel (Instruction *code, int i) { + return finaltarget(code, target(code, i)); +} + + +/* +** == behind n;

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

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

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

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

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

; failtwice; L1: */ + int pchoice = addoffsetinst(compst, IChoice); + codegen(compst, tree, 0, NOINST, fullset); + addinstruction(compst, IFailTwice, 0); + jumptohere(compst, pchoice); + } + jumptohere(compst, test); +} + + +/* +** change open calls to calls, using list 'positions' to find +** correct offsets; also optimize tail calls +*/ +static void correctcalls (CompileState *compst, int *positions, + int from, int to) { + int i; + Instruction *code = compst->p->code; + for (i = from; i < to; i += sizei(&code[i])) { + if (code[i].i.code == IOpenCall) { + int n = code[i].i.key; /* rule number */ + int rule = positions[n]; /* rule position */ + assert(rule == from || code[rule - 1].i.code == IRet); + if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ + code[i].i.code = IJmp; /* tail call */ + else + code[i].i.code = ICall; + jumptothere(compst, i, rule); /* call jumps to respective rule */ + } + } + assert(i == to); +} + + +/* +** Code for a grammar: +** call L1; jmp L2; L1: rule 1; ret; rule 2; ret; ...; L2: +*/ +static void codegrammar (CompileState *compst, TTree *grammar) { + int positions[MAXRULES]; + int rulenumber = 0; + TTree *rule; + int firstcall = addoffsetinst(compst, ICall); /* call initial rule */ + int jumptoend = addoffsetinst(compst, IJmp); /* jump to the end */ + int start = gethere(compst); /* here starts the initial rule */ + jumptohere(compst, firstcall); + for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { + positions[rulenumber++] = gethere(compst); /* save rule position */ + codegen(compst, sib1(rule), 0, NOINST, fullset); /* code rule */ + addinstruction(compst, IRet, 0); + } + assert(rule->tag == TTrue); + jumptohere(compst, jumptoend); + correctcalls(compst, positions, start, gethere(compst)); +} + + +static void codecall (CompileState *compst, TTree *call) { + int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ + getinstr(compst, c).i.key = sib2(call)->cap; /* rule number */ + assert(sib2(call)->tag == TRule); +} + + +/* +** Code first child of a sequence +** (second child is called in-place to allow tail call) +** Return 'tt' for second child +*/ +static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2, + int tt, const Charset *fl) { + if (needfollow(p1)) { + Charset fl1; + getfirst(p2, fl, &fl1); /* p1 follow is p2 first */ + codegen(compst, p1, 0, tt, &fl1); + } + else /* use 'fullset' as follow */ + codegen(compst, p1, 0, tt, fullset); + if (fixedlen(p1) != 0) /* can 'p1' consume anything? */ + return NOINST; /* invalidate test */ + else return tt; /* else 'tt' still protects sib2 */ +} + + +/* +** Main code-generation function: dispatch to auxiliar functions +** according to kind of tree. ('needfollow' should return true +** only for consructions that use 'fl'.) +*/ +static void codegen (CompileState *compst, TTree *tree, int opt, int tt, + const Charset *fl) { + tailcall: + switch (tree->tag) { + case TChar: codechar(compst, tree->u.n, tt); break; + case TAny: addinstruction(compst, IAny, 0); break; + case TSet: codecharset(compst, treebuffer(tree), tt); break; + case TTrue: break; + case TFalse: addinstruction(compst, IFail, 0); break; + case TChoice: codechoice(compst, sib1(tree), sib2(tree), opt, fl); break; + case TRep: coderep(compst, sib1(tree), opt, fl); break; + case TBehind: codebehind(compst, tree); break; + case TNot: codenot(compst, sib1(tree)); break; + case TAnd: codeand(compst, sib1(tree), tt); break; + case TCapture: codecapture(compst, tree, tt, fl); break; + case TRunTime: coderuntime(compst, tree, tt); break; + case TGrammar: codegrammar(compst, tree); break; + case TCall: codecall(compst, tree); break; + case TSeq: { + tt = codeseq1(compst, sib1(tree), sib2(tree), tt, fl); /* code 'p1' */ + /* codegen(compst, p2, opt, tt, fl); */ + tree = sib2(tree); goto tailcall; + } + default: assert(0); + } +} + + +/* +** Optimize jumps and other jump-like instructions. +** * Update labels of instructions with labels to their final +** destinations (e.g., choice L1; ... L1: jmp L2: becomes +** choice L2) +** * Jumps to other instructions that do jumps become those +** instructions (e.g., jump to return becomes a return; jump +** to commit becomes a commit) +*/ +static void peephole (CompileState *compst) { + Instruction *code = compst->p->code; + int i; + for (i = 0; i < compst->ncode; i += sizei(&code[i])) { + redo: + switch (code[i].i.code) { + case IChoice: case ICall: case ICommit: case IPartialCommit: + case IBackCommit: case ITestChar: case ITestSet: + case ITestAny: { /* instructions with labels */ + jumptothere(compst, i, finallabel(code, i)); /* optimize label */ + break; + } + case IJmp: { + int ft = finaltarget(code, i); + switch (code[ft].i.code) { /* jumping to what? */ + case IRet: case IFail: case IFailTwice: + case IEnd: { /* instructions with unconditional implicit jumps */ + code[i] = code[ft]; /* jump becomes that instruction */ + code[i + 1].i.code = IAny; /* 'no-op' for target position */ + break; + } + case ICommit: case IPartialCommit: + case IBackCommit: { /* inst. with unconditional explicit jumps */ + int fft = finallabel(code, ft); + code[i] = code[ft]; /* jump becomes that instruction... */ + jumptothere(compst, i, fft); /* but must correct its offset */ + goto redo; /* reoptimize its label */ + } + default: { + jumptothere(compst, i, ft); /* optimize label */ + break; + } + } + break; + } + default: break; + } + } + assert(code[i - 1].i.code == IEnd); +} + + +/* +** Compile a pattern +*/ +Instruction *compile (lua_State *L, Pattern *p) { + CompileState compst; + compst.p = p; compst.ncode = 0; compst.L = L; + realloccode(L, p, 2); /* minimum initial size */ + codegen(&compst, p->tree, 0, NOINST, fullset); + addinstruction(&compst, IEnd, 0); + realloccode(L, p, compst.ncode); /* set final size */ + peephole(&compst); + return p->code; +} + + +/* }====================================================== */ + diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h new file mode 100644 index 00000000..2a5861ef --- /dev/null +++ b/3rd/lpeg/lpcode.h @@ -0,0 +1,40 @@ +/* +** $Id: lpcode.h,v 1.8 2016/09/15 17:46:13 roberto Exp $ +*/ + +#if !defined(lpcode_h) +#define lpcode_h + +#include "lua.h" + +#include "lptypes.h" +#include "lptree.h" +#include "lpvm.h" + +int tocharset (TTree *tree, Charset *cs); +int checkaux (TTree *tree, int pred); +int 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); +int sizei (const Instruction *i); + + +#define PEnullable 0 +#define PEnofail 1 + +/* +** nofail(t) implies that 't' cannot fail with any input +*/ +#define nofail(t) checkaux(t, PEnofail) + +/* +** (not nullable(t)) implies 't' cannot match without consuming +** something +*/ +#define nullable(t) checkaux(t, PEnullable) + + + +#endif diff --git a/3rd/lpeg/lpeg-128.gif b/3rd/lpeg/lpeg-128.gif new file mode 100644 index 00000000..bbf5e78b Binary files /dev/null and b/3rd/lpeg/lpeg-128.gif differ diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html new file mode 100644 index 00000000..5c9535f7 --- /dev/null +++ b/3rd/lpeg/lpeg.html @@ -0,0 +1,1445 @@ + + + + LPeg - Parsing Expression Grammars For Lua + + + + + + + +

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

Introduction

+ +

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

+ +

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

+ +

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

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

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

+ +

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

+ + +

Functions

+ + +

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

+

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

+ +

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

+ +

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

+ +

lpeg.type (value)

+

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

+ +

lpeg.version ()

+

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

+ +

lpeg.setmaxstack (max)

+

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

+ + +

Basic Constructions

+ +

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

+ + + +

lpeg.P (value)

+

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

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

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

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

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

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

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

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

  • + +
+ + +

lpeg.B(patt)

+

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

+ +

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

+ + +

lpeg.R ({range})

+

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

+ +

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

+ + +

lpeg.S (string)

+

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

+ +

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

+ +

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

+ + +

lpeg.V (v)

+

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

+ + +

lpeg.locale ([table])

+

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

+ +

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

+ + +

#patt

+

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

+ + +

+This pattern never produces any capture. +

+ + +

-patt

+

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

+ +

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

+ +

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

+ + +

patt1 + patt2

+

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

+ +

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

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

patt1 - patt2

+

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

+ +

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

+ +

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

+ + +

patt1 * patt2

+

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

+ +

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

+ + +

patt^n

+

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

+ +

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

+ +

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

+ +

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

+ + + +

Grammars

+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

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

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

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

Captures

+ +

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

+ +

+The following table summarizes the basic captures: +

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

+A capture pattern produces its values only when it succeeds. +For instance, +the pattern lpeg.C(lpeg.P"a"^-1) +produces the empty string when there is no "a" +(because the pattern "a"? succeeds), +while the pattern lpeg.C("a")^-1 +does not produce any value when there is no "a" +(because the pattern "a" fails). +A pattern inside a loop or inside a recursive structure +produces values for each match. +

+ +

+Usually, +LPeg does not specify when (and if) it evaluates its captures. +(As an example, +consider the pattern lpeg.P"a" / func / 0. +Because the "division" by 0 instructs LPeg to throw away the +results from the pattern, +LPeg may or may not call func.) +Therefore, captures should avoid side effects. +Moreover, +most captures cannot affect the way a pattern matches a subject. +The only exception to this rule is the +so-called match-time capture. +When a match-time capture matches, +it forces the immediate evaluation of all its nested captures +and then calls its corresponding function, +which defines whether the match succeeds and also +what values are produced. +

+ +

lpeg.C (patt)

+

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

+ + +

lpeg.Carg (n)

+

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

+ + +

lpeg.Cb (name)

+

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

+ +

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

+ +

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

+ +

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

+

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

+ + +

lpeg.Cf (patt, func)

+

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

+ +

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

+ +

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

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

lpeg.Cg (patt [, name])

+

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

+ +

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

+ + +

lpeg.Cp ()

+

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

+ + +

lpeg.Cs (patt)

+

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

+ + +

lpeg.Ct (patt)

+

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

+ + +

patt / string

+

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

+ + +

patt / number

+

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

+ + +

patt / table

+

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

+ + +

patt / function

+

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

+ + +

lpeg.Cmt(patt, function)

+

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

+ +

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

+ +

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

+ +

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

+ + + + +

Some Examples

+ +

Using a Pattern

+

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

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

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

+ + +

Name-value lists

+

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

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

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

+ +

Splitting a string

+

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

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

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

+ +

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

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

Searching for a pattern

+

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

+ +

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

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

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

+ +

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

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

+Another option for the search is like this: +

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

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

+ +

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

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

Balanced parentheses

+

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

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

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

+ + +

Global substitution

+

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

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

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

+ + +

Comma-Separated Values (CSV)

+

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

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

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

+ +

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

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

UTF-8 and Latin 1

+

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

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

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

+ +

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

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

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

+ + +

UTF-8 and Unicode

+

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

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

Lua's long strings

+

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

+ +

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

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

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

+ + +

Arithmetic expressions

+

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

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

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

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

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

+ + + +

Download

+ +

LPeg +source code.

+ + +

License

+ +

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

+

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

+ +

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

+ +

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

+ +
+ +
+ +
+

+$Id: lpeg.html,v 1.77 2017/01/13 13:40:05 roberto Exp $ +

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

The re Module

+ +

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

+ +

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

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

+ +

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

+ +

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

+ + +

Functions

+ +

re.compile (string, [, defs])

+

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

+ +

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

+

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

+ +

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

+ +

re.gsub (subject, pattern, replacement)

+

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

re.match (subject, pattern)

+

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

+ +

re.updatelocale ()

+

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

+ + +

Some Examples

+ +

A complete simple program

+

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

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

Balanced parentheses

+

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

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

String reversal

+

+The next example reverses a string: +

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

CSV decoder

+

+The next example replicates the CSV decoder: +

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

Lua's long strings

+

+The next example matches Lua long strings: +

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

Abstract Syntax Trees

+

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

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

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

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

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

+

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

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

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

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

Indented blocks

+

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

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

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

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

+The resulting table t will be like this: +

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

Macro expander

+

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

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

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

+ +

Patterns

+

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

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

License

+ +

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

+ +

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

+ +
+ +
+ +
+

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

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

1. Overview

-
-

The Lua CJSON module provides JSON support for Lua.

-
-
-Features -
-
-
    -
  • -

    -Fast, standards compliant encoding/parsing routines -

    -
  • -
  • -

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

    -
  • -
  • -

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

    -
  • -
  • -

    -No dependencies on other libraries -

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

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

    -
  • -
-
-
-

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

-

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

-

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

-
-

2. Installation

-
-

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

-

The build method can be selected from 4 options:

-
-
-Make -
-
-

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

-
-
-CMake -
-
-

-Unix, Windows -

-
-
-RPM -
-
-

-Linux -

-
-
-LuaRocks -
-
-

-Unix, Windows -

-
-
-

2.1. Make

-

The included Makefile has generic settings.

-

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

-

Next, build and install the module:

-
-
-
make install
-

Or install manually into your Lua module directory:

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

2.2. CMake

-

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

-

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

-

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

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

Next, build and install the module:

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

Review the -CMake documentation -for further details.

-

2.3. RPM

-

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

-

Build and install the module via RPM:

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

2.4. LuaRocks

-

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

-

First, extract the Lua CJSON source package.

-

Next, install the module:

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

Review the LuaRocks documentation -for further details.

-

2.5. Build Options (#define)

-

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

-
-
-USE_INTERNAL_ISINF -
-
-

-Workaround for Solaris platforms missing isinf. -

-
-
-DISABLE_INVALID_NUMBERS -
-
-

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

-
-
-

2.5.1. Built-in floating point conversion

-

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

-
-
-USE_INTERNAL_FPCONV -
-
-

-Enable internal number conversion routines. -

-
-
-IEEE_BIG_ENDIAN -
-
-

-Must be set on big endian architectures. -

-
-
-MULTIPLE_THREADS -
-
-

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

-
-
-
-

3. API (Functions)

-
-

3.1. Synopsis

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

3.2. Module Instantiation

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

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

-

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

-

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

-

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

-

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

-
    -
  • -

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

    -
  • -
  • -

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

    -
  • -
  • -

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

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

3.3. decode

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

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

-

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

-

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

-

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

-

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

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

3.4. decode_invalid_numbers

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

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

-
    -
  • -

    -infinity -

    -
  • -
  • -

    -not-a-number (NaN) -

    -
  • -
  • -

    -hexadecimal -

    -
  • -
-

Available settings:

-
-
-true -
-
-

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

-
-
-false -
-
-

-Throw an error when invalid numbers are encountered. -

-
-
-

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

-

3.5. decode_max_depth

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

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

-

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

-

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

-

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

-

3.6. encode

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

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

-

cjson.encode supports the following types:

-
    -
  • -

    -boolean -

    -
  • -
  • -

    -lightuserdata (NULL value only) -

    -
  • -
  • -

    -nil -

    -
  • -
  • -

    -number -

    -
  • -
  • -

    -string -

    -
  • -
  • -

    -table -

    -
  • -
-

The remaining Lua types will generate an error:

-
    -
  • -

    -function -

    -
  • -
  • -

    -lightuserdata (non-NULL values) -

    -
  • -
  • -

    -thread -

    -
  • -
  • -

    -userdata -

    -
  • -
-

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

-

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

-
    -
  • -

    -Control characters (ASCII 0 - 31) -

    -
  • -
  • -

    -Double quote (ASCII 34) -

    -
  • -
  • -

    -Forward slash (ASCII 47) -

    -
  • -
  • -

    -Blackslash (ASCII 92) -

    -
  • -
  • -

    -Delete (ASCII 127) -

    -
  • -
-

All other bytes are passed transparently.

-
- - - -
-
Caution
-
-

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

-

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

-
-
-

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

-

Lua CJSON does not use metamethods when serialising tables.

-
    -
  • -

    -rawget is used to iterate over Lua arrays -

    -
  • -
  • -

    -next is used to iterate over Lua objects -

    -
  • -
-

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

-

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

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

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

-
    -
  • -

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

    -
  • -
  • -

    -Tables nested more than 1000 levels deep -

    -
  • -
  • -

    -Excessively sparse Lua arrays -

    -
  • -
-

These defaults can be changed with:

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

3.7. encode_invalid_numbers

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

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

-
    -
  • -

    -infinity -

    -
  • -
  • -

    -not-a-number (NaN) -

    -
  • -
-

Available settings:

-
-
-true -
-
-

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

-
-
-"null" -
-
-

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

-
-
-false -
-
-

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

-
-
-

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

-

3.8. encode_keep_buffer

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

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

-

Available settings:

-
-
-true -
-
-

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

-
-
-false -
-
-

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

-
-
-

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

-

3.9. encode_max_depth

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

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

-

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

-

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

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

3.10. encode_number_precision

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

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

-

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

-

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

-

3.11. encode_sparse_array

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

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

-
-
-Normal -
-
-

-All values are available. -

-
-
-Sparse -
-
-

-At least 1 value is missing. -

-
-
-Excessively sparse -
-
-

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

-
-
-

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

-

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

-
    -
  • -

    -ratio > 0 -

    -
  • -
  • -

    -maximum_index > safe -

    -
  • -
  • -

    -maximum_index > item_count * ratio -

    -
  • -
-

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

-

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

-

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

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

4. API (Variables)

-
-

4.1. _NAME

-

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

-

4.2. _VERSION

-

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

-

4.3. null

-

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

-
-

5. References

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

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

-

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

-

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

-
-
-

1. Modules

-
-

The following JSON modules for Lua were tested:

-
-
-DKJSON 2.1 -
-
-
    -
  • -

    -Lua implementation with no dependencies on other libraries -

    -
  • -
  • -

    -Supports LPeg to improve decode performance -

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

    -C wrapper for the YAJL library -

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

    -C implementation with no dependencies on other libraries -

    -
  • -
-
-
-
-

2. Summary

-
-

All modules were built and tested as follows:

-
-
-DKJSON -
-
-

-Tested with/without LPeg 10.2. -

-
-
-Lua YAJL -
-
-

-Tested with YAJL 2.0.4. -

-
-
-Lua CJSON -
-
-

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

-
-
-

The following Lua implementations were used for this comparison:

-
-

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

-
-
Decoding performance
-
-
             | DKJSON                  | Lua YAJL   | Lua CJSON
-             | No LPeg     With LPeg   |            | Libc         Internal
-             | Lua  JIT    Lua    JIT  | Lua   JIT  | Lua   JIT    Lua   JIT
-example1     | 1x   2x     2.6x   3.4x | 7.1x  10x  | 14x   20x    14x   20x
-example2     | 1x   2.2x   2.9x   4.4x | 6.7x  9.9x | 14x   22x    14x   22x
-example3     | 1x   2.1x   3x     4.3x | 6.9x  9.3x | 14x   21x    15x   22x
-example4     | 1x   2x     2.5x   3.7x | 7.3x  10x  | 12x   19x    12x   20x
-example5     | 1x   2.2x   3x     4.5x | 7.8x  11x  | 16x   24x    16x   24x
-numbers      | 1x   2.2x   2.3x   4x   | 4.6x  5.5x | 8.9x  10x    13x   17x
-rfc-example1 | 1x   2.1x   2.8x   4.3x | 6.1x  8.1x | 13x   19x    14x   21x
-rfc-example2 | 1x   2.1x   3.1x   4.2x | 7.1x  9.2x | 15x   21x    17x   24x
-types        | 1x   2.2x   2.6x   4.3x | 5.3x  7.4x | 12x   20x    13x   21x
--------------|-------------------------|------------|-----------------------
-= Average => | 1x   2.1x   2.7x   4.1x | 6.5x  9x   | 13x   20x    14x   21x
-
-
-
Encoding performance
-
-
             | DKJSON                  | Lua YAJL   | Lua CJSON
-             | No LPeg     With LPeg   |            | Libc         Internal
-             | Lua  JIT    Lua    JIT  | Lua   JIT  | Lua   JIT    Lua   JIT
-example1     | 1x   1.8x   0.97x  1.6x | 3.1x  5.2x | 23x   29x    23x   29x
-example2     | 1x   2x     0.97x  1.7x | 2.6x  4.3x | 22x   28x    22x   28x
-example3     | 1x   1.9x   0.98x  1.6x | 2.8x  4.3x | 13x   15x    16x   18x
-example4     | 1x   1.7x   0.96x  1.3x | 3.9x  6.1x | 15x   19x    17x   21x
-example5     | 1x   2x     0.98x  1.7x | 2.7x  4.5x | 20x   23x    20x   23x
-numbers      | 1x   2.3x   1x     2.2x | 1.3x  1.9x | 3.8x  4.1x   4.2x  4.6x
-rfc-example1 | 1x   1.9x   0.97x  1.6x | 2.2x  3.2x | 8.5x  9.3x   11x   12x
-rfc-example2 | 1x   1.9x   0.98x  1.6x | 2.6x  3.9x | 10x   11x    17x   19x
-types        | 1x   2.2x   0.97x  2x   | 1.2x  1.9x | 11x   13x    12x   14x
--------------|-------------------------|------------|-----------------------
-= Average => | 1x   1.9x   0.98x  1.7x | 2.5x  3.9x | 14x   17x    16x   19x
-
-
- - - diff --git a/3rd/lua-cjson/performance.txt b/3rd/lua-cjson/performance.txt deleted file mode 100644 index fc3a5bb5..00000000 --- a/3rd/lua-cjson/performance.txt +++ /dev/null @@ -1,89 +0,0 @@ -JSON module performance comparison under Lua -============================================ -Mark Pulford -:revdate: January 22, 2012 - -This performance comparison aims to provide a guide of relative -performance between several fast and popular JSON modules. - -The examples used in this comparison were mostly sourced from the -http://json.org[JSON website] and -http://tools.ietf.org/html/rfc4627[RFC 4627]. - -Performance will vary widely between platforms and data sets. These -results should only be used as an approximation. - - -Modules -------- - -The following JSON modules for Lua were tested: - -http://chiselapp.com/user/dhkolf/repository/dkjson/[DKJSON 2.1]:: - - Lua implementation with no dependencies on other libraries - - Supports LPeg to improve decode performance - -https://github.com/brimworks/lua-yajl[Lua YAJL 2.0]:: - - C wrapper for the YAJL library - -http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CSJON 2.0.0]:: - - C implementation with no dependencies on other libraries - - -Summary -------- - -All modules were built and tested as follows: - -DKJSON:: Tested with/without LPeg 10.2. -Lua YAJL:: Tested with YAJL 2.0.4. -Lua CJSON:: Tested with Libc and internal floating point conversion - routines. - -The following Lua implementations were used for this comparison: - -- http://www.lua.org[Lua 5.1.4] (_Lua_) -- http://www.luajit.org[LuaJIT 2.0.0-beta9] (_JIT_) - -These results show the number of JSON operations per second sustained by -each module. All results have been normalised against the pure Lua -DKJSON implementation. - -.Decoding performance -............................................................................ - | DKJSON | Lua YAJL | Lua CJSON - | No LPeg With LPeg | | Libc Internal - | Lua JIT Lua JIT | Lua JIT | Lua JIT Lua JIT -example1 | 1x 2x 2.6x 3.4x | 7.1x 10x | 14x 20x 14x 20x -example2 | 1x 2.2x 2.9x 4.4x | 6.7x 9.9x | 14x 22x 14x 22x -example3 | 1x 2.1x 3x 4.3x | 6.9x 9.3x | 14x 21x 15x 22x -example4 | 1x 2x 2.5x 3.7x | 7.3x 10x | 12x 19x 12x 20x -example5 | 1x 2.2x 3x 4.5x | 7.8x 11x | 16x 24x 16x 24x -numbers | 1x 2.2x 2.3x 4x | 4.6x 5.5x | 8.9x 10x 13x 17x -rfc-example1 | 1x 2.1x 2.8x 4.3x | 6.1x 8.1x | 13x 19x 14x 21x -rfc-example2 | 1x 2.1x 3.1x 4.2x | 7.1x 9.2x | 15x 21x 17x 24x -types | 1x 2.2x 2.6x 4.3x | 5.3x 7.4x | 12x 20x 13x 21x --------------|-------------------------|------------|----------------------- -= Average => | 1x 2.1x 2.7x 4.1x | 6.5x 9x | 13x 20x 14x 21x -............................................................................ - -.Encoding performance -............................................................................. - | DKJSON | Lua YAJL | Lua CJSON - | No LPeg With LPeg | | Libc Internal - | Lua JIT Lua JIT | Lua JIT | Lua JIT Lua JIT -example1 | 1x 1.8x 0.97x 1.6x | 3.1x 5.2x | 23x 29x 23x 29x -example2 | 1x 2x 0.97x 1.7x | 2.6x 4.3x | 22x 28x 22x 28x -example3 | 1x 1.9x 0.98x 1.6x | 2.8x 4.3x | 13x 15x 16x 18x -example4 | 1x 1.7x 0.96x 1.3x | 3.9x 6.1x | 15x 19x 17x 21x -example5 | 1x 2x 0.98x 1.7x | 2.7x 4.5x | 20x 23x 20x 23x -numbers | 1x 2.3x 1x 2.2x | 1.3x 1.9x | 3.8x 4.1x 4.2x 4.6x -rfc-example1 | 1x 1.9x 0.97x 1.6x | 2.2x 3.2x | 8.5x 9.3x 11x 12x -rfc-example2 | 1x 1.9x 0.98x 1.6x | 2.6x 3.9x | 10x 11x 17x 19x -types | 1x 2.2x 0.97x 2x | 1.2x 1.9x | 11x 13x 12x 14x --------------|-------------------------|------------|----------------------- -= Average => | 1x 1.9x 0.98x 1.7x | 2.5x 3.9x | 14x 17x 16x 19x -............................................................................. - - -// vi:ft=asciidoc tw=72: diff --git a/3rd/lua-cjson/rfc4627.txt b/3rd/lua-cjson/rfc4627.txt deleted file mode 100644 index 67b89092..00000000 --- a/3rd/lua-cjson/rfc4627.txt +++ /dev/null @@ -1,563 +0,0 @@ - - - - - - -Network Working Group D. Crockford -Request for Comments: 4627 JSON.org -Category: Informational July 2006 - - - The application/json Media Type for JavaScript Object Notation (JSON) - -Status of This Memo - - This memo provides information for the Internet community. It does - not specify an Internet standard of any kind. Distribution of this - memo is unlimited. - -Copyright Notice - - Copyright (C) The Internet Society (2006). - -Abstract - - JavaScript Object Notation (JSON) is a lightweight, text-based, - language-independent data interchange format. It was derived from - the ECMAScript Programming Language Standard. JSON defines a small - set of formatting rules for the portable representation of structured - data. - -1. Introduction - - JavaScript Object Notation (JSON) is a text format for the - serialization of structured data. It is derived from the object - literals of JavaScript, as defined in the ECMAScript Programming - Language Standard, Third Edition [ECMA]. - - JSON can represent four primitive types (strings, numbers, booleans, - and null) and two structured types (objects and arrays). - - A string is a sequence of zero or more Unicode characters [UNICODE]. - - An object is an unordered collection of zero or more name/value - pairs, where a name is a string and a value is a string, number, - boolean, null, object, or array. - - An array is an ordered sequence of zero or more values. - - The terms "object" and "array" come from the conventions of - JavaScript. - - JSON's design goals were for it to be minimal, portable, textual, and - a subset of JavaScript. - - - -Crockford Informational [Page 1] - -RFC 4627 JSON July 2006 - - -1.1. Conventions Used in This Document - - The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", - "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this - document are to be interpreted as described in [RFC2119]. - - The grammatical rules in this document are to be interpreted as - described in [RFC4234]. - -2. JSON Grammar - - A JSON text is a sequence of tokens. The set of tokens includes six - structural characters, strings, numbers, and three literal names. - - A JSON text is a serialized object or array. - - JSON-text = object / array - - These are the six structural characters: - - begin-array = ws %x5B ws ; [ left square bracket - - begin-object = ws %x7B ws ; { left curly bracket - - end-array = ws %x5D ws ; ] right square bracket - - end-object = ws %x7D ws ; } right curly bracket - - name-separator = ws %x3A ws ; : colon - - value-separator = ws %x2C ws ; , comma - - Insignificant whitespace is allowed before or after any of the six - structural characters. - - ws = *( - %x20 / ; Space - %x09 / ; Horizontal tab - %x0A / ; Line feed or New line - %x0D ; Carriage return - ) - -2.1. Values - - A JSON value MUST be an object, array, number, or string, or one of - the following three literal names: - - false null true - - - -Crockford Informational [Page 2] - -RFC 4627 JSON July 2006 - - - The literal names MUST be lowercase. No other literal names are - allowed. - - value = false / null / true / object / array / number / string - - false = %x66.61.6c.73.65 ; false - - null = %x6e.75.6c.6c ; null - - true = %x74.72.75.65 ; true - -2.2. Objects - - An object structure is represented as a pair of curly brackets - surrounding zero or more name/value pairs (or members). A name is a - string. A single colon comes after each name, separating the name - from the value. A single comma separates a value from a following - name. The names within an object SHOULD be unique. - - object = begin-object [ member *( value-separator member ) ] - end-object - - member = string name-separator value - -2.3. Arrays - - An array structure is represented as square brackets surrounding zero - or more values (or elements). Elements are separated by commas. - - array = begin-array [ value *( value-separator value ) ] end-array - -2.4. Numbers - - The representation of numbers is similar to that used in most - programming languages. A number contains an integer component that - may be prefixed with an optional minus sign, which may be followed by - a fraction part and/or an exponent part. - - Octal and hex forms are not allowed. Leading zeros are not allowed. - - A fraction part is a decimal point followed by one or more digits. - - An exponent part begins with the letter E in upper or lowercase, - which may be followed by a plus or minus sign. The E and optional - sign are followed by one or more digits. - - Numeric values that cannot be represented as sequences of digits - (such as Infinity and NaN) are not permitted. - - - -Crockford Informational [Page 3] - -RFC 4627 JSON July 2006 - - - number = [ minus ] int [ frac ] [ exp ] - - decimal-point = %x2E ; . - - digit1-9 = %x31-39 ; 1-9 - - e = %x65 / %x45 ; e E - - exp = e [ minus / plus ] 1*DIGIT - - frac = decimal-point 1*DIGIT - - int = zero / ( digit1-9 *DIGIT ) - - minus = %x2D ; - - - plus = %x2B ; + - - zero = %x30 ; 0 - -2.5. Strings - - The representation of strings is similar to conventions used in the C - family of programming languages. A string begins and ends with - quotation marks. All Unicode characters may be placed within the - quotation marks except for the characters that must be escaped: - quotation mark, reverse solidus, and the control characters (U+0000 - through U+001F). - - Any character may be escaped. If the character is in the Basic - Multilingual Plane (U+0000 through U+FFFF), then it may be - represented as a six-character sequence: a reverse solidus, followed - by the lowercase letter u, followed by four hexadecimal digits that - encode the character's code point. The hexadecimal letters A though - F can be upper or lowercase. So, for example, a string containing - only a single reverse solidus character may be represented as - "\u005C". - - Alternatively, there are two-character sequence escape - representations of some popular characters. So, for example, a - string containing only a single reverse solidus character may be - represented more compactly as "\\". - - To escape an extended character that is not in the Basic Multilingual - Plane, the character is represented as a twelve-character sequence, - encoding the UTF-16 surrogate pair. So, for example, a string - containing only the G clef character (U+1D11E) may be represented as - "\uD834\uDD1E". - - - -Crockford Informational [Page 4] - -RFC 4627 JSON July 2006 - - - string = quotation-mark *char quotation-mark - - char = unescaped / - escape ( - %x22 / ; " quotation mark U+0022 - %x5C / ; \ reverse solidus U+005C - %x2F / ; / solidus U+002F - %x62 / ; b backspace U+0008 - %x66 / ; f form feed U+000C - %x6E / ; n line feed U+000A - %x72 / ; r carriage return U+000D - %x74 / ; t tab U+0009 - %x75 4HEXDIG ) ; uXXXX U+XXXX - - escape = %x5C ; \ - - quotation-mark = %x22 ; " - - unescaped = %x20-21 / %x23-5B / %x5D-10FFFF - -3. Encoding - - JSON text SHALL be encoded in Unicode. The default encoding is - UTF-8. - - Since the first two characters of a JSON text will always be ASCII - characters [RFC0020], it is possible to determine whether an octet - stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking - at the pattern of nulls in the first four octets. - - 00 00 00 xx UTF-32BE - 00 xx 00 xx UTF-16BE - xx 00 00 00 UTF-32LE - xx 00 xx 00 UTF-16LE - xx xx xx xx UTF-8 - -4. Parsers - - A JSON parser transforms a JSON text into another representation. A - JSON parser MUST accept all texts that conform to the JSON grammar. - A JSON parser MAY accept non-JSON forms or extensions. - - An implementation may set limits on the size of texts that it - accepts. An implementation may set limits on the maximum depth of - nesting. An implementation may set limits on the range of numbers. - An implementation may set limits on the length and character contents - of strings. - - - - -Crockford Informational [Page 5] - -RFC 4627 JSON July 2006 - - -5. Generators - - A JSON generator produces JSON text. The resulting text MUST - strictly conform to the JSON grammar. - -6. IANA Considerations - - The MIME media type for JSON text is application/json. - - Type name: application - - Subtype name: json - - Required parameters: n/a - - Optional parameters: n/a - - Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32 - - JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON - is written in UTF-8, JSON is 8bit compatible. When JSON is - written in UTF-16 or UTF-32, the binary content-transfer-encoding - must be used. - - Security considerations: - - Generally there are security issues with scripting languages. JSON - is a subset of JavaScript, but it is a safe subset that excludes - assignment and invocation. - - A JSON text can be safely passed into JavaScript's eval() function - (which compiles and executes a string) if all the characters not - enclosed in strings are in the set of characters that form JSON - tokens. This can be quickly determined in JavaScript with two - regular expressions and calls to the test and replace methods. - - var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( - text.replace(/"(\\.|[^"\\])*"/g, ''))) && - eval('(' + text + ')'); - - Interoperability considerations: n/a - - Published specification: RFC 4627 - - - - - - - - -Crockford Informational [Page 6] - -RFC 4627 JSON July 2006 - - - Applications that use this media type: - - JSON has been used to exchange data between applications written - in all of these programming languages: ActionScript, C, C#, - ColdFusion, Common Lisp, E, Erlang, Java, JavaScript, Lua, - Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Scheme. - - Additional information: - - Magic number(s): n/a - File extension(s): .json - Macintosh file type code(s): TEXT - - Person & email address to contact for further information: - Douglas Crockford - douglas@crockford.com - - Intended usage: COMMON - - Restrictions on usage: none - - Author: - Douglas Crockford - douglas@crockford.com - - Change controller: - Douglas Crockford - douglas@crockford.com - -7. Security Considerations - - See Security Considerations in Section 6. - -8. Examples - - This is a JSON object: - - { - "Image": { - "Width": 800, - "Height": 600, - "Title": "View from 15th Floor", - "Thumbnail": { - "Url": "http://www.example.com/image/481989943", - "Height": 125, - "Width": "100" - }, - "IDs": [116, 943, 234, 38793] - - - -Crockford Informational [Page 7] - -RFC 4627 JSON July 2006 - - - } - } - - Its Image member is an object whose Thumbnail member is an object - and whose IDs member is an array of numbers. - - This is a JSON array containing two objects: - - [ - { - "precision": "zip", - "Latitude": 37.7668, - "Longitude": -122.3959, - "Address": "", - "City": "SAN FRANCISCO", - "State": "CA", - "Zip": "94107", - "Country": "US" - }, - { - "precision": "zip", - "Latitude": 37.371991, - "Longitude": -122.026020, - "Address": "", - "City": "SUNNYVALE", - "State": "CA", - "Zip": "94085", - "Country": "US" - } - ] - -9. References - -9.1. Normative References - - [ECMA] European Computer Manufacturers Association, "ECMAScript - Language Specification 3rd Edition", December 1999, - . - - [RFC0020] Cerf, V., "ASCII format for network interchange", RFC 20, - October 1969. - - [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate - Requirement Levels", BCP 14, RFC 2119, March 1997. - - [RFC4234] Crocker, D. and P. Overell, "Augmented BNF for Syntax - Specifications: ABNF", RFC 4234, October 2005. - - - -Crockford Informational [Page 8] - -RFC 4627 JSON July 2006 - - - [UNICODE] The Unicode Consortium, "The Unicode Standard Version 4.0", - 2003, . - -Author's Address - - Douglas Crockford - JSON.org - EMail: douglas@crockford.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Crockford Informational [Page 9] - -RFC 4627 JSON July 2006 - - -Full Copyright Statement - - Copyright (C) The Internet Society (2006). - - This document is subject to the rights, licenses and restrictions - contained in BCP 78, and except as set forth therein, the authors - retain all their rights. - - This document and the information contained herein are provided on an - "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS - OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET - ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, - INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE - INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED - WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - -Intellectual Property - - The IETF takes no position regarding the validity or scope of any - Intellectual Property Rights or other rights that might be claimed to - pertain to the implementation or use of the technology described in - this document or the extent to which any license under such rights - might or might not be available; nor does it represent that it has - made any independent effort to identify any such rights. Information - on the procedures with respect to rights in RFC documents can be - found in BCP 78 and BCP 79. - - Copies of IPR disclosures made to the IETF Secretariat and any - assurances of licenses to be made available, or the result of an - attempt made to obtain a general license or permission for the use of - such proprietary rights by implementers or users of this - specification can be obtained from the IETF on-line IPR repository at - http://www.ietf.org/ipr. - - The IETF invites any interested party to bring to its attention any - copyrights, patents or patent applications, or other proprietary - rights that may cover technology that may be required to implement - this standard. Please address the information to the IETF at - ietf-ipr@ietf.org. - -Acknowledgement - - Funding for the RFC Editor function is provided by the IETF - Administrative Support Activity (IASA). - - - - - - - -Crockford Informational [Page 10] - diff --git a/3rd/lua-cjson/runtests.sh b/3rd/lua-cjson/runtests.sh deleted file mode 100755 index c87a8ee0..00000000 --- a/3rd/lua-cjson/runtests.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/sh - -PLATFORM="`uname -s`" -[ "$1" ] && VERSION="$1" || VERSION="2.1.0" - -set -e - -# Portable "ggrep -A" replacement. -# Work around Solaris awk record limit of 2559 bytes. -# contextgrep PATTERN POST_MATCH_LINES -contextgrep() { - cut -c -2500 | awk "/$1/ { count = ($2 + 1) } count > 0 { count--; print }" -} - -do_tests() { - echo - cd tests - lua -e 'print("Testing Lua CJSON version " .. require("cjson")._VERSION)' - ./test.lua | contextgrep 'FAIL|Summary' 3 | grep -v PASS | cut -c -150 - cd .. -} - -echo "===== Setting LuaRocks PATH =====" -eval "`luarocks path`" - -echo "===== Building UTF-8 test data =====" -( cd tests && ./genutf8.pl; ) - -echo "===== Cleaning old build data =====" -make clean -rm -f tests/cjson.so - -echo "===== Verifying cjson.so is not installed =====" - -cd tests -if lua -e 'require "cjson"' 2>/dev/null -then - cat < "$LOG" - RPM="`awk '/^Wrote: / && ! /debuginfo/ { print $2}' < "$LOG"`" - sudo -- rpm -Uvh \"$RPM\" - do_tests - sudo -- rpm -e lua-cjson - rm -f "$LOG" - else - echo "==> skipping, $TGZ not found" - fi -fi - -# vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/strbuf.c b/3rd/lua-cjson/strbuf.c deleted file mode 100644 index f0f7f4b9..00000000 --- a/3rd/lua-cjson/strbuf.c +++ /dev/null @@ -1,251 +0,0 @@ -/* strbuf - String buffer routines - * - * Copyright (c) 2010-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include -#include - -#include "strbuf.h" - -static void die(const char *fmt, ...) -{ - va_list arg; - - va_start(arg, fmt); - vfprintf(stderr, fmt, arg); - va_end(arg); - fprintf(stderr, "\n"); - - exit(-1); -} - -void strbuf_init(strbuf_t *s, int len) -{ - int size; - - if (len <= 0) - size = STRBUF_DEFAULT_SIZE; - else - size = len + 1; /* \0 terminator */ - - s->buf = NULL; - s->size = size; - s->length = 0; - s->increment = STRBUF_DEFAULT_INCREMENT; - s->dynamic = 0; - s->reallocs = 0; - s->debug = 0; - - s->buf = malloc(size); - if (!s->buf) - die("Out of memory"); - - strbuf_ensure_null(s); -} - -strbuf_t *strbuf_new(int len) -{ - strbuf_t *s; - - s = malloc(sizeof(strbuf_t)); - if (!s) - die("Out of memory"); - - strbuf_init(s, len); - - /* Dynamic strbuf allocation / deallocation */ - s->dynamic = 1; - - return s; -} - -void strbuf_set_increment(strbuf_t *s, int increment) -{ - /* Increment > 0: Linear buffer growth rate - * Increment < -1: Exponential buffer growth rate */ - if (increment == 0 || increment == -1) - die("BUG: Invalid string increment"); - - s->increment = increment; -} - -static inline void debug_stats(strbuf_t *s) -{ - if (s->debug) { - fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n", - (long)s, s->reallocs, s->length, s->size); - } -} - -/* If strbuf_t has not been dynamically allocated, strbuf_free() can - * be called any number of times strbuf_init() */ -void strbuf_free(strbuf_t *s) -{ - debug_stats(s); - - if (s->buf) { - free(s->buf); - s->buf = NULL; - } - if (s->dynamic) - free(s); -} - -char *strbuf_free_to_string(strbuf_t *s, int *len) -{ - char *buf; - - debug_stats(s); - - strbuf_ensure_null(s); - - buf = s->buf; - if (len) - *len = s->length; - - if (s->dynamic) - free(s); - - return buf; -} - -static int calculate_new_size(strbuf_t *s, int len) -{ - int reqsize, newsize; - - if (len <= 0) - die("BUG: Invalid strbuf length requested"); - - /* Ensure there is room for optional NULL termination */ - reqsize = len + 1; - - /* If the user has requested to shrink the buffer, do it exactly */ - if (s->size > reqsize) - return reqsize; - - newsize = s->size; - if (s->increment < 0) { - /* Exponential sizing */ - while (newsize < reqsize) - newsize *= -s->increment; - } else { - /* Linear sizing */ - newsize = ((newsize + s->increment - 1) / s->increment) * s->increment; - } - - return newsize; -} - - -/* Ensure strbuf can handle a string length bytes long (ignoring NULL - * optional termination). */ -void strbuf_resize(strbuf_t *s, int len) -{ - int newsize; - - newsize = calculate_new_size(s, len); - - if (s->debug > 1) { - fprintf(stderr, "strbuf(%lx) resize: %d => %d\n", - (long)s, s->size, newsize); - } - - s->size = newsize; - s->buf = realloc(s->buf, s->size); - if (!s->buf) - die("Out of memory"); - s->reallocs++; -} - -void strbuf_append_string(strbuf_t *s, const char *str) -{ - int space, i; - - space = strbuf_empty_length(s); - - for (i = 0; str[i]; i++) { - if (space < 1) { - strbuf_resize(s, s->length + 1); - space = strbuf_empty_length(s); - } - - s->buf[s->length] = str[i]; - s->length++; - space--; - } -} - -/* strbuf_append_fmt() should only be used when an upper bound - * is known for the output string. */ -void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...) -{ - va_list arg; - int fmt_len; - - strbuf_ensure_empty_length(s, len); - - va_start(arg, fmt); - fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg); - va_end(arg); - - if (fmt_len < 0) - die("BUG: Unable to convert number"); /* This should never happen.. */ - - s->length += fmt_len; -} - -/* strbuf_append_fmt_retry() can be used when the there is no known - * upper bound for the output string. */ -void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...) -{ - va_list arg; - int fmt_len, try; - int empty_len; - - /* If the first attempt to append fails, resize the buffer appropriately - * and try again */ - for (try = 0; ; try++) { - va_start(arg, fmt); - /* Append the new formatted string */ - /* fmt_len is the length of the string required, excluding the - * trailing NULL */ - empty_len = strbuf_empty_length(s); - /* Add 1 since there is also space to store the terminating NULL. */ - fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg); - va_end(arg); - - if (fmt_len <= empty_len) - break; /* SUCCESS */ - if (try > 0) - die("BUG: length of formatted string changed"); - - strbuf_resize(s, s->length + fmt_len); - } - - s->length += fmt_len; -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/strbuf.h b/3rd/lua-cjson/strbuf.h deleted file mode 100644 index d861108c..00000000 --- a/3rd/lua-cjson/strbuf.h +++ /dev/null @@ -1,154 +0,0 @@ -/* strbuf - String buffer routines - * - * Copyright (c) 2010-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include - -/* Size: Total bytes allocated to *buf - * Length: String length, excluding optional NULL terminator. - * Increment: Allocation increments when resizing the string buffer. - * Dynamic: True if created via strbuf_new() - */ - -typedef struct { - char *buf; - int size; - int length; - int increment; - int dynamic; - int reallocs; - int debug; -} strbuf_t; - -#ifndef STRBUF_DEFAULT_SIZE -#define STRBUF_DEFAULT_SIZE 1023 -#endif -#ifndef STRBUF_DEFAULT_INCREMENT -#define STRBUF_DEFAULT_INCREMENT -2 -#endif - -/* Initialise */ -extern strbuf_t *strbuf_new(int len); -extern void strbuf_init(strbuf_t *s, int len); -extern void strbuf_set_increment(strbuf_t *s, int increment); - -/* Release */ -extern void strbuf_free(strbuf_t *s); -extern char *strbuf_free_to_string(strbuf_t *s, int *len); - -/* Management */ -extern void strbuf_resize(strbuf_t *s, int len); -static int strbuf_empty_length(strbuf_t *s); -static int strbuf_length(strbuf_t *s); -static char *strbuf_string(strbuf_t *s, int *len); -static void strbuf_ensure_empty_length(strbuf_t *s, int len); -static char *strbuf_empty_ptr(strbuf_t *s); -static void strbuf_extend_length(strbuf_t *s, int len); - -/* Update */ -extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...); -extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...); -static void strbuf_append_mem(strbuf_t *s, const char *c, int len); -extern void strbuf_append_string(strbuf_t *s, const char *str); -static void strbuf_append_char(strbuf_t *s, const char c); -static void strbuf_ensure_null(strbuf_t *s); - -/* Reset string for before use */ -static inline void strbuf_reset(strbuf_t *s) -{ - s->length = 0; -} - -static inline int strbuf_allocated(strbuf_t *s) -{ - return s->buf != NULL; -} - -/* Return bytes remaining in the string buffer - * Ensure there is space for a NULL terminator. */ -static inline int strbuf_empty_length(strbuf_t *s) -{ - return s->size - s->length - 1; -} - -static inline void strbuf_ensure_empty_length(strbuf_t *s, int len) -{ - if (len > strbuf_empty_length(s)) - strbuf_resize(s, s->length + len); -} - -static inline char *strbuf_empty_ptr(strbuf_t *s) -{ - return s->buf + s->length; -} - -static inline void strbuf_extend_length(strbuf_t *s, int len) -{ - s->length += len; -} - -static inline int strbuf_length(strbuf_t *s) -{ - return s->length; -} - -static inline void strbuf_append_char(strbuf_t *s, const char c) -{ - strbuf_ensure_empty_length(s, 1); - s->buf[s->length++] = c; -} - -static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c) -{ - s->buf[s->length++] = c; -} - -static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len) -{ - strbuf_ensure_empty_length(s, len); - memcpy(s->buf + s->length, c, len); - s->length += len; -} - -static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len) -{ - memcpy(s->buf + s->length, c, len); - s->length += len; -} - -static inline void strbuf_ensure_null(strbuf_t *s) -{ - s->buf[s->length] = 0; -} - -static inline char *strbuf_string(strbuf_t *s, int *len) -{ - if (len) - *len = s->length; - - return s->buf; -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/tests/README b/3rd/lua-cjson/tests/README deleted file mode 100644 index 39e8bd45..00000000 --- a/3rd/lua-cjson/tests/README +++ /dev/null @@ -1,4 +0,0 @@ -These JSON examples were taken from the JSON website -(http://json.org/example.html) and RFC 4627. - -Used with permission. diff --git a/3rd/lua-cjson/tests/bench.lua b/3rd/lua-cjson/tests/bench.lua deleted file mode 100755 index 648020b1..00000000 --- a/3rd/lua-cjson/tests/bench.lua +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env lua - --- This benchmark script measures wall clock time and should be --- run on an unloaded system. --- --- Your Mileage May Vary. --- --- Mark Pulford - -local json_module = os.getenv("JSON_MODULE") or "cjson" - -require "socket" -local json = require(json_module) -local util = require "cjson.util" - -local function find_func(mod, funcnames) - for _, v in ipairs(funcnames) do - if mod[v] then - return mod[v] - end - end - - return nil -end - -local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" }) -local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" }) - -local function average(t) - local total = 0 - for _, v in ipairs(t) do - total = total + v - end - return total / #t -end - -function benchmark(tests, seconds, rep) - local function bench(func, iter) - -- Use socket.gettime() to measure microsecond resolution - -- wall clock time. - local t = socket.gettime() - for i = 1, iter do - func(i) - end - t = socket.gettime() - t - - -- Don't trust any results when the run lasted for less than a - -- millisecond - return nil. - if t < 0.001 then - return nil - end - - return (iter / t) - end - - -- Roughly calculate the number of interations required - -- to obtain a particular time period. - local function calc_iter(func, seconds) - local iter = 1 - local rate - -- Warm up the bench function first. - func() - while not rate do - rate = bench(func, iter) - iter = iter * 10 - end - return math.ceil(seconds * rate) - end - - local test_results = {} - for name, func in pairs(tests) do - -- k(number), v(string) - -- k(string), v(function) - -- k(number), v(function) - if type(func) == "string" then - name = func - func = _G[name] - end - - local iter = calc_iter(func, seconds) - - local result = {} - for i = 1, rep do - result[i] = bench(func, iter) - end - - -- Remove the slowest half (round down) of the result set - table.sort(result) - for i = 1, math.floor(#result / 2) do - table.remove(result, 1) - end - - test_results[name] = average(result) - end - - return test_results -end - -function bench_file(filename) - local data_json = util.file_load(filename) - local data_obj = json_decode(data_json) - - local function test_encode() - json_encode(data_obj) - end - local function test_decode() - json_decode(data_json) - end - - local tests = {} - if json_encode then tests.encode = test_encode end - if json_decode then tests.decode = test_decode end - - return benchmark(tests, 0.1, 5) -end - --- Optionally load any custom configuration required for this module -local success, data = pcall(util.file_load, ("bench-%s.lua"):format(json_module)) -if success then - util.run_script(data, _G) - configure(json) -end - -for i = 1, #arg do - local results = bench_file(arg[i]) - for k, v in pairs(results) do - print(("%s\t%s\t%d"):format(arg[i], k, v)) - end -end - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/example1.json b/3rd/lua-cjson/tests/example1.json deleted file mode 100644 index 42486cec..00000000 --- a/3rd/lua-cjson/tests/example1.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "glossary": { - "title": "example glossary", - "GlossDiv": { - "title": "S", - "GlossList": { - "GlossEntry": { - "ID": "SGML", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Mark up Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { - "para": "A meta-markup language, used to create markup languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"] - }, - "GlossSee": "markup" - } - } - } - } -} diff --git a/3rd/lua-cjson/tests/example2.json b/3rd/lua-cjson/tests/example2.json deleted file mode 100644 index 5600991a..00000000 --- a/3rd/lua-cjson/tests/example2.json +++ /dev/null @@ -1,11 +0,0 @@ -{"menu": { - "id": "file", - "value": "File", - "popup": { - "menuitem": [ - {"value": "New", "onclick": "CreateNewDoc()"}, - {"value": "Open", "onclick": "OpenDoc()"}, - {"value": "Close", "onclick": "CloseDoc()"} - ] - } -}} diff --git a/3rd/lua-cjson/tests/example3.json b/3rd/lua-cjson/tests/example3.json deleted file mode 100644 index d7237a5a..00000000 --- a/3rd/lua-cjson/tests/example3.json +++ /dev/null @@ -1,26 +0,0 @@ -{"widget": { - "debug": "on", - "window": { - "title": "Sample Konfabulator Widget", - "name": "main_window", - "width": 500, - "height": 500 - }, - "image": { - "src": "Images/Sun.png", - "name": "sun1", - "hOffset": 250, - "vOffset": 250, - "alignment": "center" - }, - "text": { - "data": "Click Here", - "size": 36, - "style": "bold", - "name": "text1", - "hOffset": 250, - "vOffset": 100, - "alignment": "center", - "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" - } -}} diff --git a/3rd/lua-cjson/tests/example4.json b/3rd/lua-cjson/tests/example4.json deleted file mode 100644 index d31a395b..00000000 --- a/3rd/lua-cjson/tests/example4.json +++ /dev/null @@ -1,88 +0,0 @@ -{"web-app": { - "servlet": [ - { - "servlet-name": "cofaxCDS", - "servlet-class": "org.cofax.cds.CDSServlet", - "init-param": { - "configGlossary:installationAt": "Philadelphia, PA", - "configGlossary:adminEmail": "ksm@pobox.com", - "configGlossary:poweredBy": "Cofax", - "configGlossary:poweredByIcon": "/images/cofax.gif", - "configGlossary:staticPath": "/content/static", - "templateProcessorClass": "org.cofax.WysiwygTemplate", - "templateLoaderClass": "org.cofax.FilesTemplateLoader", - "templatePath": "templates", - "templateOverridePath": "", - "defaultListTemplate": "listTemplate.htm", - "defaultFileTemplate": "articleTemplate.htm", - "useJSP": false, - "jspListTemplate": "listTemplate.jsp", - "jspFileTemplate": "articleTemplate.jsp", - "cachePackageTagsTrack": 200, - "cachePackageTagsStore": 200, - "cachePackageTagsRefresh": 60, - "cacheTemplatesTrack": 100, - "cacheTemplatesStore": 50, - "cacheTemplatesRefresh": 15, - "cachePagesTrack": 200, - "cachePagesStore": 100, - "cachePagesRefresh": 10, - "cachePagesDirtyRead": 10, - "searchEngineListTemplate": "forSearchEnginesList.htm", - "searchEngineFileTemplate": "forSearchEngines.htm", - "searchEngineRobotsDb": "WEB-INF/robots.db", - "useDataStore": true, - "dataStoreClass": "org.cofax.SqlDataStore", - "redirectionClass": "org.cofax.SqlRedirection", - "dataStoreName": "cofax", - "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", - "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", - "dataStoreUser": "sa", - "dataStorePassword": "dataStoreTestQuery", - "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", - "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", - "dataStoreInitConns": 10, - "dataStoreMaxConns": 100, - "dataStoreConnUsageLimit": 100, - "dataStoreLogLevel": "debug", - "maxUrlLength": 500}}, - { - "servlet-name": "cofaxEmail", - "servlet-class": "org.cofax.cds.EmailServlet", - "init-param": { - "mailHost": "mail1", - "mailHostOverride": "mail2"}}, - { - "servlet-name": "cofaxAdmin", - "servlet-class": "org.cofax.cds.AdminServlet"}, - - { - "servlet-name": "fileServlet", - "servlet-class": "org.cofax.cds.FileServlet"}, - { - "servlet-name": "cofaxTools", - "servlet-class": "org.cofax.cms.CofaxToolsServlet", - "init-param": { - "templatePath": "toolstemplates/", - "log": 1, - "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", - "logMaxSize": "", - "dataLog": 1, - "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", - "dataLogMaxSize": "", - "removePageCache": "/content/admin/remove?cache=pages&id=", - "removeTemplateCache": "/content/admin/remove?cache=templates&id=", - "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", - "lookInContext": 1, - "adminGroupID": 4, - "betaServer": true}}], - "servlet-mapping": { - "cofaxCDS": "/", - "cofaxEmail": "/cofaxutil/aemail/*", - "cofaxAdmin": "/admin/*", - "fileServlet": "/static/*", - "cofaxTools": "/tools/*"}, - - "taglib": { - "taglib-uri": "cofax.tld", - "taglib-location": "/WEB-INF/tlds/cofax.tld"}}} diff --git a/3rd/lua-cjson/tests/example5.json b/3rd/lua-cjson/tests/example5.json deleted file mode 100644 index 49980ca2..00000000 --- a/3rd/lua-cjson/tests/example5.json +++ /dev/null @@ -1,27 +0,0 @@ -{"menu": { - "header": "SVG Viewer", - "items": [ - {"id": "Open"}, - {"id": "OpenNew", "label": "Open New"}, - null, - {"id": "ZoomIn", "label": "Zoom In"}, - {"id": "ZoomOut", "label": "Zoom Out"}, - {"id": "OriginalView", "label": "Original View"}, - null, - {"id": "Quality"}, - {"id": "Pause"}, - {"id": "Mute"}, - null, - {"id": "Find", "label": "Find..."}, - {"id": "FindAgain", "label": "Find Again"}, - {"id": "Copy"}, - {"id": "CopyAgain", "label": "Copy Again"}, - {"id": "CopySVG", "label": "Copy SVG"}, - {"id": "ViewSVG", "label": "View SVG"}, - {"id": "ViewSource", "label": "View Source"}, - {"id": "SaveAs", "label": "Save As"}, - null, - {"id": "Help"}, - {"id": "About", "label": "About Adobe CVG Viewer..."} - ] -}} diff --git a/3rd/lua-cjson/tests/genutf8.pl b/3rd/lua-cjson/tests/genutf8.pl deleted file mode 100755 index db661a19..00000000 --- a/3rd/lua-cjson/tests/genutf8.pl +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env perl - -# Create test comparison data using a different UTF-8 implementation. - -# The generated utf8.dat file must have the following MD5 sum: -# cff03b039d850f370a7362f3313e5268 - -use strict; - -# 0xD800 - 0xDFFF are used to encode supplementary codepoints -# 0x10000 - 0x10FFFF are supplementary codepoints -my (@codepoints) = (0 .. 0xD7FF, 0xE000 .. 0x10FFFF); - -my $utf8 = pack("U*", @codepoints); -defined($utf8) or die "Unable create UTF-8 string\n"; - -open(FH, ">:utf8", "utf8.dat") - or die "Unable to open utf8.dat: $!\n"; -print FH $utf8 - or die "Unable to write utf8.dat\n"; -close(FH); - -# vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/numbers.json b/3rd/lua-cjson/tests/numbers.json deleted file mode 100644 index 4f981ff2..00000000 --- a/3rd/lua-cjson/tests/numbers.json +++ /dev/null @@ -1,7 +0,0 @@ -[ 0.110001, - 0.12345678910111, - 0.412454033640, - 2.6651441426902, - 2.718281828459, - 3.1415926535898, - 2.1406926327793 ] diff --git a/3rd/lua-cjson/tests/octets-escaped.dat b/3rd/lua-cjson/tests/octets-escaped.dat deleted file mode 100644 index ee99a6bf..00000000 --- a/3rd/lua-cjson/tests/octets-escaped.dat +++ /dev/null @@ -1 +0,0 @@ -"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ" \ No newline at end of file diff --git a/3rd/lua-cjson/tests/rfc-example1.json b/3rd/lua-cjson/tests/rfc-example1.json deleted file mode 100644 index 73532fa9..00000000 --- a/3rd/lua-cjson/tests/rfc-example1.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "Image": { - "Width": 800, - "Height": 600, - "Title": "View from 15th Floor", - "Thumbnail": { - "Url": "http://www.example.com/image/481989943", - "Height": 125, - "Width": "100" - }, - "IDs": [116, 943, 234, 38793] - } -} diff --git a/3rd/lua-cjson/tests/rfc-example2.json b/3rd/lua-cjson/tests/rfc-example2.json deleted file mode 100644 index 2a0cb681..00000000 --- a/3rd/lua-cjson/tests/rfc-example2.json +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - "precision": "zip", - "Latitude": 37.7668, - "Longitude": -122.3959, - "Address": "", - "City": "SAN FRANCISCO", - "State": "CA", - "Zip": "94107", - "Country": "US" - }, - { - "precision": "zip", - "Latitude": 37.371991, - "Longitude": -122.026020, - "Address": "", - "City": "SUNNYVALE", - "State": "CA", - "Zip": "94085", - "Country": "US" - } -] diff --git a/3rd/lua-cjson/tests/test.lua b/3rd/lua-cjson/tests/test.lua deleted file mode 100755 index c8f3c441..00000000 --- a/3rd/lua-cjson/tests/test.lua +++ /dev/null @@ -1,423 +0,0 @@ -#!/usr/bin/env lua - --- Lua CJSON tests --- --- Mark Pulford --- --- Note: The output of this script is easier to read with "less -S" - -local json = require "cjson" -local json_safe = require "cjson.safe" -local util = require "cjson.util" - -local function gen_raw_octets() - local chars = {} - for i = 0, 255 do chars[i + 1] = string.char(i) end - return table.concat(chars) -end - --- Generate every UTF-16 codepoint, including supplementary codes -local function gen_utf16_escaped() - -- Create raw table escapes - local utf16_escaped = {} - local count = 0 - - local function append_escape(code) - local esc = ('\\u%04X'):format(code) - table.insert(utf16_escaped, esc) - end - - table.insert(utf16_escaped, '"') - for i = 0, 0xD7FF do - append_escape(i) - end - -- Skip 0xD800 - 0xDFFF since they are used to encode supplementary - -- codepoints - for i = 0xE000, 0xFFFF do - append_escape(i) - end - -- Append surrogate pair for each supplementary codepoint - for high = 0xD800, 0xDBFF do - for low = 0xDC00, 0xDFFF do - append_escape(high) - append_escape(low) - end - end - table.insert(utf16_escaped, '"') - - return table.concat(utf16_escaped) -end - -function load_testdata() - local data = {} - - -- Data for 8bit raw <-> escaped octets tests - data.octets_raw = gen_raw_octets() - data.octets_escaped = util.file_load("octets-escaped.dat") - - -- Data for \uXXXX -> UTF-8 test - data.utf16_escaped = gen_utf16_escaped() - - -- Load matching data for utf16_escaped - local utf8_loaded - utf8_loaded, data.utf8_raw = pcall(util.file_load, "utf8.dat") - if not utf8_loaded then - data.utf8_raw = "Failed to load utf8.dat - please run genutf8.pl" - end - - data.table_cycle = {} - data.table_cycle[1] = data.table_cycle - - local big = {} - for i = 1, 1100 do - big = { { 10, false, true, json.null }, "string", a = big } - end - data.deeply_nested_data = big - - return data -end - -function test_decode_cycle(filename) - local obj1 = json.decode(util.file_load(filename)) - local obj2 = json.decode(json.encode(obj1)) - return util.compare_values(obj1, obj2) -end - --- Set up data used in tests -local Inf = math.huge; -local NaN = math.huge * 0; - -local testdata = load_testdata() - -local cjson_tests = { - -- Test API variables - { "Check module name, version", - function () return json._NAME, json._VERSION end, { }, - true, { "cjson", "2.1.0" } }, - - -- Test decoding simple types - { "Decode string", - json.decode, { '"test string"' }, true, { "test string" } }, - { "Decode numbers", - json.decode, { '[ 0.0, -5e3, -1, 0.3e-3, 1023.2, 0e10 ]' }, - true, { { 0.0, -5000, -1, 0.0003, 1023.2, 0 } } }, - { "Decode null", - json.decode, { 'null' }, true, { json.null } }, - { "Decode true", - json.decode, { 'true' }, true, { true } }, - { "Decode false", - json.decode, { 'false' }, true, { false } }, - { "Decode object with numeric keys", - json.decode, { '{ "1": "one", "3": "three" }' }, - true, { { ["1"] = "one", ["3"] = "three" } } }, - { "Decode object with string keys", - json.decode, { '{ "a": "a", "b": "b" }' }, - true, { { a = "a", b = "b" } } }, - { "Decode array", - json.decode, { '[ "one", null, "three" ]' }, - true, { { "one", json.null, "three" } } }, - - -- Test decoding errors - { "Decode UTF-16BE [throw error]", - json.decode, { '\0"\0"' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-16LE [throw error]", - json.decode, { '"\0"\0' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-32BE [throw error]", - json.decode, { '\0\0\0"' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-32LE [throw error]", - json.decode, { '"\0\0\0' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode partial JSON [throw error]", - json.decode, { '{ "unexpected eof": ' }, - false, { "Expected value but found T_END at character 21" } }, - { "Decode with extra comma [throw error]", - json.decode, { '{ "extra data": true }, false' }, - false, { "Expected the end but found T_COMMA at character 23" } }, - { "Decode invalid escape code [throw error]", - json.decode, { [[ { "bad escape \q code" } ]] }, - false, { "Expected object key string but found invalid escape code at character 16" } }, - { "Decode invalid unicode escape [throw error]", - json.decode, { [[ { "bad unicode \u0f6 escape" } ]] }, - false, { "Expected object key string but found invalid unicode escape code at character 17" } }, - { "Decode invalid keyword [throw error]", - json.decode, { ' [ "bad barewood", test ] ' }, - false, { "Expected value but found invalid token at character 20" } }, - { "Decode invalid number #1 [throw error]", - json.decode, { '[ -+12 ]' }, - false, { "Expected value but found invalid number at character 3" } }, - { "Decode invalid number #2 [throw error]", - json.decode, { '-v' }, - false, { "Expected value but found invalid number at character 1" } }, - { "Decode invalid number exponent [throw error]", - json.decode, { '[ 0.4eg10 ]' }, - false, { "Expected comma or array end but found invalid token at character 6" } }, - - -- Test decoding nested arrays / objects - { "Set decode_max_depth(5)", - json.decode_max_depth, { 5 }, true, { 5 } }, - { "Decode array at nested limit", - json.decode, { '[[[[[ "nested" ]]]]]' }, - true, { {{{{{ "nested" }}}}} } }, - { "Decode array over nested limit [throw error]", - json.decode, { '[[[[[[ "nested" ]]]]]]' }, - false, { "Found too many nested data structures (6) at character 6" } }, - { "Decode object at nested limit", - json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' }, - true, { {a={b={c={d={e="nested"}}}}} } }, - { "Decode object over nested limit [throw error]", - json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' }, - false, { "Found too many nested data structures (6) at character 26" } }, - { "Set decode_max_depth(1000)", - json.decode_max_depth, { 1000 }, true, { 1000 } }, - { "Decode deeply nested array [throw error]", - json.decode, { string.rep("[", 1100) .. '1100' .. string.rep("]", 1100)}, - false, { "Found too many nested data structures (1001) at character 1001" } }, - - -- Test encoding nested tables - { "Set encode_max_depth(5)", - json.encode_max_depth, { 5 }, true, { 5 } }, - { "Encode nested table as array at nested limit", - json.encode, { {{{{{"nested"}}}}} }, true, { '[[[[["nested"]]]]]' } }, - { "Encode nested table as array after nested limit [throw error]", - json.encode, { { {{{{{"nested"}}}}} } }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Encode nested table as object at nested limit", - json.encode, { {a={b={c={d={e="nested"}}}}} }, - true, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' } }, - { "Encode nested table as object over nested limit [throw error]", - json.encode, { {a={b={c={d={e={f="nested"}}}}}} }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Encode table with cycle [throw error]", - json.encode, { testdata.table_cycle }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Set encode_max_depth(1000)", - json.encode_max_depth, { 1000 }, true, { 1000 } }, - { "Encode deeply nested data [throw error]", - json.encode, { testdata.deeply_nested_data }, - false, { "Cannot serialise, excessive nesting (1001)" } }, - - -- Test encoding simple types - { "Encode null", - json.encode, { json.null }, true, { 'null' } }, - { "Encode true", - json.encode, { true }, true, { 'true' } }, - { "Encode false", - json.encode, { false }, true, { 'false' } }, - { "Encode empty object", - json.encode, { { } }, true, { '{}' } }, - { "Encode integer", - json.encode, { 10 }, true, { '10' } }, - { "Encode string", - json.encode, { "hello" }, true, { '"hello"' } }, - { "Encode Lua function [throw error]", - json.encode, { function () end }, - false, { "Cannot serialise function: type not supported" } }, - - -- Test decoding invalid numbers - { "Set decode_invalid_numbers(true)", - json.decode_invalid_numbers, { true }, true, { true } }, - { "Decode hexadecimal", - json.decode, { '0x6.ffp1' }, true, { 13.9921875 } }, - { "Decode numbers with leading zero", - json.decode, { '[ 0123, 00.33 ]' }, true, { { 123, 0.33 } } }, - { "Decode +-Inf", - json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } }, - { "Decode +-Infinity", - json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, - true, { { Inf, Inf, -Inf } } }, - { "Decode +-NaN", - json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } }, - { "Decode Infrared (not infinity) [throw error]", - json.decode, { 'Infrared' }, - false, { "Expected the end but found invalid token at character 4" } }, - { "Decode Noodle (not NaN) [throw error]", - json.decode, { 'Noodle' }, - false, { "Expected value but found invalid token at character 1" } }, - { "Set decode_invalid_numbers(false)", - json.decode_invalid_numbers, { false }, true, { false } }, - { "Decode hexadecimal [throw error]", - json.decode, { '0x6' }, - false, { "Expected value but found invalid number at character 1" } }, - { "Decode numbers with leading zero [throw error]", - json.decode, { '[ 0123, 00.33 ]' }, - false, { "Expected value but found invalid number at character 3" } }, - { "Decode +-Inf [throw error]", - json.decode, { '[ +Inf, Inf, -Inf ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { "Decode +-Infinity [throw error]", - json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { "Decode +-NaN [throw error]", - json.decode, { '[ +NaN, NaN, -NaN ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { 'Set decode_invalid_numbers("on")', - json.decode_invalid_numbers, { "on" }, true, { true } }, - - -- Test encoding invalid numbers - { "Set encode_invalid_numbers(false)", - json.encode_invalid_numbers, { false }, true, { false } }, - { "Encode NaN [throw error]", - json.encode, { NaN }, - false, { "Cannot serialise number: must not be NaN or Inf" } }, - { "Encode Infinity [throw error]", - json.encode, { Inf }, - false, { "Cannot serialise number: must not be NaN or Inf" } }, - { "Set encode_invalid_numbers(\"null\")", - json.encode_invalid_numbers, { "null" }, true, { "null" } }, - { "Encode NaN as null", - json.encode, { NaN }, true, { "null" } }, - { "Encode Infinity as null", - json.encode, { Inf }, true, { "null" } }, - { "Set encode_invalid_numbers(true)", - json.encode_invalid_numbers, { true }, true, { true } }, - { "Encode NaN", - json.encode, { NaN }, true, { "nan" } }, - { "Encode Infinity", - json.encode, { Inf }, true, { "inf" } }, - { 'Set encode_invalid_numbers("off")', - json.encode_invalid_numbers, { "off" }, true, { false } }, - - -- Test encoding tables - { "Set encode_sparse_array(true, 2, 3)", - json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } }, - { "Encode sparse table as array #1", - json.encode, { { [3] = "sparse test" } }, - true, { '[null,null,"sparse test"]' } }, - { "Encode sparse table as array #2", - json.encode, { { [1] = "one", [4] = "sparse test" } }, - true, { '["one",null,null,"sparse test"]' } }, - { "Encode sparse array as object", - json.encode, { { [1] = "one", [5] = "sparse test" } }, - true, { '{"1":"one","5":"sparse test"}' } }, - { "Encode table with numeric string key as object", - json.encode, { { ["2"] = "numeric string key test" } }, - true, { '{"2":"numeric string key test"}' } }, - { "Set encode_sparse_array(false)", - json.encode_sparse_array, { false }, true, { false, 2, 3 } }, - { "Encode table with incompatible key [throw error]", - json.encode, { { [false] = "wrong" } }, - false, { "Cannot serialise boolean: table key must be a number or string" } }, - - -- Test escaping - { "Encode all octets (8-bit clean)", - json.encode, { testdata.octets_raw }, true, { testdata.octets_escaped } }, - { "Decode all escaped octets", - json.decode, { testdata.octets_escaped }, true, { testdata.octets_raw } }, - { "Decode single UTF-16 escape", - json.decode, { [["\uF800"]] }, true, { "\239\160\128" } }, - { "Decode all UTF-16 escapes (including surrogate combinations)", - json.decode, { testdata.utf16_escaped }, true, { testdata.utf8_raw } }, - { "Decode swapped surrogate pair [throw error]", - json.decode, { [["\uDC00\uD800"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode duplicate high surrogate [throw error]", - json.decode, { [["\uDB00\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode duplicate low surrogate [throw error]", - json.decode, { [["\uDB00\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode missing low surrogate [throw error]", - json.decode, { [["\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode invalid low surrogate [throw error]", - json.decode, { [["\uDB00\uD"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - - -- Test locale support - -- - -- The standard Lua interpreter is ANSI C online doesn't support locales - -- by default. Force a known problematic locale to test strtod()/sprintf(). - { "Set locale to cs_CZ (comma separator)", function () - os.setlocale("cs_CZ") - json.new() - end }, - { "Encode number under comma locale", - json.encode, { 1.5 }, true, { '1.5' } }, - { "Decode number in array under comma locale", - json.decode, { '[ 10, "test" ]' }, true, { { 10, "test" } } }, - { "Revert locale to POSIX", function () - os.setlocale("C") - json.new() - end }, - - -- Test encode_keep_buffer() and enable_number_precision() - { "Set encode_keep_buffer(false)", - json.encode_keep_buffer, { false }, true, { false } }, - { "Set encode_number_precision(3)", - json.encode_number_precision, { 3 }, true, { 3 } }, - { "Encode number with precision 3", - json.encode, { 1/3 }, true, { "0.333" } }, - { "Set encode_number_precision(14)", - json.encode_number_precision, { 14 }, true, { 14 } }, - { "Set encode_keep_buffer(true)", - json.encode_keep_buffer, { true }, true, { true } }, - - -- Test config API errors - -- Function is listed as '?' due to pcall - { "Set encode_number_precision(0) [throw error]", - json.encode_number_precision, { 0 }, - false, { "bad argument #1 to '?' (expected integer between 1 and 14)" } }, - { "Set encode_number_precision(\"five\") [throw error]", - json.encode_number_precision, { "five" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, - { "Set encode_keep_buffer(nil, true) [throw error]", - json.encode_keep_buffer, { nil, true }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, - { "Set encode_max_depth(\"wrong\") [throw error]", - json.encode_max_depth, { "wrong" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, - { "Set decode_max_depth(0) [throw error]", - json.decode_max_depth, { "0" }, - false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } }, - { "Set encode_invalid_numbers(-2) [throw error]", - json.encode_invalid_numbers, { -2 }, - false, { "bad argument #1 to '?' (invalid option '-2')" } }, - { "Set decode_invalid_numbers(true, false) [throw error]", - json.decode_invalid_numbers, { true, false }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, - { "Set encode_sparse_array(\"not quite on\") [throw error]", - json.encode_sparse_array, { "not quite on" }, - false, { "bad argument #1 to '?' (invalid option 'not quite on')" } }, - - { "Reset Lua CJSON configuration", function () json = json.new() end }, - -- Wrap in a function to ensure the table returned by json.new() is used - { "Check encode_sparse_array()", - function (...) return json.encode_sparse_array(...) end, { }, - true, { false, 2, 10 } }, - - { "Encode (safe) simple value", - json_safe.encode, { true }, - true, { "true" } }, - { "Encode (safe) argument validation [throw error]", - json_safe.encode, { "arg1", "arg2" }, - false, { "bad argument #1 to '?' (expected 1 argument)" } }, - { "Decode (safe) error generation", - json_safe.decode, { "Oops" }, - true, { nil, "Expected value but found invalid token at character 1" } }, - { "Decode (safe) error generation after new()", - function(...) return json_safe.new().decode(...) end, { "Oops" }, - true, { nil, "Expected value but found invalid token at character 1" } }, -} - -print(("==> Testing Lua CJSON version %s\n"):format(json._VERSION)) - -util.run_test_group(cjson_tests) - -for _, filename in ipairs(arg) do - util.run_test("Decode cycle " .. filename, test_decode_cycle, { filename }, - true, { true }) -end - -local pass, total = util.run_test_summary() - -if pass == total then - print("==> Summary: all tests succeeded") -else - print(("==> Summary: %d/%d tests failed"):format(total - pass, total)) - os.exit(1) -end - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/types.json b/3rd/lua-cjson/tests/types.json deleted file mode 100644 index c01e7d20..00000000 --- a/3rd/lua-cjson/tests/types.json +++ /dev/null @@ -1 +0,0 @@ -{ "array": [ 10, true, null ] } diff --git a/3rd/lua-int64/Makefile b/3rd/lua-int64/Makefile deleted file mode 100644 index 82eacaa6..00000000 --- a/3rd/lua-int64/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -install : - gcc -g -Wall -fPIC --shared -o int64.so int64.c - -clean : - rm int64.so - diff --git a/3rd/lua-int64/README b/3rd/lua-int64/README deleted file mode 100644 index 7996553d..00000000 --- a/3rd/lua-int64/README +++ /dev/null @@ -1,2 +0,0 @@ -See https://github.com/cloudwu/lua-int64 -It will remove when Lua upgrade to 5.3 . diff --git a/3rd/lua-int64/int64.c b/3rd/lua-int64/int64.c deleted file mode 100644 index f261b21e..00000000 --- a/3rd/lua-int64/int64.c +++ /dev/null @@ -1,317 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static int64_t -_int64(lua_State *L, int index) { - int type = lua_type(L,index); - int64_t n = 0; - switch(type) { - case LUA_TNUMBER: { - lua_Number d = lua_tonumber(L,index); - n = (int64_t)d; - break; - } - case LUA_TSTRING: { - size_t len = 0; - const uint8_t * str = (const uint8_t *)lua_tolstring(L, index, &len); - if (len>8) { - return luaL_error(L, "The string (length = %d) is not an int64 string", len); - } - int i = 0; - uint64_t n64 = 0; - for (i=0;i<(int)len;i++) { - n64 |= (uint64_t)str[i] << (i*8); - } - n = (int64_t)n64; - break; - } - case LUA_TLIGHTUSERDATA: { - void * p = lua_touserdata(L,index); - n = (intptr_t)p; - break; - } - default: - return luaL_error(L, "argument %d error type %s", index, lua_typename(L,type)); - } - return n; -} - -static inline void -_pushint64(lua_State *L, int64_t n) { - void * p = (void *)(intptr_t)n; - lua_pushlightuserdata(L,p); -} - -static int -int64_add(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a+b); - - return 1; -} - -static int -int64_sub(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a-b); - - return 1; -} - -static int -int64_mul(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a * b); - - return 1; -} - -static int -int64_div(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - if (b == 0) { - return luaL_error(L, "div by zero"); - } - _pushint64(L, a / b); - - return 1; -} - -static int -int64_mod(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - if (b == 0) { - return luaL_error(L, "mod by zero"); - } - _pushint64(L, a % b); - - return 1; -} - -static int64_t -_pow64(int64_t a, int64_t b) { - if (b == 1) { - return a; - } - int64_t a2 = a * a; - if (b % 2 == 1) { - return _pow64(a2, b/2) * a; - } else { - return _pow64(a2, b/2); - } -} - -static int -int64_pow(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - int64_t p; - if (b > 0) { - p = _pow64(a,b); - } else if (b == 0) { - p = 1; - } else { - return luaL_error(L, "pow by nagtive number %d",(int)b); - } - _pushint64(L, p); - - return 1; -} - -static int -int64_unm(lua_State *L) { - int64_t a = _int64(L,1); - _pushint64(L, -a); - return 1; -} - -static int -int64_new(lua_State *L) { - int top = lua_gettop(L); - int64_t n; - switch(top) { - case 0 : - lua_pushlightuserdata(L,NULL); - break; - case 1 : - n = _int64(L,1); - _pushint64(L,n); - break; - default: { - int base = luaL_checkinteger(L,2); - if (base < 2) { - luaL_error(L, "base must be >= 2"); - } - const char * str = luaL_checkstring(L, 1); - n = strtoll(str, NULL, base); - _pushint64(L,n); - break; - } - } - return 1; -} - -static int -int64_eq(lua_State *L) { - // __eq metamethod can't be invoke by lightuserdata - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a == b); - return 1; -} - -static int -int64_lt(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a < b); - return 1; -} - -static int -int64_le(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a <= b); - return 1; -} - -static int -int64_len(lua_State *L) { - int64_t a = _int64(L,1); - lua_pushnumber(L,(lua_Number)a); - return 1; -} - -static int -tostring(lua_State *L) { - static char hex[16] = "0123456789ABCDEF"; - uintptr_t n = (uintptr_t)lua_touserdata(L,1); - if (lua_gettop(L) == 1) { - luaL_Buffer b; - luaL_buffinit(L , &b); - luaL_addstring(&b, "int64: 0x"); - int i; - bool strip = true; - for (i=15;i>=0;i--) { - int c = (n >> (i*4)) & 0xf; - if (strip && c ==0) { - continue; - } - strip = false; - luaL_addchar(&b, hex[c]); - } - if (strip) { - luaL_addchar(&b , '0'); - } - luaL_pushresult(&b); - } else { - int base = luaL_checkinteger(L,2); - int shift =0, mask =0; - switch(base) { - case 0: { - unsigned char buffer[8]; - int i; - for (i=0;i<8;i++) { - buffer[i] = (n >> (i*8)) & 0xff; - } - lua_pushlstring(L,(const char *)buffer, 8); - return 1; - } - case 10: { - int64_t dec = (int64_t)n; - luaL_Buffer b; - luaL_buffinit(L , &b); - if (dec<0) { - luaL_addchar(&b, '-'); - dec = -dec; - } - int buffer[32]; - int i; - for (i=0;i<32;i++) { - buffer[i] = dec%10; - dec /= 10; - if (dec == 0) - break; - } - while (i>=0) { - luaL_addchar(&b, hex[buffer[i]]); - --i; - } - luaL_pushresult(&b); - return 1; - } - case 2: - shift = 1; - mask = 1; - break; - case 8: - shift = 3; - mask = 7; - break; - case 16: - shift = 4; - mask = 0xf; - break; - default: - luaL_error(L, "Unsupport base %d",base); - break; - } - int i; - char buffer[64]; - for (i=0;i<64;i+=shift) { - buffer[i/shift] = hex[(n>>(64-shift-i)) & mask]; - } - lua_pushlstring(L, buffer, 64 / shift); - } - return 1; -} - -static void -make_mt(lua_State *L) { - luaL_Reg lib[] = { - { "__add", int64_add }, - { "__sub", int64_sub }, - { "__mul", int64_mul }, - { "__div", int64_div }, - { "__mod", int64_mod }, - { "__unm", int64_unm }, - { "__pow", int64_pow }, - { "__eq", int64_eq }, - { "__lt", int64_lt }, - { "__le", int64_le }, - { "__len", int64_len }, - { "__tostring", tostring }, - { NULL, NULL }, - }; - luaL_newlib(L,lib); -} - -int -luaopen_int64(lua_State *L) { - if (sizeof(intptr_t)!=sizeof(int64_t)) { - return luaL_error(L, "Only support 64bit architecture"); - } - lua_pushlightuserdata(L,NULL); - make_mt(L); - lua_setmetatable(L,-2); - lua_pop(L,1); - - lua_newtable(L); - lua_pushcfunction(L, int64_new); - lua_setfield(L, -2, "new"); - lua_pushcfunction(L, tostring); - lua_setfield(L, -2, "tostring"); - - return 1; -} - diff --git a/3rd/lua-int64/test.lua b/3rd/lua-int64/test.lua deleted file mode 100644 index 8aedf561..00000000 --- a/3rd/lua-int64/test.lua +++ /dev/null @@ -1,17 +0,0 @@ -lib = require "int64" - -local int64 = lib.new -print(lib.tostring(int64 "\1\2\3\4\5\6\7\8")) - -a = 1 + int64(1) -b = int64 "\16" + int64("9",10) -print(lib.tostring(a,10), lib.tostring(b,2)) -print("+", a+b) -print("-", lib.tostring(a-b,10)) -print("*", a*b) -print("/", a/b) -print("%", a%b) -print("^", a^b) -print("==", a == b) -print(">", a > b) -print("#", #a) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 7b4b2b75..0b7c8781 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -6,8 +6,8 @@ # Your platform. See PLATS for possible values. PLAT= none -CC= gcc -CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) +CC= gcc -std=gnu99 +CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) @@ -19,21 +19,21 @@ SYSCFLAGS= SYSLDFLAGS= SYSLIBS= -MYCFLAGS= +MYCFLAGS=-I../../skynet-src -g MYLDFLAGS= MYLIBS= MYOBJS= # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris +PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris LUA_A= liblua.a CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ ltm.o lundump.o lvm.o lzio.o LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \ - lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o + lmathlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o loadlib.o linit.o BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) LUA_T= lua @@ -91,12 +91,16 @@ none: aix: $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" -ansi: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI" - bsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" +c89: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" + @echo '' + @echo '*** C89 does not guarantee 64-bit integers for Lua.' + @echo '' + + freebsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" @@ -109,7 +113,7 @@ macosx: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc mingw: - $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ + $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \ "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe $(MAKE) "LUAC_T=luac.exe" luac.exe @@ -118,70 +122,76 @@ posix: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" solaris: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" # list targets that do not create files (but not all makes understand .PHONY) .PHONY: all $(PLATS) default o a clean depend echo none # DO NOT DELETE -lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ - lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \ +lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ + ltable.h lundump.h lvm.h +lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h +lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lgc.h lstring.h ltable.h lvm.h +lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h +ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ + ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h +ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lparser.h lstring.h ltable.h lundump.h lvm.h +ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ + ltm.h lzio.h lmem.h lundump.h +lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \ + lgc.h lstate.h ltm.h lzio.h lmem.h +lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h +linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h +liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ + lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ + lstring.h ltable.h +lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h +loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ + ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ lvm.h -lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h -lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h -lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h -lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ - lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ - lstring.h ltable.h lvm.h -lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h -lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h -ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h -ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ - ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \ - lfunc.h lstring.h lgc.h ltable.h lvm.h -ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ - lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \ - lstring.h ltable.h lundump.h lvm.h -ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ - lzio.h lmem.h lundump.h -lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \ - lstate.h ltm.h lzio.h lmem.h -lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ - lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h -linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h -liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h -llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \ - lstate.h ltm.h lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h -lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h -lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ - ltm.h lzio.h lmem.h ldo.h lgc.h -loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h -lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h -lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h -loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h -lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ - lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \ - lstring.h lgc.h ltable.h -lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ - ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \ - ltable.h -lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ - ltm.h lzio.h lstring.h lgc.h -lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h -ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ - ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h -ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ - lmem.h lstring.h lgc.h ltable.h -lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h -luac.o: luac.c lua.h luaconf.h lauxlib.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h -lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h -lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ - lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h -lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ - lzio.h +lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h +loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lfunc.h lstring.h lgc.h ltable.h +lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ + lstring.h ltable.h +lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h +lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h +ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h +lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h lobject.h llimits.h \ + lstate.h ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h +lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ + lundump.h +lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ + ltable.h lvm.h +lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ + lobject.h ltm.h lzio.h +# (end of Makefile) diff --git a/3rd/lua/README b/3rd/lua/README index 9e1aea2f..de361c84 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,3 +1,6 @@ -This is a modify version of lua 5.2.3 (http://www.lua.org/ftp/lua-5.2.3.tar.gz) . +This is a modify version of lua 5.3.2 (http://www.lua.org/ftp/lua-5.3.2.tar.gz) . -For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html +For detail , + Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html + Shared short string table : http://blog.codingnow.com/2015/08/lua_vm_share_string.html + Signal for debug use : http://blog.codingnow.com/2015/03/skynet_signal.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index a4fce7f2..d8ee0386 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,16 +1,18 @@ /* -** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ +#define lapi_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define lapi_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -26,6 +28,7 @@ #include "ltm.h" #include "lundump.h" #include "lvm.h" +#include "lfunc.h" @@ -43,13 +46,16 @@ const char lua_ident[] = /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) +/* test for upvalue */ +#define isupvalue(i) ((i) < LUA_REGISTRYINDEX) + /* test for valid but not pseudo index */ #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) -#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index") +#define api_checkvalidindex(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") +#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) { @@ -89,21 +95,22 @@ static void growstack (lua_State *L, void *ud) { } -LUA_API int lua_checkstack (lua_State *L, int size) { +LUA_API int lua_checkstack (lua_State *L, int n) { int res; CallInfo *ci = L->ci; lua_lock(L); - if (L->stack_last - L->top > size) /* stack large enough? */ + api_check(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 - size) /* can grow without overflow? */ + if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ res = 0; /* no */ else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK); + res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); } - if (res && ci->top < L->top + size) - ci->top = L->top + size; /* adjust frame top */ + if (res && ci->top < L->top + n) + ci->top = L->top + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -115,10 +122,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { lua_lock(to); api_checknelems(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); + api_check(from, to->ci->top - to->top >= n, "stack overflow"); from->top -= n; for (i = 0; i < n; i++) { - setobj2s(to, to->top++, from->top + i); + setobj2s(to, to->top, from->top + i); + to->top++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } @@ -153,7 +161,7 @@ LUA_API const lua_Number *lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->ci->func + idx); + : cast_int(L->top - L->ci->func) + idx; } @@ -173,61 +181,56 @@ LUA_API void lua_settop (lua_State *L, int idx) { } else { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* `subtract' index (index is negative) */ + L->top += idx+1; /* 'subtract' index (index is negative) */ } lua_unlock(L); } -LUA_API void lua_remove (lua_State *L, int idx) { - StkId p; +/* +** Reverse the stack segment from 'from' to 'to' +** (auxiliary to 'lua_rotate') +*/ +static void reverse (lua_State *L, StkId from, StkId to) { + for (; from < to; from++, to--) { + TValue temp; + setobj(L, &temp, from); + setobjs2s(L, from, to); + setobj2s(L, to, &temp); + } +} + + +/* +** Let x = AB, where A is a prefix of length 'n'. Then, +** rotate x n == BA. But BA == (A^r . B^r)^r. +*/ +LUA_API void lua_rotate (lua_State *L, int idx, int n) { + StkId p, t, m; lua_lock(L); - p = index2addr(L, idx); + t = L->top - 1; /* end of stack segment being rotated */ + p = index2addr(L, idx); /* start of segment */ api_checkstackindex(L, idx, p); - while (++p < L->top) setobjs2s(L, p-1, p); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_insert (lua_State *L, int idx) { - StkId p; - StkId q; - lua_lock(L); - p = index2addr(L, idx); - api_checkstackindex(L, idx, p); - for (q = L->top; q > p; q--) /* use L->top as a temporary */ - setobjs2s(L, q, q - 1); - setobjs2s(L, p, L->top); - lua_unlock(L); -} - - -static void moveto (lua_State *L, TValue *fr, int idx) { - TValue *to = index2addr(L, idx); - api_checkvalidindex(L, to); - setobj(L, to, fr); - if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); - /* LUA_REGISTRYINDEX does not need gc barrier - (collector revisits it before finishing collection) */ -} - - -LUA_API void lua_replace (lua_State *L, int idx) { - lua_lock(L); - api_checknelems(L, 1); - moveto(L, L->top - 1, idx); - L->top--; + api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); + m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ + reverse(L, p, m); /* reverse the prefix with length 'n' */ + reverse(L, m + 1, t); /* reverse the suffix */ + reverse(L, p, t); /* reverse the entire segment */ lua_unlock(L); } LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr; + TValue *fr, *to; lua_lock(L); fr = index2addr(L, fromidx); - moveto(L, fr, toidx); + to = index2addr(L, toidx); + api_checkvalidindex(L, to); + setobj(L, to, fr); + if (isupvalue(toidx)) /* function upvalue? */ + luaC_barrier(L, clCvalue(L->ci->func), fr); + /* LUA_REGISTRYINDEX does not need gc barrier + (collector revisits it before finishing collection) */ lua_unlock(L); } @@ -248,12 +251,13 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { LUA_API int lua_type (lua_State *L, int idx) { StkId o = index2addr(L, idx); - return (isvalid(o) ? ttypenv(o) : LUA_TNONE); + return (isvalid(o) ? ttnov(o) : LUA_TNONE); } LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); + api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); return ttypename(t); } @@ -264,22 +268,28 @@ LUA_API int lua_iscfunction (lua_State *L, int idx) { } +LUA_API int lua_isinteger (lua_State *L, int idx) { + StkId o = index2addr(L, idx); + return ttisinteger(o); +} + + LUA_API int lua_isnumber (lua_State *L, int idx) { - TValue n; + lua_Number n; const TValue *o = index2addr(L, idx); return tonumber(o, &n); } LUA_API int lua_isstring (lua_State *L, int idx) { - int t = lua_type(L, idx); - return (t == LUA_TSTRING || t == LUA_TNUMBER); + const TValue *o = index2addr(L, idx); + return (ttisstring(o) || cvt2str(o)); } LUA_API int lua_isuserdata (lua_State *L, int idx) { const TValue *o = index2addr(L, idx); - return (ttisuserdata(o) || ttislightuserdata(o)); + return (ttisfulluserdata(o) || ttislightuserdata(o)); } @@ -291,24 +301,17 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { LUA_API void lua_arith (lua_State *L, int op) { - StkId o1; /* 1st operand */ - StkId o2; /* 2nd operand */ lua_lock(L); - if (op != LUA_OPUNM) /* all other operations expect two operands */ - api_checknelems(L, 2); - else { /* for unary minus, add fake 2nd operand */ + if (op != LUA_OPUNM && op != LUA_OPBNOT) + api_checknelems(L, 2); /* all other operations expect two operands */ + else { /* for unary operations, add fake 2nd operand */ api_checknelems(L, 1); setobjs2s(L, L->top, L->top - 1); - L->top++; + api_incr_top(L); } - o1 = L->top - 2; - o2 = L->top - 1; - if (ttisnumber(o1) && ttisnumber(o2)) { - setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2))); - } - else - luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); - L->top--; + /* first operand at top - 2, second at top - 1; result go to top - 2 */ + luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); + L->top--; /* remove second operand */ lua_unlock(L); } @@ -321,7 +324,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { o2 = index2addr(L, index2); if (isvalid(o1) && isvalid(o2)) { switch (op) { - case LUA_OPEQ: i = equalobj(L, o1, o2); break; + case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; default: api_check(L, 0, "invalid option"); @@ -332,51 +335,33 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { - TValue n; - const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - if (isnum) *isnum = 1; - return nvalue(o); - } - else { - if (isnum) *isnum = 0; - return 0; - } +LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { + size_t sz = luaO_str2num(s, L->top); + if (sz != 0) + api_incr_top(L); + return sz; } -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { - TValue n; +LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { + lua_Number n; const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Integer res; - lua_Number num = nvalue(o); - lua_number2integer(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } + int isnum = tonumber(o, &n); + if (!isnum) + n = 0; /* call to 'tonumber' may change 'n' even if it fails */ + if (pisnum) *pisnum = isnum; + return n; } -LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { - TValue n; +LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { + lua_Integer res; const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Unsigned res; - lua_Number num = nvalue(o); - lua_number2unsigned(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } + int isnum = tointeger(o, &res); + if (!isnum) + res = 0; /* call to 'tointeger' may change 'n' even if it fails */ + if (pisnum) *pisnum = isnum; + return res; } @@ -389,25 +374,27 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { StkId o = index2addr(L, idx); if (!ttisstring(o)) { - lua_lock(L); /* `luaV_tostring' may create a new string */ - if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; - lua_unlock(L); return NULL; } + lua_lock(L); /* 'luaO_tostring' may create a new string */ + luaO_tostring(L, o); luaC_checkGC(L); o = index2addr(L, idx); /* previous call may reallocate the stack */ lua_unlock(L); } - if (len != NULL) *len = tsvalue(o)->len; + if (len != NULL) + *len = vslen(o); return svalue(o); } LUA_API size_t lua_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); - switch (ttypenv(o)) { - case LUA_TSTRING: return tsvalue(o)->len; + switch (ttype(o)) { + case LUA_TSHRSTR: return tsvalue(o)->shrlen; + case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; case LUA_TUSERDATA: return uvalue(o)->len; case LUA_TTABLE: return luaH_getn(hvalue(o)); default: return 0; @@ -426,8 +413,8 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { LUA_API void *lua_touserdata (lua_State *L, int idx) { StkId o = index2addr(L, idx); - switch (ttypenv(o)) { - case LUA_TUSERDATA: return (rawuvalue(o) + 1); + switch (ttnov(o)) { + case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } @@ -448,9 +435,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { case LUA_TCCL: return clCvalue(o); case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - return lua_touserdata(L, idx); + case LUA_TUSERDATA: return getudatamem(uvalue(o)); + case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } } @@ -472,9 +458,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setnvalue(L->top, n); - luai_checknum(L, L->top, - luaG_runerror(L, "C API - attempt to push a signaling NaN")); + setfltvalue(L->top, n); api_incr_top(L); lua_unlock(L); } @@ -482,49 +466,43 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); - setnvalue(L->top, cast_num(n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { - lua_Number n; - lua_lock(L); - n = lua_unsigned2number(u); - setnvalue(L->top, n); + setivalue(L->top, n); api_incr_top(L); lua_unlock(L); } +/* +** Pushes on the stack a string with given length. Avoid using 's' when +** 'len' == 0 (as 's' can be NULL in that case), due to later use of +** 'memcmp' and 'memcpy'. +*/ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); - luaC_checkGC(L); - ts = luaS_newlstr(L, s, len); + ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top, ts); api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); return getstr(ts); } LUA_API const char *lua_pushstring (lua_State *L, const char *s) { - if (s == NULL) { - lua_pushnil(L); - return NULL; - } + lua_lock(L); + if (s == NULL) + setnilvalue(L->top); else { TString *ts; - lua_lock(L); - luaC_checkGC(L); ts = luaS_new(L, s); setsvalue2s(L, L->top, ts); - api_incr_top(L); - lua_unlock(L); - return getstr(ts); + s = getstr(ts); /* internal copy's address */ } + api_incr_top(L); + luaC_checkGC(L); + lua_unlock(L); + return s; } @@ -532,8 +510,8 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *ret; lua_lock(L); - luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, argp); + luaC_checkGC(L); lua_unlock(L); return ret; } @@ -543,10 +521,10 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); - luaC_checkGC(L); va_start(argp, fmt); ret = luaO_pushvfstring(L, fmt, argp); va_end(argp); + luaC_checkGC(L); lua_unlock(L); return ret; } @@ -558,18 +536,20 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { setfvalue(L->top, fn); } else { - Closure *cl; + CClosure *cl; api_checknelems(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); - luaC_checkGC(L); cl = luaF_newCclosure(L, n); - cl->c.f = fn; + cl->f = fn; L->top -= n; - while (n--) - setobj2n(L, &cl->c.upvalue[n], L->top + n); + while (n--) { + setobj2n(L, &cl->upvalue[n], L->top + n); + /* does not need barrier because closure is white */ + } setclCvalue(L, L->top, cl); } api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); } @@ -605,48 +585,77 @@ LUA_API int lua_pushthread (lua_State *L) { */ -LUA_API void lua_getglobal (lua_State *L, const char *var) { +static 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); + api_incr_top(L); + } + else { + setsvalue2s(L, L->top, str); + api_incr_top(L); + luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + } + lua_unlock(L); + return ttnov(L->top - 1); +} + + +LUA_API int lua_getglobal (lua_State *L, const char *name) { Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ lua_lock(L); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); - luaV_gettable(L, gt, L->top - 1, L->top - 1); - lua_unlock(L); + return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } -LUA_API void lua_gettable (lua_State *L, int idx) { +LUA_API int lua_gettable (lua_State *L, int idx) { StkId t; lua_lock(L); t = index2addr(L, idx); luaV_gettable(L, t, L->top - 1, L->top - 1); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { +LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { + lua_lock(L); + return auxgetstr(L, index2addr(L, idx), k); +} + + +LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { StkId t; + const TValue *slot; lua_lock(L); t = index2addr(L, idx); - setsvalue2s(L, L->top, luaS_new(L, k)); - api_incr_top(L); - luaV_gettable(L, t, L->top - 1, L->top - 1); + if (luaV_fastget(L, t, n, slot, luaH_getint)) { + setobj2s(L, L->top, slot); + api_incr_top(L); + } + else { + setivalue(L->top, n); + api_incr_top(L); + luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + } lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_rawget (lua_State *L, int idx) { +LUA_API int lua_rawget (lua_State *L, int idx) { StkId t; 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); } -LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { +LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); t = index2addr(L, idx); @@ -654,10 +663,11 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { setobj2s(L, L->top, luaH_getint(hvalue(t), n)); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { +LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { StkId t; TValue k; lua_lock(L); @@ -667,29 +677,30 @@ LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); - luaC_checkGC(L); t = luaH_new(L); sethvalue(L, L->top, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); + luaC_checkGC(L); lua_unlock(L); } LUA_API int lua_getmetatable (lua_State *L, int objindex) { const TValue *obj; - Table *mt = NULL; - int res; + Table *mt; + int res = 0; lua_lock(L); obj = index2addr(L, objindex); - switch (ttypenv(obj)) { + switch (ttnov(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; @@ -697,12 +708,10 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { mt = uvalue(obj)->metatable; break; default: - mt = G(L)->mt[ttypenv(obj)]; + mt = G(L)->mt[ttnov(obj)]; break; } - if (mt == NULL) - res = 0; - else { + if (mt != NULL) { sethvalue(L, L->top, mt); api_incr_top(L); res = 1; @@ -712,17 +721,15 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { } -LUA_API void lua_getuservalue (lua_State *L, int idx) { +LUA_API int lua_getuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (uvalue(o)->env) { - sethvalue(L, L->top, uvalue(o)->env); - } else - setnilvalue(L->top); + api_check(L, ttisfulluserdata(o), "full userdata expected"); + getuservalue(L, uvalue(o), L->top); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } @@ -730,17 +737,29 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) { ** set functions (stack -> Lua) */ - -LUA_API void lua_setglobal (lua_State *L, const char *var) { - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ - lua_lock(L); +/* +** t[k] = value at the top of the stack (where 'k' is a string) +*/ +static void auxsetstr (lua_State *L, const TValue *t, const char *k) { + const TValue *slot; + TString *str = luaS_new(L, k); api_checknelems(L, 1); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); - luaV_settable(L, gt, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ - lua_unlock(L); + if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) + L->top--; /* pop value */ + else { + setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ + api_incr_top(L); + luaV_finishset(L, t, L->top - 1, L->top - 2, slot); + L->top -= 2; /* pop value and key */ + } + lua_unlock(L); /* lock done by caller */ +} + + +LUA_API void lua_setglobal (lua_State *L, const char *name) { + Table *reg = hvalue(&G(L)->l_registry); + lua_lock(L); /* unlock done in 'auxsetstr' */ + auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } @@ -756,54 +775,69 @@ 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); +} + + +LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { StkId t; + const TValue *slot; lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - setsvalue2s(L, L->top++, luaS_new(L, k)); - luaV_settable(L, t, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ + if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) + L->top--; /* pop value */ + else { + setivalue(L->top, n); + api_incr_top(L); + luaV_finishset(L, t, L->top - 1, L->top - 2, slot); + L->top -= 2; /* pop value and key */ + } lua_unlock(L); } LUA_API void lua_rawset (lua_State *L, int idx) { - StkId t; + StkId o; + TValue *slot; lua_lock(L); api_checknelems(L, 2); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); - invalidateTMcache(hvalue(t)); - luaC_barrierback(L, gcvalue(t), L->top-1); + 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; lua_unlock(L); } -LUA_API void lua_rawseti (lua_State *L, int idx, int n) { - StkId t; +LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { + StkId o; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - luaH_setint(L, hvalue(t), n, L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top-1); + o = index2addr(L, idx); + api_check(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_rawsetp (lua_State *L, int idx, const void *p) { - StkId t; - TValue k; + StkId o; + TValue k, *slot; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + o = index2addr(L, idx); + api_check(L, ttistable(o), "table expected"); setpvalue(&k, cast(void *, p)); - setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top - 1); + slot = luaH_set(L, hvalue(o), &k); + setobj2t(L, slot, L->top - 1); + luaC_barrierback(L, hvalue(o), L->top - 1); L->top--; lua_unlock(L); } @@ -821,11 +855,11 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { api_check(L, ttistable(L->top - 1), "table expected"); mt = hvalue(L->top - 1); } - switch (ttypenv(obj)) { + switch (ttnov(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = mt; if (mt) { - luaC_objbarrierback(L, gcvalue(obj), mt); + luaC_objbarrier(L, gcvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; @@ -833,13 +867,13 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { case LUA_TUSERDATA: { uvalue(obj)->metatable = mt; if (mt) { - luaC_objbarrier(L, rawuvalue(obj), mt); + luaC_objbarrier(L, uvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } default: { - G(L)->mt[ttypenv(obj)] = mt; + G(L)->mt[ttnov(obj)] = mt; break; } } @@ -854,21 +888,16 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (ttisnil(L->top - 1)) - uvalue(o)->env = NULL; - else { - api_check(L, ttistable(L->top - 1), "table expected"); - uvalue(o)->env = hvalue(L->top - 1); - luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - } + api_check(L, ttisfulluserdata(o), "full userdata expected"); + setuservalue(L, uvalue(o), L->top - 1); + luaC_barrier(L, gcvalue(o), L->top - 1); L->top--; lua_unlock(L); } /* -** `load' and `call' functions (run Lua code) +** 'load' and 'call' functions (run Lua code) */ @@ -877,17 +906,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { "results from function overflow current stack size") -LUA_API int lua_getctx (lua_State *L, int *ctx) { - if (L->ci->callstatus & CIST_YIELDED) { - if (ctx) *ctx = L->ci->u.c.ctx; - return L->ci->u.c.status; - } - else return LUA_OK; -} - - -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k) { +LUA_API void lua_callk (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), @@ -899,10 +919,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ - luaD_call(L, func, nresults, 1); /* do the call */ + luaD_call(L, func, nresults); /* do the call */ } else /* no continuation or no yieldable */ - luaD_call(L, func, nresults, 0); /* just do the call */ + luaD_callnoyield(L, func, nresults); /* just do the call */ adjustresults(L, nresults); lua_unlock(L); } @@ -912,7 +932,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, /* ** Execute a protected call. */ -struct CallS { /* data to `f_call' */ +struct CallS { /* data to 'f_call' */ StkId func; int nresults; }; @@ -920,13 +940,13 @@ struct CallS { /* data to `f_call' */ static void f_call (lua_State *L, void *ud) { struct CallS *c = cast(struct CallS *, ud); - luaD_call(L, c->func, c->nresults, 0); + luaD_callnoyield(L, c->func, c->nresults); } LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k) { + lua_KContext ctx, lua_KFunction k) { struct CallS c; int status; ptrdiff_t func; @@ -954,12 +974,11 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ ci->extra = savestack(L, c.func); - ci->u.c.old_allowhook = L->allowhook; ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; - /* mark that function may do error recovery */ - ci->callstatus |= CIST_YPCALL; - luaD_call(L, c.func, nresults, 1); /* do the call */ + setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ + ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ + luaD_call(L, c.func, nresults); /* do the call */ ci->callstatus &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ @@ -980,24 +999,22 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ LClosure *f = clLvalue(L->top - 1); /* get newly created function */ - if (f->nupvalues == 1) { /* does it have one upvalue? */ + if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); + luaC_upvalbarrier(L, f->upvals[0]); } } lua_unlock(L); return status; } -static Proto * -cloneproto (lua_State *L, const Proto *src) { +static void cloneproto (lua_State *L, Proto *f, const Proto *src) { /* copy constants and nested proto */ int i,n; - Proto *f = luaF_newproto (L, src->sp); n = src->sp->sizek; f->k=luaM_newvector(L,n,TValue); for (i=0; ik[i]); @@ -1005,7 +1022,7 @@ cloneproto (lua_State *L, const Proto *src) { const TValue *s=&src->k[i]; TValue *o=&f->k[i]; if (ttisstring(s)) { - TString * str = luaS_newlstr(L,svalue(s),tsvalue(s)->len); + TString * str = luaS_clonestring(L,tsvalue(s)); setsvalue2n(L,o,str); } else { setobj(L,o,s); @@ -1015,15 +1032,14 @@ cloneproto (lua_State *L, const Proto *src) { f->p=luaM_newvector(L,n,struct Proto *); for (i=0; ip[i]=NULL; for (i=0; ip[i]=cloneproto(L, src->p[i]); + f->p[i]= luaF_newproto(L, src->p[i]->sp); + cloneproto(L, f->p[i], src->p[i]); } - return f; } LUA_API void lua_clonefunction (lua_State *L, const void * fp) { - int i; - Closure *cl; - const LClosure *f = cast(const LClosure *, fp); + LClosure *cl; + LClosure *f = cast(LClosure *, fp); lua_lock(L); if (f->p->sp->l_G == G(L)) { setclLvalue(L,L->top,f); @@ -1032,33 +1048,31 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { return; } cl = luaF_newLclosure(L,f->nupvalues); - cl->l.p = cloneproto(L, f->p); setclLvalue(L,L->top,cl); api_incr_top(L); - for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ - UpVal *up = luaF_newupval(L); - cl->l.upvals[i] = up; - luaC_objbarrier(L, cl, up); - } - if (f->nupvalues == 1) { /* does it have one upvalue? */ + cl->p = luaF_newproto(L, f->p->sp); + cloneproto(L, cl->p, f->p); + luaF_initupvals(L, cl); + + if (cl->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'cl' (may be LUA_ENV) */ - setobj(L, cl->l.upvals[0]->v, gt); - luaC_barrier(L, cl->l.upvals[0], gt); + /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ + setobj(L, cl->upvals[0]->v, gt); + luaC_upvalbarrier(L, cl->upvals[0]); } lua_unlock(L); } -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; lua_lock(L); api_checknelems(L, 1); o = L->top - 1; if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, 0); + status = luaU_dump(L, getproto(o), writer, data, strip); else status = 1; lua_unlock(L); @@ -1104,19 +1118,21 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { - if (g->gckind == KGC_GEN) { /* generational mode? */ - res = (g->GCestimate == 0); /* true if it will do major collection */ - luaC_forcestep(L); /* do a single step */ + l_mem debt = 1; /* =1 to signal that it did an actual step */ + lu_byte oldrunning = g->gcrunning; + g->gcrunning = 1; /* allow GC to run */ + if (data == 0) { + luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ + luaC_step(L); } - else { - lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE; - if (g->gcrunning) - debt += g->GCdebt; /* include current debt */ - luaE_setdebt(g, debt); - luaC_forcestep(L); - if (g->gcstate == GCSpause) /* end of cycle? */ - res = 1; /* signal it */ + else { /* add 'data' to total debt */ + debt = cast(l_mem, data) * 1024 + g->GCdebt; + luaE_setdebt(g, debt); + luaC_checkGC(L); } + g->gcrunning = oldrunning; /* restore previous state */ + if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ + res = 1; /* signal it */ break; } case LUA_GCSETPAUSE: { @@ -1124,13 +1140,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { g->gcpause = data; break; } - case LUA_GCSETMAJORINC: { - res = g->gcmajorinc; - g->gcmajorinc = data; - break; - } case LUA_GCSETSTEPMUL: { res = g->gcstepmul; + if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ g->gcstepmul = data; break; } @@ -1138,14 +1150,6 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { res = g->gcrunning; break; } - case LUA_GCGEN: { /* change collector to generational mode */ - luaC_changemode(L, KGC_GEN); - break; - } - case LUA_GCINC: { /* change collector to incremental mode */ - luaC_changemode(L, KGC_NORMAL); - break; - } default: res = -1; /* invalid option */ } lua_unlock(L); @@ -1189,7 +1193,6 @@ LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); if (n >= 2) { - luaC_checkGC(L); luaV_concat(L, n); } else if (n == 0) { /* push empty string */ @@ -1197,6 +1200,7 @@ LUA_API void lua_concat (lua_State *L, int n) { api_incr_top(L); } /* else n == 1; nothing to do */ + luaC_checkGC(L); lua_unlock(L); } @@ -1232,24 +1236,24 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { LUA_API void *lua_newuserdata (lua_State *L, size_t size) { Udata *u; lua_lock(L); - luaC_checkGC(L); - u = luaS_newudata(L, size, NULL); + u = luaS_newudata(L, size); setuvalue(L, L->top, u); api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); - return u + 1; + return getudatamem(u); } static const char *aux_upvalue (StkId fi, int n, TValue **val, - GCObject **owner) { + CClosure **owner, UpVal **uv) { switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(1 <= n && n <= f->nupvalues)) return NULL; *val = &f->upvalue[n-1]; - if (owner) *owner = obj2gco(f); + if (owner) *owner = f; return ""; } case LUA_TLCL: { /* Lua closure */ @@ -1258,9 +1262,9 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, SharedProto *p = f->p->sp; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; - if (owner) *owner = obj2gco(f->upvals[n - 1]); + if (uv) *uv = f->upvals[n - 1]; name = p->upvalues[n-1].name; - return (name == NULL) ? "" : getstr(name); + return (name == NULL) ? "(*no name)" : getstr(name); } default: return NULL; /* not a closure */ } @@ -1271,7 +1275,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL); + name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); if (name) { setobj2s(L, L->top, val); api_incr_top(L); @@ -1284,16 +1288,18 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ - GCObject *owner = NULL; /* to avoid warnings */ + CClosure *owner = NULL; + UpVal *uv = NULL; StkId fi; lua_lock(L); fi = index2addr(L, funcindex); api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner); + name = aux_upvalue(fi, n, &val, &owner, &uv); if (name) { L->top--; setobj(L, val, L->top); - luaC_barrier(L, owner, L->top); + if (owner) { luaC_barrier(L, owner, L->top); } + else if (uv) { luaC_upvalbarrier(L, uv); } } lua_unlock(L); return name; @@ -1305,7 +1311,7 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { StkId fi = index2addr(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check(L, (1 <= n && n <= f->p->sp->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } @@ -1335,7 +1341,11 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); + luaC_upvdeccount(L, *up1); *up1 = *up2; - luaC_objbarrier(L, f1, *up2); + (*up1)->refcount++; + if (upisopen(*up1)) (*up1)->u.open.touched = 1; + luaC_upvalbarrier(L, *up1); } + diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index c7d34ad8..6d36dee3 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 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 03975d2c..5988509e 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,9 +1,14 @@ /* -** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ +#define lauxlib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include @@ -12,13 +17,11 @@ #include -/* This file uses only the official API of Lua. +/* +** This file uses only the official API of Lua. ** Any function declared here could be written as an application function. */ -#define lauxlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -31,8 +34,8 @@ */ -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ +#define LEVELS1 10 /* size of the first part of the stack */ +#define LEVELS2 11 /* size of the second part of the stack */ @@ -64,11 +67,19 @@ static int findfield (lua_State *L, int objidx, int level) { } +/* +** Search for a name for a function in all loaded modules +*/ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ - lua_pushglobaltable(L); + lua_getfield(L, LUA_REGISTRYINDEX, 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.'? */ + lua_pushstring(L, name + 3); /* push name without prefix */ + lua_remove(L, -2); /* remove original name */ + } lua_copy(L, -1, top + 1); /* move name to proper place */ lua_pop(L, 2); /* remove pushed values */ return 1; @@ -81,24 +92,22 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (*ar->namewhat != '\0') /* is there a name? */ - lua_pushfstring(L, "function " LUA_QS, ar->name); + if (pushglobalfuncname(L, ar)) { /* try first a global name */ + lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } + else if (*ar->namewhat != '\0') /* is there a name from code? */ + lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ else if (*ar->what == 'm') /* main? */ lua_pushliteral(L, "main chunk"); - else if (*ar->what == 'C') { - if (pushglobalfuncname(L, ar)) { - lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else - lua_pushliteral(L, "?"); - } - else + else if (*ar->what != 'C') /* for Lua functions, use */ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); + else /* nothing left... */ + lua_pushliteral(L, "?"); } -static int countlevels (lua_State *L) { +static int lastlevel (lua_State *L) { lua_Debug ar; int li = 1, le = 1; /* find an upper bound */ @@ -117,14 +126,16 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { lua_Debug ar; int top = lua_gettop(L); - int numlevels = countlevels(L1); - int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0; - if (msg) lua_pushfstring(L, "%s\n", msg); + int last = lastlevel(L1); + int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; + if (msg) + lua_pushfstring(L, "%s\n", msg); + luaL_checkstack(L, 10, NULL); lua_pushliteral(L, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { - if (level == mark) { /* too many levels? */ + if (n1-- == 0) { /* too many levels? */ lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = numlevels - LEVELS2; /* and skip to last ones */ + level = last - LEVELS2 + 1; /* and skip to last ones */ } else { lua_getinfo(L1, "Slnt", &ar); @@ -150,36 +161,47 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, ** ======================================================= */ -LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { +LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { lua_Debug ar; if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); + return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); lua_getinfo(L, "n", &ar); if (strcmp(ar.namewhat, "method") == 0) { - narg--; /* do not count `self' */ - if (narg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling " LUA_QS " on bad self (%s)", + arg--; /* do not count 'self' */ + if (arg == 0) /* error is in the self argument itself? */ + return luaL_error(L, "calling '%s' on bad self (%s)", ar.name, extramsg); } if (ar.name == NULL) ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", - narg, ar.name, extramsg); + return luaL_error(L, "bad argument #%d to '%s' (%s)", + arg, ar.name, extramsg); } -static int typeerror (lua_State *L, int narg, const char *tname) { - const char *msg = lua_pushfstring(L, "%s expected, got %s", - tname, luaL_typename(L, narg)); - return luaL_argerror(L, narg, msg); +static int typeerror (lua_State *L, int arg, const char *tname) { + const char *msg; + const char *typearg; /* name for the type of the actual argument */ + if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) + typearg = lua_tostring(L, -1); /* use the given type name */ + else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) + typearg = "light userdata"; /* special name for messages */ + else + typearg = luaL_typename(L, arg); /* standard name */ + msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); + return luaL_argerror(L, arg, msg); } -static void tag_error (lua_State *L, int narg, int tag) { - typeerror(L, narg, lua_typename(L, tag)); +static void tag_error (lua_State *L, int arg, int tag) { + typeerror(L, arg, lua_typename(L, tag)); } +/* +** The use of 'lua_pushfstring' ensures this function does not +** need reserved stack space when called. +*/ LUALIB_API void luaL_where (lua_State *L, int level) { lua_Debug ar; if (lua_getstack(L, level, &ar)) { /* check function at level */ @@ -189,10 +211,15 @@ LUALIB_API void luaL_where (lua_State *L, int level) { return; } } - lua_pushliteral(L, ""); /* else, no information available... */ + lua_pushfstring(L, ""); /* else, no information available... */ } +/* +** Again, the use of 'lua_pushvfstring' ensures this function does +** not need reserved stack space when called. (At worst, it generates +** an error with "stack overflow" instead of the given message.) +*/ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); @@ -222,7 +249,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { } -#if !defined(inspectstat) /* { */ +#if !defined(l_inspectstat) /* { */ #if defined(LUA_USE_POSIX) @@ -231,13 +258,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { /* ** use appropriate macros to interpret 'pclose' return status */ -#define inspectstat(stat,what) \ +#define l_inspectstat(stat,what) \ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } #else -#define inspectstat(stat,what) /* no op */ +#define l_inspectstat(stat,what) /* no op */ #endif @@ -249,7 +276,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { if (stat == -1) /* error? */ return luaL_fileresult(L, 0, NULL); else { - inspectstat(stat, what); /* interpret result */ + l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else @@ -270,11 +297,12 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { */ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); /* try to get metatable */ - if (!lua_isnil(L, -1)) /* name already in use? */ + if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ return 0; /* leave previous value on top, but return 0 */ lua_pop(L, 1); - lua_newtable(L); /* create metatable */ + lua_createtable(L, 0, 2); /* create metatable */ + lua_pushstring(L, tname); + lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ return 1; @@ -317,23 +345,28 @@ LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { ** ======================================================= */ -LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, +LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, narg, def) : - luaL_checkstring(L, narg); + const char *name = (def) ? luaL_optstring(L, arg, def) : + luaL_checkstring(L, arg); int i; for (i=0; lst[i]; i++) if (strcmp(lst[i], name) == 0) return i; - return luaL_argerror(L, narg, - lua_pushfstring(L, "invalid option " LUA_QS, name)); + return luaL_argerror(L, arg, + lua_pushfstring(L, "invalid option '%s'", name)); } +/* +** Ensures the stack has at least 'space' extra slots, raising an error +** if it cannot fulfill the request. (The error handling needs a few +** extra slots to format the error message. In case of an error without +** this extra space, Lua will generate the same 'stack overflow' error, +** but without 'msg'.) +*/ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - /* keep some extra space to run error routines, if needed */ - const int extra = LUA_MINSTACK; - if (!lua_checkstack(L, space + extra)) { + if (!lua_checkstack(L, space)) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else @@ -342,77 +375,71 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { } -LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { - if (lua_type(L, narg) != t) - tag_error(L, narg, t); +LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { + if (lua_type(L, arg) != t) + tag_error(L, arg, t); } -LUALIB_API void luaL_checkany (lua_State *L, int narg) { - if (lua_type(L, narg) == LUA_TNONE) - luaL_argerror(L, narg, "value expected"); +LUALIB_API void luaL_checkany (lua_State *L, int arg) { + if (lua_type(L, arg) == LUA_TNONE) + luaL_argerror(L, arg, "value expected"); } -LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { - const char *s = lua_tolstring(L, narg, len); - if (!s) tag_error(L, narg, LUA_TSTRING); +LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { + const char *s = lua_tolstring(L, arg, len); + if (!s) tag_error(L, arg, LUA_TSTRING); return s; } -LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, +LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, const char *def, size_t *len) { - if (lua_isnoneornil(L, narg)) { + if (lua_isnoneornil(L, arg)) { if (len) *len = (def ? strlen(def) : 0); return def; } - else return luaL_checklstring(L, narg, len); + else return luaL_checklstring(L, arg, len); } -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; - lua_Number d = lua_tonumberx(L, narg, &isnum); + lua_Number d = lua_tonumberx(L, arg, &isnum); if (!isnum) - tag_error(L, narg, LUA_TNUMBER); + tag_error(L, arg, LUA_TNUMBER); return d; } -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, narg, def); +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { + return luaL_opt(L, luaL_checknumber, arg, def); } -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { +static void interror (lua_State *L, int arg) { + if (lua_isnumber(L, arg)) + luaL_argerror(L, arg, "number has no integer representation"); + else + tag_error(L, arg, LUA_TNUMBER); +} + + +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; - lua_Integer d = lua_tointegerx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); + lua_Integer d = lua_tointegerx(L, arg, &isnum); + if (!isnum) { + interror(L, arg); + } return d; } -LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) { - int isnum; - lua_Unsigned d = lua_tounsignedx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, narg, def); -} - - -LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, - lua_Unsigned def) { - return luaL_opt(L, luaL_checkunsigned, narg, def); + return luaL_opt(L, luaL_checkinteger, arg, def); } /* }====================================================== */ @@ -424,6 +451,47 @@ LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, ** ======================================================= */ +/* userdata to box arbitrary data */ +typedef struct UBox { + void *box; + size_t bsize; +} UBox; + + +static void *resizebox (lua_State *L, int idx, size_t newsize) { + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + UBox *box = (UBox *)lua_touserdata(L, idx); + void *temp = allocf(ud, box->box, box->bsize, newsize); + if (temp == NULL && newsize > 0) { /* allocation error? */ + resizebox(L, idx, 0); /* free buffer */ + luaL_error(L, "not enough memory for buffer allocation"); + } + box->box = temp; + box->bsize = newsize; + return temp; +} + + +static int boxgc (lua_State *L) { + resizebox(L, 1, 0); + return 0; +} + + +static void *newbox (lua_State *L, size_t newsize) { + UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); + box->box = NULL; + box->bsize = 0; + if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */ + lua_pushcfunction(L, boxgc); + lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ + } + lua_setmetatable(L, -2); + return resizebox(L, -1, newsize); +} + + /* ** check whether buffer is using a userdata on the stack as a temporary ** buffer @@ -444,11 +512,12 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { if (newsize < B->n || newsize - B->n < sz) luaL_error(L, "buffer too large"); /* create larger buffer */ - newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char)); - /* move content to new buffer */ - memcpy(newbuff, B->b, B->n * sizeof(char)); if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ + newbuff = (char *)resizebox(L, -1, newsize); + else { /* no buffer yet */ + newbuff = (char *)newbox(L, newsize); + memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ + } B->b = newbuff; B->size = newsize; } @@ -457,9 +526,11 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - char *b = luaL_prepbuffsize(B, l); - memcpy(b, s, l * sizeof(char)); - luaL_addsize(B, l); + if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ + char *b = luaL_prepbuffsize(B, l); + memcpy(b, s, l * sizeof(char)); + luaL_addsize(B, l); + } } @@ -471,8 +542,10 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ + if (buffonstack(B)) { + resizebox(L, -2, 0); /* delete old buffer */ + lua_remove(L, -2); /* remove its header from the stack */ + } } @@ -523,7 +596,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ + return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); lua_rawgeti(L, t, freelist); /* get first free element */ @@ -562,7 +635,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { typedef struct LoadF { int n; /* number of pre-read characters */ FILE *f; /* file being read */ - char buff[LUAL_BUFFERSIZE]; /* area for reading file */ + char buff[BUFSIZ]; /* area for reading file */ } LoadF; @@ -594,7 +667,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ + const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ int c; lf->n = 0; do { @@ -619,7 +692,7 @@ static int skipcomment (LoadF *lf, int *cp) { if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ c = getc(lf->f); - } while (c != EOF && c != '\n') ; + } while (c != EOF && c != '\n'); *cp = getc(lf->f); /* skip end-of-line, if present */ return 1; /* there was a comment */ } @@ -655,7 +728,7 @@ static int luaL_loadfilex_ (lua_State *L, const char *filename, readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from `lua_load' */ + lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); @@ -698,23 +771,23 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { if (!lua_getmetatable(L, obj)) /* no metatable? */ - return 0; - lua_pushstring(L, event); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - lua_pop(L, 2); /* remove metatable and metafield */ - return 0; - } + return LUA_TNIL; else { - lua_remove(L, -2); /* remove only metatable */ - return 1; + int tt; + lua_pushstring(L, event); + tt = lua_rawget(L, -2); + if (tt == LUA_TNIL) /* is metafield nil? */ + lua_pop(L, 2); /* remove metatable and metafield */ + else + lua_remove(L, -2); /* remove only metatable */ + return tt; /* return metafield type */ } } LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { obj = lua_absindex(L, obj); - if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ + if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ return 0; lua_pushvalue(L, obj); lua_call(L, 1, 1); @@ -722,22 +795,32 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { } -LUALIB_API int luaL_len (lua_State *L, int idx) { - int l; +LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { + lua_Integer l; int isnum; lua_len(L, idx); - l = (int)lua_tointegerx(L, -1, &isnum); + l = lua_tointegerx(L, -1, &isnum); if (!isnum) - luaL_error(L, "object length is not a number"); + luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; } LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ + if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); + } + else { switch (lua_type(L, idx)) { - case LUA_TNUMBER: + case LUA_TNUMBER: { + if (lua_isinteger(L, idx)) + lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); + else + lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); + break; + } case LUA_TSTRING: lua_pushvalue(L, idx); break; @@ -747,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { case LUA_TNIL: lua_pushliteral(L, "nil"); break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), - lua_topointer(L, idx)); + default: { + int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ + const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : + luaL_typename(L, idx); + lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); + if (tt != LUA_TNIL) + lua_remove(L, -2); /* remove '__name' */ break; + } } } return lua_tolstring(L, -1, len); @@ -772,8 +860,7 @@ static const char *luaL_findtable (lua_State *L, int idx, e = strchr(fname, '.'); if (e == NULL) e = fname + strlen(fname); lua_pushlstring(L, fname, e - fname); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ + if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */ lua_pop(L, 1); /* remove this nil */ lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ lua_pushlstring(L, fname, e - fname); @@ -803,24 +890,23 @@ static int libsize (const luaL_Reg *l) { /* ** Find or create a module table with a given name. The function -** first looks at the _LOADED table and, if that fails, try a +** first looks at the LOADED table and, if that fails, try a ** global variable with that name. In any case, leaves on the stack ** the module table. */ LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, int sizehint) { - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ - lua_getfield(L, -1, modname); /* get _LOADED[modname] */ - if (!lua_istable(L, -1)) { /* not found? */ + 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 " LUA_QS, modname); + luaL_error(L, "name conflict for module '%s'", modname); lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ + lua_setfield(L, -3, modname); /* LOADED[modname] = new table */ } - lua_remove(L, -2); /* remove _LOADED table */ + lua_remove(L, -2); /* remove LOADED table */ } @@ -846,7 +932,6 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, ** Returns with only the table at the stack. */ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkversion(L); luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; @@ -864,8 +949,8 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { ** into the stack */ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { - lua_getfield(L, idx, fname); - if (lua_istable(L, -1)) return 1; /* table already there */ + if (lua_getfield(L, idx, fname) == LUA_TTABLE) + return 1; /* table already there */ else { lua_pop(L, 1); /* remove previous result */ idx = lua_absindex(L, idx); @@ -878,22 +963,26 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { /* -** stripped-down 'require'. Calls 'openf' to open a module, -** registers the result in 'package.loaded' table and, if 'glb' -** is true, also registers the result in the global table. +** Stripped-down 'require': After checking "loaded" table, calls 'openf' +** to open a module, registers the result in 'package.loaded' table and, +** if 'glb' is true, also registers the result in the global table. ** Leaves resulting module on the top. */ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* open module */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_pushvalue(L, -2); /* make copy of module (call result) */ - lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ - lua_pop(L, 1); /* remove _LOADED table */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + lua_getfield(L, -1, modname); /* LOADED[modname] */ + if (!lua_toboolean(L, -1)) { /* package not already loaded? */ + lua_pop(L, 1); /* remove field */ + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); /* argument to open function */ + lua_call(L, 1, 1); /* call 'openf' to open module */ + lua_pushvalue(L, -1); /* make copy of module (call result) */ + lua_setfield(L, -3, modname); /* LOADED[modname] = module */ + } + lua_remove(L, -2); /* remove LOADED table */ if (glb) { - lua_pushvalue(L, -1); /* copy of 'mod' */ + lua_pushvalue(L, -1); /* copy of module */ lua_setglobal(L, modname); /* _G[modname] = module */ } } @@ -908,7 +997,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, while ((wild = strstr(s, p)) != NULL) { luaL_addlstring(&b, s, wild - s); /* push prefix */ luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ + s = wild + l; /* continue after 'p' */ } luaL_addstring(&b, s); /* push last suffix */ luaL_pushresult(&b); @@ -928,8 +1017,8 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { - luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", + lua_tostring(L, -1)); return 0; /* return to Lua to abort */ } @@ -941,47 +1030,41 @@ LUALIB_API lua_State *luaL_newstate (void) { } -LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) { +LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { const lua_Number *v = lua_version(L); + if (sz != LUAL_NUMSIZES) /* check numeric types */ + luaL_error(L, "core and library have incompatible numeric types"); if (v != lua_version(NULL)) luaL_error(L, "multiple Lua VMs detected"); else if (*v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - ver, *v); - /* check conversions number -> integer types */ - lua_pushnumber(L, -(lua_Number)0x1234); - if (lua_tointeger(L, -1) != -0x1234 || - lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234) - luaL_error(L, "bad conversion number->int;" - " must recompile Lua with proper settings"); - lua_pop(L, 1); + (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); } // use clonefunction -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); +#include "spinlock.h" struct codecache { - int lock; + struct spinlock lock; lua_State *L; }; -static struct codecache CC = { 0 , NULL }; +static struct codecache CC; static void clearcache() { if (CC.L == NULL) return; - LOCK(&CC) + SPIN_LOCK(&CC) lua_close(CC.L); CC.L = luaL_newstate(); - UNLOCK(&CC) + SPIN_UNLOCK(&CC) } static void init() { - CC.lock = 0; + SPIN_INIT(&CC); CC.L = luaL_newstate(); } @@ -989,13 +1072,13 @@ static const void * load(const char *key) { if (CC.L == NULL) return NULL; - LOCK(&CC) + SPIN_LOCK(&CC) lua_State *L = CC.L; lua_pushstring(L, key); lua_rawget(L, LUA_REGISTRYINDEX); const void * result = lua_touserdata(L, -1); lua_pop(L, 1); - UNLOCK(&CC) + SPIN_UNLOCK(&CC) return result; } @@ -1005,7 +1088,7 @@ save(const char *key, const void * proto) { lua_State *L; const void * result = NULL; - LOCK(&CC) + SPIN_LOCK(&CC) if (CC.L == NULL) { init(); L = CC.L; @@ -1023,17 +1106,66 @@ save(const char *key, const void * proto) { lua_pop(L,2); } } - UNLOCK(&CC) + SPIN_UNLOCK(&CC) return result; } +#define CACHE_OFF 0 +#define CACHE_EXIST 1 +#define CACHE_ON 2 + +static int cache_key = 0; + +static int cache_level(lua_State *L) { + int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); + int r = lua_tointeger(L, -1); + lua_pop(L,1); + if (t == LUA_TNUMBER) { + return r; + } + return CACHE_ON; +} + +static int cache_mode(lua_State *L) { + static const char * lst[] = { + "OFF", + "EXIST", + "ON", + NULL, + }; + if (lua_isnoneornil(L,1)) { + int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); + int r = lua_tointeger(L, -1); + if (t == LUA_TNUMBER) { + if (r < 0 || r >= CACHE_ON) { + r = CACHE_ON; + } + } else { + r = CACHE_ON; + } + lua_pushstring(L, lst[r]); + return 1; + } + int t = luaL_checkoption(L, 1, "OFF" , lst); + lua_pushinteger(L, t); + lua_rawsetp(L, LUA_REGISTRYINDEX, &cache_key); + return 0; +} + LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { + int level = cache_level(L); + if (level == CACHE_OFF) { + return luaL_loadfilex_(L, filename, mode); + } const void * proto = load(filename); if (proto) { lua_clonefunction(L, proto); return LUA_OK; } + if (level == CACHE_EXIST) { + return luaL_loadfilex_(L, filename, mode); + } lua_State * eL = luaL_newstate(); if (eL == NULL) { lua_pushliteral(L, "New state failed"); @@ -1056,11 +1188,13 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lua_clonefunction(L, proto); /* Never close it. notice: memory leak */ } + return LUA_OK; } static int cache_clear(lua_State *L) { + (void)(L); clearcache(); return 0; } @@ -1068,10 +1202,11 @@ cache_clear(lua_State *L) { LUAMOD_API int luaopen_cache(lua_State *L) { luaL_Reg l[] = { { "clear", cache_clear }, + { "mode", cache_mode }, { NULL, NULL }, }; luaL_newlib(L,l); - lua_getglobal(L, "loadfile"); - lua_setfield(L, -2, "loadfile"); + lua_getglobal(L, "loadfile"); + lua_setfield(L, -2, "loadfile"); return 1; } diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 0fb023b8..9a2e66aa 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -16,40 +16,48 @@ -/* extra error code for `luaL_load' */ +/* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) +/* key, in the registry, for table of loaded modules */ +#define LUA_LOADED_TABLE "_LOADED" + + +/* key, in the registry, for table of preloaded loaders */ +#define LUA_PRELOAD_TABLE "_PRELOAD" + + typedef struct luaL_Reg { const char *name; lua_CFunction func; } luaL_Reg; -LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); -#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) +#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) + +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); +#define luaL_checkversion(L) \ + luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, +LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, lua_Integer def); -LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); -LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, - lua_Unsigned def); LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); +LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int arg); LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); @@ -59,13 +67,13 @@ LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_where) (lua_State *L, int lvl); LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, +LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, const char *const lst[]); LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); -/* pre-defined references */ +/* predefined references */ #define LUA_NOREF (-2) #define LUA_REFNIL (-1) @@ -83,7 +91,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); -LUALIB_API int (luaL_len) (lua_State *L, int idx); +LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); @@ -108,16 +116,13 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) -#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) +#define luaL_newlib(L,l) \ + (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_argcheck(L, cond,arg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) @@ -207,6 +212,53 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, #endif +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) +#endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + + +/* +** {============================================================ +** Compatibility with deprecated conversions +** ============================================================= +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) +#define luaL_optunsigned(L,a,d) \ + ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) + +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) + +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#endif +/* }============================================================ */ + + + #endif diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 5255b3cd..08523e6e 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,9 +1,13 @@ /* -** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ +#define lbaselib_c +#define LUA_LIB + +#include "lprefix.h" #include @@ -11,9 +15,6 @@ #include #include -#define lbaselib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -32,65 +33,77 @@ static int luaB_print (lua_State *L) { lua_call(L, 1, 1); s = lua_tolstring(L, -1, &l); /* get result */ if (s == NULL) - return luaL_error(L, - LUA_QL("tostring") " must return a string to " LUA_QL("print")); - if (i>1) luai_writestring("\t", 1); - luai_writestring(s, l); + return luaL_error(L, "'tostring' must return a string to 'print'"); + if (i>1) lua_writestring("\t", 1); + lua_writestring(s, l); lua_pop(L, 1); /* pop result */ } - luai_writeline(); + lua_writeline(); return 0; } #define SPACECHARS " \f\n\r\t\v" +static const char *b_str2int (const char *s, int base, lua_Integer *pn) { + lua_Unsigned n = 0; + int neg = 0; + s += strspn(s, SPACECHARS); /* skip initial spaces */ + if (*s == '-') { s++; neg = 1; } /* handle signal */ + else if (*s == '+') s++; + if (!isalnum((unsigned char)*s)) /* no digit? */ + return NULL; + do { + int digit = (isdigit((unsigned char)*s)) ? *s - '0' + : (toupper((unsigned char)*s) - 'A') + 10; + if (digit >= base) return NULL; /* invalid numeral */ + n = n * base + digit; + s++; + } while (isalnum((unsigned char)*s)); + s += strspn(s, SPACECHARS); /* skip trailing spaces */ + *pn = (lua_Integer)((neg) ? (0u - n) : n); + return s; +} + + static int luaB_tonumber (lua_State *L) { - if (lua_isnoneornil(L, 2)) { /* standard conversion */ - int isnum; - lua_Number n = lua_tonumberx(L, 1, &isnum); - if (isnum) { - lua_pushnumber(L, n); - return 1; - } /* else not a number; must be something */ + if (lua_isnoneornil(L, 2)) { /* standard conversion? */ luaL_checkany(L, 1); + if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ + lua_settop(L, 1); /* yes; return it */ + return 1; + } + else { + size_t l; + const char *s = lua_tolstring(L, 1, &l); + if (s != NULL && lua_stringtonumber(L, s) == l + 1) + return 1; /* successful conversion to number */ + /* else not a number */ + } } else { size_t l; - const char *s = luaL_checklstring(L, 1, &l); - const char *e = s + l; /* end point for 's' */ - int base = luaL_checkint(L, 2); - int neg = 0; + const char *s; + lua_Integer n = 0; /* to avoid warnings */ + lua_Integer base = luaL_checkinteger(L, 2); + luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ + s = lua_tolstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle signal */ - else if (*s == '+') s++; - if (isalnum((unsigned char)*s)) { - lua_Number n = 0; - do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : toupper((unsigned char)*s) - 'A' + 10; - if (digit >= base) break; /* invalid numeral; force a fail */ - n = n * (lua_Number)base + (lua_Number)digit; - s++; - } while (isalnum((unsigned char)*s)); - s += strspn(s, SPACECHARS); /* skip trailing spaces */ - if (s == e) { /* no invalid trailing characters? */ - lua_pushnumber(L, (neg) ? -n : n); - return 1; - } /* else not a number */ + if (b_str2int(s, (int)base, &n) == s + l) { + lua_pushinteger(L, n); + return 1; } /* else not a number */ - } + } /* else not a number */ lua_pushnil(L); /* not a number */ return 1; } static int luaB_error (lua_State *L) { - int level = luaL_optint(L, 2, 1); + int level = (int)luaL_optinteger(L, 2, 1); lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ - luaL_where(L, level); + if (lua_type(L, 1) == LUA_TSTRING && level > 0) { + luaL_where(L, level); /* add extra information */ lua_pushvalue(L, 1); lua_concat(L, 2); } @@ -114,7 +127,7 @@ static int luaB_setmetatable (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table expected"); - if (luaL_getmetafield(L, 1, "__metatable")) + if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); @@ -160,19 +173,18 @@ static int luaB_rawset (lua_State *L) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", - "setmajorinc", "isrunning", "generational", "incremental", NULL}; + "isrunning", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; + LUA_GCISRUNNING}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - int ex = luaL_optint(L, 2, 0); + int ex = (int)luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, ex); switch (o) { case LUA_GCCOUNT: { int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, res + ((lua_Number)b/1024)); - lua_pushinteger(L, b); - return 2; + lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); + return 1; } case LUA_GCSTEP: case LUA_GCISRUNNING: { lua_pushboolean(L, res); @@ -187,16 +199,17 @@ static int luaB_collectgarbage (lua_State *L) { static int luaB_type (lua_State *L) { - luaL_checkany(L, 1); - lua_pushstring(L, luaL_typename(L, 1)); + int t = lua_type(L, 1); + luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); + lua_pushstring(L, lua_typename(L, t)); return 1; } static int pairsmeta (lua_State *L, const char *method, int iszero, lua_CFunction iter) { - if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ - luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ + 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 */ @@ -227,18 +240,30 @@ static int luaB_pairs (lua_State *L) { } +/* +** Traversal function for 'ipairs' +*/ static int ipairsaux (lua_State *L) { - int i = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - i++; /* next value */ + lua_Integer i = luaL_checkinteger(L, 2) + 1; lua_pushinteger(L, i); - lua_rawgeti(L, 1, i); - return (lua_isnil(L, -1)) ? 1 : 2; + return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; } +/* +** 'ipairs' function. Returns 'ipairsaux', given "table", 0. +** (The given "table" may not be a table.) +*/ static int luaB_ipairs (lua_State *L) { +#if defined(LUA_COMPAT_IPAIRS) 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 } @@ -284,7 +309,7 @@ static int luaB_loadfile (lua_State *L) { /* -** Reader for generic `load' function: `lua_load' uses the +** Reader for generic 'load' function: 'lua_load' uses the ** stack for internal stuff, so the reader cannot change the ** stack top. Instead, it keeps its resulting string in a ** reserved slot inside the stack. @@ -328,7 +353,8 @@ static int luaB_load (lua_State *L) { /* }====================================================== */ -static int dofilecont (lua_State *L) { +static int dofilecont (lua_State *L, int d1, lua_KContext d2) { + (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ return lua_gettop(L) - 1; } @@ -339,14 +365,20 @@ static int luaB_dofile (lua_State *L) { if (luaL_loadfile(L, fname) != LUA_OK) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L); + return dofilecont(L, 0, 0); } static int luaB_assert (lua_State *L) { - if (!lua_toboolean(L, 1)) - return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); - return lua_gettop(L); + if (lua_toboolean(L, 1)) /* condition is true? */ + return lua_gettop(L); /* return all arguments */ + else { /* error */ + luaL_checkany(L, 1); /* there must be a condition */ + lua_remove(L, 1); /* remove it */ + lua_pushliteral(L, "assertion failed!"); /* default message */ + lua_settop(L, 1); /* leave only message (default if no other one) */ + return luaB_error(L); /* call 'error' */ + } } @@ -357,53 +389,57 @@ static int luaB_select (lua_State *L) { return 1; } else { - int i = luaL_checkint(L, 1); + lua_Integer i = luaL_checkinteger(L, 1); if (i < 0) i = n + i; else if (i > n) i = n; luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - i; + return n - (int)i; } } -static int finishpcall (lua_State *L, int status) { - if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ - lua_settop(L, 0); /* create space for return values */ - lua_pushboolean(L, 0); - lua_pushstring(L, "stack overflow"); +/* +** Continuation function for 'pcall' and 'xpcall'. Both functions +** already pushed a 'true' before doing the call, so in case of success +** 'finishpcall' only has to return everything in the stack minus +** 'extra' values (where 'extra' is exactly the number of items to be +** ignored). +*/ +static int finishpcall (lua_State *L, int status, lua_KContext extra) { + if (status != LUA_OK && status != LUA_YIELD) { /* error? */ + lua_pushboolean(L, 0); /* first result (false) */ + lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ } - lua_pushboolean(L, status); /* first result (status) */ - lua_replace(L, 1); /* put first result in first slot */ - return lua_gettop(L); -} - - -static int pcallcont (lua_State *L) { - int status = lua_getctx(L, NULL); - return finishpcall(L, (status == LUA_YIELD)); + else + return lua_gettop(L) - (int)extra; /* return all results */ } static int luaB_pcall (lua_State *L) { int status; luaL_checkany(L, 1); - lua_pushnil(L); - lua_insert(L, 1); /* create space for status result */ - status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); + lua_pushboolean(L, 1); /* first result if no errors */ + lua_insert(L, 1); /* put it in place */ + status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); + return finishpcall(L, status, 0); } +/* +** Do a protected call with error handling. After 'lua_rotate', the +** stack will have ; so, the function passes +** 2 to 'finishpcall' to skip the 2 first values when returning results. +*/ static int luaB_xpcall (lua_State *L) { int status; int n = lua_gettop(L); - luaL_argcheck(L, n >= 2, 2, "value expected"); - lua_pushvalue(L, 1); /* exchange function... */ - lua_copy(L, 2, 1); /* ...and error handler */ - lua_replace(L, 2); - status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); + luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ + lua_pushboolean(L, 1); /* first result */ + lua_pushvalue(L, 1); /* function */ + lua_rotate(L, 3, 2); /* move them below function's arguments */ + status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); + return finishpcall(L, status, 2); } @@ -440,19 +476,23 @@ static const luaL_Reg base_funcs[] = { {"tostring", luaB_tostring}, {"type", luaB_type}, {"xpcall", luaB_xpcall}, + /* placeholders */ + {"_G", NULL}, + {"_VERSION", NULL}, {NULL, NULL} }; LUAMOD_API int luaopen_base (lua_State *L) { - /* set global _G */ - lua_pushglobaltable(L); - lua_pushglobaltable(L); - lua_setfield(L, -2, "_G"); /* open lib into global table */ + lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); + /* set global _G */ + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_G"); + /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ + lua_setfield(L, -2, "_VERSION"); return 1; } diff --git a/3rd/lua/lbitlib.c b/3rd/lua/lbitlib.c index 31c7b66f..1cb1d5b9 100644 --- a/3rd/lua/lbitlib.c +++ b/3rd/lua/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ +** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ @@ -7,20 +7,36 @@ #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) @@ -29,28 +45,25 @@ #define mask(n) (~((ALLONES << 1) << ((n) - 1))) -typedef lua_Unsigned b_uint; - - -static b_uint andaux (lua_State *L) { +static lua_Unsigned andaux (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = ~(b_uint)0; + lua_Unsigned r = ~(lua_Unsigned)0; for (i = 1; i <= n; i++) - r &= luaL_checkunsigned(L, i); + r &= checkunsigned(L, i); return trim(r); } static int b_and (lua_State *L) { - b_uint r = andaux(L); - lua_pushunsigned(L, r); + lua_Unsigned r = andaux(L); + pushunsigned(L, r); return 1; } static int b_test (lua_State *L) { - b_uint r = andaux(L); + lua_Unsigned r = andaux(L); lua_pushboolean(L, r != 0); return 1; } @@ -58,32 +71,32 @@ static int b_test (lua_State *L) { static int b_or (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = 0; + lua_Unsigned r = 0; for (i = 1; i <= n; i++) - r |= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); + r |= checkunsigned(L, i); + pushunsigned(L, trim(r)); return 1; } static int b_xor (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = 0; + lua_Unsigned r = 0; for (i = 1; i <= n; i++) - r ^= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); + r ^= checkunsigned(L, i); + pushunsigned(L, trim(r)); return 1; } static int b_not (lua_State *L) { - b_uint r = ~luaL_checkunsigned(L, 1); - lua_pushunsigned(L, trim(r)); + lua_Unsigned r = ~checkunsigned(L, 1); + pushunsigned(L, trim(r)); return 1; } -static int b_shift (lua_State *L, b_uint r, int i) { +static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { if (i < 0) { /* shift right? */ i = -i; r = trim(r); @@ -95,54 +108,54 @@ static int b_shift (lua_State *L, b_uint r, int i) { else r <<= i; r = trim(r); } - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } static int b_lshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); + return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); } static int b_rshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); + return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); } static int b_arshift (lua_State *L) { - b_uint r = luaL_checkunsigned(L, 1); - int i = luaL_checkint(L, 2); - if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) + lua_Unsigned r = checkunsigned(L, 1); + lua_Integer i = luaL_checkinteger(L, 2); + if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) return b_shift(L, r, -i); else { /* arithmetic shift for 'negative' number */ if (i >= LUA_NBITS) r = ALLONES; else - r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ - lua_pushunsigned(L, r); + r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ + pushunsigned(L, r); return 1; } } -static int b_rot (lua_State *L, int i) { - b_uint r = luaL_checkunsigned(L, 1); - i &= (LUA_NBITS - 1); /* i = i % NBITS */ +static int b_rot (lua_State *L, lua_Integer d) { + lua_Unsigned r = checkunsigned(L, 1); + int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ r = trim(r); if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ r = (r << i) | (r >> (LUA_NBITS - i)); - lua_pushunsigned(L, trim(r)); + pushunsigned(L, trim(r)); return 1; } static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkint(L, 2)); + return b_rot(L, luaL_checkinteger(L, 2)); } static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkint(L, 2)); + return b_rot(L, -luaL_checkinteger(L, 2)); } @@ -153,36 +166,35 @@ static int b_rrot (lua_State *L) { ** 'width' being used uninitialized.) */ static int fieldargs (lua_State *L, int farg, int *width) { - int f = luaL_checkint(L, farg); - int w = luaL_optint(L, farg + 1, 1); + lua_Integer f = luaL_checkinteger(L, farg); + lua_Integer w = luaL_optinteger(L, farg + 1, 1); luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); if (f + w > LUA_NBITS) luaL_error(L, "trying to access non-existent bits"); - *width = w; - return f; + *width = (int)w; + return (int)f; } static int b_extract (lua_State *L) { int w; - b_uint r = luaL_checkunsigned(L, 1); + lua_Unsigned r = trim(checkunsigned(L, 1)); int f = fieldargs(L, 2, &w); r = (r >> f) & mask(w); - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } static int b_replace (lua_State *L) { int w; - b_uint r = luaL_checkunsigned(L, 1); - b_uint v = luaL_checkunsigned(L, 2); + lua_Unsigned r = trim(checkunsigned(L, 1)); + lua_Unsigned v = trim(checkunsigned(L, 2)); int f = fieldargs(L, 3, &w); - int m = mask(w); - v &= m; /* erase bits outside given width */ - r = (r & ~(m << f)) | (v << f); - lua_pushunsigned(L, r); + lua_Unsigned m = mask(w); + r = (r & ~(m << f)) | ((v & m) << f); + pushunsigned(L, r); return 1; } @@ -210,3 +222,12 @@ LUAMOD_API int luaopen_bit32 (lua_State *L) { return 1; } + +#else /* }{ */ + + +LUAMOD_API int luaopen_bit32 (lua_State *L) { + return luaL_error(L, "library 'bit32' has been deprecated"); +} + +#endif /* } */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 5ba570e0..18116445 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,15 +1,18 @@ /* -** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ - -#include - #define lcode_c #define LUA_CORE +#include "lprefix.h" + + +#include +#include + #include "lua.h" #include "lcode.h" @@ -26,21 +29,45 @@ #include "lvm.h" +/* Maximum number of registers in a Lua function (must fit in 8 bits) */ +#define MAXREGS 255 + + #define hasjumps(e) ((e)->t != (e)->f) -static int isnumeral(expdesc *e) { - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); +/* +** 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) { + if (hasjumps(e)) + return 0; /* not a numeral */ + switch (e->k) { + case VKINT: + if (v) setivalue(v, e->u.ival); + return 1; + case VKFLT: + if (v) setfltvalue(v, e->u.nval); + return 1; + default: return 0; + } } +/* +** Create a OP_LOADNIL instruction, but try to optimize: if the previous +** instruction is also OP_LOADNIL and ranges are compatible, adjust +** range of previous instruction instead of emitting a new one. (For +** instance, 'local a; local b' will generate a single opcode.) +*/ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ previous = &fs->f->sp->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { - int pfrom = GETARG_A(*previous); + if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ + int pfrom = GETARG_A(*previous); /* get previous range */ int pl = pfrom + GETARG_B(*previous); if ((pfrom <= from && from <= pl + 1) || (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ @@ -56,47 +83,10 @@ void luaK_nil (FuncState *fs, int from, int n) { } -int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - fs->jpc = NO_JUMP; - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); - luaK_concat(fs, &j, jpc); /* keep them on hold */ - return j; -} - - -void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); -} - - -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { - luaK_codeABC(fs, op, A, B, C); - return luaK_jump(fs); -} - - -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->sp->code[pc]; - int offset = dest-(pc+1); - lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); -} - - /* -** returns current `pc' and marks it as a jump target (to avoid wrong -** optimizations with consecutive instructions not in the same basic block). +** Gets the destination address of a jump instruction. Used to traverse +** a list of jumps. */ -int luaK_getlabel (FuncState *fs) { - fs->lasttarget = fs->pc; - return fs->pc; -} - - static int getjump (FuncState *fs, int pc) { int offset = GETARG_sBx(fs->f->sp->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ @@ -106,6 +96,86 @@ static int getjump (FuncState *fs, int pc) { } +/* +** Fix jump instruction at position 'pc' to jump to 'dest'. +** (Jump addresses are relative in Lua) +*/ +static void fixjump (FuncState *fs, int pc, int dest) { + Instruction *jmp = &fs->f->sp->code[pc]; + int offset = dest - (pc + 1); + lua_assert(dest != NO_JUMP); + if (abs(offset) > MAXARG_sBx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_sBx(*jmp, offset); +} + + +/* +** Concatenate jump-list 'l2' into jump-list 'l1' +*/ +void luaK_concat (FuncState *fs, int *l1, int l2) { + if (l2 == NO_JUMP) return; /* nothing to concatenate? */ + else if (*l1 == NO_JUMP) /* no original list? */ + *l1 = l2; /* 'l1' points to 'l2' */ + else { + int list = *l1; + int next; + while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ + list = next; + fixjump(fs, list, l2); /* last element links to 'l2' */ + } +} + + +/* +** Create a jump instruction and return its position, so its destination +** can be fixed later (with 'fixjump'). If there are jumps to +** this position (kept in 'jpc'), link them all together so that +** 'patchlistaux' will fix all them directly to the final destination. +*/ +int luaK_jump (FuncState *fs) { + int jpc = fs->jpc; /* save list of jumps to here */ + int j; + fs->jpc = NO_JUMP; /* no more jumps to here */ + j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); + luaK_concat(fs, &j, jpc); /* keep them on hold */ + return j; +} + + +/* +** Code a 'return' instruction +*/ +void luaK_ret (FuncState *fs, int first, int nret) { + luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); +} + + +/* +** Code a "conditional jump", that is, a test or comparison opcode +** followed by a jump. Return jump position. +*/ +static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { + luaK_codeABC(fs, op, A, B, C); + return luaK_jump(fs); +} + + +/* +** returns current 'pc' and marks it as a jump target (to avoid wrong +** optimizations with consecutive instructions not in the same basic block). +*/ +int luaK_getlabel (FuncState *fs) { + fs->lasttarget = fs->pc; + return fs->pc; +} + + +/* +** Returns the position of the instruction "controlling" a given +** jump (that is, its condition), or the jump itself if it is +** unconditional. +*/ static Instruction *getjumpcontrol (FuncState *fs, int pc) { Instruction *pi = &fs->f->sp->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) @@ -116,37 +186,41 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) { /* -** check whether list has any jump that do not produce a value -** (or produce an inverted value) +** Patch destination register for a TESTSET instruction. +** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). +** Otherwise, if 'reg' is not 'NO_REG', set it as the destination +** register. Otherwise, change instruction to a simple 'TEST' (produces +** no register value) */ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - static int patchtestreg (FuncState *fs, int node, int reg) { Instruction *i = getjumpcontrol(fs, node); if (GET_OPCODE(*i) != OP_TESTSET) return 0; /* cannot patch other instructions */ if (reg != NO_REG && reg != GETARG_B(*i)) SETARG_A(*i, reg); - else /* no register to put value or register already has the value */ + else { + /* no register to put value or register already has the value; + change instruction to simple test */ *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); - + } return 1; } +/* +** Traverse a list of tests ensuring no one produces a value +*/ static void removevalues (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) patchtestreg(fs, list, NO_REG); } +/* +** Traverse a list of tests, patching their destination address and +** registers: tests producing values jump to 'vtarget' (and put their +** values in 'reg'), other tests jump to 'dtarget'. +*/ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, int dtarget) { while (list != NO_JUMP) { @@ -160,15 +234,35 @@ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, } +/* +** Ensure all pending jumps to current position are fixed (jumping +** to current position with no values) and reset list of pending +** jumps +*/ static void dischargejpc (FuncState *fs) { patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); fs->jpc = NO_JUMP; } +/* +** Add elements in 'list' to list of pending jumps to "here" +** (current position) +*/ +void luaK_patchtohere (FuncState *fs, int list) { + luaK_getlabel(fs); /* mark "here" as a jump target */ + luaK_concat(fs, &fs->jpc, list); +} + + +/* +** Path all jumps in 'list' to jump to 'target'. +** (The assert means that we cannot fix a jump to a forward address +** because we only know addresses once code is generated.) +*/ void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) - luaK_patchtohere(fs, list); + if (target == fs->pc) /* 'target' is current position? */ + luaK_patchtohere(fs, list); /* add list to pending jumps */ else { lua_assert(target < fs->pc); patchlistaux(fs, list, target, NO_REG, target); @@ -176,54 +270,45 @@ void luaK_patchlist (FuncState *fs, int list, int target) { } -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) { +/* +** Path all jumps in 'list' to close upvalues up to given 'level' +** (The assertion checks that jumps either were closing nothing +** or were closing higher levels, from inner blocks.) +*/ +void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ - while (list != NO_JUMP) { - int next = getjump(fs, list); + for (; list != NO_JUMP; list = getjump(fs, list)) { lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && (GETARG_A(fs->f->sp->code[list]) == 0 || GETARG_A(fs->f->sp->code[list]) >= level)); SETARG_A(fs->f->sp->code[list], level); - list = next; - } -} - - -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); - luaK_concat(fs, &fs->jpc, list); -} - - -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; - else if (*l1 == NO_JUMP) - *l1 = l2; - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); } } +/* +** Emit instruction 'i', checking for array sizes and saving also its +** line information. Return 'i' position. +*/ static int luaK_code (FuncState *fs, Instruction i) { - SharedProto *f = fs->f->sp; - dischargejpc(fs); /* `pc' will change */ + Proto *f = fs->f; + dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, + luaM_growvector(fs->ls->L, f->sp->code, fs->pc, f->sp->sizecode, Instruction, MAX_INT, "opcodes"); - f->code[fs->pc] = i; + f->sp->code[fs->pc] = i; /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, + luaM_growvector(fs->ls->L, f->sp->lineinfo, fs->pc, f->sp->sizelineinfo, int, MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; + f->sp->lineinfo[fs->pc] = fs->ls->lastline; return fs->pc++; } +/* +** Format and emit an 'iABC' instruction. (Assertions check consistency +** of parameters versus opcode.) +*/ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { lua_assert(getOpMode(o) == iABC); lua_assert(getBMode(o) != OpArgN || b == 0); @@ -233,6 +318,9 @@ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { } +/* +** Format and emit an 'iABx' instruction. +*/ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); lua_assert(getCMode(o) == OpArgN); @@ -241,12 +329,20 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { } +/* +** Emit an "extra argument" instruction (format 'iAx') +*/ static int codeextraarg (FuncState *fs, int a) { lua_assert(a <= MAXARG_Ax); return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); } +/* +** Emit a "load constant" instruction, using either 'OP_LOADK' +** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' +** instruction with "extra argument". +*/ int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); @@ -258,22 +354,35 @@ int luaK_codek (FuncState *fs, int reg, int k) { } +/* +** Check register-stack level, keeping track of its maximum size +** in field 'maxstacksize' +*/ void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->sp->maxstacksize) { - if (newstack >= MAXSTACK) - luaX_syntaxerror(fs->ls, "function or expression too complex"); + if (newstack >= MAXREGS) + luaX_syntaxerror(fs->ls, + "function or expression needs too many registers"); fs->f->sp->maxstacksize = cast_byte(newstack); } } +/* +** Reserve 'n' registers in register stack +*/ void luaK_reserveregs (FuncState *fs, int n) { luaK_checkstack(fs, n); fs->freereg += n; } +/* +** Free register 'reg', if it is neither a constant index nor +** a local variable. +) +*/ static void freereg (FuncState *fs, int reg) { if (!ISK(reg) && reg >= fs->nactvar) { fs->freereg--; @@ -282,31 +391,58 @@ static void freereg (FuncState *fs, int reg) { } +/* +** Free register used by expression 'e' (if any) +*/ static void freeexp (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) freereg(fs, e->u.info); } +/* +** Free registers used by expressions 'e1' and 'e2' (if any) in proper +** order. +*/ +static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { + int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; + int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; + if (r1 > r2) { + freereg(fs, r1); + freereg(fs, r2); + } + else { + freereg(fs, r2); + freereg(fs, r1); + } +} + + +/* +** Add constant 'v' to prototype's list of constants (field 'k'). +** Use scanner's table to cache position of constants in constant list +** and try to reuse constants. Because some values should not be used +** as keys (nil cannot be a key, integer keys can collapse with float +** keys), the caller must provide a useful 'key' for indexing the cache. +*/ static int addk (FuncState *fs, TValue *key, TValue *v) { lua_State *L = fs->ls->L; - TValue *idx = luaH_set(L, fs->h, key); Proto *f = fs->f; + TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ int k, oldsize; - if (ttisnumber(idx)) { - lua_Number n = nvalue(idx); - lua_number2int(k, n); - if (luaV_rawequalobj(&f->k[k], v)) - return k; - /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0"); - go through and create a new entry for this value */ + if (ttisinteger(idx)) { /* is there an index there? */ + k = cast_int(ivalue(idx)); + /* correct value? (warning: must distinguish floats from integers!) */ + if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && + luaV_rawequalobj(&f->k[k], v)) + return k; /* reuse index */ } /* constant not found; create a new entry */ oldsize = f->sp->sizek; k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ - setnvalue(idx, cast_num(k)); + 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++]); setobj(L, &f->k[k], v); @@ -316,94 +452,134 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { } +/* +** Add a string to list of constants and return its index. +*/ int luaK_stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); + return addk(fs, &o, &o); /* use string itself as key */ } -int luaK_numberK (FuncState *fs, lua_Number r) { - int n; - lua_State *L = fs->ls->L; +/* +** Add an integer to list of constants and return its index. +** Integers use userdata as keys to avoid collision with floats with +** same value; conversion to 'void*' is used only for hashing, so there +** are no "precision" problems. +*/ +int luaK_intK (FuncState *fs, lua_Integer n) { + TValue k, o; + setpvalue(&k, cast(void*, cast(size_t, n))); + setivalue(&o, n); + return addk(fs, &k, &o); +} + +/* +** Add a float to list of constants and return its index. +*/ +static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o; - setnvalue(&o, r); - if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ - /* use raw representation as key to avoid numeric problems */ - setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); - n = addk(fs, L->top - 1, &o); - L->top--; - } - else - n = addk(fs, &o, &o); /* regular case */ - return n; + setfltvalue(&o, r); + return addk(fs, &o, &o); /* use number itself as key */ } +/* +** Add a boolean to list of constants and return its index. +*/ static int boolK (FuncState *fs, int b) { TValue o; setbvalue(&o, b); - return addk(fs, &o, &o); + return addk(fs, &o, &o); /* use boolean itself as key */ } +/* +** Add nil to list of constants and return its index. +*/ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->h); + sethvalue(fs->ls->L, &k, fs->ls->h); return addk(fs, &k, &v); } +/* +** Fix an expression to return the number of results 'nresults'. +** Either 'e' is a multi-ret expression (function call or vararg) +** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). +*/ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getcode(fs, e), nresults+1); + SETARG_C(getinstruction(fs, e), nresults + 1); } else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), nresults+1); - SETARG_A(getcode(fs, e), fs->freereg); + Instruction *pc = &getinstruction(fs, e); + SETARG_B(*pc, nresults + 1); + SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } + else lua_assert(nresults == LUA_MULTRET); } +/* +** Fix an expression to return one result. +** If expression is not a multi-ret expression (function call or +** vararg), it already returns one result, so nothing needs to be done. +** Function calls become VNONRELOC expressions (as its result comes +** fixed in the base register of the call), while vararg expressions +** become VRELOCABLE (as OP_VARARG puts its results where it wants). +** (Calls are created returning one result, so that does not need +** to be fixed.) +*/ void luaK_setoneret (FuncState *fs, expdesc *e) { if (e->k == VCALL) { /* expression is an open function call? */ - e->k = VNONRELOC; - e->u.info = GETARG_A(getcode(fs, e)); + /* already returns 1 value */ + lua_assert(GETARG_C(getinstruction(fs, e)) == 2); + e->k = VNONRELOC; /* result has fixed position */ + e->u.info = GETARG_A(getinstruction(fs, e)); } else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), 2); + SETARG_B(getinstruction(fs, e), 2); e->k = VRELOCABLE; /* can relocate its simple result */ } } +/* +** Ensure that expression 'e' is not a variable. +*/ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { - case VLOCAL: { - e->k = VNONRELOC; + case VLOCAL: { /* already in a register */ + e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } - case VUPVAL: { + case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); e->k = VRELOCABLE; break; } case VINDEXED: { - OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */ + OpCode op; freereg(fs, e->u.ind.idx); - if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */ + if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ freereg(fs, e->u.ind.t); op = OP_GETTABLE; } + else { + lua_assert(e->u.ind.vt == VUPVAL); + op = OP_GETTABUP; /* 't' is in an upvalue */ + } e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOCABLE; break; } - case VVARARG: - case VCALL: { + case VVARARG: case VCALL: { luaK_setoneret(fs, e); break; } @@ -412,12 +588,10 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { } -static int code_label (FuncState *fs, int A, int b, int jump) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); -} - - +/* +** Ensures expression value is in register 'reg' (and therefore +** 'e' will become a non-relocatable expression). +*/ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); switch (e->k) { @@ -433,13 +607,17 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_codek(fs, reg, e->u.info); break; } - case VKNUM: { + case VKFLT: { luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); break; } + case VKINT: { + luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + break; + } case VRELOCABLE: { - Instruction *pc = &getcode(fs, e); - SETARG_A(*pc, reg); + Instruction *pc = &getinstruction(fs, e); + SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ break; } case VNONRELOC: { @@ -448,7 +626,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } default: { - lua_assert(e->k == VVOID || e->k == VJMP); + lua_assert(e->k == VJMP); return; /* nothing to do... */ } } @@ -457,26 +635,55 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { } +/* +** Ensures expression value is in any register. +*/ static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { - luaK_reserveregs(fs, 1); - discharge2reg(fs, e, fs->freereg-1); + if (e->k != VNONRELOC) { /* no fixed register yet? */ + luaK_reserveregs(fs, 1); /* get a register */ + discharge2reg(fs, e, fs->freereg-1); /* put value there */ } } +static int code_loadbool (FuncState *fs, int A, int b, int jump) { + luaK_getlabel(fs); /* those instructions may be jump targets */ + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); +} + + +/* +** check whether list has any jump that do not produce a value +** or produce an inverted value +*/ +static int need_value (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) { + Instruction i = *getjumpcontrol(fs, list); + if (GET_OPCODE(i) != OP_TESTSET) return 1; + } + return 0; /* not found */ +} + + +/* +** Ensures final expression result (including results from its jump +** lists) is in register 'reg'. +** If expression has jumps, need to patch these jumps either to +** its final position or to "load" instructions (for those tests +** that do not produce values). +*/ static void exp2reg (FuncState *fs, expdesc *e, int reg) { discharge2reg(fs, e, reg); - if (e->k == VJMP) - luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */ + if (e->k == VJMP) /* expression itself is a test? */ + luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ if (hasjumps(e)) { int final; /* position after whole expression */ int p_f = NO_JUMP; /* position of an eventual LOAD false */ int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_label(fs, reg, 0, 1); - p_t = code_label(fs, reg, 1, 0); + p_f = code_loadbool(fs, reg, 0, 1); + p_t = code_loadbool(fs, reg, 1, 0); luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); @@ -489,6 +696,10 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { } +/* +** Ensures final expression result (including results from its jump +** lists) is in next available register. +*/ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); freeexp(fs, e); @@ -497,26 +708,39 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { } +/* +** Ensures final expression result (including results from its jump +** lists) is in some (any) register and return that register. +*/ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { - if (!hasjumps(e)) return e->u.info; /* exp is already in a register */ + if (e->k == VNONRELOC) { /* expression already has a register? */ + if (!hasjumps(e)) /* no jumps? */ + return e->u.info; /* result is already in a register */ if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.info); /* put value on it */ + exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } } - luaK_exp2nextreg(fs, e); /* default */ + luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ return e->u.info; } +/* +** Ensures final expression result is either in a register or in an +** upvalue. +*/ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { if (e->k != VUPVAL || hasjumps(e)) luaK_exp2anyreg(fs, e); } +/* +** Ensures final expression result is either in a register or it is +** a constant. +*/ void luaK_exp2val (FuncState *fs, expdesc *e) { if (hasjumps(e)) luaK_exp2anyreg(fs, e); @@ -525,29 +749,26 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { } +/* +** Ensures final expression result is in a valid R/K index +** (that is, it is either in a register or in 'k' with an index +** in the range of R/K indices). +** Returns R/K index. +*/ int luaK_exp2RK (FuncState *fs, expdesc *e) { luaK_exp2val(fs, e); - switch (e->k) { - case VTRUE: - case VFALSE: - case VNIL: { - if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */ - e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE)); - e->k = VK; - return RKASK(e->u.info); - } - else break; - } - case VKNUM: { - e->u.info = luaK_numberK(fs, e->u.nval); + 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; - /* go through */ - } - case VK: { - if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ + if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ return RKASK(e->u.info); else break; - } default: break; } /* not a constant in the right range: put it in a register */ @@ -555,11 +776,14 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { } +/* +** Generate code to store result of expression 'ex' into variable 'var'. +*/ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); + exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -573,29 +797,32 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); break; } - default: { - lua_assert(0); /* invalid var kind to store */ - break; - } + default: lua_assert(0); /* invalid var kind to store */ } freeexp(fs, ex); } +/* +** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). +*/ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { int ereg; luaK_exp2anyreg(fs, e); ereg = e->u.info; /* register where 'e' was placed */ freeexp(fs, e); e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; + e->k = VNONRELOC; /* self expression has a fixed register */ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); freeexp(fs, key); } -static void invertjump (FuncState *fs, expdesc *e) { +/* +** Negate condition 'e' (where 'e' is a comparison). +*/ +static void negatecondition (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->u.info); lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && GET_OPCODE(*pc) != OP_TEST); @@ -603,9 +830,15 @@ static void invertjump (FuncState *fs, expdesc *e) { } +/* +** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' +** is true, code will jump if 'e' is true.) Return jump position. +** Optimize when 'e' is 'not' something, inverting the condition +** and removing the 'not'. +*/ static int jumponcond (FuncState *fs, expdesc *e, int cond) { if (e->k == VRELOCABLE) { - Instruction ie = getcode(fs, e); + Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); @@ -618,36 +851,42 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { } +/* +** Emit code to go through if 'e' is true, jump otherwise. +*/ void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ + int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { - case VJMP: { - invertjump(fs, e); - pc = e->u.info; + case VJMP: { /* condition? */ + negatecondition(fs, e); /* jump when it is false */ + pc = e->u.info; /* save jump position */ break; } - case VK: case VKNUM: case VTRUE: { + case VK: case VKFLT: case VKINT: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } default: { - pc = jumponcond(fs, e, 0); + pc = jumponcond(fs, e, 0); /* jump when false */ break; } } - luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ - luaK_patchtohere(fs, e->t); + luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ + luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ e->t = NO_JUMP; } +/* +** Emit code to go through if 'e' is false, jump otherwise. +*/ void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ + int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { case VJMP: { - pc = e->u.info; + pc = e->u.info; /* already jump if true */ break; } case VNIL: case VFALSE: { @@ -655,29 +894,32 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { break; } default: { - pc = jumponcond(fs, e, 1); + pc = jumponcond(fs, e, 1); /* jump if true */ break; } } - luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ - luaK_patchtohere(fs, e->f); + luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ + luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ e->f = NO_JUMP; } +/* +** Code 'not e', doing constant folding. +*/ static void codenot (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); switch (e->k) { case VNIL: case VFALSE: { - e->k = VTRUE; + e->k = VTRUE; /* true == not nil == not false */ break; } - case VK: case VKNUM: case VTRUE: { - e->k = VFALSE; + case VK: case VKFLT: case VKINT: case VTRUE: { + e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } case VJMP: { - invertjump(fs, e); + negatecondition(fs, e); break; } case VRELOCABLE: @@ -688,118 +930,181 @@ static void codenot (FuncState *fs, expdesc *e) { e->k = VRELOCABLE; break; } - default: { - lua_assert(0); /* cannot happen */ - break; - } + default: lua_assert(0); /* cannot happen */ } /* interchange true and false lists */ { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); + removevalues(fs, e->f); /* values are useless when negated */ removevalues(fs, e->t); } +/* +** Create expression 't[k]'. 't' must have its final result already in a +** register or upvalue. +*/ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t)); - t->u.ind.t = t->u.info; - t->u.ind.idx = luaK_exp2RK(fs, k); - t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL - : check_exp(vkisinreg(t->k), VLOCAL); + lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); + t->u.ind.t = t->u.info; /* register or upvalue index */ + t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ + t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; t->k = VINDEXED; } -static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { - lua_Number r; - if (!isnumeral(e1) || !isnumeral(e2)) return 0; - if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0) - return 0; /* do not attempt to divide by 0 */ - r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval); - e1->u.nval = r; +/* +** Return false if folding can raise an error. +** Bitwise operations need operands convertible to integers; division +** operations cannot have 0 as divisor. +*/ +static int validop (int op, TValue *v1, TValue *v2) { + switch (op) { + case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: + case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ + lua_Integer i; + return (tointeger(v1, &i) && tointeger(v2, &i)); + } + case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ + return (nvalue(v2) != 0); + default: return 1; /* everything else is valid */ + } +} + + +/* +** Try to "constant-fold" an operation; return 1 iff successful. +** (In this case, 'e1' has the final result.) +*/ +static int constfolding (FuncState *fs, int op, expdesc *e1, + const expdesc *e2) { + TValue v1, v2, res; + if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) + return 0; /* non-numeric operands or not safe to fold */ + luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ + if (ttisinteger(&res)) { + e1->k = VKINT; + e1->u.ival = ivalue(&res); + } + else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ + lua_Number n = fltvalue(&res); + if (luai_numisnan(n) || n == 0) + return 0; + e1->k = VKFLT; + e1->u.nval = n; + } return 1; } -static void codearith (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - if (constfolding(op, e1, e2)) - return; - else { - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; - int o1 = luaK_exp2RK(fs, e1); - if (o1 > o2) { - freeexp(fs, e1); - freeexp(fs, e2); - } - else { - freeexp(fs, e2); - freeexp(fs, e1); - } - e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; - luaK_fixline(fs, line); - } +/* +** Emit code for unary expressions that "produce values" +** (everything but 'not'). +** Expression to produce final result will be encoded in 'e'. +*/ +static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { + int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ + freeexp(fs, e); + e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ + e->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); } -static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, - expdesc *e2) { - int o1 = luaK_exp2RK(fs, e1); - int o2 = luaK_exp2RK(fs, e2); - freeexp(fs, e2); - freeexp(fs, e1); - if (cond == 0 && op != OP_EQ) { - int temp; /* exchange args to replace by `<' or `<=' */ - temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ - cond = 1; +/* +** Emit code for binary expressions that "produce values" +** (everything but logical operators 'and'/'or' and comparison +** operators). +** Expression to produce final result will be encoded in 'e1'. +** 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); + freeexps(fs, e1, e2); + e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); +} + + +/* +** Emit code for comparisons. +** 'e1' was already put in R/K form by 'luaK_infix'. +*/ +static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { + int rk1 = (e1->k == VK) ? RKASK(e1->u.info) + : check_exp(e1->k == VNONRELOC, e1->u.info); + int rk2 = luaK_exp2RK(fs, e2); + freeexps(fs, e1, e2); + switch (opr) { + case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ + e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2); + break; + } + case OPR_GT: case OPR_GE: { + /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ + OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); + e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */ + break; + } + default: { /* '==', '<', '<=' use their own opcodes */ + OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); + e1->u.info = condjump(fs, op, 1, rk1, rk2); + break; + } } - e1->u.info = condjump(fs, op, cond, o1, o2); e1->k = VJMP; } +/* +** Aplly prefix operation 'op' to expression 'e'. +*/ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; + static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; switch (op) { - case OPR_MINUS: { - if (isnumeral(e)) /* minus constant? */ - e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */ - else { - luaK_exp2anyreg(fs, e); - codearith(fs, OP_UNM, e, &e2, line); - } + case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ + if (constfolding(fs, op + LUA_OPUNM, e, &ef)) + break; + /* FALLTHROUGH */ + case OPR_LEN: + codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); break; - } case OPR_NOT: codenot(fs, e); break; - case OPR_LEN: { - luaK_exp2anyreg(fs, e); /* cannot operate on constants */ - codearith(fs, OP_LEN, e, &e2, line); - break; - } default: lua_assert(0); } } +/* +** Process 1st operand 'v' of binary operation 'op' before reading +** 2nd operand. +*/ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { switch (op) { case OPR_AND: { - luaK_goiftrue(fs, v); + luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ break; } case OPR_OR: { - luaK_goiffalse(fs, v); + luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ break; } case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ + luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ break; } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); + case OPR_ADD: case OPR_SUB: + case OPR_MUL: case OPR_DIV: case OPR_IDIV: + case OPR_MOD: case OPR_POW: + case OPR_BAND: case OPR_BOR: case OPR_BXOR: + case OPR_SHL: case OPR_SHR: { + if (!tonumeral(v, NULL)) + luaK_exp2RK(fs, v); + /* else keep numeral, which may be folded with 2nd operand */ break; } default: { @@ -810,18 +1115,24 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } +/* +** Finalize code for binary operation, after reading 2nd operand. +** For '(a .. b .. c)' (which is '(a .. (b .. c))', because +** concatenation is right associative), merge second CONCAT into first +** one. +*/ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2, int line) { switch (op) { case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ + lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ luaK_dischargevars(fs, e2); luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ + lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ luaK_dischargevars(fs, e2); luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; @@ -829,29 +1140,30 @@ void luaK_posfix (FuncState *fs, BinOpr op, } case OPR_CONCAT: { luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1); + if (e2->k == VRELOCABLE && + GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { + lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); freeexp(fs, e1); - SETARG_B(getcode(fs, e2), e1->u.info); + SETARG_B(getinstruction(fs, e2), e1->u.info); e1->k = VRELOCABLE; e1->u.info = e2->u.info; } else { luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codearith(fs, OP_CONCAT, e1, e2, line); + codebinexpval(fs, OP_CONCAT, e1, e2, line); } break; } case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line); - break; - } - case OPR_EQ: case OPR_LT: case OPR_LE: { - codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2); + 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); break; } + case OPR_EQ: case OPR_LT: case OPR_LE: case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2); + codecomp(fs, op, e1, e2); break; } default: lua_assert(0); @@ -859,15 +1171,25 @@ void luaK_posfix (FuncState *fs, BinOpr op, } +/* +** Change line information associated with current position. +*/ void luaK_fixline (FuncState *fs, int line) { fs->f->sp->lineinfo[fs->pc - 1] = line; } +/* +** Emit a SETLIST instruction. +** 'base' is register that keeps table; +** 'nelems' is #table plus those to be stored now; +** 'tostore' is number of values (in registers 'base + 1',...) to add to +** table (or LUA_MULTRET to add up to stack top). +*/ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0); + lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); if (c <= MAXARG_C) luaK_codeABC(fs, OP_SETLIST, base, b, c); else if (c <= MAXARG_Ax) { diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 5a8e0b80..7a867434 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -24,7 +24,11 @@ ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, + OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, + OPR_DIV, + OPR_IDIV, + OPR_BAND, OPR_BOR, OPR_BXOR, + OPR_SHL, OPR_SHR, OPR_CONCAT, OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, @@ -33,10 +37,11 @@ typedef enum BinOpr { } BinOpr; -typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; +typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) +/* get (pointer to) instruction of given 'expdesc' */ +#define getinstruction(fs,e) ((fs)->f->sp->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) @@ -52,7 +57,7 @@ LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); +LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index ce4f6ad4..2303429e 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -1,22 +1,30 @@ /* -** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp $ ** Coroutine Library ** See Copyright Notice in lua.h */ - -#include - - #define lcorolib_c #define LUA_LIB +#include "lprefix.h" + + +#include + #include "lua.h" #include "lauxlib.h" #include "lualib.h" +static lua_State *getco (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "thread expected"); + return co; +} + + static int auxresume (lua_State *L, lua_State *co, int narg) { int status; if (!lua_checkstack(co, narg)) { @@ -47,9 +55,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { static int luaB_coresume (lua_State *L) { - lua_State *co = lua_tothread(L, 1); + lua_State *co = getco(L); int r; - luaL_argcheck(L, co, 1, "coroutine expected"); r = auxresume(L, co, lua_gettop(L) - 1); if (r < 0) { lua_pushboolean(L, 0); @@ -59,7 +66,7 @@ static int luaB_coresume (lua_State *L) { else { lua_pushboolean(L, 1); lua_insert(L, -(r + 1)); - return r + 1; /* return true + `resume' returns */ + return r + 1; /* return true + 'resume' returns */ } } @@ -68,7 +75,7 @@ static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { - if (lua_isstring(L, -1)) { /* error object is a string? */ + if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info */ lua_insert(L, -2); lua_concat(L, 2); @@ -102,8 +109,7 @@ static int luaB_yield (lua_State *L) { static int luaB_costatus (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "coroutine expected"); + lua_State *co = getco(L); if (L == co) lua_pushliteral(L, "running"); else { switch (lua_status(co)) { @@ -129,6 +135,12 @@ static int luaB_costatus (lua_State *L) { } +static int luaB_yieldable (lua_State *L) { + lua_pushboolean(L, lua_isyieldable(L)); + return 1; +} + + static int luaB_corunning (lua_State *L) { int ismain = lua_pushthread(L); lua_pushboolean(L, ismain); @@ -143,6 +155,7 @@ static const luaL_Reg co_funcs[] = { {"status", luaB_costatus}, {"wrap", luaB_cowrap}, {"yield", luaB_yield}, + {"isyieldable", luaB_yieldable}, {NULL, NULL} }; diff --git a/3rd/lua/lctype.c b/3rd/lua/lctype.c index 93f8cadc..ae9367e6 100644 --- a/3rd/lua/lctype.c +++ b/3rd/lua/lctype.c @@ -1,5 +1,5 @@ /* -** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ @@ -7,6 +7,9 @@ #define lctype_c #define LUA_CORE +#include "lprefix.h" + + #include "lctype.h" #if !LUA_USE_CTYPE /* { */ diff --git a/3rd/lua/lctype.h b/3rd/lua/lctype.h index b09b21a3..99c7d122 100644 --- a/3rd/lua/lctype.h +++ b/3rd/lua/lctype.h @@ -1,5 +1,5 @@ /* -** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 84fe3c7d..786f6cd9 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,26 +1,42 @@ /* -** $Id: ldblib.c,v 1.132.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ +#define ldblib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include #include -#define ldblib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#define HOOKKEY "_HKEY" +/* +** The hook table at registry[&HOOKKEY] maps threads to their current +** hook function. (We only need the unique address of 'HOOKKEY'.) +*/ +static const int HOOKKEY = 0; +/* +** If L1 != L, L1 can be in any state, and therefore there are no +** guarantees about its stack space; any push in L1 must be +** checked. +*/ +static void checkstack (lua_State *L, lua_State *L1, int n) { + if (L != L1 && !lua_checkstack(L1, n)) + luaL_error(L, "stack overflow"); +} + static int db_getregistry (lua_State *L) { lua_pushvalue(L, LUA_REGISTRYINDEX); @@ -57,35 +73,20 @@ static int db_getuservalue (lua_State *L) { static int db_setuservalue (lua_State *L) { - if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) - luaL_argerror(L, 1, "full userdata expected, got light userdata"); luaL_checktype(L, 1, LUA_TUSERDATA); - if (!lua_isnoneornil(L, 2)) - luaL_checktype(L, 2, LUA_TTABLE); + luaL_checkany(L, 2); lua_settop(L, 2); lua_setuservalue(L, 1); return 1; } -static void settabss (lua_State *L, const char *i, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsi (lua_State *L, const char *i, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsb (lua_State *L, const char *i, int v) { - lua_pushboolean(L, v); - lua_setfield(L, -2, i); -} - - +/* +** Auxiliary function used by several library functions: check for +** an optional thread as function's first argument and set 'arg' with +** 1 if this argument is present (so that functions can skip it to +** access their other arguments) +*/ static lua_State *getthread (lua_State *L, int *arg) { if (lua_isthread(L, 1)) { *arg = 1; @@ -93,44 +94,74 @@ static lua_State *getthread (lua_State *L, int *arg) { } else { *arg = 0; - return L; + return L; /* function will operate over current thread */ } } +/* +** Variations of 'lua_settable', used by 'db_getinfo' to put results +** from 'lua_getinfo' into result table. Key is always a string; +** value can be a string, an int, or a boolean. +*/ +static void settabss (lua_State *L, const char *k, const char *v) { + lua_pushstring(L, v); + lua_setfield(L, -2, k); +} + +static void settabsi (lua_State *L, const char *k, int v) { + lua_pushinteger(L, v); + lua_setfield(L, -2, k); +} + +static void settabsb (lua_State *L, const char *k, int v) { + lua_pushboolean(L, v); + lua_setfield(L, -2, k); +} + + +/* +** In function 'db_getinfo', the call to 'lua_getinfo' may push +** results on the stack; later it creates the result table to put +** these objects. Function 'treatstackoption' puts the result from +** 'lua_getinfo' on top of the result table so that it can call +** 'lua_setfield'. +*/ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } + if (L == L1) + lua_rotate(L, -2, 1); /* exchange object and table */ else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); + lua_xmove(L1, L, 1); /* move object to the "main" stack */ + lua_setfield(L, -2, fname); /* put object into table */ } +/* +** Calls 'lua_getinfo' and collects all results in a new table. +** L1 needs stack space for an optional input (function) plus +** two optional outputs (function and line table) from function +** 'lua_getinfo'. +*/ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnStu"); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { + checkstack(L, L1, 3); + if (lua_isfunction(L, arg + 1)) { /* info about a function? */ + options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ + lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ + lua_xmove(L, L1, 1); + } + else { /* stack level */ + if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } - else if (lua_isfunction(L, arg+1)) { - lua_pushfstring(L, ">%s", options); - options = lua_tostring(L, -1); - lua_pushvalue(L, arg+1); - lua_xmove(L, L1, 1); - } - else - return luaL_argerror(L, arg+1, "function or level expected"); if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); - lua_createtable(L, 0, 2); + lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); @@ -164,20 +195,22 @@ static int db_getlocal (lua_State *L) { lua_State *L1 = getthread(L, &arg); lua_Debug ar; const char *name; - int nvar = luaL_checkint(L, arg+2); /* local-variable index */ + int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ if (lua_isfunction(L, arg + 1)) { /* function argument? */ lua_pushvalue(L, arg + 1); /* push function */ lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ - return 1; + return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + int level = (int)luaL_checkinteger(L, arg + 1); + if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); + checkstack(L, L1, 1); name = lua_getlocal(L1, &ar, nvar); if (name) { - lua_xmove(L1, L, 1); /* push local value */ + lua_xmove(L1, L, 1); /* move local value */ lua_pushstring(L, name); /* push name */ - lua_pushvalue(L, -2); /* re-order */ + lua_rotate(L, -2, 1); /* re-order */ return 2; } else { @@ -190,26 +223,36 @@ static int db_getlocal (lua_State *L) { static int db_setlocal (lua_State *L) { int arg; + const char *name; lua_State *L1 = getthread(L, &arg); lua_Debug ar; - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + int level = (int)luaL_checkinteger(L, arg + 1); + int nvar = (int)luaL_checkinteger(L, arg + 2); + if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); + checkstack(L, L1, 1); lua_xmove(L, L1, 1); - lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); + name = lua_setlocal(L1, &ar, nvar); + if (name == NULL) + lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ + lua_pushstring(L, name); return 1; } +/* +** get (if 'get' is true) or set an upvalue from a closure +*/ static int auxupvalue (lua_State *L, int get) { const char *name; - int n = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); + int n = (int)luaL_checkinteger(L, 2); /* upvalue index */ + luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); if (name == NULL) return 0; lua_pushstring(L, name); - lua_insert(L, -(get+1)); + lua_insert(L, -(get+1)); /* no-op if get is false */ return get + 1; } @@ -225,13 +268,15 @@ static int db_setupvalue (lua_State *L) { } +/* +** Check whether a given upvalue from a given closure exists and +** returns its index +*/ static int checkupval (lua_State *L, int argf, int argnup) { - lua_Debug ar; - int nup = luaL_checkint(L, argnup); - luaL_checktype(L, argf, LUA_TFUNCTION); - lua_pushvalue(L, argf); - lua_getinfo(L, ">u", &ar); - luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); + int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ + luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ + luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, + "invalid upvalue index"); return nup; } @@ -253,26 +298,29 @@ static int db_upvaluejoin (lua_State *L) { } -#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) - - +/* +** Call hook function registered at hook table for the current +** thread (if there is one) +*/ static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; - gethooktable(L); + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_pushthread(L); - lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); + if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ + lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); + lua_pushinteger(L, ar->currentline); /* push current line */ else lua_pushnil(L); lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); + lua_call(L, 2, 0); /* call hook function */ } } +/* +** Convert a string mask (for 'sethook') into a bit mask +*/ static int makemask (const char *smask, int count) { int mask = 0; if (strchr(smask, 'c')) mask |= LUA_MASKCALL; @@ -283,6 +331,9 @@ static int makemask (const char *smask, int count) { } +/* +** Convert a bit mask (for 'gethook') into a string mask +*/ static char *unmakemask (int mask, char *smask) { int i = 0; if (mask & LUA_MASKCALL) smask[i++] = 'c'; @@ -297,26 +348,30 @@ static int db_sethook (lua_State *L) { int arg, mask, count; lua_Hook func; lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { + if (lua_isnoneornil(L, arg+1)) { /* no hook? */ lua_settop(L, arg+1); func = NULL; mask = 0; count = 0; /* turn off hooks */ } else { const char *smask = luaL_checkstring(L, arg+2); luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); + count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } - if (gethooktable(L) == 0) { /* creating hook table? */ + if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { + lua_createtable(L, 0, 2); /* create a hook table */ + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ lua_pushstring(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ } - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_pushvalue(L, arg+1); - lua_rawset(L, -3); /* set new hook */ - lua_sethook(L1, func, mask, count); /* set hooks */ + checkstack(L, L1, 1); + lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ + lua_pushvalue(L, arg + 1); /* value (hook function) */ + lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ + lua_sethook(L1, func, mask, count); return 0; } @@ -327,16 +382,19 @@ static int db_gethook (lua_State *L) { char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); - if (hook != NULL && hook != hookf) /* external hook? */ + if (hook == NULL) /* no hook? */ + lua_pushnil(L); + else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); - else { - gethooktable(L); + else { /* hook table must exist */ + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_rawget(L, -2); /* get hook */ + lua_rawget(L, -2); /* 1st result = hooktable[L1] */ lua_remove(L, -2); /* remove hook table */ } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); + lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ + lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ return 3; } @@ -344,13 +402,13 @@ static int db_gethook (lua_State *L) { static int db_debug (lua_State *L) { for (;;) { char buffer[250]; - luai_writestringerror("%s", "lua_debug> "); + lua_writestringerror("%s", "lua_debug> "); if (fgets(buffer, sizeof(buffer), stdin) == 0 || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) - luai_writestringerror("%s\n", lua_tostring(L, -1)); + lua_writestringerror("%s\n", lua_tostring(L, -1)); lua_settop(L, 0); /* remove eventual returns */ } } @@ -363,7 +421,7 @@ static int db_traceback (lua_State *L) { if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ lua_pushvalue(L, arg + 1); /* return it untouched */ else { - int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); + int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0); luaL_traceback(L, L1, msg, level); } return 1; diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 9097f671..3e684b0b 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,18 +1,19 @@ /* -** $Id: ldebug.c,v 2.90.1.3 2013/05/16 16:04:15 roberto Exp $ +** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ +#define ldebug_c +#define LUA_CORE + +#include "lprefix.h" + #include #include #include - -#define ldebug_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -33,7 +34,12 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue((ci)->func)) + + +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, + const char **name); static int currentpc (CallInfo *ci) { @@ -48,9 +54,31 @@ static int currentline (CallInfo *ci) { /* -** this function can be called asynchronous (e.g. during a signal) +** 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. */ -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { +static void swapextra (lua_State *L) { + if (L->status == LUA_YIELD) { + CallInfo *ci = L->ci; /* get function that yielded */ + StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ + ci->func = restorestack(L, ci->extra); + ci->extra = savestack(L, temp); + } +} + + +/* +** This function can be called asynchronously (e.g. during a signal). +** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by +** 'resethookcount') are for debug only, and it is no problem if they +** get arbitrary values (causes at most one wrong hook call). 'hookmask' +** is an atomic value. We assume that pointers are atomic too (e.g., gcc +** ensures that for all platforms where it runs). Moreover, 'hook' is +** always checked before being called (see 'luaD_hook'). +*/ +LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; @@ -61,7 +89,6 @@ LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); - return 1; } @@ -106,7 +133,7 @@ static const char *upvalname (Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { int nparams = clLvalue(ci->func)->p->sp->numparams; - if (n >= ci->u.l.base - ci->func - nparams) + if (n >= cast_int(ci->u.l.base - ci->func) - nparams) return NULL; /* no such vararg */ else { *pos = ci->func + nparams + n; @@ -144,6 +171,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); + swapextra(L); if (ar == NULL) { /* information about non-active function? */ if (!isLfunction(L->top - 1)) /* not a Lua function? */ name = NULL; @@ -151,25 +179,30 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); } else { /* active function; get information through 'ar' */ - StkId pos = 0; /* to avoid warnings */ + StkId pos = NULL; /* to avoid warnings */ name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobj2s(L, L->top, pos); api_incr_top(L); } } + swapextra(L); lua_unlock(L); return name; } LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = 0; /* to avoid warnings */ - const char *name = findlocal(L, ar->i_ci, n, &pos); + StkId pos = NULL; /* to avoid warnings */ + const char *name; lua_lock(L); - if (name) + swapextra(L); + name = findlocal(L, ar->i_ci, n, &pos); + if (name) { setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ + L->top--; /* pop value */ + } + swapextra(L); lua_unlock(L); return name; } @@ -212,6 +245,20 @@ static void collectvalidlines (lua_State *L, Closure *f) { } +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { + if (ci == NULL) /* no 'ci'? */ + return NULL; /* no info */ + else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ + *name = "__gc"; + return "metamethod"; /* report it as such */ + } + /* calling function is a known Lua function? */ + else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) + return funcnamefromcode(L, ci->previous, name); + else return NULL; /* no way to find a name */ +} + + static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, Closure *f, CallInfo *ci) { int status = 1; @@ -242,11 +289,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'n': { - /* calling function is a known Lua function? */ - if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - ar->namewhat = getfuncname(L, ci->previous, &ar->name); - else - ar->namewhat = NULL; + ar->namewhat = getfuncname(L, ci, &ar->name); if (ar->namewhat == NULL) { ar->namewhat = ""; /* not found */ ar->name = NULL; @@ -269,6 +312,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { CallInfo *ci; StkId func; lua_lock(L); + swapextra(L); if (*what == '>') { ci = NULL; func = L->top - 1; @@ -287,6 +331,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { setobjs2s(L, L->top, func); api_incr_top(L); } + swapextra(L); /* correct before option 'L', which can raise a mem. error */ if (strchr(what, 'L')) collectvalidlines(L, cl); lua_unlock(L); @@ -366,18 +411,13 @@ static int findsetreg (Proto *p, int lastpc, int reg) { case OP_JMP: { int b = GETARG_sBx(i); int dest = pc + 1 + b; - /* jump is forward and do not skip `lastpc'? */ + /* jump is forward and do not skip 'lastpc'? */ if (pc < dest && dest <= lastpc) { if (dest > jmptarget) jmptarget = dest; /* update 'jmptarget' */ } break; } - case OP_TEST: { - if (reg == a) /* jumped code can change 'a' */ - setreg = filterpc(pc, jmptarget); - break; - } default: if (testAMode(op) && reg == a) /* any instruction that set A */ setreg = filterpc(pc, jmptarget); @@ -442,39 +482,53 @@ static const char *getobjname (Proto *p, int lastpc, int reg, } -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - TMS tm; +/* +** Try to find a name for a function based on the code that called it. +** (Only works when function was called by a Lua function.) +** Returns what the name is (e.g., "for iterator", "method", +** "metamethod") and sets '*name' to point to the name. +*/ +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, + const char **name) { + TMS tm = (TMS)0; /* (initial value avoids warnings) */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->sp->code[pc]; /* calling instruction */ + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + *name = "?"; + return "hook"; + } switch (GET_OPCODE(i)) { case OP_CALL: - case OP_TAILCALL: /* get function name */ - return getobjname(p, pc, GETARG_A(i), name); + case OP_TAILCALL: + return getobjname(p, pc, GETARG_A(i), name); /* get function name */ case OP_TFORCALL: { /* for iterator */ *name = "for iterator"; return "for iterator"; } - /* all other instructions can call only through metamethods */ - case OP_SELF: - case OP_GETTABUP: - case OP_GETTABLE: tm = TM_INDEX; break; - case OP_SETTABUP: - case OP_SETTABLE: tm = TM_NEWINDEX; break; - case OP_EQ: tm = TM_EQ; break; - case OP_ADD: tm = TM_ADD; break; - case OP_SUB: tm = TM_SUB; break; - case OP_MUL: tm = TM_MUL; break; - case OP_DIV: tm = TM_DIV; break; - case OP_MOD: tm = TM_MOD; break; - case OP_POW: tm = TM_POW; break; + /* other instructions can do calls through metamethods */ + case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: + tm = TM_INDEX; + break; + case OP_SETTABUP: case OP_SETTABLE: + tm = TM_NEWINDEX; + break; + case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: + case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: + case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { + int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */ + tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */ + break; + } case OP_UNM: tm = TM_UNM; break; + case OP_BNOT: tm = TM_BNOT; break; case OP_LEN: tm = TM_LEN; break; + case OP_CONCAT: tm = TM_CONCAT; break; + case OP_EQ: tm = TM_EQ; break; case OP_LT: tm = TM_LT; break; case OP_LE: tm = TM_LE; break; - case OP_CONCAT: tm = TM_CONCAT; break; default: - return NULL; /* else no useful name can be found */ + return NULL; /* cannot find a reasonable name */ } *name = getstr(G(L)->tmname[tm]); return "metamethod"; @@ -485,17 +539,21 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { /* -** only ANSI way to check whether a pointer points to an array -** (used only for error messages, so efficiency is not a big concern) +** The subtraction of two potentially unrelated pointers is +** not ISO C, but it should not crash a program; the subsequent +** checks are ISO C and ensure a correct result. */ static int isinstack (CallInfo *ci, const TValue *o) { - StkId p; - for (p = ci->u.l.base; p < ci->top; p++) - if (o == p) return 1; - return 0; + ptrdiff_t i = o - ci->u.l.base; + return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); } +/* +** Checks whether value 'o' came from an upvalue. (That can only happen +** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on +** upvalues.) +*/ static const char *getupvalname (CallInfo *ci, const TValue *o, const char **name) { LClosure *c = ci_func(ci); @@ -510,10 +568,9 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, } -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { +static const char *varinfo (lua_State *L, const TValue *o) { + const char *name = NULL; /* to avoid warnings */ CallInfo *ci = L->ci; - const char *name = NULL; - const char *t = objtypename(o); const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ @@ -521,73 +578,121 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { kind = getobjname(ci_func(ci)->p, currentpc(ci), cast_int(o - ci->u.l.base), &name); } - if (kind) - luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", - op, kind, name, t); - else - luaG_runerror(L, "attempt to %s a %s value", op, t); + return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } -l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; - lua_assert(!ttisstring(p1) && !ttisnumber(p1)); +l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + const char *t = luaT_objtypename(L, o); + luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); +} + + +l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { + if (ttisstring(p1) || cvt2str(p1)) p1 = p2; luaG_typeerror(L, p1, "concatenate"); } -l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { - TValue temp; - if (luaV_tonumber(p1, &temp) == NULL) - p2 = p1; /* first operand is wrong */ - luaG_typeerror(L, p2, "perform arithmetic on"); +l_noret luaG_opinterror (lua_State *L, const TValue *p1, + const TValue *p2, const char *msg) { + lua_Number temp; + if (!tonumber(p1, &temp)) /* first operand is wrong? */ + p2 = p1; /* now second is wrong */ + luaG_typeerror(L, p2, msg); +} + + +/* +** Error when both values are convertible to numbers, but not to integers +*/ +l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { + lua_Integer temp; + if (!tointeger(p1, &temp)) + p2 = p1; + luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = objtypename(p1); - const char *t2 = objtypename(p2); - if (t1 == t2) + const char *t1 = luaT_objtypename(L, p1); + const char *t2 = luaT_objtypename(L, p2); + if (strcmp(t1, t2) == 0) luaG_runerror(L, "attempt to compare two %s values", t1); else luaG_runerror(L, "attempt to compare %s with %s", t1, t2); } -static void addinfo (lua_State *L, const char *msg) { - CallInfo *ci = L->ci; - if (isLua(ci)) { /* is Lua code? */ - char buff[LUA_IDSIZE]; /* add file:line information */ - int line = currentline(ci); - TString *src = ci_func(ci)->p->sp->source; - if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); +/* add src:line information to 'msg' */ +const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, + int line) { + char buff[LUA_IDSIZE]; + if (src) + luaO_chunkid(buff, getstr(src), LUA_IDSIZE); + else { /* no source available; use "?" instead */ + buff[0] = '?'; buff[1] = '\0'; } + return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); - if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; - luaD_call(L, L->top - 2, 1, 0); /* call it */ + L->top++; /* assume EXTRA_STACK */ + luaD_callnoyield(L, L->top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); } l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { + CallInfo *ci = L->ci; + const char *msg; va_list argp; va_start(argp, fmt); - addinfo(L, luaO_pushvfstring(L, fmt, argp)); + msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); + if (isLua(ci)) /* if Lua function, add source:line information */ + luaG_addinfo(L, msg, ci_func(ci)->p->sp->source, currentline(ci)); luaG_errormsg(L); } + +void luaG_traceexec (lua_State *L) { + CallInfo *ci = L->ci; + lu_byte mask = L->hookmask; + int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + if (counthook) + resethookcount(L); /* reset count */ + else if (!(mask & LUA_MASKLINE)) + return; /* no line hook and count != 0; nothing to be done */ + if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ + return; /* do not call hook again (VM yielded, so it did not move) */ + } + if (counthook) + luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ + if (mask & LUA_MASKLINE) { + Proto *p = ci_func(ci)->p; + int npc = pcRel(ci->u.l.savedpc, p); + int newline = getfuncline(p, npc); + if (npc == 0 || /* call linehook when enter a new function, */ + ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ + newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ + luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ + } + L->oldpc = ci->u.l.savedpc; + if (L->status == LUA_YIELD) { /* did hook yield? */ + if (counthook) + L->hookcount = 1; /* undo decrement to zero */ + ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ + ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ + ci->func = L->top - 1; /* protect stack below results */ + luaD_throw(L, LUA_YIELD); + } +} + diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 04d646df..0579311b 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -13,22 +13,27 @@ #define pcRel(pc, p) (cast(int, (pc) - (p)->sp->code) - 1) -#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : 0) +#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) - LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); -LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); -LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, +LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, + const TValue *p2, + const char *msg); +LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); +LUAI_FUNC 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); + #endif diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index ee5509f6..955af4ef 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,17 +1,19 @@ /* -** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $ +** $Id: ldo.c,v 2.157 2016/12/13 15:52:21 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ +#define ldo_c +#define LUA_CORE + +#include "lprefix.h" + #include #include #include -#define ldo_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -33,6 +35,8 @@ +#define errorstatus(s) ((s) > LUA_YIELD) + /* ** {====================================================== @@ -46,30 +50,33 @@ ** C++ code, with _longjmp/_setjmp when asked to use them, and with ** longjmp/setjmp otherwise. */ -#if !defined(LUAI_THROW) +#if !defined(LUAI_THROW) /* { */ + +#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) #define LUAI_TRY(L,c,a) \ try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } #define luai_jmpbuf int /* dummy variable */ -#elif defined(LUA_USE_ULONGJMP) -/* in Unix, try _longjmp/_setjmp (more efficient) */ +#elif defined(LUA_USE_POSIX) /* }{ */ + +/* in POSIX, try _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } #define luai_jmpbuf jmp_buf -#else -/* default handling with long jumps */ +#else /* }{ */ + +/* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } #define luai_jmpbuf jmp_buf -#endif +#endif /* } */ -#endif +#endif /* } */ @@ -106,15 +113,19 @@ l_noret luaD_throw (lua_State *L, int errcode) { LUAI_THROW(L, L->errorJmp); /* jump to it */ } else { /* thread has no error handler */ + global_State *g = G(L); L->status = cast_byte(errcode); /* mark it as dead */ - if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ + if (g->mainthread->errorJmp) { /* main thread has a handler? */ + setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ - if (G(L)->panic) { /* panic function? */ + if (g->panic) { /* panic function? */ + seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ + if (L->ci->top < L->top) + L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); - G(L)->panic(L); /* call it (last chance to jump out) */ + g->panic(L); /* call panic function (last chance to jump out) */ } abort(); } @@ -139,12 +150,17 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { /* }====================================================== */ +/* +** {================================================================== +** Stack reallocation +** =================================================================== +*/ static void correctstack (lua_State *L, TValue *oldstack) { CallInfo *ci; - GCObject *up; + UpVal *up; L->top = (L->top - oldstack) + L->stack; - for (up = L->openupval; up != NULL; up = up->gch.next) - gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v = (up->v - oldstack) + L->stack; for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; @@ -195,9 +211,9 @@ static int stackinuse (lua_State *L) { CallInfo *ci; StkId lim = L->top; for (ci = L->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L->stack_last); if (lim < ci->top) lim = ci->top; } + lua_assert(lim <= L->stack_last); return cast_int(lim - L->stack) + 1; /* part of stack in use */ } @@ -205,18 +221,38 @@ static int stackinuse (lua_State *L) { void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; - if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; - if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ - goodsize >= L->stacksize) /* would grow instead of shrink? */ - condmovestack(L); /* don't change stack (change only for debugging) */ + 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 - luaD_reallocstack(L, goodsize); /* shrink it */ + 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); + else /* don't change stack */ + condmovestack(L,{},{}); /* (change only for debugging) */ } +void luaD_inctop (lua_State *L) { + luaD_checkstack(L, 1); + L->top++; +} + +/* }================================================================== */ + + +/* +** Call a hook for the given event. Make sure there is a hook to be +** called. (Both 'L->hook' and 'L->hookmask', which triggers this +** function, can be changed asynchronously by signals.) +*/ void luaD_hook (lua_State *L, int event, int line) { lua_Hook hook = L->hook; - if (hook && L->allowhook) { + if (hook && L->allowhook) { /* make sure there is a hook */ CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); ptrdiff_t ci_top = savestack(L, ci->top); @@ -258,111 +294,88 @@ static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; - lua_assert(actual >= nfixargs); /* move fixed parameters to final position */ - luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ fixed = L->top - actual; /* first fixed argument */ base = L->top; /* final position of first argument */ - for (i=0; itop++, fixed + i); - setnilvalue(fixed + i); + setnilvalue(fixed + i); /* erase original copy (for GC) */ } + for (; i < nfixargs; i++) + setnilvalue(L->top++); /* complete missing arguments */ return base; } -static StkId tryfuncTM (lua_State *L, StkId func) { +/* +** Check whether __call metafield of 'func' is a function. If so, put +** it in stack below original 'func' so that 'luaD_precall' can call +** it. Raise an error if __call metafield is not a function. +*/ +static void tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); StkId p; - ptrdiff_t funcr = savestack(L, func); if (!ttisfunction(tm)) luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at `func' */ - for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - incr_top(L); - func = restorestack(L, funcr); /* previous call may change stack */ + /* Open a hole inside the stack at 'func' */ + for (p = L->top; p > func; p--) + setobjs2s(L, p, p-1); + L->top++; /* slot ensured by caller */ setobj2s(L, func, tm); /* tag method is the new function to be called */ - return func; } - -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) - - /* -** returns true if function has been executed (C function) +** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. +** Handle most typical cases (zero results for commands, one result for +** expressions, multiple results for tail calls/single parameters) +** separated. */ -int luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - CallInfo *ci; - int n; /* number of arguments (Lua) or returns (C) */ - ptrdiff_t funcr = savestack(L, func); - switch (ttype(func)) { - case LUA_TLCF: /* light C function */ - f = fvalue(func); - goto Cfunc; - case LUA_TCCL: { /* C closure */ - f = clCvalue(func)->f; - Cfunc: - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = restorestack(L, funcr); - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; - luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, L->top - n); - return 1; +static int moveresults (lua_State *L, const TValue *firstResult, StkId res, + int nres, int wanted) { + switch (wanted) { /* handle typical cases separately */ + case 0: break; /* nothing to move */ + case 1: { /* one result needed */ + if (nres == 0) /* no results? */ + firstResult = luaO_nilobject; /* adjust with nil */ + setobjs2s(L, res, firstResult); /* move it to proper place */ + break; } - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - SharedProto *p = clLvalue(func)->p->sp; - n = cast_int(L->top - func) - 1; /* number of real arguments */ - luaD_checkstack(L, p->maxstacksize); - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - if (!p->is_vararg) { - func = restorestack(L, funcr); - base = func + 1; - } - else { - base = adjust_varargs(L, p, n); - func = restorestack(L, funcr); /* previous call can change stack */ - } - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->u.l.base = base; - ci->top = base + p->maxstacksize; - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - L->top = ci->top; - luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; + 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: { /* not a function */ - func = tryfuncTM(L, func); /* retry with 'function' tag method */ - return luaD_precall(L, func, nresults); /* now it must be a function */ + default: { + int i; + if (wanted <= nres) { /* enough results? */ + for (i = 0; i < wanted; i++) /* move wanted results to correct place */ + setobjs2s(L, res + i, firstResult + i); + } + else { /* not enough results; use all of them plus nils */ + for (i = 0; i < nres; i++) /* move all results to correct place */ + setobjs2s(L, res + i, firstResult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(res + i); + } + break; } } + L->top = res + wanted; /* top points after the last result */ + return 1; } -int luaD_poscall (lua_State *L, StkId firstResult) { +/* +** Finishes a function call: calls hook if necessary, removes CallInfo, +** moves current number of results to proper place; returns 0 iff call +** wanted multiple (variable number of) results. +*/ +int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { StkId res; - int wanted, i; - CallInfo *ci = L->ci; + int wanted = ci->nresults; if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { if (L->hookmask & LUA_MASKRET) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ @@ -372,15 +385,104 @@ int luaD_poscall (lua_State *L, StkId firstResult) { L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ } res = ci->func; /* res == final position of 1st result */ - wanted = ci->nresults; - L->ci = ci = ci->previous; /* back to caller */ - /* move results to correct place */ - for (i = wanted; i != 0 && firstResult < L->top; i--) - setobjs2s(L, res++, firstResult++); - while (i-- > 0) - setnilvalue(res++); - L->top = res; - return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ + L->ci = ci->previous; /* back to caller */ + /* move results to proper place */ + return moveresults(L, firstResult, res, nres, wanted); +} + + + +#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) + + +/* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ + luaC_checkGC(L), /* stack grow uses memory */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + +/* +** Prepares a function call: checks the stack, creates a new CallInfo +** entry, fills in the relevant information, calls hook if needed. +** If function is a C function, does the call, too. (Otherwise, leave +** the execution ('luaV_execute') to the caller, to allow stackless +** calls.) Returns true iff function has been executed (C function). +*/ +int luaD_precall (lua_State *L, StkId func, int nresults) { + lua_CFunction f; + CallInfo *ci; + switch (ttype(func)) { + case LUA_TCCL: /* C closure */ + f = clCvalue(func)->f; + goto Cfunc; + case LUA_TLCF: /* light C function */ + f = fvalue(func); + Cfunc: { + int n; /* number of returns */ + checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); + ci->callstatus = 0; + if (L->hookmask & LUA_MASKCALL) + luaD_hook(L, LUA_HOOKCALL, -1); + lua_unlock(L); + n = (*f)(L); /* do the actual call */ + lua_lock(L); + api_checknelems(L, n); + luaD_poscall(L, ci, L->top - n, n); + return 1; + } + case LUA_TLCL: { /* Lua function: prepare its call */ + StkId base; + SharedProto *p = clLvalue(func)->p->sp; + int n = cast_int(L->top - func) - 1; /* number of real arguments */ + int fsize = p->maxstacksize; /* frame size */ + checkstackp(L, fsize, func); + if (p->is_vararg) + base = adjust_varargs(L, p, n); + else { /* non vararg function */ + for (; n < p->numparams; n++) + setnilvalue(L->top++); /* complete missing arguments */ + base = func + 1; + } + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + ci->u.l.base = base; + L->top = ci->top = base + fsize; + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus = CIST_LUA; + if (L->hookmask & LUA_MASKCALL) + callhook(L, ci); + return 0; + } + default: { /* not a function */ + checkstackp(L, 1, func); /* ensure space for metamethod */ + tryfuncTM(L, func); /* try to get '__call' metamethod */ + return luaD_precall(L, func, nresults); /* now it must be a function */ + } + } +} + + +/* +** Check appropriate error for stack overflow ("regular" overflow or +** overflow while handling stack overflow). If 'nCalls' is larger than +** 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 */ } @@ -390,53 +492,65 @@ int luaD_poscall (lua_State *L, StkId firstResult) { ** When returns, all the results are on the stack, starting at the original ** function position. */ -void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ - } - if (!allowyield) L->nny++; +void luaD_call (lua_State *L, StkId func, int nResults) { + if (++L->nCcalls >= LUAI_MAXCCALLS) + stackerror(L); if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ luaV_execute(L); /* call it */ - if (!allowyield) L->nny--; L->nCcalls--; } -static void finishCcall (lua_State *L) { - CallInfo *ci = L->ci; - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ - L->errfunc = ci->u.c.old_errfunc; - } - /* finish 'lua_callk'/'lua_pcall' */ - adjustresults(L, ci->nresults); - /* call continuation function */ - if (!(ci->callstatus & CIST_STAT)) /* no call status? */ - ci->u.c.status = LUA_YIELD; /* 'default' status */ - lua_assert(ci->u.c.status != LUA_OK); - ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; - lua_unlock(L); - n = (*ci->u.c.k)(L); - lua_lock(L); - api_checknelems(L, n); - /* finish 'luaD_precall' */ - luaD_poscall(L, L->top - n); +/* +** Similar to 'luaD_call', but does not allow yields during the call +*/ +void luaD_callnoyield (lua_State *L, StkId func, int nResults) { + L->nny++; + luaD_call(L, func, nResults); + L->nny--; } +/* +** Completes the execution of an interrupted C function, calling its +** continuation function. +*/ +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); + /* 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); + luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ +} + + +/* +** Executes "full continuation" (everything in the stack) of a +** previously interrupted coroutine until the stack is empty (or another +** interruption long-jumps out of the loop). If the coroutine is +** recovering from an error, 'ud' points to the error status, which must +** be passed to the first continuation function (otherwise the default +** status is LUA_YIELD). +*/ static void unroll (lua_State *L, void *ud) { - UNUSED(ud); - for (;;) { - if (L->ci == &L->base_ci) /* stack is empty? */ - return; /* coroutine finished normally */ + if (ud != NULL) /* error status? */ + finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ + while (L->ci != &L->base_ci) { /* something in the stack */ if (!isLua(L->ci)) /* C function? */ - finishCcall(L); + finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L); /* execute down to higher C 'boundary' */ @@ -446,7 +560,8 @@ static void unroll (lua_State *L, void *ud) { /* -** check whether thread has a suspended protected call +** Try to find a suspended protected call (a "recover point") for the +** given thread. */ static CallInfo *findpcall (lua_State *L) { CallInfo *ci; @@ -458,6 +573,11 @@ static CallInfo *findpcall (lua_State *L) { } +/* +** Recovers from an error in a coroutine. Finds a recover point (if +** there is one) and completes the execution of the interrupted +** 'luaD_pcall'. If there is no recover point, returns zero. +*/ static int recover (lua_State *L, int status) { StkId oldtop; CallInfo *ci = findpcall(L); @@ -467,94 +587,94 @@ static int recover (lua_State *L, int status) { luaF_close(L, oldtop); seterrorobj(L, status, oldtop); L->ci = ci; - L->allowhook = ci->u.c.old_allowhook; + L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); L->errfunc = ci->u.c.old_errfunc; - ci->callstatus |= CIST_STAT; /* call has error status */ - ci->u.c.status = status; /* (here it is) */ return 1; /* continue running the coroutine */ } /* -** signal an error in the call to 'resume', not in the execution of the -** coroutine itself. (Such errors should not be handled by any coroutine -** error handler and should not kill the coroutine.) +** Signal an error in the call to 'lua_resume', not in the execution +** of the coroutine itself. (Such errors should not be handled by any +** coroutine error handler and should not kill the coroutine.) */ -static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { - L->top = firstArg; /* remove args from the stack */ +static int resume_error (lua_State *L, const char *msg, int narg) { + L->top -= narg; /* remove args from the stack */ setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ api_incr_top(L); - luaD_throw(L, -1); /* jump back to 'lua_resume' */ + lua_unlock(L); + return LUA_ERRRUN; } /* -** do the work for 'lua_resume' in protected mode +** Do the work for 'lua_resume' in protected mode. Most of the work +** depends on the status of the coroutine: initial state, suspended +** inside a hook, or regularly suspended (optionally with a continuation +** function), plus erroneous cases: non-suspended coroutine or dead +** coroutine. */ static void resume (lua_State *L, void *ud) { - int nCcalls = L->nCcalls; - StkId firstArg = cast(StkId, ud); + int n = *(cast(int*, ud)); /* number of arguments */ + StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; - if (nCcalls >= LUAI_MAXCCALLS) - resume_error(L, "C stack overflow", firstArg); - if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (ci != &L->base_ci) /* not in base level? */ - resume_error(L, "cannot resume non-suspended coroutine", firstArg); - /* coroutine is in base level; start running it */ + if (L->status == LUA_OK) { /* starting a coroutine? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ luaV_execute(L); /* call it */ } - else if (L->status != LUA_YIELD) - resume_error(L, "cannot resume dead coroutine", firstArg); else { /* resuming from previous yield */ - L->status = LUA_OK; + 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 */ else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation? */ - int n; - ci->u.c.status = LUA_YIELD; /* 'default' status */ - ci->callstatus |= CIST_YIELDED; + if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); - n = (*ci->u.c.k)(L); /* call continuation */ + n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ } - unroll(L, NULL); + unroll(L, NULL); /* run continuation */ } - lua_assert(nCcalls == L->nCcalls); } LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { int status; - int oldnny = L->nny; /* save 'nny' */ + unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); - luai_userstateresume(L, nargs); + if (L->status == LUA_OK) { /* may be starting a coroutine */ + if (L->ci != &L->base_ci) /* not in base level? */ + return resume_error(L, "cannot resume non-suspended coroutine", nargs); + } + else if (L->status != LUA_YIELD) + return resume_error(L, "cannot resume dead coroutine", nargs); L->nCcalls = (from) ? from->nCcalls + 1 : 1; + if (L->nCcalls >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow", nargs); + luai_userstateresume(L, nargs); L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); - status = luaD_rawrunprotected(L, resume, L->top - nargs); + status = luaD_rawrunprotected(L, resume, &nargs); if (status == -1) /* error calling 'lua_resume'? */ status = LUA_ERRRUN; - else { /* yield or regular error */ - while (status != LUA_OK && status != LUA_YIELD) { /* error? */ - if (recover(L, status)) /* recover point? */ - status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ - else { /* unrecoverable error */ - L->status = cast_byte(status); /* mark thread as `dead' */ - seterrorobj(L, status, L->top); - L->ci->top = L->top; - break; - } + else { /* continue running after recoverable errors */ + while (errorstatus(status) && recover(L, status)) { + /* unroll continuation */ + status = luaD_rawrunprotected(L, unroll, &status); } - lua_assert(status == L->status); + if (errorstatus(status)) { /* unrecoverable error? */ + L->status = cast_byte(status); /* mark thread as 'dead' */ + seterrorobj(L, status, L->top); /* push error message */ + L->ci->top = L->top; + } + else lua_assert(status == L->status); /* normal end or yield */ } L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; @@ -564,7 +684,13 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { } -LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { +LUA_API int lua_isyieldable (lua_State *L) { + return (L->nny == 0); +} + + +LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k) { CallInfo *ci = L->ci; luai_userstateyield(L, nresults); lua_lock(L); @@ -619,7 +745,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, /* ** Execute a protected parser. */ -struct SParser { /* data to `f_parser' */ +struct SParser { /* data to 'f_parser' */ ZIO *z; Mbuffer buff; /* dynamic structure used by the scanner */ Dyndata dyd; /* dynamic structures used by the parser */ @@ -631,31 +757,26 @@ struct SParser { /* data to `f_parser' */ static void checkmode (lua_State *L, const char *mode, const char *x) { if (mode && strchr(mode, x[0]) == NULL) { luaO_pushfstring(L, - "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); + "attempt to load a %s chunk (mode is '%s')", x, mode); luaD_throw(L, LUA_ERRSYNTAX); } } static void f_parser (lua_State *L, void *ud) { - int i; - Closure *cl; + LClosure *cl; struct SParser *p = cast(struct SParser *, ud); int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, &p->buff, p->name); + cl = luaU_undump(L, p->z, p->name); } else { checkmode(L, p->mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } - lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); - for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ - UpVal *up = luaF_newupval(L); - cl->l.upvals[i] = up; - luaC_objbarrier(L, cl, up); - } + lua_assert(cl->nupvalues == cl->p->sp->sizeupvalues); + luaF_initupvals(L, cl); } diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index d3d3082c..4f5d51c3 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -13,31 +13,43 @@ #include "lzio.h" -#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ - luaD_growstack(L, n); else condmovestack(L); +/* +** Macro to check stack size and grow stack if needed. Parameters +** 'pre'/'pos' allow the macro to preserve a pointer into the +** stack across reallocations, doing the work only when needed. +** 'condmovestack' is used in heavy tests to force a stack reallocation +** at every check. +*/ +#define luaD_checkstackaux(L,n,pre,pos) \ + if (L->stack_last - L->top <= (n)) \ + { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } + +/* In general, 'pre'/'pos' are empty (nothing to save) */ +#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) -#define incr_top(L) {L->top++; luaD_checkstack(L,0);} #define savestack(L,p) ((char *)(p) - (char *)L->stack) #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) -/* type of protected functions, to be ran by `runprotected' */ +/* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, - int allowyield); +LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); +LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); +LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, + int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_shrinkstack (lua_State *L); +LUAI_FUNC void luaD_inctop (lua_State *L); LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index dd232ecf..dc100ca1 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,174 +1,216 @@ /* -** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ -#include - #define ldump_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lobject.h" #include "lstate.h" #include "lundump.h" + typedef struct { - lua_State* L; - lua_Writer writer; - void* data; - int strip; - int status; + lua_State *L; + lua_Writer writer; + void *data; + int strip; + int status; } DumpState; -#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) -#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) -static void DumpBlock(const void* b, size_t size, DumpState* D) -{ - if (D->status==0) - { - lua_unlock(D->L); - D->status=(*D->writer)(D->L,b,size,D->data); - lua_lock(D->L); - } -} +/* +** All high-level dumps go through DumpVector; you can change it to +** change the endianness of the result +*/ +#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) -static void DumpChar(int y, DumpState* D) -{ - char x=(char)y; - DumpVar(x,D); -} +#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) -static void DumpInt(int x, DumpState* D) -{ - DumpVar(x,D); -} -static void DumpNumber(lua_Number x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpVector(const void* b, int n, size_t size, DumpState* D) -{ - DumpInt(n,D); - DumpMem(b,n,size,D); -} - -static void DumpString(const TString* s, DumpState* D) -{ - if (s==NULL) - { - size_t size=0; - DumpVar(size,D); - } - else - { - size_t size=s->tsv.len+1; /* include trailing '\0' */ - DumpVar(size,D); - DumpBlock(getstr(s),size*sizeof(char),D); - } -} - -#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) - -static void DumpFunction(const Proto* f, DumpState* D); - -static void DumpConstants(const Proto* f, DumpState* D) -{ - int i,n=f->sp->sizek; - DumpInt(n,D); - for (i=0; ik[i]; - DumpChar(ttypenv(o),D); - switch (ttypenv(o)) - { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpChar(bvalue(o),D); - break; - case LUA_TNUMBER: - DumpNumber(nvalue(o),D); - break; - case LUA_TSTRING: - DumpString(rawtsvalue(o),D); - break; - default: lua_assert(0); +static void DumpBlock (const void *b, size_t size, DumpState *D) { + if (D->status == 0 && size > 0) { + lua_unlock(D->L); + D->status = (*D->writer)(D->L, b, size, D->data); + lua_lock(D->L); } - } - n=f->sp->sizep; - DumpInt(n,D); - for (i=0; ip[i],D); } -static void DumpUpvalues(const Proto* f, DumpState* D) -{ - int i,n=f->sp->sizeupvalues; - DumpInt(n,D); - for (i=0; isp->upvalues[i].instack,D); - DumpChar(f->sp->upvalues[i].idx,D); - } + +#define DumpVar(x,D) DumpVector(&x,1,D) + + +static void DumpByte (int y, DumpState *D) { + lu_byte x = (lu_byte)y; + DumpVar(x, D); } -static void DumpDebug(const SharedProto* f, DumpState* D) -{ - int i,n; - DumpString((D->strip) ? NULL : f->source,D); - n= (D->strip) ? 0 : f->sizelineinfo; - DumpVector(f->lineinfo,n,sizeof(int),D); - n= (D->strip) ? 0 : f->sizelocvars; - DumpInt(n,D); - for (i=0; ilocvars[i].varname,D); - DumpInt(f->locvars[i].startpc,D); - DumpInt(f->locvars[i].endpc,D); - } - n= (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n,D); - for (i=0; iupvalues[i].name,D); + +static void DumpInt (int x, DumpState *D) { + DumpVar(x, D); } -static void DumpFunction(const Proto* f, DumpState* D) -{ - const SharedProto *sp = f->sp; - DumpInt(sp->linedefined,D); - DumpInt(sp->lastlinedefined,D); - DumpChar(sp->numparams,D); - DumpChar(sp->is_vararg,D); - DumpChar(sp->maxstacksize,D); - DumpCode(sp,D); - DumpConstants(f,D); - DumpUpvalues(f,D); - DumpDebug(sp,D); + +static void DumpNumber (lua_Number x, DumpState *D) { + DumpVar(x, D); } -static void DumpHeader(DumpState* D) -{ - lu_byte h[LUAC_HEADERSIZE]; - luaU_header(h); - DumpBlock(h,LUAC_HEADERSIZE,D); + +static void DumpInteger (lua_Integer x, DumpState *D) { + DumpVar(x, D); } + +static void DumpString (const TString *s, DumpState *D) { + if (s == NULL) + DumpByte(0, D); + else { + size_t size = tsslen(s) + 1; /* include trailing '\0' */ + const char *str = getstr(s); + if (size < 0xFF) + DumpByte(cast_int(size), D); + else { + DumpByte(0xFF, D); + DumpVar(size, D); + } + DumpVector(str, size - 1, D); /* no need to save '\0' */ + } +} + + +static void DumpCode (const SharedProto *f, DumpState *D) { + DumpInt(f->sizecode, D); + DumpVector(f->code, f->sizecode, D); +} + + +static void DumpFunction(const Proto *f, TString *psource, DumpState *D); + +static void DumpConstants (const Proto *f, DumpState *D) { + int i; + int n = f->sp->sizek; + DumpInt(n, D); + for (i = 0; i < n; i++) { + const TValue *o = &f->k[i]; + DumpByte(ttype(o), D); + switch (ttype(o)) { + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpByte(bvalue(o), D); + break; + case LUA_TNUMFLT: + DumpNumber(fltvalue(o), D); + break; + case LUA_TNUMINT: + DumpInteger(ivalue(o), D); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + DumpString(tsvalue(o), D); + break; + default: + lua_assert(0); + } + } +} + + +static void DumpProtos (const Proto *f, DumpState *D) { + int i; + int n = f->sp->sizep; + DumpInt(n, D); + for (i = 0; i < n; i++) + DumpFunction(f->p[i], f->sp->source, D); +} + + +static void DumpUpvalues (const SharedProto *f, DumpState *D) { + int i, n = f->sizeupvalues; + DumpInt(n, D); + for (i = 0; i < n; i++) { + DumpByte(f->upvalues[i].instack, D); + DumpByte(f->upvalues[i].idx, D); + } +} + + +static void DumpDebug (const SharedProto *f, DumpState *D) { + int i, n; + n = (D->strip) ? 0 : f->sizelineinfo; + DumpInt(n, D); + DumpVector(f->lineinfo, n, D); + n = (D->strip) ? 0 : f->sizelocvars; + DumpInt(n, D); + for (i = 0; i < n; i++) { + DumpString(f->locvars[i].varname, D); + DumpInt(f->locvars[i].startpc, D); + DumpInt(f->locvars[i].endpc, D); + } + n = (D->strip) ? 0 : f->sizeupvalues; + DumpInt(n, D); + for (i = 0; i < n; i++) + DumpString(f->upvalues[i].name, D); +} + + +static void DumpFunction (const Proto *fp, TString *psource, DumpState *D) { + const SharedProto *f = fp->sp; + if (D->strip || f->source == psource) + DumpString(NULL, D); /* no debug info or same source as its parent */ + else + 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(fp, D); + DumpUpvalues(f, D); + DumpProtos(fp, D); + DumpDebug(f, D); +} + + +static void DumpHeader (DumpState *D) { + DumpLiteral(LUA_SIGNATURE, D); + DumpByte(LUAC_VERSION, D); + DumpByte(LUAC_FORMAT, D); + DumpLiteral(LUAC_DATA, D); + DumpByte(sizeof(int), D); + DumpByte(sizeof(size_t), D); + DumpByte(sizeof(Instruction), D); + DumpByte(sizeof(lua_Integer), D); + DumpByte(sizeof(lua_Number), D); + DumpInteger(LUAC_INT, D); + DumpNumber(LUAC_NUM, D); +} + + /* ** dump Lua function as precompiled chunk */ -int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) -{ - DumpState D; - D.L=L; - D.writer=w; - D.data=data; - D.strip=strip; - D.status=0; - DumpHeader(&D); - DumpFunction(f,&D); - return D.status; +int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, + int strip) { + DumpState D; + D.L = L; + D.writer = w; + D.data = data; + D.strip = strip; + D.status = 0; + DumpHeader(&D); + DumpByte(f->sp->sizeupvalues, &D); + DumpFunction(f, NULL, &D); + return D.status; } + diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 9dbb034e..b0d9db4b 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -1,15 +1,17 @@ /* -** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ - -#include - #define lfunc_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lfunc.h" @@ -20,100 +22,87 @@ -Closure *luaF_newCclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; - c->c.nupvalues = cast_byte(n); +CClosure *luaF_newCclosure (lua_State *L, int n) { + GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); + CClosure *c = gco2ccl(o); + c->nupvalues = cast_byte(n); return c; } -Closure *luaF_newLclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; - c->l.p = NULL; - c->l.nupvalues = cast_byte(n); - while (n--) c->l.upvals[n] = NULL; +LClosure *luaF_newLclosure (lua_State *L, int n) { + GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); + LClosure *c = gco2lcl(o); + c->p = NULL; + c->nupvalues = cast_byte(n); + while (n--) c->upvals[n] = NULL; return c; } - -UpVal *luaF_newupval (lua_State *L) { - UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; - uv->v = &uv->u.value; - setnilvalue(uv->v); - return uv; +/* +** fill a closure with new closed upvalues +*/ +void luaF_initupvals (lua_State *L, LClosure *cl) { + int i; + for (i = 0; i < cl->nupvalues; i++) { + UpVal *uv = luaM_new(L, UpVal); + uv->refcount = 1; + uv->v = &uv->u.value; /* make it closed */ + setnilvalue(uv->v); + cl->upvals[i] = uv; + } } UpVal *luaF_findupval (lua_State *L, StkId level) { - global_State *g = G(L); - GCObject **pp = &L->openupval; + UpVal **pp = &L->openupval; UpVal *p; UpVal *uv; - while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { - GCObject *o = obj2gco(p); - lua_assert(p->v != &p->u.value); - lua_assert(!isold(o) || isold(obj2gco(L))); - if (p->v == level) { /* found a corresponding upvalue? */ - if (isdead(g, o)) /* is it dead? */ - changewhite(o); /* resurrect it */ - return p; - } - pp = &p->next; + lua_assert(isintwups(L) || L->openupval == NULL); + while (*pp != NULL && (p = *pp)->v >= level) { + lua_assert(upisopen(p)); + if (p->v == level) /* found a corresponding upvalue? */ + return p; /* return it */ + pp = &p->u.open.next; } - /* not found: create a new one */ - uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; + /* not found: create a new upvalue */ + uv = luaM_new(L, UpVal); + uv->refcount = 0; + uv->u.open.next = *pp; /* link it to list of open upvalues */ + uv->u.open.touched = 1; + *pp = uv; uv->v = level; /* current value lives in the stack */ - uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ - uv->u.l.next = g->uvhead.u.l.next; - uv->u.l.next->u.l.prev = uv; - g->uvhead.u.l.next = uv; - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ + L->twups = G(L)->twups; /* link it to the list */ + G(L)->twups = L; + } return uv; } -static void unlinkupval (UpVal *uv) { - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ - uv->u.l.prev->u.l.next = uv->u.l.next; -} - - -void luaF_freeupval (lua_State *L, UpVal *uv) { - if (uv->v != &uv->u.value) /* is it open? */ - unlinkupval(uv); /* remove from open list */ - luaM_free(L, uv); /* free upvalue */ -} - - void luaF_close (lua_State *L, StkId level) { UpVal *uv; - global_State *g = G(L); - while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o) && uv->v != &uv->u.value); - L->openupval = uv->next; /* remove from `open' list */ - if (isdead(g, o)) - luaF_freeupval(L, uv); /* free upvalue */ + while (L->openupval != NULL && (uv = L->openupval)->v >= level) { + lua_assert(upisopen(uv)); + L->openupval = uv->u.open.next; /* remove from 'open' list */ + if (uv->refcount == 0) /* no references? */ + luaM_free(L, uv); /* free upvalue */ else { - unlinkupval(uv); /* remove upvalue from 'uvhead' list */ setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ uv->v = &uv->u.value; /* now current value lives here */ - gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ - g->allgc = o; - luaC_checkupvalcolor(g, uv); + luaC_upvalbarrier(L, uv); } } } Proto *luaF_newproto (lua_State *L, SharedProto *sp) { - Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; - f->k = NULL; + GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); + Proto *f = gco2p(o); f->sp = NULL; + f->k = NULL; f->p = NULL; f->cache = NULL; - if (sp == NULL) { sp = luaM_new(L, SharedProto); sp->l_G = G(L); @@ -135,12 +124,11 @@ Proto *luaF_newproto (lua_State *L, SharedProto *sp) { sp->source = NULL; } f->sp = sp; - return f; } -static void -luaF_freesharedproto (lua_State *L, SharedProto *f) { + +static void freesharedproto (lua_State *L, SharedProto *f) { if (f == NULL || G(L) != f->l_G) return; luaM_freearray(L, f->code, f->sizecode); @@ -151,15 +139,15 @@ luaF_freesharedproto (lua_State *L, SharedProto *f) { } void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->k, f->sp->sizek); luaM_freearray(L, f->p, f->sp->sizep); - luaF_freesharedproto(L, f->sp); + luaM_freearray(L, f->k, f->sp->sizek); + freesharedproto(L, f->sp); luaM_free(L, f); } /* -** Look for n-th local variable at line `line' in function `func'. +** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index ba315966..21cbe34e 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -18,14 +18,42 @@ cast(int, sizeof(TValue *)*((n)-1))) +/* test whether thread is in 'twups' list */ +#define isintwups(L) (L->twups != L) + + +/* +** maximum number of upvalues in a closure (both C and Lua). (Value +** must fit in a VM register.) +*/ +#define MAXUPVAL 255 + + +/* +** Upvalues for Lua closures +*/ +struct UpVal { + TValue *v; /* points to stack or to its own value */ + lu_mem refcount; /* reference counter */ + union { + struct { /* (when open) */ + UpVal *next; /* linked list */ + int touched; /* mark to avoid cycles with dead threads */ + } open; + TValue value; /* the value (when closed) */ + } u; +}; + +#define upisopen(up) ((up)->v != &(up)->u.value) + + LUAI_FUNC Proto *luaF_newproto (lua_State *L, SharedProto *sp); -LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); -LUAI_FUNC UpVal *luaF_newupval (lua_State *L); +LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); +LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_close (lua_State *L, StkId level); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index c0f2858c..8a029f79 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,14 +1,17 @@ /* -** $Id: lgc.c,v 2.140.1.2 2013/04/26 18:22:05 roberto Exp $ +** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ -#include - #define lgc_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -23,6 +26,11 @@ #include "ltm.h" +/* +** internal state for collector while inside the atomic phase. The +** collector should never be in this state while running regular code. +*/ +#define GCSinsideatomic (GCSpause + 1) /* ** cost of sweeping one element (the size of a small object divided @@ -33,8 +41,8 @@ /* maximum number of elements to sweep in each single step */ #define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) -/* maximum number of finalizers to call in each GC step */ -#define GCFINALIZENUM 4 +/* cost of calling one finalizer */ +#define GCFINALIZECOST GCSWEEPCOST /* @@ -52,18 +60,18 @@ /* -** 'makewhite' erases all color bits plus the old bit and then -** sets only the current white bit +** 'makewhite' erases all color bits then sets only the current white +** bit */ -#define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS)) +#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) #define makewhite(g,x) \ - (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) -#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS) -#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT) +#define white2gray(x) resetbits(x->marked, WHITEBITS) +#define black2gray(x) resetbit(x->marked, BLACKBIT) -#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT) +#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) @@ -75,8 +83,13 @@ #define markvalue(g,o) { checkconsistency(o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } -#define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \ - reallymarkobject(g, obj2gco(t)); } +#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } + +/* +** mark an object that can be NULL (either because it is really optional, +** or it was stripped as debug info, or inside an uncompleted structure) +*/ +#define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); @@ -95,33 +108,38 @@ static void reallymarkobject (global_State *g, GCObject *o); /* -** link table 'h' into list pointed by 'p' +** link collectable object 'o' into list pointed by 'p' */ -#define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h)) +#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) /* -** if key is not marked, mark its entry as dead (therefore removing it -** from the table) +** If key is not marked, mark its entry as dead. This allows key to be +** collected, but keeps its entry in the table. A dead node is needed +** when Lua looks up for a key (it may be part of a chain) and when +** traversing a weak table (key might be removed from the table during +** traversal). Other places never manipulate dead keys, because its +** associated nil value is enough to signal that the entry is logically +** empty. */ static void removeentry (Node *n) { lua_assert(ttisnil(gval(n))); if (valiswhite(gkey(n))) - setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */ + setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */ } /* ** tells whether a key or value can be cleared from a weak ** table. Non-collectable objects are never removed from weak -** tables. Strings behave as `values', so are never removed too. for +** tables. Strings behave as 'values', so are never removed too. for ** other objects: if really collected, cannot keep them; for objects ** being finalized, keep them in keys, but not in values */ static int iscleared (global_State *g, const TValue *o) { if (!iscollectable(o)) return 0; else if (ttisstring(o)) { - markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */ + markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */ return 0; } else return iswhite(gcvalue(o)); @@ -130,14 +148,14 @@ static int iscleared (global_State *g, const TValue *o) { /* ** barrier that moves collector forward, that is, mark the white object -** being pointed by a black object. +** being pointed by a black object. (If in sweep phase, clear the black +** object to white [sweep it] to avoid other barrier calls for this +** same object.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - lua_assert(g->gcstate != GCSpause); - lua_assert(gch(o)->tt != LUA_TTABLE); - if (keepinvariantout(g)) /* must keep invariant? */ + if (keepinvariant(g)) /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ else { /* sweep phase */ lua_assert(issweepphase(g)); @@ -148,78 +166,53 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { /* ** barrier that moves collector backward, that is, mark the black object -** pointing to a white object as gray again. (Current implementation -** only works for tables; access to 'gclist' is not uniform across -** different types.) +** pointing to a white object as gray again. */ -void luaC_barrierback_ (lua_State *L, GCObject *o) { +void luaC_barrierback_ (lua_State *L, Table *t) { global_State *g = G(L); - lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE); - black2gray(o); /* make object gray (again) */ - gco2t(o)->gclist = g->grayagain; - g->grayagain = o; + lua_assert(isblack(t) && !isdead(g, t)); + black2gray(t); /* make table gray (again) */ + linkgclist(t, g->grayagain); } /* -** barrier for prototypes. When creating first closure (cache is -** NULL), use a forward barrier; this may be the only closure of the -** prototype (if it is a "regular" function, with a single instance) -** and the prototype may be big, so it is better to avoid traversing -** it again. Otherwise, use a backward barrier, to avoid marking all -** possible instances. +** barrier for assignments to closed upvalues. Because upvalues are +** shared among closures, it is impossible to know the color of all +** closures pointing to it. So, we assume that the object being assigned +** must be marked. */ -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c) { +void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { global_State *g = G(L); - lua_assert(isblack(obj2gco(p))); - if (p->cache == NULL) { /* first time? */ - luaC_objbarrier(L, p, c); - } - else { /* use a backward barrier */ - black2gray(obj2gco(p)); /* make prototype gray (again) */ - p->gclist = g->grayagain; - g->grayagain = obj2gco(p); - } + GCObject *o = gcvalue(uv->v); + lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ + if (keepinvariant(g)) + markobject(g, o); } -/* -** check color (and invariants) for an upvalue that was closed, -** i.e., moved into the 'allgc' list -*/ -void luaC_checkupvalcolor (global_State *g, UpVal *uv) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o)); /* open upvalues are never black */ - if (isgray(o)) { - if (keepinvariant(g)) { - resetoldbit(o); /* see MOVE OLD rule */ - gray2black(o); /* it is being visited now */ - markvalue(g, uv->v); - } - else { - lua_assert(issweepphase(g)); - makewhite(g, o); - } - } +void luaC_fix (lua_State *L, GCObject *o) { + global_State *g = G(L); + if (g->allgc != o) + return; /* if object is not 1st in 'allgc' list, it is in global short string table */ + white2gray(o); /* they will be gray forever */ + g->allgc = o->next; /* remove object from 'allgc' list */ + o->next = g->fixedgc; /* link it to 'fixedgc' list */ + g->fixedgc = o; } /* ** create a new collectable object (with given type and size) and link -** it to '*list'. 'offset' tells how many bytes to allocate before the -** object itself (used only by states). +** it to 'allgc' list. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, - int offset) { +GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { global_State *g = G(L); - char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz)); - GCObject *o = obj2gco(raw + offset); - if (list == NULL) - list = &g->allgc; /* standard list for collectable objects */ - gch(o)->marked = luaC_white(g); - gch(o)->tt = tt; - gch(o)->next = *list; - *list = o; + GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); + o->marked = luaC_white(g); + o->tt = tt; + o->next = g->allgc; + g->allgc = o; return o; } @@ -241,57 +234,53 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, ** upvalues are already linked in 'headuv' list.) */ static void reallymarkobject (global_State *g, GCObject *o) { - lu_mem size; + reentry: white2gray(o); - switch (gch(o)->tt) { - case LUA_TSHRSTR: - case LUA_TLNGSTR: { - size = sizestring(gco2ts(o)); - break; /* nothing else to mark; make it black */ - } - case LUA_TUSERDATA: { - Table *mt = gco2u(o)->metatable; - markobject(g, mt); - markobject(g, gco2u(o)->env); - size = sizeudata(gco2u(o)); + switch (o->tt) { + case LUA_TSHRSTR: { + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); break; } - case LUA_TUPVAL: { - UpVal *uv = gco2uv(o); - markvalue(g, uv->v); - if (uv->v != &uv->u.value) /* open? */ - return; /* open upvalues remain gray */ - size = sizeof(UpVal); + case LUA_TLNGSTR: { + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); + 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; + } break; } case LUA_TLCL: { - gco2lcl(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2lcl(o), g->gray); + break; } case LUA_TCCL: { - gco2ccl(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2ccl(o), g->gray); + break; } case LUA_TTABLE: { - linktable(gco2t(o), &g->gray); - return; + linkgclist(gco2t(o), g->gray); + break; } case LUA_TTHREAD: { - gco2th(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2th(o), g->gray); + break; } case LUA_TPROTO: { - gco2p(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2p(o), g->gray); + break; } - default: lua_assert(0); return; + default: lua_assert(0); break; } - gray2black(o); - g->GCmemtrav += size; } @@ -301,7 +290,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { static void markmt (global_State *g) { int i; for (i=0; i < LUA_NUMTAGS; i++) - markobject(g, g->mt[i]); + markobjectN(g, g->mt[i]); } @@ -310,29 +299,41 @@ static void markmt (global_State *g) { */ static void markbeingfnz (global_State *g) { GCObject *o; - for (o = g->tobefnz; o != NULL; o = gch(o)->next) { - makewhite(g, o); - reallymarkobject(g, o); - } + for (o = g->tobefnz; o != NULL; o = o->next) + markobject(g, o); } /* -** mark all values stored in marked open upvalues. (See comment in -** 'lstate.h'.) +** Mark all values stored in marked open upvalues from non-marked threads. +** (Values from marked threads were already marked when traversing the +** thread.) Remove from the list threads that no longer have upvalues and +** not-marked threads. */ static void remarkupvals (global_State *g) { - UpVal *uv; - for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { - if (isgray(obj2gco(uv))) - markvalue(g, uv->v); + lua_State *thread; + lua_State **p = &g->twups; + while ((thread = *p) != NULL) { + lua_assert(!isblack(thread)); /* threads are never black */ + if (isgray(thread) && thread->openupval != NULL) + p = &thread->twups; /* keep marked thread with upvalues in the list */ + else { /* thread is not marked or without upvalues */ + UpVal *uv; + *p = thread->twups; /* remove thread from the list */ + thread->twups = thread; /* mark that it is out of list */ + for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { + if (uv->u.open.touched) { + markvalue(g, uv->v); /* remark upvalue's value */ + uv->u.open.touched = 0; + } + } + } } } /* -** mark root set and reset all gray lists, to start a new -** incremental (or full) collection +** mark root set and reset all gray lists, to start a new collection */ static void restartcollection (global_State *g) { g->gray = g->grayagain = NULL; @@ -352,12 +353,18 @@ static void restartcollection (global_State *g) { ** ======================================================= */ +/* +** Traverse a table with weak values and link it to proper list. During +** propagate phase, keep it in 'grayagain' list, to be revisited in the +** atomic phase. In the atomic phase, if table has any white value, +** put it in 'weak' list, to be cleared. +*/ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - /* if there is array part, assume it may have white values (do not - traverse it just to check) */ + /* if there is array part, assume it may have white values (it is not + worth traversing it now just to check) */ int hasclears = (h->sizearray > 0); - for (n = gnode(h, 0); n < limit; n++) { + for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ @@ -368,20 +375,30 @@ static void traverseweakvalue (global_State *g, Table *h) { hasclears = 1; /* table will have to be cleared */ } } - if (hasclears) - linktable(h, &g->weak); /* has to be cleared later */ - else /* no white values */ - linktable(h, &g->grayagain); /* no need to clean */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasclears) + linkgclist(h, g->weak); /* has to be cleared later */ } +/* +** Traverse an ephemeron table and link it to proper list. Returns true +** iff any object was marked during this traversal (which implies that +** convergence has to continue). During propagation phase, keep table +** in 'grayagain' list, to be visited again in the atomic phase. In +** the atomic phase, if table has any white->white entry, it has to +** be revisited during ephemeron convergence (as that key may turn +** black). Otherwise, if it has any white key, table has to be cleared +** (in the atomic phase). +*/ static int traverseephemeron (global_State *g, Table *h) { int marked = 0; /* true if an object is marked in this traversal */ int hasclears = 0; /* true if table has white keys */ - int prop = 0; /* true if table has entry "white-key -> white-value" */ + int hasww = 0; /* true if table has entry "white-key -> white-value" */ Node *n, *limit = gnodelast(h); - int i; - /* traverse array part (numeric keys are 'strong') */ + unsigned int i; + /* traverse array part */ for (i = 0; i < h->sizearray; i++) { if (valiswhite(&h->array[i])) { marked = 1; @@ -396,26 +413,27 @@ static int traverseephemeron (global_State *g, Table *h) { else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ - prop = 1; /* must propagate again */ + hasww = 1; /* white-white entry */ } else if (valiswhite(gval(n))) { /* value not marked yet? */ marked = 1; reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ } } - if (prop) - linktable(h, &g->ephemeron); /* have to propagate again */ - else if (hasclears) /* does table have white keys? */ - linktable(h, &g->allweak); /* may have to clean white keys */ - else /* no white keys */ - linktable(h, &g->grayagain); /* no need to clean */ + /* link table into proper list */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasww) /* table has white->white entries? */ + linkgclist(h, g->ephemeron); /* have to propagate again */ + else if (hasclears) /* table has white keys? */ + linkgclist(h, g->allweak); /* may have to clean white keys */ return marked; } static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - int i; + unsigned int i; for (i = 0; i < h->sizearray; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ @@ -434,52 +452,59 @@ static void traversestrongtable (global_State *g, Table *h) { static lu_mem traversetable (global_State *g, Table *h) { const char *weakkey, *weakvalue; const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - markobject(g, h->metatable); + markobjectN(g, h->metatable); if (mode && ttisstring(mode) && /* is there a weak mode? */ ((weakkey = strchr(svalue(mode), 'k')), (weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(obj2gco(h)); /* keep table gray */ + black2gray(h); /* keep table gray */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ traverseephemeron(g, h); else /* all weak */ - linktable(h, &g->allweak); /* nothing to traverse now */ + linkgclist(h, g->allweak); /* nothing to traverse now */ } else /* not weak */ traversestrongtable(g, h); return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, sizenode(h)); + sizeof(Node) * cast(size_t, allocsizenode(h)); } -static int -marksharedproto (global_State *g, SharedProto *f) { +static int marksharedproto (global_State *g, SharedProto *f) { int i; if (g != f->l_G) return 0; - markobject(g, f->source); + markobjectN(g, f->source); for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobject(g, f->upvalues[i].name); + markobjectN(g, f->upvalues[i].name); for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobject(g, f->locvars[i].varname); + 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 +** arrays can be larger than needed; the extra slots are filled with +** NULL, so the use of 'markobjectN') +*/ static int traverseproto (global_State *g, Proto *f) { - int i; - if (f->cache && iswhite(obj2gco(f->cache))) + int i,nk,np; + if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - for (i = 0; i < f->sp->sizek; i++) /* mark literals */ + if (f->sp == NULL) + return sizeof(Proto); + nk = (f->k == NULL) ? 0 : f->sp->sizek; + np = (f->p == NULL) ? 0 : f->sp->sizep; + for (i = 0; i < nk; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ - markobject(g, f->p[i]); - return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + - sizeof(TValue) * f->sp->sizek + + for (i = 0; i < np; i++) /* mark nested protos */ + markobjectN(g, f->p[i]); + return sizeof(Proto) + sizeof(Proto *) * np + + sizeof(TValue) * nk + marksharedproto(g, f->sp); } @@ -491,34 +516,50 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) { return sizeCclosure(cl->nupvalues); } +/* +** open upvalues point to values in a thread, so those values should +** be marked when the thread is traversed except in the atomic phase +** (because then the value cannot be changed by the thread and the +** thread may not be traversed again) +*/ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; - markobject(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markobject(g, cl->upvals[i]); + markobjectN(g, cl->p); /* mark its prototype */ + for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ + UpVal *uv = cl->upvals[i]; + if (uv != NULL) { + if (upisopen(uv) && g->gcstate != GCSinsideatomic) + uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ + else + markvalue(g, uv->v); + } + } return sizeLclosure(cl->nupvalues); } -static lu_mem traversestack (global_State *g, lua_State *th) { - int n = 0; +static lu_mem traversethread (global_State *g, lua_State *th) { StkId o = th->stack; if (o == NULL) return 1; /* stack not completely built yet */ + lua_assert(g->gcstate == GCSinsideatomic || + th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, o); - if (g->gcstate == GCSatomic) { /* final traversal? */ + if (g->gcstate == GCSinsideatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ setnilvalue(o); + /* 'remarkupvals' may have removed thread from 'twups' list */ + if (!isintwups(th) && th->openupval != NULL) { + th->twups = g->twups; /* link it back to the list */ + g->twups = th; + } } - else { /* count call infos to compute size */ - CallInfo *ci; - for (ci = &th->base_ci; ci != th->ci; ci = ci->next) - n++; - } - return sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * n; + else if (g->gckind != KGC_EMERGENCY) + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ + return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + + sizeof(CallInfo) * th->nci); } @@ -531,7 +572,7 @@ static void propagatemark (global_State *g) { GCObject *o = g->gray; lua_assert(isgray(o)); gray2black(o); - switch (gch(o)->tt) { + switch (o->tt) { case LUA_TTABLE: { Table *h = gco2t(o); g->gray = h->gclist; /* remove from 'gray' list */ @@ -553,10 +594,9 @@ static void propagatemark (global_State *g) { case LUA_TTHREAD: { lua_State *th = gco2th(o); g->gray = th->gclist; /* remove from 'gray' list */ - th->gclist = g->grayagain; - g->grayagain = o; /* insert into 'grayagain' list */ + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(o); - size = traversestack(g, th); + size = traversethread(g, th); break; } case LUA_TPROTO: { @@ -576,35 +616,12 @@ static void propagateall (global_State *g) { } -static void propagatelist (global_State *g, GCObject *l) { - lua_assert(g->gray == NULL); /* no grays left */ - g->gray = l; - propagateall(g); /* traverse all elements from 'l' */ -} - -/* -** retraverse all gray lists. Because tables may be reinserted in other -** lists when traversed, traverse the original lists to avoid traversing -** twice the same table (which is not wrong, but inefficient) -*/ -static void retraversegrays (global_State *g) { - GCObject *weak = g->weak; /* save original lists */ - GCObject *grayagain = g->grayagain; - GCObject *ephemeron = g->ephemeron; - g->weak = g->grayagain = g->ephemeron = NULL; - propagateall(g); /* traverse main gray list */ - propagatelist(g, grayagain); - propagatelist(g, weak); - propagatelist(g, ephemeron); -} - - static void convergeephemerons (global_State *g) { int changed; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ - g->ephemeron = NULL; /* tables will return to this list when traversed */ + g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; while ((w = next) != NULL) { next = gco2t(w)->gclist; @@ -652,7 +669,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); - int i; + unsigned int i; for (i = 0; i < h->sizearray; i++) { TValue *o = &h->array[i]; if (iscleared(g, o)) /* value was collected? */ @@ -668,26 +685,45 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { } +void luaC_upvdeccount (lua_State *L, UpVal *uv) { + lua_assert(uv->refcount > 0); + uv->refcount--; + if (uv->refcount == 0 && !upisopen(uv)) + luaM_free(L, uv); +} + + +static void freeLclosure (lua_State *L, LClosure *cl) { + int i; + for (i = 0; i < cl->nupvalues; i++) { + UpVal *uv = cl->upvals[i]; + if (uv) + luaC_upvdeccount(L, uv); + } + luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); +} + + static void freeobj (lua_State *L, GCObject *o) { - switch (gch(o)->tt) { + switch (o->tt) { case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; case LUA_TLCL: { - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + freeLclosure(L, gco2lcl(o)); break; } case LUA_TCCL: { luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; } - case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; case LUA_TTABLE: luaH_free(L, gco2t(o)); break; case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; case LUA_TSHRSTR: - G(L)->strt.nuse--; - /* go through */ + 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, sizestring(gco2ts(o))); + luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; } default: lua_assert(0); @@ -699,61 +735,27 @@ static void freeobj (lua_State *L, GCObject *o) { static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); -/* -** sweep the (open) upvalues of a thread and resize its stack and -** list of call-info structures. -*/ -static void sweepthread (lua_State *L, lua_State *L1) { - if (L1->stack == NULL) return; /* stack not completely built yet */ - sweepwholelist(L, &L1->openupval); /* sweep open upvalues */ - luaE_freeCI(L1); /* free extra CallInfo slots */ - /* should not change the stack during an emergency gc cycle */ - if (G(L)->gckind != KGC_EMERGENCY) - luaD_shrinkstack(L1); -} - - /* ** sweep at most 'count' elements from a list of GCObjects erasing dead -** objects, where a dead (not alive) object is one marked with the "old" -** (non current) white and not fixed. -** In non-generational mode, change all non-dead objects back to white, -** preparing for next collection cycle. -** In generational mode, keep black objects black, and also mark them as -** old; stop when hitting an old object, as all objects after that -** one will be old too. -** When object is a thread, sweep its list of open upvalues too. +** objects, where a dead object is one marked with the old (non current) +** white; change all non-dead objects back to white, preparing for next +** collection cycle. Return where to continue the traversal or NULL if +** list is finished. */ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { global_State *g = G(L); int ow = otherwhite(g); - int toclear, toset; /* bits to clear and to set in all live objects */ - int tostop; /* stop sweep when this is true */ - if (isgenerational(g)) { /* generational mode? */ - toclear = ~0; /* clear nothing */ - toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */ - tostop = bitmask(OLDBIT); /* do not sweep old generation */ - } - else { /* normal mode */ - toclear = maskcolors; /* clear all color bits + old bit */ - toset = luaC_white(g); /* make object white */ - tostop = 0; /* do not stop */ - } + int white = luaC_white(g); /* current white */ while (*p != NULL && count-- > 0) { GCObject *curr = *p; - int marked = gch(curr)->marked; + int marked = curr->marked; if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - *p = gch(curr)->next; /* remove 'curr' from list */ + *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } - else { - if (testbits(marked, tostop)) - return NULL; /* stop sweeping this list */ - if (gch(curr)->tt == LUA_TTHREAD) - sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */ - /* update marks */ - gch(curr)->marked = cast_byte((marked & toclear) | toset); - p = &gch(curr)->next; /* go to next element */ + else { /* change mark to 'white' */ + curr->marked = cast_byte((marked & maskcolors) | white); + p = &curr->next; /* go to next element */ } } return (*p == NULL) ? NULL : p; @@ -763,14 +765,11 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { /* ** sweep a list until a live object (or end of list) */ -static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { - GCObject ** old = p; - int i = 0; +static GCObject **sweeptolive (lua_State *L, GCObject **p) { + GCObject **old = p; do { - i++; p = sweeplist(L, p, 1); } while (p == old); - if (n) *n += i; return p; } @@ -783,26 +782,27 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { ** ======================================================= */ -static void checkSizes (lua_State *L) { - global_State *g = G(L); - if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */ - int hs = g->strt.size / 2; /* half the size of the string table */ - if (g->strt.nuse < cast(lu_int32, hs)) /* using less than that half? */ - luaS_resize(L, hs); /* halve its size */ - luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ +/* +** 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(isfinalized(o)); - g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */ - gch(o)->next = g->allgc; /* return it to 'allgc' list */ + lua_assert(tofinalize(o)); + g->tobefnz = o->next; /* remove it from 'tobefnz' list */ + o->next = g->allgc; /* return it to 'allgc' list */ g->allgc = o; - resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */ - lua_assert(!isold(o)); /* see MOVE OLD rule */ - if (!keepinvariantout(g)) /* not keeping invariant? */ + resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ + if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ return o; } @@ -810,7 +810,7 @@ static GCObject *udata2finalize (global_State *g) { static void dothecall (lua_State *L, void *ud) { UNUSED(ud); - luaD_call(L, L->top - 2, 0, 0); + luaD_callnoyield(L, L->top - 2, 0); } @@ -829,7 +829,9 @@ static void GCTM (lua_State *L, int propagateerrors) { setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top + 1, &v); /* ... and its argument */ L->top += 2; /* and (next line) call the finalizer */ + L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); + L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ @@ -846,29 +848,58 @@ static void GCTM (lua_State *L, int propagateerrors) { } +/* +** call a few (up to 'g->gcfinnum') finalizers +*/ +static int runafewfinalizers (lua_State *L) { + global_State *g = G(L); + unsigned int i; + lua_assert(!g->tobefnz || g->gcfinnum > 0); + for (i = 0; g->tobefnz && i < g->gcfinnum; i++) + GCTM(L, 1); /* call one finalizer */ + g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ + : g->gcfinnum * 2; /* else call a few more next time */ + return i; +} + + +/* +** call all pending finalizers +*/ +static void callallpendingfinalizers (lua_State *L) { + global_State *g = G(L); + while (g->tobefnz) + GCTM(L, 0); +} + + +/* +** find last 'next' field in list 'p' list (to add elements in its end) +*/ +static GCObject **findlast (GCObject **p) { + while (*p != NULL) + p = &(*p)->next; + return p; +} + + /* ** move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized) */ -static void separatetobefnz (lua_State *L, int all) { - global_State *g = G(L); - GCObject **p = &g->finobj; +static void separatetobefnz (global_State *g, int all) { GCObject *curr; - GCObject **lastnext = &g->tobefnz; - /* find last 'next' field in 'tobefnz' list (to add elements in its end) */ - while (*lastnext != NULL) - lastnext = &gch(*lastnext)->next; + GCObject **p = &g->finobj; + GCObject **lastnext = findlast(&g->tobefnz); while ((curr = *p) != NULL) { /* traverse all finalizable objects */ - lua_assert(!isfinalized(curr)); - lua_assert(testbit(gch(curr)->marked, SEPARATED)); + lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ - p = &gch(curr)->next; /* don't bother with it */ + p = &curr->next; /* don't bother with it */ else { - l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ - *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */ - gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */ + *p = curr->next; /* remove 'curr' from 'finobj' list */ + curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; - lastnext = &gch(curr)->next; + lastnext = &curr->next; } } } @@ -880,33 +911,29 @@ static void separatetobefnz (lua_State *L, int all) { */ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); - if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */ - isfinalized(o) || /* ... or is finalized... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ + if (tofinalize(o) || /* obj. is already marked... */ + gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; - GCheader *ho = gch(o); - if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */ - lua_assert(issweepphase(g)); - g->sweepgc = sweeptolive(L, g->sweepgc, NULL); + if (issweepphase(g)) { + makewhite(g, o); /* "sweep" object 'o' */ + if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ + g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } /* search for pointer pointing to 'o' */ - for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ } - *p = ho->next; /* remove 'o' from root list */ - ho->next = g->finobj; /* link it in list 'finobj' */ + for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } + *p = o->next; /* remove 'o' from 'allgc' list */ + o->next = g->finobj; /* link it in 'finobj' list */ g->finobj = o; - l_setbit(ho->marked, SEPARATED); /* mark it as such */ - if (!keepinvariantout(g)) /* not keeping invariant? */ - makewhite(g, o); /* "sweep" object */ - else - resetoldbit(o); /* see MOVE OLD rule */ + l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */ } } /* }====================================================== */ + /* ** {====================================================== ** GC control @@ -915,195 +942,164 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* -** set a reasonable "time" to wait before starting a new GC cycle; -** cycle will start when memory use hits threshold +** Set a reasonable "time" to wait before starting a new GC cycle; cycle +** will start when memory use hits threshold. (Division by 'estimate' +** should be OK: it cannot be zero (because Lua cannot even start with +** less than PAUSEADJ bytes). */ -static void setpause (global_State *g, l_mem estimate) { - l_mem debt, threshold; - estimate = estimate / PAUSEADJ; /* adjust 'estimate' */ +static void setpause (global_State *g) { + l_mem threshold, debt; + l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ + lua_assert(estimate > 0); threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ ? estimate * g->gcpause /* no overflow */ : MAX_LMEM; /* overflow; truncate to maximum */ - debt = -cast(l_mem, threshold - gettotalbytes(g)); + debt = gettotalbytes(g) - threshold; luaE_setdebt(g, debt); } -#define sweepphases \ - (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep)) - - /* -** enter first sweep phase (strings) and prepare pointers for other -** sweep phases. The calls to 'sweeptolive' make pointers point to an -** object inside the list (instead of to the header), so that the real -** sweep do not need to skip objects created between "now" and the start -** of the real sweep. -** Returns how many objects it swept. +** Enter first sweep phase. +** The call to 'sweeplist' tries to make pointer point to an object +** inside the list (instead of to the header), so that the real sweep do +** not need to skip objects created between "now" and the start of the +** real sweep. */ -static int entersweep (lua_State *L) { +static void entersweep (lua_State *L) { global_State *g = G(L); - int n = 0; - g->gcstate = GCSsweepstring; - lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); - /* prepare to sweep strings, finalizable objects, and regular objects */ - g->sweepstrgc = 0; - g->sweepfin = sweeptolive(L, &g->finobj, &n); - g->sweepgc = sweeptolive(L, &g->allgc, &n); - return n; -} - - -/* -** change GC mode -*/ -void luaC_changemode (lua_State *L, int mode) { - global_State *g = G(L); - if (mode == g->gckind) return; /* nothing to change */ - if (mode == KGC_GEN) { /* change to generational mode */ - /* make sure gray lists are consistent */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - g->GCestimate = gettotalbytes(g); - g->gckind = KGC_GEN; - } - else { /* change to incremental mode */ - /* sweep all objects to turn them back to white - (as white has not changed, nothing extra will be collected) */ - g->gckind = KGC_NORMAL; - entersweep(L); - luaC_runtilstate(L, ~sweepphases); - } -} - - -/* -** call all pending finalizers -*/ -static void callallpendingfinalizers (lua_State *L, int propagateerrors) { - global_State *g = G(L); - while (g->tobefnz) { - resetoldbit(g->tobefnz); - GCTM(L, propagateerrors); - } + g->gcstate = GCSswpallgc; + lua_assert(g->sweepgc == NULL); + g->sweepgc = sweeplist(L, &g->allgc, 1); } void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); - int i; - separatetobefnz(L, 1); /* separate all objects with finalizers */ + separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - callallpendingfinalizers(L, 0); + 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); /* finalizers can create objs. in 'finobj' */ + sweepwholelist(L, &g->finobj); sweepwholelist(L, &g->allgc); - for (i = 0; i < g->strt.size; i++) /* free all string lists */ - sweepwholelist(L, &g->strt.hash[i]); + sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } static l_mem atomic (lua_State *L) { global_State *g = G(L); - l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */ + l_mem work; GCObject *origweak, *origall; - lua_assert(!iswhite(obj2gco(g->mainthread))); + GCObject *grayagain = g->grayagain; /* save original list */ + lua_assert(g->ephemeron == NULL && g->weak == NULL); + lua_assert(!iswhite(g->mainthread)); + g->gcstate = GCSinsideatomic; + g->GCmemtrav = 0; /* start counting work */ markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); - markmt(g); /* mark basic metatables */ + markmt(g); /* mark global metatables */ /* remark occasional upvalues of (maybe) dead threads */ remarkupvals(g); propagateall(g); /* propagate changes */ - work += g->GCmemtrav; /* stop counting (do not (re)count grays) */ - /* traverse objects caught by write barrier and by 'remarkupvals' */ - retraversegrays(g); - work -= g->GCmemtrav; /* restart counting */ + work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ + g->gray = grayagain; + propagateall(g); /* traverse 'grayagain' list */ + g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ - /* clear values from weak tables, before checking finalizers */ + /* Clear values from weak tables, before checking finalizers */ clearvalues(g, g->weak, NULL); clearvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; work += g->GCmemtrav; /* stop counting (objects being finalized) */ - separatetobefnz(L, 0); /* separate objects to be finalized */ + separatetobefnz(g, 0); /* separate objects to be finalized */ + g->gcfinnum = 1; /* there may be objects to be finalized */ markbeingfnz(g); /* mark objects that will be finalized */ - propagateall(g); /* remark, to propagate `preserveness' */ - work -= g->GCmemtrav; /* restart counting */ + propagateall(g); /* remark, to propagate 'resurrection' */ + g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */ + clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */ /* clear values from resurrected weak tables */ clearvalues(g, g->weak, origweak); clearvalues(g, g->allweak, origall); + 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' */ } +static lu_mem sweepstep (lua_State *L, global_State *g, + int nextstate, GCObject **nextlist) { + if (g->sweepgc) { + l_mem olddebt = g->GCdebt; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + g->GCestimate += g->GCdebt - olddebt; /* update estimate */ + if (g->sweepgc) /* is there still something to sweep? */ + return (GCSWEEPMAX * GCSWEEPCOST); + } + /* else enter next state */ + g->gcstate = nextstate; + g->sweepgc = nextlist; + return 0; +} + + static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - /* start to count memory traversed */ g->GCmemtrav = g->strt.size * sizeof(GCObject*); - lua_assert(!isgenerational(g)); restartcollection(g); g->gcstate = GCSpropagate; return g->GCmemtrav; } case GCSpropagate: { - if (g->gray) { - lu_mem oldtrav = g->GCmemtrav; - propagatemark(g); - return g->GCmemtrav - oldtrav; /* memory traversed in this step */ - } - else { /* no more `gray' objects */ - lu_mem work; - int sw; - g->gcstate = GCSatomic; /* finish mark phase */ - g->GCestimate = g->GCmemtrav; /* save what was counted */; - work = atomic(L); /* add what was traversed by 'atomic' */ - g->GCestimate += work; /* estimate of total memory traversed */ - sw = entersweep(L); - return work + sw * GCSWEEPCOST; - } + g->GCmemtrav = 0; + lua_assert(g->gray); + propagatemark(g); + if (g->gray == NULL) /* no more gray objects? */ + g->gcstate = GCSatomic; /* finish propagate phase */ + return g->GCmemtrav; /* memory traversed in this step */ } - case GCSsweepstring: { - int i; - for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++) - sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]); - g->sweepstrgc += i; - if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */ - g->gcstate = GCSsweepudata; - return i * GCSWEEPCOST; + case GCSatomic: { + lu_mem work; + propagateall(g); /* make sure gray list is empty */ + work = atomic(L); /* work is what was traversed by 'atomic' */ + entersweep(L); + g->GCestimate = gettotalbytes(g); /* first estimate */; + return work; } - case GCSsweepudata: { - if (g->sweepfin) { - g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; - } - else { - g->gcstate = GCSsweep; - return 0; - } + case GCSswpallgc: { /* sweep "regular" objects */ + return sweepstep(L, g, GCSswpfinobj, &g->finobj); } - case GCSsweep: { - if (g->sweepgc) { - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; + case GCSswpfinobj: { /* sweep objects with finalizers */ + return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + } + case GCSswptobefnz: { /* sweep objects to be finalized */ + return sweepstep(L, g, GCSswpend, NULL); + } + case GCSswpend: { /* finish sweeps */ + makewhite(g, g->mainthread); /* sweep main thread */ + checkSizes(L, g); + g->gcstate = GCScallfin; + return 0; + } + case GCScallfin: { /* call remaining finalizers */ + if (g->tobefnz && g->gckind != KGC_EMERGENCY) { + int n = runafewfinalizers(L); + return (n * GCFINALIZECOST); } - else { - /* sweep main thread */ - GCObject *mt = obj2gco(g->mainthread); - sweeplist(L, &mt, 1); - checkSizes(L); + else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ - return GCSWEEPCOST; + return 0; } } default: lua_assert(0); return 0; @@ -1122,105 +1118,70 @@ void luaC_runtilstate (lua_State *L, int statesmask) { } -static void generationalcollection (lua_State *L) { - global_State *g = G(L); - lua_assert(g->gcstate == GCSpropagate); - if (g->GCestimate == 0) { /* signal for another major collection? */ - luaC_fullgc(L, 0); /* perform a full regular collection */ - g->GCestimate = gettotalbytes(g); /* update control */ - } - else { - lu_mem estimate = g->GCestimate; - luaC_runtilstate(L, bitmask(GCSpause)); /* run complete (minor) cycle */ - g->gcstate = GCSpropagate; /* skip restart */ - if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc) - g->GCestimate = 0; /* signal for a major collection */ - else - g->GCestimate = estimate; /* keep estimate from last major coll. */ - - } - setpause(g, gettotalbytes(g)); - lua_assert(g->gcstate == GCSpropagate); -} - - -static void incstep (lua_State *L) { - global_State *g = G(L); +/* +** get GC debt and convert it from Kb to 'work units' (avoid zero debt +** and overflows) +*/ +static l_mem getdebt (global_State *g) { l_mem debt = g->GCdebt; int stepmul = g->gcstepmul; - if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */ - /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */ - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - do { /* always perform at least one single step */ - lu_mem work = singlestep(L); /* do some work */ - debt -= work; - } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); - if (g->gcstate == GCSpause) - setpause(g, g->GCestimate); /* pause until next cycle */ + if (debt <= 0) return 0; /* minimal debt */ else { - debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */ - luaE_setdebt(g, debt); + debt = (debt / STEPMULADJ) + 1; + debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; + return debt; } } - /* -** performs a basic GC step -*/ -void luaC_forcestep (lua_State *L) { - global_State *g = G(L); - int i; - if (isgenerational(g)) generationalcollection(L); - else incstep(L); - /* run a few finalizers (or all of them at the end of a collect cycle) */ - for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++) - GCTM(L, 1); /* call one finalizer */ -} - - -/* -** performs a basic GC step only if collector is running +** performs a basic GC step when collector is running */ void luaC_step (lua_State *L) { global_State *g = G(L); - if (g->gcrunning) luaC_forcestep(L); - else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */ + l_mem debt = getdebt(g); /* GC deficit (be paid now) */ + if (!g->gcrunning) { /* not running? */ + luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ + return; + } + do { /* repeat until pause or enough "credit" (negative debt) */ + lu_mem work = singlestep(L); /* perform one single step */ + debt -= work; + } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); + if (g->gcstate == GCSpause) + setpause(g); /* pause until next cycle */ + else { + debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ + luaE_setdebt(g, debt); + runafewfinalizers(L); + } } - /* -** performs a full GC cycle; if "isemergency", does not call -** finalizers (which could change stack positions) +** Performs a full GC cycle; if 'isemergency', set a flag to avoid +** some operations which could change the interpreter state in some +** unexpected ways (running finalizers and shrinking some structures). +** Before running the collection, check 'keepinvariant'; if it is true, +** there may be some objects marked as black, so the collector has +** to sweep all objects to turn them back to white (as white has not +** changed, nothing will be collected). */ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); - int origkind = g->gckind; - lua_assert(origkind != KGC_EMERGENCY); - if (isemergency) /* do not run finalizers during emergency GC */ - g->gckind = KGC_EMERGENCY; - else { - g->gckind = KGC_NORMAL; - callallpendingfinalizers(L, 1); - } - if (keepinvariant(g)) { /* may there be some black objects? */ - /* must sweep all objects to turn them back to white - (as white has not changed, nothing will be collected) */ - entersweep(L); + lua_assert(g->gckind == KGC_NORMAL); + if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ + if (keepinvariant(g)) { /* black objects? */ + entersweep(L); /* sweep everything to turn them back to white */ } /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */ - luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */ - if (origkind == KGC_GEN) { /* generational mode? */ - /* generational mode must be kept in propagate phase */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - } - g->gckind = origkind; - setpause(g, gettotalbytes(g)); - if (!isemergency) /* do not run finalizers during emergency GC */ - callallpendingfinalizers(L, 1); + luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ + /* estimate must be correct after a full GC cycle */ + lua_assert(g->GCestimate == gettotalbytes(g)); + luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + g->gckind = KGC_NORMAL; + setpause(g); } /* }====================================================== */ diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 84bb1cdf..aed3e18a 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -38,36 +38,27 @@ */ #define GCSpropagate 0 #define GCSatomic 1 -#define GCSsweepstring 2 -#define GCSsweepudata 3 -#define GCSsweep 4 -#define GCSpause 5 +#define GCSswpallgc 2 +#define GCSswpfinobj 3 +#define GCSswptobefnz 4 +#define GCSswpend 5 +#define GCScallfin 6 +#define GCSpause 7 #define issweepphase(g) \ - (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) + (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) -#define isgenerational(g) ((g)->gckind == KGC_GEN) /* -** macros to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a non-generational collection, the sweep +** macro to tell when main invariant (white objects cannot point to black +** ones) must be kept. During a collection, the sweep ** phase may break the invariant, as objects turned white may point to ** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. During a generational collection, the -** invariant must be kept all times. +** all objects are white again. */ -#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) - - -/* -** Outside the collector, the state in generational mode is kept in -** 'propagate', so 'keepinvariant' is always true. -*/ -#define keepinvariantout(g) \ - check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ - g->gcstate <= GCSatomic) +#define keepinvariant(g) ((g)->gcstate <= GCSatomic) /* @@ -83,75 +74,74 @@ #define testbit(x,b) testbits(x, bitmask(b)) -/* Layout for bit use in `marked' field: */ +/* Layout for bit use in 'marked' field: */ #define WHITE0BIT 0 /* object is white (type 0) */ #define WHITE1BIT 1 /* object is white (type 1) */ #define BLACKBIT 2 /* object is black */ -#define FINALIZEDBIT 3 /* object has been separated for finalization */ -#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ -#define FIXEDBIT 5 /* object is fixed (should not be collected) */ -#define OLDBIT 6 /* object is old (only in generational mode) */ +#define FINALIZEDBIT 3 /* object has been marked for finalization */ /* bit 7 is currently used by tests (luaL_checkmemory) */ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) -#define iswhite(x) testbits((x)->gch.marked, WHITEBITS) -#define isblack(x) testbit((x)->gch.marked, BLACKBIT) +#define iswhite(x) testbits((x)->marked, WHITEBITS) +#define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ - (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) + (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) -#define isold(x) testbit((x)->gch.marked, OLDBIT) +#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) -/* MOVE OLD rule: whenever an object is moved to the beginning of - a GC list, its old bit must be cleared */ -#define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) - -#define otherwhite(g) (g->currentwhite ^ WHITEBITS) +#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) +#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) -#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) +#define changewhite(x) ((x)->marked ^= WHITEBITS) +#define gray2black(x) l_setbit((x)->marked, BLACKBIT) #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) -#define luaC_condGC(L,c) \ - {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} -#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) +/* +** Does one step of collection when debt becomes positive. 'pre'/'pos' +** allows some adjustments to be done only when needed. macro +** 'condchangemem' is used only for heavy tests (forcing a full +** GC cycle on every opportunity) +*/ +#define luaC_condGC(L,pre,pos) \ + { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ + condchangemem(L,pre,pos); } + +/* more often than not, 'pre'/'pos' are empty */ +#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) -#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)); } +#define luaC_barrier(L,p,v) ( \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) -#define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - luaC_barrierback_(L,p); } +#define luaC_barrierback(L,p,v) ( \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + luaC_barrierback_(L,p) : cast_void(0)) -#define luaC_objbarrier(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)); } +#define luaC_objbarrier(L,p,o) ( \ + (isblack(p) && iswhite(o)) ? \ + luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#define luaC_objbarrierback(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } - -#define luaC_barrierproto(L,p,c) \ - { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } +#define luaC_upvalbarrier(L,uv) ( \ + (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); -LUAI_FUNC void luaC_forcestep (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, - GCObject **list, int offset); +LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); +LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); +LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); -LUAI_FUNC void luaC_changemode (lua_State *L, int mode); +LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); + #endif diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index c1a38304..afcaf98b 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,20 +1,33 @@ /* -** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ +#define linit_c +#define LUA_LIB + /* ** If you embed Lua in your program and need to open the standard ** libraries, call luaL_openlibs in your program. If you need a ** different set of libraries, copy this file to your project and edit ** it to suit your needs. +** +** You can also *preload* libraries, so that a later 'require' can +** open the library, which is already linked to the application. +** For that, do the following code: +** +** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); +** lua_pushcfunction(L, luaopen_modname); +** lua_setfield(L, -2, modname); +** lua_pop(L, 1); // remove PRELOAD table */ +#include "lprefix.h" -#define linit_c -#define LUA_LIB + +#include #include "lua.h" @@ -34,34 +47,22 @@ static const luaL_Reg loadedlibs[] = { {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, - {LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math}, + {LUA_UTF8LIBNAME, luaopen_utf8}, {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - - -/* -** these libs are preloaded and must be required before used -*/ -static const luaL_Reg preloadedlibs[] = { +#if defined(LUA_COMPAT_BITLIB) + {LUA_BITLIBNAME, luaopen_bit32}, +#endif {NULL, NULL} }; LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; - /* call open functions from 'loadedlibs' and set results to global table */ + /* "require" functions from 'loadedlibs' and set results to global table */ for (lib = loadedlibs; lib->func; lib++) { luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); /* remove lib */ } - /* add open functions from 'preloadedlibs' into 'package.preload' table */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); - for (lib = preloadedlibs; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); - } - lua_pop(L, 1); /* remove _PRELOAD table */ } diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 2a4ec4aa..15684035 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,120 +1,140 @@ /* -** $Id: liolib.c,v 2.112.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: liolib.c,v 2.151 2016/12/20 18:37:00 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ +#define liolib_c +#define LUA_LIB -/* -** This definition must come before the inclusion of 'stdio.h'; it -** should not affect non-POSIX systems -*/ -#if !defined(_FILE_OFFSET_BITS) -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif +#include "lprefix.h" +#include #include +#include #include #include #include -#define liolib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#if !defined(lua_checkmode) + /* -** Check whether 'mode' matches '[rwa]%+?b?'. ** Change this macro to accept other modes for 'fopen' besides ** the standard ones. */ -#define lua_checkmode(mode) \ - (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ - (*mode != '+' || ++mode) && /* skip if char is '+' */ \ - (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ - (*mode == '\0')) +#if !defined(l_checkmode) + +/* accepted extensions to 'mode' in 'fopen' */ +#if !defined(L_MODEEXT) +#define L_MODEEXT "b" +#endif + +/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ +static int l_checkmode (const char *mode) { + return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && + (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ + (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ +} #endif /* ** {====================================================== -** lua_popen spawns a new process connected to the current +** l_popen spawns a new process connected to the current ** one through the file streams. ** ======================================================= */ -#if !defined(lua_popen) /* { */ +#if !defined(l_popen) /* { */ -#if defined(LUA_USE_POPEN) /* { */ +#if defined(LUA_USE_POSIX) /* { */ -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) -#define lua_pclose(L,file) ((void)L, pclose(file)) +#define l_popen(L,c,m) (fflush(NULL), popen(c,m)) +#define l_pclose(L,file) (pclose(file)) -#elif defined(LUA_WIN) /* }{ */ - -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) -#define lua_pclose(L,file) ((void)L, _pclose(file)) +#elif defined(LUA_USE_WINDOWS) /* }{ */ +#define l_popen(L,c,m) (_popen(c,m)) +#define l_pclose(L,file) (_pclose(file)) #else /* }{ */ -#define lua_popen(L,c,m) ((void)((void)c, m), \ - luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) -#define lua_pclose(L,file) ((void)((void)L, file), -1) - +/* ISO C definitions */ +#define l_popen(L,c,m) \ + ((void)((void)c, m), \ + luaL_error(L, "'popen' not supported"), \ + (FILE*)0) +#define l_pclose(L,file) ((void)L, (void)file, -1) #endif /* } */ -#endif /* } */ +#endif /* } */ /* }====================================================== */ +#if !defined(l_getc) /* { */ + +#if defined(LUA_USE_POSIX) +#define l_getc(f) getc_unlocked(f) +#define l_lockfile(f) flockfile(f) +#define l_unlockfile(f) funlockfile(f) +#else +#define l_getc(f) getc(f) +#define l_lockfile(f) ((void)0) +#define l_unlockfile(f) ((void)0) +#endif + +#endif /* } */ + + /* ** {====================================================== -** lua_fseek: configuration for longer offsets +** l_fseek: configuration for longer offsets ** ======================================================= */ -#if !defined(lua_fseek) && !defined(LUA_ANSI) /* { */ +#if !defined(l_fseek) /* { */ #if defined(LUA_USE_POSIX) /* { */ +#include + #define l_fseek(f,o,w) fseeko(f,o,w) #define l_ftell(f) ftello(f) #define l_seeknum off_t -#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \ +#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ -/* Windows (but not DDK) and Visual C++ 2005 or higher */ +/* Windows (but not DDK) and Visual C++ 2005 or higher */ #define l_fseek(f,o,w) _fseeki64(f,o,w) #define l_ftell(f) _ftelli64(f) #define l_seeknum __int64 -#endif /* } */ +#else /* }{ */ -#endif /* } */ - - -#if !defined(l_fseek) /* default definitions */ +/* ISO C definitions */ #define l_fseek(f,o,w) fseek(f,o,w) #define l_ftell(f) ftell(f) #define l_seeknum long -#endif + +#endif /* } */ + +#endif /* } */ /* }====================================================== */ #define IO_PREFIX "_IO_" +#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) #define IO_INPUT (IO_PREFIX "input") #define IO_OUTPUT (IO_PREFIX "output") @@ -161,9 +181,9 @@ static FILE *tofile (lua_State *L) { /* -** When creating file handles, always creates a `closed' file handle +** When creating file handles, always creates a 'closed' file handle ** before opening the actual file; so, if there is a memory error, the -** file is not left opened. +** handle is in a consistent state. */ static LStream *newprefile (lua_State *L) { LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); @@ -173,9 +193,14 @@ static LStream *newprefile (lua_State *L) { } +/* +** Calls the 'close' function from a file handle. The 'volatile' avoids +** a bug in some versions of the Clang compiler (e.g., clang 3.0 for +** 32 bits). +*/ static int aux_close (lua_State *L) { LStream *p = tolstream(L); - lua_CFunction cf = p->closef; + volatile lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ } @@ -219,7 +244,7 @@ static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); p->f = fopen(fname, mode); if (p->f == NULL) - luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); + luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -228,7 +253,7 @@ static int io_open (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ - luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); + luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -239,7 +264,7 @@ static int io_open (lua_State *L) { */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); - return luaL_execresult(L, lua_pclose(L, p->f)); + return luaL_execresult(L, l_pclose(L, p->f)); } @@ -247,7 +272,7 @@ static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); - p->f = lua_popen(L, filename, mode); + p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -265,7 +290,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); + luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -300,15 +325,18 @@ static int io_output (lua_State *L) { static int io_readline (lua_State *L); +/* +** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit +** in the limit for upvalues of a closure) +*/ +#define MAXARGLINE 250 + static void aux_lines (lua_State *L, int toclose) { - int i; int n = lua_gettop(L) - 1; /* number of arguments to read */ - /* ensure that arguments will fit here and into 'io_readline' stack */ - luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); - lua_pushvalue(L, 1); /* file handle */ + luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); lua_pushinteger(L, n); /* number of arguments to read */ lua_pushboolean(L, toclose); /* close/not close file when finished */ - for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ + lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ lua_pushcclosure(L, io_readline, 3 + n); } @@ -347,13 +375,91 @@ static int io_lines (lua_State *L) { */ -static int read_number (lua_State *L, FILE *f) { - lua_Number d; - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { - lua_pushnumber(L, d); - return 1; +/* maximum length of a numeral */ +#if !defined (L_MAXLENNUM) +#define L_MAXLENNUM 200 +#endif + + +/* auxiliary structure used by 'read_number' */ +typedef struct { + FILE *f; /* file being read */ + int c; /* current character (look ahead) */ + int n; /* number of elements in buffer 'buff' */ + char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ +} RN; + + +/* +** Add current char to buffer (if not out of space) and read next one +*/ +static int nextc (RN *rn) { + if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ + rn->buff[0] = '\0'; /* invalidate result */ + return 0; /* fail */ } else { + rn->buff[rn->n++] = rn->c; /* save current char */ + rn->c = l_getc(rn->f); /* read next one */ + return 1; + } +} + + +/* +** Accept current char if it is in 'set' (of size 2) +*/ +static int test2 (RN *rn, const char *set) { + if (rn->c == set[0] || rn->c == set[1]) + return nextc(rn); + else return 0; +} + + +/* +** Read a sequence of (hex)digits +*/ +static int readdigits (RN *rn, int hex) { + int count = 0; + while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) + count++; + return count; +} + + +/* +** Read a number: first reads a valid prefix of a numeral into a buffer. +** Then it calls 'lua_stringtonumber' to check whether the format is +** correct and to convert it to a Lua number +*/ +static int read_number (lua_State *L, FILE *f) { + RN rn; + int count = 0; + int hex = 0; + char decp[2]; + rn.f = f; rn.n = 0; + decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ + decp[1] = '.'; /* always accept a dot */ + l_lockfile(rn.f); + do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ + test2(&rn, "-+"); /* optional signal */ + if (test2(&rn, "00")) { + if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ + else count = 1; /* count initial '0' as a valid digit */ + } + count += readdigits(&rn, hex); /* integral part */ + if (test2(&rn, decp)) /* decimal point? */ + count += readdigits(&rn, hex); /* fractional part */ + if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ + test2(&rn, "-+"); /* exponent signal */ + readdigits(&rn, 0); /* exponent digits */ + } + ungetc(rn.c, rn.f); /* unread look-ahead char */ + l_unlockfile(rn.f); + rn.buff[rn.n] = '\0'; /* finish string */ + if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ + return 1; /* ok */ + else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ } @@ -362,48 +468,42 @@ static int read_number (lua_State *L, FILE *f) { static int test_eof (lua_State *L, FILE *f) { int c = getc(f); - ungetc(c, f); - lua_pushlstring(L, NULL, 0); + ungetc(c, f); /* no-op when c == EOF */ + lua_pushliteral(L, ""); return (c != EOF); } static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; + int c = '\0'; luaL_buffinit(L, &b); - for (;;) { - size_t l; - char *p = luaL_prepbuffer(&b); - if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ - luaL_pushresult(&b); /* close buffer */ - return (lua_rawlen(L, -1) > 0); /* check whether read something */ - } - l = strlen(p); - if (l == 0 || p[l-1] != '\n') - luaL_addsize(&b, l); - else { - luaL_addsize(&b, l - chop); /* chop 'eol' if needed */ - luaL_pushresult(&b); /* close buffer */ - return 1; /* read at least an `eol' */ - } + while (c != EOF && c != '\n') { /* repeat until end of line */ + char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ + int i = 0; + l_lockfile(f); /* no memory errors can happen inside the lock */ + while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') + buff[i++] = c; + l_unlockfile(f); + luaL_addsize(&b, i); } + if (!chop && c == '\n') /* want a newline and have one? */ + luaL_addchar(&b, c); /* add ending newline to result */ + luaL_pushresult(&b); /* close buffer */ + /* return ok if read something (either a newline or something else) */ + return (c == '\n' || lua_rawlen(L, -1) > 0); } -#define MAX_SIZE_T (~(size_t)0) - static void read_all (lua_State *L, FILE *f) { - size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ + size_t nr; luaL_Buffer b; luaL_buffinit(L, &b); - for (;;) { - char *p = luaL_prepbuffsize(&b, rlen); - size_t nr = fread(p, sizeof(char), rlen, f); + do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ + char *p = luaL_prepbuffer(&b); + nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); luaL_addsize(&b, nr); - if (nr < rlen) break; /* eof? */ - else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ - rlen *= 2; /* double buffer size at each iteration */ - } + } while (nr == LUAL_BUFFERSIZE); luaL_pushresult(&b); /* close buffer */ } @@ -435,13 +535,13 @@ static int g_read (lua_State *L, FILE *f, int first) { success = 1; for (n = first; nargs-- && success; n++) { if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)lua_tointeger(L, n); + size_t l = (size_t)luaL_checkinteger(L, n); success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); } else { - const char *p = lua_tostring(L, n); - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); - switch (p[1]) { + const char *p = luaL_checkstring(L, n); + if (*p == '*') p++; /* skip optional '*' (for compatibility) */ + switch (*p) { case 'n': /* number */ success = read_number(L, f); break; @@ -488,11 +588,12 @@ static int io_readline (lua_State *L) { if (isclosed(p)) /* file is already closed? */ return luaL_error(L, "file is already closed"); lua_settop(L , 1); + luaL_checkstack(L, n, "too many arguments"); for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ - if (!lua_isnil(L, -n)) /* read at least one value? */ + if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ @@ -517,8 +618,12 @@ static int g_write (lua_State *L, FILE *f, int arg) { for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ - status = status && - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + int len = lua_isinteger(L, arg) + ? fprintf(f, LUA_INTEGER_FMT, + (LUAI_UACINT)lua_tointeger(L, arg)) + : fprintf(f, LUA_NUMBER_FMT, + (LUAI_UACNUMBER)lua_tonumber(L, arg)); + status = status && (len > 0); } else { size_t l; @@ -548,15 +653,15 @@ static int f_seek (lua_State *L) { static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L); int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Number p3 = luaL_optnumber(L, 3, 0); + lua_Integer p3 = luaL_optinteger(L, 3, 0); l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Number)offset == p3, 3, + luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); op = l_fseek(f, offset, mode[op]); if (op) return luaL_fileresult(L, 0, NULL); /* error */ else { - lua_pushnumber(L, (lua_Number)l_ftell(f)); + lua_pushinteger(L, (lua_Integer)l_ftell(f)); return 1; } } @@ -568,7 +673,7 @@ static int f_setvbuf (lua_State *L) { FILE *f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], sz); + int res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index c4b820e8..70328273 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,20 +1,24 @@ /* -** $Id: llex.c,v 2.63.1.2 2013/08/30 15:49:41 roberto Exp $ +** $Id: llex.c,v 2.96 2016/05/02 14:02:12 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ +#define llex_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define llex_c -#define LUA_CORE - #include "lua.h" #include "lctype.h" +#include "ldebug.h" #include "ldo.h" +#include "lgc.h" #include "llex.h" #include "lobject.h" #include "lparser.h" @@ -38,8 +42,9 @@ static const char *const luaX_tokens [] = { "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", - "..", "...", "==", ">=", "<=", "~=", "::", "", - "", "", "" + "//", "..", "...", "==", ">=", "<=", "~=", + "<<", ">>", "::", "", + "", "", "", "" }; @@ -53,7 +58,7 @@ static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZET/2) + if (luaZ_sizebuffer(b) >= MAX_SIZE/2) lexerror(ls, "lexical element too long", 0); newsize = luaZ_sizebuffer(b) * 2; luaZ_resizebuffer(ls->L, b, newsize); @@ -64,24 +69,25 @@ static void save (LexState *ls, int c) { void luaX_init (lua_State *L) { int i; + TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ + luaC_fix(L, obj2gco(e)); /* never collect this name */ for (i=0; itsv.extra = cast_byte(i+1); /* reserved word */ + luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */ + ts->extra = cast_byte(i+1); /* reserved word */ } } const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast(unsigned char, token)); - return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : - luaO_pushfstring(ls->L, "char(%d)", token); + lua_assert(token == cast_uchar(token)); + return luaO_pushfstring(ls->L, "'%c'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ - return luaO_pushfstring(ls->L, LUA_QS, s); + return luaO_pushfstring(ls->L, "'%s'", s); else /* names, strings, and numerals */ return s; } @@ -90,11 +96,10 @@ const char *luaX_token2str (LexState *ls, int token) { static const char *txtToken (LexState *ls, int token) { switch (token) { - case TK_NAME: - case TK_STRING: - case TK_NUMBER: + case TK_NAME: case TK_STRING: + case TK_FLT: case TK_INT: save(ls, '\0'); - return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); + return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); default: return luaX_token2str(ls, token); } @@ -102,9 +107,7 @@ static const char *txtToken (LexState *ls, int token) { static l_noret lexerror (LexState *ls, const char *msg, int token) { - char buff[LUA_IDSIZE]; - luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE); - msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); + msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); if (token) luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); luaD_throw(ls->L, LUA_ERRSYNTAX); @@ -117,24 +120,24 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** creates a new string and anchors it in function's table so that -** it will not be collected until the end of the function's compilation -** (by that time it should be anchored in function's prototype) +** creates a new string and anchors it in scanner's table so that +** it will not be collected until the end of the compilation +** (by that time it should be anchored somewhere) */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; - TValue *o; /* entry for `str' */ + TValue *o; /* entry for 'str' */ TString *ts = luaS_newlstr(L, str, l); /* create new string */ setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->fs->h, L->top - 1); - if (ttisnil(o)) { /* not in use yet? (see 'addK') */ + o = luaH_set(L, ls->h, L->top - 1); + if (ttisnil(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setbvalue(o, 1); /* t[string] = true */ luaC_checkGC(L); } else { /* string already present */ - ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ + ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ } L->top--; /* remove string from stack */ return ts; @@ -148,17 +151,17 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { static void inclinenumber (LexState *ls) { int old = ls->current; lua_assert(currIsNewline(ls)); - next(ls); /* skip `\n' or `\r' */ + next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip `\n\r' or `\r\n' */ + next(ls); /* skip '\n\r' or '\r\n' */ if (++ls->linenumber >= MAX_INT) - luaX_syntaxerror(ls, "chunk has too many lines"); + lexerror(ls, "chunk has too many lines", 0); } void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar) { - ls->decpoint = '.'; + ls->t.token = 0; ls->L = L; ls->current = firstchar; ls->lookahead.token = TK_EOS; /* no look-ahead token */ @@ -167,8 +170,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_new(L, LUA_ENV); /* create env name */ - luaS_fix(ls->envn); /* never collect this name */ + ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } @@ -181,78 +183,70 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, */ - -static int check_next (LexState *ls, const char *set) { - if (ls->current == '\0' || !strchr(set, ls->current)) - return 0; - save_and_next(ls); - return 1; -} - - -/* -** change all characters 'from' in buffer to 'to' -*/ -static void buffreplace (LexState *ls, char from, char to) { - size_t n = luaZ_bufflen(ls->buff); - char *p = luaZ_buffer(ls->buff); - while (n--) - if (p[n] == from) p[n] = to; -} - - -#if !defined(getlocaledecpoint) -#define getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - - -#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) - -/* -** in case of format error, try to change decimal point separator to -** the one defined in the current locale and check again -*/ -static void trydecpoint (LexState *ls, SemInfo *seminfo) { - char old = ls->decpoint; - ls->decpoint = getlocaledecpoint(); - buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ - if (!buff2d(ls->buff, &seminfo->r)) { - /* format error with correct decimal point: no more options */ - buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - lexerror(ls, "malformed number", TK_NUMBER); +static int check_next1 (LexState *ls, int c) { + if (ls->current == c) { + next(ls); + return 1; } + else return 0; +} + + +/* +** Check whether current char is in set 'set' (with two chars) and +** saves it +*/ +static int check_next2 (LexState *ls, const char *set) { + lua_assert(set[2] == '\0'); + if (ls->current == set[0] || ls->current == set[1]) { + save_and_next(ls); + return 1; + } + else return 0; } /* LUA_NUMBER */ /* -** this function is quite liberal in what it accepts, as 'luaO_str2d' +** this function is quite liberal in what it accepts, as 'luaO_str2num' ** will reject ill-formed numerals. */ -static void read_numeral (LexState *ls, SemInfo *seminfo) { +static int read_numeral (LexState *ls, SemInfo *seminfo) { + TValue obj; const char *expo = "Ee"; int first = ls->current; lua_assert(lisdigit(ls->current)); save_and_next(ls); - if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ + if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next(ls, expo)) /* exponent part? */ - check_next(ls, "+-"); /* optional exponent sign */ - if (lisxdigit(ls->current) || ls->current == '.') + if (check_next2(ls, expo)) /* exponent part? */ + check_next2(ls, "-+"); /* optional exponent sign */ + if (lisxdigit(ls->current)) save_and_next(ls); - else break; + else if (ls->current == '.') + save_and_next(ls); + else break; } save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ - trydecpoint(ls, seminfo); /* try to update decimal point separator */ + if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ + lexerror(ls, "malformed number", TK_FLT); + if (ttisinteger(&obj)) { + seminfo->i = ivalue(&obj); + return TK_INT; + } + else { + lua_assert(ttisfloat(&obj)); + seminfo->r = fltvalue(&obj); + return TK_FLT; + } } /* -** skip a sequence '[=*[' or ']=*]' and return its number of '='s or -** -1 if sequence is malformed +** 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) */ static int skip_sep (LexState *ls) { int count = 0; @@ -268,18 +262,22 @@ static int skip_sep (LexState *ls) { static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { - save_and_next(ls); /* skip 2nd `[' */ + int line = ls->linenumber; /* initial line (for error message) */ + save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ inclinenumber(ls); /* skip it */ for (;;) { switch (ls->current) { - case EOZ: - lexerror(ls, (seminfo) ? "unfinished long string" : - "unfinished long comment", TK_EOS); + case EOZ: { /* error */ + const char *what = (seminfo ? "string" : "comment"); + const char *msg = luaO_pushfstring(ls->L, + "unfinished long %s (starting at line %d)", what, line); + lexerror(ls, msg, TK_EOS); break; /* to avoid warnings */ + } case ']': { if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `]' */ + save_and_next(ls); /* skip 2nd ']' */ goto endloop; } break; @@ -302,40 +300,65 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { } -static void escerror (LexState *ls, int *c, int n, const char *msg) { - int i; - luaZ_resetbuffer(ls->buff); /* prepare error message */ - save(ls, '\\'); - for (i = 0; i < n && c[i] != EOZ; i++) - save(ls, c[i]); - lexerror(ls, msg, TK_STRING); +static void esccheck (LexState *ls, int c, const char *msg) { + if (!c) { + if (ls->current != EOZ) + save_and_next(ls); /* add current to buffer for error message */ + lexerror(ls, msg, TK_STRING); + } +} + + +static int gethexa (LexState *ls) { + save_and_next(ls); + esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); + return luaO_hexavalue(ls->current); } static int readhexaesc (LexState *ls) { - int c[3], i; /* keep input for error message */ - int r = 0; /* result accumulator */ - c[0] = 'x'; /* for error message */ - for (i = 1; i < 3; i++) { /* read two hexadecimal digits */ - c[i] = next(ls); - if (!lisxdigit(c[i])) - escerror(ls, c, i + 1, "hexadecimal digit expected"); - r = (r << 4) + luaO_hexavalue(c[i]); - } + int r = gethexa(ls); + r = (r << 4) + gethexa(ls); + luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ return r; } +static unsigned long readutf8esc (LexState *ls) { + unsigned long r; + int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ + save_and_next(ls); /* skip 'u' */ + esccheck(ls, ls->current == '{', "missing '{'"); + r = gethexa(ls); /* must have at least one digit */ + while ((save_and_next(ls), lisxdigit(ls->current))) { + i++; + r = (r << 4) + luaO_hexavalue(ls->current); + esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large"); + } + esccheck(ls, ls->current == '}', "missing '}'"); + next(ls); /* skip '}' */ + luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ + return r; +} + + +static void utf8esc (LexState *ls) { + char buff[UTF8BUFFSZ]; + int n = luaO_utf8esc(buff, readutf8esc(ls)); + for (; n > 0; n--) /* add 'buff' to string */ + save(ls, buff[UTF8BUFFSZ - n]); +} + + static int readdecesc (LexState *ls) { - int c[3], i; + int i; int r = 0; /* result accumulator */ for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ - c[i] = ls->current; - r = 10*r + c[i] - '0'; - next(ls); + r = 10*r + ls->current - '0'; + save_and_next(ls); } - if (r > UCHAR_MAX) - escerror(ls, c, i, "decimal escape too large"); + esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); + luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ return r; } @@ -353,7 +376,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { break; /* to avoid warnings */ case '\\': { /* escape sequences */ int c; /* final character to be saved */ - next(ls); /* do not save the `\' */ + save_and_next(ls); /* keep '\\' for error messages */ switch (ls->current) { case 'a': c = '\a'; goto read_save; case 'b': c = '\b'; goto read_save; @@ -363,12 +386,14 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { case 't': c = '\t'; goto read_save; case 'v': c = '\v'; goto read_save; case 'x': c = readhexaesc(ls); goto read_save; + case 'u': utf8esc(ls); goto no_save; case '\n': case '\r': inclinenumber(ls); c = '\n'; goto only_save; case '\\': case '\"': case '\'': c = ls->current; goto read_save; case EOZ: goto no_save; /* will raise an error next loop */ case 'z': { /* zap following span of spaces */ + luaZ_buffremove(ls->buff, 1); /* remove '\\' */ next(ls); /* skip the 'z' */ while (lisspace(ls->current)) { if (currIsNewline(ls)) inclinenumber(ls); @@ -377,15 +402,18 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { goto no_save; } default: { - if (!lisdigit(ls->current)) - escerror(ls, &ls->current, 1, "invalid escape sequence"); - /* digital escape \ddd */ - c = readdecesc(ls); + esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); + c = readdecesc(ls); /* digital escape '\ddd' */ goto only_save; } } - read_save: next(ls); /* read next character */ - only_save: save(ls, c); /* save 'c' */ + read_save: + next(ls); + /* go through */ + only_save: + luaZ_buffremove(ls->buff, 1); /* remove '\\' */ + save(ls, c); + /* go through */ no_save: break; } default: @@ -417,7 +445,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { next(ls); if (ls->current == '[') { /* long comment? */ int sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ + luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ @@ -435,33 +463,41 @@ static int llex (LexState *ls, SemInfo *seminfo) { read_long_string(ls, seminfo, sep); return TK_STRING; } - else if (sep == -1) return '['; - else lexerror(ls, "invalid long string delimiter", TK_STRING); + else if (sep != -1) /* '[=...' missing second bracket */ + lexerror(ls, "invalid long string delimiter", TK_STRING); + return '['; } case '=': { next(ls); - if (ls->current != '=') return '='; - else { next(ls); return TK_EQ; } + if (check_next1(ls, '=')) return TK_EQ; + else return '='; } case '<': { next(ls); - if (ls->current != '=') return '<'; - else { next(ls); return TK_LE; } + if (check_next1(ls, '=')) return TK_LE; + else if (check_next1(ls, '<')) return TK_SHL; + else return '<'; } case '>': { next(ls); - if (ls->current != '=') return '>'; - else { next(ls); return TK_GE; } + if (check_next1(ls, '=')) return TK_GE; + else if (check_next1(ls, '>')) return TK_SHR; + else return '>'; + } + case '/': { + next(ls); + if (check_next1(ls, '/')) return TK_IDIV; + else return '/'; } case '~': { next(ls); - if (ls->current != '=') return '~'; - else { next(ls); return TK_NE; } + if (check_next1(ls, '=')) return TK_NE; + else return '~'; } case ':': { next(ls); - if (ls->current != ':') return ':'; - else { next(ls); return TK_DBCOLON; } + if (check_next1(ls, ':')) return TK_DBCOLON; + else return ':'; } case '"': case '\'': { /* short literal strings */ read_string(ls, ls->current, seminfo); @@ -469,18 +505,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '.': { /* '.', '..', '...', or number */ save_and_next(ls); - if (check_next(ls, ".")) { - if (check_next(ls, ".")) + if (check_next1(ls, '.')) { + if (check_next1(ls, '.')) return TK_DOTS; /* '...' */ else return TK_CONCAT; /* '..' */ } else if (!lisdigit(ls->current)) return '.'; - /* else go through */ + else return read_numeral(ls, seminfo); } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { - read_numeral(ls, seminfo); - return TK_NUMBER; + return read_numeral(ls, seminfo); } case EOZ: { return TK_EOS; @@ -495,7 +530,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { luaZ_bufflen(ls->buff)); seminfo->ts = ts; if (isreserved(ts)) /* reserved word? */ - return ts->tsv.extra - 1 + FIRST_RESERVED; + return ts->extra - 1 + FIRST_RESERVED; else { return TK_NAME; } diff --git a/3rd/lua/llex.h b/3rd/lua/llex.h index a4acdd30..2363d87e 100644 --- a/3rd/lua/llex.h +++ b/3rd/lua/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -14,6 +14,10 @@ #define FIRST_RESERVED 257 +#if !defined(LUA_ENV) +#define LUA_ENV "_ENV" +#endif + /* * WARNING: if you change the order of this enumeration, @@ -26,8 +30,10 @@ enum RESERVED { TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ - TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, - TK_NUMBER, TK_NAME, TK_STRING + TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, + TK_SHL, TK_SHR, + TK_DBCOLON, TK_EOS, + TK_FLT, TK_INT, TK_NAME, TK_STRING }; /* number of reserved words */ @@ -36,6 +42,7 @@ enum RESERVED { typedef union { lua_Number r; + lua_Integer i; TString *ts; } SemInfo; /* semantics information */ @@ -51,17 +58,17 @@ typedef struct Token { typedef struct LexState { int current; /* current character (charint) */ int linenumber; /* input line counter */ - int lastline; /* line of last token `consumed' */ + int lastline; /* line of last token 'consumed' */ Token t; /* current token */ Token lookahead; /* look ahead token */ struct FuncState *fs; /* current function (parser) */ struct lua_State *L; ZIO *z; /* input stream */ Mbuffer *buff; /* buffer for tokens */ + Table *h; /* to avoid collection/reuse strings */ struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ - char decpoint; /* locale decimal point */ } LexState; diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 152dd055..f21377fe 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,6 +1,6 @@ /* -** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $ -** Limits, basic types, and some other `installation-dependent' definitions +** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp $ +** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -14,54 +14,77 @@ #include "lua.h" - -typedef unsigned LUA_INT32 lu_int32; - +/* +** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count +** the total memory used by Lua (in bytes). Usually, 'size_t' and +** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. +*/ +#if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_UMEM lu_mem; - typedef LUAI_MEM l_mem; +#elif LUAI_BITSINT >= 32 /* }{ */ +typedef size_t lu_mem; +typedef ptrdiff_t l_mem; +#else /* 16-bit ints */ /* }{ */ +typedef unsigned long lu_mem; +typedef long l_mem; +#endif /* } */ - -/* chars used as small naturals (so that `char' is reserved for characters) */ +/* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; -#define MAX_SIZET ((size_t)(~(size_t)0)-2) +/* maximum value for size_t */ +#define MAX_SIZET ((size_t)(~(size_t)0)) -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) - -#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) +/* maximum size visible for Lua (must be representable in a lua_Integer */ +#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ + : (size_t)(LUA_MAXINTEGER)) -#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ +#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) + +#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) + + +#define MAX_INT INT_MAX /* maximum value of an int */ + /* -** conversion of pointer to integer +** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer ** cannot hold the whole pointer value */ -#define IntPoint(p) ((unsigned int)(lu_mem)(p)) +#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) /* type to ensure maximum alignment */ -#if !defined(LUAI_USER_ALIGNMENT_T) -#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } +#if defined(LUAI_USER_ALIGNMENT_T) +typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; +#else +typedef union { + lua_Number n; + double u; + void *s; + lua_Integer i; + long l; +} L_Umaxalign; #endif -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; -/* result of a `usual argument conversion' over lua_Number */ +/* types of 'usual argument conversions' for lua_Number and lua_Integer */ typedef LUAI_UACNUMBER l_uacNumber; +typedef LUAI_UACINT l_uacInt; /* internal assertions for in-house debugging */ #if defined(lua_assert) #define check_exp(c,e) (lua_assert(c), (e)) /* to avoid problems with conditions too long */ -#define lua_longassert(c) { if (!(c)) lua_assert(0); } +#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) #else #define lua_assert(c) ((void)0) #define check_exp(c,e) (e) @@ -72,38 +95,49 @@ typedef LUAI_UACNUMBER l_uacNumber; ** assertion for checking API calls */ #if !defined(luai_apicheck) - -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(L,e) assert(e) -#else -#define luai_apicheck(L,e) lua_assert(e) -#endif - +#define luai_apicheck(l,e) lua_assert(e) #endif #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) +/* macro to avoid warnings about unused variables */ #if !defined(UNUSED) -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ +#define UNUSED(x) ((void)(x)) #endif +/* type casts (a macro highlights casts in the code) */ #define cast(t, exp) ((t)(exp)) +#define cast_void(i) cast(void, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_uchar(i) cast(unsigned char, (i)) +/* cast a signed lua_Integer to lua_Unsigned */ +#if !defined(l_castS2U) +#define l_castS2U(i) ((lua_Unsigned)(i)) +#endif + +/* +** cast a lua_Unsigned to a signed lua_Integer; this cast is +** not strict ISO C, but two-complement architectures should +** work fine. +*/ +#if !defined(l_castU2S) +#define l_castU2S(i) ((lua_Integer)(i)) +#endif + + /* ** non-return type */ #if defined(__GNUC__) #define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && _MSC_VER >= 1200 #define l_noret void __declspec(noreturn) #else #define l_noret void @@ -119,29 +153,50 @@ typedef LUAI_UACNUMBER l_uacNumber; #define LUAI_MAXCCALLS 200 #endif -/* -** maximum number of upvalues in a closure (both C and Lua). (Value -** must fit in an unsigned char.) -*/ -#define MAXUPVAL UCHAR_MAX /* -** type for virtual-machine instructions +** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) */ -typedef lu_int32 Instruction; +#if LUAI_BITSINT >= 32 +typedef unsigned int Instruction; +#else +typedef unsigned long Instruction; +#endif -/* maximum stack for a Lua function */ -#define MAXSTACK 250 +/* +** Maximum length for short strings, that is, strings that are +** internalized. (Cannot be smaller than reserved words or tags for +** metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#if !defined(LUAI_MAXSHORTLEN) +#define LUAI_MAXSHORTLEN 40 +#endif - -/* minimum size for the string table (must be power of 2) */ +/* +** Initial size for the string table (must be power of 2). +** The Lua core alone registers ~50 strings (reserved words + +** metaevent keys + a few others). Libraries would typically add +** a few dozens more. +*/ #if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 32 +#define MINSTRTABSIZE 128 +#endif + + +/* +** Size of cache for strings in the API. 'N' is the number of +** sets (better be a prime) and "M" is the size of each set (M == 1 +** makes a direct cache.) +*/ +#if !defined(STRCACHE_N) +#define STRCACHE_N 53 +#define STRCACHE_M 2 #endif @@ -151,13 +206,21 @@ typedef lu_int32 Instruction; #endif +/* +** macros that are executed whenever program enters the Lua core +** ('lua_lock') and leaves the core ('lua_unlock') +*/ #if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) #endif +/* +** macro executed during Lua functions at points where the +** function can yield. +*/ #if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} #endif @@ -183,127 +246,78 @@ typedef lu_int32 Instruction; #endif #if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) +#define luai_userstateresume(L,n) ((void)L) #endif #if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) +#define luai_userstateyield(L,n) ((void)L) +#endif + + + +/* +** The luai_num* macros define the primitive operations over numbers. +*/ + +/* floor division (defined as 'floor(a/b)') */ +#if !defined(luai_numidiv) +#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) +#endif + +/* float division */ +#if !defined(luai_numdiv) +#define luai_numdiv(L,a,b) ((a)/(b)) #endif /* -** lua_number2int is a macro to convert lua_Number to int. -** lua_number2integer is a macro to convert lua_Number to lua_Integer. -** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. -** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. -** luai_hashnum is a macro to hash a lua_Number value into an integer. -** The hash must be deterministic and give reasonable values for -** both small and large values (outside the range of integers). +** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when +** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of +** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) +** ~= floor(a/b)'. That happens when the division has a non-integer +** negative result, which is equivalent to the test below. */ - -#if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ -/* trick with Microsoft assembler for X86 */ - -#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} -#define lua_number2integer(i,n) lua_number2int(i, n) -#define lua_number2unsigned(i,n) \ - {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} - - -#elif defined(LUA_IEEE754TRICK) /* }{ */ -/* the next trick should work on any machine using IEEE754 with - a 32-bit int type */ - -union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; - -#if !defined(LUA_IEEEENDIAN) /* { */ -#define LUAI_EXTRAIEEE \ - static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; -#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) -#else -#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN -#define LUAI_EXTRAIEEE /* empty */ -#endif /* } */ - -#define lua_number2int32(i,n,t) \ - { LUAI_EXTRAIEEE \ - volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ - (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } - -#define luai_hashnum(i,n) \ - { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ - (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ - -#define lua_number2int(i,n) lua_number2int32(i, n, int) -#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) - -/* the trick can be expanded to lua_Integer when it is a 32-bit value */ -#if defined(LUA_IEEELL) -#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) +#if !defined(luai_nummod) +#define luai_nummod(L,a,b,m) \ + { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } #endif -#endif /* } */ - - -/* the following definitions always work, but may be slow */ - -#if !defined(lua_number2int) -#define lua_number2int(i,n) ((i)=(int)(n)) +/* exponentiation */ +#if !defined(luai_numpow) +#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) #endif -#if !defined(lua_number2integer) -#define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) -#endif - -#if !defined(lua_number2unsigned) /* { */ -/* the following definition assures proper modulo behavior */ -#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) -#include -#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) -#define lua_number2unsigned(i,n) \ - ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) -#else -#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) -#endif -#endif /* } */ - - -#if !defined(lua_unsigned2number) -/* on several machines, coercion from unsigned to double is slow, - so it may be worth to avoid */ -#define lua_unsigned2number(u) \ - (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) +/* the others are quite standard operations */ +#if !defined(luai_numadd) +#define luai_numadd(L,a,b) ((a)+(b)) +#define luai_numsub(L,a,b) ((a)-(b)) +#define luai_nummul(L,a,b) ((a)*(b)) +#define luai_numunm(L,a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) #endif -#if defined(ltable_c) && !defined(luai_hashnum) - -#include -#include - -#define luai_hashnum(i,n) { int e; \ - n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ - lua_number2int(i, n); i += e; } - -#endif - /* ** macro to control inclusion of some hard tests on stack reallocation */ #if !defined(HARDSTACKTESTS) -#define condmovestack(L) ((void)0) +#define condmovestack(L,pre,pos) ((void)0) #else /* realloc stack keeping its size */ -#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) +#define condmovestack(L,pre,pos) \ + { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } #endif #if !defined(HARDMEMTESTS) -#define condchangemem(L) condmovestack(L) +#define condchangemem(L,pre,pos) ((void)0) #else -#define condchangemem(L) \ - ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) +#define condchangemem(L,pre,pos) \ + { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } #endif #endif diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index fe9fc542..b7f8baee 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,16 +1,18 @@ /* -** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ +#define lmathlib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include -#define lmathlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -18,13 +20,30 @@ #undef PI -#define PI ((lua_Number)(3.1415926535897932384626433832795)) -#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) +#define PI (l_mathop(3.141592653589793238462643383279502884)) +#if !defined(l_rand) /* { */ +#if defined(LUA_USE_POSIX) +#define l_rand() random() +#define l_srand(x) srandom(x) +#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */ +#else +#define l_rand() rand() +#define l_srand(x) srand(x) +#define L_RANDMAX RAND_MAX +#endif +#endif /* } */ + static int math_abs (lua_State *L) { - lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); + if (lua_isinteger(L, 1)) { + lua_Integer n = lua_tointeger(L, 1); + if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n); + lua_pushinteger(L, n); + } + else + lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); return 1; } @@ -33,31 +52,16 @@ static int math_sin (lua_State *L) { return 1; } -static int math_sinh (lua_State *L) { - lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_cos (lua_State *L) { lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); return 1; } -static int math_cosh (lua_State *L) { - lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_tan (lua_State *L) { lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); return 1; } -static int math_tanh (lua_State *L) { - lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_asin (lua_State *L) { lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); return 1; @@ -69,49 +73,106 @@ static int math_acos (lua_State *L) { } static int math_atan (lua_State *L) { - lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); + lua_Number y = luaL_checknumber(L, 1); + lua_Number x = luaL_optnumber(L, 2, 1); + lua_pushnumber(L, l_mathop(atan2)(y, x)); return 1; } -static int math_atan2 (lua_State *L) { - lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); + +static int math_toint (lua_State *L) { + int valid; + lua_Integer n = lua_tointegerx(L, 1, &valid); + if (valid) + lua_pushinteger(L, n); + else { + luaL_checkany(L, 1); + lua_pushnil(L); /* value is not convertible to integer */ + } return 1; } -static int math_ceil (lua_State *L) { - lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); - return 1; + +static void pushnumint (lua_State *L, lua_Number d) { + lua_Integer n; + if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */ + lua_pushinteger(L, n); /* result is integer */ + else + lua_pushnumber(L, d); /* result is float */ } + static int math_floor (lua_State *L) { - lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); + if (lua_isinteger(L, 1)) + lua_settop(L, 1); /* integer is its own floor */ + else { + lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); + pushnumint(L, d); + } return 1; } + +static int math_ceil (lua_State *L) { + if (lua_isinteger(L, 1)) + lua_settop(L, 1); /* integer is its own ceil */ + else { + lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); + pushnumint(L, d); + } + return 1; +} + + static int math_fmod (lua_State *L) { - lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); + if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { + lua_Integer d = lua_tointeger(L, 2); + if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ + luaL_argcheck(L, d != 0, 2, "zero"); + lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ + } + else + lua_pushinteger(L, lua_tointeger(L, 1) % d); + } + else + lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), + luaL_checknumber(L, 2))); return 1; } + +/* +** next function does not use 'modf', avoiding problems with 'double*' +** (which is not compatible with 'float*') when lua_Number is not +** 'double'. +*/ static int math_modf (lua_State *L) { - lua_Number ip; - lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); - lua_pushnumber(L, ip); - lua_pushnumber(L, fp); + if (lua_isinteger(L ,1)) { + lua_settop(L, 1); /* number is its own integer part */ + lua_pushnumber(L, 0); /* no fractional part */ + } + else { + lua_Number n = luaL_checknumber(L, 1); + /* integer part (rounds toward zero) */ + lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); + pushnumint(L, ip); + /* fractional part (test needed for inf/-inf) */ + lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); + } return 2; } + static int math_sqrt (lua_State *L) { lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); return 1; } -static int math_pow (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number y = luaL_checknumber(L, 2); - lua_pushnumber(L, l_mathop(pow)(x, y)); + +static int math_ult (lua_State *L) { + lua_Integer a = luaL_checkinteger(L, 1); + lua_Integer b = luaL_checkinteger(L, 2); + lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); return 1; } @@ -122,32 +183,145 @@ static int math_log (lua_State *L) { res = l_mathop(log)(x); else { lua_Number base = luaL_checknumber(L, 2); - if (base == (lua_Number)10.0) res = l_mathop(log10)(x); - else res = l_mathop(log)(x)/l_mathop(log)(base); +#if !defined(LUA_USE_C89) + if (base == l_mathop(2.0)) + res = l_mathop(log2)(x); else +#endif + if (base == l_mathop(10.0)) + res = l_mathop(log10)(x); + else + res = l_mathop(log)(x)/l_mathop(log)(base); } lua_pushnumber(L, res); return 1; } -#if defined(LUA_COMPAT_LOG10) -static int math_log10 (lua_State *L) { - lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); - return 1; -} -#endif - static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; } static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); + lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; } static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); + lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); + return 1; +} + + +static int math_min (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int imin = 1; /* index of current minimum value */ + int i; + luaL_argcheck(L, n >= 1, 1, "value expected"); + for (i = 2; i <= n; i++) { + if (lua_compare(L, i, imin, LUA_OPLT)) + imin = i; + } + lua_pushvalue(L, imin); + return 1; +} + + +static int math_max (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int imax = 1; /* index of current maximum value */ + int i; + luaL_argcheck(L, n >= 1, 1, "value expected"); + for (i = 2; i <= n; i++) { + if (lua_compare(L, imax, i, LUA_OPLT)) + imax = i; + } + lua_pushvalue(L, imax); + return 1; +} + +/* +** This function uses 'double' (instead of 'lua_Number') to ensure that +** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0' +** will keep full precision (ensuring that 'r' is always less than 1.0.) +*/ +static int math_random (lua_State *L) { + lua_Integer low, up; + double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0)); + switch (lua_gettop(L)) { /* check number of arguments */ + case 0: { /* no arguments */ + lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */ + return 1; + } + case 1: { /* only upper limit */ + low = 1; + up = luaL_checkinteger(L, 1); + break; + } + case 2: { /* lower and upper limits */ + low = luaL_checkinteger(L, 1); + up = luaL_checkinteger(L, 2); + break; + } + default: return luaL_error(L, "wrong number of arguments"); + } + /* random integer in the interval [low, up] */ + luaL_argcheck(L, low <= up, 1, "interval is empty"); + luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, + "interval too large"); + r *= (double)(up - low) + 1.0; + lua_pushinteger(L, (lua_Integer)r + low); + return 1; +} + + +static int math_randomseed (lua_State *L) { + l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); + (void)l_rand(); /* discard first value to avoid undesirable correlations */ + return 0; +} + + +static int math_type (lua_State *L) { + if (lua_type(L, 1) == LUA_TNUMBER) { + if (lua_isinteger(L, 1)) + lua_pushliteral(L, "integer"); + else + lua_pushliteral(L, "float"); + } + else { + luaL_checkany(L, 1); + lua_pushnil(L); + } + return 1; +} + + +/* +** {================================================================== +** Deprecated functions (for compatibility only) +** =================================================================== +*/ +#if defined(LUA_COMPAT_MATHLIB) + +static int math_cosh (lua_State *L) { + lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sinh (lua_State *L) { + lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tanh (lua_State *L) { + lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_pow (lua_State *L) { + lua_Number x = luaL_checknumber(L, 1); + lua_Number y = luaL_checknumber(L, 2); + lua_pushnumber(L, l_mathop(pow)(x, y)); return 1; } @@ -160,107 +334,60 @@ static int math_frexp (lua_State *L) { static int math_ldexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); - int ep = luaL_checkint(L, 2); + int ep = (int)luaL_checkinteger(L, 2); lua_pushnumber(L, l_mathop(ldexp)(x, ep)); return 1; } - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmin = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d < dmin) - dmin = d; - } - lua_pushnumber(L, dmin); +static int math_log10 (lua_State *L) { + lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; } +#endif +/* }================================================================== */ -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmax = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d > dmax) - dmax = d; - } - lua_pushnumber(L, dmax); - return 1; -} - - -static int math_random (lua_State *L) { - /* the `%' avoids the (rare) case of r==1, and is needed also because on - some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ - lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, r); /* Number between 0 and 1 */ - break; - } - case 1: { /* only upper limit */ - lua_Number u = luaL_checknumber(L, 1); - luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ - break; - } - case 2: { /* lower and upper limits */ - lua_Number l = luaL_checknumber(L, 1); - lua_Number u = luaL_checknumber(L, 2); - luaL_argcheck(L, l <= u, 2, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - return 1; -} - - -static int math_randomseed (lua_State *L) { - srand(luaL_checkunsigned(L, 1)); - (void)rand(); /* discard first value to avoid undesirable correlations */ - return 0; -} static const luaL_Reg mathlib[] = { {"abs", math_abs}, {"acos", math_acos}, {"asin", math_asin}, - {"atan2", math_atan2}, {"atan", math_atan}, {"ceil", math_ceil}, - {"cosh", math_cosh}, {"cos", math_cos}, {"deg", math_deg}, {"exp", math_exp}, + {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, -#if defined(LUA_COMPAT_LOG10) - {"log10", math_log10}, -#endif + {"ult", math_ult}, {"log", math_log}, {"max", math_max}, {"min", math_min}, {"modf", math_modf}, - {"pow", math_pow}, {"rad", math_rad}, {"random", math_random}, {"randomseed", math_randomseed}, - {"sinh", math_sinh}, {"sin", math_sin}, {"sqrt", math_sqrt}, - {"tanh", math_tanh}, {"tan", math_tan}, + {"type", math_type}, +#if defined(LUA_COMPAT_MATHLIB) + {"atan2", math_atan}, + {"cosh", math_cosh}, + {"sinh", math_sinh}, + {"tanh", math_tanh}, + {"pow", math_pow}, + {"frexp", math_frexp}, + {"ldexp", math_ldexp}, + {"log10", math_log10}, +#endif + /* placeholders */ + {"pi", NULL}, + {"huge", NULL}, + {"maxinteger", NULL}, + {"mininteger", NULL}, {NULL, NULL} }; @@ -272,8 +399,12 @@ LUAMOD_API int luaopen_math (lua_State *L) { luaL_newlib(L, mathlib); lua_pushnumber(L, PI); lua_setfield(L, -2, "pi"); - lua_pushnumber(L, HUGE_VAL); + lua_pushnumber(L, (lua_Number)HUGE_VAL); lua_setfield(L, -2, "huge"); + lua_pushinteger(L, LUA_MAXINTEGER); + lua_setfield(L, -2, "maxinteger"); + lua_pushinteger(L, LUA_MININTEGER); + lua_setfield(L, -2, "mininteger"); return 1; } diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index ee343e3e..0a0476cc 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -1,15 +1,17 @@ /* -** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ - -#include - #define lmem_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -24,15 +26,15 @@ /* ** About the realloc function: ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** (`osize' is the old size, `nsize' is the new size) +** ('osize' is the old size, 'nsize' is the new size) ** -** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no +** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no ** matter 'x'). ** -** * frealloc(ud, p, x, 0) frees the block `p' +** * frealloc(ud, p, x, 0) frees the block 'p' ** (in this specific case, frealloc must return NULL); ** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ANSI C) +** (which is equivalent to free(NULL) in ISO C) ** ** frealloc returns NULL if it cannot create or reallocate the area ** (any reallocation to an equal or smaller size cannot fail!) @@ -83,9 +85,8 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { #endif newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { - api_check(L, nsize > realosize, - "realloc cannot fail when shrinking a block"); - if (g->gcrunning) { + 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 */ } diff --git a/3rd/lua/lmem.h b/3rd/lua/lmem.h index bd4f4e07..30f48489 100644 --- a/3rd/lua/lmem.h +++ b/3rd/lua/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -15,20 +15,32 @@ /* -** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is -** always constant. -** The macro is somewhat complex to avoid warnings: -** +1 avoids warnings of "comparison has constant result"; -** cast to 'void' avoids warnings of "value unused". +** This macro reallocs a vector 'b' from 'on' to 'n' elements, where +** each element has size 'e'. In case of arithmetic overflow of the +** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because +** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). +** +** (The macro is somewhat complex to avoid warnings: The 'sizeof' +** comparison avoids a runtime comparison when overflow cannot occur. +** The compiler should be able to optimize the real test by itself, but +** when it does it, it may give a warning about "comparison is always +** false due to limited range of data type"; the +1 tricks the compiler, +** avoiding this warning but also this optimization.) */ #define luaM_reallocv(L,b,on,n,e) \ - (cast(void, \ - (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ + (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ + ? luaM_toobig(L) : cast_void(0)) , \ luaM_realloc_(L, (b), (on)*(e), (n)*(e))) +/* +** Arrays of chars do not need any test +*/ +#define luaM_reallocvchar(L,b,on,n) \ + cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) + #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) +#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index bedbea3e..4791e748 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -8,22 +8,16 @@ ** systems. */ - -/* -** if needed, includes windows header before everything else -*/ -#if defined(_WIN32) -#include -#endif - - -#include -#include - - #define loadlib_c #define LUA_LIB +#include "lprefix.h" + + +#include +#include +#include + #include "lua.h" #include "lauxlib.h" @@ -31,40 +25,9 @@ /* -** LUA_PATH and LUA_CPATH are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH) -#define LUA_PATH "LUA_PATH" -#endif - -#if !defined(LUA_CPATH) -#define LUA_CPATH "LUA_CPATH" -#endif - -#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - -#define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX -#define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX - -/* -** LUA_PATH_SEP is the character that separates templates in a path. -** LUA_PATH_MARK is the string that marks the substitution points in a -** template. -** LUA_EXEC_DIR in a Windows path is replaced by the executable's -** directory. ** LUA_IGMARK is a mark to ignore all before it when building the ** luaopen_ function name. */ -#if !defined (LUA_PATH_SEP) -#define LUA_PATH_SEP ";" -#endif -#if !defined (LUA_PATH_MARK) -#define LUA_PATH_MARK "?" -#endif -#if !defined (LUA_EXEC_DIR) -#define LUA_EXEC_DIR "!" -#endif #if !defined (LUA_IGMARK) #define LUA_IGMARK "-" #endif @@ -92,29 +55,46 @@ #define LUA_OFSEP "_" -/* table (in the registry) that keeps handles for all loaded C libraries */ -#define CLIBS "_CLIBS" +/* +** unique key for table in the registry that keeps handles +** for all loaded C libraries +*/ +static const int CLIBS = 0; #define LIB_FAIL "open" -/* error codes for ll_loadfunc */ -#define ERRLIB 1 -#define ERRFUNC 2 - -#define setprogdir(L) ((void)0) +#define setprogdir(L) ((void)0) /* ** system-dependent functions */ -static void ll_unloadlib (void *lib); -static void *ll_load (lua_State *L, const char *path, int seeglb); -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); + +/* +** unload library 'lib' +*/ +static void lsys_unloadlib (void *lib); + +/* +** load C library in file 'path'. If 'seeglb', load with all names in +** the library global. +** Returns the library; in case of error, returns NULL plus an +** error string in the stack. +*/ +static void *lsys_load (lua_State *L, const char *path, int seeglb); + +/* +** Try to find a function named 'sym' in library 'lib'. +** Returns the function; in case of error, returns NULL plus an +** error string in the stack. +*/ +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); -#if defined(LUA_USE_DLOPEN) + +#if defined(LUA_USE_DLOPEN) /* { */ /* ** {======================================================================== ** This is an implementation of loadlib based on the dlfcn interface. @@ -126,20 +106,32 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); #include -static void ll_unloadlib (void *lib) { +/* +** Macro to convert pointer-to-void* to pointer-to-function. This cast +** is undefined according to ISO C, but POSIX assumes that it works. +** (The '__extension__' in gnu compilers is only to avoid warnings.) +*/ +#if defined(__GNUC__) +#define cast_func(p) (__extension__ (lua_CFunction)(p)) +#else +#define cast_func(p) ((lua_CFunction)(p)) +#endif + + +static void lsys_unloadlib (void *lib) { dlclose(lib); } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); if (lib == NULL) lua_pushstring(L, dlerror()); return lib; } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); +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()); return f; } @@ -148,14 +140,15 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { -#elif defined(LUA_DL_DLL) +#elif defined(LUA_DL_DLL) /* }{ */ /* ** {====================================================================== ** This is an implementation of loadlib for Windows using native functions. ** ======================================================================= */ -#undef setprogdir +#include + /* ** optional flags for LoadLibraryEx @@ -165,21 +158,30 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { #endif +#undef setprogdir + + +/* +** Replace in the path (on the top of the stack) any occurrence +** of LUA_EXEC_DIR with the executable's path. +*/ static void setprogdir (lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { - *lb = '\0'; + *lb = '\0'; /* cut name on the last '\\' to get the path */ luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); lua_remove(L, -2); /* remove original string */ } } + + static void pusherror (lua_State *L) { int error = GetLastError(); char buffer[128]; @@ -190,12 +192,12 @@ static void pusherror (lua_State *L) { lua_pushfstring(L, "system error %d\n", error); } -static void ll_unloadlib (void *lib) { +static void lsys_unloadlib (void *lib) { FreeLibrary((HMODULE)lib); } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); (void)(seeglb); /* not used: symbols are 'global' by default */ if (lib == NULL) pusherror(L); @@ -203,7 +205,7 @@ static void *ll_load (lua_State *L, const char *path, int seeglb) { } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); if (f == NULL) pusherror(L); return f; @@ -212,7 +214,7 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { /* }====================================================== */ -#else +#else /* }{ */ /* ** {====================================================== ** Fallback for other systems @@ -226,31 +228,95 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { #define DLMSG "dynamic libraries not enabled; check your Lua installation" -static void ll_unloadlib (void *lib) { +static void lsys_unloadlib (void *lib) { (void)(lib); /* not used */ } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { (void)(path); (void)(seeglb); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { (void)(lib); (void)(sym); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } /* }====================================================== */ +#endif /* } */ + + +/* +** {================================================================== +** Set Paths +** =================================================================== +*/ + +/* +** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment +** variables that Lua check to set its paths. +*/ +#if !defined(LUA_PATH_VAR) +#define LUA_PATH_VAR "LUA_PATH" +#endif + +#if !defined(LUA_CPATH_VAR) +#define LUA_CPATH_VAR "LUA_CPATH" #endif -static void *ll_checkclib (lua_State *L, const char *path) { +#define AUXMARK "\1" /* auxiliary mark */ + + +/* +** return registry.LUA_NOENV as a boolean +*/ +static int noenv (lua_State *L) { + int b; + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); + b = lua_toboolean(L, -1); + lua_pop(L, 1); /* remove value */ + return b; +} + + +/* +** Set a path +*/ +static void setpath (lua_State *L, const char *fieldname, + const char *envname, + const char *dft) { + const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); + const char *path = getenv(nver); /* use versioned name */ + if (path == NULL) /* no environment variable? */ + path = getenv(envname); /* try unversioned name */ + if (path == NULL || noenv(L)) /* no environment variable? */ + lua_pushstring(L, dft); /* use default */ + else { + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, + LUA_PATH_SEP AUXMARK LUA_PATH_SEP); + luaL_gsub(L, path, AUXMARK, dft); + lua_remove(L, -2); /* remove result from 1st 'gsub' */ + } + setprogdir(L); + lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ + lua_pop(L, 1); /* pop versioned variable name */ +} + +/* }================================================================== */ + + +/* +** return registry.CLIBS[path] +*/ +static void *checkclib (lua_State *L, const char *path) { void *plib; - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); + lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_getfield(L, -1, path); plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */ @@ -258,8 +324,12 @@ static void *ll_checkclib (lua_State *L, const char *path) { } -static void ll_addtoclib (lua_State *L, const char *path, void *plib) { - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); +/* +** registry.CLIBS[path] = plib -- for queries +** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries +*/ +static void addtoclib (lua_State *L, const char *path, void *plib) { + lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_pushlightuserdata(L, plib); lua_pushvalue(L, -1); lua_setfield(L, -3, path); /* CLIBS[path] = plib */ @@ -269,33 +339,49 @@ static void ll_addtoclib (lua_State *L, const char *path, void *plib) { /* -** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib +** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib ** handles in list CLIBS */ static int gctm (lua_State *L) { - int n = luaL_len(L, 1); + lua_Integer n = luaL_len(L, 1); for (; n >= 1; n--) { /* for each handle, in reverse order */ lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - ll_unloadlib(lua_touserdata(L, -1)); + lsys_unloadlib(lua_touserdata(L, -1)); lua_pop(L, 1); /* pop handle */ } return 0; } -static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { - void *reg = ll_checkclib(L, path); /* check loaded C libraries */ + +/* error codes for 'lookforfunc' */ +#define ERRLIB 1 +#define ERRFUNC 2 + +/* +** Look for a C function named 'sym' in a dynamically loaded library +** 'path'. +** First, check whether the library is already loaded; if not, try +** to load it. +** Then, if 'sym' is '*', return true (as library has been loaded). +** Otherwise, look for symbol 'sym' in the library and push a +** C function with that symbol. +** Return 0 and 'true' or a function in the stack; in case of +** errors, return an error code and an error message in the stack. +*/ +static int lookforfunc (lua_State *L, const char *path, const char *sym) { + void *reg = checkclib(L, path); /* check loaded C libraries */ if (reg == NULL) { /* must load library? */ - reg = ll_load(L, path, *sym == '*'); + reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ if (reg == NULL) return ERRLIB; /* unable to load library */ - ll_addtoclib(L, path, reg); + addtoclib(L, path, reg); } if (*sym == '*') { /* loading only library (no function)? */ lua_pushboolean(L, 1); /* return 'true' */ return 0; /* no errors */ } else { - lua_CFunction f = ll_sym(L, reg, sym); + lua_CFunction f = lsys_sym(L, reg, sym); if (f == NULL) return ERRFUNC; /* unable to find function */ lua_pushcfunction(L, f); /* else create new function */ @@ -307,7 +393,7 @@ static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); - int stat = ll_loadfunc(L, path, init); + int stat = lookforfunc(L, path, init); if (stat == 0) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ @@ -360,7 +446,7 @@ static const char *searchpath (lua_State *L, const char *name, lua_remove(L, -2); /* remove path template */ if (readable(filename)) /* does file exist and is readable? */ return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); + lua_pushfstring(L, "\n\tno file '%s'", filename); lua_remove(L, -2); /* remove file name */ luaL_addvalue(&msg); /* concatenate error msg. entry */ } @@ -390,7 +476,7 @@ static const char *findfile (lua_State *L, const char *name, lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); + luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } @@ -401,8 +487,7 @@ static int checkload (lua_State *L, int stat, const char *filename) { return 2; /* return open function and file name */ } else - return luaL_error(L, "error loading module " LUA_QS - " from file " LUA_QS ":\n\t%s", + return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), filename, lua_tostring(L, -1)); } @@ -416,21 +501,29 @@ static int searcher_Lua (lua_State *L) { } +/* +** Try to find a load function for module 'modname' at file 'filename'. +** First, change '.' to '_' in 'modname'; then, if 'modname' has +** the form X-Y (that is, it has an "ignore mark"), build a function +** name "luaopen_X" and look for it. (For compatibility, if that +** fails, it also tries "luaopen_Y".) If there is no ignore mark, +** look for a function named "luaopen_modname". +*/ static int loadfunc (lua_State *L, const char *filename, const char *modname) { - const char *funcname; + const char *openfunc; const char *mark; modname = luaL_gsub(L, modname, ".", LUA_OFSEP); mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; - funcname = lua_pushlstring(L, modname, mark - modname); - funcname = lua_pushfstring(L, LUA_POF"%s", funcname); - stat = ll_loadfunc(L, filename, funcname); + openfunc = lua_pushlstring(L, modname, mark - modname); + openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); + stat = lookforfunc(L, filename, openfunc); if (stat != ERRFUNC) return stat; modname = mark + 1; /* else go ahead and try old-style name */ } - funcname = lua_pushfstring(L, LUA_POF"%s", modname); - return ll_loadfunc(L, filename, funcname); + openfunc = lua_pushfstring(L, LUA_POF"%s", modname); + return lookforfunc(L, filename, openfunc); } @@ -455,8 +548,7 @@ static int searcher_Croot (lua_State *L) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); + lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); return 1; } } @@ -467,9 +559,8 @@ static int searcher_Croot (lua_State *L) { static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) /* not found? */ + 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; } @@ -479,17 +570,15 @@ static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); - lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ - if (!lua_istable(L, 3)) - luaL_error(L, LUA_QL("package.searchers") " must be a table"); + /* push 'package.searchers' to index 3 in the stack */ + if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) + luaL_error(L, "'package.searchers' must be a table"); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { - lua_rawgeti(L, 3, i); /* get a searcher */ - if (lua_isnil(L, -1)) { /* no more searchers? */ + if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_pushresult(&msg); /* create error message */ - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -1)); + luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } lua_pushstring(L, name); lua_call(L, 1, 2); /* call it */ @@ -507,9 +596,9 @@ static void findloader (lua_State *L, const char *name) { static int ll_require (lua_State *L) { const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); /* _LOADED[name] */ + lua_settop(L, 1); /* LOADED table will be at index 2 */ + lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + lua_getfield(L, 2, name); /* LOADED[name] */ if (lua_toboolean(L, -1)) /* is it there? */ return 1; /* package is already loaded */ /* else must load package */ @@ -519,12 +608,11 @@ static int ll_require (lua_State *L) { lua_insert(L, -2); /* name is 1st argument (before search data) */ lua_call(L, 2, 1); /* run loader to load module */ if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if (lua_isnil(L, -1)) { /* module did not set a value? */ + lua_setfield(L, 2, name); /* LOADED[name] = returned value */ + if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ + lua_setfield(L, 2, name); /* LOADED[name] = true */ } return 1; } @@ -548,7 +636,7 @@ static void set_env (lua_State *L) { if (lua_getstack(L, 1, &ar) == 0 || lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); + luaL_error(L, "'module' not called from a Lua function"); lua_pushvalue(L, -2); /* copy new environment table to top */ lua_setupvalue(L, -2, 1); lua_pop(L, 1); /* remove function */ @@ -587,9 +675,8 @@ static int ll_module (lua_State *L) { int lastarg = lua_gettop(L); /* last parameter */ luaL_pushmodule(L, modname, 1); /* get/create module table */ /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) /* is table an initialized module? */ - lua_pop(L, 1); + if (lua_getfield(L, -1, "_NAME") != LUA_TNIL) + lua_pop(L, 1); /* table is an initialized module */ else { /* no; initialize it */ lua_pop(L, 1); modinit(L, modname); @@ -618,47 +705,18 @@ static int ll_seeall (lua_State *L) { -/* auxiliary mark (for internal use) */ -#define AUXMARK "\1" - - -/* -** return registry.LUA_NOENV as a boolean -*/ -static int noenv (lua_State *L) { - int b; - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - b = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - return b; -} - - -static void setpath (lua_State *L, const char *fieldname, const char *envname1, - const char *envname2, const char *def) { - const char *path = getenv(envname1); - if (path == NULL) /* no environment variable? */ - path = getenv(envname2); /* try alternative name */ - if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, def); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - - static const luaL_Reg pk_funcs[] = { {"loadlib", ll_loadlib}, {"searchpath", ll_searchpath}, #if defined(LUA_COMPAT_MODULE) {"seeall", ll_seeall}, #endif + /* placeholders */ + {"preload", NULL}, + {"cpath", NULL}, + {"path", NULL}, + {"searchers", NULL}, + {"loaded", NULL}, {NULL, NULL} }; @@ -678,43 +736,50 @@ static void createsearcherstable (lua_State *L) { int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); - /* fill it with pre-defined searchers */ + /* fill it with predefined searchers */ for (i=0; searchers[i] != NULL; i++) { lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ lua_pushcclosure(L, searchers[i], 1); lua_rawseti(L, -2, i+1); } +#if defined(LUA_COMPAT_LOADERS) + lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ + lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */ +#endif + lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ +} + + +/* +** create table CLIBS to keep track of loaded C libraries, +** setting a finalizer to close all libraries when closing state. +*/ +static void createclibstable (lua_State *L) { + lua_newtable(L); /* create CLIBS table */ + lua_createtable(L, 0, 1); /* create metatable for CLIBS */ + lua_pushcfunction(L, gctm); + lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ + lua_setmetatable(L, -2); + lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ } LUAMOD_API int luaopen_package (lua_State *L) { - /* create table CLIBS to keep track of loaded C libraries */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); - lua_createtable(L, 0, 1); /* metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); - /* create `package' table */ - luaL_newlib(L, pk_funcs); + createclibstable(L); + luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); -#if defined(LUA_COMPAT_LOADERS) - lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ - lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ -#endif - lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ - /* set field 'path' */ - setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); - /* set field 'cpath' */ - setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT); + /* set paths */ + setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); + setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); lua_setfield(L, -2, "config"); - /* set field `loaded' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + /* set field 'loaded' */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_setfield(L, -2, "loaded"); - /* set field `preload' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); + /* set field 'preload' */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); lua_setfield(L, -2, "preload"); lua_pushglobaltable(L); lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 882d994d..2da76899 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,17 +1,22 @@ /* -** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ +#define lobject_c +#define LUA_CORE + +#include "lprefix.h" + + +#include +#include #include #include #include #include -#define lobject_c -#define LUA_CORE - #include "lua.h" #include "lctype.h" @@ -36,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; int luaO_int2fb (unsigned int x) { int e = 0; /* exponent */ if (x < 8) return x; - while (x >= 0x10) { - x = (x+1) >> 1; + while (x >= (8 << 4)) { /* coarse steps */ + x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ + e += 4; + } + while (x >= (8 << 1)) { /* fine steps */ + x = (x + 1) >> 1; /* x = ceil(x / 2) */ e++; } return ((e+1) << 3) | (cast_int(x) - 8); @@ -46,14 +55,15 @@ int luaO_int2fb (unsigned int x) { /* converts back */ int luaO_fb2int (int x) { - int e = (x >> 3) & 0x1f; - if (e == 0) return x; - else return ((x & 7) + 8) << (e - 1); + return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); } +/* +** Computes ceil(log2(x)) +*/ int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { + static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, @@ -70,29 +80,90 @@ int luaO_ceillog2 (unsigned int x) { } -lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { +static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, + lua_Integer v2) { switch (op) { - case LUA_OPADD: return luai_numadd(NULL, v1, v2); - case LUA_OPSUB: return luai_numsub(NULL, v1, v2); - case LUA_OPMUL: return luai_nummul(NULL, v1, v2); - case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); - case LUA_OPMOD: return luai_nummod(NULL, v1, v2); - case LUA_OPPOW: return luai_numpow(NULL, v1, v2); - case LUA_OPUNM: return luai_numunm(NULL, v1); + case LUA_OPADD: return intop(+, v1, v2); + case LUA_OPSUB:return intop(-, v1, v2); + case LUA_OPMUL:return intop(*, v1, v2); + case LUA_OPMOD: return luaV_mod(L, v1, v2); + case LUA_OPIDIV: return luaV_div(L, v1, v2); + case LUA_OPBAND: return intop(&, v1, v2); + case LUA_OPBOR: return intop(|, v1, v2); + case LUA_OPBXOR: return intop(^, v1, v2); + case LUA_OPSHL: return luaV_shiftl(v1, v2); + case LUA_OPSHR: return luaV_shiftl(v1, -v2); + case LUA_OPUNM: return intop(-, 0, v1); + case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; } } -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return ltolower(c) - 'a' + 10; +static lua_Number numarith (lua_State *L, int op, lua_Number v1, + lua_Number v2) { + switch (op) { + case LUA_OPADD: return luai_numadd(L, v1, v2); + case LUA_OPSUB: return luai_numsub(L, v1, v2); + case LUA_OPMUL: return luai_nummul(L, v1, v2); + case LUA_OPDIV: return luai_numdiv(L, v1, v2); + case LUA_OPPOW: return luai_numpow(L, v1, v2); + case LUA_OPIDIV: return luai_numidiv(L, v1, v2); + case LUA_OPUNM: return luai_numunm(L, v1); + case LUA_OPMOD: { + lua_Number m; + luai_nummod(L, v1, v2, m); + return m; + } + default: lua_assert(0); return 0; + } } -#if !defined(lua_strx2number) +void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, + TValue *res) { + switch (op) { + case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: + case LUA_OPSHL: case LUA_OPSHR: + case LUA_OPBNOT: { /* operate only on integers */ + lua_Integer i1; lua_Integer i2; + if (tointeger(p1, &i1) && tointeger(p2, &i2)) { + setivalue(res, intarith(L, op, i1, i2)); + return; + } + else break; /* go to the end */ + } + case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ + lua_Number n1; lua_Number n2; + if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + setfltvalue(res, numarith(L, op, n1, n2)); + return; + } + else break; /* go to the end */ + } + default: { /* other operations */ + lua_Number n1; lua_Number n2; + if (ttisinteger(p1) && ttisinteger(p2)) { + setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); + return; + } + else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + setfltvalue(res, numarith(L, op, n1, n2)); + return; + } + else break; /* go to the end */ + } + } + /* could not perform raw operation; try metamethod */ + lua_assert(L != NULL); /* should not fail when folding (compile time) */ + luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); +} -#include + +int luaO_hexavalue (int c) { + if (lisdigit(c)) return c - '0'; + else return (ltolower(c) - 'a') + 10; +} static int isneg (const char **s) { @@ -102,122 +173,285 @@ static int isneg (const char **s) { } -static lua_Number readhexa (const char **s, lua_Number r, int *count) { - for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ - r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); - (*count)++; - } - return r; -} +/* +** {================================================================== +** Lua's implementation for 'lua_strx2number' +** =================================================================== +*/ + +#if !defined(lua_strx2number) + +/* maximum number of significant digits to read (to avoid overflows + even with single floats) */ +#define MAXSIGDIG 30 /* ** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { - lua_Number r = 0.0; - int e = 0, i = 0; - int neg = 0; /* 1 if number is negative */ + int dot = lua_getlocaledecpoint(); + lua_Number r = 0.0; /* result (accumulator) */ + int sigdig = 0; /* number of significant digits */ + int nosigdig = 0; /* number of non-significant digits */ + int e = 0; /* exponent correction */ + int neg; /* 1 if number is negative */ + int hasdot = 0; /* true after seen a dot */ *endptr = cast(char *, s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check signal */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ return 0.0; /* invalid format (no '0x') */ - s += 2; /* skip '0x' */ - r = readhexa(&s, r, &i); /* read integer part */ - if (*s == '.') { - s++; /* skip dot */ - r = readhexa(&s, r, &e); /* read fractional part */ + for (s += 2; ; s++) { /* skip '0x' and read numeral */ + if (*s == dot) { + if (hasdot) break; /* second dot? stop loop */ + else hasdot = 1; + } + else if (lisxdigit(cast_uchar(*s))) { + if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ + nosigdig++; + else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ + r = (r * cast_num(16.0)) + luaO_hexavalue(*s); + else e++; /* too many digits; ignore, but still count for exponent */ + if (hasdot) e--; /* decimal digit? correct exponent */ + } + else break; /* neither a dot nor a digit */ } - if (i == 0 && e == 0) - return 0.0; /* invalid format (no digit) */ - e *= -4; /* each fractional digit divides value by 2^-4 */ + if (nosigdig + sigdig == 0) /* no digits? */ + return 0.0; /* invalid format */ *endptr = cast(char *, s); /* valid up to here */ + e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ - int exp1 = 0; - int neg1; + int exp1 = 0; /* exponent value */ + int neg1; /* exponent signal */ s++; /* skip 'p' */ neg1 = isneg(&s); /* signal */ if (!lisdigit(cast_uchar(*s))) - goto ret; /* must have at least one digit */ + return 0.0; /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; e += exp1; + *endptr = cast(char *, s); /* valid up to here */ } - *endptr = cast(char *, s); /* valid up to here */ - ret: if (neg) r = -r; return l_mathop(ldexp)(r, e); } #endif +/* }====================================================== */ -int luaO_str2d (const char *s, size_t len, lua_Number *result) { +/* maximum length of a numeral */ +#if !defined (L_MAXLENNUM) +#define L_MAXLENNUM 200 +#endif + +static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; - if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ - return 0; - else if (strpbrk(s, "xX")) /* hexa? */ - *result = lua_strx2number(s, &endptr); - else - *result = lua_str2number(s, &endptr); - if (endptr == s) return 0; /* nothing recognized */ - while (lisspace(cast_uchar(*endptr))) endptr++; - return (endptr == s + len); /* OK if no trailing characters */ + *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ + : lua_str2number(s, &endptr); + if (endptr == s) return NULL; /* nothing recognized? */ + while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ + return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ } +/* +** Convert string 's' to a Lua number (put in 'result'). Return NULL +** on fail or the address of the ending '\0' on success. +** 'pmode' points to (and 'mode' contains) special things in the string: +** - 'x'/'X' means an hexadecimal numeral +** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) +** - '.' just optimizes the search for the common case (nothing special) +** This function accepts both the current locale or a dot as the radix +** mark. If the convertion fails, it may mean number has a dot but +** locale accepts something else. In that case, the code copies 's' +** to a buffer (because 's' is read-only), changes the dot to the +** current locale radix mark, and tries to convert again. +*/ +static const char *l_str2d (const char *s, lua_Number *result) { + const char *endptr; + const char *pmode = strpbrk(s, ".xXnN"); + int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; + if (mode == 'n') /* reject 'inf' and 'nan' */ + return NULL; + endptr = l_str2dloc(s, result, mode); /* try to convert */ + 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) + return NULL; /* string too long or no dot; fail */ + strcpy(buff, s); /* copy string to buffer */ + buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ + endptr = l_str2dloc(buff, result, mode); /* try again */ + if (endptr != NULL) + endptr = s + (endptr - buff); /* make relative to 's' */ + } + return endptr; +} + + +#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) +#define MAXLASTD cast_int(LUA_MAXINTEGER % 10) + +static const char *l_str2int (const char *s, lua_Integer *result) { + lua_Unsigned a = 0; + int empty = 1; + int neg; + while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ + neg = isneg(&s); + if (s[0] == '0' && + (s[1] == 'x' || s[1] == 'X')) { /* hex? */ + s += 2; /* skip '0x' */ + for (; lisxdigit(cast_uchar(*s)); s++) { + a = a * 16 + luaO_hexavalue(*s); + empty = 0; + } + } + else { /* decimal */ + for (; lisdigit(cast_uchar(*s)); s++) { + int d = *s - '0'; + if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ + return NULL; /* do not accept it (as integer) */ + a = a * 10 + d; + empty = 0; + } + } + while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ + if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ + else { + *result = l_castU2S((neg) ? 0u - a : a); + return s; + } +} + + +size_t luaO_str2num (const char *s, TValue *o) { + lua_Integer i; lua_Number n; + const char *e; + if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ + setivalue(o, i); + } + else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ + setfltvalue(o, n); + } + else + return 0; /* conversion failed */ + return (e - s) + 1; /* success; return string size */ +} + + +int luaO_utf8esc (char *buff, unsigned long x) { + int n = 1; /* number of bytes put in buffer (backwards) */ + lua_assert(x <= 0x10FFFF); + if (x < 0x80) /* ascii? */ + buff[UTF8BUFFSZ - 1] = cast(char, x); + else { /* need continuation bytes */ + unsigned int mfb = 0x3f; /* maximum that fits in first byte */ + do { /* add continuation bytes */ + buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f)); + x >>= 6; /* remove added bits */ + mfb >>= 1; /* now there is one less bit available in first byte */ + } while (x > mfb); /* still needs continuation byte? */ + buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */ + } + return n; +} + + +/* maximum length of the conversion of a number to a string */ +#define MAXNUMBER2STR 50 + + +/* +** Convert a number object to a string +*/ +void luaO_tostring (lua_State *L, StkId obj) { + char buff[MAXNUMBER2STR]; + size_t len; + lua_assert(ttisnumber(obj)); + if (ttisinteger(obj)) + len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); + else { + len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); +#if !defined(LUA_COMPAT_FLOATSTRING) + if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ + buff[len++] = lua_getlocaledecpoint(); + buff[len++] = '0'; /* adds '.0' to result */ + } +#endif + } + setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); +} + static void pushstr (lua_State *L, const char *str, size_t l) { - setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); + setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); + luaD_inctop(L); } -/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ +/* +** this function handles only '%d', '%c', '%f', '%p', and '%s' + conventional formats, plus Lua-specific '%I' and '%U' +*/ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { int n = 0; for (;;) { const char *e = strchr(fmt, '%'); if (e == NULL) break; - luaD_checkstack(L, 2); /* fmt + item */ pushstr(L, fmt, e - fmt); switch (*(e+1)) { - case 's': { + case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; pushstr(L, s, strlen(s)); break; } - case 'c': { - char buff; - buff = cast(char, va_arg(argp, int)); - pushstr(L, &buff, 1); + 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)); break; } - case 'd': { - setnvalue(L->top++, cast_num(va_arg(argp, int))); + case 'd': { /* an 'int' */ + setivalue(L->top, va_arg(argp, int)); + goto top2str; + } + case 'I': { /* a 'lua_Integer' */ + setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); + goto top2str; + } + 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); break; } - case 'f': { - setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); - break; - } - case 'p': { - char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ - int l = sprintf(buff, "%p", va_arg(argp, void *)); + case 'p': { /* a pointer */ + char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ + int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); pushstr(L, buff, l); break; } + case 'U': { /* 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); + break; + } case '%': { pushstr(L, "%", 1); break; } default: { - luaG_runerror(L, - "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), - *(e + 1)); + luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", + *(e + 1)); } } n += 2; diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 1428725b..ec6a1f0c 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.71.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -19,14 +19,13 @@ /* ** Extra tags for non-values */ -#define LUA_TPROTO LUA_NUMTAGS -#define LUA_TUPVAL (LUA_NUMTAGS+1) -#define LUA_TDEADKEY (LUA_NUMTAGS+2) +#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */ /* ** number of all possible tags (including LUA_TNONE but excluding DEADKEY) */ -#define LUA_TOTALTAGS (LUA_TUPVAL+2) +#define LUA_TOTALTAGS (LUA_TPROTO + 2) /* @@ -36,8 +35,6 @@ ** bit 6: whether value is collectable */ -#define VARBITS (3 << 4) - /* ** LUA_TFUNCTION variants: @@ -57,6 +54,11 @@ #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ +/* Variant tags for numbers */ +#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ +#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ + + /* Bit mark for collectable types */ #define BIT_ISCOLLECTABLE (1 << 6) @@ -65,9 +67,9 @@ /* -** Union of all collectable objects +** Common type for all collectable objects */ -typedef union GCObject GCObject; +typedef struct GCObject GCObject; /* @@ -78,32 +80,40 @@ typedef union GCObject GCObject; /* -** Common header in struct form +** Common type has only the common header */ -typedef struct GCheader { +struct GCObject { CommonHeader; -} GCheader; +}; -/* -** Union of all Lua values -*/ -typedef union Value Value; - - -#define numfield lua_Number n; /* numbers */ - - /* ** Tagged Values. This is the basic representation of values in Lua, ** an actual value plus a tag with its type. */ +/* +** Union of all Lua values +*/ +typedef union Value { + GCObject *gc; /* collectable objects */ + void *p; /* light userdata */ + int b; /* booleans */ + lua_CFunction f; /* light C functions */ + lua_Integer i; /* integer numbers */ + lua_Number n; /* float numbers */ +} Value; + + #define TValuefields Value value_; int tt_ -typedef struct lua_TValue TValue; + +typedef struct lua_TValue { + TValuefields; +} TValue; + /* macro defining a nil value */ @@ -111,7 +121,6 @@ typedef struct lua_TValue TValue; #define val_(o) ((o)->value_) -#define num_(o) (val_(o).n) /* raw type tag of a TValue */ @@ -124,13 +133,15 @@ typedef struct lua_TValue TValue; #define ttype(o) (rttype(o) & 0x3F) /* type tag of a TValue with no variants (bits 0-3) */ -#define ttypenv(o) (novariant(rttype(o))) +#define ttnov(o) (novariant(rttype(o))) /* Macros to test type */ #define checktag(o,t) (rttype(o) == (t)) -#define checktype(o,t) (ttypenv(o) == (t)) -#define ttisnumber(o) checktag((o), LUA_TNUMBER) +#define checktype(o,t) (ttnov(o) == (t)) +#define ttisnumber(o) checktype((o), LUA_TNUMBER) +#define ttisfloat(o) checktag((o), LUA_TNUMFLT) +#define ttisinteger(o) checktag((o), LUA_TNUMINT) #define ttisnil(o) checktag((o), LUA_TNIL) #define ttisboolean(o) checktag((o), LUA_TBOOLEAN) #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) @@ -143,27 +154,27 @@ typedef struct lua_TValue TValue; #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) #define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA)) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) -#define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) /* Macros to access values */ -#define nvalue(o) check_exp(ttisnumber(o), num_(o)) +#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) +#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) +#define nvalue(o) check_exp(ttisnumber(o), \ + (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) -#define tsvalue(o) (&rawtsvalue(o)->tsv) -#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u) -#define uvalue(o) (&rawuvalue(o)->uv) -#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) -#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l) -#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c) +#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) +#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) +#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) +#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) +#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) #define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h) +#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th) +#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) /* a dead value may get the 'gc' field, but cannot access its contents */ #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) @@ -174,18 +185,27 @@ typedef struct lua_TValue TValue; /* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt) +#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) -#define checkliveness(g,obj) \ +#define checkliveness(L,obj) \ lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && !isdead(g,gcvalue(obj)))) + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) /* Macros to set values */ #define settt_(o,t) ((o)->tt_=(t)) -#define setnvalue(obj,x) \ - { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } +#define setfltvalue(obj,x) \ + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } + +#define chgfltvalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } + +#define setivalue(obj,x) \ + { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } + +#define chgivalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } #define setnilvalue(obj) settt_(obj, LUA_TNIL) @@ -199,48 +219,46 @@ typedef struct lua_TValue TValue; { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } #define setgcovalue(L,obj,x) \ - { TValue *io=(obj); GCObject *i_g=(x); \ - val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } + { TValue *io = (obj); GCObject *i_g=(x); \ + val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } #define setsvalue(L,obj,x) \ - { TValue *io=(obj); \ - TString *x_ = (x); \ - val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \ - checkliveness(G(L),io); } + { TValue *io = (obj); TString *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ + checkliveness(L,io); } #define setuvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(G(L),io); } + { TValue *io = (obj); Udata *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ + checkliveness(L,io); } #define setthvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ - checkliveness(G(L),io); } + { TValue *io = (obj); lua_State *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ + checkliveness(L,io); } #define setclLvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \ - checkliveness(G(L),io); } + { TValue *io = (obj); LClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ + checkliveness(L,io); } #define setclCvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \ - checkliveness(G(L),io); } + { 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); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ - checkliveness(G(L),io); } + { 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) #define setobj(L,obj1,obj2) \ - { const TValue *io2=(obj2); TValue *io1=(obj1); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - checkliveness(G(L),io1); } + { TValue *io1=(obj1); *io1 = *(obj2); \ + (void)L; checkliveness(L,io1); } /* @@ -256,125 +274,13 @@ typedef struct lua_TValue TValue; #define setptvalue2s setptvalue /* from table to same table */ #define setobjt2t setobj -/* to table */ -#define setobj2t setobj /* to new object */ #define setobj2n setobj #define setsvalue2n setsvalue +/* to table (define it as an expression to be used in macros) */ +#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1))) -/* check whether a number is valid (useful only for NaN trick) */ -#define luai_checknum(L,o,c) { /* empty */ } - - -/* -** {====================================================== -** NaN Trick -** ======================================================= -*/ -#if defined(LUA_NANTRICK) - -/* -** numbers are represented in the 'd_' field. All other values have the -** value (NNMARK | tag) in 'tt__'. A number with such pattern would be -** a "signaled NaN", which is never generated by regular operations by -** the CPU (nor by 'strtod') -*/ - -/* allows for external implementation for part of the trick */ -#if !defined(NNMARK) /* { */ - - -#if !defined(LUA_IEEEENDIAN) -#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN' -#endif - - -#define NNMARK 0x7FF7A500 -#define NNMASK 0x7FFFFF00 - -#undef TValuefields -#undef NILCONSTANT - -#if (LUA_IEEEENDIAN == 0) /* { */ - -/* little endian */ -#define TValuefields \ - union { struct { Value v__; int tt__; } i; double d__; } u -#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#else /* }{ */ - -/* big endian */ -#define TValuefields \ - union { struct { int tt__; Value v__; } i; double d__; } u -#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#endif /* } */ - -#endif /* } */ - - -/* correspondence with standard representation */ -#undef val_ -#define val_(o) v_(o) -#undef num_ -#define num_(o) d_(o) - - -#undef numfield -#define numfield /* no such field; numbers are the entire struct */ - -/* basic check to distinguish numbers from non-numbers */ -#undef ttisnumber -#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK) - -#define tag2tt(t) (NNMARK | (t)) - -#undef rttype -#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff) - -#undef settt_ -#define settt_(o,t) (tt_(o) = tag2tt(t)) - -#undef setnvalue -#define setnvalue(obj,x) \ - { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); } - -#undef setobj -#define setobj(L,obj1,obj2) \ - { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ - o1_->u = o2_->u; \ - checkliveness(G(L),o1_); } - - -/* -** these redefinitions are not mandatory, but these forms are more efficient -*/ - -#undef checktag -#undef checktype -#define checktag(o,t) (tt_(o) == tag2tt(t)) -#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS)) - -#undef ttisequal -#define ttisequal(o1,o2) \ - (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2))) - - -#undef luai_checknum -#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } - -#endif -/* }====================================================== */ @@ -385,20 +291,6 @@ typedef struct lua_TValue TValue; */ -union Value { - GCObject *gc; /* collectable objects */ - void *p; /* light userdata */ - int b; /* booleans */ - lua_CFunction f; /* light C functions */ - numfield /* numbers */ -}; - - -struct lua_TValue { - TValuefields; -}; - - typedef TValue *StkId; /* index to stack elements */ @@ -406,46 +298,94 @@ typedef TValue *StkId; /* index to stack elements */ /* ** Header for string value; string bytes follow the end of this structure +** (aligned according to 'UTString'; see next). */ -typedef union TString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ - struct { - CommonHeader; - lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - unsigned int hash; - size_t len; /* number of characters in string */ - } tsv; +typedef struct TString { + CommonHeader; + lu_byte extra; /* reserved words for short strings; "has hash" for longs */ + lu_byte shrlen; /* length for short strings */ + unsigned int hash; + union { + size_t lnglen; /* length for long strings */ + struct TString *hnext; /* linked list for hash table */ + } u; } TString; -/* get the actual string (array of bytes) from a TString */ -#define getstr(ts) cast(const char *, (ts) + 1) +/* +** Ensures that address after this type is always fully aligned. +*/ +typedef union UTString { + L_Umaxalign dummy; /* ensures maximum alignment for strings */ + TString tsv; +} UTString; + + +/* +** Get the actual string (array of bytes) from a 'TString'. +** (Access to 'extra' ensures that value is really a 'TString'.) +*/ +#define getstr(ts) \ + check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString)) + /* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(rawtsvalue(o)) +#define svalue(o) getstr(tsvalue(o)) + +/* get string length from 'TString *s' */ +#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen) + +/* get string length from 'TValue *o' */ +#define vslen(o) tsslen(tsvalue(o)) /* ** Header for userdata; memory area follows the end of this structure +** (aligned according to 'UUdata'; see next). */ -typedef union Udata { - L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ - struct { - CommonHeader; - struct Table *metatable; - struct Table *env; - size_t len; /* number of bytes */ - } uv; +typedef struct Udata { + CommonHeader; + lu_byte ttuv_; /* user value's tag */ + struct Table *metatable; + size_t len; /* number of bytes */ + union Value user_; /* user value */ } Udata; +/* +** Ensures that address after this type is always fully aligned. +*/ +typedef union UUdata { + L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */ + Udata uv; +} UUdata; + + +/* +** Get the address of memory block inside 'Udata'. +** (Access to 'ttuv_' ensures that value is really a 'Udata'.) +*/ +#define getudatamem(u) \ + check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata))) + +#define setuservalue(L,u,o) \ + { const TValue *io=(o); Udata *iu = (u); \ + iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ + checkliveness(L,io); } + + +#define getuservalue(L,u,o) \ + { TValue *io=(o); const Udata *iu = (u); \ + io->value_ = iu->user_; settt_(io, iu->ttuv_); \ + checkliveness(L,io); } + /* ** Description of an upvalue for function prototypes */ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ - lu_byte instack; /* whether it is in stack */ + lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ } Upvaldesc; @@ -460,25 +400,24 @@ typedef struct LocVar { int endpc; /* first point where variable is dead */ } LocVar; - typedef struct SharedProto { + lu_byte numparams; /* number of fixed parameters */ + lu_byte is_vararg; + lu_byte maxstacksize; /* number of registers needed by this function */ + int sizeupvalues; /* size of 'upvalues' */ + int sizek; /* size of 'k' */ + int sizecode; + int sizelineinfo; + int sizep; /* size of 'p' */ + int sizelocvars; + int linedefined; /* debug information */ + int lastlinedefined; /* debug information */ void *l_G; /* global state belongs to */ - Instruction *code; + Instruction *code; /* opcodes */ int *lineinfo; /* map from opcodes to source lines (debug information) */ LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ TString *source; /* used for debug information */ - int sizeupvalues; /* size of 'upvalues' */ - int sizek; /* size of `k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of `p' */ - int sizelocvars; - int linedefined; - int lastlinedefined; - lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; - lu_byte maxstacksize; /* maximum stack used by this function */ } SharedProto; /* @@ -486,10 +425,10 @@ typedef struct SharedProto { */ typedef struct Proto { CommonHeader; - TValue *k; /* constants used by the function */ struct SharedProto *sp; + TValue *k; /* constants used by the function */ struct Proto **p; /* functions defined inside the function */ - union Closure *cache; /* last created closure with this prototype */ + struct LClosure *cache; /* last-created closure with this prototype */ GCObject *gclist; } Proto; @@ -498,17 +437,7 @@ typedef struct Proto { /* ** Lua Upvalues */ -typedef struct UpVal { - CommonHeader; - TValue *v; /* points to stack or to its own value */ - union { - TValue value; /* the value (when closed) */ - struct { /* double linked list (when open) */ - struct UpVal *prev; - struct UpVal *next; - } l; - } u; -} UpVal; +typedef struct UpVal UpVal; /* @@ -550,12 +479,19 @@ typedef union Closure { typedef union TKey { struct { TValuefields; - struct Node *next; /* for chaining */ + int next; /* for chaining (offset for next node) */ } nk; TValue tvk; } TKey; +/* copy a value into a key without messing up field 'next' */ +#define 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_); } + + typedef struct Node { TValue i_val; TKey i_key; @@ -565,19 +501,19 @@ typedef struct Node { typedef struct Table { CommonHeader; lu_byte flags; /* 1<

#include "lopcodes.h" @@ -31,10 +34,17 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "ADD", "SUB", "MUL", - "DIV", "MOD", "POW", + "DIV", + "IDIV", + "BAND", + "BOR", + "BXOR", + "SHL", + "SHR", "UNM", + "BNOT", "NOT", "LEN", "CONCAT", @@ -79,10 +89,17 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,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_DIV */ ,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 */ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 51f57915..bbc4b619 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.142.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -14,12 +14,12 @@ 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 + '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 + '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 @@ -58,7 +58,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ */ #if SIZE_Bx < LUAI_BITSINT-1 #define MAXARG_Bx ((1<>1) /* `sBx' is signed */ +#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */ #else #define MAXARG_Bx MAX_INT #define MAXARG_sBx MAX_INT @@ -76,10 +76,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define MAXARG_C ((1<> RK(C) */ OP_UNM,/* A B R(A) := -R(B) */ +OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ -OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */ +OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */ OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ @@ -231,16 +240,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ /*=========================================================================== Notes: - (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is + (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is set to last_result+1, so next open instruction (OP_CALL, OP_RETURN, - OP_SETLIST) may use `top'. + OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (B == 0) then use actual number of varargs and set top (like in OP_CALL with C == 0). - (*) In OP_RETURN, if (B == 0) then return up to `top'. + (*) In OP_RETURN, if (B == 0) then return up to 'top'. - (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next + (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next 'instruction' is EXTRAARG(real C). (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. @@ -248,7 +257,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) For comparisons, A specifies what condition the test should accept (true or false). - (*) All `skips' (pc++) assume that next instruction is a jump. + (*) All 'skips' (pc++) assume that next instruction is a jump. ===========================================================================*/ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 052ba174..5a94eb90 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,9 +1,14 @@ /* -** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ +#define loslib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include @@ -11,9 +16,6 @@ #include #include -#define loslib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -21,60 +23,119 @@ /* -** list of valid conversion specifiers for the 'strftime' function +** {================================================================== +** List of valid conversion specifiers for the 'strftime' function; +** options are grouped by length; group of length 2 start with '||'. +** =================================================================== */ -#if !defined(LUA_STRFTIMEOPTIONS) - -#if !defined(LUA_USE_POSIX) -#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } -#else -#define LUA_STRFTIMEOPTIONS \ - { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \ - "", "E", "cCxXyY", \ - "O", "deHImMSuUVwWy" } -#endif +#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 +#else /* C99 specification */ +#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 #endif +#endif /* } */ +/* }================================================================== */ /* -** By default, Lua uses tmpnam except when POSIX is available, where it -** uses mkstemp. +** {================================================================== +** Configuration for time-related stuff +** =================================================================== */ -#if defined(LUA_USE_MKSTEMP) -#include -#define LUA_TMPNAMBUFSIZE 32 -#define lua_tmpnam(b,e) { \ - strcpy(b, "/tmp/lua_XXXXXX"); \ - e = mkstemp(b); \ - if (e != -1) close(e); \ - e = (e == -1); } -#elif !defined(lua_tmpnam) +#if !defined(l_time_t) /* { */ +/* +** type to represent time_t in Lua +*/ +#define l_timet lua_Integer +#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) -#define LUA_TMPNAMBUFSIZE L_tmpnam -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } +static time_t l_checktime (lua_State *L, int arg) { + lua_Integer t = luaL_checkinteger(L, arg); + luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); + return (time_t)t; +} -#endif +#endif /* } */ +#if !defined(l_gmtime) /* { */ /* ** By default, Lua uses gmtime/localtime, except when POSIX is available, ** where it uses gmtime_r/localtime_r */ -#if defined(LUA_USE_GMTIME_R) + +#if defined(LUA_USE_POSIX) /* { */ #define l_gmtime(t,r) gmtime_r(t,r) #define l_localtime(t,r) localtime_r(t,r) -#elif !defined(l_gmtime) +#else /* }{ */ -#define l_gmtime(t,r) ((void)r, gmtime(t)) -#define l_localtime(t,r) ((void)r, localtime(t)) +/* ISO C definitions */ +#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) +#endif /* } */ + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Configuration for 'tmpnam': +** By default, Lua uses tmpnam except when POSIX is available, where +** it uses mkstemp. +** =================================================================== +*/ +#if !defined(lua_tmpnam) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + +#include + +#define LUA_TMPNAMBUFSIZE 32 + +#if !defined(LUA_TMPNAMTEMPLATE) +#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" #endif +#define lua_tmpnam(b,e) { \ + strcpy(b, LUA_TMPNAMTEMPLATE); \ + e = mkstemp(b); \ + if (e != -1) close(e); \ + e = (e == -1); } + +#else /* }{ */ + +/* ISO C definitions */ +#define LUA_TMPNAMBUFSIZE L_tmpnam +#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } + +#endif /* } */ + +#endif /* } */ +/* }================================================================== */ + + static int os_execute (lua_State *L) { @@ -145,45 +206,68 @@ static void setboolfield (lua_State *L, const char *key, int value) { lua_setfield(L, -2, key); } + +/* +** Set all fields from structure 'tm' in the table on top of the stack +*/ +static void setallfields (lua_State *L, struct tm *stm) { + setfield(L, "sec", stm->tm_sec); + setfield(L, "min", stm->tm_min); + setfield(L, "hour", stm->tm_hour); + setfield(L, "day", stm->tm_mday); + setfield(L, "month", stm->tm_mon + 1); + setfield(L, "year", stm->tm_year + 1900); + setfield(L, "wday", stm->tm_wday + 1); + setfield(L, "yday", stm->tm_yday + 1); + setboolfield(L, "isdst", stm->tm_isdst); +} + + static int getboolfield (lua_State *L, const char *key) { int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); lua_pop(L, 1); return res; } -static int getfield (lua_State *L, const char *key, int d) { - int res, isnum; - lua_getfield(L, -1, key); - res = (int)lua_tointegerx(L, -1, &isnum); - if (!isnum) { - if (d < 0) - return luaL_error(L, "field " LUA_QS " missing in date table", 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 */ + lua_Integer res = lua_tointegerx(L, -1, &isnum); + if (!isnum) { /* field is not an integer? */ + if (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? */ + return luaL_error(L, "field '%s' missing in date table", key); res = d; } + else { + if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + return luaL_error(L, "field '%s' is out-of-bound", key); + res -= delta; + } lua_pop(L, 1); - return res; + return (int)res; } -static const char *checkoption (lua_State *L, const char *conv, char *buff) { - static const char *const options[] = LUA_STRFTIMEOPTIONS; - unsigned int i; - for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { - if (*conv != '\0' && strchr(options[i], *conv) != NULL) { - buff[1] = *conv; - if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ - buff[2] = '\0'; /* end buffer */ - return conv + 1; - } - else if (*(conv + 1) != '\0' && - strchr(options[i + 1], *(conv + 1)) != NULL) { - buff[2] = *(conv + 1); /* valid two-char conversion specifier */ - buff[3] = '\0'; /* end buffer */ - return conv + 2; - } +static const char *checkoption (lua_State *L, const char *conv, + ptrdiff_t convlen, char *buff) { + const char *option = LUA_STRFTIMEOPTIONS; + int oplen = 1; /* length of options being checked */ + for (; *option != '\0' && oplen <= convlen; option += oplen) { + if (*option == '|') /* next block? */ + oplen++; /* will check options with next length (+1) */ + else if (memcmp(conv, option, oplen) == 0) { /* match? */ + memcpy(buff, conv, oplen); /* copy valid option to buffer */ + buff[oplen] = '\0'; + return conv + oplen; /* return next item */ } } luaL_argerror(L, 1, @@ -192,44 +276,43 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { } +/* maximum size for an individual 'strftime' item */ +#define SIZETIMEFMT 250 + + static int os_date (lua_State *L) { - const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); + size_t slen; + const char *s = luaL_optlstring(L, 1, "%c", &slen); + time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); + const char *se = s + slen; /* 's' end */ struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); - s++; /* skip `!' */ + s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ - lua_pushnil(L); - else if (strcmp(s, "*t") == 0) { + luaL_error(L, "time result cannot be represented in this installation"); + if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); + setallfields(L, stm); } else { - char cc[4]; + char cc[4]; /* buffer for individual conversion specifiers */ luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); - while (*s) { - if (*s != '%') /* no conversion specifier? */ + while (s < se) { + if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; - char buff[200]; /* should be big enough for any conversion result */ - s = checkoption(L, s + 1, cc); - reslen = strftime(buff, sizeof(buff), cc, stm); - luaL_addlstring(&b, buff, reslen); + char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); + s++; /* skip '%' */ + s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ + reslen = strftime(buff, SIZETIMEFMT, cc, stm); + luaL_addsize(&b, reslen); } } luaL_pushresult(&b); @@ -246,26 +329,27 @@ static int os_time (lua_State *L) { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; + ts.tm_sec = getfield(L, "sec", 0, 0); + ts.tm_min = getfield(L, "min", 0, 0); + ts.tm_hour = getfield(L, "hour", 12, 0); + ts.tm_mday = getfield(L, "day", -1, 0); + ts.tm_mon = getfield(L, "month", -1, 1); + ts.tm_year = getfield(L, "year", -1, 1900); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); + setallfields(L, &ts); /* update fields with normalized values */ } - if (t == (time_t)(-1)) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)t); + if (t != (time_t)(l_timet)t || t == (time_t)(-1)) + luaL_error(L, "time result cannot be represented in this installation"); + l_pushtime(L, t); return 1; } static int os_difftime (lua_State *L) { - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, 0)))); + time_t t1 = l_checktime(L, 1); + time_t t2 = l_checktime(L, 2); + lua_pushnumber(L, (lua_Number)difftime(t1, t2)); return 1; } @@ -289,7 +373,7 @@ static int os_exit (lua_State *L) { if (lua_isboolean(L, 1)) status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); else - status = luaL_optint(L, 1, EXIT_SUCCESS); + status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); if (lua_toboolean(L, 2)) lua_close(L); if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index a8e37fdd..4aa3e356 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,15 +1,17 @@ /* -** $Id: lparser.c,v 2.130.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ - -#include - #define lparser_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lcode.h" @@ -35,17 +37,21 @@ #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) +/* because all strings are unified by the scanner, the parser + can use pointer equality for string equality */ +#define eqstr(a,b) ((a) == (b)) + /* ** nodes for block list (list of active blocks) */ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ - short firstlabel; /* index of first label in this block */ - short firstgoto; /* index of first pending goto in this block */ + int firstlabel; /* index of first label in this block */ + int firstgoto; /* index of first pending goto in this block */ lu_byte nactvar; /* # active locals outside the block */ lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if `block' is a loop */ + lu_byte isloop; /* true if 'block' is a loop */ } BlockCnt; @@ -57,19 +63,9 @@ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); -static void anchor_token (LexState *ls) { - /* last token from outer function must be EOS */ - lua_assert(ls->fs != NULL || ls->t.token == TK_EOS); - if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { - TString *ts = ls->t.seminfo.ts; - luaX_newstring(ls, getstr(ts), ts->tsv.len); - } -} - - /* semantic error */ static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove 'near to' from final message */ + ls->t.token = 0; /* remove "near " from final message */ luaX_syntaxerror(ls, msg); } @@ -169,7 +165,8 @@ static int registerlocalvar (LexState *ls, TString *varname) { int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); - while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; + while (oldsize < f->sizelocvars) + f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; luaC_objbarrier(ls->L, fp, varname); return fs->nlocvars++; @@ -223,7 +220,7 @@ static int searchupvalue (FuncState *fs, TString *name) { int i; Upvaldesc *up = fs->f->sp->upvalues; for (i = 0; i < fs->nups; i++) { - if (luaS_eqstr(up[i].name, name)) return i; + if (eqstr(up[i].name, name)) return i; } return -1; /* not found */ } @@ -236,7 +233,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, Upvaldesc, MAXUPVAL, "upvalues"); - while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; + while (oldsize < f->sizeupvalues) + f->upvalues[oldsize++].name = NULL; f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; @@ -248,7 +246,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { static int searchvar (FuncState *fs, TString *n) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (luaS_eqstr(n, getlocvar(fs, i)->varname)) + if (eqstr(n, getlocvar(fs, i)->varname)) return i; } return -1; /* not found */ @@ -261,7 +259,8 @@ static int searchvar (FuncState *fs, TString *n) { */ static void markupval (FuncState *fs, int level) { BlockCnt *bl = fs->bl; - while (bl->nactvar > level) bl = bl->previous; + while (bl->nactvar > level) + bl = bl->previous; bl->upval = 1; } @@ -270,27 +269,26 @@ static void markupval (FuncState *fs, int level) { Find variable with given name 'n'. If it is an upvalue, add this upvalue into all intermediate functions. */ -static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { +static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ - return VVOID; /* default is global */ + init_exp(var, VVOID, 0); /* default is global */ else { int v = searchvar(fs, n); /* look up locals at current level */ if (v >= 0) { /* found? */ init_exp(var, VLOCAL, v); /* variable is local */ if (!base) markupval(fs, v); /* local will be used as an upval */ - return VLOCAL; } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ - if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ - return VVOID; /* not found; is a global */ + singlevaraux(fs->prev, n, var, 0); /* try upper levels */ + if (var->k == VVOID) /* not found? */ + return; /* it is a global */ /* else was LOCAL or UPVAL */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ } - init_exp(var, VUPVAL, idx); - return VUPVAL; + init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } } @@ -299,10 +297,11 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { static void singlevar (LexState *ls, expdesc *var) { TString *varname = str_checkname(ls); FuncState *fs = ls->fs; - if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */ + singlevaraux(fs, varname, var, 1); + if (var->k == VVOID) { /* global name? */ expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k == VLOCAL || var->k == VUPVAL); + lua_assert(var->k != VVOID); /* this one must exist */ codestring(ls, &key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } @@ -326,6 +325,8 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { luaK_nil(fs, reg, extra); } } + if (nexps > nvars) + ls->fs->freereg -= nexps - nvars; /* remove extra values */ } @@ -344,11 +345,11 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { FuncState *fs = ls->fs; Labellist *gl = &ls->dyd->gt; Labeldesc *gt = &gl->arr[g]; - lua_assert(luaS_eqstr(gt->name, label->name)); + lua_assert(eqstr(gt->name, label->name)); if (gt->nactvar < label->nactvar) { TString *vname = getlocvar(fs, gt->nactvar)->varname; const char *msg = luaO_pushfstring(ls->L, - " at line %d jumps into the scope of local " LUA_QS, + " at line %d jumps into the scope of local '%s'", getstr(gt->name), gt->line, getstr(vname)); semerror(ls, msg); } @@ -371,7 +372,7 @@ static int findlabel (LexState *ls, int g) { /* check labels in current block for a match */ for (i = bl->firstlabel; i < dyd->label.n; i++) { Labeldesc *lb = &dyd->label.arr[i]; - if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */ + if (eqstr(lb->name, gt->name)) { /* correct label? */ if (gt->nactvar > lb->nactvar && (bl->upval || dyd->label.n > bl->firstlabel)) luaK_patchclose(ls->fs, gt->pc, lb->nactvar); @@ -392,7 +393,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, l->arr[n].line = line; l->arr[n].nactvar = ls->fs->nactvar; l->arr[n].pc = pc; - l->n++; + l->n = n + 1; return n; } @@ -405,7 +406,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; int i = ls->fs->bl->firstgoto; while (i < gl->n) { - if (luaS_eqstr(gl->arr[i].name, lb->name)) + if (eqstr(gl->arr[i].name, lb->name)) closegoto(ls, i, lb); else i++; @@ -414,7 +415,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { /* -** "export" pending gotos to outer level, to check them against +** export pending gotos to outer level, to check them against ** outer labels; if the block being exited has upvalues, and ** the goto exits the scope of any variable (which can be the ** upvalue), close those variables being exited. @@ -450,7 +451,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { /* -** create a label named "break" to resolve break statements +** create a label named 'break' to resolve break statements */ static void breaklabel (LexState *ls) { TString *n = luaS_new(ls->L, "break"); @@ -465,7 +466,7 @@ static void breaklabel (LexState *ls) { static l_noret undefgoto (LexState *ls, Labeldesc *gt) { const char *msg = isreserved(gt->name) ? "<%s> at line %d not inside a loop" - : "no visible label " LUA_QS " for at line %d"; + : "no visible label '%s' for at line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); semerror(ls, msg); } @@ -505,7 +506,8 @@ static Proto *addprototype (LexState *ls) { if (fs->np >= f->sp->sizep) { int oldsize = f->sp->sizep; luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; + while (oldsize < f->sp->sizep) + f->p[oldsize++] = NULL; } f->p[fs->np++] = clp = luaF_newproto(L, NULL); luaC_objbarrier(L, f, clp); @@ -527,7 +529,6 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - lua_State *L = ls->L; SharedProto *f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; @@ -546,10 +547,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { f = fs->f->sp; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ - fs->h = luaH_new(L); - /* anchor table of constants (to avoid being collected) */ - sethvalue2s(L, L->top, fs->h); - incr_top(L); enterblock(fs, bl, 0); } @@ -575,9 +572,6 @@ static void close_func (LexState *ls) { sp->sizeupvalues = fs->nups; lua_assert(fs->bl == NULL); ls->fs = fs->prev; - /* last token read was anchored in defunct function; must re-anchor it */ - anchor_token(ls); - L->top--; /* pop table of constants */ luaC_checkGC(L); } @@ -591,7 +585,7 @@ static void close_func (LexState *ls) { /* ** check whether current token is in the follow set of a block. ** 'until' closes syntactical blocks, but do not close scope, -** so it handled in separate. +** so it is handled in separate. */ static int block_follow (LexState *ls, int withuntil) { switch (ls->t.token) { @@ -605,7 +599,7 @@ static int block_follow (LexState *ls, int withuntil) { static void statlist (LexState *ls) { - /* statlist -> { stat [`;'] } */ + /* statlist -> { stat [';'] } */ while (!block_follow(ls, 1)) { if (ls->t.token == TK_RETURN) { statement(ls); @@ -646,14 +640,14 @@ static void yindex (LexState *ls, expdesc *v) { struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ - int nh; /* total number of `record' elements */ + int nh; /* total number of 'record' elements */ int na; /* total number of array elements */ int tostore; /* number of array elements pending to be stored */ }; static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | `['exp1`]') = exp1 */ + /* recfield -> (NAME | '['exp1']') = exp1 */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; expdesc key, val; @@ -760,12 +754,12 @@ static void constructor (LexState *ls, expdesc *t) { static void parlist (LexState *ls) { - /* parlist -> [ param { `,' param } ] */ + /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; SharedProto *f = fs->f->sp; int nparams = 0; f->is_vararg = 0; - if (ls->t.token != ')') { /* is `parlist' not empty? */ + if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { case TK_NAME: { /* param -> NAME */ @@ -773,12 +767,12 @@ static void parlist (LexState *ls) { nparams++; break; } - case TK_DOTS: { /* param -> `...' */ + case TK_DOTS: { /* param -> '...' */ luaX_next(ls); - f->is_vararg = 1; + f->is_vararg = 1; /* declared vararg */ break; } - default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); + default: luaX_syntaxerror(ls, " or '...' expected"); } } while (!f->is_vararg && testnext(ls, ',')); } @@ -789,7 +783,7 @@ static void parlist (LexState *ls) { static void body (LexState *ls, expdesc *e, int ismethod, int line) { - /* body -> `(' parlist `)' block END */ + /* body -> '(' parlist ')' block END */ FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); @@ -811,7 +805,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { static int explist (LexState *ls, expdesc *v) { - /* explist -> expr { `,' expr } */ + /* explist -> expr { ',' expr } */ int n = 1; /* at least one expression */ expr(ls, v); while (testnext(ls, ',')) { @@ -828,7 +822,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { expdesc args; int base, nparams; switch (ls->t.token) { - case '(': { /* funcargs -> `(' [ explist ] `)' */ + case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); if (ls->t.token == ')') /* arg list is empty? */ args.k = VVOID; @@ -845,7 +839,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { } case TK_STRING: { /* funcargs -> STRING */ codestring(ls, &args, ls->t.seminfo.ts); - luaX_next(ls); /* must use `seminfo' before `next' */ + luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } default: { @@ -911,14 +905,14 @@ static void suffixedexp (LexState *ls, expdesc *v) { fieldsel(ls, v); break; } - case '[': { /* `[' exp1 `]' */ + case '[': { /* '[' exp1 ']' */ expdesc key; luaK_exp2anyregup(fs, v); yindex(ls, &key); luaK_indexed(fs, v, &key); break; } - case ':': { /* `:' NAME funcargs */ + case ':': { /* ':' NAME funcargs */ expdesc key; luaX_next(ls); checkname(ls, &key); @@ -938,14 +932,19 @@ static void suffixedexp (LexState *ls, expdesc *v) { static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | + /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | constructor | FUNCTION body | suffixedexp */ switch (ls->t.token) { - case TK_NUMBER: { - init_exp(v, VKNUM, 0); + case TK_FLT: { + init_exp(v, VKFLT, 0); v->u.nval = ls->t.seminfo.r; break; } + case TK_INT: { + init_exp(v, VKINT, 0); + v->u.ival = ls->t.seminfo.i; + break; + } case TK_STRING: { codestring(ls, v, ls->t.seminfo.ts); break; @@ -965,7 +964,7 @@ static void simpleexp (LexState *ls, expdesc *v) { case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; check_condition(ls, fs->f->sp->is_vararg, - "cannot use " LUA_QL("...") " outside a vararg function"); + "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; } @@ -991,6 +990,7 @@ static UnOpr getunopr (int op) { switch (op) { case TK_NOT: return OPR_NOT; case '-': return OPR_MINUS; + case '~': return OPR_BNOT; case '#': return OPR_LEN; default: return OPR_NOUNOPR; } @@ -1002,9 +1002,15 @@ static BinOpr getbinopr (int op) { case '+': return OPR_ADD; case '-': return OPR_SUB; case '*': return OPR_MUL; - case '/': return OPR_DIV; case '%': return OPR_MOD; case '^': return OPR_POW; + case '/': return OPR_DIV; + case TK_IDIV: return OPR_IDIV; + case '&': return OPR_BAND; + case '|': return OPR_BOR; + case '~': return OPR_BXOR; + case TK_SHL: return OPR_SHL; + case TK_SHR: return OPR_SHR; case TK_CONCAT: return OPR_CONCAT; case TK_NE: return OPR_NE; case TK_EQ: return OPR_EQ; @@ -1023,19 +1029,24 @@ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ } priority[] = { /* ORDER OPR */ - {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `*' `/' `%' */ - {10, 9}, {5, 4}, /* ^, .. (right associative) */ - {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ - {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ - {2, 2}, {1, 1} /* and, or */ + {10, 10}, {10, 10}, /* '+' '-' */ + {11, 11}, {11, 11}, /* '*' '%' */ + {14, 13}, /* '^' (right associative) */ + {11, 11}, {11, 11}, /* '/' '//' */ + {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ + {7, 7}, {7, 7}, /* '<<' '>>' */ + {9, 8}, /* '..' (right associative) */ + {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ + {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ + {2, 2}, {1, 1} /* and, or */ }; -#define UNARY_PRIORITY 8 /* priority for unary operators */ +#define UNARY_PRIORITY 12 /* priority for unary operators */ /* ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where `binop' is any binary operator with a priority higher than `limit' +** where 'binop' is any binary operator with a priority higher than 'limit' */ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { BinOpr op; @@ -1049,7 +1060,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { luaK_prefix(ls->fs, uop, v, line); } else simpleexp(ls, v); - /* expand while operators have priorities higher than `limit' */ + /* expand while operators have priorities higher than 'limit' */ op = getbinopr(ls->t.token); while (op != OPR_NOBINOPR && priority[op].left > limit) { expdesc v2; @@ -1149,15 +1160,12 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { "C levels"); assignment(ls, &nv, nvars+1); } - else { /* assignment -> `=' explist */ + else { /* assignment -> '=' explist */ int nexps; checknext(ls, '='); nexps = explist(ls, &e); - if (nexps != nvars) { + if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } else { luaK_setoneret(ls->fs, &e); /* close last expression */ luaK_storevar(ls->fs, &lh->v, &e); @@ -1173,7 +1181,7 @@ static int cond (LexState *ls) { /* cond -> exp */ expdesc v; expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ + if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ luaK_goiftrue(ls->fs, &v); return v.f; } @@ -1198,9 +1206,9 @@ static void gotostat (LexState *ls, int pc) { static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { int i; for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (luaS_eqstr(label, ll->arr[i].name)) { + if (eqstr(label, ll->arr[i].name)) { const char *msg = luaO_pushfstring(fs->ls->L, - "label " LUA_QS " already defined on line %d", + "label '%s' already defined on line %d", getstr(label), ll->arr[i].line); semerror(fs->ls, msg); } @@ -1223,7 +1231,7 @@ static void labelstat (LexState *ls, TString *label, int line) { checkrepeated(fs, ll, label); /* check for repeated labels */ checknext(ls, TK_DBCOLON); /* skip double colon */ /* create new entry for this label */ - l = newlabelentry(ls, ll, label, line, fs->pc); + l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); skipnoopstat(ls); /* skip other no-op statements */ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ /* assume that locals are already out of scope */ @@ -1324,7 +1332,7 @@ static void fornum (LexState *ls, TString *varname, int line) { if (testnext(ls, ',')) exp1(ls); /* optional step */ else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_numberK(fs, 1)); + luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); luaK_reserveregs(fs, 1); } forbody(ls, base, line, 1, 1); @@ -1362,15 +1370,15 @@ static void forstat (LexState *ls, int line) { TString *varname; BlockCnt bl; enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip `for' */ + luaX_next(ls); /* skip 'for' */ varname = str_checkname(ls); /* first variable name */ switch (ls->t.token) { case '=': fornum(ls, varname, line); break; case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); + default: luaX_syntaxerror(ls, "'=' or 'in' expected"); } check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope (`break' jumps to this point) */ + leaveblock(fs); /* loop scope ('break' jumps to this point) */ } @@ -1387,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 */ - skipnoopstat(ls); /* skip other no-op statements */ + while (testnext(ls, ';')) {} /* skip colons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ @@ -1400,7 +1408,7 @@ static void test_then_block (LexState *ls, int *escapelist) { enterblock(fs, &bl, 0); jf = v.f; } - statlist(ls); /* `then' part */ + statlist(ls); /* 'then' part */ leaveblock(fs); if (ls->t.token == TK_ELSE || ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ @@ -1417,7 +1425,7 @@ static void ifstat (LexState *ls, int line) { while (ls->t.token == TK_ELSEIF) test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ if (testnext(ls, TK_ELSE)) - block(ls); /* `else' part */ + block(ls); /* 'else' part */ check_match(ls, TK_END, TK_IF, line); luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ } @@ -1435,7 +1443,7 @@ static void localfunc (LexState *ls) { static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */ + /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ int nvars = 0; int nexps; expdesc e; @@ -1455,7 +1463,7 @@ static void localstat (LexState *ls) { static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {fieldsel} [`:' NAME] */ + /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; singlevar(ls, v); while (ls->t.token == '.') @@ -1476,7 +1484,7 @@ static void funcstat (LexState *ls, int line) { ismethod = funcname(ls, &v); body(ls, &b, ismethod, line); luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ + luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } @@ -1491,7 +1499,7 @@ static void exprstat (LexState *ls) { } else { /* stat -> func */ check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ + SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ } } @@ -1508,8 +1516,8 @@ static void retstat (LexState *ls) { if (hasmultret(e.k)) { luaK_setmultret(fs, &e); if (e.k == VCALL && nret == 1) { /* tail call? */ - SET_OPCODE(getcode(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); + SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); + lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); } first = fs->nactvar; nret = LUA_MULTRET; /* return all values */ @@ -1518,8 +1526,8 @@ static void retstat (LexState *ls) { if (nret == 1) /* only one single value? */ first = luaK_exp2anyreg(fs, &e); else { - luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ - first = fs->nactvar; /* return all `active' values */ + luaK_exp2nextreg(fs, &e); /* values must go to the stack */ + first = fs->nactvar; /* return all active values */ lua_assert(nret == fs->freereg - first); } } @@ -1591,7 +1599,7 @@ static void statement (LexState *ls) { break; } } - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && + lua_assert(ls->fs->f->sp->maxstacksize >= ls->fs->freereg && ls->fs->freereg >= ls->fs->nactvar); ls->fs->freereg = ls->fs->nactvar; /* free registers */ leavelevel(ls); @@ -1608,7 +1616,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->sp->is_vararg = 1; /* main function is always vararg */ + fs->f->sp->is_vararg = 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 */ @@ -1618,16 +1626,19 @@ static void mainfunc (LexState *ls, FuncState *fs) { } -Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar) { +LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + Dyndata *dyd, const char *name, int firstchar) { LexState lexstate; FuncState funcstate; - Closure *cl = luaF_newLclosure(L, 1); /* create main closure */ - /* anchor closure (to avoid being collected) */ - setclLvalue(L, L->top, cl); - incr_top(L); - funcstate.f = cl->l.p = luaF_newproto(L, NULL); + LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ + setclLvalue(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 */ + luaD_inctop(L); + funcstate.f = cl->p = luaF_newproto(L, NULL); funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ + lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; @@ -1636,6 +1647,7 @@ Closure *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); - return cl; /* it's on the stack too */ + L->top--; /* remove scanner's table */ + return cl; /* closure is on the stack, too */ } diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 0346e3c4..02e9b03a 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -13,24 +13,38 @@ /* -** Expression descriptor +** Expression and variable descriptor. +** Code generation for variables and expressions can be delayed to allow +** optimizations; An 'expdesc' structure describes a potentially-delayed +** variable/expression. It has a description of its "main" value plus a +** list of conditional jumps that can also produce its value (generated +** by short-circuit operators 'and'/'or'). */ +/* kinds of variables/expressions */ typedef enum { - VVOID, /* no value */ - VNIL, - VTRUE, - VFALSE, - VK, /* info = index of constant in `k' */ - VKNUM, /* nval = numerical value */ - VNONRELOC, /* info = result register */ - VLOCAL, /* info = local register */ - VUPVAL, /* info = index of upvalue in 'upvalues' */ - VINDEXED, /* t = table register/upvalue; idx = index R/K */ - VJMP, /* info = instruction pc */ - VRELOCABLE, /* info = instruction pc */ - VCALL, /* info = instruction pc */ - VVARARG /* info = instruction pc */ + VVOID, /* when 'expdesc' describes the last expression a list, + this kind means an empty list (so, no expression) */ + VNIL, /* constant nil */ + VTRUE, /* constant true */ + VFALSE, /* constant false */ + VK, /* constant in 'k'; info = index of constant in 'k' */ + VKFLT, /* floating constant; nval = numerical float value */ + VKINT, /* integer constant; nval = numerical integer value */ + VNONRELOC, /* expression has its value in a fixed register; + info = result register */ + VLOCAL, /* local variable; info = local register */ + VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VINDEXED, /* indexed variable; + ind.vt = whether 't' is register or upvalue; + ind.t = table register or upvalue; + ind.idx = key's R/K index */ + VJMP, /* expression is a test/comparison; + info = pc of corresponding jump instruction */ + VRELOCABLE, /* expression can put result in any register; + info = instruction pc */ + VCALL, /* expression is a function call; info = instruction pc */ + VVARARG /* vararg expression; info = instruction pc */ } expkind; @@ -40,16 +54,17 @@ typedef enum { typedef struct expdesc { expkind k; union { + lua_Integer ival; /* for VKINT */ + lua_Number nval; /* for VKFLT */ + int info; /* for generic use */ struct { /* for indexed variables (VINDEXED) */ short idx; /* index (R/K) */ lu_byte t; /* table (register or upvalue) */ lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; - int info; /* for generic use */ - lua_Number nval; /* for VKNUM */ } u; - int t; /* patch list of `exit when true' */ - int f; /* patch list of `exit when false' */ + int t; /* patch list of 'exit when true' */ + int f; /* patch list of 'exit when false' */ } expdesc; @@ -95,15 +110,14 @@ struct BlockCnt; /* defined in lparser.c */ /* state needed to generate code for a given function */ typedef struct FuncState { Proto *f; /* current function header */ - Table *h; /* table to find (and reuse) elements in `k' */ struct FuncState *prev; /* enclosing function */ struct LexState *ls; /* lexical state */ struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to `ncode') */ + int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ - int jpc; /* list of pending jumps to `pc' */ - int nk; /* number of elements in `k' */ - int np; /* number of elements in `p' */ + int jpc; /* list of pending jumps to 'pc' */ + int nk; /* number of elements in 'k' */ + int np; /* number of elements in 'p' */ int firstlocal; /* index of first local var (in Dyndata array) */ short nlocvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ @@ -112,8 +126,8 @@ typedef struct FuncState { } FuncState; -LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar); +LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + Dyndata *dyd, const char *name, int firstchar); #endif diff --git a/3rd/lua/lprefix.h b/3rd/lua/lprefix.h new file mode 100644 index 00000000..02daa837 --- /dev/null +++ b/3rd/lua/lprefix.h @@ -0,0 +1,45 @@ +/* +** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ +** Definitions for Lua code that must come before any other header file +** See Copyright Notice in lua.h +*/ + +#ifndef lprefix_h +#define lprefix_h + + +/* +** Allows POSIX/XSI stuff +*/ +#if !defined(LUA_USE_C89) /* { */ + +#if !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE 600 +#elif _XOPEN_SOURCE == 0 +#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ +#endif + +/* +** Allows manipulation of large files in gcc and some other compilers +*/ +#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) +#define _LARGEFILE_SOURCE 1 +#define _FILE_OFFSET_BITS 64 +#endif + +#endif /* } */ + + +/* +** Windows stuff +*/ +#if defined(_WIN32) /* { */ + +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ +#endif + +#endif /* } */ + +#endif + diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index c7f2672b..9194ac34 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,16 +1,18 @@ /* -** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $ +** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ +#define lstate_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define lstate_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -30,18 +32,11 @@ #define LUAI_GCPAUSE 200 /* 200% */ #endif -#if !defined(LUAI_GCMAJOR) -#define LUAI_GCMAJOR 200 /* 200% */ -#endif - #if !defined(LUAI_GCMUL) #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ #endif -#define MEMERRMSG "not enough memory" - - /* ** a macro to help the creation of a unique random seed when a state is ** created; the seed is used to randomize hashes. @@ -57,9 +52,7 @@ ** thread state + extra space */ typedef struct LX { -#if defined(LUAI_EXTRASPACE) - char buff[LUAI_EXTRASPACE]; -#endif + lu_byte extra_[LUA_EXTRASPACE]; lua_State l; } LX; @@ -78,13 +71,12 @@ typedef struct LG { /* -** Compute an initial seed as random as possible. In ANSI, rely on -** Address Space Layout Randomization (if present) to increase -** randomness.. +** Compute an initial seed as random as possible. Rely on Address Space +** Layout Randomization (if present) to increase randomness.. */ #define addbuff(b,p,e) \ { size_t t = cast(size_t, e); \ - memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } + memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } static unsigned int makeseed (lua_State *L) { char buff[4 * sizeof(size_t)]; @@ -101,10 +93,14 @@ static unsigned int makeseed (lua_State *L) { /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant +** invariant (and avoiding underflows in 'totalbytes') */ void luaE_setdebt (global_State *g, l_mem debt) { - g->totalbytes -= (debt - g->GCdebt); + l_mem tb = gettotalbytes(g); + lua_assert(tb > 0); + if (debt < tb - MAX_LMEM) + debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ + g->totalbytes = tb - debt; g->GCdebt = debt; } @@ -115,10 +111,14 @@ CallInfo *luaE_extendCI (lua_State *L) { L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; + L->nci++; return ci; } +/* +** free all CallInfo structures not in use by a thread +*/ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; @@ -126,6 +126,24 @@ void luaE_freeCI (lua_State *L) { while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); + L->nci--; + } +} + + +/* +** free half of the CallInfo structures not in use by a thread +*/ +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 */ + L->nci--; + ci->next = next2; /* remove 'next' from the list */ + next2->previous = ci; + ci = next2; /* keep next's next */ } } @@ -155,6 +173,7 @@ static void freestack (lua_State *L) { return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ luaE_freeCI(L); + lua_assert(L->nci == 0); luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ } @@ -163,34 +182,32 @@ static void freestack (lua_State *L) { ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { - TValue mt; + TValue temp; /* create registry */ Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &mt, L); - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); + setthvalue(L, &temp, L); /* temp = L */ + luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &mt, luaH_new(L)); - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); + sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ + luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); } /* -** open parts of the state that may cause memory-allocation errors +** open parts of the state that may cause memory-allocation errors. +** ('g->version' != NULL flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + luaS_init(L); luaT_init(L); luaX_init(L); - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaS_fix(g->memerrmsg); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); @@ -198,14 +215,16 @@ static void f_luaopen (lua_State *L, void *ud) { /* -** preinitialize a state with consistent values without allocating +** preinitialize a thread with consistent values without allocating ** any memory (to avoid errors) */ -static void preinit_state (lua_State *L, global_State *g) { +static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; L->ci = NULL; + L->nci = 0; L->stacksize = 0; + L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; L->nCcalls = 0; L->hook = NULL; @@ -227,7 +246,6 @@ static void close_state (lua_State *L) { if (g->version) /* closing a fully built state? */ luai_userstateclose(L); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); - luaZ_freebuffer(L, &g->buff); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ @@ -235,17 +253,28 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { + global_State *g = G(L); lua_State *L1; lua_lock(L); luaC_checkGC(L); - L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; + /* create new thread */ + L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; + L1->marked = luaC_white(g); + L1->tt = LUA_TTHREAD; + /* link it on list 'allgc' */ + L1->next = g->allgc; + g->allgc = obj2gco(L1); + /* anchor it on L stack */ setthvalue(L, L->top, L1); api_incr_top(L); - preinit_state(L1, G(L)); + preinit_thread(L1, g); L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; resethookcount(L1); + /* initialize L1 extra space */ + memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), + LUA_EXTRASPACE); luai_userstatethread(L, L1); stack_init(L1, L); /* init stack */ lua_unlock(L); @@ -273,36 +302,31 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g = &l->g; L->next = NULL; L->tt = LUA_TTHREAD; - g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); + g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); - g->gckind = KGC_NORMAL; - preinit_state(L, g); + preinit_thread(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->seed = makeseed(L); - g->uvhead.u.l.prev = &g->uvhead; - g->uvhead.u.l.next = &g->uvhead; g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; - g->strt.size = 0; - g->strt.nuse = 0; + g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); - luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->version = NULL; g->gcstate = GCSpause; - g->allgc = NULL; - g->finobj = NULL; - g->tobefnz = NULL; - g->sweepgc = g->sweepfin = NULL; + g->gckind = KGC_NORMAL; + g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; + g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; + g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; + g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; - g->gcmajorinc = LUAI_GCMAJOR; g->gcstepmul = LUAI_GCMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index daffd9aa..a469466c 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -16,25 +16,16 @@ /* -** Some notes about garbage-collected objects: All objects in Lua must -** be kept somehow accessible until being freed. +** Some notes about garbage-collected objects: All objects in Lua must +** be kept somehow accessible until being freed, so all objects always +** belong to one (and only one) of these lists, using field 'next' of +** the 'CommonHeader' for the link: ** -** Lua keeps most objects linked in list g->allgc. The link uses field -** 'next' of the CommonHeader. -** -** Strings are kept in several lists headed by the array g->strt.hash. -** -** Open upvalues are not subject to independent garbage collection. They -** are collected together with their respective threads. Lua keeps a -** double-linked list with all open upvalues (g->uvhead) so that it can -** mark objects referred by them. (They are always gray, so they must -** be remarked in the atomic step. Usually their contents would be marked -** when traversing the respective threads, but the thread may already be -** dead, while the upvalue is still accessible through closures.) -** -** Objects with finalizers are kept in the list g->finobj. -** -** The list g->tobefnz links all objects being finalized. +** 'allgc': all objects not marked for finalization; +** 'finobj': all objects marked for finalization; +** 'tobefnz': all objects ready to be finalized; +** 'fixedgc': all objects that are not to be collected (currently +** only small strings, such as reserved words). */ @@ -42,6 +33,15 @@ struct lua_longjmp; /* defined in ldo.c */ +/* +** Atomic type (relative to signals) to better ensure that 'lua_sethook' +** is thread safe +*/ +#if !defined(l_signalT) +#include +#define l_signalT sig_atomic_t +#endif + /* extra stack space to handle TM calls and some other extras */ #define EXTRA_STACK 5 @@ -53,66 +53,73 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ #define KGC_NORMAL 0 #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ -#define KGC_GEN 2 /* generational collection */ typedef struct stringtable { - GCObject **hash; - lu_int32 nuse; /* number of elements */ + TString **hash; + int nuse; /* number of elements */ int size; } stringtable; /* -** information about a call +** Information about a call. +** When a thread yields, 'func' is adjusted to pretend that the +** top function has only the yielded values in its stack; in that +** case, the actual 'func' value is saved in field 'extra'. +** When a function calls another with a continuation, 'extra' keeps +** the function index so that, in case of errors, the continuation +** function can be called with the correct top. */ typedef struct CallInfo { StkId func; /* function index in the stack */ StkId top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ - short nresults; /* expected number of results from this function */ - lu_byte callstatus; - ptrdiff_t extra; union { struct { /* only for Lua functions */ StkId base; /* base for this function */ const Instruction *savedpc; } l; struct { /* only for C functions */ - int ctx; /* context info. in case of yields */ - lua_CFunction k; /* continuation in case of yields */ + lua_KFunction k; /* continuation in case of yields */ ptrdiff_t old_errfunc; - lu_byte old_allowhook; - lu_byte status; + lua_KContext ctx; /* context info. in case of yields */ } c; } u; + ptrdiff_t extra; + short nresults; /* expected number of results from this function */ + unsigned short callstatus; } CallInfo; /* ** Bits in CallInfo status */ -#define CIST_LUA (1<<0) /* call is running a Lua function */ -#define CIST_HOOKED (1<<1) /* call is running a debug hook */ -#define CIST_REENTRY (1<<2) /* call is running on same invocation of - luaV_execute of previous call */ -#define CIST_YIELDED (1<<3) /* call reentered after suspension */ +#define CIST_OAH (1<<0) /* original value of 'allowhook' */ +#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_HOOKED (1<<2) /* call is running a debug hook */ +#define CIST_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_STAT (1<<5) /* call has an error status (pcall) */ -#define CIST_TAIL (1<<6) /* call was tail called */ -#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ - +#define CIST_TAIL (1<<5) /* call was tail called */ +#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ +#define CIST_LEQ (1<<7) /* using __lt for __le */ +#define CIST_FIN (1<<8) /* call is running a finalizer */ #define isLua(ci) ((ci)->callstatus & CIST_LUA) +/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ +#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) +#define getoah(st) ((st) & CIST_OAH) + /* -** `global state', shared by all threads of this state +** 'global state', shared by all threads of this state */ typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to `frealloc' */ - lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ + 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 */ @@ -123,36 +130,36 @@ typedef struct global_State { lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcrunning; /* true if GC is running */ - int sweepstrgc; /* position of sweep in `strt' */ GCObject *allgc; /* list of all collectable objects */ + GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ - GCObject **sweepgc; /* current position of sweep in list 'allgc' */ - GCObject **sweepfin; /* current position of sweep in list 'finobj' */ GCObject *gray; /* list of gray objects */ GCObject *grayagain; /* list of objects to be traversed atomically */ GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ - UpVal uvhead; /* head of double-linked list of all open upvalues */ - Mbuffer buff; /* temporary buffer for string concatenation */ + GCObject *fixedgc; /* list of objects not to be collected */ + 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 gcmajorinc; /* pause between major collections (only in gen. mode) */ - int gcstepmul; /* GC `granularity' */ + int gcstepmul; /* GC 'granularity' */ lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; const lua_Number *version; /* pointer to version number */ TString *memerrmsg; /* memory-error message */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ + TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ } global_State; /* -** `per thread' state +** 'per thread' state */ struct lua_State { CommonHeader; + unsigned short nci; /* number of items in 'ci' list */ lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; @@ -160,19 +167,20 @@ struct lua_State { const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ + UpVal *openupval; /* list of open upvalues in this stack */ + GCObject *gclist; + struct lua_State *twups; /* list of threads with open upvalues */ + struct lua_longjmp *errorJmp; /* current error recover point */ + CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ + volatile lua_Hook hook; + ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; - unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls */ - lu_byte hookmask; - lu_byte allowhook; int basehookcount; int hookcount; - lua_Hook hook; - GCObject *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_longjmp *errorJmp; /* current error recover point */ - ptrdiff_t errfunc; /* current error handling function (stack index) */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ + unsigned short nny; /* number of non-yieldable calls in stack */ + unsigned short nCcalls; /* number of nested C calls */ + l_signalT hookmask; + lu_byte allowhook; }; @@ -180,48 +188,47 @@ struct lua_State { /* -** Union of all collectable objects +** Union of all collectable objects (only for conversions) */ -union GCObject { - GCheader gch; /* common header */ - union TString ts; - union Udata u; +union GCUnion { + GCObject gc; /* common header */ + struct TString ts; + struct Udata u; union Closure cl; struct Table h; struct Proto p; - struct UpVal uv; struct lua_State th; /* thread */ }; -#define gch(o) (&(o)->gch) +#define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ -#define rawgco2ts(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) -#define gco2ts(o) (&rawgco2ts(o)->tsv) -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) -#define gco2u(o) (&rawgco2u(o)->uv) -#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) -#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) +#define gco2ts(o) \ + check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) +#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) +#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) -#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) -#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) + check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) +#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) +#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) +#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -/* macro to convert any Lua object into a GCObject */ -#define obj2gco(v) (cast(GCObject *, (v))) + +/* macro to convert a Lua object into a GCObject */ +#define obj2gco(v) \ + check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) /* actual number of total bytes allocated */ -#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) +#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); +LUAI_FUNC void luaE_shrinkCI (lua_State *L); #endif diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index af96c89c..71443a91 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,23 +1,30 @@ /* -** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ - -#include - #define lstring_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" +#include "ldebug.h" +#include "ldo.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" +#define MEMERRMSG "not enough memory" + + /* ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to ** compute its hash @@ -31,124 +38,167 @@ ** equality for long strings */ int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->tsv.len; - lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); + size_t len = a->u.lnglen; + lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); return (a == b) || /* same instance or... */ - ((len == b->tsv.len) && /* equal length and ... */ + ((len == b->u.lnglen) && /* equal length and ... */ (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ } -/* -** equality for strings -*/ -int luaS_eqstr (TString *a, TString *b) { - return (a->tsv.tt == b->tsv.tt) && - (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); -} - - unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast(unsigned int, l); - size_t l1; size_t step = (l >> LUAI_HASHLIMIT) + 1; - for (l1 = l; l1 >= step; l1 -= step) - h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); + for (; l >= step; l -= step) + h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } +unsigned int luaS_hashlongstr (TString *ts) { + lua_assert(ts->tt == LUA_TLNGSTR); + if (ts->extra == 0) { /* no hash? */ + ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); + ts->extra = 1; /* now it has its hash */ + } + return ts->hash; +} + + /* ** resizes the string table */ void luaS_resize (lua_State *L, int newsize) { int i; stringtable *tb = &G(L)->strt; - /* cannot resize while GC is traversing strings */ - luaC_runtilstate(L, ~bitmask(GCSsweepstring)); - if (newsize > tb->size) { - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); - for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; + if (newsize > tb->size) { /* grow table if needed */ + luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); + for (i = tb->size; i < newsize; i++) + tb->hash[i] = NULL; } - /* rehash */ - for (i=0; isize; i++) { - GCObject *p = tb->hash[i]; + for (i = 0; i < tb->size; i++) { /* rehash */ + TString *p = tb->hash[i]; tb->hash[i] = NULL; while (p) { /* for each node in the list */ - GCObject *next = gch(p)->next; /* save next */ - unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ - gch(p)->next = tb->hash[h]; /* chain it */ + TString *hnext = p->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; - resetoldbit(p); /* see MOVE OLD rule */ - p = next; + p = hnext; } } - if (newsize < tb->size) { - /* shrinking slice must be empty */ + if (newsize < tb->size) { /* shrink table if needed */ + /* vanishing slice should be empty */ lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); + luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); } tb->size = newsize; } /* -** creates a new string object +** Clear API string cache. (Entries cannot be empty, so fill them with +** a non-collectable string.) */ -static TString *createstrobj (lua_State *L, const char *str, size_t l, - int tag, unsigned int h, GCObject **list) { - TString *ts; - size_t totalsize; /* total size of TString object */ - totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); - ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; - ts->tsv.len = l; - ts->tsv.hash = h; - ts->tsv.extra = 0; - memcpy(ts+1, str, l*sizeof(char)); - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ - return ts; +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 */ + } } /* -** creates a new short string, inserting it into string table +** Initialize the string table and the string cache */ -static TString *newshrstr (lua_State *L, const char *str, size_t l, - unsigned int h) { - GCObject **list; /* (pointer to) list where it will be inserted */ +void luaS_init (lua_State *L) { + global_State *g = G(L); + int i, j; + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + /* pre-create memory-error message */ + g->memerrmsg = luaS_newliteral(L, MEMERRMSG); + luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ + for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ + for (j = 0; j < STRCACHE_M; j++) + g->strcache[i][j] = g->memerrmsg; +} + + + +/* +** creates a new string object +*/ +static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { + TString *ts; + GCObject *o; + size_t totalsize; /* total size of TString object */ + totalsize = sizelstring(l); + o = luaC_newobj(L, tag, totalsize); + ts = gco2ts(o); + ts->hash = h; + ts->extra = 0; + getstr(ts)[l] = '\0'; /* ending 0 */ + return ts; +} + + +TString *luaS_createlngstrobj (lua_State *L, size_t l) { + TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); + ts->u.lnglen = l; + return ts; +} + + +void luaS_remove (lua_State *L, TString *ts) { stringtable *tb = &G(L)->strt; - TString *s; - if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) - luaS_resize(L, tb->size*2); /* too crowded */ - list = &tb->hash[lmod(h, tb->size)]; - s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); - tb->nuse++; - return s; + TString **p = &tb->hash[lmod(ts->hash, tb->size)]; + while (*p != ts) /* find previous element */ + p = &(*p)->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) { - GCObject *o; +static TString *queryshrstr (lua_State *L, const char *str, size_t l, unsigned int h) { + TString *ts; global_State *g = G(L); - unsigned int h = luaS_hash(str, l, g->seed); - for (o = g->strt.hash[lmod(h, g->strt.size)]; - o != NULL; - o = gch(o)->next) { - TString *ts = rawgco2ts(o); - if (h == ts->tsv.hash && - l == ts->tsv.len && + TString **list = &g->strt.hash[lmod(h, g->strt.size)]; + 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 (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ - changewhite(o); /* resurrect it */ + /* found! */ + if (isdead(g, ts)) /* dead (but not collected yet)? */ + changewhite(ts); /* resurrect it */ return ts; } } - return newshrstr(L, str, l, h); /* not found; create a new string */ + 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); /* ** new string (with explicit length) @@ -157,29 +207,263 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l <= LUAI_MAXSHORTLEN) /* short string? */ return internshrstr(L, str, l); else { - if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + TString *ts; + if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) luaM_toobig(L); - return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); + ts = luaS_createlngstrobj(L, l); + memcpy(getstr(ts), str, l * sizeof(char)); + return ts; } } /* -** new zero-terminated string +** Create or reuse a zero-terminated string, first checking in the +** cache (using the string address as a key). The cache can contain +** only zero-terminated strings, so it is safe to use 'strcmp' to +** check hits. */ TString *luaS_new (lua_State *L, const char *str) { - return luaS_newlstr(L, str, strlen(str)); + unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ + int j; + TString **p = G(L)->strcache[i]; + for (j = 0; j < STRCACHE_M; j++) { + if (strcmp(str, getstr(p[j])) == 0) /* hit? */ + return p[j]; /* that is it */ + } + /* normal route */ + for (j = STRCACHE_M - 1; j > 0; j--) + p[j] = p[j - 1]; /* move out last element */ + /* new element is first in the list */ + p[0] = luaS_newlstr(L, str, strlen(str)); + return p[0]; } -Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { +Udata *luaS_newudata (lua_State *L, size_t s) { Udata *u; - if (s > MAX_SIZET - sizeof(Udata)) + GCObject *o; + if (s > MAX_SIZE - sizeof(Udata)) luaM_toobig(L); - u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; - u->uv.len = s; - u->uv.metatable = NULL; - u->uv.env = e; + o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); + u = gco2u(o); + u->len = s; + u->metatable = NULL; + setuservalue(L, u, luaO_nilobject); return u; } +/* + * global shared table + */ + +#include "rwlock.h" +#include "atomic.h" +#include + +#define SHRSTR_SLOT 0x10000 +#define HASH_NODE(h) ((h) % SHRSTR_SLOT) +#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) + +struct shrmap_slot { + struct rwlock lock; + TString *str; +}; + +struct shrmap { + struct shrmap_slot h[SHRSTR_SLOT]; + int n; +}; + +static struct shrmap SSM; + +LUA_API void +luaS_initshr() { + struct shrmap * s = &SSM; + int i; + for (i=0;ih[i].lock); + } +} + +LUA_API void +luaS_exitshr() { + int i; + for (i=0;iu.hnext; + free(str); + str = next; + } + } +} + +static TString * +query_string(unsigned int h, const char *str, lu_byte l) { + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + break; + } + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); + return ts; +} + +static TString * +query_ptr(TString *t) { + unsigned int h = t->hash; + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts == t) + break; + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); + return ts; +} + +static TString * +new_string(unsigned int h, const char *str, lu_byte l) { + size_t sz = sizelstring(l); + TString *ts = malloc(sz); + memset(ts, 0, sz); + ts->tt = LUA_TSHRSTR; + ts->hash = h; + ts->shrlen = l; + memcpy(ts+1, str, l); + return ts; +} + +static TString * +add_string(unsigned int h, const char *str, lu_byte l) { + TString * tmp = new_string(h, str, l); + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; + rwlock_wlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + break; + } + ts = ts->u.hnext; + } + if (ts == NULL) { + ts = tmp; + ts->u.hnext = s->str; + s->str = ts; + tmp = NULL; + } + rwlock_wunlock(&s->lock); + if (tmp) { + // string is create by other thread, so free tmp + free(tmp); + } + return ts; +} + +static TString * +internshrstr (lua_State *L, const char *str, size_t l) { + TString *ts; + global_State *g = G(L); + unsigned int h = luaS_hash(str, l, g->seed); + unsigned int h0; + // lookup global state of this L first + ts = queryshrstr (L, str, l, h); + if (ts) + return ts; + // lookup SSM again + h0 = luaS_hash(str, l, 0); + ts = query_string(h0, str, l); + if (ts) + return ts; + // If SSM.n greate than 0, add it to SSM + if (SSM.n > 0) { + ATOM_DEC(&SSM.n); + return add_string(h0, str, l); + } + // Else add it to global state (local) + return addshrstr (L, str, l, h); +} + +LUA_API void +luaS_expandshr(int n) { + ATOM_ADD(&SSM.n, n); +} + +LUAI_FUNC TString * +luaS_clonestring(lua_State *L, TString *ts) { + unsigned int h; + int l; + const char * str = getaddrstr(ts); + global_State *g = G(L); + TString *result; + if (ts->tt == LUA_TLNGSTR) + return luaS_newlstr(L, str, ts->u.lnglen); + // look up global state of this L first + l = ts->shrlen; + h = luaS_hash(str, l, g->seed); + result = queryshrstr (L, str, l, h); + if (result) + return result; + // look up SSM by ptr + result = query_ptr(ts); + if (result) + return result; + h = luaS_hash(str, l, 0); + result = query_string(h, str, l); + if (result) + return result; + // ts is not in SSM, so recalc hash, and add it to SSM + return add_string(h, str, l); +} + +struct slotinfo { + int len; + int size; +}; + +static void +getslot(struct shrmap_slot *s, struct slotinfo *info) { + memset(info, 0, sizeof(*info)); + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + ++info->len; + info->size += sizelstring(ts->shrlen); + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); +} + +LUA_API int +luaS_shrinfo(lua_State *L) { + struct slotinfo total; + struct slotinfo tmp; + memset(&total, 0, sizeof(total)); + int i; + int len = 0; + for (i=0;i total.len) { + total.len = tmp.len; + } + total.size += tmp.size; + } + lua_pushinteger(L, len); + lua_pushinteger(L, total.size); + lua_pushinteger(L, total.len); + lua_pushinteger(L, SSM.n); + return 4; +} diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 260e7f16..ca5f0a35 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -12,35 +12,45 @@ #include "lstate.h" -#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) +#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) -#define sizeudata(u) (sizeof(union Udata)+(u)->len) +#define sizeludata(l) (sizeof(union UUdata) + (l)) +#define sizeudata(u) sizeludata((u)->len) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) - /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) +#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) /* ** equality for short strings, which are always internalized */ -#define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) +#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); +LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); -LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); +LUAI_FUNC void luaS_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); +#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); #endif diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 9261fd22..c7aa755f 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,19 +1,24 @@ /* -** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ +#define lstrlib_c +#define LUA_LIB + +#include "lprefix.h" + #include +#include +#include +#include #include #include #include #include -#define lstrlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -22,17 +27,29 @@ /* ** maximum number of captures that a pattern can do during -** pattern-matching. This limit is arbitrary. +** pattern-matching. This limit is arbitrary, but must fit in +** an unsigned char. */ #if !defined(LUA_MAXCAPTURES) #define LUA_MAXCAPTURES 32 #endif -/* macro to `unsign' a character */ +/* macro to 'unsign' a character */ #define uchar(c) ((unsigned char)(c)) +/* +** Some sizes are better limited to fit in 'int', but must also fit in +** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) +*/ +#define MAX_SIZET ((size_t)(~(size_t)0)) + +#define MAXSIZE \ + (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) + + + static int str_len (lua_State *L) { size_t l; @@ -43,22 +60,22 @@ static int str_len (lua_State *L) { /* translate a relative string position: negative means back from end */ -static size_t posrelat (ptrdiff_t pos, size_t len) { - if (pos >= 0) return (size_t)pos; +static lua_Integer posrelat (lua_Integer pos, size_t len) { + if (pos >= 0) return pos; else if (0u - (size_t)pos > len) return 0; - else return len - ((size_t)-pos) + 1; + else return (lua_Integer)len + pos + 1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - size_t start = posrelat(luaL_checkinteger(L, 2), l); - size_t end = posrelat(luaL_optinteger(L, 3, -1), l); + lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); + lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); if (start < 1) start = 1; - if (end > l) end = l; + if (end > (lua_Integer)l) end = l; if (start <= end) - lua_pushlstring(L, s + start - 1, end - start + 1); + lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1); else lua_pushliteral(L, ""); return 1; } @@ -102,25 +119,23 @@ static int str_upper (lua_State *L) { } -/* reasonable limit to avoid arithmetic overflow */ -#define MAXSIZE ((~(size_t)0) >> 1) - static int str_rep (lua_State *L) { size_t l, lsep; const char *s = luaL_checklstring(L, 1, &l); - int n = luaL_checkint(L, 2); + lua_Integer n = luaL_checkinteger(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); if (n <= 0) lua_pushliteral(L, ""); - else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ + else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */ return luaL_error(L, "resulting string too large"); else { - size_t totallen = n * l + (n - 1) * lsep; + size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ memcpy(p, s, l * sizeof(char)); p += l; - if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ - memcpy(p, sep, lsep * sizeof(char)); p += lsep; + if (lsep > 0) { /* empty 'memcpy' is not that cheap */ + memcpy(p, sep, lsep * sizeof(char)); + p += lsep; } } memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ @@ -133,15 +148,15 @@ static int str_rep (lua_State *L) { static int str_byte (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); - size_t pose = posrelat(luaL_optinteger(L, 3, posi), l); + lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); + lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); int n, i; if (posi < 1) posi = 1; - if (pose > l) pose = l; + if (pose > (lua_Integer)l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ - n = (int)(pose - posi + 1); - if (posi + n <= pose) /* (size_t -> int) overflow? */ + if (pose - posi >= INT_MAX) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); + n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; ip_end) - luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); + luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; - do { /* look for a `]' */ + do { /* look for a ']' */ if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); + luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) - p++; /* skip escapes (e.g. `%]') */ + p++; /* skip escapes (e.g. '%]') */ } while (*p != ']'); return p+1; } @@ -287,7 +303,7 @@ static int matchbracketclass (int c, const char *p, const char *ec) { int sig = 1; if (*(p+1) == '^') { sig = 0; - p++; /* skip the `^' */ + p++; /* skip the '^' */ } while (++p < ec) { if (*p == L_ESC) { @@ -325,8 +341,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p, static const char *matchbalance (MatchState *ms, const char *s, const char *p) { if (p >= ms->p_end - 1) - luaL_error(ms->L, "malformed pattern " - "(missing arguments to " LUA_QL("%%b") ")"); + luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { int b = *p; @@ -425,7 +440,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { break; } case '$': { - if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ + if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ goto dflt; /* no; go to default */ s = (s == ms->src_end) ? s : NULL; /* check end of string */ break; @@ -443,8 +458,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { const char *ep; char previous; p += 2; if (*p != '[') - luaL_error(ms->L, "missing " LUA_QL("[") " after " - LUA_QL("%%f") " in pattern"); + luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); if (!matchbracketclass(uchar(previous), p, ep - 1) && @@ -490,7 +504,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { } case '+': /* 1 or more repetitions */ s++; /* 1 match already done */ - /* go through */ + /* FALLTHROUGH */ case '*': /* 0 or more repetitions */ s = max_expand(ms, s, p, ep); break; @@ -514,16 +528,16 @@ static const char *match (MatchState *ms, const char *s, const char *p) { static const char *lmemfind (const char *s1, size_t l1, const char *s2, size_t l2) { if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative `l1' */ + else if (l2 > l1) return NULL; /* avoids a negative 'l1' */ else { - const char *init; /* to search for a `*s2' inside `s1' */ - l2--; /* 1st char will be checked by `memchr' */ - l1 = l1-l2; /* `s2' cannot be found after that */ + const char *init; /* to search for a '*s2' inside 's1' */ + l2--; /* 1st char will be checked by 'memchr' */ + l1 = l1-l2; /* 's2' cannot be found after that */ while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { init++; /* 1st char is already checked */ if (memcmp(init, s2+1, l2) == 0) return init-1; - else { /* correct `l1' and `s1' to try again */ + else { /* correct 'l1' and 's1' to try again */ l1 -= init-s1; s1 = init; } @@ -539,13 +553,13 @@ static void push_onecapture (MatchState *ms, int i, const char *s, if (i == 0) /* ms->level == 0, too */ lua_pushlstring(ms->L, s, e - s); /* add whole match */ else - luaL_error(ms->L, "invalid capture index"); + luaL_error(ms->L, "invalid capture index %%%d", i + 1); } else { ptrdiff_t l = ms->capture[i].len; if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); + lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); else lua_pushlstring(ms->L, ms->capture[i].init, l); } @@ -574,23 +588,39 @@ static int nospecials (const char *p, size_t l) { } +static void prepstate (MatchState *ms, lua_State *L, + const char *s, size_t ls, const char *p, size_t lp) { + ms->L = L; + ms->matchdepth = MAXCCALLS; + ms->src_init = s; + ms->src_end = s + ls; + ms->p_end = p + lp; +} + + +static void reprepstate (MatchState *ms) { + ms->level = 0; + lua_assert(ms->matchdepth == MAXCCALLS); +} + + static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); - size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); + lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); if (init < 1) init = 1; - else if (init > ls + 1) { /* start after string's end? */ + else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ - const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); + const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); if (s2) { - lua_pushinteger(L, s2 - s + 1); - lua_pushinteger(L, s2 - s + lp); + lua_pushinteger(L, (s2 - s) + 1); + lua_pushinteger(L, (s2 - s) + lp); return 2; } } @@ -601,18 +631,13 @@ static int str_find_aux (lua_State *L, int find) { if (anchor) { p++; lp--; /* skip anchor character */ } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s + ls; - ms.p_end = p + lp; + prepstate(&ms, L, s, ls, p, lp); do { const char *res; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); + reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { - lua_pushinteger(L, s1 - s + 1); /* start */ + lua_pushinteger(L, (s1 - s) + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return push_captures(&ms, NULL, 0) + 2; } @@ -636,29 +661,25 @@ static int str_match (lua_State *L) { } +/* state for 'gmatch' */ +typedef struct GMatchState { + const char *src; /* current position */ + const char *p; /* pattern */ + const char *lastmatch; /* end of last match */ + MatchState ms; /* match state */ +} GMatchState; + + static int gmatch_aux (lua_State *L) { - MatchState ms; - size_t ls, lp; - const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); - const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); + GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); const char *src; - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s+ls; - ms.p_end = p + lp; - for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); - src <= ms.src_end; - src++) { + gm->ms.L = L; + for (src = gm->src; src <= gm->ms.src_end; src++) { const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - if ((e = match(&ms, src, p)) != NULL) { - lua_Integer newstart = e-s; - if (e == src) newstart++; /* empty match? go at least one position */ - lua_pushinteger(L, newstart); - lua_replace(L, lua_upvalueindex(3)); - return push_captures(&ms, src, e); + reprepstate(&gm->ms); + if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { + gm->src = gm->lastmatch = e; + return push_captures(&gm->ms, src, e); } } return 0; /* not found */ @@ -666,10 +687,14 @@ static int gmatch_aux (lua_State *L) { static int gmatch (lua_State *L) { - luaL_checkstring(L, 1); - luaL_checkstring(L, 2); - lua_settop(L, 2); - lua_pushinteger(L, 0); + size_t ls, lp; + const char *s = luaL_checklstring(L, 1, &ls); + const char *p = luaL_checklstring(L, 2, &lp); + GMatchState *gm; + lua_settop(L, 2); /* keep them on closure to avoid being collected */ + gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); + prepstate(&gm->ms, L, s, ls, p, lp); + gm->src = s; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); return 1; } @@ -678,7 +703,8 @@ static int gmatch (lua_State *L) { static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); + lua_State *L = ms->L; + const char *news = lua_tolstring(L, 3, &l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) luaL_addchar(b, news[i]); @@ -686,14 +712,15 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, i++; /* skip ESC */ if (!isdigit(uchar(news[i]))) { if (news[i] != L_ESC) - luaL_error(ms->L, "invalid use of " LUA_QL("%c") - " in replacement string", L_ESC); + luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); luaL_addchar(b, news[i]); } else if (news[i] == '0') luaL_addlstring(b, s, e - s); else { push_onecapture(ms, news[i] - '1', s, e); + luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ + lua_remove(L, -2); /* remove original value */ luaL_addvalue(b); /* add capture to accumulated result */ } } @@ -734,12 +761,13 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, static int str_gsub (lua_State *L) { size_t srcl, lp; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checklstring(L, 2, &lp); - int tr = lua_type(L, 3); - size_t max_s = luaL_optinteger(L, 4, srcl+1); + const char *src = luaL_checklstring(L, 1, &srcl); /* subject */ + const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ + const char *lastmatch = NULL; /* end of last match */ + int tr = lua_type(L, 3); /* replacement type */ + lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ int anchor = (*p == '^'); - size_t n = 0; + lua_Integer n = 0; /* replacement count */ MatchState ms; luaL_Buffer b; luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || @@ -749,25 +777,18 @@ static int str_gsub (lua_State *L) { if (anchor) { p++; lp--; /* skip anchor character */ } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = src; - ms.src_end = src+srcl; - ms.p_end = p + lp; + prepstate(&ms, L, src, srcl, p, lp); while (n < max_s) { const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - e = match(&ms, src, p); - if (e) { + reprepstate(&ms); /* (re)prepare state for new match */ + if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; - add_value(&ms, &b, src, e, tr); + add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ + src = lastmatch = e; } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) + else if (src < ms.src_end) /* otherwise, skip one character */ luaL_addchar(&b, *src++); - else break; + else break; /* end of subject */ if (anchor) break; } luaL_addlstring(&b, src, ms.src_end-src); @@ -786,65 +807,117 @@ static int str_gsub (lua_State *L) { ** ======================================================= */ +#if !defined(lua_number2strx) /* { */ + /* -** LUA_INTFRMLEN is the length modifier for integer conversions in -** 'string.format'; LUA_INTFRM_T is the integer type corresponding to -** the previous length +** Hexadecimal floating-point formatter */ -#if !defined(LUA_INTFRMLEN) /* { */ -#if defined(LUA_USE_LONGLONG) -#define LUA_INTFRMLEN "ll" -#define LUA_INTFRM_T long long +#include -#else +#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long -#endif +/* +** Number of bits that goes into the first digit. It can be any value +** between 1 and 4; the following definition tries to align the number +** to nibble boundaries by making what is left after that first digit a +** multiple of 4. +*/ +#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1) + + +/* +** Add integer part of 'x' to buffer and return new 'x' +*/ +static lua_Number adddigit (char *buff, int n, lua_Number x) { + lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ + int d = (int)dd; + buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ + return x - dd; /* return what is left */ +} + + +static int num2straux (char *buff, int sz, lua_Number x) { + /* if 'inf' or 'NaN', format it like '%g' */ + if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) + return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); + else if (x == 0) { /* can be -0... */ + /* create "0" or "-0" followed by exponent */ + return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x); + } + else { + int e; + lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ + int n = 0; /* character count */ + if (m < 0) { /* is number negative? */ + buff[n++] = '-'; /* add signal */ + m = -m; /* make it positive */ + } + buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ + m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ + e -= L_NBFD; /* this digit goes before the radix point */ + if (m > 0) { /* more digits? */ + buff[n++] = lua_getlocaledecpoint(); /* add radix point */ + do { /* add as many digits as needed */ + m = adddigit(buff, n++, m * 16); + } while (m > 0); + } + n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ + lua_assert(n < sz); + return n; + } +} + + +static int lua_number2strx (lua_State *L, char *buff, int sz, + const char *fmt, lua_Number x) { + int n = num2straux(buff, sz, x); + if (fmt[SIZELENMOD] == 'A') { + int i; + for (i = 0; i < n; i++) + buff[i] = toupper(uchar(buff[i])); + } + else if (fmt[SIZELENMOD] != 'a') + luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); + return n; +} + #endif /* } */ /* -** LUA_FLTFRMLEN is the length modifier for float conversions in -** 'string.format'; LUA_FLTFRM_T is the float type corresponding to -** the previous length +** Maximum size of each formatted item. This maximum 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) */ -#if !defined(LUA_FLTFRMLEN) - -#define LUA_FLTFRMLEN "" -#define LUA_FLTFRM_T double - -#endif +#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM 512 /* valid flags in a format specification */ #define FLAGS "-+ #0" + /* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) +** maximum size of each format specification (such as "%-099.99d") */ -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) +#define MAX_FORMAT 32 -static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); +static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '"'); - while (l--) { + while (len--) { if (*s == '"' || *s == '\\' || *s == '\n') { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } - else if (*s == '\0' || iscntrl(uchar(*s))) { + else if (iscntrl(uchar(*s))) { char buff[10]; if (!isdigit(uchar(*(s+1)))) - sprintf(buff, "\\%d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); else - sprintf(buff, "\\%03d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); luaL_addstring(b, buff); } else @@ -854,6 +927,57 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { luaL_addchar(b, '"'); } + +/* +** Ensures the 'buff' string uses a dot as the radix character. +*/ +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 void addliteral (lua_State *L, luaL_Buffer *b, int arg) { + switch (lua_type(L, arg)) { + case LUA_TSTRING: { + size_t len; + const char *s = lua_tolstring(L, arg, &len); + addquoted(b, s, len); + break; + } + case LUA_TNUMBER: { + char *buff = luaL_prepbuffsize(b, MAX_ITEM); + int nb; + if (!lua_isinteger(L, arg)) { /* float? */ + lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */ + nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); + checkdp(buff, nb); /* ensure it uses a dot */ + } + else { /* integers */ + lua_Integer n = lua_tointeger(L, arg); + const char *format = (n == LUA_MININTEGER) /* corner case? */ + ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ + : LUA_INTEGER_FMT; /* else use default format */ + nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); + } + luaL_addsize(b, nb); + break; + } + case LUA_TNIL: case LUA_TBOOLEAN: { + luaL_tolstring(L, arg, NULL); + luaL_addvalue(b); + break; + } + default: { + luaL_argerror(L, arg, "value has no literal form"); + } + } +} + + static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ @@ -869,8 +993,8 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { if (isdigit(uchar(*p))) luaL_error(L, "invalid format (width or precision too long)"); *(form++) = '%'; - memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); - form += p - strfrmt + 1; + memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); + form += (p - strfrmt) + 1; *form = '\0'; return p; } @@ -903,7 +1027,7 @@ static int str_format (lua_State *L) { else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ + char form[MAX_FORMAT]; /* to store the format ('%...') */ char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) @@ -911,62 +1035,56 @@ static int str_format (lua_State *L) { strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = sprintf(buff, form, luaL_checkint(L, arg)); - break; - } - case 'd': case 'i': { - lua_Number n = luaL_checknumber(L, arg); - LUA_INTFRM_T ni = (LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); + nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); break; } + case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { - lua_Number n = luaL_checknumber(L, arg); - unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a non-negative number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); + lua_Integer n = luaL_checkinteger(L, arg); + addlenmod(form, LUA_INTEGER_FRMLEN); + nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); break; } - case 'e': case 'E': case 'f': -#if defined(LUA_USE_AFORMAT) case 'a': case 'A': -#endif + addlenmod(form, LUA_NUMBER_FRMLEN); + nb = lua_number2strx(L, buff, MAX_ITEM, form, + luaL_checknumber(L, arg)); + break; + case 'e': case 'E': case 'f': case 'g': case 'G': { - addlenmod(form, LUA_FLTFRMLEN); - nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); + lua_Number n = luaL_checknumber(L, arg); + addlenmod(form, LUA_NUMBER_FRMLEN); + nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); break; } case 'q': { - addquoted(L, &b, arg); + addliteral(L, &b, arg); break; } case 's': { size_t l; const char *s = luaL_tolstring(L, arg, &l); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - luaL_addvalue(&b); - break; - } + if (form[2] == '\0') /* no modifiers? */ + luaL_addvalue(&b); /* keep entire string */ else { - nb = sprintf(buff, form, s); - lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ - break; + luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted */ + luaL_addvalue(&b); /* keep entire string */ + } + else { /* format the string into 'buff' */ + nb = l_sprintf(buff, MAX_ITEM, form, s); + lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + } } + break; } - default: { /* also treat cases `pnLlh' */ - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " - LUA_QL("format"), *(strfrmt - 1)); + default: { /* also treat cases 'pnLlh' */ + return luaL_error(L, "invalid option '%%%c' to 'format'", + *(strfrmt - 1)); } } + lua_assert(nb < MAX_ITEM); luaL_addsize(&b, nb); } } @@ -977,6 +1095,450 @@ static int str_format (lua_State *L) { /* }====================================================== */ +/* +** {====================================================== +** PACK/UNPACK +** ======================================================= +*/ + + +/* value used for padding */ +#if !defined(LUAL_PACKPADBYTE) +#define LUAL_PACKPADBYTE 0x00 +#endif + +/* maximum size for the binary representation of an integer */ +#define MAXINTSIZE 16 + +/* number of bits in a character */ +#define NB CHAR_BIT + +/* mask for one character (NB 1's) */ +#define MC ((1 << NB) - 1) + +/* size of a lua_Integer */ +#define SZINT ((int)sizeof(lua_Integer)) + + +/* dummy union to get native endianness */ +static const union { + int dummy; + char little; /* true iff machine is little endian */ +} nativeendian = {1}; + + +/* dummy structure to get native alignment requirements */ +struct cD { + char c; + union { double d; void *p; lua_Integer i; lua_Number n; } u; +}; + +#define MAXALIGN (offsetof(struct cD, u)) + + +/* +** Union for serializing floats +*/ +typedef union Ftypes { + float f; + double d; + lua_Number n; + char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ +} Ftypes; + + +/* +** information to pack/unpack stuff +*/ +typedef struct Header { + lua_State *L; + int islittle; + int maxalign; +} Header; + + +/* +** options for pack/unpack +*/ +typedef enum KOption { + Kint, /* signed integers */ + Kuint, /* unsigned integers */ + Kfloat, /* floating-point numbers */ + Kchar, /* fixed-length strings */ + Kstring, /* strings with prefixed length */ + Kzstr, /* zero-terminated strings */ + Kpadding, /* padding */ + Kpaddalign, /* padding for alignment */ + Knop /* no-op (configuration or spaces) */ +} KOption; + + +/* +** Read an integer numeral from string 'fmt' or return 'df' if +** there is no numeral +*/ +static int digit (int c) { return '0' <= c && c <= '9'; } + +static int getnum (const char **fmt, int df) { + if (!digit(**fmt)) /* no number? */ + return df; /* return default value */ + else { + int a = 0; + do { + a = a*10 + (*((*fmt)++) - '0'); + } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); + return a; + } +} + + +/* +** Read an integer numeral and raises an error if it is larger +** than the maximum size for integers. +*/ +static int getnumlimit (Header *h, const char **fmt, int df) { + int sz = getnum(fmt, df); + if (sz > MAXINTSIZE || sz <= 0) + luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); + return sz; +} + + +/* +** Initialize Header +*/ +static void initheader (lua_State *L, Header *h) { + h->L = L; + h->islittle = nativeendian.little; + h->maxalign = 1; +} + + +/* +** Read and classify next option. 'size' is filled with option's size. +*/ +static KOption getoption (Header *h, const char **fmt, int *size) { + int opt = *((*fmt)++); + *size = 0; /* default */ + switch (opt) { + case 'b': *size = sizeof(char); return Kint; + case 'B': *size = sizeof(char); return Kuint; + case 'h': *size = sizeof(short); return Kint; + case 'H': *size = sizeof(short); return Kuint; + case 'l': *size = sizeof(long); return Kint; + case 'L': *size = sizeof(long); return Kuint; + case 'j': *size = sizeof(lua_Integer); return Kint; + case 'J': *size = sizeof(lua_Integer); return Kuint; + case 'T': *size = sizeof(size_t); return Kuint; + case 'f': *size = sizeof(float); return Kfloat; + case 'd': *size = sizeof(double); return Kfloat; + case 'n': *size = sizeof(lua_Number); return Kfloat; + case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; + case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; + case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; + case 'c': + *size = getnum(fmt, -1); + if (*size == -1) + luaL_error(h->L, "missing size for format option 'c'"); + return Kchar; + case 'z': return Kzstr; + case 'x': *size = 1; return Kpadding; + case 'X': return Kpaddalign; + case ' ': break; + case '<': h->islittle = 1; break; + case '>': h->islittle = 0; break; + case '=': h->islittle = nativeendian.little; break; + case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; + default: luaL_error(h->L, "invalid format option '%c'", opt); + } + return Knop; +} + + +/* +** Read, classify, and fill other details about the next option. +** 'psize' is filled with option's size, 'notoalign' with its +** alignment requirements. +** Local variable 'size' gets the size to be aligned. (Kpadal option +** always gets its full alignment, other options are limited by +** the maximum alignment ('maxalign'). Kchar option needs no alignment +** despite its size. +*/ +static KOption getdetails (Header *h, size_t totalsize, + const char **fmt, int *psize, int *ntoalign) { + KOption opt = getoption(h, fmt, psize); + int align = *psize; /* usually, alignment follows size */ + if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ + if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) + luaL_argerror(h->L, 1, "invalid next option for option 'X'"); + } + if (align <= 1 || opt == Kchar) /* need no alignment? */ + *ntoalign = 0; + else { + if (align > h->maxalign) /* enforce maximum alignment */ + align = h->maxalign; + if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); + *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); + } + return opt; +} + + +/* +** Pack integer 'n' with 'size' bytes and 'islittle' endianness. +** The final 'if' handles the case when 'size' is larger than +** the size of a Lua integer, correcting the extra sign-extension +** bytes if necessary (by default they would be zeros). +*/ +static void packint (luaL_Buffer *b, lua_Unsigned n, + int islittle, int size, int neg) { + char *buff = luaL_prepbuffsize(b, size); + int i; + buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ + for (i = 1; i < size; i++) { + n >>= NB; + buff[islittle ? i : size - 1 - i] = (char)(n & MC); + } + if (neg && size > SZINT) { /* negative number need sign extension? */ + for (i = SZINT; i < size; i++) /* correct extra bytes */ + buff[islittle ? i : size - 1 - i] = (char)MC; + } + luaL_addsize(b, size); /* add result to buffer */ +} + + +/* +** Copy 'size' bytes from 'src' to 'dest', correcting endianness if +** given 'islittle' is different from native endianness. +*/ +static void copywithendian (volatile char *dest, volatile const char *src, + int size, int islittle) { + if (islittle == nativeendian.little) { + while (size-- != 0) + *(dest++) = *(src++); + } + else { + dest += size - 1; + while (size-- != 0) + *(dest--) = *(src++); + } +} + + +static int str_pack (lua_State *L) { + luaL_Buffer b; + Header h; + const char *fmt = luaL_checkstring(L, 1); /* format string */ + int arg = 1; /* current argument to pack */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + lua_pushnil(L); /* mark to separate arguments from string buffer */ + luaL_buffinit(L, &b); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + totalsize += ntoalign + size; + while (ntoalign-- > 0) + luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ + arg++; + switch (opt) { + case Kint: { /* signed integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) { /* need overflow check? */ + lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); + luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); + } + packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); + break; + } + case Kuint: { /* unsigned integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) /* need overflow check? */ + luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), + arg, "unsigned overflow"); + packint(&b, (lua_Unsigned)n, h.islittle, size, 0); + break; + } + case Kfloat: { /* floating-point options */ + volatile Ftypes u; + char *buff = luaL_prepbuffsize(&b, size); + lua_Number n = luaL_checknumber(L, arg); /* get argument */ + if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ + else if (size == sizeof(u.d)) u.d = (double)n; + else u.n = n; + /* move 'u' to final result, correcting endianness if needed */ + copywithendian(buff, u.buff, size, h.islittle); + luaL_addsize(&b, size); + break; + } + case Kchar: { /* fixed-size string */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, len <= (size_t)size, arg, + "string longer than given size"); + luaL_addlstring(&b, s, len); /* add string */ + while (len++ < (size_t)size) /* pad extra space */ + luaL_addchar(&b, LUAL_PACKPADBYTE); + break; + } + case Kstring: { /* strings with length count */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, size >= (int)sizeof(size_t) || + len < ((size_t)1 << (size * NB)), + arg, "string length does not fit in given size"); + packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ + luaL_addlstring(&b, s, len); + totalsize += len; + break; + } + case Kzstr: { /* zero-terminated string */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); + luaL_addlstring(&b, s, len); + luaL_addchar(&b, '\0'); /* add zero at the end */ + totalsize += len + 1; + break; + } + case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */ + case Kpaddalign: case Knop: + arg--; /* undo increment */ + break; + } + } + luaL_pushresult(&b); + return 1; +} + + +static int str_packsize (lua_State *L) { + Header h; + const char *fmt = luaL_checkstring(L, 1); /* format string */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + size += ntoalign; /* total space used by option */ + luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, + "format result too large"); + totalsize += size; + switch (opt) { + case Kstring: /* strings with length count */ + case Kzstr: /* zero-terminated string */ + luaL_argerror(L, 1, "variable-length format"); + /* call never return, but to avoid warnings: *//* FALLTHROUGH */ + default: break; + } + } + lua_pushinteger(L, (lua_Integer)totalsize); + return 1; +} + + +/* +** Unpack an integer with 'size' bytes and 'islittle' endianness. +** If size is smaller than the size of a Lua integer and integer +** is signed, must do sign extension (propagating the sign to the +** higher bits); if size is larger than the size of a Lua integer, +** it must check the unread bytes to see whether they do not cause an +** overflow. +*/ +static lua_Integer unpackint (lua_State *L, const char *str, + int islittle, int size, int issigned) { + lua_Unsigned res = 0; + int i; + int limit = (size <= SZINT) ? size : SZINT; + for (i = limit - 1; i >= 0; i--) { + res <<= NB; + res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; + } + if (size < SZINT) { /* real size smaller than lua_Integer? */ + if (issigned) { /* needs sign extension? */ + lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); + res = ((res ^ mask) - mask); /* do sign extension */ + } + } + else if (size > SZINT) { /* must check unread bytes */ + int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; + for (i = limit; i < size; i++) { + if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) + luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); + } + } + return (lua_Integer)res; +} + + +static int str_unpack (lua_State *L) { + Header h; + const char *fmt = luaL_checkstring(L, 1); + size_t ld; + const char *data = luaL_checklstring(L, 2, &ld); + size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1; + int n = 0; /* number of results */ + luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); + initheader(L, &h); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); + if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) + luaL_argerror(L, 2, "data string too short"); + pos += ntoalign; /* skip alignment */ + /* stack space for item + next position */ + luaL_checkstack(L, 2, "too many results"); + n++; + switch (opt) { + case Kint: + case Kuint: { + lua_Integer res = unpackint(L, data + pos, h.islittle, size, + (opt == Kint)); + lua_pushinteger(L, res); + break; + } + case Kfloat: { + volatile Ftypes u; + lua_Number num; + copywithendian(u.buff, data + pos, size, h.islittle); + if (size == sizeof(u.f)) num = (lua_Number)u.f; + else if (size == sizeof(u.d)) num = (lua_Number)u.d; + else num = u.n; + lua_pushnumber(L, num); + break; + } + case Kchar: { + lua_pushlstring(L, data + pos, size); + break; + } + case Kstring: { + size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); + luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); + lua_pushlstring(L, data + pos + size, len); + pos += len; /* skip string */ + break; + } + case Kzstr: { + size_t len = (int)strlen(data + pos); + lua_pushlstring(L, data + pos, len); + pos += len + 1; /* skip string plus final '\0' */ + break; + } + case Kpaddalign: case Kpadding: case Knop: + n--; /* undo increment */ + break; + } + pos += size; + } + lua_pushinteger(L, pos + 1); /* next position */ + return n + 1; +} + +/* }====================================================== */ + + static const luaL_Reg strlib[] = { {"byte", str_byte}, {"char", str_char}, @@ -992,6 +1554,9 @@ static const luaL_Reg strlib[] = { {"reverse", str_reverse}, {"sub", str_sub}, {"upper", str_upper}, + {"pack", str_pack}, + {"packsize", str_packsize}, + {"unpack", str_unpack}, {NULL, NULL} }; diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 5d76f97e..d080189f 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,27 +1,30 @@ /* -** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ +#define ltable_c +#define LUA_CORE + +#include "lprefix.h" + /* ** Implementation of tables (aka arrays, objects, or hash tables). ** Tables keep its elements in two parts: an array part and a hash part. ** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest `n' such that at -** least half the slots between 0 and n are in use. +** part. The actual size of the array is the largest 'n' such that +** more than half the slots between 1 and n are in use. ** Hash uses a mix of chained scatter table with Brent's variation. ** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the `original' position that its hash gives +** in its main position (i.e. the 'original' position that its hash gives ** to it), then the colliding element is in its own main position. ** Hence even when the load factor reaches 100%, performance remains good. */ -#include - -#define ltable_c -#define LUA_CORE +#include +#include #include "lua.h" @@ -37,21 +40,26 @@ /* -** max size of array part is 2^MAXBITS +** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is +** the largest integer such that MAXASIZE fits in an unsigned int. */ -#if LUAI_BITSINT >= 32 -#define MAXBITS 30 -#else -#define MAXBITS (LUAI_BITSINT-2) -#endif +#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) +#define MAXASIZE (1u << MAXABITS) -#define MAXASIZE (1 << MAXBITS) +/* +** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest +** integer such that 2^MAXHBITS fits in a signed int. (Note that the +** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still +** fits comfortably in an unsigned int.) +*/ +#define MAXHBITS (MAXABITS - 1) #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) +#define hashstr(t,str) hashpow2(t, (str)->hash) #define hashboolean(t,p) hashpow2(t, p) +#define hashint(t,i) hashpow2(t, i) /* @@ -61,53 +69,61 @@ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) -#define hashpointer(t,p) hashmod(t, IntPoint(p)) +#define hashpointer(t,p) hashmod(t, point2uint(p)) #define dummynode (&dummynode_) -#define isdummy(n) ((n) == dummynode) - static const Node dummynode_ = { {NILCONSTANT}, /* value */ - {{NILCONSTANT, NULL}} /* key */ + {{NILCONSTANT, 0}} /* key */ }; /* -** hash for lua_Numbers +** Hash for floating-point numbers. +** The main computation should be just +** n = frexp(n, &i); return (n * INT_MAX) + i +** but there are some numerical subtleties. +** In a two-complement representation, INT_MAX does not has an exact +** representation as a float, but INT_MIN does; because the absolute +** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the +** absolute value of the product 'frexp * -INT_MIN' is smaller or equal +** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when +** adding 'i'; the use of '~u' (instead of '-u') avoids problems with +** INT_MIN. */ -static Node *hashnum (const Table *t, lua_Number n) { +#if !defined(l_hashfloat) +static int l_hashfloat (lua_Number n) { int i; - luai_hashnum(i, n); - if (i < 0) { - if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */ - i = 0; /* handle INT_MIN */ - i = -i; /* must be a positive value */ + lua_Integer ni; + n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); + if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ + lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); + return 0; + } + else { /* normal case */ + unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni); + return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u); } - return hashmod(t, i); } - +#endif /* -** returns the `main' position of an element in a table (that is, the index +** returns the 'main' position of an element in a table (that is, the index ** of its hash value) */ static Node *mainposition (const Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TNUMBER: - return hashnum(t, nvalue(key)); - case LUA_TLNGSTR: { - TString *s = rawtsvalue(key); - if (s->tsv.extra == 0) { /* no hash? */ - s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash); - s->tsv.extra = 1; /* now it has its hash */ - } - return hashstr(t, rawtsvalue(key)); - } + case LUA_TNUMINT: + return hashint(t, ivalue(key)); + case LUA_TNUMFLT: + return hashmod(t, l_hashfloat(fltvalue(key))); case LUA_TSHRSTR: - return hashstr(t, rawtsvalue(key)); + 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: @@ -115,67 +131,68 @@ static Node *mainposition (const Table *t, const TValue *key) { case LUA_TLCF: return hashpointer(t, fvalue(key)); default: + lua_assert(!ttisdeadkey(key)); return hashpointer(t, gcvalue(key)); } } /* -** returns the index for `key' if `key' is an appropriate key to live in -** the array part of the table, -1 otherwise. +** returns the index for 'key' if 'key' is an appropriate key to live in +** the array part of the table, 0 otherwise. */ -static int arrayindex (const TValue *key) { - if (ttisnumber(key)) { - lua_Number n = nvalue(key); - int k; - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) - return k; +static unsigned int arrayindex (const TValue *key) { + if (ttisinteger(key)) { + lua_Integer k = ivalue(key); + if (0 < k && (lua_Unsigned)k <= MAXASIZE) + return cast(unsigned int, k); /* 'key' is an appropriate array index */ } - return -1; /* `key' did not match some condition */ + return 0; /* 'key' did not match some condition */ } /* -** returns the index of a `key' for table traversals. First goes all +** returns the index of a 'key' for table traversals. First goes all ** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signaled by -1. +** beginning of a traversal is signaled by 0. */ -static int findindex (lua_State *L, Table *t, StkId key) { - int i; - if (ttisnil(key)) return -1; /* first iteration */ +static unsigned int findindex (lua_State *L, Table *t, StkId key) { + unsigned int i; + if (ttisnil(key)) return 0; /* first iteration */ i = arrayindex(key); - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ - return i-1; /* yes; that's the index (corrected to C) */ + if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ + return i; /* yes; that's the index */ else { + int nx; Node *n = mainposition(t, key); - for (;;) { /* check whether `key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in `next' */ + for (;;) { /* check whether 'key' is somewhere in the chain */ + /* key may be dead already, but it is ok to use it in 'next' */ if (luaV_rawequalobj(gkey(n), key) || (ttisdeadkey(gkey(n)) && iscollectable(key) && deadvalue(gkey(n)) == gcvalue(key))) { i = cast_int(n - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ - return i + t->sizearray; + return (i + 1) + t->sizearray; } - else n = gnext(n); - if (n == NULL) - luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ + nx = gnext(n); + if (nx == 0) + luaG_runerror(L, "invalid key to 'next'"); /* key not found */ + else n += nx; } } } int luaH_next (lua_State *L, Table *t, StkId key) { - int i = findindex(L, t, key); /* find original element */ - for (i++; i < t->sizearray; i++) { /* try first array part */ + unsigned int i = findindex(L, t, key); /* find original element */ + for (; i < t->sizearray; i++) { /* try first array part */ if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setnvalue(key, cast_num(i+1)); + setivalue(key, i + 1); setobj2s(L, key+1, &t->array[i]); return 1; } } - for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ + for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ setobj2s(L, key, gkey(gnode(t, i))); setobj2s(L, key+1, gval(gnode(t, i))); @@ -192,32 +209,38 @@ int luaH_next (lua_State *L, Table *t, StkId key) { ** ============================================================== */ - -static int computesizes (int nums[], int *narray) { +/* +** Compute the optimal size for the array part of table 't'. 'nums' is a +** "count array" where 'nums[i]' is the number of integers in the table +** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of +** integer keys in the table and leaves with the number of keys that +** will go to the array part; return the optimal size. +*/ +static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { int i; - int twotoi; /* 2^i */ - int a = 0; /* number of elements smaller than 2^i */ - int na = 0; /* number of elements to go to array part */ - int n = 0; /* optimal size for array part */ - for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { + unsigned int twotoi; /* 2^i (candidate for optimal size) */ + unsigned int a = 0; /* number of elements smaller than 2^i */ + unsigned int na = 0; /* number of elements to go to array part */ + unsigned int optimal = 0; /* optimal size for array part */ + /* loop while keys can fill more than half of total size */ + for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { if (nums[i] > 0) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ - n = twotoi; /* optimal size (till now) */ - na = a; /* all elements smaller than n will go to array part */ + optimal = twotoi; /* optimal size (till now) */ + na = a; /* all elements up to 'optimal' will go to array part */ } } - if (a == *narray) break; /* all elements already counted */ } - *narray = n; - lua_assert(*narray/2 <= na && na <= *narray); - return na; + lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); + *pna = na; + return optimal; } -static int countint (const TValue *key, int *nums) { - int k = arrayindex(key); - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ +static int countint (const TValue *key, unsigned int *nums) { + unsigned int k = arrayindex(key); + if (k != 0) { /* is 'key' an appropriate array index? */ nums[luaO_ceillog2(k)]++; /* count as such */ return 1; } @@ -226,20 +249,26 @@ static int countint (const TValue *key, int *nums) { } -static int numusearray (const Table *t, int *nums) { +/* +** Count keys in array part of table 't': Fill 'nums[i]' with +** number of keys that will go into corresponding slice and return +** total number of non-nil keys. +*/ +static unsigned int numusearray (const Table *t, unsigned int *nums) { int lg; - int ttlg; /* 2^lg */ - int ause = 0; /* summation of `nums' */ - int i = 1; /* count to traverse all array keys */ - for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ - int lc = 0; /* counter */ - int lim = ttlg; + unsigned int ttlg; /* 2^lg */ + unsigned int ause = 0; /* summation of 'nums' */ + unsigned int i = 1; /* count to traverse all array keys */ + /* traverse each slice */ + for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { + unsigned int lc = 0; /* counter */ + unsigned int lim = ttlg; if (lim > t->sizearray) { lim = t->sizearray; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } - /* count elements in range (2^(lg-1), 2^lg] */ + /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { if (!ttisnil(&t->array[i-1])) lc++; @@ -251,9 +280,9 @@ static int numusearray (const Table *t, int *nums) { } -static int numusehash (const Table *t, int *nums, int *pnasize) { +static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { int totaluse = 0; /* total number of elements */ - int ause = 0; /* summation of `nums' */ + int ause = 0; /* elements added to 'nums' (can go to array part) */ int i = sizenode(t); while (i--) { Node *n = &t->node[i]; @@ -262,13 +291,13 @@ static int numusehash (const Table *t, int *nums, int *pnasize) { totaluse++; } } - *pnasize += ause; + *pna += ause; return totaluse; } -static void setarrayvector (lua_State *L, Table *t, int size) { - int i; +static void setarrayvector (lua_State *L, Table *t, unsigned int size) { + unsigned int i; luaM_reallocvector(L, t->array, t->sizearray, size, TValue); for (i=t->sizearray; iarray[i]); @@ -276,35 +305,37 @@ static void setarrayvector (lua_State *L, Table *t, int size) { } -static void setnodevector (lua_State *L, Table *t, int size) { - int lsize; +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' */ - lsize = 0; + t->node = cast(Node *, dummynode); /* use common 'dummynode' */ + t->lsizenode = 0; + t->lastfree = NULL; /* signal that it is using dummy node */ } else { int i; - lsize = luaO_ceillog2(size); - if (lsize > MAXBITS) + int lsize = luaO_ceillog2(size); + if (lsize > MAXHBITS) luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); - for (i=0; ilsizenode = cast_byte(lsize); + t->lastfree = gnode(t, size); /* all positions are free */ } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ } -void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { - int i; - int oldasize = t->sizearray; - int oldhsize = t->lsizenode; +void luaH_resize (lua_State *L, Table *t, unsigned int nasize, + unsigned int nhsize) { + unsigned int i; + int j; + unsigned int oldasize = t->sizearray; + int oldhsize = allocsizenode(t); Node *nold = t->node; /* save old hash ... */ if (nasize > oldasize) /* array part must grow? */ setarrayvector(L, t, nasize); @@ -321,41 +352,44 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { luaM_reallocvector(L, t->array, oldasize, nasize, TValue); } /* re-insert elements from hash part */ - for (i = twoto(oldhsize) - 1; i >= 0; i--) { - Node *old = nold+i; + for (j = 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)); } } - if (!isdummy(nold)) - luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */ + if (oldhsize > 0) /* not the dummy node? */ + luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ } -void luaH_resizearray (lua_State *L, Table *t, int nasize) { - int nsize = isdummy(t->node) ? 0 : sizenode(t); +void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { + int nsize = allocsizenode(t); luaH_resize(L, t, nasize, nsize); } - +/* +** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i +*/ static void rehash (lua_State *L, Table *t, const TValue *ek) { - int nasize, na; - int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */ + unsigned int asize; /* optimal size for array part */ + unsigned int na; /* number of keys in the array part */ + unsigned int nums[MAXABITS + 1]; int i; int totaluse; - for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ - nasize = numusearray(t, nums); /* count keys in array part */ - totaluse = nasize; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ + for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ + na = numusearray(t, nums); /* count keys in array part */ + totaluse = na; /* all those keys are integer keys */ + totaluse += numusehash(t, nums, &na); /* count keys in hash part */ /* count extra key */ - nasize += countint(ek, nums); + na += countint(ek, nums); totaluse++; /* compute new size for array part */ - na = computesizes(nums, &nasize); + asize = computesizes(nums, &na); /* resize the table to new computed sizes */ - luaH_resize(L, t, nasize, totaluse - na); + luaH_resize(L, t, asize, totaluse - na); } @@ -366,7 +400,8 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { Table *luaH_new (lua_State *L) { - Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h; + GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); + Table *t = gco2t(o); t->metatable = NULL; t->flags = cast_byte(~0); t->array = NULL; @@ -377,7 +412,7 @@ Table *luaH_new (lua_State *L) { void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t->node)) + if (!isdummy(t)) luaM_freearray(L, t->node, cast(size_t, sizenode(t))); luaM_freearray(L, t->array, t->sizearray); luaM_free(L, t); @@ -385,10 +420,12 @@ void luaH_free (lua_State *L, Table *t) { static Node *getfreepos (Table *t) { - while (t->lastfree > t->node) { - t->lastfree--; - if (ttisnil(gkey(t->lastfree))) - return t->lastfree; + if (!isdummy(t)) { + while (t->lastfree > t->node) { + t->lastfree--; + if (ttisnil(gkey(t->lastfree))) + return t->lastfree; + } } return NULL; /* could not find a free place */ } @@ -404,37 +441,51 @@ static Node *getfreepos (Table *t) { */ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { Node *mp; + TValue aux; if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) - luaG_runerror(L, "table index is NaN"); + else if (ttisfloat(key)) { + lua_Integer k; + if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */ + setivalue(&aux, k); + key = &aux; /* insert it as an integer */ + } + else if (luai_numisnan(fltvalue(key))) + luaG_runerror(L, "table index is NaN"); + } mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ + if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; - Node *n = getfreepos(t); /* get a free place */ - if (n == NULL) { /* cannot find a free place? */ + Node *f = getfreepos(t); /* get a free place */ + if (f == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' take care of TM cache and GC barrier */ + /* whatever called 'newkey' takes care of TM cache */ return luaH_set(L, t, key); /* insert key into grown table */ } - lua_assert(!isdummy(n)); + lua_assert(!isdummy(t)); othern = mainposition(t, gkey(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ - gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ - *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - gnext(mp) = NULL; /* now `mp' is free */ + while (othern + gnext(othern) != mp) /* find previous */ + othern += gnext(othern); + gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ + *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ + if (gnext(mp) != 0) { + gnext(f) += cast_int(mp - f); /* correct 'next' */ + gnext(mp) = 0; /* now 'mp' is free */ + } setnilvalue(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ - gnext(n) = gnext(mp); /* chain new position */ - gnext(mp) = n; - mp = n; + if (gnext(mp) != 0) + gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ + else lua_assert(gnext(f) == 0); + gnext(mp) = cast_int(f - mp); + mp = f; } } - setobj2t(L, gkey(mp), key); - luaC_barrierback(L, obj2gco(t), key); + setnodekey(L, &mp->i_key, key); + luaC_barrierback(L, t, key); lua_assert(ttisnil(gval(mp))); return gval(mp); } @@ -443,18 +494,21 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { /* ** search function for integers */ -const TValue *luaH_getint (Table *t, int key) { +const TValue *luaH_getint (Table *t, lua_Integer key) { /* (1 <= key && key <= t->sizearray) */ - if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) - return &t->array[key-1]; + if (l_castS2U(key) - 1 < t->sizearray) + return &t->array[key - 1]; else { - lua_Number nk = cast_num(key); - Node *n = hashnum(t, nk); - do { /* check whether `key' is somewhere in the chain */ - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) + Node *n = hashint(t, key); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) return gval(n); /* that's it */ - else n = gnext(n); - } while (n); + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; + } + } return luaO_nilobject; } } @@ -463,15 +517,50 @@ const TValue *luaH_getint (Table *t, int key) { /* ** search function for short strings */ -const TValue *luaH_getstr (Table *t, TString *key) { +const TValue *luaH_getshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); - lua_assert(key->tsv.tt == LUA_TSHRSTR); - do { /* check whether `key' is somewhere in the chain */ - if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key)) + lua_assert(key->tt == LUA_TSHRSTR); + for (;;) { /* check whether 'key' is somewhere in the chain */ + const TValue *k = gkey(n); + if (ttisshrstring(k) && eqshrstr(tsvalue(k), key)) return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; + 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); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (luaV_rawequalobj(gkey(n), key)) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) + return luaO_nilobject; /* not found */ + n += nx; + } + } +} + + +const TValue *luaH_getstr (Table *t, TString *key) { + if (key->tt == LUA_TSHRSTR) + return luaH_getshortstr(t, key); + else { /* for long strings, use generic case */ + TValue ko; + setsvalue(cast(lua_State *, NULL), &ko, key); + return getgeneric(t, &ko); + } } @@ -480,25 +569,17 @@ const TValue *luaH_getstr (Table *t, TString *key) { */ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); + case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); + case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); case LUA_TNIL: return luaO_nilobject; - case LUA_TNUMBER: { - int k; - lua_Number n = nvalue(key); - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) /* index is int? */ + case LUA_TNUMFLT: { + lua_Integer k; + if (luaV_tointeger(key, &k, 0)) /* index is int? */ return luaH_getint(t, k); /* use specialized version */ - /* else go through */ - } - default: { - Node *n = mainposition(t, key); - do { /* check whether `key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; - } + /* else... */ + } /* FALLTHROUGH */ + default: + return getgeneric(t, key); } } @@ -515,14 +596,14 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { } -void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { +void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { const TValue *p = luaH_getint(t, key); TValue *cell; if (p != luaO_nilobject) cell = cast(TValue *, p); else { TValue k; - setnvalue(&k, cast_num(key)); + setivalue(&k, key); cell = luaH_newkey(L, t, &k); } setobj2t(L, cell, value); @@ -532,16 +613,16 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { static int unbound_search (Table *t, unsigned int j) { unsigned int i = j; /* i is zero or a present index */ j++; - /* find `i' and `j' such that i is present and j is not */ + /* find 'i' and 'j' such that i is present and j is not */ while (!ttisnil(luaH_getint(t, j))) { i = j; - j *= 2; - if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ + if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ /* table was built with bad purposes: resort to linear search */ i = 1; while (!ttisnil(luaH_getint(t, i))) i++; return i - 1; } + j *= 2; } /* now do a binary search between them */ while (j - i > 1) { @@ -554,7 +635,7 @@ static int unbound_search (Table *t, unsigned int j) { /* -** Try to find a boundary in table `t'. A `boundary' is an integer index +** Try to find a boundary in table 't'. A 'boundary' is an integer index ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). */ int luaH_getn (Table *t) { @@ -570,7 +651,7 @@ int luaH_getn (Table *t) { return i; } /* else must find a boundary in hash part */ - else if (isdummy(t->node)) /* hash part is empty? */ + else if (isdummy(t)) /* hash part is empty? */ return j; /* that is easy... */ else return unbound_search(t, j); } @@ -583,6 +664,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainposition(t, key); } -int luaH_isdummy (Node *n) { return isdummy(n); } +int luaH_isdummy (const Table *t) { return isdummy(t); } #endif diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index d69449b2..6da9024f 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ +** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -11,26 +11,47 @@ #define gnode(t,i) (&(t)->node[i]) -#define gkey(n) (&(n)->i_key.tvk) #define gval(n) (&(n)->i_val) #define gnext(n) ((n)->i_key.nk.next) + +/* 'const' to avoid wrong writings that can mess up field 'next' */ +#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) + +/* +** writable version of 'gkey'; allows updates to individual fields, +** but not to the whole (which has incompatible type) +*/ +#define wgkey(n) (&(n)->i_key.nk) + #define invalidateTMcache(t) ((t)->flags = 0) + +/* true when 't' is using 'dummynode' as its hash part */ +#define isdummy(t) ((t)->lastfree == NULL) + + +/* allocated size for hash nodes */ +#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) + + /* returns the key, given the value of a table entry */ #define keyfromval(v) \ (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) -LUAI_FUNC const TValue *luaH_getint (Table *t, int key); -LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); +LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, + TValue *value); +LUAI_FUNC const TValue *luaH_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 Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); +LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, + unsigned int nhsize); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC int luaH_getn (Table *t); @@ -38,7 +59,7 @@ LUAI_FUNC int luaH_getn (Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (Node *n); +LUAI_FUNC int luaH_isdummy (const Table *t); #endif diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index 6001224e..98b2f871 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,24 +1,62 @@ /* -** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ - -#include - #define ltablib_c #define LUA_LIB +#include "lprefix.h" + + +#include +#include +#include + #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) +/* +** Operations that an object must define to mimic a table +** (some functions only need some of them) +*/ +#define TAB_R 1 /* read */ +#define TAB_W 2 /* write */ +#define TAB_L 4 /* length */ +#define TAB_RW (TAB_R | TAB_W) /* read/write */ +#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) + + +static int checkfield (lua_State *L, const char *key, int n) { + lua_pushstring(L, key); + return (lua_rawget(L, -n) != LUA_TNIL); +} + + +/* +** Check that 'arg' either is a table or can behave like one (that is, +** has a metatable with the required metamethods) +*/ +static void checktab (lua_State *L, int arg, int what) { + if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ + int n = 1; /* number of elements to pop */ + if (lua_getmetatable(L, arg) && /* must have metatable */ + (!(what & TAB_R) || checkfield(L, "__index", ++n)) && + (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && + (!(what & TAB_L) || checkfield(L, "__len", ++n))) { + lua_pop(L, n); /* pop metatable and tested metamethods */ + } + else + luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ + } +} + #if defined(LUA_COMPAT_MAXN) static int maxn (lua_State *L) { @@ -39,65 +77,102 @@ static int maxn (lua_State *L) { static int tinsert (lua_State *L) { - int e = aux_getn(L, 1) + 1; /* first empty element */ - int pos; /* where to insert new element */ + lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ + lua_Integer pos; /* where to insert new element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ break; } case 3: { - int i; - pos = luaL_checkint(L, 2); /* 2nd argument is the position */ + lua_Integer i; + pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ - lua_rawgeti(L, 1, i-1); - lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ + lua_geti(L, 1, i - 1); + lua_seti(L, 1, i); /* t[i] = t[i - 1] */ } break; } default: { - return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); + return luaL_error(L, "wrong number of arguments to 'insert'"); } } - lua_rawseti(L, 1, pos); /* t[pos] = v */ + lua_seti(L, 1, pos); /* t[pos] = v */ return 0; } static int tremove (lua_State *L) { - int size = aux_getn(L, 1); - int pos = luaL_optint(L, 2, size); + 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"); - lua_rawgeti(L, 1, pos); /* result = t[pos] */ + lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { - lua_rawgeti(L, 1, pos+1); - lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ + lua_geti(L, 1, pos + 1); + lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } lua_pushnil(L); - lua_rawseti(L, 1, pos); /* t[pos] = nil */ + lua_seti(L, 1, pos); /* t[pos] = nil */ return 1; } -static void addfield (lua_State *L, luaL_Buffer *b, int i) { - lua_rawgeti(L, 1, i); +/* +** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever +** possible, copy in increasing order, which is better for rehashing. +** "possible" means destination after original range, or smaller +** than origin, or copying to another table. +*/ +static int tmove (lua_State *L) { + lua_Integer f = luaL_checkinteger(L, 2); + lua_Integer e = luaL_checkinteger(L, 3); + lua_Integer t = luaL_checkinteger(L, 4); + int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ + checktab(L, 1, TAB_R); + checktab(L, tt, TAB_W); + if (e >= f) { /* otherwise, nothing to move */ + lua_Integer n, i; + luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, + "too many elements to move"); + n = e - f + 1; /* number of elements to move */ + luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, + "destination wrap around"); + if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { + for (i = 0; i < n; i++) { + lua_geti(L, 1, f + i); + lua_seti(L, tt, t + i); + } + } + else { + for (i = n - 1; i >= 0; i--) { + lua_geti(L, 1, f + i); + lua_seti(L, tt, t + i); + } + } + } + lua_pushvalue(L, tt); /* return destination table */ + return 1; +} + + +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 " - LUA_QL("concat"), luaL_typename(L, -1), i); + luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", + luaL_typename(L, -1), i); luaL_addvalue(b); } static int tconcat (lua_State *L) { luaL_Buffer b; + lua_Integer last = aux_getn(L, 1, TAB_R); size_t lsep; - int i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 3, 1); - last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); + lua_Integer i = luaL_optinteger(L, 3, 1); + last = luaL_optinteger(L, 4, last); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); @@ -117,35 +192,31 @@ static int tconcat (lua_State *L) { */ static int pack (lua_State *L) { + int i; int n = lua_gettop(L); /* number of elements to pack */ lua_createtable(L, n, 1); /* create result table */ + lua_insert(L, 1); /* put it at index 1 */ + for (i = n; i >= 1; i--) /* assign elements */ + lua_seti(L, 1, i); lua_pushinteger(L, n); - lua_setfield(L, -2, "n"); /* t.n = number of elements */ - if (n > 0) { /* at least one element? */ - int i; - lua_pushvalue(L, 1); - lua_rawseti(L, -2, 1); /* insert first element */ - lua_replace(L, 1); /* move table into index 1 */ - for (i = n; i >= 2; i--) /* assign other elements */ - lua_rawseti(L, 1, i); - } + lua_setfield(L, 1, "n"); /* t.n = number of elements */ return 1; /* return table */ } static int unpack (lua_State *L) { - int i, e, n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); + lua_Unsigned n; + lua_Integer i = luaL_optinteger(L, 2, 1); + lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ - n = e - i + 1; /* number of elements */ - if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ + n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ + if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; + for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ + lua_geti(L, 1, i); + } + lua_geti(L, 1, e); /* push last element */ + return (int)n; } /* }====================================================== */ @@ -155,102 +226,197 @@ static int unpack (lua_State *L) { /* ** {====================================================== ** Quicksort -** (based on `Algorithms in MODULA-3', Robert Sedgewick; +** (based on 'Algorithms in MODULA-3', Robert Sedgewick; ** Addison-Wesley, 1993.) ** ======================================================= */ -static void set2 (lua_State *L, int i, int j) { - lua_rawseti(L, 1, i); - lua_rawseti(L, 1, j); +/* type for array indices */ +typedef unsigned int IdxT; + + +/* +** Produce a "random" 'unsigned int' to randomize pivot choice. This +** macro is used only when 'sort' detects a big imbalance in the result +** of a partition. (If you don't want/need this "randomness", ~0 is a +** good choice.) +*/ +#if !defined(l_randomizePivot) /* { */ + +#include + +/* size of 'e' measured in number of 'unsigned int's */ +#define sof(e) (sizeof(e) / sizeof(unsigned int)) + +/* +** Use 'time' and 'clock' as sources of "randomness". Because we don't +** know the types 'clock_t' and 'time_t', we cannot cast them to +** anything without risking overflows. A safe way to use their values +** is to copy them to an array of a known type and use the array values. +*/ +static unsigned int l_randomizePivot (void) { + clock_t c = clock(); + time_t t = time(NULL); + unsigned int buff[sof(c) + sof(t)]; + unsigned int i, rnd = 0; + memcpy(buff, &c, sof(c) * sizeof(unsigned int)); + memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); + for (i = 0; i < sof(buff); i++) + rnd += buff[i]; + return rnd; } +#endif /* } */ + + +/* arrays larger than 'RANLIMIT' may use randomized pivots */ +#define RANLIMIT 100u + + +static void set2 (lua_State *L, IdxT i, IdxT j) { + lua_seti(L, 1, i); + lua_seti(L, 1, j); +} + + +/* +** Return true iff value at stack index 'a' is less than the value at +** index 'b' (according to the order of the sort). +*/ static int sort_comp (lua_State *L, int a, int b) { - if (!lua_isnil(L, 2)) { /* function? */ + if (lua_isnil(L, 2)) /* no function? */ + return lua_compare(L, a, b, LUA_OPLT); /* a < b */ + else { /* function */ int res; - lua_pushvalue(L, 2); + lua_pushvalue(L, 2); /* push function */ lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ - lua_call(L, 2, 1); - res = lua_toboolean(L, -1); - lua_pop(L, 1); + lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ + lua_call(L, 2, 1); /* call function */ + res = lua_toboolean(L, -1); /* get result */ + lua_pop(L, 1); /* pop result */ return res; } - else /* a < b? */ - return lua_compare(L, a, b, LUA_OPLT); } -static void auxsort (lua_State *L, int l, int u) { - while (l < u) { /* for tail recursion */ - int i, j; - /* sort elements a[l], a[(l+u)/2] and a[u] */ - lua_rawgeti(L, 1, l); - lua_rawgeti(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, l, u); /* swap a[l] - a[u] */ + +/* +** Does the partition: Pivot P is at the top of the stack. +** precondition: a[lo] <= P == a[up-1] <= a[up], +** so it only needs to do the partition from lo + 1 to up - 2. +** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] +** returns 'i'. +*/ +static IdxT partition (lua_State *L, IdxT lo, IdxT up) { + IdxT i = lo; /* will be incremented before first use */ + IdxT j = up - 1; /* will be decremented before first use */ + /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ + for (;;) { + /* next loop: repeat ++i while a[i] < P */ + while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ + luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[i] */ + } + /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ + /* next loop: repeat --j while P < a[j] */ + while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { + if (j < i) /* j < i but a[j] > P ?? */ + luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[j] */ + } + /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ + if (j < i) { /* no elements out of place? */ + /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ + lua_pop(L, 1); /* pop a[j] */ + /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ + set2(L, up - 1, i); + return i; + } + /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ + set2(L, i, j); + } +} + + +/* +** Choose an element in the middle (2nd-3th quarters) of [lo,up] +** "randomized" by 'rnd' +*/ +static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { + IdxT r4 = (up - lo) / 4; /* range/4 */ + IdxT p = rnd % (r4 * 2) + (lo + r4); + lua_assert(lo + r4 <= p && p <= up - r4); + return p; +} + + +/* +** QuickSort algorithm (recursive function) +*/ +static void auxsort (lua_State *L, IdxT lo, IdxT up, + unsigned int rnd) { + while (lo < up) { /* loop for tail recursion */ + IdxT p; /* Pivot index */ + IdxT n; /* to be used later */ + /* sort elements 'lo', 'p', and 'up' */ + lua_geti(L, 1, lo); + lua_geti(L, 1, up); + if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ + set2(L, lo, up); /* swap a[lo] - a[up] */ else - lua_pop(L, 2); - if (u-l == 1) break; /* only 2 elements */ - i = (l+u)/2; - lua_rawgeti(L, 1, i); - lua_rawgeti(L, 1, l); - if (sort_comp(L, -2, -1)) /* a[i]= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>=u) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j<=l) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[j] */ - } - if (j n) /* partition too imbalanced? */ + rnd = l_randomizePivot(); /* try a new randomization */ + } /* tail call auxsort(L, lo, up, rnd) */ } + static int sort (lua_State *L) { - int n = aux_getn(L, 1); - luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ - if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ - luaL_checktype(L, 2, LUA_TFUNCTION); - lua_settop(L, 2); /* make sure there is two arguments */ - auxsort(L, 1, n); + lua_Integer n = aux_getn(L, 1, TAB_RW); + if (n > 1) { /* non-trivial interval? */ + luaL_argcheck(L, n < INT_MAX, 1, "array too big"); + if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ + luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ + lua_settop(L, 2); /* make sure there are two arguments */ + auxsort(L, 1, (IdxT)n, 0); + } return 0; } @@ -266,6 +432,7 @@ static const luaL_Reg tab_funcs[] = { {"pack", pack}, {"unpack", unpack}, {"remove", tremove}, + {"move", tmove}, {"sort", sort}, {NULL, NULL} }; diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 69b4ed77..14e52578 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,22 +1,27 @@ /* -** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ - -#include - #define ltm_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" +#include "ldebug.h" +#include "ldo.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" +#include "lvm.h" static const char udatatypename[] = "userdata"; @@ -25,7 +30,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", - "proto", "upval" /* these last two cases are used for tests only */ + "proto" /* this last case is used for tests only */ }; @@ -33,14 +38,16 @@ void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", "__gc", "__mode", "__len", "__eq", - "__add", "__sub", "__mul", "__div", "__mod", - "__pow", "__unm", "__lt", "__le", + "__add", "__sub", "__mul", "__mod", "__pow", + "__div", "__idiv", + "__band", "__bor", "__bxor", "__shl", "__shr", + "__unm", "__bnot", "__lt", "__le", "__concat", "__call" }; int i; for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaS_fix(G(L)->tmname[i]); /* never collect these names */ + luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ } } @@ -50,7 +57,7 @@ void luaT_init (lua_State *L) { ** tag methods */ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getstr(events, ename); + const TValue *tm = luaH_getshortstr(events, ename); lua_assert(event <= TM_EQ); if (ttisnil(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<metatable; break; @@ -70,8 +77,89 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { mt = uvalue(o)->metatable; break; default: - mt = G(L)->mt[ttypenv(o)]; + mt = G(L)->mt[ttnov(o)]; } - return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); + return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); +} + + +/* +** Return the name of the type of an object. For tables and userdata +** with metatable, use their '__name' metafield, if present. +*/ +const char *luaT_objtypename (lua_State *L, const TValue *o) { + Table *mt; + if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || + (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { + const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); + if (ttisstring(name)) /* is '__name' a string? */ + return getstr(tsvalue(name)); /* use it as type name */ + } + return ttypename(ttnov(o)); /* else use standard type name */ +} + + +void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, TValue *p3, int hasres) { + ptrdiff_t result = savestack(L, p3); + 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); + 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); + } +} + + +int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ + if (ttisnil(tm)) + tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ + if (ttisnil(tm)) return 0; + luaT_callTM(L, tm, p1, p2, res, 1); + return 1; +} + + +void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + if (!luaT_callbinTM(L, p1, p2, res, event)) { + switch (event) { + case TM_CONCAT: + luaG_concaterror(L, p1, p2); + /* 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)) + luaG_tointerror(L, p1, p2); + else + luaG_opinterror(L, p1, p2, "perform bitwise operation on"); + } + /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ + default: + luaG_opinterror(L, p1, p2, "perform arithmetic on"); + } + } +} + + +int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, + TMS event) { + if (!luaT_callbinTM(L, p1, p2, L->top, event)) + return -1; /* no metamethod */ + else + return !l_isfalse(L->top); } diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 7f89c841..63db7269 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -13,7 +13,7 @@ /* * WARNING: if you change the order of this enumeration, -* grep "ORDER TM" +* grep "ORDER TM" and "ORDER OP" */ typedef enum { TM_INDEX, @@ -21,14 +21,21 @@ typedef enum { TM_GC, TM_MODE, TM_LEN, - TM_EQ, /* last tag method with `fast' access */ + TM_EQ, /* last tag method with fast access */ TM_ADD, TM_SUB, TM_MUL, - TM_DIV, TM_MOD, TM_POW, + TM_DIV, + TM_IDIV, + TM_BAND, + TM_BOR, + TM_BXOR, + TM_SHL, + TM_SHR, TM_UNM, + TM_BNOT, TM_LT, TM_LE, TM_CONCAT, @@ -44,14 +51,26 @@ typedef enum { #define fasttm(l,et,e) gfasttm(G(l), et, e) #define ttypename(x) luaT_typenames_[(x) + 1] -#define objtypename(x) ttypename(ttypenv(x)) LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; +LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); + LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event); LUAI_FUNC void luaT_init (lua_State *L); +LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, TValue *p3, int hasres); +LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event); +LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event); +LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, + const TValue *p2, TMS event); + + + #endif diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 4345e554..febd9876 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,21 +1,25 @@ /* -** $Id: lua.c,v 1.206.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lua.c,v 1.230 2017/01/12 17:14:26 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ +#define lua_c + +#include "lprefix.h" + #include #include #include #include -#define lua_c - #include "lua.h" #include "lauxlib.h" #include "lualib.h" +#include "lstring.h" + #if !defined(LUA_PROMPT) @@ -31,28 +35,39 @@ #define LUA_MAXINPUT 512 #endif -#if !defined(LUA_INIT) -#define LUA_INIT "LUA_INIT" +#if !defined(LUA_INIT_VAR) +#define LUA_INIT_VAR "LUA_INIT" #endif -#define LUA_INITVERSION \ - LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR +#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX /* ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that ** is, whether we're running lua interactively). */ -#if defined(LUA_USE_ISATTY) +#if !defined(lua_stdin_is_tty) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + #include #define lua_stdin_is_tty() isatty(0) -#elif defined(LUA_WIN) + +#elif defined(LUA_USE_WINDOWS) /* }{ */ + #include -#include +#include + #define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#else + +#else /* }{ */ + +/* ISO C definition */ #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ -#endif + +#endif /* } */ + +#endif /* } */ /* @@ -61,26 +76,27 @@ ** lua_saveline defines how to "save" a read line in a "history". ** lua_freeline defines how to free a line read by lua_readline. */ -#if defined(LUA_USE_READLINE) +#if !defined(lua_readline) /* { */ + +#if defined(LUA_USE_READLINE) /* { */ -#include #include #include #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,idx) \ - if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ - add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_freeline(L,b) ((void)L, free(b)) -#elif !defined(lua_readline) +#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,idx) { (void)L; (void)idx; } +#define lua_saveline(L,line) { (void)L; (void)line; } #define lua_freeline(L,b) { (void)L; (void)b; } -#endif +#endif /* } */ + +#endif /* } */ @@ -90,33 +106,40 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; - +/* +** Hook set by signal function to stop the interpreter. +*/ static void lstop (lua_State *L, lua_Debug *ar) { (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); + lua_sethook(L, NULL, 0, 0); /* reset hook */ luaL_error(L, "interrupted!"); } +/* +** Function to be called at a C signal. Because a C signal cannot +** just change a Lua state (as there is no proper synchronization), +** this function only sets a hook that, when called, will stop the +** interpreter. +*/ static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ + signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); } static void print_usage (const char *badoption) { - luai_writestringerror("%s: ", progname); + lua_writestringerror("%s: ", progname); if (badoption[1] == 'e' || badoption[1] == 'l') - luai_writestringerror("'%s' needs argument\n", badoption); + lua_writestringerror("'%s' needs argument\n", badoption); else - luai_writestringerror("unrecognized option '%s'\n", badoption); - luai_writestringerror( + lua_writestringerror("unrecognized option '%s'\n", badoption); + lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" - " -e stat execute string " LUA_QL("stat") "\n" - " -i enter interactive mode after executing " LUA_QL("script") "\n" - " -l name require library " LUA_QL("name") "\n" + " -e stat execute string 'stat'\n" + " -i enter interactive mode after executing 'script'\n" + " -l name require library 'name'\n" " -v show version information\n" " -E ignore environment variables\n" " -- stop handling options\n" @@ -126,101 +149,114 @@ static void print_usage (const char *badoption) { } +/* +** Prints an error message, adding the program name in front of it +** (if present) +*/ static void l_message (const char *pname, const char *msg) { - if (pname) luai_writestringerror("%s: ", pname); - luai_writestringerror("%s\n", msg); + if (pname) lua_writestringerror("%s: ", pname); + lua_writestringerror("%s\n", msg); } +/* +** Check whether 'status' is not OK and, if so, prints the error +** message on the top of the stack. It assumes that the error object +** is a string, as it was either generated by Lua or by 'msghandler'. +*/ static int report (lua_State *L, int status) { - if (status != LUA_OK && !lua_isnil(L, -1)) { + if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; l_message(progname, msg); - lua_pop(L, 1); - /* force a complete garbage collection in case of errors */ - lua_gc(L, LUA_GCCOLLECT, 0); + lua_pop(L, 1); /* remove message */ } return status; } -/* the next function is called unprotected, so it must avoid errors */ -static void finalreport (lua_State *L, int status) { - if (status != LUA_OK) { - const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) - : NULL; - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } -} - - -static int traceback (lua_State *L) { +/* +** Message handler used to run all chunks +*/ +static int msghandler (lua_State *L) { const char *msg = lua_tostring(L, 1); - if (msg) - luaL_traceback(L, L, msg, 1); - else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ - if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ - lua_pushliteral(L, "(no error message)"); + if (msg == NULL) { /* is error object not a string? */ + if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ + lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ + return 1; /* that is the message */ + else + msg = lua_pushfstring(L, "(error object is a %s value)", + luaL_typename(L, 1)); } - return 1; + luaL_traceback(L, L, msg, 1); /* append a standard traceback */ + return 1; /* return the traceback */ } +/* +** Interface to 'lua_pcall', which sets appropriate message function +** and C-signal handler. Used to run all chunks. +*/ static int docall (lua_State *L, int narg, int nres) { int status; int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ + lua_pushcfunction(L, msghandler); /* push message handler */ + lua_insert(L, base); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); + signal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); - lua_remove(L, base); /* remove traceback function */ + signal(SIGINT, SIG_DFL); /* reset C-signal handler */ + lua_remove(L, base); /* remove message handler from the stack */ return status; } static void print_version (void) { - luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); - luai_writeline(); + lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); + lua_writeline(); } -static int getargs (lua_State *L, char **argv, int n) { - int narg; - int i; - int argc = 0; - while (argv[argc]) argc++; /* count total number of arguments */ - narg = argc - (n + 1); /* number of arguments to the script */ - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i=n+1; i < argc; i++) +/* +** Create the 'arg' table, which stores all arguments from the +** command line ('argv'). It should be aligned so that, at index 0, +** it has 'argv[script]', which is the script name. The arguments +** to the script (everything after 'script') go to positive indices; +** other arguments (before the script name) go to negative indices. +** If there is no script name, assume interpreter's name as base. +*/ +static void createargtable (lua_State *L, char **argv, int argc, int script) { + int i, narg; + if (script == argc) script = 0; /* no script name? */ + narg = argc - (script + 1); /* number of positive indices */ + lua_createtable(L, narg, script + 1); + for (i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); - lua_createtable(L, narg, n + 1); - for (i=0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); + lua_rawseti(L, -2, i - script); } - return narg; + lua_setglobal(L, "arg"); +} + + +static int dochunk (lua_State *L, int status) { + if (status == LUA_OK) status = docall(L, 0, 0); + return report(L, status); } static int dofile (lua_State *L, const char *name) { - int status = luaL_loadfile(L, name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); + return dochunk(L, luaL_loadfile(L, name)); } static int dostring (lua_State *L, const char *s, const char *name) { - int status = luaL_loadbuffer(L, s, strlen(s), name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); + return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); } +/* +** Calls 'require(name)' and stores the result in a global variable +** with the given name. +*/ static int dolibrary (lua_State *L, const char *name) { int status; lua_getglobal(L, "require"); @@ -232,6 +268,9 @@ static int dolibrary (lua_State *L, const char *name) { } +/* +** Returns the string to be used as a prompt by the interpreter. +*/ static const char *get_prompt (lua_State *L, int firstline) { const char *p; lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); @@ -244,6 +283,12 @@ static const char *get_prompt (lua_State *L, int firstline) { #define EOFMARK "" #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) + +/* +** Check whether 'status' signals a syntax error and the error +** message at the top of the stack ends with the above mark for +** incomplete statements. +*/ static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { size_t lmsg; @@ -257,174 +302,242 @@ static int incomplete (lua_State *L, int status) { } +/* +** Prompt the user, read a line, and push it into the Lua stack. +*/ static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; char *b = buffer; size_t l; const char *prmt = get_prompt(L, firstline); int readstatus = lua_readline(L, b, prmt); - lua_pop(L, 1); /* remove result from 'get_prompt' */ if (readstatus == 0) - return 0; /* no input */ + return 0; /* no input (prompt will be popped by caller) */ + lua_pop(L, 1); /* remove prompt */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[l-1] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* first line starts with `=' ? */ - lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ + b[--l] = '\0'; /* remove it */ + if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ + lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ else - lua_pushstring(L, b); + lua_pushlstring(L, b, l); lua_freeline(L, b); return 1; } +/* +** Try to compile line on the stack as 'return ;'; on return, stack +** has either compiled chunk or original line (if compilation failed). +*/ +static int addreturn (lua_State *L) { + const char *line = lua_tostring(L, -1); /* original line */ + const char *retline = lua_pushfstring(L, "return %s;", line); + int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); + if (status == LUA_OK) { + lua_remove(L, -2); /* remove modified line */ + if (line[0] != '\0') /* non empty? */ + lua_saveline(L, line); /* keep history */ + } + else + lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ + return status; +} + + +/* +** Read multiple lines until a complete Lua statement +*/ +static int multiline (lua_State *L) { + for (;;) { /* repeat until gets a complete statement */ + size_t len; + const char *line = lua_tolstring(L, 1, &len); /* get what it has */ + int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ + if (!incomplete(L, status) || !pushline(L, 0)) { + lua_saveline(L, line); /* keep history */ + return status; /* cannot or should not try to add continuation line */ + } + lua_pushliteral(L, "\n"); /* add newline... */ + lua_insert(L, -2); /* ...between the two lines */ + lua_concat(L, 3); /* join them */ + } +} + + +/* +** Read a line and try to load (compile) it first as an expression (by +** adding "return " in front of it) and second as a statement. Return +** the final status of load/call with the resulting function (if any) +** in the top of the stack. +*/ static int loadline (lua_State *L) { int status; lua_settop(L, 0); if (!pushline(L, 1)) return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - size_t l; - const char *line = lua_tolstring(L, 1, &l); - status = luaL_loadbuffer(L, line, l, "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_saveline(L, 1); - lua_remove(L, 1); /* remove line */ + if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ + status = multiline(L); /* try as command, maybe with continuation lines */ + lua_remove(L, 1); /* remove line from the stack */ + lua_assert(lua_gettop(L) == 1); return status; } -static void dotty (lua_State *L) { +/* +** Prints (calling the Lua 'print' function) any values on the stack +*/ +static void l_print (lua_State *L) { + int n = lua_gettop(L); + if (n > 0) { /* any result to be printed? */ + luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); + lua_getglobal(L, "print"); + lua_insert(L, 1); + if (lua_pcall(L, n, 0, 0) != LUA_OK) + l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", + lua_tostring(L, -1))); + } +} + + +/* +** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and +** print any results. +*/ +static void doREPL (lua_State *L) { int status; const char *oldprogname = progname; - progname = NULL; + progname = NULL; /* no 'progname' on errors in interactive mode */ while ((status = loadline(L)) != -1) { - if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); - report(L, status); - if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ - luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) - l_message(progname, lua_pushfstring(L, - "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } + if (status == LUA_OK) + status = docall(L, 0, LUA_MULTRET); + if (status == LUA_OK) l_print(L); + else report(L, status); } lua_settop(L, 0); /* clear stack */ - luai_writeline(); + lua_writeline(); progname = oldprogname; } -static int handle_script (lua_State *L, char **argv, int n) { +/* +** Push on the stack the contents of table 'arg' from 1 to #arg +*/ +static int pushargs (lua_State *L) { + int i, n; + if (lua_getglobal(L, "arg") != LUA_TTABLE) + luaL_error(L, "'arg' is not a table"); + n = (int)luaL_len(L, -1); + luaL_checkstack(L, n + 3, "too many arguments to script"); + for (i = 1; i <= n; i++) + lua_rawgeti(L, -i, i); + lua_remove(L, -i); /* remove table from the stack */ + return n; +} + + +static int handle_script (lua_State *L, char **argv) { int status; - const char *fname; - int narg = getargs(L, argv, n); /* collect arguments */ - lua_setglobal(L, "arg"); - fname = argv[n]; - if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) + const char *fname = argv[0]; + if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) fname = NULL; /* stdin */ status = luaL_loadfile(L, fname); - lua_insert(L, -(narg+1)); - if (status == LUA_OK) - status = docall(L, narg, LUA_MULTRET); - else - lua_pop(L, narg); + if (status == LUA_OK) { + int n = pushargs(L); /* push arguments to script */ + status = docall(L, n, LUA_MULTRET); + } return report(L, status); } -/* check that argument has no extra characters at the end */ -#define noextrachars(x) {if ((x)[2] != '\0') return -1;} +/* bits of various argument indicators in 'args' */ +#define has_error 1 /* bad option */ +#define has_i 2 /* -i */ +#define has_v 4 /* -v */ +#define has_e 8 /* -e */ +#define has_E 16 /* -E */ -/* indices of various argument indicators in array args */ -#define has_i 0 /* -i */ -#define has_v 1 /* -v */ -#define has_e 2 /* -e */ -#define has_E 3 /* -E */ - -#define num_has 4 /* number of 'has_*' */ - - -static int collectargs (char **argv, int *args) { +/* +** Traverses all arguments from 'argv', returning a mask with those +** needed before running any Lua code (or an error code if it finds +** any invalid argument). 'first' returns the first not-handled argument +** (either the script name or a bad argument in case of error). +*/ +static int collectargs (char **argv, int *first) { + int args = 0; int i; for (i = 1; argv[i] != NULL; i++) { + *first = i; if (argv[i][0] != '-') /* not an option? */ - return i; - switch (argv[i][1]) { /* option */ - case '-': - noextrachars(argv[i]); - return (argv[i+1] != NULL ? i+1 : 0); - case '\0': - return i; + return args; /* stop handling options */ + switch (argv[i][1]) { /* else check option */ + case '-': /* '--' */ + if (argv[i][2] != '\0') /* extra characters after '--'? */ + return has_error; /* invalid option */ + *first = i + 1; + return args; + case '\0': /* '-' */ + return args; /* script "name" is '-' */ case 'E': - args[has_E] = 1; + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_E; break; case 'i': - noextrachars(argv[i]); - args[has_i] = 1; /* go through */ + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': - noextrachars(argv[i]); - args[has_v] = 1; + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_v; break; case 'e': - args[has_e] = 1; /* go through */ + args |= has_e; /* 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 -(i - 1); /* no next argument or it is another option */ + return has_error; /* no next argument or it is another option */ } break; - default: /* invalid option; return its index... */ - return -i; /* ...as a negative value */ + default: /* invalid option */ + return has_error; } } - return 0; + *first = i; /* no script name */ + return args; } +/* +** Processes options 'e' and 'l', which involve running Lua code. +** Returns 0 if some code raises an error. +*/ static int runargs (lua_State *L, char **argv, int n) { int i; for (i = 1; i < n; i++) { - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { /* option */ - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != LUA_OK) - return 0; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename) != LUA_OK) - return 0; /* stop if file fails */ - break; - } - default: break; + int 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_INITVERSION; + const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); if (init == NULL) { - name = "=" LUA_INIT; + name = "=" LUA_INIT_VAR; init = getenv(name + 1); /* try alternative name */ } if (init == NULL) return LUA_OK; @@ -435,40 +548,44 @@ static int handle_luainit (lua_State *L) { } +/* +** Main body of stand-alone interpreter (to be called in protected mode). +** Reads the options and handles them all. +*/ static int pmain (lua_State *L) { int argc = (int)lua_tointeger(L, 1); char **argv = (char **)lua_touserdata(L, 2); int script; - int args[num_has]; - args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; + int args = collectargs(argv, &script); + luaL_checkversion(L); /* check that interpreter has correct version */ if (argv[0] && argv[0][0]) progname = argv[0]; - script = collectargs(argv, args); - if (script < 0) { /* invalid arg? */ - print_usage(argv[-script]); + if (args == has_error) { /* bad arg? */ + print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; } - if (args[has_v]) print_version(); - if (args[has_E]) { /* option '-E'? */ + if (args & has_v) /* option '-v'? */ + print_version(); + if (args & has_E) { /* option '-E'? */ lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } - /* open standard libraries */ - luaL_checkversion(L); - lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ - luaL_openlibs(L); /* open libraries */ - lua_gc(L, LUA_GCRESTART, 0); - if (!args[has_E] && handle_luainit(L) != LUA_OK) - return 0; /* error running LUA_INIT */ - /* execute arguments -e and -l */ - if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; - /* execute main script (if there is one) */ - if (script && handle_script(L, argv, script) != LUA_OK) return 0; - if (args[has_i]) /* -i option? */ - dotty(L); - else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ - if (lua_stdin_is_tty()) { + luaL_openlibs(L); /* open standard libraries */ + createargtable(L, argv, argc, script); /* create table 'arg' */ + if (!(args & has_E)) { /* no option '-E'? */ + if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ + return 0; /* error running LUA_INIT */ + } + if (!runargs(L, argv, script)) /* execute arguments -e and -l */ + return 0; /* something failed */ + if (script < argc && /* execute main script (if there is one) */ + handle_script(L, argv + script) != LUA_OK) + return 0; + if (args & has_i) /* -i option? */ + doREPL(L); /* do read-eval-print loop */ + else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); - dotty(L); + doREPL(L); /* do read-eval-print loop */ } else dofile(L, NULL); /* executes stdin as a file */ } @@ -479,19 +596,21 @@ static int pmain (lua_State *L) { int main (int argc, char **argv) { int status, result; - lua_State *L = luaL_newstate(); /* create state */ + lua_State *L; + luaS_initshr(); /* init global short string table */ + L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } - /* call 'pmain' in protected mode */ - lua_pushcfunction(L, &pmain); + lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ - status = lua_pcall(L, 2, 1, 0); + status = lua_pcall(L, 2, 1, 0); /* do the call */ result = lua_toboolean(L, -1); /* get result */ - finalreport(L, status); + report(L, status); lua_close(L); + luaS_exitshr(); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 1fceb4a5..24a8886d 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.285.1.2 2013/11/11 12:09:16 roberto Exp $ +** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -17,27 +17,29 @@ #define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "2" -#define LUA_VERSION_NUM 502 -#define LUA_VERSION_RELEASE "3" +#define LUA_VERSION_MINOR "3" +#define LUA_VERSION_NUM 503 +#define LUA_VERSION_RELEASE "4" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2013 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2017 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" /* mark for precompiled code ('Lua') */ -#define LUA_SIGNATURE "\033Lua" +#define LUA_SIGNATURE "\x1bLua" /* option for multiple returns in 'lua_pcall' and 'lua_call' */ #define LUA_MULTRET (-1) /* -** pseudo-indices +** Pseudo-indices +** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty +** space after that to help overflow detection) */ -#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX +#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) @@ -53,22 +55,6 @@ typedef struct lua_State lua_State; -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - /* ** basic types @@ -109,6 +95,34 @@ typedef LUA_INTEGER lua_Integer; /* unsigned integer type */ typedef LUA_UNSIGNED lua_Unsigned; +/* type for continuation-function contexts */ +typedef LUA_KCONTEXT lua_KContext; + + +/* +** Type for C functions registered with Lua +*/ +typedef int (*lua_CFunction) (lua_State *L); + +/* +** Type for continuation functions +*/ +typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); + + +/* +** Type for functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); + + +/* +** Type for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + /* @@ -145,11 +159,9 @@ LUA_API int (lua_absindex) (lua_State *L, int idx); LUA_API int (lua_gettop) (lua_State *L); LUA_API void (lua_settop) (lua_State *L, int idx); LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API void (lua_rotate) (lua_State *L, int idx, int n); LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); +LUA_API int (lua_checkstack) (lua_State *L, int n); LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); @@ -161,13 +173,13 @@ LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); LUA_API int (lua_isnumber) (lua_State *L, int idx); LUA_API int (lua_isstring) (lua_State *L, int idx); LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isinteger) (lua_State *L, int idx); LUA_API int (lua_isuserdata) (lua_State *L, int idx); LUA_API int (lua_type) (lua_State *L, int idx); LUA_API const char *(lua_typename) (lua_State *L, int tp); LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum); LUA_API int (lua_toboolean) (lua_State *L, int idx); LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); LUA_API size_t (lua_rawlen) (lua_State *L, int idx); @@ -181,13 +193,20 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx); ** Comparison and arithmetic functions */ -#define LUA_OPADD 0 /* ORDER TM */ +#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ #define LUA_OPSUB 1 #define LUA_OPMUL 2 -#define LUA_OPDIV 3 -#define LUA_OPMOD 4 -#define LUA_OPPOW 5 -#define LUA_OPUNM 6 +#define LUA_OPMOD 3 +#define LUA_OPPOW 4 +#define LUA_OPDIV 5 +#define LUA_OPIDIV 6 +#define LUA_OPBAND 7 +#define LUA_OPBOR 8 +#define LUA_OPBXOR 9 +#define LUA_OPSHL 10 +#define LUA_OPSHR 11 +#define LUA_OPUNM 12 +#define LUA_OPBNOT 13 LUA_API void (lua_arith) (lua_State *L, int op); @@ -205,8 +224,7 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); -LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); @@ -220,26 +238,29 @@ LUA_API int (lua_pushthread) (lua_State *L); /* ** get functions (Lua -> stack) */ -LUA_API void (lua_getglobal) (lua_State *L, const char *var); -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); +LUA_API int (lua_getglobal) (lua_State *L, const char *name); +LUA_API int (lua_gettable) (lua_State *L, int idx); +LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawget) (lua_State *L, int idx); +LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); + LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getuservalue) (lua_State *L, int idx); +LUA_API int (lua_getuservalue) (lua_State *L, int idx); /* ** set functions (stack -> Lua) */ -LUA_API void (lua_setglobal) (lua_State *L, const char *var); +LUA_API void (lua_setglobal) (lua_State *L, const char *name); LUA_API void (lua_settable) (lua_State *L, int idx); LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_setmetatable) (lua_State *L, int objindex); LUA_API void (lua_setuservalue) (lua_State *L, int idx); @@ -248,32 +269,33 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); /* ** 'load' and 'call' functions (load and run Lua code) */ -LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k); +LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k); #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) -LUA_API int (lua_getctx) (lua_State *L, int *ctx); - LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k); + lua_KContext ctx, lua_KFunction k); #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, - const char *mode); + const char *chunkname, const char *mode); -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); LUA_API void (lua_clonefunction) (lua_State *L, const void *eL); + /* ** coroutine functions */ -LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, - lua_CFunction k); +LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k); +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); +LUA_API int (lua_status) (lua_State *L); +LUA_API int (lua_isyieldable) (lua_State *L); + #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); -LUA_API int (lua_status) (lua_State *L); + /* ** garbage-collection function and options @@ -287,10 +309,7 @@ LUA_API int (lua_status) (lua_State *L); #define LUA_GCSTEP 5 #define LUA_GCSETPAUSE 6 #define LUA_GCSETSTEPMUL 7 -#define LUA_GCSETMAJORINC 8 #define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 LUA_API int (lua_gc) (lua_State *L, int what, int data); @@ -306,20 +325,23 @@ LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); + LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); /* -** =============================================================== +** {============================================================== ** some useful macros ** =============================================================== */ -#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) -#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) -#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) +#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) + +#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) +#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) #define lua_pop(L,n) lua_settop(L, -(n)-1) @@ -338,15 +360,36 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) +#define lua_pushliteral(L, s) lua_pushstring(L, "" s) #define lua_pushglobaltable(L) \ - lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) + ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) +#define lua_insert(L,idx) lua_rotate(L, (idx), 1) + +#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) + +#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) + +/* }============================================================== */ + + +/* +** {============================================================== +** compatibility macros for unsigned conversions +** =============================================================== +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) +#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) +#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) + +#endif +/* }============================================================== */ /* ** {====================================================================== @@ -391,7 +434,7 @@ LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2); -LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); +LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); @@ -417,9 +460,14 @@ 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-2013 Lua.org, PUC-Rio. +* Copyright (C) 1994-2017 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 20594048..86d47518 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,23 +1,27 @@ /* -** $Id: luac.c,v 1.69 2011/11/29 17:46:33 lhf Exp $ -** Lua compiler (saves bytecodes to files; also list bytecodes) +** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ +** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ +#define luac_c +#define LUA_CORE + +#include "lprefix.h" + +#include #include #include #include #include -#define luac_c -#define LUA_CORE - #include "lua.h" #include "lauxlib.h" #include "lobject.h" #include "lstate.h" #include "lundump.h" +#include "lstring.h" static void PrintFunction(const Proto* f, int full); #define luaU_print PrintFunction @@ -47,14 +51,14 @@ static void cannot(const char* what) static void usage(const char* message) { if (*message=='-') - fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); + fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); else fprintf(stderr,"%s: %s\n",progname,message); fprintf(stderr, "usage: %s [options] [filenames]\n" "Available options are:\n" " -l list (use -l -l for full listing)\n" - " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" + " -o name output to file 'name' (default is \"%s\")\n" " -p parse only\n" " -s strip debug information\n" " -v show version information\n" @@ -89,7 +93,7 @@ static int doargs(int argc, char* argv[]) { output=argv[++i]; if (output==NULL || *output==0 || (*output=='-' && output[1]!=0)) - usage(LUA_QL("-o") " needs argument"); + usage("'-o' needs argument"); if (IS("-")) output=NULL; } else if (IS("-p")) /* parse only */ @@ -192,6 +196,7 @@ int main(int argc, char* argv[]) int i=doargs(argc,argv); argc-=i; argv+=i; if (argc<=0) usage("no input files given"); + luaS_initshr(); L=luaL_newstate(); if (L==NULL) fatal("cannot create state: not enough memory"); lua_pushcfunction(L,&pmain); @@ -203,7 +208,7 @@ int main(int argc, char* argv[]) } /* -** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $ +** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ @@ -223,7 +228,7 @@ int main(int argc, char* argv[]) static void PrintString(const TString* ts) { const char* s=getstr(ts); - size_t i,n=ts->tsv.len; + size_t i,n=tsslen(ts); printf("%c",'"'); for (i=0; ik[i]; - switch (ttypenv(o)) + switch (ttype(o)) { case LUA_TNIL: printf("nil"); @@ -259,11 +264,19 @@ static void PrintConstant(const Proto* f, int i) case LUA_TBOOLEAN: printf(bvalue(o) ? "true" : "false"); break; - case LUA_TNUMBER: - printf(LUA_NUMBER_FMT,nvalue(o)); + case LUA_TNUMFLT: + { + char buff[100]; + sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); + printf("%s",buff); + if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); break; - case LUA_TSTRING: - PrintString(rawtsvalue(o)); + } + case LUA_TNUMINT: + printf(LUA_INTEGER_FMT,ivalue(o)); + break; + case LUA_TSHRSTR: case LUA_TLNGSTR: + PrintString(tsvalue(o)); break; default: /* cannot happen */ printf("? type=%d",ttype(o)); @@ -276,9 +289,8 @@ static void PrintConstant(const Proto* f, int i) static void PrintCode(const Proto* f) { - const SharedProto *sp = f->sp; - const Instruction* code=sp->code; - int pc,n=sp->sizecode; + const Instruction* code=f->sp->code; + int pc,n=f->sp->sizecode; for (pc=0; pclocvars[i].varname),sp->locvars[i].startpc+1,sp->locvars[i].endpc+1); } - n=sp->sizeupvalues; + n=f->sp->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; i #include /* -** ================================================================== +** =================================================================== ** Search for "@@" to find all configurable definitions. ** =================================================================== */ /* -@@ LUA_ANSI controls the use of non-ansi features. -** CHANGE it (define it) if you want Lua to avoid the use of any -** non-ansi feature or library. +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance compiling it with 32-bit numbers or +** restricting it to C89. +** ===================================================================== */ -#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__) -#define LUA_ANSI + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You +** can also define LUA_32BITS in the make file, but changing here you +** ensure that all software connected to Lua will be compiled with the +** same configuration. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ #endif -#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE) -#define LUA_WIN /* enable goodies for regular Windows platforms */ +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ #endif -#if defined(LUA_WIN) -#define LUA_DL_DLL -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#endif - - #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ #define LUA_USE_READLINE /* needs some extra libraries */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ #endif + #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* does not need -ldl */ +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ #define LUA_USE_READLINE /* needs an extra library: -lreadline */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ +#endif + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS #endif /* -@@ LUA_USE_POSIX includes all functionality listed as X/Open System -@* Interfaces Extension (XSI). -** CHANGE it (define it) if your system is XSI compatible. +@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. */ -#if defined(LUA_USE_POSIX) -#define LUA_USE_MKSTEMP -#define LUA_USE_ISATTY -#define LUA_USE_POPEN -#define LUA_USE_ULONGJMP -#define LUA_USE_GMTIME_R +/* avoid undefined shifts */ +#if ((INT_MAX >> 15) >> 15) >= 1 +#define LUAI_BITSINT 32 +#else +/* 'int' always must have at least 16 bits */ +#define LUAI_BITSINT 16 #endif +/* +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. +** Lua should work fine with any mix of these options (if supported +** by your C compiler). The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 + +#if defined(LUA_32BITS) /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#define LUA_INT_TYPE LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_TYPE LUA_INT_LONG +#endif +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT + +#elif defined(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 /* } */ + + +/* +** default configuration for 64-bit Lua ('long long' and 'double') +*/ +#if !defined(LUA_INT_TYPE) +#define LUA_INT_TYPE LUA_INT_LONGLONG +#endif + +#if !defined(LUA_FLOAT_TYPE) +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE +#endif + +/* }================================================================== */ + + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ + +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +#define LUA_PATH_SEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXEC_DIR "!" + /* @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -@* Lua libraries. +** Lua libraries. @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -@* C libraries. +** C libraries. ** CHANGE them if your machine has a non-conventional directory ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #if defined(_WIN32) /* { */ /* ** In Windows, any exclamation mark ('!') in the path is replaced by the @@ -91,21 +187,26 @@ */ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" ".\\?.lua" + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" #define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll" + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" #else /* }{ */ -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" #define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR -#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" "./?.lua" + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + "./?.lua;" "./?/init.lua" #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" #endif /* } */ @@ -122,14 +223,14 @@ #define LUA_DIRSEP "/" #endif +/* }================================================================== */ + /* -@@ LUA_ENV is the name of the variable that holds the current -@@ environment, used to access global names. -** CHANGE it if you do not like this name. +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== */ -#define LUA_ENV "_ENV" - /* @@ LUA_API is a mark for all core API functions. @@ -162,10 +263,10 @@ /* @@ LUAI_FUNC is a mark for all extern functions that are not to be -@* exported to outside modules. +** exported to outside modules. @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables -@* that are not to be exported to outside modules (LUAI_DDEF for -@* definitions and LUAI_DDEC for declarations). +** that are not to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). ** CHANGE them if you need to mark them in some special way. Elf/gcc ** (versions 3.2 and later) mark them as "hidden" to optimize access ** when Lua is compiled as a shared library. Not all elf targets support @@ -177,60 +278,14 @@ #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ defined(__ELF__) /* { */ #define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + #define LUAI_DDEC LUAI_FUNC #define LUAI_DDEF /* empty */ -#else /* }{ */ -#define LUAI_FUNC extern -#define LUAI_DDEC extern -#define LUAI_DDEF /* empty */ -#endif /* } */ - - - -/* -@@ LUA_QL describes how error messages quote program elements. -** CHANGE it if you want a different appearance. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@* of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -@@ luai_writestring/luai_writeline define how 'print' prints its results. -** They are only used in libraries and the stand-alone program. (The #if -** avoids including 'stdio.h' everywhere.) -*/ -#if defined(LUA_LIB) || defined(lua_c) -#include -#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout)) -#endif - -/* -@@ luai_writestringerror defines how to print error messages. -** (A format string with one argument is enough for Lua...) -*/ -#define luai_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) - - -/* -@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, -** strings that are internalized. (Cannot be smaller than reserved words -** or tags for metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#define LUAI_MAXSHORTLEN 40 - +/* }================================================================== */ /* @@ -240,11 +295,44 @@ */ /* -@@ LUA_COMPAT_ALL controls all compatibility options. +@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. +@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. ** You can define it to get all options, or change specific options ** to fit your specific needs. */ -#if defined(LUA_COMPAT_ALL) /* { */ +#if defined(LUA_COMPAT_5_2) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. +*/ +#define LUA_COMPAT_BITLIB + +/* +@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. +*/ +#define LUA_COMPAT_IPAIRS + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +*/ +#define LUA_COMPAT_APIINTCASTS + +#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'. @@ -305,52 +393,329 @@ #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 */ + /* }================================================================== */ /* -@@ LUAI_BITSINT defines the number of bits in an int. -** CHANGE here if Lua cannot automatically detect the number of bits of -** your machine. Probably you do not need to change this. +** {================================================================== +** Configuration for Numbers. +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* +** satisfy your needs. +** =================================================================== */ -/* avoid overflows in comparison */ -#if INT_MAX-20 < 32760 /* { */ -#define LUAI_BITSINT 16 -#elif INT_MAX > 2147483640L /* }{ */ -/* int has at least 32 bits */ -#define LUAI_BITSINT 32 -#else /* }{ */ -#error "you must define LUA_BITSINT with number of bits in an integer" -#endif /* } */ + +/* +@@ LUA_NUMBER is the floating-point type used by Lua. +@@ LUAI_UACNUMBER is the result of a 'default argument promotion' +@@ over a floating number. +@@ l_mathlim(x) corrects limit name 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. +@@ 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. +*/ + + +/* The following definitions are good for most cases here */ + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,sz,n) \ + l_sprintf((s), sz, LUA_NUMBER_FMT, (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.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* now the variable definitions */ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_mathlim(n) (FLT_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ + +#define LUA_NUMBER long double + +#define l_mathlim(n) (LDBL_##n) + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ + +#define LUA_NUMBER double + +#define l_mathlim(n) (DBL_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#else /* }{ */ + +#error "numeric float type not defined" + +#endif /* } */ + /* -@@ LUA_INT32 is an signed integer with exactly 32 bits. -@@ LUAI_UMEM is an unsigned integer big enough to count the total -@* memory used by Lua. -@@ LUAI_MEM is a signed integer big enough to count the total memory -@* used by Lua. -** CHANGE here if for some weird reason the default definitions are not -** good enough for your machine. Probably you do not need to change -** this. +@@ LUA_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. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ lua_integer2str converts an integer to a string. */ -#if LUAI_BITSINT >= 32 /* { */ -#define LUA_INT32 int -#define LUAI_UMEM size_t -#define LUAI_MEM ptrdiff_t + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" + +#define LUAI_UACINT LUA_INTEGER + +#define lua_integer2str(s,sz,n) \ + l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +/* now the variable definitions */ + +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ + +/* use presence of macro LLONG_MAX as proxy for C99 compliance */ +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + #else /* }{ */ -/* 16-bit ints */ -#define LUA_INT32 long -#define LUAI_UMEM unsigned long -#define LUAI_MEM long + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + #endif /* } */ +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 and other C details +** =================================================================== +*/ + +/* +@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. +** (All uses in Lua have only one format item.) +*/ +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) +#else +#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) +#endif + + +/* +@@ lua_strx2number converts an hexadecimal numeric string to a number. +** In C99, 'strtod' does that conversion. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ lua_number2strx converts a float to an hexadecimal numeric string. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,sz,f,n) \ + ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). You probably do not want/need to change them. +** ===================================================================== +*/ /* @@ LUAI_MAXSTACK limits the size of the Lua stack. ** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua to consume unlimited stack +** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). */ #if LUAI_BITSINT >= 32 @@ -359,183 +724,48 @@ #define LUAI_MAXSTACK 15000 #endif -/* reserve some space for error handling */ -#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000) + +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. +** CHANGE it if it uses too much C-stack space. (For long double, +** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a +** smaller buffer would force a memory allocation for each call to +** 'string.format'.) */ -#define LUAL_BUFFERSIZE BUFSIZ - - - - -/* -** {================================================================== -@@ LUA_NUMBER is the type of numbers in Lua. -** CHANGE the following definitions only if you want to build Lua -** with a number type different from double. You may also need to -** change lua_number2int & lua_number2integer. -** =================================================================== -*/ - -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double - -/* -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' -@* over a number. -*/ -#define LUAI_UACNUMBER double - - -/* -@@ LUA_NUMBER_SCAN is the format for reading numbers. -@@ LUA_NUMBER_FMT is the format for writing numbers. -@@ lua_number2str converts a number to a string. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. -*/ -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ - - -/* -@@ l_mathop allows the addition of an 'l' or 'f' to all math operations -*/ -#define l_mathop(x) (x) - - -/* -@@ lua_str2number converts a decimal numeric string to a number. -@@ lua_strx2number converts an hexadecimal numeric string to a number. -** In C99, 'strtod' does both conversions. C89, however, has no function -** to convert floating hexadecimal strings to numbers. For these -** systems, you can leave 'lua_strx2number' undefined and Lua will -** provide its own implementation. -*/ -#define lua_str2number(s,p) strtod((s), (p)) - -#if defined(LUA_USE_STRTODHEX) -#define lua_strx2number(s,p) strtod((s), (p)) +#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE +#define LUAL_BUFFERSIZE 8192 +#else +#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) #endif - -/* -@@ The luai_num* macros define the primitive operations over numbers. -*/ - -/* the following operations need the math library */ -#if defined(lobject_c) || defined(lvm_c) -#include -#define luai_nummod(L,a,b) ((a) - l_mathop(floor)((a)/(b))*(b)) -#define luai_numpow(L,a,b) (l_mathop(pow)(a,b)) -#endif - -/* these are quite standard operations */ -#if defined(LUA_CORE) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numdiv(L,a,b) ((a)/(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(L,a,b) ((a)<(b)) -#define luai_numle(L,a,b) ((a)<=(b)) -#define luai_numisnan(L,a) (!luai_numeq((a), (a))) -#endif - - - -/* -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most -** machines, ptrdiff_t gives a good choice between int or long.) -*/ -#define LUA_INTEGER ptrdiff_t - -/* -@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned. -** It must have at least 32 bits. -*/ -#define LUA_UNSIGNED unsigned LUA_INT32 - - - -/* -** Some tricks with doubles -*/ - -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ -/* -** The next definitions activate some tricks to speed up the -** conversion from doubles to integer types, mainly to LUA_UNSIGNED. -** -@@ LUA_MSASMTRICK uses Microsoft assembler to avoid clashes with a -** DirectX idiosyncrasy. -** -@@ LUA_IEEE754TRICK uses a trick that should work on any machine -** using IEEE754 with a 32-bit integer type. -** -@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be -** defined when LUA_INTEGER is a 32-bit integer. -** -@@ LUA_IEEEENDIAN is the endianness of doubles in your machine -** (0 for little endian, 1 for big endian); if not defined, Lua will -** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK). -** -@@ LUA_NANTRICK controls the use of a trick to pack all types into -** a single double value, using NaN values to represent non-number -** values. The trick only works on 32-bit machines (ints and pointers -** are 32-bit values) with numbers represented as IEEE 754-2008 doubles -** with conventional endianess (12345678 or 87654321), in CPUs that do -** not produce signaling NaN values (all NaNs are quiet). -*/ - -/* Microsoft compiler on a Pentium (32 bit) ? */ -#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ - -#define LUA_MSASMTRICK -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - - -/* pentium 32 bits? */ -#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEELL -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - -/* pentium 64 bits? */ -#elif defined(__x86_64) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 0 - -#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 1 - -#else /* }{ */ - -/* assume IEEE754 and a 32-bit integer type */ -#define LUA_IEEE754TRICK - -#endif /* } */ - -#endif /* } */ - /* }================================================================== */ +/* +@@ LUA_QL describes how error messages quote program elements. +** Lua does not use these macros anymore; they are here for +** compatibility only. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + /* =================================================================== */ @@ -547,5 +777,7 @@ + + #endif diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 9be47e58..54ae8df7 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -11,6 +11,9 @@ #include "lua.h" +/* version suffix for environment variable names */ +#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR + LUAMOD_API int (luaopen_base) (lua_State *L); @@ -29,6 +32,9 @@ LUAMOD_API int (luaopen_os) (lua_State *L); #define LUA_STRLIBNAME "string" LUAMOD_API int (luaopen_string) (lua_State *L); +#define LUA_UTF8LIBNAME "utf8" +LUAMOD_API int (luaopen_utf8) (lua_State *L); + #define LUA_BITLIBNAME "bit32" LUAMOD_API int (luaopen_bit32) (lua_State *L); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index a404594e..39674cd9 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -1,14 +1,17 @@ /* -** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ -#include - #define lundump_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -20,240 +23,258 @@ #include "lundump.h" #include "lzio.h" -typedef struct { - lua_State* L; - ZIO* Z; - Mbuffer* b; - const char* name; -} LoadState; - -static l_noret error(LoadState* S, const char* why) -{ - luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); - luaD_throw(S->L,LUA_ERRSYNTAX); -} - -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) -#define LoadByte(S) (lu_byte)LoadChar(S) -#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) -#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) #if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ +#define luai_verifycode(L,b,f) /* empty */ #endif -static void LoadBlock(LoadState* S, void* b, size_t size) -{ - if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); + +typedef struct { + lua_State *L; + ZIO *Z; + const char *name; +} LoadState; + + +static l_noret error(LoadState *S, const char *why) { + luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); + luaD_throw(S->L, LUA_ERRSYNTAX); } -static int LoadChar(LoadState* S) -{ - char x; - LoadVar(S,x); - return x; + +/* +** All high-level loads go through LoadVector; you can change it to +** adapt to the endianness of the input +*/ +#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) + +static void LoadBlock (LoadState *S, void *b, size_t size) { + if (luaZ_read(S->Z, b, size) != 0) + error(S, "truncated"); } -static int LoadInt(LoadState* S) -{ - int x; - LoadVar(S,x); - if (x<0) error(S,"corrupted"); - return x; + +#define LoadVar(S,x) LoadVector(S,&x,1) + + +static lu_byte LoadByte (LoadState *S) { + lu_byte x; + LoadVar(S, x); + return x; } -static lua_Number LoadNumber(LoadState* S) -{ - lua_Number x; - LoadVar(S,x); - return x; + +static int LoadInt (LoadState *S) { + int x; + LoadVar(S, x); + return x; } -static TString* LoadString(LoadState* S) -{ - size_t size; - LoadVar(S,size); - if (size==0) - return NULL; - else - { - char* s=luaZ_openspace(S->L,S->b,size); - LoadBlock(S,s,size*sizeof(char)); - return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ - } + +static lua_Number LoadNumber (LoadState *S) { + lua_Number x; + LoadVar(S, x); + return x; } -static void LoadCode(LoadState* S, SharedProto* f) -{ - int n=LoadInt(S); - f->code=luaM_newvector(S->L,n,Instruction); - f->sizecode=n; - LoadVector(S,f->code,n,sizeof(Instruction)); + +static lua_Integer LoadInteger (LoadState *S) { + lua_Integer x; + LoadVar(S, x); + return x; } -static void LoadFunction(LoadState* S, Proto* f); -static void LoadConstants(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->k=luaM_newvector(S->L,n,TValue); - f->sp->sizek=n; - for (i=0; ik[i]); - for (i=0; ik[i]; - int t=LoadChar(S); - switch (t) - { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o,LoadChar(S)); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); - break; - case LUA_TSTRING: - setsvalue2n(S->L,o,LoadString(S)); - break; - default: lua_assert(0); +static TString *LoadString (LoadState *S) { + size_t size = LoadByte(S); + if (size == 0xFF) + LoadVar(S, size); + if (size == 0) + return NULL; + else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ + char buff[LUAI_MAXSHORTLEN]; + LoadVector(S, buff, size); + return luaS_newlstr(S->L, buff, size); + } + else { /* long string */ + TString *ts = luaS_createlngstrobj(S->L, size); + LoadVector(S, getstr(ts), size); /* load directly in final place */ + return ts; } - } - n=LoadInt(S); - f->p=luaM_newvector(S->L,n,Proto*); - f->sp->sizep=n; - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=luaF_newproto(S->L, NULL); - LoadFunction(S,f->p[i]); - } } -static void LoadUpvalues(LoadState* S, SharedProto* f) -{ - int i,n; - n=LoadInt(S); - f->upvalues=luaM_newvector(S->L,n,Upvaldesc); - f->sizeupvalues=n; - for (i=0; iupvalues[i].name=NULL; - for (i=0; iupvalues[i].instack=LoadByte(S); - f->upvalues[i].idx=LoadByte(S); - } + +static void LoadCode (LoadState *S, SharedProto *f) { + int n = LoadInt(S); + f->code = luaM_newvector(S->L, n, Instruction); + f->sizecode = n; + LoadVector(S, f->code, n); } -static void LoadDebug(LoadState* S, SharedProto* f) -{ - int i,n; - f->source=LoadString(S); - n=LoadInt(S); - f->lineinfo=luaM_newvector(S->L,n,int); - f->sizelineinfo=n; - LoadVector(S,f->lineinfo,n,sizeof(int)); - n=LoadInt(S); - f->locvars=luaM_newvector(S->L,n,LocVar); - f->sizelocvars=n; - for (i=0; ilocvars[i].varname=NULL; - for (i=0; ilocvars[i].varname=LoadString(S); - f->locvars[i].startpc=LoadInt(S); - f->locvars[i].endpc=LoadInt(S); - } - n=LoadInt(S); - for (i=0; iupvalues[i].name=LoadString(S); + +static void LoadFunction(LoadState *S, Proto *f, TString *psource); + + +static void LoadConstants (LoadState *S, Proto *f) { + int i; + int n = LoadInt(S); + f->k = luaM_newvector(S->L, n, TValue); + f->sp->sizek = n; + for (i = 0; i < n; i++) + setnilvalue(&f->k[i]); + for (i = 0; i < n; i++) { + TValue *o = &f->k[i]; + int t = LoadByte(S); + switch (t) { + case LUA_TNIL: + setnilvalue(o); + break; + case LUA_TBOOLEAN: + setbvalue(o, LoadByte(S)); + break; + case LUA_TNUMFLT: + setfltvalue(o, LoadNumber(S)); + break; + case LUA_TNUMINT: + setivalue(o, LoadInteger(S)); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + setsvalue2n(S->L, o, LoadString(S)); + break; + default: + lua_assert(0); + } + } } -static void LoadFunction(LoadState* S, Proto* f) -{ - SharedProto *sp = f->sp; - sp->linedefined=LoadInt(S); - sp->lastlinedefined=LoadInt(S); - sp->numparams=LoadByte(S); - sp->is_vararg=LoadByte(S); - sp->maxstacksize=LoadByte(S); - LoadCode(S,sp); - LoadConstants(S,f); - LoadUpvalues(S,sp); - LoadDebug(S,sp); + +static void LoadProtos (LoadState *S, Proto *f) { + int i; + int n = LoadInt(S); + f->p = luaM_newvector(S->L, n, Proto *); + f->sp->sizep = n; + for (i = 0; i < n; i++) + f->p[i] = NULL; + for (i = 0; i < n; i++) { + f->p[i] = luaF_newproto(S->L, NULL); + LoadFunction(S, f->p[i], f->sp->source); + } } -/* the code below must be consistent with the code in luaU_header */ -#define N0 LUAC_HEADERSIZE -#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) -#define N2 N1+2 -#define N3 N2+6 -static void LoadHeader(LoadState* S) -{ - lu_byte h[LUAC_HEADERSIZE]; - lu_byte s[LUAC_HEADERSIZE]; - luaU_header(h); - memcpy(s,h,sizeof(char)); /* first char already read */ - LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); - if (memcmp(h,s,N0)==0) return; - if (memcmp(h,s,N1)!=0) error(S,"not a"); - if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); - if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); +static void LoadUpvalues (LoadState *S, SharedProto *f) { + int i, n; + n = LoadInt(S); + f->upvalues = luaM_newvector(S->L, n, Upvaldesc); + f->sizeupvalues = n; + for (i = 0; i < n; i++) + f->upvalues[i].name = NULL; + for (i = 0; i < n; i++) { + f->upvalues[i].instack = LoadByte(S); + f->upvalues[i].idx = LoadByte(S); + } } + +static void LoadDebug (LoadState *S, SharedProto *f) { + int i, n; + n = LoadInt(S); + f->lineinfo = luaM_newvector(S->L, n, int); + f->sizelineinfo = n; + LoadVector(S, f->lineinfo, n); + n = LoadInt(S); + f->locvars = luaM_newvector(S->L, n, LocVar); + f->sizelocvars = n; + for (i = 0; i < n; i++) + f->locvars[i].varname = NULL; + for (i = 0; i < n; i++) { + f->locvars[i].varname = LoadString(S); + f->locvars[i].startpc = LoadInt(S); + f->locvars[i].endpc = LoadInt(S); + } + n = LoadInt(S); + for (i = 0; i < n; i++) + f->upvalues[i].name = LoadString(S); +} + + +static void LoadFunction (LoadState *S, Proto *fp, TString *psource) { + SharedProto *f = fp->sp; + f->source = LoadString(S); + if (f->source == NULL) /* no source in dump? */ + f->source = psource; /* reuse parent's source */ + 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, fp); + LoadUpvalues(S, f); + LoadProtos(S, fp); + LoadDebug(S, f); +} + + +static void checkliteral (LoadState *S, const char *s, const char *msg) { + char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ + size_t len = strlen(s); + LoadVector(S, buff, len); + if (memcmp(s, buff, len) != 0) + error(S, msg); +} + + +static void fchecksize (LoadState *S, size_t size, const char *tname) { + if (LoadByte(S) != size) + error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); +} + + +#define checksize(S,t) fchecksize(S,sizeof(t),#t) + +static void checkHeader (LoadState *S) { + checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ + if (LoadByte(S) != LUAC_VERSION) + error(S, "version mismatch in"); + if (LoadByte(S) != LUAC_FORMAT) + error(S, "format mismatch in"); + checkliteral(S, LUAC_DATA, "corrupted"); + checksize(S, int); + checksize(S, size_t); + checksize(S, Instruction); + checksize(S, lua_Integer); + checksize(S, lua_Number); + if (LoadInteger(S) != LUAC_INT) + error(S, "endianness mismatch in"); + if (LoadNumber(S) != LUAC_NUM) + error(S, "float format mismatch in"); +} + + /* ** load precompiled chunk */ -Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) -{ - LoadState S; - Closure* cl; - if (*name=='@' || *name=='=') - S.name=name+1; - else if (*name==LUA_SIGNATURE[0]) - S.name="binary string"; - else - S.name=name; - S.L=L; - S.Z=Z; - S.b=buff; - LoadHeader(&S); - cl=luaF_newLclosure(L,1); - setclLvalue(L,L->top,cl); incr_top(L); - cl->l.p=luaF_newproto(L, NULL); - LoadFunction(&S,cl->l.p); - if (cl->l.p->sp->sizeupvalues != 1) - { - Proto* p=cl->l.p; - cl=luaF_newLclosure(L,cl->l.p->sp->sizeupvalues); - cl->l.p=p; - setclLvalue(L,L->top-1,cl); - } - luai_verifycode(L,buff,cl->l.p); - return cl; +LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { + LoadState S; + LClosure *cl; + if (*name == '@' || *name == '=') + S.name = name + 1; + else if (*name == LUA_SIGNATURE[0]) + S.name = "binary string"; + else + S.name = name; + S.L = L; + S.Z = Z; + checkHeader(&S); + cl = luaF_newLclosure(L, LoadByte(&S)); + setclLvalue(L, L->top, cl); + luaD_inctop(L); + cl->p = luaF_newproto(L, NULL); + LoadFunction(&S, cl->p, NULL); + lua_assert(cl->nupvalues == cl->p->sp->sizeupvalues); + luai_verifycode(L, buff, cl->p); + return cl; } -#define MYINT(s) (s[0]-'0') -#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) -#define FORMAT 0 /* this is the official format */ - -/* -* make header for precompiled chunks -* if you change the code below be sure to update LoadHeader and FORMAT above -* and LUAC_HEADERSIZE in lundump.h -*/ -void luaU_header (lu_byte* h) -{ - int x=1; - memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); - h+=sizeof(LUA_SIGNATURE)-sizeof(char); - *h++=cast_byte(VERSION); - *h++=cast_byte(FORMAT); - *h++=cast_byte(*(char*)&x); /* endianness */ - *h++=cast_byte(sizeof(int)); - *h++=cast_byte(sizeof(size_t)); - *h++=cast_byte(sizeof(Instruction)); - *h++=cast_byte(sizeof(lua_Number)); - *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ - memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); -} diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index 5255db25..aa5cc82f 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -7,22 +7,26 @@ #ifndef lundump_h #define lundump_h +#include "llimits.h" #include "lobject.h" #include "lzio.h" -/* load one chunk; from lundump.c */ -LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); - -/* make header; from lundump.c */ -LUAI_FUNC void luaU_header (lu_byte* h); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); /* data to catch conversion errors */ -#define LUAC_TAIL "\x19\x93\r\n\x1a\n" +#define LUAC_DATA "\x19\x93\r\n\x1a\n" -/* size in bytes of header of binary files */ -#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) +#define LUAC_INT 0x5678 +#define LUAC_NUM cast_num(370.5) + +#define MYINT(s) (s[0]-'0') +#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_FORMAT 0 /* this is the official format */ + +/* load one chunk; from lundump.c */ +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); + +/* dump one chunk; from ldump.c */ +LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, + void* data, int strip); #endif diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c new file mode 100644 index 00000000..de9e3dcd --- /dev/null +++ b/3rd/lua/lutf8lib.c @@ -0,0 +1,256 @@ +/* +** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp $ +** Standard library for UTF-8 manipulation +** See Copyright Notice in lua.h +*/ + +#define lutf8lib_c +#define LUA_LIB + +#include "lprefix.h" + + +#include +#include +#include +#include + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + +#define MAXUNICODE 0x10FFFF + +#define iscont(p) ((*(p) & 0xC0) == 0x80) + + +/* from strlib */ +/* translate a relative string position: negative means back from end */ +static lua_Integer u_posrelat (lua_Integer pos, size_t len) { + if (pos >= 0) return pos; + else if (0u - (size_t)pos > len) return 0; + else return (lua_Integer)len + pos + 1; +} + + +/* +** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. +*/ +static const char *utf8_decode (const char *o, int *val) { + static 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 */ + if (c < 0x80) /* ascii? */ + res = c; + else { + int count = 0; /* to count number of continuation bytes */ + while (c & 0x40) { /* still have continuation bytes? */ + int cc = s[++count]; /* read next byte */ + if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + return NULL; /* invalid byte sequence */ + res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ + c <<= 1; /* to test next bit */ + } + res |= ((c & 0x7F) << (count * 5)); /* add first byte */ + if (count > 3 || res > MAXUNICODE || res <= limits[count]) + return NULL; /* invalid byte sequence */ + s += count; /* skip continuation bytes read */ + } + if (val) *val = res; + return (const char *)s + 1; /* +1 to include first byte */ +} + + +/* +** utf8len(s [, i [, j]]) --> number of characters that start in the +** range [i,j], or nil + current position if 's' is not well formed in +** that interval +*/ +static int utflen (lua_State *L) { + int n = 0; + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); + lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, + "initial position out of string"); + luaL_argcheck(L, --posj < (lua_Integer)len, 3, + "final position out of string"); + while (posi <= posj) { + const char *s1 = utf8_decode(s + posi, NULL); + if (s1 == NULL) { /* conversion error? */ + lua_pushnil(L); /* return nil ... */ + lua_pushinteger(L, posi + 1); /* ... and current position */ + return 2; + } + posi = s1 - s; + n++; + } + lua_pushinteger(L, n); + return 1; +} + + +/* +** codepoint(s, [i, [j]]) -> returns codepoints for all characters +** that start in the range [i,j] +*/ +static int codepoint (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); + lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); + int n; + const char *se; + luaL_argcheck(L, posi >= 1, 2, "out of range"); + luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); + if (posi > pose) return 0; /* empty interval; return no values */ + if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ + return luaL_error(L, "string slice too long"); + n = (int)(pose - posi) + 1; + luaL_checkstack(L, n, "string slice too long"); + n = 0; + se = s + pose; + for (s += posi - 1; s < se;) { + int code; + s = utf8_decode(s, &code); + if (s == NULL) + return luaL_error(L, "invalid UTF-8 code"); + lua_pushinteger(L, code); + n++; + } + return n; +} + + +static void pushutfchar (lua_State *L, int arg) { + lua_Integer code = luaL_checkinteger(L, arg); + luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); + lua_pushfstring(L, "%U", (long)code); +} + + +/* +** utfchar(n1, n2, ...) -> char(n1)..char(n2)... +*/ +static int utfchar (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + if (n == 1) /* optimize common case of single char */ + pushutfchar(L, 1); + else { + int i; + luaL_Buffer b; + luaL_buffinit(L, &b); + for (i = 1; i <= n; i++) { + pushutfchar(L, i); + luaL_addvalue(&b); + } + luaL_pushresult(&b); + } + return 1; +} + + +/* +** offset(s, n, [i]) -> index where n-th character counting from +** position 'i' starts; 0 means character at 'i'. +*/ +static int byteoffset (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer n = luaL_checkinteger(L, 2); + lua_Integer posi = (n >= 0) ? 1 : len + 1; + posi = u_posrelat(luaL_optinteger(L, 3, posi), len); + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, + "position out of range"); + if (n == 0) { + /* find beginning of current byte sequence */ + while (posi > 0 && iscont(s + posi)) posi--; + } + else { + if (iscont(s + posi)) + luaL_error(L, "initial position is a continuation byte"); + if (n < 0) { + while (n < 0 && posi > 0) { /* move back */ + do { /* find beginning of previous character */ + posi--; + } while (posi > 0 && iscont(s + posi)); + n++; + } + } + else { + n--; /* do not move for 1st character */ + while (n > 0 && posi < (lua_Integer)len) { + do { /* find beginning of next character */ + posi++; + } while (iscont(s + posi)); /* (cannot pass final '\0') */ + n--; + } + } + } + if (n == 0) /* did it find given character? */ + lua_pushinteger(L, posi + 1); + else /* no such character */ + lua_pushnil(L); + return 1; +} + + +static int iter_aux (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer n = lua_tointeger(L, 2) - 1; + if (n < 0) /* first iteration? */ + n = 0; /* start from here */ + else if (n < (lua_Integer)len) { + n++; /* skip current byte */ + while (iscont(s + n)) n++; /* and its continuations */ + } + if (n >= (lua_Integer)len) + return 0; /* no more codepoints */ + else { + int code; + const char *next = utf8_decode(s + n, &code); + if (next == NULL || iscont(next)) + return luaL_error(L, "invalid UTF-8 code"); + lua_pushinteger(L, n + 1); + lua_pushinteger(L, code); + return 2; + } +} + + +static int iter_codes (lua_State *L) { + luaL_checkstring(L, 1); + lua_pushcfunction(L, iter_aux); + lua_pushvalue(L, 1); + lua_pushinteger(L, 0); + return 3; +} + + +/* pattern to match a single UTF-8 character */ +#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" + + +static const luaL_Reg funcs[] = { + {"offset", byteoffset}, + {"codepoint", codepoint}, + {"char", utfchar}, + {"len", utflen}, + {"codes", iter_codes}, + /* placeholders */ + {"charpattern", NULL}, + {NULL, NULL} +}; + + +LUAMOD_API int luaopen_utf8 (lua_State *L) { + luaL_newlib(L, funcs); + lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); + lua_setfield(L, -2, "charpattern"); + return 1; +} + diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 43019d43..eeaa1008 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,17 +1,21 @@ /* -** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lvm.c,v 2.268 2016/02/05 19:59:14 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ +#define lvm_c +#define LUA_CORE +#include "lprefix.h" + +#include +#include +#include #include #include #include -#define lvm_c -#define LUA_CORE - #include "lua.h" #include "ldebug.h" @@ -27,200 +31,246 @@ #include "lvm.h" - /* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 100 +#define MAXTAGLOOP 2000 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) { - lua_Number num; - if (ttisnumber(obj)) return obj; - if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { - setnvalue(n, num); - return n; + +/* +** '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. +*/ +#if !defined(l_intfitsf) + +/* number of bits in the mantissa of a float */ +#define NBM (l_mathlim(MANT_DIG)) + +/* +** Check whether some integers may not fit in a float, that is, whether +** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). +** (The shifts are done in parts to avoid shifting by more than the size +** of an integer. In a worst case, NBM == 113 for long double and +** sizeof(integer) == 32.) +*/ +#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ + >> (NBM - (3 * (NBM / 4)))) > 0 + +#define l_intfitsf(i) \ + (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) + +#endif + +#endif + +/* 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); } - else - return NULL; } - -int luaV_tostring (lua_State *L, StkId obj) { - if (!ttisnumber(obj)) - return 0; - else { - char s[LUAI_MAXNUMBER2STR]; - lua_Number n = nvalue(obj); - int l = lua_number2str(s, n); - setsvalue2s(L, obj, luaS_newlstr(L, s, l)); +/* +** Try to convert a value to a float. The float case is already handled +** by the macro 'tonumber'. +*/ +int luaV_tonumber_ (const TValue *obj, lua_Number *n) { + TValue v; + if (ttisinteger(obj)) { + *n = cast_num(ivalue(obj)); return 1; } + else if (cvt2num(obj) && /* string convertible to number? */ + luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { + *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ + return 1; + } + else + return 0; /* conversion failed */ } -static void traceexec (lua_State *L) { - CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; - int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); - if (counthook) - resethookcount(L); /* reset count */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return; /* do not call hook again (VM yielded, so it did not move) */ - } - if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); - int newline = getfuncline(p, npc); - if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ - luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ - } - L->oldpc = ci->u.l.savedpc; - if (L->status == LUA_YIELD) { /* did hook yield? */ - if (counthook) - L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - ci->func = L->top - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); - } -} - - -static void callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres) { - ptrdiff_t result = savestack(L, p3); - setobj2s(L, L->top++, f); /* push function */ - setobj2s(L, L->top++, p1); /* 1st argument */ - setobj2s(L, L->top++, p2); /* 2nd argument */ - if (!hasres) /* no result? 'p3' is third argument */ - setobj2s(L, L->top++, p3); /* 3rd argument */ - /* metamethod may yield only when called from Lua code */ - luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); - if (hasres) { /* if has result, move it to its place */ - p3 = restorestack(L, result); - setobjs2s(L, p3, --L->top); - } -} - - -void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - const TValue *res = luaH_get(h, key); /* do a primitive get */ - if (!ttisnil(res) || /* result is not nil? */ - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); - return; - } - /* else will try the tag method */ +/* +** 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 +*/ +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) */ } - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 1); - return; - } - t = tm; /* else repeat with 'tm' */ + return lua_numbertointeger(f, p); } - luaG_runerror(L, "loop in gettable"); + 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 */ + } + return 0; /* conversion failed */ } -void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - TValue *oldval = cast(TValue *, luaH_get(h, key)); - /* if previous value is not nil, there must be a previous entry - in the table; moreover, a metamethod has no relevance */ - if (!ttisnil(oldval) || - /* previous value is nil; must check the metamethod */ - ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (oldval != luaO_nilobject || - /* no previous entry; must create one. (The next test is - always true; we only need the assignment.) */ - (oldval = luaH_newkey(L, h, key), 1)))) { - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, oldval, val); /* assign new value to that entry */ - invalidateTMcache(h); - luaC_barrierback(L, obj2gco(h), val); - return; - } - /* else will try the metamethod */ +/* +** Try to convert a 'for' limit to an integer, preserving the +** semantics of the loop. +** (The following explanation assumes a non-negative step; it is valid +** for negative steps mutatis mutandis.) +** If the limit can be converted to an integer, rounding down, that is +** it. +** Otherwise, check whether the limit can be converted to a number. If +** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, +** which means no limit. If the number is too negative, the loop +** should not run, because any initial integer value is larger than the +** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects +** the extreme case when the initial value is LUA_MININTEGER, in which +** case the LUA_MININTEGER limit would still run the loop once. +*/ +static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, + int *stopnow) { + *stopnow = 0; /* usually, let loops run */ + if (!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; } - else /* not a table; check metamethod */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) - luaG_typeerror(L, t, "index"); - /* there is a metamethod */ - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 0); - return; + else { /* float is smaller than min integer */ + *p = LUA_MININTEGER; + if (step >= 0) *stopnow = 1; } - t = tm; /* else repeat with 'tm' */ } - luaG_runerror(L, "loop in settable"); -} - - -static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (ttisnil(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - callTM(L, tm, p1, p2, res, 1); return 1; } -static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, - TMS event) { - const TValue *tm1 = fasttm(L, mt1, event); - const TValue *tm2; - if (tm1 == NULL) return NULL; /* no metamethod */ - if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ - tm2 = fasttm(L, mt2, event); - if (tm2 == NULL) return NULL; /* no metamethod */ - if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */ - return tm1; - return NULL; +/* +** Finish the table access 'val = t[key]'. +** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to +** t[k] entry (which must be nil). +*/ +void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, + const TValue *slot) { + int loop; /* counter to avoid infinite loops */ + const TValue *tm; /* metamethod */ + for (loop = 0; loop < MAXTAGLOOP; loop++) { + if (slot == NULL) { /* 't' is not a table? */ + lua_assert(!ttistable(t)); + tm = luaT_gettmbyobj(L, t, TM_INDEX); + if (ttisnil(tm)) + luaG_typeerror(L, t, "index"); /* no metamethod */ + /* else will try the metamethod */ + } + else { /* 't' is a table */ + lua_assert(ttisnil(slot)); + tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ + if (tm == NULL) { /* no metamethod? */ + setnilvalue(val); /* result is nil */ + return; + } + /* else will try the metamethod */ + } + if (ttisfunction(tm)) { /* is metamethod a function? */ + luaT_callTM(L, tm, t, key, val, 1); /* call it */ + return; + } + t = tm; /* else try to access 'tm[key]' */ + if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ + setobj2s(L, val, slot); /* done */ + return; + } + /* else repeat (tail call 'luaV_finishget') */ + } + luaG_runerror(L, "'__index' chain too long; possible loop"); } -static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, - TMS event) { - if (!call_binTM(L, p1, p2, L->top, event)) - return -1; /* no metamethod */ - else - return !l_isfalse(L->top); +/* +** Finish a table assignment 't[key] = val'. +** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points +** to the entry 't[key]', or to 'luaO_nilobject' if there is no such +** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' +** would have done the job.) +*/ +void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *slot) { + int loop; /* counter to avoid infinite loops */ + for (loop = 0; loop < MAXTAGLOOP; loop++) { + const TValue *tm; /* '__newindex' metamethod */ + if (slot != NULL) { /* is 't' a table? */ + Table *h = hvalue(t); /* save 't' table */ + lua_assert(ttisnil(slot)); /* old value must be nil */ + tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ + if (tm == NULL) { /* no metamethod? */ + if (slot == luaO_nilobject) /* no previous entry? */ + slot = luaH_newkey(L, h, key); /* create one */ + /* no metamethod and (now) there is an entry with given key */ + setobj2t(L, cast(TValue *, slot), val); /* set its new value */ + invalidateTMcache(h); + luaC_barrierback(L, h, val); + return; + } + /* else will try the metamethod */ + } + else { /* not a table; check metamethod */ + if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) + luaG_typeerror(L, t, "index"); + } + /* try the metamethod */ + if (ttisfunction(tm)) { + luaT_callTM(L, tm, t, key, val, 0); + return; + } + t = tm; /* else repeat assignment over 'tm' */ + if (luaV_fastset(L, t, key, slot, luaH_get, val)) + return; /* done */ + /* else 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'. +** The code is a little tricky because it allows '\0' in the strings +** and it uses 'strcoll' (to respect locales) for each segments +** of the strings. +*/ static int l_strcmp (const TString *ls, const TString *rs) { const char *l = getstr(ls); - size_t ll = ls->tsv.len; + size_t ll = tsslen(ls); const char *r = getstr(rs); - size_t lr = rs->tsv.len; - for (;;) { + size_t lr = tsslen(rs); + for (;;) { /* for each segment */ int temp = strcoll(l, r); - if (temp != 0) return temp; - else { /* strings are equal up to a `\0' */ - size_t len = strlen(l); /* index of first `\0' in both strings */ - if (len == lr) /* r is finished? */ - return (len == ll) ? 0 : 1; - else if (len == ll) /* l is finished? */ - return -1; /* l is smaller than r (because r is not finished) */ - /* both strings longer than `len'; go on comparing (after the `\0') */ + if (temp != 0) /* not equal? */ + return temp; /* done */ + else { /* strings are equal up to a '\0' */ + size_t len = strlen(l); /* index of first '\0' in both strings */ + if (len == lr) /* 'rs' is finished? */ + return (len == ll) ? 0 : 1; /* check 'ls' */ + else if (len == ll) /* 'ls' is finished? */ + return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ + /* both strings longer than 'len'; go on comparing after the '\0' */ len++; l += len; ll -= len; r += len; lr -= len; } @@ -228,103 +278,242 @@ static int l_strcmp (const TString *ls, const TString *rs) { } -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numlt(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; - else if ((res = call_orderTM(L, l, r, TM_LT)) < 0) - luaG_ordererror(L, l, r); - return res; -} - - -int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numle(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; - else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */ - return res; - else if ((res = call_orderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */ - luaG_ordererror(L, l, r); - return !res; +/* +** Check whether integer 'i' is less than float 'f'. If 'i' has an +** exact representation as a float ('l_intfitsf'), compare numbers as +** floats. Otherwise, if 'f' is outside the range for integers, result +** is trivial. Otherwise, compare them as integers. (When 'i' has no +** float representation, either 'f' is "far away" from 'i' or 'f' has +** no precision left for a fractional part; either way, how 'f' is +** truncated is irrelevant.) When 'f' is NaN, comparisons must result +** in false. +*/ +static int LTintfloat (lua_Integer i, lua_Number f) { +#if defined(l_intfitsf) + if (!l_intfitsf(i)) { + if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ + return 1; /* f >= maxint + 1 > i */ + else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ + return (i < cast(lua_Integer, f)); /* compare them as integers */ + else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ + return 0; + } +#endif + return luai_numlt(cast_num(i), f); /* compare them as floats */ } /* -** equality of Lua values. L == NULL means raw equality (no metamethods) +** Check whether integer 'i' is less than or equal to float 'f'. +** See comments on previous function. */ -int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) { +static int LEintfloat (lua_Integer i, lua_Number f) { +#if defined(l_intfitsf) + if (!l_intfitsf(i)) { + if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ + return 1; /* f >= maxint + 1 > i */ + else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ + return (i <= cast(lua_Integer, f)); /* compare them as integers */ + else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ + return 0; + } +#endif + return luai_numle(cast_num(i), f); /* compare them as floats */ +} + + +/* +** Return 'l < r', for numbers. +*/ +static int LTnum (const TValue *l, const TValue *r) { + if (ttisinteger(l)) { + lua_Integer li = ivalue(l); + if (ttisinteger(r)) + return li < ivalue(r); /* both are integers */ + else /* 'l' is int and 'r' is float */ + return LTintfloat(li, fltvalue(r)); /* l < r ? */ + } + else { + lua_Number lf = fltvalue(l); /* 'l' must be float */ + if (ttisfloat(r)) + return luai_numlt(lf, fltvalue(r)); /* both are float */ + else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ + return 0; /* NaN < i is always false */ + else /* without NaN, (l < r) <--> not(r <= l) */ + return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ + } +} + + +/* +** Return 'l <= r', for numbers. +*/ +static int LEnum (const TValue *l, const TValue *r) { + if (ttisinteger(l)) { + lua_Integer li = ivalue(l); + if (ttisinteger(r)) + return li <= ivalue(r); /* both are integers */ + else /* 'l' is int and 'r' is float */ + return LEintfloat(li, fltvalue(r)); /* l <= r ? */ + } + else { + lua_Number lf = fltvalue(l); /* 'l' must be float */ + if (ttisfloat(r)) + return luai_numle(lf, fltvalue(r)); /* both are float */ + else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ + return 0; /* NaN <= i is always false */ + else /* without NaN, (l <= r) <--> not(r < l) */ + return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ + } +} + + +/* +** Main operation less than; return 'l < r'. +*/ +int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { + int res; + 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; +} + + +/* +** 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 */ + } +} + + +/* +** Main operation for equality of Lua values; return 't1 == t2'. +** L == NULL means raw equality (no metamethods) +*/ +int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; - lua_assert(ttisequal(t1, t2)); + if (ttype(t1) != ttype(t2)) { /* not the same variant? */ + if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) + return 0; /* only numbers can be equal with different variants */ + else { /* two numbers with different variants */ + lua_Integer i1, i2; /* compare them as integers */ + return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); + } + } + /* values have same type and same variant */ switch (ttype(t1)) { case LUA_TNIL: return 1; - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); + case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); - case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2)); + case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); case LUA_TUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; - tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ); + tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_TTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; - tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); + tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } default: - lua_assert(iscollectable(t1)); return gcvalue(t1) == gcvalue(t2); } - if (tm == NULL) return 0; /* no TM? */ - callTM(L, tm, t1, t2, L->top, 1); /* call TM */ + if (tm == NULL) /* no TM? */ + return 0; /* objects are different */ + luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ return !l_isfalse(L->top); } +/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ +#define tostring(L,o) \ + (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) + +#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) + +/* copy strings in stack from top - n up to top - 1 to buffer */ +static void copy2buff (StkId top, int n, char *buff) { + size_t tl = 0; /* size already copied */ + do { + size_t l = vslen(top - n); /* length of string being copied */ + memcpy(buff + tl, svalue(top - n), l * sizeof(char)); + tl += l; + } while (--n > 0); +} + + +/* +** Main operation for concatenation: concat 'total' values in the stack, +** from 'L->top - total' up to 'L->top - 1'. +*/ void luaV_concat (lua_State *L, int total) { lua_assert(total >= 2); do { StkId top = L->top; int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); - } - else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ - (void)tostring(L, top - 2); /* result is first operand */ - else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) { + 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? */ setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { /* at least two non-empty string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; - char *buffer; - int i; - /* collect total length */ - for (i = 1; i < total && tostring(L, top-i-1); i++) { - size_t l = tsvalue(top-i-1)->len; - if (l >= (MAX_SIZET/sizeof(char)) - tl) + size_t tl = vslen(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) luaG_runerror(L, "string length overflow"); tl += l; } - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - n = i; - do { /* concat all strings */ - size_t l = tsvalue(top-i)->len; - memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); - tl += l; - } while (--i > 0); - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); + if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ + char buff[LUAI_MAXSHORTLEN]; + copy2buff(top, n, buff); /* copy strings to buffer */ + ts = luaS_newlstr(L, buff, tl); + } + else { /* long string; copy strings directly to final result */ + ts = luaS_createlngstrobj(L, tl); + copy2buff(top, n, getstr(ts)); + } + setsvalue2s(L, top - n, ts); /* create result */ } total -= n-1; /* got 'n' strings to create 1 new */ L->top -= n-1; /* popped 'n' strings and pushed one */ @@ -332,18 +521,25 @@ void luaV_concat (lua_State *L, int total) { } +/* +** Main operation 'ra' = #rb'. +*/ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; - switch (ttypenv(rb)) { + switch (ttype(rb)) { case LUA_TTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */ + setivalue(ra, luaH_getn(h)); /* else primitive len */ return; } - case LUA_TSTRING: { - setnvalue(ra, cast_num(tsvalue(rb)->len)); + case LUA_TSHRSTR: { + setivalue(ra, tsvalue(rb)->shrlen); + return; + } + case LUA_TLNGSTR: { + setivalue(ra, tsvalue(rb)->u.lnglen); return; } default: { /* try metamethod */ @@ -353,21 +549,66 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { break; } } - callTM(L, tm, rb, rb, ra, 1); + luaT_callTM(L, tm, rb, rb, ra, 1); } -void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op) { - TValue tempb, tempc; - const TValue *b, *c; - if ((b = luaV_tonumber(rb, &tempb)) != NULL && - (c = luaV_tonumber(rc, &tempc)) != NULL) { - lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c)); - setnvalue(ra, res); +/* +** Integer division; return 'm // n', that is, floor(m/n). +** C division truncates its result (rounds towards zero). +** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, +** otherwise 'floor(q) == trunc(q) - 1'. +*/ +lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (n == 0) + luaG_runerror(L, "attempt to divide by zero"); + return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ + } + else { + lua_Integer q = m / n; /* perform C division */ + if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ + q -= 1; /* correct result for different rounding */ + return q; + } +} + + +/* +** Integer modulus; return 'm % n'. (Assume that C '%' with +** negative operands follows C99 behavior. See previous comment +** about luaV_div.) +*/ +lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (n == 0) + luaG_runerror(L, "attempt to perform 'n%%0'"); + return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ + } + else { + lua_Integer r = m % n; + if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ + r += n; /* correct result for different rounding */ + return r; + } +} + + +/* number of bits in an integer */ +#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) + +/* +** Shift left operation. (Shift right just negates 'y'.) +*/ +lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { + if (y < 0) { /* shift right? */ + if (y <= -NBITS) return 0; + else return intop(>>, x, -y); + } + else { /* shift left */ + if (y >= NBITS) return 0; + else return intop(<<, x, y); } - else if (!call_binTM(L, rb, rc, ra, op)) - luaG_aritherror(L, rb, rc); } @@ -376,15 +617,15 @@ void luaV_arith (lua_State *L, StkId ra, const TValue *rb, ** whether there is a cached closure with the same upvalues needed by ** new closure to be created. */ -static Closure *getcached (Proto *p, UpVal **encup, StkId base) { - Closure *c = p->cache; +static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { + LClosure *c = p->cache; if (c != NULL) { /* is there a cached closure? */ int nup = p->sp->sizeupvalues; Upvaldesc *uv = p->sp->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; - if (c->l.upvals[i]->v != v) + if (c->upvals[i]->v != v) return NULL; /* wrong upvalue; cannot reuse closure */ } } @@ -394,26 +635,28 @@ static Closure *getcached (Proto *p, UpVal **encup, StkId base) { /* ** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the call to 'luaC_barrierproto' must come -** before the assignment to 'p->cache', as the function needs the -** original value of that field. +** its upvalues. Note that the closure is not cached if prototype is +** already black (which means that 'cache' was already cleared by the +** GC). */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { int nup = p->sp->sizeupvalues; Upvaldesc *uv = p->sp->upvalues; int i; - Closure *ncl = luaF_newLclosure(L, nup); - ncl->l.p = p; + LClosure *ncl = luaF_newLclosure(L, nup); + ncl->p = p; setclLvalue(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx); + ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ - ncl->l.upvals[i] = encup[uv[i].idx]; + ncl->upvals[i] = encup[uv[i].idx]; + ncl->upvals[i]->refcount++; + /* new closure is white, so we do not need a barrier here */ } - luaC_barrierproto(L, p, ncl); - p->cache = ncl; /* save it on cache for reuse */ + if (!isblack(p)) /* cache will not break GC invariant? */ + p->cache = ncl; /* save it on cache for reuse */ } @@ -426,8 +669,10 @@ void luaV_finishOp (lua_State *L) { Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: - case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN: + case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: + case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: + case OP_MOD: case OP_POW: + case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { setobjs2s(L, base + GETARG_A(inst), --L->top); break; @@ -435,18 +680,18 @@ void luaV_finishOp (lua_State *L) { case OP_LE: case OP_LT: case OP_EQ: { int res = !l_isfalse(L->top - 1); L->top--; - /* metamethod should not be called when operand is K */ - lua_assert(!ISK(GETARG_B(inst))); - if (op == OP_LE && /* "<=" using "<" instead? */ - ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE))) - res = !res; /* invert result */ + if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ + lua_assert(op == OP_LE); + ci->callstatus ^= CIST_LEQ; /* clear mark */ + res = !res; /* negate result */ + } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_A(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'call_binTM' was called */ + StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ int b = GETARG_B(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ setobj2s(L, top - 2, top); /* put TM result in proper position */ @@ -477,31 +722,32 @@ void luaV_finishOp (lua_State *L) { + /* -** some macros for common tasks in `luaV_execute' +** {================================================================== +** Function 'luaV_execute': main interpreter loop +** =================================================================== */ -#if !defined luai_runtimecheck -#define luai_runtimecheck(L, c) /* void */ -#endif + +/* +** some macros for common tasks in 'luaV_execute' +*/ #define RA(i) (base+GETARG_A(i)) -/* to be used after possible stack reallocation */ #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) -#define KBx(i) \ - (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++))) /* execute a jump instruction */ #define dojump(ci,i,e) \ { int a = GETARG_A(i); \ - if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \ + if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ ci->u.l.savedpc += GETARG_sBx(i) + e; } /* for test instructions, execute the jump instruction that follows it */ @@ -511,96 +757,124 @@ void luaV_finishOp (lua_State *L) { #define Protect(x) { {x;}; base = ci->u.l.base; } #define checkGC(L,c) \ - Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \ - luaC_step(L); \ - L->top = ci->top;}) /* restore top */ \ - luai_threadyield(L); ) + { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + Protect(L->top = ci->top)); /* restore top */ \ + luai_threadyield(L); } -#define arith_op(op,tm) { \ - TValue *rb = RKB(i); \ - TValue *rc = RKC(i); \ - if (ttisnumber(rb) && ttisnumber(rc)) { \ - lua_Number nb = nvalue(rb), nc = nvalue(rc); \ - setnvalue(ra, op(L, nb, nc)); \ - } \ - else { Protect(luaV_arith(L, ra, rb, rc, tm)); } } - +/* fetch an instruction and prepare its execution */ +#define vmfetch() { \ + i = *(ci->u.l.savedpc++); \ + if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \ + Protect(luaG_traceexec(L)); \ + ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ + lua_assert(base == ci->u.l.base); \ + lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \ +} #define vmdispatch(o) switch(o) -#define vmcase(l,b) case l: {b} break; -#define vmcasenb(l,b) case l: {b} /* nb = no break */ +#define vmcase(l) case 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; LClosure *cl; TValue *k; StkId base; + ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); - cl = clLvalue(ci->func); - k = cl->p->k; - base = ci->u.l.base; + cl = clLvalue(ci->func); /* local reference to function's closure */ + k = cl->p->k; /* local reference to function's constant table */ + base = ci->u.l.base; /* local copy of function's base */ /* main loop of interpreter */ for (;;) { - Instruction i = *(ci->u.l.savedpc++); + Instruction i; StkId ra; - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && - (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { - Protect(traceexec(L)); - } - /* WARNING: several calls may realloc the stack and invalidate `ra' */ - ra = RA(i); - lua_assert(base == ci->u.l.base); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + vmfetch(); vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE, + vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); - ) - vmcase(OP_LOADK, + vmbreak; + } + vmcase(OP_LOADK) { TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); - ) - vmcase(OP_LOADKX, + vmbreak; + } + vmcase(OP_LOADKX) { TValue *rb; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); rb = k + GETARG_Ax(*ci->u.l.savedpc++); setobj2s(L, ra, rb); - ) - vmcase(OP_LOADBOOL, + vmbreak; + } + vmcase(OP_LOADBOOL) { setbvalue(ra, GETARG_B(i)); if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ - ) - vmcase(OP_LOADNIL, + vmbreak; + } + vmcase(OP_LOADNIL) { int b = GETARG_B(i); do { setnilvalue(ra++); } while (b--); - ) - vmcase(OP_GETUPVAL, + vmbreak; + } + vmcase(OP_GETUPVAL) { int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); - ) - vmcase(OP_GETTABUP, - int b = GETARG_B(i); - Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); - ) - vmcase(OP_GETTABLE, - Protect(luaV_gettable(L, RB(i), RKC(i), ra)); - ) - vmcase(OP_SETTABUP, - int a = GETARG_A(i); - Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); - ) - vmcase(OP_SETUPVAL, + vmbreak; + } + vmcase(OP_GETTABUP) { + TValue *upval = cl->upvals[GETARG_B(i)]->v; + TValue *rc = RKC(i); + gettableProtected(L, upval, rc, ra); + vmbreak; + } + vmcase(OP_GETTABLE) { + StkId rb = RB(i); + TValue *rc = RKC(i); + gettableProtected(L, rb, rc, ra); + vmbreak; + } + vmcase(OP_SETTABUP) { + TValue *upval = cl->upvals[GETARG_A(i)]->v; + TValue *rb = RKB(i); + TValue *rc = RKC(i); + settableProtected(L, upval, rb, rc); + vmbreak; + } + vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); - ) - vmcase(OP_SETTABLE, - Protect(luaV_settable(L, ra, RKB(i), RKC(i))); - ) - vmcase(OP_NEWTABLE, + luaC_upvalbarrier(L, uv); + vmbreak; + } + vmcase(OP_SETTABLE) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + settableProtected(L, ra, rb, rc); + vmbreak; + } + vmcase(OP_NEWTABLE) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t = luaH_new(L); @@ -608,96 +882,253 @@ void luaV_execute (lua_State *L) { if (b != 0 || c != 0) luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); checkGC(L, ra + 1); - ) - vmcase(OP_SELF, + vmbreak; + } + vmcase(OP_SELF) { + const TValue *aux; StkId rb = RB(i); - setobjs2s(L, ra+1, rb); - Protect(luaV_gettable(L, rb, RKC(i), ra)); - ) - vmcase(OP_ADD, - arith_op(luai_numadd, TM_ADD); - ) - vmcase(OP_SUB, - arith_op(luai_numsub, TM_SUB); - ) - vmcase(OP_MUL, - arith_op(luai_nummul, TM_MUL); - ) - vmcase(OP_DIV, - arith_op(luai_numdiv, TM_DIV); - ) - vmcase(OP_MOD, - arith_op(luai_nummod, TM_MOD); - ) - vmcase(OP_POW, - arith_op(luai_numpow, TM_POW); - ) - vmcase(OP_UNM, + TValue *rc = RKC(i); + TString *key = tsvalue(rc); /* key must be a string */ + setobjs2s(L, ra + 1, rb); + if (luaV_fastget(L, rb, key, aux, luaH_getstr)) { + setobj2s(L, ra, aux); + } + else Protect(luaV_finishget(L, rb, rc, ra, aux)); + vmbreak; + } + vmcase(OP_ADD) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(+, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numadd(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } + vmbreak; + } + vmcase(OP_SUB) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(-, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numsub(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } + vmbreak; + } + vmcase(OP_MUL) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(*, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_nummul(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } + vmbreak; + } + vmcase(OP_DIV) { /* float division (always with floats) */ + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numdiv(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } + vmbreak; + } + vmcase(OP_BAND) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(&, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } + vmbreak; + } + vmcase(OP_BOR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(|, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } + vmbreak; + } + vmcase(OP_BXOR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(^, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } + vmbreak; + } + vmcase(OP_SHL) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, luaV_shiftl(ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } + vmbreak; + } + vmcase(OP_SHR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, luaV_shiftl(ib, -ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } + vmbreak; + } + vmcase(OP_MOD) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, luaV_mod(L, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + lua_Number m; + luai_nummod(L, nb, nc, m); + setfltvalue(ra, m); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } + vmbreak; + } + vmcase(OP_IDIV) { /* floor division */ + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, luaV_div(L, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numidiv(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } + vmbreak; + } + vmcase(OP_POW) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numpow(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } + vmbreak; + } + vmcase(OP_UNM) { TValue *rb = RB(i); - if (ttisnumber(rb)) { - lua_Number nb = nvalue(rb); - setnvalue(ra, luai_numunm(L, nb)); + lua_Number nb; + if (ttisinteger(rb)) { + lua_Integer ib = ivalue(rb); + setivalue(ra, intop(-, 0, ib)); + } + else if (tonumber(rb, &nb)) { + setfltvalue(ra, luai_numunm(L, nb)); } else { - Protect(luaV_arith(L, ra, rb, rb, TM_UNM)); + Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); } - ) - vmcase(OP_NOT, + vmbreak; + } + vmcase(OP_BNOT) { + TValue *rb = RB(i); + lua_Integer ib; + if (tointeger(rb, &ib)) { + setivalue(ra, intop(^, ~l_castS2U(0), ib)); + } + else { + Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); + } + vmbreak; + } + vmcase(OP_NOT) { TValue *rb = RB(i); int res = l_isfalse(rb); /* next assignment may change this value */ setbvalue(ra, res); - ) - vmcase(OP_LEN, + vmbreak; + } + vmcase(OP_LEN) { Protect(luaV_objlen(L, ra, RB(i))); - ) - vmcase(OP_CONCAT, + vmbreak; + } + vmcase(OP_CONCAT) { int b = GETARG_B(i); int c = GETARG_C(i); StkId rb; L->top = base + c + 1; /* mark the end of concat operands */ Protect(luaV_concat(L, c - b + 1)); - ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ - rb = b + base; + 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 */ - ) - vmcase(OP_JMP, + vmbreak; + } + vmcase(OP_JMP) { + lua_checksig(L); dojump(ci, i, 0); - ) - vmcase(OP_EQ, + vmbreak; + } + vmcase(OP_EQ) { TValue *rb = RKB(i); TValue *rc = RKC(i); Protect( - if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i)) + if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_LT, + vmbreak; + } + vmcase(OP_LT) { Protect( if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_LE, + vmbreak; + } + vmcase(OP_LE) { Protect( if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_TEST, + vmbreak; + } + vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) ci->u.l.savedpc++; else donextjump(ci); - ) - vmcase(OP_TESTSET, + vmbreak; + } + vmcase(OP_TESTSET) { TValue *rb = RB(i); if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) ci->u.l.savedpc++; @@ -705,27 +1136,32 @@ void luaV_execute (lua_State *L) { setobjs2s(L, ra, rb); donextjump(ci); } - ) - vmcase(OP_CALL, + vmbreak; + } + vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; + lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (luaD_precall(L, ra, nresults)) { /* C function? */ - if (nresults >= 0) L->top = ci->top; /* adjust results */ - base = ci->u.l.base; + if (nresults >= 0) + L->top = ci->top; /* adjust results */ + Protect((void)0); /* update 'base' */ } else { /* Lua function */ ci = L->ci; - ci->callstatus |= CIST_REENTRY; goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcase(OP_TAILCALL, + 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? */ - base = ci->u.l.base; + if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ + Protect((void)0); /* update 'base' */ + } else { /* tail call: put called frame (n) in place of caller one (o) */ CallInfo *nci = L->ci; /* called frame */ @@ -745,16 +1181,16 @@ void luaV_execute (lua_State *L) { oci->u.l.savedpc = nci->u.l.savedpc; oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); + lua_assert(L->top == oci->u.l.base + getproto(ofunc)->sp->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcasenb(OP_RETURN, + vmbreak; + } + vmcase(OP_RETURN) { int b = GETARG_B(i); - if (b != 0) L->top = ra+b-1; if (cl->p->sp->sizep > 0) luaF_close(L, base); - b = luaD_poscall(L, ra); - if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ + b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); + if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ ci = L->ci; @@ -763,105 +1199,139 @@ void luaV_execute (lua_State *L) { lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcase(OP_FORLOOP, - lua_Number step = nvalue(ra+2); - lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */ - lua_Number limit = nvalue(ra+1); - if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit) - : luai_numle(L, limit, idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - setnvalue(ra, idx); /* update internal index... */ - setnvalue(ra+3, idx); /* ...and external index */ + } + vmcase(OP_FORLOOP) { + 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 */ + } + } + 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 */ + } + } + 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)); } - ) - vmcase(OP_FORPREP, - const TValue *init = ra; - const TValue *plimit = ra+1; - const TValue *pstep = ra+2; - if (!tonumber(init, ra)) - luaG_runerror(L, LUA_QL("for") " initial value must be a number"); - else if (!tonumber(plimit, ra+1)) - luaG_runerror(L, LUA_QL("for") " limit must be a number"); - else if (!tonumber(pstep, ra+2)) - luaG_runerror(L, LUA_QL("for") " step must be a number"); - setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep))); ci->u.l.savedpc += GETARG_sBx(i); - ) - vmcasenb(OP_TFORCALL, + vmbreak; + } + vmcase(OP_TFORCALL) { StkId cb = ra + 3; /* call base */ setobjs2s(L, cb+2, ra+2); setobjs2s(L, cb+1, ra+1); setobjs2s(L, cb, ra); L->top = cb + 3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i), 1)); + Protect(luaD_call(L, cb, GETARG_C(i))); L->top = ci->top; i = *(ci->u.l.savedpc++); /* go to next instruction */ ra = RA(i); lua_assert(GET_OPCODE(i) == OP_TFORLOOP); goto l_tforloop; - ) - vmcase(OP_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 */ } - ) - vmcase(OP_SETLIST, + vmbreak; + } + vmcase(OP_SETLIST) { int n = GETARG_B(i); int c = GETARG_C(i); - int last; + unsigned int last; Table *h; if (n == 0) n = cast_int(L->top - ra) - 1; if (c == 0) { lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); c = GETARG_Ax(*ci->u.l.savedpc++); } - luai_runtimecheck(L, ttistable(ra)); h = hvalue(ra); last = ((c-1)*LFIELDS_PER_FLUSH) + n; if (last > h->sizearray) /* needs more space? */ - luaH_resizearray(L, h, last); /* pre-allocate it at once */ + luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { TValue *val = ra+n; luaH_setint(L, h, last--, val); - luaC_barrierback(L, obj2gco(h), val); + luaC_barrierback(L, h, val); } L->top = ci->top; /* correct top (in case of previous open call) */ - ) - vmcase(OP_CLOSURE, + vmbreak; + } + vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; - Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */ + LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ if (ncl == NULL) /* no match? */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ else setclLvalue(L, ra, ncl); /* push cashed closure */ checkGC(L, ra + 1); - ) - vmcase(OP_VARARG, - int b = GETARG_B(i) - 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; + if (n < 0) /* less arguments than parameters? */ + n = 0; /* no vararg arguments */ if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); ra = RA(i); /* previous call may change the stack */ L->top = ra + n; } - for (j = 0; j < b; j++) { - if (j < n) { - setobjs2s(L, ra + j, base - n + j); - } - else { - setnilvalue(ra + j); - } - } - ) - vmcase(OP_EXTRAARG, + for (j = 0; j < b && j < n; j++) + setobjs2s(L, ra + j, base - n + j); + for (; j < b; j++) /* complete required results with nil */ + setnilvalue(ra + j); + vmbreak; + } + vmcase(OP_EXTRAARG) { lua_assert(0); - ) + vmbreak; + } } } } +/* }================================================================== */ + diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 5380270d..422f8719 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -13,32 +13,101 @@ #include "ltm.h" -#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) - -#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) - -#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) - -#define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) +#if !defined(LUA_NOCVTN2S) +#define cvt2str(o) ttisnumber(o) +#else +#define cvt2str(o) 0 /* no conversion from numbers to strings */ +#endif -/* not to called directly */ -LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); +#if !defined(LUA_NOCVTS2N) +#define cvt2num(o) ttisstring(o) +#else +#define cvt2num(o) 0 /* no conversion from strings to numbers */ +#endif +/* +** You can define LUA_FLOORN2I if you want to convert floats to integers +** by flooring them (instead of raising an error if they are not +** integral values) +*/ +#if !defined(LUA_FLOORN2I) +#define LUA_FLOORN2I 0 +#endif + + +#define tonumber(o,n) \ + (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) + +#define tointeger(o,i) \ + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + +#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) + +#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) + + +/* +** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, +** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, +** return 0 (meaning it will have to check metamethod) with 'slot' +** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). +** 'f' is the raw get function to use. +*/ +#define luaV_fastget(L,t,k,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? */ + +/* +** standard implementation for 'gettable' +*/ +#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); } + + +/* +** Fast track for set table. If 't' is a table and 't[k]' is not nil, +** call GC barrier, do a raw 't[k]=v', and return true; otherwise, +** return false with 'slot' equal to NULL (if 't' is not a table) or +** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro +** returns true, there is no need to 'invalidateTMcache', because the +** call is not creating a new entry. +*/ +#define luaV_fastset(L,t,k,slot,f,v) \ + (!ttistable(t) \ + ? (slot = NULL, 0) \ + : (slot = f(hvalue(t), k), \ + ttisnil(slot) ? 0 \ + : (luaC_barrierback(L, hvalue(t), v), \ + setobj2t(L, cast(TValue *,slot), v), \ + 1))) + + +#define luaV_settable(L,t,k,v) { const TValue *slot; \ + if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ + luaV_finishset(L,t,k,v,slot); } + + + +LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); -LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); -LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, - StkId val); +LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); +LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); +LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *slot); +LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op); +LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); #endif diff --git a/3rd/lua/lzio.c b/3rd/lua/lzio.c index 20efea98..c9e1f491 100644 --- a/3rd/lua/lzio.c +++ b/3rd/lua/lzio.c @@ -1,15 +1,17 @@ /* -** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ - -#include - #define lzio_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "llimits.h" @@ -64,13 +66,3 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) { return 0; } -/* ------------------------------------------------------------------------ */ -char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { - if (n > buff->buffsize) { - if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; - luaZ_resizebuffer(L, buff, n); - } - return buff->buffer; -} - - diff --git a/3rd/lua/lzio.h b/3rd/lua/lzio.h index 441f7479..e7b6f34b 100644 --- a/3rd/lua/lzio.h +++ b/3rd/lua/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ @@ -32,20 +32,21 @@ typedef struct Mbuffer { #define luaZ_sizebuffer(buff) ((buff)->buffsize) #define luaZ_bufflen(buff) ((buff)->n) +#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) #define luaZ_resetbuffer(buff) ((buff)->n = 0) #define luaZ_resizebuffer(L, buff, size) \ - (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ + ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ + (buff)->buffsize, size), \ (buff)->buffsize = size) #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) -LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ +LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ @@ -55,7 +56,7 @@ struct Zio { size_t n; /* bytes still unread */ const char *p; /* current position in buffer */ lua_Reader reader; /* reader function */ - void* data; /* additional data */ + void *data; /* additional data */ lua_State *L; /* Lua state (for reader) */ }; diff --git a/HISTORY.md b/HISTORY.md index 07a4c501..3b7ea80e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,333 @@ -Dev version +v1.0.0 (2016-7-11) ----------- -* Bugfix: update lua-bson (signed 32bit int bug) +* Version 1.0.0 Released + +v1.0.0-rc5 (2016-7-4) +----------- +* MongoDB : Support auth_scram_sha1 +* MongoDB : Auto determine primary host +* Bugfix : memory leak in multicast +* Bugfix : Lua 5.3.3 +* Bson : support meta array + +v1.0.0-rc4 (2016-6-13) +----------- +* Update lua to 5.3.3 +* Update jemalloc to 4.2.1 +* Add debug console command ping +* Lua bson support __pairs +* Add mongo.createIndexes and fix bug in old mongo.createIndex +* Handle signal HUP to reopen log file (for logrotate) + +v1.0.0-rc3 (2016-5-9) +----------- +* Update jemalloc 4.1.1 +* Update lua 5.3.3 rc1 +* Update sproto to support encoding empty table +* Make skynet.init stable (keep order) +* skynet.getenv can return empty string +* Add lua VM memory warning +* lua VM support memory limit +* skynet.pcall suport varargs +* Bugfix : Global name query +* Bugfix : snax.queryglobal + +v1.0.0-rc2 (2016-3-7) +----------- +* Fix a bug in lua 5.3.2 +* Update sproto (fix bugs and add ud for package) +* Fix a bug in http +* Fix a bug in harbor +* Fix a bug in socket channel +* Enhance remote debugger + +v1.0.0-rc (2015-12-28) +----------- +* Update to lua 5.3.2 +* Add skynet.coroutine lib +* Add new debug api to show c memory used +* httpc can use async dns query +* Redis driver support pipeline +* socket.send support string table, and rewrite redis driver +* socket.shutdown would abandon unsend buffer +* Improve some sproto api +* c memory doesn't count the memory allocated by lua vm +* some other bugfix (In multicast, socketchannel, etc) + +v1.0.0-beta (2015-11-10) +----------- +* Improve and fix bug for sproto +* Add global short string pool for lua vm +* Add code cache mode +* Add a callback for mysql auth +* Add hmac_md5 +* Sharedata support filename as a string +* Fix a bug in socket.httpc +* Fix a lua stack overflow bug in lua bson +* Fix a socketchannel bug may block the data steam +* Avoid dead loop when sending message to the service exiting +* Fix memory leak in netpack +* Improve DH key exchange implement +* Minor fix for socket +* Minor fix for multicast +* Update jemalloc to 4.0.4 +* Update lpeg to 1.0.0 + +v1.0.0-alpha10 (2015-8-17) +----------- +* Remove the size limit of cluster RPC message. +* Remove the size limit of local message. +* Add cluster.query and clsuter.register. +* Add an option of pthread mutex lock. +* Add skynet.core.intcommand to optimize skynet.sleep etc. +* Fix a memory leak bug in lua shared proto. +* snax.msgserver use string instead of lightuserdata/size. +* Remove some unused api in netpack. +* Raise error when skynet.send to 0. + +v1.0.0-alpha9 (2015-8-10) +----------- +* Improve lua serialization , support pairs metamethod. +* Bugfix : sproto (See commits log of sproto) +* Add user log service support (In config) +* Other minor bugfix (See commits log) + +v1.0.0-alpha8 (2015-6-29) +----------- +* Update lua 5.3.1 +* Bugfix: skynet exit issue +* Bugfix: timer race condition +* Use atom increment in bson object id +* remove assert when write to a listen fd +* sproto encode doesn't use raw table api + +v1.0.0-alpha7 (2015-6-8) +----------- +* console support launch snax service +* Add cluster.snax +* Add nodelay in clusterd +* Merge sproto bugfix patch +* Move some skynet api into skynet.manager +* DNS support underscore +* Add logservice in config file for user defined log service +* skynet.fork returns coroutine +* Fix a few of bugs , see the commits log + +v1.0.0-alpha6 (2015-5-18) +----------- +* bugfix: httpc.get +* bugfix: seri lib stack overflow +* bugfix: udp send +* bugfix: udp address +* bugfix: sproto dump +* add: sproto default +* improve: skynet.wakeup (can wakeup skynet.call by raise an error) +* improve: skynet.exit (raise error when uncall response) +* remove: task overload warning +* move: some skynet api move into skynet.manager + +v1.0.0-alpha5 (2015-4-27) +----------- +* merge lua 5.3 offical bugfix +* improve sproto rpc api +* fix a deadlock bug when service retire +* improve cluster config reload +* add skynet.pcall for calling a function with `require` +* better error log in loginserver + +v1.0.0-alpha4 (2015-4-13) +----------- +* sproto can share c struct between states +* udp api changed (use lua string now) +* fix memory leak in dns module + +v1.0.0-alpha3 (2015-3-30) +----------- +* Update sproto (bugfix) +* Add async dns query +* improve httpc + +v1.0.0-alpha2 (2015-3-16) +----------- +* Update examples client to lua 5.3 +* Patch lua 5.3 to interrupt the dead loop (for debug) +* Update sproto (fix some bugs and support unordered map) + +v1.0.0-alpha (2015-3-9) +----------- +* Update lua from 5.2 to 5.3 +* Add an online lua debugger +* Add sharemap as an example use case of stm +* Improve sproto for multi-state +* Improve mongodb driver +* Fix known bugs + +v0.9.3 (2015-1-5) +----------- +* Add : mongo createIndex +* Update : sproto +* bugfix : sharedata check dirty flag when len/pairs metamethod +* bugfix : multicast + +v0.9.2 (2014-12-8) +----------- +* Simplify the message queue +* Add create_index in mongo driver +* Fix a bug in big-endian architecture (sproto) + +v0.9.0 / v0.9.1 (2014-11-17) +----------- +* Add UDP support +* Add IPv6 support +* socket send package can define a release method +* dispatch read before write in epoll +* remove snax queue mode +* Fix a bug in big-endian architecture + +v0.8.1 (2014-11-3) +----------- +* Send to an invalid remote service will raise an error +* Bugifx: socket open address string +* Remove sha1 from mysqlaux +* merge lua and sproto bugfix , use crypt lib instead +* Fix a memory leak in socket +* minor bugfix in http module + +v0.8.0 (2014-10-27) +----------- +* Add mysql client driver +* Bugfix : skynet.queue + +v0.7.4 (2014-10-13) +----------- +* Bugfix : clear coroutine pool when GC +* hotfix : A bug introduce by 0.7.3 + +v0.7.3 (2014-10-13) +----------- +* Add some logs (warning) when overload +* Bugfix: crash on exit + +v0.7.2 (2014-9-29) +----------- +* Bugfix : datacenter.wait +* Bugfix : error in forker coroutine +* Add skynet.term +* Accept socket report port +* sharedata can be update more than once + +v0.7.1 (2014-9-22) +----------- +* bugfix: wakeup sleep should return BREAK +* bugfix: sharedatad load string +* bugfix: dataserver forward error msg + +v0.7.0 (2014-9-8) +----------- +* Use sproto instead of cjson +* Add message logger +* Add hmac-sha1 +* Some minor bugfix + +v0.6.2 (2014-9-1) +----------- +* bugfix: only skynet.call response PTYPE_ERROR + +v0.6.1 (2014-8-25) +----------- +* bugfix: datacenter.wakeup +* change struct msg name to avoid conflict in mac +* improve seri library + +v0.6.0 (2014-8-18) +----------- +* add sharedata +* bugfix: service exit before init would not report back +* add skynet.response and check multicall skynet.ret +* skynet.newservice throw error when lanuch faild +* Don't check imported function in snax.hotfix +* snax service add change SERVICE_PATH and add it to package.path +* skynet.redirect support string address +* bugfix: skynet.harbor.link may block +* add skynet.harbor.queryname to query globalname +* add cluster.proxy +* add DEBUG command exit (send a message to lua service by DEBUG) +* add DEBUG command run (debug_console command inject) +* bugfix : socketchannel connect once +* bugfix : mongo driver + +v0.5.2 (2014-8-11) +----------- +* Bugfix : httpd request +* Bugifx : http chunked mode +* Add : httpc +* timer support more than 497 days + +v0.5.1 (2014-8-4) +----------- +* Bugfix : http module +* Bugfix : multicast local channel delete +* Bugfix : socket.read(fd) + +v0.5.0 (2014-7-28) +----------- +* skynet.exit will quit service immediately. +* Add snax.gateserver, snax.loginserver, snax.msgserver +* Simplify clientsocket lib +* mongo driver support replica set +* config file support read from ENV +* add simple httpd (see examples/simpleweb.lua) + +v0.4.2 (2014-7-14) +----------- +* Bugfix : invalid negative socket id +* Add optional TCP_NODELAY support +* Add worker thread weight +* Add skynet.queue +* Bugfix: socketchannel +* cluster can throw error +* Add readline and writeline to clientsocket lib +* Add cluster.reload to reload config file +* Add datacenter.wait + +v0.4.1 (2014-7-7) +----------- +* Add SERVICE_NAME in loader +* Throw error back when skynet.error +* Add skynet.task +* Bugfix for last version (harbor service bugs) + +v0.4.0 (2014-6-30) +----------- +* Optimize redis driver `compose_message`. +* Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . +* cluster.open support cluster name. +* Add new api skynet.packstring , and skynet.unpack support lua string +* socket.listen support put port into address. (address:port) +* Redesign harbor/master/dummy, remove lots of C code and rewite in lua. +* Remove block connect api, queue sending message during connecting now. +* Add skynet.time() + +v0.3.2 (2014-6-23) +---------- +* Bugfix : cluster (double free). +* Add socket.header() to decode big-endian package header (and fix the bug in cluster). + +v0.3.1 (2014-6-16) +----------- +* Bugfix: lua mongo driver . Hold reply string before decode bson data. +* More check in bson decoding. +* Use big-endian for encoding bson objectid. + +v0.3.0 (2014-6-2) +----------- +* Add cluster support +* Add single node mode +* Add daemon mode +* Bugfix: update lua-bson (signed 32bit int bug / check string length) +* Optimize timer +* Simplify message queue and optimize message dispatch +* Use jemalloc release 3.6.0 v0.2.1 (2014-5-19) ----------- diff --git a/LICENSE b/LICENSE index a777a3f0..0650949a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2014 codingnow.com +Copyright (c) 2012-2015 codingnow.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/Makefile b/Makefile index d7bb53cf..4f8b17a7 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ CSERVICE_PATH ?= cservice SKYNET_BUILD_PATH ?= . -CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) +CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) +# CFLAGS += -DUSE_PTHREAD_LOCK # lua @@ -14,7 +15,7 @@ LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua $(LUA_STATICLIB) : - cd 3rd/lua && $(MAKE) CC=$(CC) $(PLAT) + cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # jemalloc @@ -23,7 +24,7 @@ JEMALLOC_INC := 3rd/jemalloc/include/jemalloc all : jemalloc -.PHONY : jemalloc +.PHONY : jemalloc update3rd MALLOC_STATICLIB := $(JEMALLOC_STATICLIB) @@ -38,15 +39,37 @@ $(JEMALLOC_STATICLIB) : 3rd/jemalloc/Makefile jemalloc : $(MALLOC_STATICLIB) +update3rd : + rm -rf 3rd/jemalloc && git submodule update --init + # skynet -CSERVICE = snlua logger gate master harbor -LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack cjson clientsocket memory profile multicast +CSERVICE = snlua logger gate harbor +LUA_CLIB = skynet \ + client \ + bson md5 sproto lpeg + +LUA_CLIB_SKYNET = \ + lua-skynet.c lua-seri.c \ + lua-socket.c \ + lua-mongo.c \ + lua-netpack.c \ + lua-memory.c \ + lua-profile.c \ + lua-multicast.c \ + lua-cluster.c \ + lua-crypt.c lsha1.c \ + lua-sharedata.c \ + lua-stm.c \ + lua-mysqlaux.c \ + lua-debugchannel.c \ + lua-datasheet.c \ + \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ - malloc_hook.c + malloc_hook.c skynet_daemon.c skynet_log.c all : \ $(SKYNET_BUILD_PATH)/skynet \ @@ -64,52 +87,36 @@ $(CSERVICE_PATH) : define CSERVICE_TEMP $$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH) - $(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src + $$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src endef $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) -$(LUA_CLIB_PATH)/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.so : $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src -$(LUA_CLIB_PATH)/socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src - -$(LUA_CLIB_PATH)/int64.so : 3rd/lua-int64/int64.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src - -$(LUA_CLIB_PATH)/mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -Iskynet-src $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ -$(LUA_CLIB_PATH)/netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@ - -$(LUA_CLIB_PATH)/cjson.so : | $(LUA_CLIB_PATH) - cd 3rd/lua-cjson && $(MAKE) LUA_INCLUDE_DIR=../../$(LUA_INC) CC=$(CC) CJSON_LDFLAGS="$(SHARED)" && cd ../.. && cp 3rd/lua-cjson/cjson.so $@ - -$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread -$(LUA_CLIB_PATH)/memory.so : lualib-src/lua-memory.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ -$(LUA_CLIB_PATH)/profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - -$(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so cleanall: clean - cd 3rd/lua-cjson && $(MAKE) clean - cd 3rd/jemalloc && $(MAKE) clean +ifneq (,$(wildcard 3rd/jemalloc/Makefile)) + cd 3rd/jemalloc && $(MAKE) clean && rm Makefile +endif + cd 3rd/lua && $(MAKE) clean rm -f $(LUA_STATICLIB) diff --git a/README.md b/README.md index 2b5e625d..125a8f15 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,42 @@ +## Skynet + +Skynet is a lightweight online game framework, and it can be used in many other fields. + ## Build -Install autoconf first for jemalloc +For Linux, install autoconf first for jemalloc: ``` -git clone git@github.com:cloudwu/skynet.git +git clone https://github.com/cloudwu/skynet.git cd skynet make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now ``` -Or you can : +Or you can: ``` export PLAT=linux make ``` +For FreeBSD , use gmake instead of make. + ## Test -Run these in different console +Run these in different consoles: ``` ./skynet examples/config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) -lua examples/client.lua # Launch a client, and try to input some words. +./3rd/lua/lua examples/client.lua # Launch a client, and try to input hello. ``` -## About Lua +## About Lua version -Skynet put a modified version of lua 5.2.3 in 3rd/lua , it can share proto type between lua state (http://lua-users.org/lists/lua-l/2014-03/msg00489.html) . +Skynet now uses a modified version of lua 5.3.3 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. -Each lua file only load once and cache it in memory during skynet start . so if you want to reflush the cache , call skynet.cache.clear() . +You can also use official Lua versions, just edit the Makefile by yourself. -You can also use the offical lua version , edit the makefile by yourself . +## How To Use (Sorry, Only in Chinese now) -## Blog (in Chinese) - -* http://blog.codingnow.com/2012/09/the_design_of_skynet.html -* http://blog.codingnow.com/2012/08/skynet.html -* http://blog.codingnow.com/2012/08/skynet_harbor_rpc.html -* http://blog.codingnow.com/eo/skynet/ +* Read Wiki for documents https://github.com/cloudwu/skynet/wiki +* The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ diff --git a/service/abort.lua b/examples/abort.lua similarity index 50% rename from service/abort.lua rename to examples/abort.lua index 755675a6..305cfdb5 100644 --- a/service/abort.lua +++ b/examples/abort.lua @@ -1,3 +1,4 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.abort skynet.abort() diff --git a/examples/agent.lua b/examples/agent.lua index 2f610063..3e164bb8 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -1,40 +1,93 @@ local skynet = require "skynet" -local jsonpack = require "jsonpack" -local netpack = require "netpack" -local socket = require "socket" +local netpack = require "skynet.netpack" +local socket = require "skynet.socket" +local sproto = require "sproto" +local sprotoloader = require "sprotoloader" + +local WATCHDOG +local host +local send_request local CMD = {} - +local REQUEST = {} local client_fd -local function send_client(v) - socket.write(client_fd, netpack.pack(jsonpack.pack(0, {true, v}))) +function REQUEST:get() + print("get", self.what) + local r = skynet.call("SIMPLEDB", "lua", "get", self.what) + return { result = r } end -local function response_client(session,v) - socket.write(client_fd, netpack.pack(jsonpack.response(session,v))) +function REQUEST:set() + print("set", self.what, self.value) + local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value) +end + +function REQUEST:handshake() + return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." } +end + +function REQUEST:quit() + skynet.call(WATCHDOG, "lua", "close", client_fd) +end + +local function request(name, args, response) + local f = assert(REQUEST[name]) + local r = f(args) + if response then + return response(r) + end +end + +local function send_package(pack) + local package = string.pack(">s2", pack) + socket.write(client_fd, package) end skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function (msg, sz) - return jsonpack.unpack(skynet.tostring(msg,sz)) + return host:dispatch(msg, sz) end, - dispatch = function (_, _, session, args) - local ok, result = pcall(skynet.call,"SIMPLEDB", "lua", table.unpack(args)) - if ok then - response_client(session, { true, result }) + dispatch = function (_, _, type, ...) + if type == "REQUEST" then + local ok, result = pcall(request, ...) + if ok then + if result then + send_package(result) + end + else + skynet.error(result) + end else - response_client(session, { false, "Invalid command" }) + assert(type == "RESPONSE") + error "This example doesn't support request client" end end } -function CMD.start(gate , fd) +function CMD.start(conf) + local fd = conf.client + local gate = conf.gate + WATCHDOG = conf.watchdog + -- slot 1,2 set at main.lua + host = sprotoloader.load(1):host "package" + send_request = host:attach(sprotoloader.load(2)) + skynet.fork(function() + while true do + send_package(send_request "heartbeat") + skynet.sleep(500) + end + end) + client_fd = fd skynet.call(gate, "lua", "forward", fd) - send_client "Welcome to skynet" +end + +function CMD.disconnect() + -- todo: do something before exit + skynet.exit() end skynet.start(function() diff --git a/examples/checkdeadloop.lua b/examples/checkdeadloop.lua new file mode 100644 index 00000000..46f395c1 --- /dev/null +++ b/examples/checkdeadloop.lua @@ -0,0 +1,30 @@ +local skynet = require "skynet" + +local list = {} + +local function timeout_check(ti) + if not next(list) then + return + end + skynet.sleep(ti) -- sleep 10 sec + for k,v in pairs(list) do + skynet.error("timout",ti,k,v) + end +end + +skynet.start(function() + skynet.error("ping all") + local list_ret = skynet.call(".launcher", "lua", "LIST") + for addr, desc in pairs(list_ret) do + list[addr] = desc + skynet.fork(function() + skynet.call(addr,"debug","INFO") + list[addr] = nil + end) + end + skynet.sleep(0) + timeout_check(100) + timeout_check(400) + timeout_check(500) + skynet.exit() +end) diff --git a/examples/client.lua b/examples/client.lua index 9197a1c3..740732f8 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -1,50 +1,115 @@ package.cpath = "luaclib/?.so" +package.path = "lualib/?.lua;examples/?.lua" -local socket = require "clientsocket" -local cjson = require "cjson" +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end -local fd = socket.connect("127.0.0.1", 8888) +local socket = require "client.socket" +local proto = require "proto" +local sproto = require "sproto" -local last -local result = {} +local host = sproto.new(proto.s2c):host "package" +local request = host:attach(sproto.new(proto.c2s)) -local function dispatch() - while true do - local status - status, last = socket.recv(fd, last, result) - if status == nil then - error "Server closed" - end - if not status then - break - end - for _, v in ipairs(result) do - local session,t,str = string.match(v, "(%d+)(.)(.*)") - assert(t == '-' or t == '+') - session = tonumber(session) - local result = cjson.decode(str) - print("Response:",session, result[1], result[2]) - end +local fd = assert(socket.connect("127.0.0.1", 8888)) + +local function send_package(fd, pack) + local package = string.pack(">s2", pack) + socket.send(fd, package) +end + +local function unpack_package(text) + local size = #text + if size < 2 then + return nil, text end + local s = text:byte(1) * 256 + text:byte(2) + if size < s+2 then + return nil, text + end + + return text:sub(3,2+s), text:sub(3+s) +end + +local function recv_package(last) + local result + result, last = unpack_package(last) + if result then + return result, last + end + local r = socket.recv(fd) + if not r then + return nil, last + end + if r == "" then + error "Server closed" + end + return unpack_package(last .. r) end local session = 0 -local function send_request(v) +local function send_request(name, args) session = session + 1 - local str = string.format("%d+%s",session, cjson.encode(v)) - socket.send(fd, str) + local str = request(name, args, session) + send_package(fd, str) print("Request:", session) end +local last = "" + +local function print_request(name, args) + print("REQUEST", name) + if args then + for k,v in pairs(args) do + print(k,v) + end + end +end + +local function print_response(session, args) + print("RESPONSE", session) + if args then + for k,v in pairs(args) do + print(k,v) + end + end +end + +local function print_package(t, ...) + if t == "REQUEST" then + print_request(...) + else + assert(t == "RESPONSE") + print_response(...) + end +end + +local function dispatch_package() + while true do + local v + v, last = recv_package(last) + if not v then + break + end + + print_package(host:dispatch(v)) + end +end + +send_request("handshake") +send_request("set", { what = "hello", value = "world" }) while true do - dispatch() - local cmd = socket.readline() + dispatch_package() + local cmd = socket.readstdin() if cmd then - local args = {} - string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end ) - send_request(args) + if cmd == "quit" then + send_request("quit") + else + send_request("get", { what = cmd }) + end else socket.usleep(100) end -end \ No newline at end of file +end diff --git a/examples/cluster1.lua b/examples/cluster1.lua new file mode 100644 index 00000000..79665826 --- /dev/null +++ b/examples/cluster1.lua @@ -0,0 +1,24 @@ +local skynet = require "skynet" +local cluster = require "skynet.cluster" +local snax = require "skynet.snax" + +skynet.start(function() + cluster.reload { + db = "127.0.0.1:2528", + db2 = "127.0.0.1:2529", + } + + local sdb = skynet.newservice("simpledb") + -- register name "sdb" for simpledb, you can use cluster.query() later. + -- See cluster2.lua + cluster.register("sdb", sdb) + + print(skynet.call(sdb, "lua", "SET", "a", "foobar")) + print(skynet.call(sdb, "lua", "SET", "b", "foobar2")) + print(skynet.call(sdb, "lua", "GET", "a")) + print(skynet.call(sdb, "lua", "GET", "b")) + cluster.open "db" + cluster.open "db2" + -- unique snax service + snax.uniqueservice "pingserver" +end) diff --git a/examples/cluster2.lua b/examples/cluster2.lua new file mode 100644 index 00000000..f356357c --- /dev/null +++ b/examples/cluster2.lua @@ -0,0 +1,23 @@ +local skynet = require "skynet" +local cluster = require "skynet.cluster" + +skynet.start(function() + -- query name "sdb" of cluster db. + local sdb = cluster.query("db", "sdb") + print("db.sbd=",sdb) + local proxy = cluster.proxy("db", sdb) + local largekey = string.rep("X", 128*1024) + local largevalue = string.rep("R", 100 * 1024) + print(skynet.call(proxy, "lua", "SET", largekey, largevalue)) + local v = skynet.call(proxy, "lua", "GET", largekey) + assert(largevalue == v) + skynet.send(proxy, "lua", "PING", "proxy") + + print(cluster.call("db", sdb, "GET", "a")) + print(cluster.call("db2", sdb, "GET", "b")) + cluster.send("db2", sdb, "PING", "db2:longstring" .. largevalue) + + -- test snax service + local pingserver = cluster.snax("db", "pingserver") + print(pingserver.req.ping "hello") +end) diff --git a/examples/clustername.lua b/examples/clustername.lua new file mode 100644 index 00000000..681245a1 --- /dev/null +++ b/examples/clustername.lua @@ -0,0 +1,2 @@ +db = "127.0.0.1:2528" +db2 = "127.0.0.1:2529" diff --git a/examples/config b/examples/config index f89af28b..99e7af5f 100644 --- a/examples/config +++ b/examples/config @@ -1,14 +1,15 @@ -root = "./" +include "config.path" + +-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run thread = 8 logger = nil +logpath = "." harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" -luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" -lualoader = "lualib/loader.lua" --- preload = "./examples/preload.lua" -- run preload.lua before every lua service run -snax = root.."examples/?.lua;"..root.."test/?.lua" +-- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/config.c1 b/examples/config.c1 new file mode 100644 index 00000000..0ebb4e9c --- /dev/null +++ b/examples/config.c1 @@ -0,0 +1,11 @@ +thread = 8 +logger = nil +harbor = 0 +start = "cluster1" +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" +-- use cluster.reload instead, see cluster1.lua +-- cluster = "./examples/clustername.lua" +snax = "./test/?.lua" diff --git a/examples/config.c2 b/examples/config.c2 new file mode 100644 index 00000000..23886105 --- /dev/null +++ b/examples/config.c2 @@ -0,0 +1,10 @@ +thread = 8 +logger = nil +harbor = 0 +start = "cluster2" +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" +cluster = "./examples/clustername.lua" +snax = "./test/?.lua" diff --git a/examples/config.login b/examples/config.login new file mode 100644 index 00000000..c72dcb27 --- /dev/null +++ b/examples/config.login @@ -0,0 +1,8 @@ +thread = 8 +logger = nil +harbor = 0 +start = "main" +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = "./service/?.lua;./examples/login/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" diff --git a/examples/config.mysql b/examples/config.mysql new file mode 100644 index 00000000..8f23a02d --- /dev/null +++ b/examples/config.mysql @@ -0,0 +1,11 @@ +root = "./" +thread = 8 +logger = nil +harbor = 0 +start = "main_mysql" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = "lualib/loader.lua" +snax = root.."examples/?.lua;"..root.."test/?.lua" +cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/config.path b/examples/config.path new file mode 100644 index 00000000..568b345d --- /dev/null +++ b/examples/config.path @@ -0,0 +1,6 @@ +root = "./" +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;"..root.."test/?/init.lua" +lualoader = root .. "lualib/loader.lua" +lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" +lua_cpath = root .. "luaclib/?.so" +snax = root.."examples/?.lua;"..root.."test/?.lua" diff --git a/examples/config.userlog b/examples/config.userlog new file mode 100644 index 00000000..dcd47b7b --- /dev/null +++ b/examples/config.userlog @@ -0,0 +1,15 @@ +root = "./" +thread = 8 +logger = "userlog" +logservice = "snlua" +logpath = "." +harbor = 0 +start = "main" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = "lualib/loader.lua" +-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run +snax = root.."examples/?.lua;"..root.."test/?.lua" +-- snax_interface_g = "snax_g" +cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/globallog.lua b/examples/globallog.lua index ec431cce..e45d33dc 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -1,8 +1,10 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register skynet.start(function() - skynet.dispatch("text", function(session, address, text) - print("[GLOBALLOG]", skynet.address(address),text) + skynet.dispatch("lua", function(session, address, ...) + print("[GLOBALLOG]", skynet.address(address), ...) end) + skynet.register ".log" skynet.register "LOG" end) diff --git a/examples/injectlaunch.lua b/examples/injectlaunch.lua new file mode 100644 index 00000000..4a0028c0 --- /dev/null +++ b/examples/injectlaunch.lua @@ -0,0 +1,19 @@ +if not _P then + print[[ +This file is examples to show how to inject code into lua service. +It is used to inject into launcher service to change the command.LAUNCH to command.LOGLAUNCH. +telnet the debug_console service (nc 127.0.0.1 8000), and run: +inject 3 examples/injectlaunch.lua -- 3 means launcher service +]] + return +end +local command = _P.lua.command + +if command.RAWLAUNCH then + command.LAUNCH, command.RAWLAUNCH = command.RAWLAUNCH + print "restore command.LAUNCH" +else + command.RAWLAUNCH = command.LAUNCH + command.LAUNCH = command.LOGLAUNCH + print "replace command.LAUNCH" +end diff --git a/examples/login/client.lua b/examples/login/client.lua new file mode 100644 index 00000000..4143f941 --- /dev/null +++ b/examples/login/client.lua @@ -0,0 +1,171 @@ +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" +end + +local fd = assert(socket.connect("127.0.0.1", 8001)) + +local function writeline(fd, text) + socket.send(fd, text .. "\n") +end + +local function unpack_line(text) + local from = text:find("\n", 1, true) + if from then + return text:sub(1, from-1), text:sub(from+1) + end + return nil, text +end + +local last = "" + +local function unpack_f(f) + local function try_recv(fd, last) + local result + result, last = f(last) + if result then + return result, last + end + local r = socket.recv(fd) + if not r then + return nil, last + end + if r == "" then + error "Server closed" + end + return f(last .. r) + end + + return function() + while true do + local result + result, last = try_recv(fd, last) + if result then + return result + end + socket.usleep(100) + end + end +end + +local readline = unpack_f(unpack_line) + +local challenge = crypt.base64decode(readline()) + +local clientkey = crypt.randomkey() +writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey))) +local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey) + +print("sceret is ", crypt.hexencode(secret)) + +local hmac = crypt.hmac64(challenge, secret) +writeline(fd, crypt.base64encode(hmac)) + +local token = { + server = "sample", + user = "hello", + pass = "password", +} + +local function encode_token(token) + return string.format("%s@%s:%s", + crypt.base64encode(token.user), + crypt.base64encode(token.server), + crypt.base64encode(token.pass)) +end + +local etoken = crypt.desencode(secret, encode_token(token)) +local b = crypt.base64encode(etoken) +writeline(fd, crypt.base64encode(etoken)) + +local result = readline() +print(result) +local code = tonumber(string.sub(result, 1, 3)) +assert(code == 200) +socket.close(fd) + +local subid = crypt.base64decode(string.sub(result, 5)) + +print("login ok, subid=", subid) + +----- connect to game server + +local function send_request(v, session) + local size = #v + 4 + local package = string.pack(">I2", size)..v..string.pack(">I4", session) + socket.send(fd, package) + return v, session +end + +local function recv_response(v) + local size = #v - 5 + local content, ok, session = string.unpack("c"..tostring(size).."B>I4", v) + return ok ~=0 , content, session +end + +local function unpack_package(text) + local size = #text + if size < 2 then + return nil, text + end + local s = text:byte(1) * 256 + text:byte(2) + if size < s+2 then + return nil, text + end + + return text:sub(3,2+s), text:sub(3+s) +end + +local readpackage = unpack_f(unpack_package) + +local function send_package(fd, pack) + local package = string.pack(">s2", pack) + socket.send(fd, package) +end + +local text = "echo" +local index = 1 + +print("connect") +fd = assert(socket.connect("127.0.0.1", 8888)) +last = "" + +local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) +local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) + + +send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) + +print(readpackage()) +print("===>",send_request(text,0)) +-- don't recv response +-- print("<===",recv_response(readpackage())) + +print("disconnect") +socket.close(fd) + +index = index + 1 + +print("connect again") +fd = assert(socket.connect("127.0.0.1", 8888)) +last = "" + +local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) +local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) + +send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) + +print(readpackage()) +print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake) +print("===>",send_request("again",1)) -- request again (use new session) +print("<===",recv_response(readpackage())) +print("<===",recv_response(readpackage())) + + +print("disconnect") +socket.close(fd) + diff --git a/examples/login/gated.lua b/examples/login/gated.lua new file mode 100644 index 00000000..86fa31fc --- /dev/null +++ b/examples/login/gated.lua @@ -0,0 +1,89 @@ +local msgserver = require "snax.msgserver" +local crypt = require "skynet.crypt" +local skynet = require "skynet" + +local loginservice = tonumber(...) + +local server = {} +local users = {} +local username_map = {} +local internal_id = 0 + +-- login server disallow multi login, so login_handler never be reentry +-- call by login server +function server.login_handler(uid, secret) + if users[uid] then + error(string.format("%s is already login", uid)) + end + + internal_id = internal_id + 1 + local id = internal_id -- don't use internal_id directly + local username = msgserver.username(uid, id, servername) + + -- you can use a pool to alloc new agent + local agent = skynet.newservice "msgagent" + local u = { + username = username, + agent = agent, + uid = uid, + subid = id, + } + + -- trash subid (no used) + skynet.call(agent, "lua", "login", uid, id, secret) + + users[uid] = u + username_map[username] = u + + msgserver.login(username, secret) + + -- you should return unique subid + return id +end + +-- call by agent +function server.logout_handler(uid, subid) + local u = users[uid] + if u then + local username = msgserver.username(uid, subid, servername) + assert(u.username == username) + msgserver.logout(u.username) + users[uid] = nil + username_map[u.username] = nil + skynet.call(loginservice, "lua", "logout",uid, subid) + end +end + +-- call by login server +function server.kick_handler(uid, subid) + local u = users[uid] + if u then + local username = msgserver.username(uid, subid, servername) + assert(u.username == username) + -- NOTICE: logout may call skynet.exit, so you should use pcall. + pcall(skynet.call, u.agent, "lua", "logout") + end +end + +-- call by self (when socket disconnect) +function server.disconnect_handler(username) + local u = username_map[username] + if u then + skynet.call(u.agent, "lua", "afk") + end +end + +-- call by self (when recv a request from client) +function server.request_handler(username, msg) + local u = username_map[username] + return skynet.tostring(skynet.rawcall(u.agent, "client", msg)) +end + +-- call by self (when gate open) +function server.register_handler(name) + servername = name + skynet.call(loginservice, "lua", "register_gate", servername, skynet.self()) +end + +msgserver.start(server) + diff --git a/examples/login/logind.lua b/examples/login/logind.lua new file mode 100644 index 00000000..5c739a8a --- /dev/null +++ b/examples/login/logind.lua @@ -0,0 +1,62 @@ +local login = require "snax.loginserver" +local crypt = require "skynet.crypt" +local skynet = require "skynet" + +local server = { + host = "127.0.0.1", + port = 8001, + multilogin = false, -- disallow multilogin + name = "login_master", +} + +local server_list = {} +local user_online = {} +local user_login = {} + +function server.auth_handler(token) + -- the token is base64(user)@base64(server):base64(password) + local user, server, password = token:match("([^@]+)@([^:]+):(.+)") + user = crypt.base64decode(user) + server = crypt.base64decode(server) + password = crypt.base64decode(password) + assert(password == "password", "Invalid password") + return server, user +end + +function server.login_handler(server, uid, secret) + print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) + local gameserver = assert(server_list[server], "Unknown server") + -- only one can login, because disallow multilogin + local last = user_online[uid] + if last then + skynet.call(last.address, "lua", "kick", uid, last.subid) + end + if user_online[uid] then + error(string.format("user %s is already online", uid)) + end + + local subid = tostring(skynet.call(gameserver, "lua", "login", uid, secret)) + user_online[uid] = { address = gameserver, subid = subid , server = server} + return subid +end + +local CMD = {} + +function CMD.register_gate(server, address) + server_list[server] = address +end + +function CMD.logout(uid, subid) + local u = user_online[uid] + if u then + print(string.format("%s@%s is logout", uid, u.server)) + user_online[uid] = nil + end +end + +function server.command_handler(command, ...) + local f = assert(CMD[command]) + return f(...) +end + +login(server) diff --git a/examples/login/main.lua b/examples/login/main.lua new file mode 100644 index 00000000..1eef6f4d --- /dev/null +++ b/examples/login/main.lua @@ -0,0 +1,12 @@ +local skynet = require "skynet" + +skynet.start(function() + local loginserver = skynet.newservice("logind") + local gate = skynet.newservice("gated", loginserver) + + skynet.call(gate, "lua", "open" , { + port = 8888, + maxclient = 64, + servername = "sample", + }) +end) diff --git a/examples/login/msgagent.lua b/examples/login/msgagent.lua new file mode 100644 index 00000000..d00fd332 --- /dev/null +++ b/examples/login/msgagent.lua @@ -0,0 +1,53 @@ +local skynet = require "skynet" + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, + unpack = skynet.tostring, +} + +local gate +local userid, subid + +local CMD = {} + +function CMD.login(source, uid, sid, secret) + -- you may use secret to make a encrypted data stream + skynet.error(string.format("%s is login", uid)) + gate = source + userid = uid + subid = sid + -- you may load user data from database +end + +local function logout() + if gate then + skynet.call(gate, "lua", "logout", userid, subid) + end + skynet.exit() +end + +function CMD.logout(source) + -- NOTICE: The logout MAY be reentry + skynet.error(string.format("%s is logout", userid)) + logout() +end + +function CMD.afk(source) + -- the connection is broken, but the user may back + skynet.error(string.format("AFK")) +end + +skynet.start(function() + -- If you want to fork a work thread , you MUST do it in CMD.login + skynet.dispatch("lua", function(session, source, command, ...) + local f = assert(CMD[command]) + skynet.ret(skynet.pack(f(source, ...))) + end) + + skynet.dispatch("client", function(_,_, msg) + -- the simple echo service + skynet.sleep(10) -- sleep a while + skynet.ret(msg) + end) +end) diff --git a/examples/main.lua b/examples/main.lua index 8019c2bf..5a2150db 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -1,17 +1,22 @@ local skynet = require "skynet" +local sprotoloader = require "sprotoloader" local max_client = 64 skynet.start(function() - print("Server start") - local console = skynet.newservice("console") + skynet.error("Server start") + skynet.uniqueservice("protoloader") + if not skynet.getenv "daemon" then + local console = skynet.newservice("console") + end skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") skynet.call(watchdog, "lua", "start", { port = 8888, maxclient = max_client, + nodelay = true, }) - + skynet.error("Watchdog listen on", 8888) skynet.exit() end) diff --git a/examples/main_log.lua b/examples/main_log.lua index 8a5194a0..4dc700c4 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -1,10 +1,17 @@ local skynet = require "skynet" +local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.monitor + +local function monitor_master() + harbor.linkmaster() + print("master is down") + skynet.exit() +end skynet.start(function() print("Log server start") - local service = skynet.newservice("service_mgr") skynet.monitor "simplemonitor" local log = skynet.newservice("globallog") - skynet.exit() + skynet.fork(monitor_master) end) diff --git a/examples/main_mysql.lua b/examples/main_mysql.lua new file mode 100644 index 00000000..8a92b2cd --- /dev/null +++ b/examples/main_mysql.lua @@ -0,0 +1,10 @@ +local skynet = require "skynet" + + +skynet.start(function() + print("Main Server start") + local console = skynet.newservice("testmysql") + + print("Main Server exit") + skynet.exit() +end) diff --git a/examples/proto.lua b/examples/proto.lua new file mode 100644 index 00000000..af88a4b5 --- /dev/null +++ b/examples/proto.lua @@ -0,0 +1,46 @@ +local sprotoparser = require "sprotoparser" + +local proto = {} + +proto.c2s = sprotoparser.parse [[ +.package { + type 0 : integer + session 1 : integer +} + +handshake 1 { + response { + msg 0 : string + } +} + +get 2 { + request { + what 0 : string + } + response { + result 0 : string + } +} + +set 3 { + request { + what 0 : string + value 1 : string + } +} + +quit 4 {} + +]] + +proto.s2c = sprotoparser.parse [[ +.package { + type 0 : integer + session 1 : integer +} + +heartbeat 1 {} +]] + +return proto diff --git a/examples/protoloader.lua b/examples/protoloader.lua new file mode 100644 index 00000000..03df570f --- /dev/null +++ b/examples/protoloader.lua @@ -0,0 +1,13 @@ +-- module proto as examples/proto.lua +package.path = "./examples/?.lua;" .. package.path + +local skynet = require "skynet" +local sprotoparser = require "sprotoparser" +local sprotoloader = require "sprotoloader" +local proto = require "proto" + +skynet.start(function() + sprotoloader.save(proto.c2s, 1) + sprotoloader.save(proto.s2c, 2) + -- don't call skynet.exit() , because sproto.core may unload and the global slot become invalid +end) diff --git a/examples/share.lua b/examples/share.lua new file mode 100644 index 00000000..b91e3fc7 --- /dev/null +++ b/examples/share.lua @@ -0,0 +1,82 @@ +local skynet = require "skynet" +local sharedata = require "skynet.sharedata" + +local mode = ... + +if mode == "host" then + +skynet.start(function() + skynet.error("new foobar") + sharedata.new("foobar", { a=1, b= { "hello", "world" } }) + + skynet.fork(function() + skynet.sleep(200) -- sleep 2s + skynet.error("update foobar a = 2") + sharedata.update("foobar", { a =2 }) + skynet.sleep(200) -- sleep 2s + skynet.error("update foobar a = 3") + sharedata.update("foobar", { a = 3, b = { "change" } }) + skynet.sleep(100) + skynet.error("delete foobar") + sharedata.delete "foobar" + end) +end) + +else + + +skynet.start(function() + skynet.newservice(SERVICE_NAME, "host") + + local obj = sharedata.query "foobar" + + local b = obj.b + skynet.error(string.format("a=%d", obj.a)) + + for k,v in ipairs(b) do + skynet.error(string.format("b[%d]=%s", k,v)) + end + + -- test lua serialization + local s = skynet.packstring(obj) + local nobj = skynet.unpack(s) + for k,v in pairs(nobj) do + skynet.error(string.format("nobj[%s]=%s", k,v)) + end + for k,v in ipairs(nobj.b) do + skynet.error(string.format("nobj.b[%d]=%s", k,v)) + end + + for i = 1, 5 do + skynet.sleep(100) + skynet.error("second " ..i) + for k,v in pairs(obj) do + skynet.error(string.format("%s = %s", k , tostring(v))) + end + end + + local ok, err = pcall(function() + local tmp = { b[1], b[2] } -- b is invalid , so pcall should failed + end) + + if not ok then + skynet.error(err) + end + + -- obj. b is not the same with local b + for k,v in ipairs(obj.b) do + skynet.error(string.format("b[%d] = %s", k , tostring(v))) + end + + collectgarbage() + skynet.error("sleep") + skynet.sleep(100) + b = nil + collectgarbage() + skynet.error("sleep") + skynet.sleep(100) + + skynet.exit() +end) + +end diff --git a/examples/simpledb.lua b/examples/simpledb.lua index a11fd8c5..af6bac5e 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register local db = {} local command = {} @@ -15,8 +16,22 @@ end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) - local f = command[string.upper(cmd)] - skynet.ret(skynet.pack(f(...))) + cmd = cmd:upper() + if cmd == "PING" then + assert(session == 0) + local str = (...) + if #str > 20 then + str = str:sub(1,20) .. "...(" .. #str .. ")" + end + skynet.error(string.format("%s ping %s", skynet.address(address), str)) + return + end + local f = command[cmd] + if f then + skynet.ret(skynet.pack(f(...))) + else + error(string.format("Unknown command %s", tostring(cmd))) + end end) skynet.register "SIMPLEDB" end) diff --git a/examples/simplemonitor.lua b/examples/simplemonitor.lua index 1528bb50..ec294d21 100644 --- a/examples/simplemonitor.lua +++ b/examples/simplemonitor.lua @@ -12,7 +12,7 @@ skynet.register_protocol { local w = service_map[address] if w then for watcher in pairs(w) do - skynet.redirect(watcher, address, "error", "") + skynet.redirect(watcher, address, "error", 0, "") end service_map[address] = false end diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua new file mode 100644 index 00000000..283b7e91 --- /dev/null +++ b/examples/simpleweb.lua @@ -0,0 +1,80 @@ +local skynet = require "skynet" +local socket = require "skynet.socket" +local httpd = require "http.httpd" +local sockethelper = require "http.sockethelper" +local urllib = require "http.url" +local table = table +local string = string + +local mode = ... + +if mode == "agent" then + +local function response(id, ...) + local ok, err = httpd.write_response(sockethelper.writefunc(id), ...) + if not ok then + -- if err == sockethelper.socket_error , that means socket closed. + skynet.error(string.format("fd = %d, %s", id, err)) + end +end + +skynet.start(function() + skynet.dispatch("lua", function (_,_,id) + socket.start(id) + -- limit request body size to 8192 (you can pass nil to unlimit) + local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192) + if code then + if code ~= 200 then + response(id, code) + else + local tmp = {} + if header.host then + table.insert(tmp, string.format("host: %s", header.host)) + end + local path, query = urllib.parse(url) + table.insert(tmp, string.format("path: %s", path)) + if query then + local q = urllib.parse_query(query) + for k, v in pairs(q) do + table.insert(tmp, string.format("query: %s= %s", k,v)) + end + end + table.insert(tmp, "-----header----") + for k,v in pairs(header) do + table.insert(tmp, string.format("%s = %s",k,v)) + end + table.insert(tmp, "-----body----\n" .. body) + response(id, code, table.concat(tmp,"\n")) + end + else + if url == sockethelper.socket_error then + skynet.error("socket closed") + else + skynet.error(url) + end + end + socket.close(id) + end) +end) + +else + +skynet.start(function() + local agent = {} + for i= 1, 20 do + agent[i] = skynet.newservice(SERVICE_NAME, "agent") + end + local balance = 1 + local id = socket.listen("0.0.0.0", 8001) + skynet.error("Listen web port 8001") + socket.start(id , function(id, addr) + skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance])) + skynet.send(agent[balance], "lua", id) + balance = balance + 1 + if balance > #agent then + balance = 1 + end + end) +end) + +end \ No newline at end of file diff --git a/examples/userlog.lua b/examples/userlog.lua new file mode 100644 index 00000000..3a8c5d22 --- /dev/null +++ b/examples/userlog.lua @@ -0,0 +1,25 @@ +local skynet = require "skynet" +require "skynet.manager" + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + unpack = skynet.tostring, + dispatch = function(_, address, msg) + print(string.format(":%08x(%.2f): %s", address, skynet.time(), msg)) + end +} + +skynet.register_protocol { + name = "SYSTEM", + id = skynet.PTYPE_SYSTEM, + unpack = function(...) return ... end, + dispatch = function() + -- reopen signal + print("SIGHUP") + end +} + +skynet.start(function() + skynet.register ".logger" +end) \ No newline at end of file diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 8ead2253..dabbd097 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,5 +1,4 @@ local skynet = require "skynet" -local netpack = require "netpack" local CMD = {} local SOCKET = {} @@ -7,15 +6,18 @@ local gate local agent = {} function SOCKET.open(fd, addr) + skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") - skynet.call(agent[fd], "lua", "start", gate, fd) + skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() }) end local function close_agent(fd) local a = agent[fd] + agent[fd] = nil if a then - skynet.kill(a) - agent[fd] = nil + skynet.call(gate, "lua", "kick", fd) + -- disconnect never return + skynet.send(a, "lua", "disconnect") end end @@ -29,6 +31,11 @@ function SOCKET.error(fd, msg) close_agent(fd) end +function SOCKET.warning(fd, size) + -- size K bytes havn't send out in fd + print("socket warning", fd, size) +end + function SOCKET.data(fd, msg) end @@ -36,6 +43,10 @@ function CMD.start(conf) skynet.call(gate, "lua", "open" , conf) end +function CMD.close(fd) + close_agent(fd) +end + skynet.start(function() skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) if cmd == "socket" then diff --git a/lualib-src/lsha1.c b/lualib-src/lsha1.c new file mode 100644 index 00000000..8f539844 --- /dev/null +++ b/lualib-src/lsha1.c @@ -0,0 +1,313 @@ +/* +SHA-1 in C +By Steve Reid +100% Public Domain + +----------------- +Modified 7/98 +By James H. Brown +Still 100% Public Domain + +Corrected a problem which generated improper hash values on 16 bit machines +Routine SHA1Update changed from + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int +len) +to + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned +long len) + +The 'len' parameter was declared an int which works fine on 32 bit machines. +However, on 16 bit machines an int is too small for the shifts being done +against +it. This caused the hash function to generate incorrect values if len was +greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). + +Since the file IO in main() reads 16K at a time, any file 8K or larger would +be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million +"a"s). + +I also changed the declaration of variables i & j in SHA1Update to +unsigned long from unsigned int for the same reason. + +These changes should make no difference to any 32 bit implementations since +an +int and a long are the same size in those environments. + +-- +I also corrected a few compiler warnings generated by Borland C. +1. Added #include for exit() prototype +2. Removed unused variable 'j' in SHA1Final +3. Changed exit(0) to return(0) at end of main. + +ALL changes I made can be located by searching for comments containing 'JHB' +----------------- +Modified 8/98 +By Steve Reid +Still 100% public domain + +1- Removed #include and used return() instead of exit() +2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) +3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net + +----------------- +Modified 4/01 +By Saul Kravitz +Still 100% PD +Modified to run on Compaq Alpha hardware. + +----------------- +Modified 07/2002 +By Ralph Giles +Still 100% public domain +modified for use with stdint types, autoconf +code cleanup, removed attribution comments +switched SHA1Final() argument order for consistency +use SHA1_ prefix for public api +move public api to sha1.h + +----------------- +Modufiled 08/2014 +By Cloud Wu +Still 100% PD +Lua binding +*/ + +/* +Test Vectors (from FIPS PUB 180-1) +"abc" + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D +"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 +A million repetitions of "a" + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F +*/ + +#define LUA_LIB + +#include +#include +#include + +typedef struct { + uint32_t state[5]; + uint32_t count[2]; + uint8_t buffer[64]; +} SHA1_CTX; + +#define SHA1_DIGEST_SIZE 20 + +static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* blk0() and blk() perform the initial expand. */ +/* I got the idea of expanding during the round function from SSLeay */ +/* FIXME: can we do this in an endian-proof way? */ +#ifdef WORDS_BIGENDIAN +#define blk0(i) block.l[i] +#else +#define blk0(i) (block.l[i] = (rol(block.l[i],24)&0xFF00FF00) \ + |(rol(block.l[i],8)&0x00FF00FF)) +#endif +#define blk(i) (block.l[i&15] = rol(block.l[(i+13)&15]^block.l[(i+8)&15] \ + ^block.l[(i+2)&15]^block.l[i&15],1)) + +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + + +/* Hash a single 512-bit block. This is the core of the algorithm. */ +static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) +{ + uint32_t a, b, c, d, e; + typedef union { + uint8_t c[64]; + uint32_t l[16]; + } CHAR64LONG16; + CHAR64LONG16 block; + + memcpy(&block, buffer, 64); + + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + + /* Wipe variables */ + a = b = c = d = e = 0; +} + + +/* SHA1Init - Initialize new context */ +static void sat_SHA1_Init(SHA1_CTX* context) +{ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* Run your data through this. */ +static void sat_SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) +{ + size_t i, j; + +#ifdef VERBOSE + SHAPrintContext(context, "before"); +#endif + + j = (context->count[0] >> 3) & 63; + if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; + context->count[1] += (len >> 29); + if ((j + len) > 63) { + memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1_Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) { + SHA1_Transform(context->state, data + i); + } + j = 0; + } + else i = 0; + memcpy(&context->buffer[j], &data[i], len - i); + +#ifdef VERBOSE + SHAPrintContext(context, "after "); +#endif +} + + +/* Add padding and return the message digest. */ +static void sat_SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) +{ + uint32_t i; + uint8_t finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + sat_SHA1_Update(context, (uint8_t *)"\200", 1); + while ((context->count[0] & 504) != 448) { + sat_SHA1_Update(context, (uint8_t *)"\0", 1); + } + sat_SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + digest[i] = (uint8_t) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } + + /* Wipe variables */ + i = 0; + memset(context->buffer, 0, 64); + memset(context->state, 0, 20); + memset(context->count, 0, 8); + memset(finalcount, 0, 8); /* SWR */ +} + +#include +#include + +int +lsha1(lua_State *L) { + size_t sz = 0; + const uint8_t * buffer = (const uint8_t *)luaL_checklstring(L, 1, &sz); + uint8_t digest[SHA1_DIGEST_SIZE]; + SHA1_CTX ctx; + sat_SHA1_Init(&ctx); + sat_SHA1_Update(&ctx, buffer, sz); + sat_SHA1_Final(&ctx, digest); + lua_pushlstring(L, (const char *)digest, SHA1_DIGEST_SIZE); + + return 1; +} + +#define BLOCKSIZE 64 + +static inline void +xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { + int i; + for (i=0;i BLOCKSIZE) { + SHA1_CTX ctx; + sat_SHA1_Init(&ctx); + sat_SHA1_Update(&ctx, key, key_sz); + sat_SHA1_Final(&ctx, rkey); + key_sz = SHA1_DIGEST_SIZE; + } else { + memcpy(rkey, key, key_sz); + } + + xor_key(rkey, 0x5c5c5c5c); + sat_SHA1_Init(&ctx1); + sat_SHA1_Update(&ctx1, rkey, BLOCKSIZE); + + xor_key(rkey, 0x5c5c5c5c ^ 0x36363636); + sat_SHA1_Init(&ctx2); + sat_SHA1_Update(&ctx2, rkey, BLOCKSIZE); + sat_SHA1_Update(&ctx2, text, text_sz); + sat_SHA1_Final(&ctx2, digest2); + + sat_SHA1_Update(&ctx1, digest2, SHA1_DIGEST_SIZE); + sat_SHA1_Final(&ctx1, digest1); + + lua_pushlstring(L, (const char *)digest1, SHA1_DIGEST_SIZE); + + return 1; +} + diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 077e538e..76daef40 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include @@ -8,27 +10,12 @@ #include #include #include - -#if defined(_WIN32) || defined(_WIN64) - -#include - -static void -init_winsock() { - WSADATA wsaData; - WSAStartup(MAKEWORD(2,2), &wsaData); -} - -#else - -static void -init_winsock() { -} - -#endif +#include "atomic.h" #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 +// avoid circular reference while encodeing +#define MAX_DEPTH 128 #define BSON_REAL 1 #define BSON_STRING 2 @@ -68,6 +55,13 @@ struct bson_reader { int size; }; +static inline int32_t +get_length(const uint8_t * data) { + const uint8_t * b = (const uint8_t *)data; + int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; + return len; +} + static inline void bson_destroy(struct bson *b) { if (b->ptr != b->buffer) { @@ -206,10 +200,50 @@ write_length(struct bson *b, int32_t v, int off) { b->ptr[off++] = (uv >> 24)&0xff; } +#define MAXUNICODE 0x10FFFF + +static int +utf8_copy(const char *s, char *d, size_t limit) { + static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; + unsigned int c = s[0]; + unsigned int res = 0; + if (limit < 1) + return 0; + d[0] = s[0]; + if (c < 0x80) { + return 1; + } else { + int count = 0; + while (c & 0x40) { + int cc = s[++count]; + if (limit <= count || (cc & 0xC0) != 0x80) + return 0; + d[count] = s[count]; + res = (res << 6) | (cc & 0x3F); + c <<= 1; + } + res |= ((c & 0x7F) << (count * 5)); + if (count > 3 || res > MAXUNICODE || res <= limits[count]) + return 0; + return count+1; + } +} + static void -write_string(struct bson *b, const char *key, size_t sz) { +write_string(struct bson *b, lua_State *L, const char *key, size_t sz) { bson_reserve(b,sz+1); - memcpy(b->ptr + b->size, key, sz); + char *dst = (char *)(b->ptr + b->size); + const char *src = key; + size_t n = sz; + while(n > 0) { + int c = utf8_copy(src, dst, n); + if (c == 0) { + luaL_error(L, "Invalid utf8 string"); + } + src += c; + dst += c; + n -= c; + } b->ptr[b->size+sz] = '\0'; b->size+=sz+1; } @@ -246,52 +280,36 @@ write_double(struct bson *b, lua_Number d) { } } -static void pack_dict(lua_State *L, struct bson *b, bool array); - static inline void -append_key(struct bson *bs, int type, const char *key, size_t sz) { +append_key(struct bson *bs, lua_State *L, int type, const char *key, size_t sz) { write_byte(bs, type); - write_string(bs, key, sz); + write_string(bs, L, key, sz); +} + +static inline int +is_32bit(int64_t v) { + return v >= INT32_MIN && v <= INT32_MAX; } static void append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { - int64_t i = lua_tointeger(L, -1); - lua_Number d = lua_tonumber(L,-1); - if (i != d) { - append_key(bs, BSON_REAL, key, sz); - write_double(bs, d); - } else { - int si = i >> 31; - if (si == 0 || si == -1) { - append_key(bs, BSON_INT32, key, sz); + if (lua_isinteger(L, -1)) { + int64_t i = lua_tointeger(L, -1); + if (is_32bit(i)) { + append_key(bs, L, BSON_INT32, key, sz); write_int32(bs, i); } else { - append_key(bs, BSON_INT64, key, sz); + append_key(bs, L, BSON_INT64, key, sz); write_int64(bs, i); } + } else { + lua_Number d = lua_tonumber(L,-1); + append_key(bs, L, BSON_REAL, key, sz); + write_double(bs, d); } } -static void -append_table(struct bson *bs, lua_State *L, const char *key, size_t sz) { - size_t len = lua_rawlen(L, -1); - bool isarray = false; - if (len > 0) { - lua_pushinteger(L, len); - if (lua_next(L,-2) == 0) { - isarray = true; - } else { - lua_pop(L,2); - } - } - if (isarray) { - append_key(bs, BSON_ARRAY, key, sz); - } else { - append_key(bs, BSON_DOCUMENT, key, sz); - } - pack_dict(L, bs, isarray); -} +static void append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth); static void write_binary(struct bson *b, const void * buffer, size_t sz) { @@ -303,14 +321,14 @@ write_binary(struct bson *b, const void * buffer, size_t sz) { } static void -append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { +append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { int vt = lua_type(L,-1); switch(vt) { case LUA_TNUMBER: append_number(bs, L, key, sz); break; case LUA_TUSERDATA: { - append_key(bs, BSON_DOCUMENT, key, sz); + append_key(bs, L, BSON_DOCUMENT, key, sz); int32_t * doc = lua_touserdata(L,-1); int32_t sz = *doc; bson_reserve(bs,sz); @@ -323,7 +341,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { const char * str = lua_tolstring(L,-1,&len); if (len > 1 && str[0]==0) { int subt = (uint8_t)str[1]; - append_key(bs, subt, key, sz); + append_key(bs, L, subt, key, sz); switch(subt) { case BSON_BINARY: write_binary(bs, str+2, len-2); @@ -369,8 +387,8 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { break; } } - write_string(bs, str, len-i-1); - write_string(bs, str + len-i, i); + write_string(bs, L, str, len-i-1); + write_string(bs, L, str + len-i, i); break; } case BSON_MINKEY: @@ -383,18 +401,18 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { } else { size_t len; const char * str = lua_tolstring(L,-1,&len); - append_key(bs, BSON_STRING, key, sz); + append_key(bs, L, BSON_STRING, key, sz); int off = reserve_length(bs); - write_string(bs, str, len); + write_string(bs, L, str, len); write_length(bs, len+1, off); } break; } case LUA_TTABLE: - append_table(bs, L, key, sz); + append_table(bs, L, key, sz, depth+1); break; case LUA_TBOOLEAN: - append_key(bs, BSON_BOOLEAN, key, sz); + append_key(bs, L, BSON_BOOLEAN, key, sz); write_byte(bs, lua_toboolean(L,-1)); break; default: @@ -413,62 +431,137 @@ bson_numstr( char *str, unsigned int i ) { } static void -pack_dict(lua_State *L, struct bson *b, bool isarray) { +pack_array(lua_State *L, struct bson *b, int depth, size_t len) { int length = reserve_length(b); - lua_pushnil(L); - while(lua_next(L,-2) != 0) { - int kt = lua_type(L, -2); + size_t i; + for (i=1;i<=len;i++) { char numberkey[32]; - const char * key = NULL; - size_t sz; - if (isarray) { - if (kt != LUA_TNUMBER) { - luaL_error(L, "Invalid array key type : %s", lua_typename(L, kt)); - return; - } - sz = bson_numstr(numberkey, lua_tounsigned(L,-2)-1); - key = numberkey; - - append_one(b, L, key, sz); - lua_pop(L,1); - } else { - switch(kt) { - case LUA_TNUMBER: - // copy key, don't change key type - lua_pushvalue(L,-2); - lua_insert(L,-2); - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz); - lua_pop(L,2); - break; - case LUA_TSTRING: - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz); - lua_pop(L,1); - break; - default: - luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); - return; - } - } + size_t sz = bson_numstr(numberkey, i - 1); + const char * key = numberkey; + lua_geti(L, -1, i); + append_one(b, L, key, sz, depth); + lua_pop(L, 1); } write_byte(b,0); write_length(b, b->size - length, length); } static void -pack_ordered_dict(lua_State *L, struct bson *b, int n) { +pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) { + const char * key = NULL; + size_t sz; + switch(kt) { + case LUA_TNUMBER: + // copy key, don't change key type + lua_pushvalue(L,-2); + lua_insert(L,-2); + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,2); + break; + case LUA_TSTRING: + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,1); + break; + default: + luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); + return; + } +} + +static void +pack_simple_dict(lua_State *L, struct bson *b, int depth) { + int length = reserve_length(b); + lua_pushnil(L); + while(lua_next(L,-2) != 0) { + int kt = lua_type(L, -2); + pack_dict_data(L, b, depth, kt); + } + write_byte(b,0); + write_length(b, b->size - length, length); +} + +static void +pack_meta_dict(lua_State *L, struct bson *b, int depth) { + int length = reserve_length(b); + + lua_pushvalue(L, -2); // push meta_obj + lua_call(L, 1, 3); // call __pairs_func => next_func, t_data, first_k + for(;;) { + lua_pushvalue(L, -2); // copy data + lua_pushvalue(L, -2); // copy k + lua_copy(L, -5, -3); // copy next_func replace old_k + lua_call(L, 2, 2); // call next_func + + int kt = lua_type(L, -2); + if (kt == LUA_TNIL) { + lua_pop(L, 4); // pop all k, v, next_func, obj + break; + } + pack_dict_data(L, b, depth, kt); + } + write_byte(b,0); + write_length(b, b->size - length, length); +} + +static bool +is_rawarray(lua_State *L) { + size_t len = lua_rawlen(L, -1); + if (len > 0) { + lua_pushinteger(L, len); + if (lua_next(L,-2) == 0) { + return true; + } else { + lua_pop(L,2); + } + } + return false; +} + +static void +append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { + if (depth > MAX_DEPTH) { + luaL_error(L, "Too depth while encoding bson"); + } + luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table + if (luaL_getmetafield(L, -1, "__len") != LUA_TNIL) { + lua_pushvalue(L, -2); + lua_call(L, 1, 1); + if (!lua_isinteger(L, -1)) { + luaL_error(L, "__len should return integer"); + } + size_t len = lua_tointeger(L, -1); + lua_pop(L, 1); + append_key(bs, L, BSON_ARRAY, key, sz); + pack_array(L, bs, depth, len); + } else if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + append_key(bs, L, BSON_DOCUMENT, key, sz); + pack_meta_dict(L, bs, depth); + } else if (is_rawarray(L)) { + append_key(bs, L, BSON_ARRAY, key, sz); + pack_array(L, bs, depth, lua_rawlen(L, -1)); + } else { + append_key(bs, L, BSON_DOCUMENT, key, sz); + pack_simple_dict(L, bs, depth); + } +} + +static void +pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) { int length = reserve_length(b); int i; + size_t sz; + // the first key is at index n + const char * key = lua_tolstring(L, n, &sz); for (i=0;isize - length, length); @@ -501,6 +594,7 @@ make_object(lua_State *L, int type, const void * ptr, size_t len) { static void unpack_dict(lua_State *L, struct bson_reader *br, bool array) { + luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table int sz = read_int32(L, br); const void * bytes = read_bytes(L, br, sz-5); struct bson_reader t = { bytes, sz-5 }; @@ -532,6 +626,9 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { break; case BSON_STRING: { int sz = read_int32(L, &t); + if (sz <= 0) { + luaL_error(L, "Invalid bson string , length = %d", sz); + } lua_pushlstring(L, read_bytes(L, &t, sz), sz-1); break; } @@ -641,7 +738,7 @@ static int lmakeindex(lua_State *L) { int32_t *bson = luaL_checkudata(L,1,"bson"); const uint8_t * start = (const uint8_t *)bson; - struct bson_reader br = { start+4, *bson - 5 }; + struct bson_reader br = { start+4, get_length(start) - 5 }; lua_newtable(L); for (;;) { @@ -765,8 +862,7 @@ lreplace(lua_State *L) { return luaL_error(L, "call makeindex first"); } lua_pushvalue(L,2); - lua_rawget(L, -2); - if (!lua_isnumber(L,-1)) { + if (lua_rawget(L, -2) != LUA_TNUMBER) { return luaL_error(L, "Can't replace key : %s", lua_tostring(L,2)); } int id = lua_tointeger(L, -1); @@ -787,23 +883,24 @@ lreplace(lua_State *L) { replace_object(L, type, &b); break; case BSON_INT32: { - double d = luaL_checknumber(L,3); - int32_t i = lua_tointeger(L,3); - if ((int32_t)d != i) { - luaL_error(L, "%f must be a 32bit integer ", d); + if (!lua_isinteger(L, 3)) { + luaL_error(L, "%f must be a 32bit integer ", lua_tonumber(L, 3)); } + int32_t i = lua_tointeger(L,3); write_int32(&b, i); break; } case BSON_INT64: { - double d = luaL_checknumber(L,3); - lua_Integer i = lua_tointeger(L,3); - if ((lua_Integer)d != i) { - luaL_error(L, "%f must be a 64bit integer ", d); + if (!lua_isinteger(L, 3)) { + luaL_error(L, "%f must be a 64bit integer ", lua_tonumber(L, 3)); } + int64_t i = lua_tointeger(L,3); write_int64(&b, i); break; } + default: + luaL_error(L, "Can't replace type %d", type); + break; } return 0; } @@ -814,7 +911,9 @@ ldecode(lua_State *L) { if (data == NULL) { return 0; } - struct bson_reader br = { (const uint8_t *)data , *data }; + const uint8_t * b = (const uint8_t *)data; + int32_t len = get_length(b); + struct bson_reader br = { b , len }; unpack_dict(L, &br, false); @@ -841,32 +940,66 @@ bson_meta(lua_State *L) { lua_setmetatable(L, -2); } +static int +encode_bson(lua_State *L) { + struct bson *b = lua_touserdata(L, 2); + lua_settop(L, 1); + if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + pack_meta_dict(L, b, 0); + } else { + pack_simple_dict(L, b, 0); + } + void * ud = lua_newuserdata(L, b->size); + memcpy(ud, b->ptr, b->size); + return 1; +} + static int lencode(lua_State *L) { struct bson b; - bson_create(&b); lua_settop(L,1); luaL_checktype(L, 1, LUA_TTABLE); - pack_dict(L, &b, false); - void * ud = lua_newuserdata(L, b.size); - memcpy(ud, b.ptr, b.size); + bson_create(&b); + lua_pushcfunction(L, encode_bson); + lua_pushvalue(L, 1); + lua_pushlightuserdata(L, &b); + if (lua_pcall(L, 2, 1, 0) != LUA_OK) { + bson_destroy(&b); + return lua_error(L); + } bson_destroy(&b); bson_meta(L); return 1; } +static int +encode_bson_byorder(lua_State *L) { + int n = lua_gettop(L); + struct bson *b = lua_touserdata(L, n); + lua_settop(L, --n); + pack_ordered_dict(L, b, n, 0); + lua_settop(L,0); + void * ud = lua_newuserdata(L, b->size); + memcpy(ud, b->ptr, b->size); + return 1; +} + static int lencode_order(lua_State *L) { struct bson b; - bson_create(&b); int n = lua_gettop(L); if (n%2 != 0) { return luaL_error(L, "Invalid ordered dict"); } - pack_ordered_dict(L, &b, n); - lua_settop(L,1); - void * ud = lua_newuserdata(L, b.size); - memcpy(ud, b.ptr, b.size); + bson_create(&b); + lua_pushvalue(L, 1); // copy the first arg to n + lua_pushcfunction(L, encode_bson_byorder); + lua_replace(L, 1); + lua_pushlightuserdata(L, &b); + if (lua_pcall(L, n+1, 1, 0) != LUA_OK) { + bson_destroy(&b); + return lua_error(L); + } bson_destroy(&b); bson_meta(L); return 1; @@ -897,7 +1030,7 @@ ltimestamp(lua_State *L) { luaL_addlstring(&b, (const char *)&inc, sizeof(inc)); ++inc; } else { - uint32_t i = lua_tounsigned(L,2); + uint32_t i = (uint32_t)lua_tointeger(L,2); luaL_addlstring(&b, (const char *)&i, sizeof(i)); } luaL_addlstring(&b, (const char *)&d, sizeof(d)); @@ -981,8 +1114,8 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { } const uint32_t * ts = (const uint32_t *)buf; lua_pushvalue(L, lua_upvalueindex(8)); - lua_pushunsigned(L, ts[1]); - lua_pushunsigned(L, ts[0]); + lua_pushinteger(L, (lua_Integer)ts[1]); + lua_pushinteger(L, (lua_Integer)ts[0]); return 3; } case BSON_REGEX: { @@ -1068,7 +1201,7 @@ typeclosure(lua_State *L) { "string", // 5 "binary", // 6 "objectid", // 7 - "timestamp",// 8 + "timestamp", // 8 "date", // 9 "regex", // 10 "minkey", // 11 @@ -1088,7 +1221,10 @@ static uint32_t oid_counter; static void init_oid_header() { - init_winsock(); + if (oid_counter) { + // already init + return; + } pid_t pid = getpid(); uint32_t h = 0; char hostname[256]; @@ -1104,8 +1240,12 @@ init_oid_header() { oid_header[2] = (h>>16) & 0xff; oid_header[3] = pid & 0xff; oid_header[4] = (pid >> 8) & 0xff; - - oid_counter = h ^ time(NULL) ^ (uintptr_t)&h; + + uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h; + if (c == 0) { + c = 1; + } + oid_counter = c; } static inline int @@ -1134,22 +1274,24 @@ lobjectid(lua_State *L) { } } else { time_t ti = time(NULL); - oid[2] = ti & 0xff; - oid[3] = (ti>>8) & 0xff; - oid[4] = (ti>>16) & 0xff; - oid[5] = (ti>>24) & 0xff; + // old_counter is a static var, use atom inc. + uint32_t id = ATOM_FINC(&oid_counter); + + oid[2] = (ti>>24) & 0xff; + oid[3] = (ti>>16) & 0xff; + oid[4] = (ti>>8) & 0xff; + oid[5] = ti & 0xff; memcpy(oid+6 , oid_header, 5); - oid[11] = oid_counter & 0xff; - oid[12] = (oid_counter>>8) & 0xff; - oid[13] = (oid_counter>>16) & 0xff; - ++oid_counter; + oid[11] = (id>>16) & 0xff; + oid[12] = (id>>8) & 0xff; + oid[13] = id & 0xff; } lua_pushlstring( L, (const char *)oid, 14); return 1; } -int +LUAMOD_API int luaopen_bson(lua_State *L) { luaL_checkversion(L); int i; @@ -1165,7 +1307,6 @@ luaopen_bson(lua_State *L) { { "timestamp", ltimestamp }, { "regex", lregex }, { "binary", lbinary }, - { "regex", lregex }, { "objectid", lobjectid }, { "decode", ldecode }, { NULL, NULL }, diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 1e499f2d..25189032 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -2,6 +2,8 @@ // It's only for demo, limited feature. Don't use it in your project. // Rewrite socket library by yourself . +#define LUA_LIB + #include #include #include @@ -75,47 +77,12 @@ lsend(lua_State *L) { size_t sz = 0; int fd = luaL_checkinteger(L,1); const char * msg = luaL_checklstring(L, 2, &sz); - uint8_t tmp[sz + 2]; - if (sz >= 0x10000) { - return luaL_error(L, "package too long %d (16bit limited)", (int)sz); - } - tmp[0] = (sz >> 8) & 0xff; - tmp[1] = sz & 0xff; - memcpy(tmp+2, msg, sz); - block_send(L, fd, (const char *)tmp, (int)sz+2); + block_send(L, fd, msg, (int)sz); return 0; } - -static int -unpack(lua_State *L, uint8_t *buffer, int sz, int n) { - int size = 0; - if (sz >= 2) { - size = buffer[0] << 8 | buffer[1]; - if (size > sz - 2) { - goto _block; - } - } else { - goto _block; - } - ++n; - lua_pushlstring(L, (const char *)buffer+2, size); - lua_rawseti(L, 3, n); - buffer += size + 2; - sz -= size + 2; - return unpack(L, buffer, sz, n); -_block: - lua_pushboolean(L, n==0 ? 0:1); - if (sz == 0) { - lua_pushnil(L); - } else { - lua_pushlstring(L, (const char *)buffer, sz); - } - return 2; -} - /* intger fd string last @@ -125,46 +92,31 @@ _block: boolean (true: data, false: block, nil: close) string last */ + +struct socket_buffer { + void * buffer; + int sz; +}; + static int lrecv(lua_State *L) { int fd = luaL_checkinteger(L,1); - size_t sz = 0; - const char * last = lua_tolstring(L,2,&sz); - luaL_checktype(L, 3, LUA_TTABLE); - char tmp[CACHE_SIZE]; - char * buffer; - int r = recv(fd, tmp, CACHE_SIZE, 0); + char buffer[CACHE_SIZE]; + int r = recv(fd, buffer, CACHE_SIZE, 0); if (r == 0) { + lua_pushliteral(L, ""); // close - return 0; + return 1; } if (r < 0) { if (errno == EAGAIN || errno == EINTR) { - lua_pushboolean(L, 0); - lua_pushvalue(L, 2); - return 2; + return 0; } luaL_error(L, "socket error: %s", strerror(errno)); } - if (sz + r <= CACHE_SIZE) { - buffer = tmp; - memmove(buffer + sz, buffer, r); - memcpy(buffer, last, sz); - } else { - buffer = lua_newuserdata(L, r + sz); - memcpy(buffer, last, sz); - memcpy(buffer + sz, tmp, r); - } - - int i; - int n = lua_rawlen(L, 3); - for (i=1;i<=n;i++) { - lua_pushnil(L); - lua_rawseti(L, 3, i); - } - - return unpack(L, (uint8_t *)buffer, r+sz, 0); + lua_pushlstring(L, buffer, r); + return 1; } static int @@ -178,11 +130,8 @@ lusleep(lua_State *L) { #define QUEUE_SIZE 1024 -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - struct queue { - int lock; + pthread_mutex_t lock; int head; int tail; char * queue[QUEUE_SIZE]; @@ -203,7 +152,7 @@ readline_stdin(void * arg) { memcpy(str, tmp, n); str[n] = 0; - LOCK(q); + pthread_mutex_lock(&q->lock); q->queue[q->tail] = str; if (++q->tail >= QUEUE_SIZE) { @@ -213,31 +162,31 @@ readline_stdin(void * arg) { // queue overflow exit(1); } - UNLOCK(q); + pthread_mutex_unlock(&q->lock); } return NULL; } static int -lreadline(lua_State *L) { +lreadstdin(lua_State *L) { struct queue *q = lua_touserdata(L, lua_upvalueindex(1)); - LOCK(q); + pthread_mutex_lock(&q->lock); if (q->head == q->tail) { - UNLOCK(q); + pthread_mutex_unlock(&q->lock); return 0; } char * str = q->queue[q->head]; if (++q->head >= QUEUE_SIZE) { q->head = 0; } - UNLOCK(q); + pthread_mutex_unlock(&q->lock); lua_pushstring(L, str); free(str); return 1; } -int -luaopen_clientsocket(lua_State *L) { +LUAMOD_API int +luaopen_client_socket(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "connect", lconnect }, @@ -251,8 +200,9 @@ luaopen_clientsocket(lua_State *L) { struct queue * q = lua_newuserdata(L, sizeof(*q)); memset(q, 0, sizeof(*q)); - lua_pushcclosure(L, lreadline, 1); - lua_setfield(L, -2, "readline"); + pthread_mutex_init(&q->lock, NULL); + lua_pushcclosure(L, lreadstdin, 1); + lua_setfield(L, -2, "readstdin"); pthread_t pid ; pthread_create(&pid, NULL, readline_stdin, q); diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c new file mode 100644 index 00000000..84d6332c --- /dev/null +++ b/lualib-src/lua-cluster.c @@ -0,0 +1,525 @@ +#define LUA_LIB + +#include +#include +#include +#include + +#include "skynet.h" + +/* + uint32_t/string addr + uint32_t/session session + lightuserdata msg + uint32_t sz + + return + string request + uint32_t next_session + */ + +#define TEMP_LENGTH 0x8200 +#define MULTI_PART 0x8000 + +static void +fill_uint32(uint8_t * buf, uint32_t n) { + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; +} + +static void +fill_header(lua_State *L, uint8_t *buf, int sz) { + assert(sz < 0x10000); + buf[0] = (sz >> 8) & 0xff; + buf[1] = sz & 0xff; +} + +/* + The request package : + first WORD is size of the package with big-endian + DWORD in content is small-endian + size <= 0x8000 (32K) and address is id + WORD sz+9 + BYTE 0 + DWORD addr + DWORD session + PADDING msg(sz) + size > 0x8000 and address is id + WORD 13 + BYTE 1 ; multireq , 0x41: multi push + DWORD addr + DWORD session + DWORD sz + + size <= 0x8000 (32K) and address is string + WORD sz+6+namelen + BYTE 0x80 + BYTE namelen + STRING name + DWORD session + PADDING msg(sz) + size > 0x8000 and address is string + WORD 10 + namelen + BYTE 0x81 ; 0xc1 : multi push + BYTE namelen + STRING name + DWORD session + DWORD sz + + multi req + WORD sz + 5 + BYTE 2/3 ; 2:multipart, 3:multipart end + DWORD SESSION + PADDING msgpart(sz) + */ +static int +packreq_number(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { + uint32_t addr = (uint32_t)lua_tointeger(L,1); + uint8_t buf[TEMP_LENGTH]; + if (sz < MULTI_PART) { + fill_header(L, buf, sz+9); + buf[2] = 0; + fill_uint32(buf+3, addr); + fill_uint32(buf+7, is_push ? 0 : (uint32_t)session); + memcpy(buf+11,msg,sz); + + lua_pushlstring(L, (const char *)buf, sz+11); + return 0; + } else { + int part = (sz - 1) / MULTI_PART + 1; + fill_header(L, buf, 13); + buf[2] = is_push ? 0x41 : 1; // multi push or request + fill_uint32(buf+3, addr); + fill_uint32(buf+7, (uint32_t)session); + fill_uint32(buf+11, sz); + lua_pushlstring(L, (const char *)buf, 15); + return part; + } +} + +static int +packreq_string(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { + size_t namelen = 0; + const char *name = lua_tolstring(L, 1, &namelen); + if (name == NULL || namelen < 1 || namelen > 255) { + skynet_free(msg); + luaL_error(L, "name is too long %s", name); + } + + uint8_t buf[TEMP_LENGTH]; + if (sz < MULTI_PART) { + fill_header(L, buf, sz+6+namelen); + buf[2] = 0x80; + buf[3] = (uint8_t)namelen; + memcpy(buf+4, name, namelen); + fill_uint32(buf+4+namelen, is_push ? 0 : (uint32_t)session); + memcpy(buf+8+namelen,msg,sz); + + lua_pushlstring(L, (const char *)buf, sz+8+namelen); + return 0; + } else { + int part = (sz - 1) / MULTI_PART + 1; + fill_header(L, buf, 10+namelen); + buf[2] = is_push ? 0xc1 : 0x81; // multi push or request + buf[3] = (uint8_t)namelen; + memcpy(buf+4, name, namelen); + fill_uint32(buf+4+namelen, (uint32_t)session); + fill_uint32(buf+8+namelen, sz); + + lua_pushlstring(L, (const char *)buf, 12+namelen); + return part; + } +} + +static void +packreq_multi(lua_State *L, int session, void * msg, uint32_t sz) { + uint8_t buf[TEMP_LENGTH]; + int part = (sz - 1) / MULTI_PART + 1; + int i; + char *ptr = msg; + for (i=0;i MULTI_PART) { + s = MULTI_PART; + buf[2] = 2; + } else { + s = sz; + buf[2] = 3; // the last multi part + } + fill_header(L, buf, s+5); + fill_uint32(buf+3, (uint32_t)session); + memcpy(buf+7, ptr, s); + lua_pushlstring(L, (const char *)buf, s+7); + lua_rawseti(L, -2, i+1); + sz -= s; + ptr += s; + } +} + +static int +packrequest(lua_State *L, int is_push) { + void *msg = lua_touserdata(L,3); + if (msg == NULL) { + return luaL_error(L, "Invalid request message"); + } + uint32_t sz = (uint32_t)luaL_checkinteger(L,4); + int session = luaL_checkinteger(L,2); + if (session <= 0) { + skynet_free(msg); + return luaL_error(L, "Invalid request session %d", session); + } + int addr_type = lua_type(L,1); + int multipak; + if (addr_type == LUA_TNUMBER) { + multipak = packreq_number(L, session, msg, sz, is_push); + } else { + multipak = packreq_string(L, session, msg, sz, is_push); + } + int current_session = session; + if (++session < 0) { + session = 1; + } + lua_pushinteger(L, session); + if (multipak) { + lua_createtable(L, multipak, 0); + packreq_multi(L, current_session, msg, sz); + skynet_free(msg); + return 3; + } else { + skynet_free(msg); + return 2; + } +} + +static int +lpackrequest(lua_State *L) { + return packrequest(L, 0); +} + +static int +lpackpush(lua_State *L) { + return packrequest(L, 1); +} + +/* + string packed message + return + uint32_t or string addr + int session + string msg + boolean padding + */ + +static inline uint32_t +unpack_uint32(const uint8_t * buf) { + return buf[0] | buf[1]<<8 | buf[2]<<16 | buf[3]<<24; +} + +static int +unpackreq_number(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 9) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + uint32_t address = unpack_uint32(buf+1); + uint32_t session = unpack_uint32(buf+5); + lua_pushinteger(L, address); + lua_pushinteger(L, session); + lua_pushlstring(L, (const char *)buf+9, sz-9); + if (session == 0) { + lua_pushnil(L); + lua_pushboolean(L,1); // is_push, no reponse + return 5; + } + + return 3; +} + +static int +unpackmreq_number(lua_State *L, const uint8_t * buf, int sz, int is_push) { + if (sz != 13) { + return luaL_error(L, "Invalid cluster message size %d (multi req must be 13)", sz); + } + uint32_t address = unpack_uint32(buf+1); + uint32_t session = unpack_uint32(buf+5); + uint32_t size = unpack_uint32(buf+9); + lua_pushinteger(L, address); + lua_pushinteger(L, session); + lua_pushinteger(L, size); + lua_pushboolean(L, 1); // padding multi part + lua_pushboolean(L, is_push); + + return 5; +} + +static int +unpackmreq_part(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 5) { + return luaL_error(L, "Invalid cluster multi part message"); + } + int padding = (buf[0] == 2); + uint32_t session = unpack_uint32(buf+1); + lua_pushboolean(L, 0); // no address + lua_pushinteger(L, session); + lua_pushlstring(L, (const char *)buf+5, sz-5); + lua_pushboolean(L, padding); + + return 4; +} + +static int +unpackreq_string(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 2) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + size_t namesz = buf[1]; + if (sz < namesz + 6) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + lua_pushlstring(L, (const char *)buf+2, namesz); + uint32_t session = unpack_uint32(buf + namesz + 2); + lua_pushinteger(L, (uint32_t)session); + lua_pushlstring(L, (const char *)buf+2+namesz+4, sz - namesz - 6); + if (session == 0) { + lua_pushnil(L); + lua_pushboolean(L,1); // is_push, no reponse + return 5; + } + + return 3; +} + +static int +unpackmreq_string(lua_State *L, const uint8_t * buf, int sz, int is_push) { + if (sz < 2) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + size_t namesz = buf[1]; + if (sz < namesz + 10) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + lua_pushlstring(L, (const char *)buf+2, namesz); + uint32_t session = unpack_uint32(buf + namesz + 2); + uint32_t size = unpack_uint32(buf + namesz + 6); + lua_pushinteger(L, session); + lua_pushinteger(L, size); + lua_pushboolean(L, 1); // padding multipart + lua_pushboolean(L, is_push); + + return 5; +} + +static int +lunpackrequest(lua_State *L) { + size_t ssz; + const char *msg = luaL_checklstring(L,1,&ssz); + int sz = (int)ssz; + switch (msg[0]) { + case 0: + return unpackreq_number(L, (const uint8_t *)msg, sz); + case 1: + return unpackmreq_number(L, (const uint8_t *)msg, sz, 0); // request + case '\x41': + return unpackmreq_number(L, (const uint8_t *)msg, sz, 1); // push + case 2: + case 3: + return unpackmreq_part(L, (const uint8_t *)msg, sz); + case '\x80': + return unpackreq_string(L, (const uint8_t *)msg, sz); + case '\x81': + return unpackmreq_string(L, (const uint8_t *)msg, sz, 0 ); // request + case '\xc1': + return unpackmreq_string(L, (const uint8_t *)msg, sz, 1 ); // push + default: + return luaL_error(L, "Invalid req package type %d", msg[0]); + } +} + +/* + The response package : + WORD size (big endian) + DWORD session + BYTE type + 0: error + 1: ok + 2: multi begin + 3: multi part + 4: multi end + PADDING msg + type = 0, error msg + type = 1, msg + type = 2, DWORD size + type = 3/4, msg + */ +/* + int session + boolean ok + lightuserdata msg + int sz + return string response + */ +static int +lpackresponse(lua_State *L) { + uint32_t session = (uint32_t)luaL_checkinteger(L,1); + // clusterd.lua:command.socket call lpackresponse, + // and the msg/sz is return by skynet.rawcall , so don't free(msg) + int ok = lua_toboolean(L,2); + void * msg; + size_t sz; + + if (lua_type(L,3) == LUA_TSTRING) { + msg = (void *)lua_tolstring(L, 3, &sz); + } else { + msg = lua_touserdata(L,3); + sz = (size_t)luaL_checkinteger(L, 4); + } + + if (!ok) { + if (sz > MULTI_PART) { + // truncate the error msg if too long + sz = MULTI_PART; + } + } else { + if (sz > MULTI_PART) { + // return + int part = (sz - 1) / MULTI_PART + 1; + lua_createtable(L, part+1, 0); + uint8_t buf[TEMP_LENGTH]; + + // multi part begin + fill_header(L, buf, 9); + fill_uint32(buf+2, session); + buf[6] = 2; + fill_uint32(buf+7, (uint32_t)sz); + lua_pushlstring(L, (const char *)buf, 11); + lua_rawseti(L, -2, 1); + + char * ptr = msg; + int i; + for (i=0;i MULTI_PART) { + s = MULTI_PART; + buf[6] = 3; + } else { + s = sz; + buf[6] = 4; + } + fill_header(L, buf, s+5); + fill_uint32(buf+2, session); + memcpy(buf+7,ptr,s); + lua_pushlstring(L, (const char *)buf, s+7); + lua_rawseti(L, -2, i+2); + sz -= s; + ptr += s; + } + return 1; + } + } + + uint8_t buf[TEMP_LENGTH]; + fill_header(L, buf, sz+5); + fill_uint32(buf+2, session); + buf[6] = ok; + memcpy(buf+7,msg,sz); + + lua_pushlstring(L, (const char *)buf, sz+7); + + return 1; +} + +/* + string packed response + return integer session + boolean ok + string msg + boolean padding + */ +static int +lunpackresponse(lua_State *L) { + size_t sz; + const char * buf = luaL_checklstring(L, 1, &sz); + if (sz < 5) { + return 0; + } + uint32_t session = unpack_uint32((const uint8_t *)buf); + lua_pushinteger(L, (lua_Integer)session); + switch(buf[4]) { + case 0: // error + lua_pushboolean(L, 0); + lua_pushlstring(L, buf+5, sz-5); + return 3; + case 1: // ok + case 4: // multi end + lua_pushboolean(L, 1); + lua_pushlstring(L, buf+5, sz-5); + return 3; + case 2: // multi begin + if (sz != 9) { + return 0; + } + sz = unpack_uint32((const uint8_t *)buf+5); + lua_pushboolean(L, 1); + lua_pushinteger(L, sz); + lua_pushboolean(L, 1); + return 4; + case 3: // multi part + lua_pushboolean(L, 1); + lua_pushlstring(L, buf+5, sz-5); + lua_pushboolean(L, 1); + return 4; + default: + return 0; + } +} + +static int +lconcat(lua_State *L) { + if (!lua_istable(L,1)) + return 0; + if (lua_geti(L,1,1) != LUA_TNUMBER) + return 0; + int sz = lua_tointeger(L,-1); + lua_pop(L,1); + char * buff = skynet_malloc(sz); + int idx = 2; + int offset = 0; + while(lua_geti(L,1,idx) == LUA_TSTRING) { + size_t s; + const char * str = lua_tolstring(L, -1, &s); + if (s+offset > sz) { + skynet_free(buff); + return 0; + } + memcpy(buff+offset, str, s); + lua_pop(L,1); + offset += s; + ++idx; + } + if (offset != sz) { + skynet_free(buff); + return 0; + } + // buff/sz will send to other service, See clusterd.lua + lua_pushlightuserdata(L, buff); + lua_pushinteger(L, sz); + return 2; +} + +LUAMOD_API int +luaopen_skynet_cluster_core(lua_State *L) { + luaL_Reg l[] = { + { "packrequest", lpackrequest }, + { "packpush", lpackpush }, + { "unpackrequest", lunpackrequest }, + { "packresponse", lpackresponse }, + { "unpackresponse", lunpackresponse }, + { "concat", lconcat }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L,l); + + return 1; +} diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c new file mode 100644 index 00000000..888fe544 --- /dev/null +++ b/lualib-src/lua-crypt.c @@ -0,0 +1,940 @@ +#define LUA_LIB + +#include +#include + +#include +#include +#include +#include + +#define SMALL_CHUNK 256 + +/* the eight DES S-boxes */ + +static uint32_t SB1[64] = { + 0x01010400, 0x00000000, 0x00010000, 0x01010404, + 0x01010004, 0x00010404, 0x00000004, 0x00010000, + 0x00000400, 0x01010400, 0x01010404, 0x00000400, + 0x01000404, 0x01010004, 0x01000000, 0x00000004, + 0x00000404, 0x01000400, 0x01000400, 0x00010400, + 0x00010400, 0x01010000, 0x01010000, 0x01000404, + 0x00010004, 0x01000004, 0x01000004, 0x00010004, + 0x00000000, 0x00000404, 0x00010404, 0x01000000, + 0x00010000, 0x01010404, 0x00000004, 0x01010000, + 0x01010400, 0x01000000, 0x01000000, 0x00000400, + 0x01010004, 0x00010000, 0x00010400, 0x01000004, + 0x00000400, 0x00000004, 0x01000404, 0x00010404, + 0x01010404, 0x00010004, 0x01010000, 0x01000404, + 0x01000004, 0x00000404, 0x00010404, 0x01010400, + 0x00000404, 0x01000400, 0x01000400, 0x00000000, + 0x00010004, 0x00010400, 0x00000000, 0x01010004 +}; + +static uint32_t SB2[64] = { + 0x80108020, 0x80008000, 0x00008000, 0x00108020, + 0x00100000, 0x00000020, 0x80100020, 0x80008020, + 0x80000020, 0x80108020, 0x80108000, 0x80000000, + 0x80008000, 0x00100000, 0x00000020, 0x80100020, + 0x00108000, 0x00100020, 0x80008020, 0x00000000, + 0x80000000, 0x00008000, 0x00108020, 0x80100000, + 0x00100020, 0x80000020, 0x00000000, 0x00108000, + 0x00008020, 0x80108000, 0x80100000, 0x00008020, + 0x00000000, 0x00108020, 0x80100020, 0x00100000, + 0x80008020, 0x80100000, 0x80108000, 0x00008000, + 0x80100000, 0x80008000, 0x00000020, 0x80108020, + 0x00108020, 0x00000020, 0x00008000, 0x80000000, + 0x00008020, 0x80108000, 0x00100000, 0x80000020, + 0x00100020, 0x80008020, 0x80000020, 0x00100020, + 0x00108000, 0x00000000, 0x80008000, 0x00008020, + 0x80000000, 0x80100020, 0x80108020, 0x00108000 +}; + +static uint32_t SB3[64] = { + 0x00000208, 0x08020200, 0x00000000, 0x08020008, + 0x08000200, 0x00000000, 0x00020208, 0x08000200, + 0x00020008, 0x08000008, 0x08000008, 0x00020000, + 0x08020208, 0x00020008, 0x08020000, 0x00000208, + 0x08000000, 0x00000008, 0x08020200, 0x00000200, + 0x00020200, 0x08020000, 0x08020008, 0x00020208, + 0x08000208, 0x00020200, 0x00020000, 0x08000208, + 0x00000008, 0x08020208, 0x00000200, 0x08000000, + 0x08020200, 0x08000000, 0x00020008, 0x00000208, + 0x00020000, 0x08020200, 0x08000200, 0x00000000, + 0x00000200, 0x00020008, 0x08020208, 0x08000200, + 0x08000008, 0x00000200, 0x00000000, 0x08020008, + 0x08000208, 0x00020000, 0x08000000, 0x08020208, + 0x00000008, 0x00020208, 0x00020200, 0x08000008, + 0x08020000, 0x08000208, 0x00000208, 0x08020000, + 0x00020208, 0x00000008, 0x08020008, 0x00020200 +}; + +static uint32_t SB4[64] = { + 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802080, 0x00800081, 0x00800001, 0x00002001, + 0x00000000, 0x00802000, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00800080, 0x00800001, + 0x00000001, 0x00002000, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002001, 0x00002080, + 0x00800081, 0x00000001, 0x00002080, 0x00800080, + 0x00002000, 0x00802080, 0x00802081, 0x00000081, + 0x00800080, 0x00800001, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00000000, 0x00802000, + 0x00002080, 0x00800080, 0x00800081, 0x00000001, + 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802081, 0x00000081, 0x00000001, 0x00002000, + 0x00800001, 0x00002001, 0x00802080, 0x00800081, + 0x00002001, 0x00002080, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002000, 0x00802080 +}; + +static uint32_t SB5[64] = { + 0x00000100, 0x02080100, 0x02080000, 0x42000100, + 0x00080000, 0x00000100, 0x40000000, 0x02080000, + 0x40080100, 0x00080000, 0x02000100, 0x40080100, + 0x42000100, 0x42080000, 0x00080100, 0x40000000, + 0x02000000, 0x40080000, 0x40080000, 0x00000000, + 0x40000100, 0x42080100, 0x42080100, 0x02000100, + 0x42080000, 0x40000100, 0x00000000, 0x42000000, + 0x02080100, 0x02000000, 0x42000000, 0x00080100, + 0x00080000, 0x42000100, 0x00000100, 0x02000000, + 0x40000000, 0x02080000, 0x42000100, 0x40080100, + 0x02000100, 0x40000000, 0x42080000, 0x02080100, + 0x40080100, 0x00000100, 0x02000000, 0x42080000, + 0x42080100, 0x00080100, 0x42000000, 0x42080100, + 0x02080000, 0x00000000, 0x40080000, 0x42000000, + 0x00080100, 0x02000100, 0x40000100, 0x00080000, + 0x00000000, 0x40080000, 0x02080100, 0x40000100 +}; + +static uint32_t SB6[64] = { + 0x20000010, 0x20400000, 0x00004000, 0x20404010, + 0x20400000, 0x00000010, 0x20404010, 0x00400000, + 0x20004000, 0x00404010, 0x00400000, 0x20000010, + 0x00400010, 0x20004000, 0x20000000, 0x00004010, + 0x00000000, 0x00400010, 0x20004010, 0x00004000, + 0x00404000, 0x20004010, 0x00000010, 0x20400010, + 0x20400010, 0x00000000, 0x00404010, 0x20404000, + 0x00004010, 0x00404000, 0x20404000, 0x20000000, + 0x20004000, 0x00000010, 0x20400010, 0x00404000, + 0x20404010, 0x00400000, 0x00004010, 0x20000010, + 0x00400000, 0x20004000, 0x20000000, 0x00004010, + 0x20000010, 0x20404010, 0x00404000, 0x20400000, + 0x00404010, 0x20404000, 0x00000000, 0x20400010, + 0x00000010, 0x00004000, 0x20400000, 0x00404010, + 0x00004000, 0x00400010, 0x20004010, 0x00000000, + 0x20404000, 0x20000000, 0x00400010, 0x20004010 +}; + +static uint32_t SB7[64] = { + 0x00200000, 0x04200002, 0x04000802, 0x00000000, + 0x00000800, 0x04000802, 0x00200802, 0x04200800, + 0x04200802, 0x00200000, 0x00000000, 0x04000002, + 0x00000002, 0x04000000, 0x04200002, 0x00000802, + 0x04000800, 0x00200802, 0x00200002, 0x04000800, + 0x04000002, 0x04200000, 0x04200800, 0x00200002, + 0x04200000, 0x00000800, 0x00000802, 0x04200802, + 0x00200800, 0x00000002, 0x04000000, 0x00200800, + 0x04000000, 0x00200800, 0x00200000, 0x04000802, + 0x04000802, 0x04200002, 0x04200002, 0x00000002, + 0x00200002, 0x04000000, 0x04000800, 0x00200000, + 0x04200800, 0x00000802, 0x00200802, 0x04200800, + 0x00000802, 0x04000002, 0x04200802, 0x04200000, + 0x00200800, 0x00000000, 0x00000002, 0x04200802, + 0x00000000, 0x00200802, 0x04200000, 0x00000800, + 0x04000002, 0x04000800, 0x00000800, 0x00200002 +}; + +static uint32_t SB8[64] = { + 0x10001040, 0x00001000, 0x00040000, 0x10041040, + 0x10000000, 0x10001040, 0x00000040, 0x10000000, + 0x00040040, 0x10040000, 0x10041040, 0x00041000, + 0x10041000, 0x00041040, 0x00001000, 0x00000040, + 0x10040000, 0x10000040, 0x10001000, 0x00001040, + 0x00041000, 0x00040040, 0x10040040, 0x10041000, + 0x00001040, 0x00000000, 0x00000000, 0x10040040, + 0x10000040, 0x10001000, 0x00041040, 0x00040000, + 0x00041040, 0x00040000, 0x10041000, 0x00001000, + 0x00000040, 0x10040040, 0x00001000, 0x00041040, + 0x10001000, 0x00000040, 0x10000040, 0x10040000, + 0x10040040, 0x10000000, 0x00040000, 0x10001040, + 0x00000000, 0x10041040, 0x00040040, 0x10000040, + 0x10040000, 0x10001000, 0x10001040, 0x00000000, + 0x10041040, 0x00041000, 0x00041000, 0x00001040, + 0x00001040, 0x00040040, 0x10000000, 0x10041000 +}; + +/* PC1: left and right halves bit-swap */ + +static uint32_t LHs[16] = { + 0x00000000, 0x00000001, 0x00000100, 0x00000101, + 0x00010000, 0x00010001, 0x00010100, 0x00010101, + 0x01000000, 0x01000001, 0x01000100, 0x01000101, + 0x01010000, 0x01010001, 0x01010100, 0x01010101 +}; + +static uint32_t RHs[16] = { + 0x00000000, 0x01000000, 0x00010000, 0x01010000, + 0x00000100, 0x01000100, 0x00010100, 0x01010100, + 0x00000001, 0x01000001, 0x00010001, 0x01010001, + 0x00000101, 0x01000101, 0x00010101, 0x01010101, +}; + +/* platform-independant 32-bit integer manipulation macros */ + +#define GET_UINT32(n,b,i) \ +{ \ + (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ + | ( (uint32_t) (b)[(i) + 1] << 16 ) \ + | ( (uint32_t) (b)[(i) + 2] << 8 ) \ + | ( (uint32_t) (b)[(i) + 3] ); \ +} + +#define PUT_UINT32(n,b,i) \ +{ \ + (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \ + (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \ + (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \ + (b)[(i) + 3] = (uint8_t) ( (n) ); \ +} + +/* Initial Permutation macro */ + +#define DES_IP(X,Y) \ +{ \ + T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ + T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ + T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ + T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ + Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ + T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ + X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ +} + +/* Final Permutation macro */ + +#define DES_FP(X,Y) \ +{ \ + X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ + T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ + Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ + T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ + T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ + T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ + T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ +} + +/* DES round macro */ + +#define DES_ROUND(X,Y) \ +{ \ + T = *SK++ ^ X; \ + Y ^= SB8[ (T ) & 0x3F ] ^ \ + SB6[ (T >> 8) & 0x3F ] ^ \ + SB4[ (T >> 16) & 0x3F ] ^ \ + SB2[ (T >> 24) & 0x3F ]; \ + \ + T = *SK++ ^ ((X << 28) | (X >> 4)); \ + Y ^= SB7[ (T ) & 0x3F ] ^ \ + SB5[ (T >> 8) & 0x3F ] ^ \ + SB3[ (T >> 16) & 0x3F ] ^ \ + SB1[ (T >> 24) & 0x3F ]; \ +} + +/* DES key schedule */ + +static void +des_main_ks( uint32_t SK[32], const uint8_t key[8] ) { + int i; + uint32_t X, Y, T; + + GET_UINT32( X, key, 0 ); + GET_UINT32( Y, key, 4 ); + + /* Permuted Choice 1 */ + + T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); + T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); + + X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) + | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) + | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) + | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); + + Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) + | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) + | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) + | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); + + X &= 0x0FFFFFFF; + Y &= 0x0FFFFFFF; + + /* calculate subkeys */ + + for( i = 0; i < 16; i++ ) + { + if( i < 2 || i == 8 || i == 15 ) + { + X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; + Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; + } + else + { + X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; + Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; + } + + *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) + | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) + | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) + | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) + | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) + | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) + | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) + | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) + | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) + | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) + | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); + + *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) + | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) + | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) + | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) + | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) + | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) + | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) + | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) + | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) + | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) + | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); + } +} + +/* DES 64-bit block encryption/decryption */ + +static void +des_crypt( const uint32_t SK[32], const uint8_t input[8], uint8_t output[8] ) { + uint32_t X, Y, T; + + GET_UINT32( X, input, 0 ); + GET_UINT32( Y, input, 4 ); + + DES_IP( X, Y ); + + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + + DES_FP( Y, X ); + + PUT_UINT32( Y, output, 0 ); + PUT_UINT32( X, output, 4 ); +} + +static int +lrandomkey(lua_State *L) { + char tmp[8]; + int i; + char x = 0; + for (i=0;i<8;i++) { + tmp[i] = random() & 0xff; + x ^= tmp[i]; + } + if (x==0) { + tmp[0] |= 1; // avoid 0 + } + lua_pushlstring(L, tmp, 8); + return 1; +} + +static void +des_key(lua_State *L, uint32_t SK[32]) { + size_t keysz = 0; + const void * key = luaL_checklstring(L, 1, &keysz); + if (keysz != 8) { + luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz); + } + des_main_ks(SK, key); +} + +static int +ldesencode(lua_State *L) { + uint32_t SK[32]; + des_key(L, SK); + + size_t textsz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); + size_t chunksz = (textsz + 8) & ~7; + uint8_t tmp[SMALL_CHUNK]; + uint8_t *buffer = tmp; + if (chunksz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, chunksz); + } + int i; + for (i=0;i<(int)textsz-7;i+=8) { + des_crypt(SK, text+i, buffer+i); + } + int bytes = textsz - i; + uint8_t tail[8]; + int j; + for (j=0;j<8;j++) { + if (j < bytes) { + tail[j] = text[i+j]; + } else if (j==bytes) { + tail[j] = 0x80; + } else { + tail[j] = 0; + } + } + des_crypt(SK, tail, buffer+i); + lua_pushlstring(L, (const char *)buffer, chunksz); + + return 1; +} + +static int +ldesdecode(lua_State *L) { + uint32_t ESK[32]; + des_key(L, ESK); + uint32_t SK[32]; + int i; + for( i = 0; i < 32; i += 2 ) { + SK[i] = ESK[30 - i]; + SK[i + 1] = ESK[31 - i]; + } + size_t textsz = 0; + const uint8_t *text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); + if ((textsz & 7) || textsz == 0) { + return luaL_error(L, "Invalid des crypt text length %d", (int)textsz); + } + uint8_t tmp[SMALL_CHUNK]; + uint8_t *buffer = tmp; + if (textsz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, textsz); + } + for (i=0;i=textsz-8;i--) { + if (buffer[i] == 0) { + padding++; + } else if (buffer[i] == 0x80) { + break; + } else { + return luaL_error(L, "Invalid des crypt text"); + } + } + if (padding > 8) { + return luaL_error(L, "Invalid des crypt text"); + } + lua_pushlstring(L, (const char *)buffer, textsz - padding); + return 1; +} + + +static void +Hash(const char * str, int sz, uint8_t key[8]) { + uint32_t djb_hash = 5381L; + uint32_t js_hash = 1315423911L; + + int i; + for (i=0;i> 2)); + } + + key[0] = djb_hash & 0xff; + key[1] = (djb_hash >> 8) & 0xff; + key[2] = (djb_hash >> 16) & 0xff; + key[3] = (djb_hash >> 24) & 0xff; + + key[4] = js_hash & 0xff; + key[5] = (js_hash >> 8) & 0xff; + key[6] = (js_hash >> 16) & 0xff; + key[7] = (js_hash >> 24) & 0xff; +} + +static int +lhashkey(lua_State *L) { + size_t sz = 0; + const char * key = luaL_checklstring(L, 1, &sz); + uint8_t realkey[8]; + Hash(key,(int)sz,realkey); + lua_pushlstring(L, (const char *)realkey, 8); + return 1; +} + +static int +ltohex(lua_State *L) { + static char hex[] = "0123456789abcdef"; + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (sz > SMALL_CHUNK/2) { + buffer = lua_newuserdata(L, sz * 2); + } + int i; + for (i=0;i> 4]; + buffer[i*2+1] = hex[text[i] & 0xf]; + } + lua_pushlstring(L, buffer, sz * 2); + return 1; +} + +#define HEX(v,c) { char tmp = (char) c; if (tmp >= '0' && tmp <= '9') { v = tmp-'0'; } else { v = tmp - 'a' + 10; } } + +static int +lfromhex(lua_State *L) { + size_t sz = 0; + const char * text = luaL_checklstring(L, 1, &sz); + if (sz & 1) { + return luaL_error(L, "Invalid hex text size %d", (int)sz); + } + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (sz > SMALL_CHUNK*2) { + buffer = lua_newuserdata(L, sz / 2); + } + int i; + for (i=0;i 16 || low > 16) { + return luaL_error(L, "Invalid hex text", text); + } + buffer[i/2] = hi<<4 | low; + } + lua_pushlstring(L, buffer, i/2); + return 1; +} + +// Constants are the integer part of the sines of integers (in radians) * 2^32. +const uint32_t k[64] = { +0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee , +0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 , +0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be , +0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 , +0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa , +0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 , +0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed , +0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a , +0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c , +0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 , +0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 , +0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 , +0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 , +0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 , +0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 , +0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; + +// r specifies the per-round shift amounts +const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; + +// leftrotate function definition +#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) + +static void +hmac(uint32_t x[2], uint32_t y[2], uint32_t result[2]) { + uint32_t w[16]; + uint32_t a, b, c, d, f, g, temp; + int i; + + a = 0x67452301u; + b = 0xefcdab89u; + c = 0x98badcfeu; + d = 0x10325476u; + + for (i=0;i<16;i+=4) { + w[i] = x[1]; + w[i+1] = x[0]; + w[i+2] = y[1]; + w[i+3] = y[0]; + } + + for(i = 0; i<64; i++) { + if (i < 16) { + f = (b & c) | ((~b) & d); + g = i; + } else if (i < 32) { + f = (d & b) | ((~d) & c); + g = (5*i + 1) % 16; + } else if (i < 48) { + f = b ^ c ^ d; + g = (3*i + 5) % 16; + } else { + f = c ^ (b | (~d)); + g = (7*i) % 16; + } + + temp = d; + d = c; + c = b; + b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); + a = temp; + + } + + result[0] = c^d; + result[1] = a^b; +} + +static void +read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) { + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid uint64 x"); + } + const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz); + if (sz != 8) { + luaL_error(L, "Invalid uint64 y"); + } + xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + yy[0] = y[0] | y[1]<<8 | y[2]<<16 | y[3]<<24; + yy[1] = y[4] | y[5]<<8 | y[6]<<16 | y[7]<<24; +} + +static int +pushqword(lua_State *L, uint32_t result[2]) { + uint8_t tmp[8]; + tmp[0] = result[0] & 0xff; + tmp[1] = (result[0] >> 8 )& 0xff; + tmp[2] = (result[0] >> 16 )& 0xff; + tmp[3] = (result[0] >> 24 )& 0xff; + tmp[4] = result[1] & 0xff; + tmp[5] = (result[1] >> 8 )& 0xff; + tmp[6] = (result[1] >> 16 )& 0xff; + tmp[7] = (result[1] >> 24 )& 0xff; + + lua_pushlstring(L, (const char *)tmp, 8); + return 1; +} + +static int +lhmac64(lua_State *L) { + uint32_t x[2], y[2]; + read64(L, x, y); + uint32_t result[2]; + hmac(x,y,result); + return pushqword(L, result); +} + +/* + 8bytes key + string text + */ +static int +lhmac_hash(lua_State *L) { + uint32_t key[2]; + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid uint64 key"); + } + key[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + key[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + const char * text = luaL_checklstring(L, 2, &sz); + uint8_t h[8]; + Hash(text,(int)sz,h); + uint32_t htext[2]; + htext[0] = h[0] | h[1]<<8 | h[2]<<16 | h[3]<<24; + htext[1] = h[4] | h[5]<<8 | h[6]<<16 | h[7]<<24; + uint32_t result[2]; + hmac(htext,key,result); + return pushqword(L, result); +} + +// powmodp64 for DH-key exchange + +// The biggest 64bit prime +#define P 0xffffffffffffffc5ull + +static inline uint64_t +mul_mod_p(uint64_t a, uint64_t b) { + uint64_t m = 0; + while(b) { + if(b&1) { + uint64_t t = P-a; + if ( m >= t) { + m -= t; + } else { + m += a; + } + } + if (a >= P - a) { + a = a * 2 - P; + } else { + a = a * 2; + } + b>>=1; + } + return m; +} + +static inline uint64_t +pow_mod_p(uint64_t a, uint64_t b) { + if (b==1) { + return a; + } + uint64_t t = pow_mod_p(a, b>>1); + t = mul_mod_p(t,t); + if (b % 2) { + t = mul_mod_p(t, a); + } + return t; +} + +// calc a^b % p +static uint64_t +powmodp(uint64_t a, uint64_t b) { + if (a > P) + a%=P; + return pow_mod_p(a,b); +} + +static void +push64(lua_State *L, uint64_t r) { + uint8_t tmp[8]; + tmp[0] = r & 0xff; + tmp[1] = (r >> 8 )& 0xff; + tmp[2] = (r >> 16 )& 0xff; + tmp[3] = (r >> 24 )& 0xff; + tmp[4] = (r >> 32 )& 0xff; + tmp[5] = (r >> 40 )& 0xff; + tmp[6] = (r >> 48 )& 0xff; + tmp[7] = (r >> 56 )& 0xff; + + lua_pushlstring(L, (const char *)tmp, 8); +} + +static int +ldhsecret(lua_State *L) { + uint32_t x[2], y[2]; + read64(L, x, y); + uint64_t xx = (uint64_t)x[0] | (uint64_t)x[1]<<32; + uint64_t yy = (uint64_t)y[0] | (uint64_t)y[1]<<32; + if (xx == 0 || yy == 0) + return luaL_error(L, "Can't be 0"); + uint64_t r = powmodp(xx, yy); + + push64(L, r); + + return 1; +} + +#define G 5 + +static int +ldhexchange(lua_State *L) { + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid dh uint64 key"); + } + uint32_t xx[2]; + xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + + uint64_t x64 = (uint64_t)xx[0] | (uint64_t)xx[1]<<32; + if (x64 == 0) + return luaL_error(L, "Can't be 0"); + + uint64_t r = powmodp(G, x64); + push64(L, r); + return 1; +} + +// base64 + +static int +lb64encode(lua_State *L) { + static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + int encode_sz = (sz + 2)/3*4; + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (encode_sz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, encode_sz); + } + int i,j; + j=0; + for (i=0;i<(int)sz-2;i+=3) { + uint32_t v = text[i] << 16 | text[i+1] << 8 | text[i+2]; + buffer[j] = encoding[v >> 18]; + buffer[j+1] = encoding[(v >> 12) & 0x3f]; + buffer[j+2] = encoding[(v >> 6) & 0x3f]; + buffer[j+3] = encoding[(v) & 0x3f]; + j+=4; + } + int padding = sz-i; + uint32_t v; + switch(padding) { + case 1 : + v = text[i]; + buffer[j] = encoding[v >> 2]; + buffer[j+1] = encoding[(v & 3) << 4]; + buffer[j+2] = '='; + buffer[j+3] = '='; + break; + case 2 : + v = text[i] << 8 | text[i+1]; + buffer[j] = encoding[v >> 10]; + buffer[j+1] = encoding[(v >> 4) & 0x3f]; + buffer[j+2] = encoding[(v & 0xf) << 2]; + buffer[j+3] = '='; + break; + } + lua_pushlstring(L, buffer, encode_sz); + return 1; +} + +static inline int +b64index(uint8_t c) { + static const int decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; + int decoding_size = sizeof(decoding)/sizeof(decoding[0]); + if (c<43) { + return -1; + } + c -= 43; + if (c>=decoding_size) + return -1; + return decoding[c]; +} + +static int +lb64decode(lua_State *L) { + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + int decode_sz = (sz+3)/4*3; + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (decode_sz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, decode_sz); + } + int i,j; + int output = 0; + for (i=0;i=sz) { + return luaL_error(L, "Invalid base64 text"); + } + c[j] = b64index(text[i]); + if (c[j] == -1) { + ++i; + continue; + } + if (c[j] == -2) { + ++padding; + } + ++i; + ++j; + } + uint32_t v; + switch (padding) { + case 0: + v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3]; + buffer[output] = v >> 16; + buffer[output+1] = (v >> 8) & 0xff; + buffer[output+2] = v & 0xff; + output += 3; + break; + case 1: + if (c[3] != -2 || (c[2] & 3)!=0) { + return luaL_error(L, "Invalid base64 text"); + } + v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2 ; + buffer[output] = v >> 8; + buffer[output+1] = v & 0xff; + output += 2; + break; + case 2: + if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) !=0) { + return luaL_error(L, "Invalid base64 text"); + } + v = (unsigned)c[0] << 2 | c[1] >> 4; + buffer[output] = v; + ++ output; + break; + default: + return luaL_error(L, "Invalid base64 text"); + } + } + lua_pushlstring(L, buffer, output); + return 1; +} + +static int +lxor_str(lua_State *L) { + size_t len1,len2; + const char *s1 = luaL_checklstring(L,1,&len1); + const char *s2 = luaL_checklstring(L,2,&len2); + if (len2 == 0) { + return luaL_error(L, "Can't xor empty string"); + } + luaL_Buffer b; + char * buffer = luaL_buffinitsize(L, &b, len1); + int i; + for (i=0;i +#include +#include + +#define NODECACHE "_ctable" +#define PROXYCACHE "_proxy" +#define TABLES "_ctables" + +#define VALUE_NIL 0 +#define VALUE_INTEGER 1 +#define VALUE_REAL 2 +#define VALUE_BOOLEAN 3 +#define VALUE_TABLE 4 +#define VALUE_STRING 5 +#define VALUE_INVALID 6 + +#define INVALID_OFFSET 0xffffffff + +struct proxy { + const char * data; + int index; +}; + +struct document { + uint32_t strtbl; + uint32_t n; + uint32_t index[1]; + // table[n] + // strings +}; + +struct table { + uint32_t array; + uint32_t dict; + uint8_t type[1]; + // value[array] + // kvpair[dict] +}; + +static inline const struct table * +gettable(const struct document *doc, int index) { + if (doc->index[index] == INVALID_OFFSET) { + return NULL; + } + return (const struct table *)((const char *)doc + sizeof(uint32_t) + sizeof(uint32_t) + doc->n * sizeof(uint32_t) + doc->index[index]); +} + +static void +create_proxy(lua_State *L, const void *data, int index) { + const struct table * t = gettable(data, index); + if (t == NULL) { + luaL_error(L, "Invalid index %d", index); + } + lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); + if (lua_rawgetp(L, -1, t) == LUA_TTABLE) { + lua_replace(L, -2); + return; + } + lua_pop(L, 1); + lua_newtable(L); + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + lua_pushvalue(L, -1); + // NODECACHE, table, table + lua_rawsetp(L, -3, t); + // NODECACHE, table + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + // NODECACHE, table, PROXYCACHE + lua_pushvalue(L, -2); + // NODECACHE, table, PROXYCACHE, table + struct proxy * p = lua_newuserdata(L, sizeof(struct proxy)); + // NODECACHE, table, PROXYCACHE, table, proxy + p->data = data; + p->index = index; + lua_rawset(L, -3); + // NODECACHE, table, PROXYCACHE + lua_pop(L, 1); + // NODECACHE, table + lua_replace(L, -2); + // table +} + +static void +clear_table(lua_State *L) { + int t = lua_gettop(L); // clear top table + if (lua_type(L, t) != LUA_TTABLE) { + luaL_error(L, "Invalid cache"); + } + lua_pushnil(L); + while (lua_next(L, t) != 0) { + // key value + lua_pop(L, 1); + lua_pushvalue(L, -1); + lua_pushnil(L); + // key key nil + lua_rawset(L, t); + // key + } +} + +static void +update_cache(lua_State *L, const void *data, const void * newdata) { + lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); + int t = lua_gettop(L); + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + int pt = t + 1; + lua_newtable(L); // temp table + int nt = pt + 1; + lua_pushnil(L); + while (lua_next(L, t) != 0) { + // pointer (-2) -> table (-1) + lua_pushvalue(L, -1); + if (lua_rawget(L, pt) == LUA_TUSERDATA) { + // pointer, table, proxy + struct proxy * p = lua_touserdata(L, -1); + if (p->data == data) { + // update to newdata + p->data = newdata; + const struct table * newt = gettable(newdata, p->index); + lua_pop(L, 1); + // pointer, table + clear_table(L); + lua_pushvalue(L, lua_upvalueindex(1)); + // pointer, table, meta + lua_setmetatable(L, -2); + // pointer, table + if (newt) { + lua_rawsetp(L, nt, newt); + } else { + lua_pop(L, 1); + } + // pointer + lua_pushvalue(L, -1); + lua_pushnil(L); + lua_rawset(L, t); + } else { + lua_pop(L, 2); + } + } else { + lua_pop(L, 2); + // pointer + } + } + // copy nt to t + lua_pushnil(L); + while (lua_next(L, nt) != 0) { + lua_pushvalue(L, -2); + lua_insert(L, -2); + // key key value + lua_rawset(L, t); + } + // NODECACHE PROXYCACHE TEMP + lua_pop(L, 3); +} + +static int +lupdate(lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + lua_pushvalue(L, 1); + // PROXYCACHE, table + if (lua_rawget(L, -2) != LUA_TUSERDATA) { + luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); + } + struct proxy * p = lua_touserdata(L, -1); + luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); + const char * newdata = lua_touserdata(L, 2); + update_cache(L, p->data, newdata); + return 1; +} + +static inline uint32_t +getuint32(const void *v) { + union { + uint32_t d; + uint8_t t[4]; + } test = { 1 }; + if (test.t[0] == 0) { + // big endian + test.d = *(const uint32_t *)v; + return test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; + } else { + return *(const uint32_t *)v; + } +} + +static inline float +getfloat(const void *v) { + union { + uint32_t d; + float f; + uint8_t t[4]; + } test = { 1 }; + if (test.t[0] == 0) { + // big endian + test.d = *(const uint32_t *)v; + test.d = test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; + return test.f; + } else { + return *(const float *)v; + } +} + +static void +pushvalue(lua_State *L, const void *v, int type, const struct document * doc) { + switch (type) { + case VALUE_NIL: + lua_pushnil(L); + break; + case VALUE_INTEGER: + lua_pushinteger(L, (int32_t)getuint32(v)); + break; + case VALUE_REAL: + lua_pushnumber(L, getfloat(v)); + break; + case VALUE_BOOLEAN: + lua_pushboolean(L, getuint32(v)); + break; + case VALUE_TABLE: + create_proxy(L, doc, getuint32(v)); + break; + case VALUE_STRING: + lua_pushstring(L, (const char *)doc + doc->strtbl + getuint32(v)); + break; + default: + luaL_error(L, "Invalid type %d at %p", type, v); + } +} + +static void +copytable(lua_State *L, int tbl, struct proxy *p) { + const struct document * doc = (const struct document *)p->data; + if (p->index < 0 || p->index >= doc->n) { + luaL_error(L, "Invalid proxy (index = %d, total = %d)", p->index, (int)doc->n); + } + const struct table * t = gettable(doc, p->index); + if (t == NULL) { + luaL_error(L, "Invalid proxy (index = %d)", p->index); + } + const uint32_t * v = (const uint32_t *)((const char *)t + sizeof(uint32_t) + sizeof(uint32_t) + ((t->array + t->dict + 3) & ~3)); + int i; + for (i=0;iarray;i++) { + pushvalue(L, v++, t->type[i], doc); + lua_rawseti(L, tbl, i+1); + } + for (i=0;idict;i++) { + pushvalue(L, v++, VALUE_STRING, doc); + pushvalue(L, v++, t->type[t->array+i], doc); + lua_rawset(L, tbl); + } +} + +static int +lnew(lua_State *L) { + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + const char * data = lua_touserdata(L, 1); + // hold ref to data + lua_getfield(L, LUA_REGISTRYINDEX, TABLES); + lua_pushvalue(L, 1); + lua_rawsetp(L, -2, data); + + create_proxy(L, data, 0); + return 1; +} + +static void +copyfromdata(lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + lua_pushvalue(L, 1); + // PROXYCACHE, table + if (lua_rawget(L, -2) != LUA_TUSERDATA) { + luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); + } + struct proxy * p = lua_touserdata(L, -1); + lua_pop(L, 2); + copytable(L, 1, p); + lua_pushnil(L); + lua_setmetatable(L, 1); // remove metatable +} + +static int +lindex(lua_State *L) { + copyfromdata(L); + lua_rawget(L, 1); + return 1; +} + +static int +lnext(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 2); /* create a 2nd argument if there isn't one */ + if (lua_next(L, 1)) + return 2; + else { + lua_pushnil(L); + return 1; + } +} + +static int +lpairs(lua_State *L) { + copyfromdata(L); + lua_pushcfunction(L, lnext); + lua_pushvalue(L, 1); + lua_pushnil(L); + return 3; +} + +static int +llen(lua_State *L) { + copyfromdata(L); + lua_pushinteger(L, lua_rawlen(L, 1)); + return 1; +} + +static void +new_weak_table(lua_State *L, const char *mode) { + lua_newtable(L); // NODECACHE { pointer:table } + + lua_createtable(L, 0, 1); // weak meta table + lua_pushstring(L, mode); + lua_setfield(L, -2, "__mode"); + + lua_setmetatable(L, -2); // make NODECACHE weak +} + +static void +gen_metatable(lua_State *L) { + new_weak_table(L, "kv"); // NODECACHE { pointer:table } + lua_setfield(L, LUA_REGISTRYINDEX, NODECACHE); + + new_weak_table(L, "k"); // PROXYCACHE { table:userdata } + lua_setfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + + lua_newtable(L); + lua_setfield(L, LUA_REGISTRYINDEX, TABLES); + + lua_createtable(L, 0, 1); // mod table + + lua_createtable(L, 0, 2); // metatable + luaL_Reg l[] = { + { "__index", lindex }, + { "__pairs", lpairs }, + { "__len", llen }, + { NULL, NULL }, + }; + lua_pushvalue(L, -1); + luaL_setfuncs(L, l, 1); +} + +static int +lstringpointer(lua_State *L) { + const char * str = luaL_checkstring(L, 1); + lua_pushlightuserdata(L, (void *)str); + return 1; +} + +LUAMOD_API int +luaopen_skynet_datasheet_core(lua_State *L) { + luaL_checkversion(L); + luaL_Reg l[] = { + { "new", lnew }, + { "update", lupdate }, + { NULL, NULL }, + }; + + luaL_newlibtable(L,l); + gen_metatable(L); + luaL_setfuncs(L, l, 1); + lua_pushcfunction(L, lstringpointer); + lua_setfield(L, -2, "stringpointer"); + return 1; +} diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c new file mode 100644 index 00000000..7f83c9e9 --- /dev/null +++ b/lualib-src/lua-debugchannel.c @@ -0,0 +1,285 @@ +#define LUA_LIB + +// only for debug use +#include +#include +#include +#include +#include +#include "spinlock.h" + +#define METANAME "debugchannel" + +struct command { + struct command * next; + size_t sz; +}; + +struct channel { + struct spinlock lock; + int ref; + struct command * head; + struct command * tail; +}; + +static struct channel * +channel_new() { + struct channel * c = malloc(sizeof(*c)); + memset(c, 0 , sizeof(*c)); + c->ref = 1; + SPIN_INIT(c) + + return c; +} + +static struct channel * +channel_connect(struct channel *c) { + struct channel * ret = NULL; + SPIN_LOCK(c) + if (c->ref == 1) { + ++c->ref; + ret = c; + } + SPIN_UNLOCK(c) + return ret; +} + +static struct channel * +channel_release(struct channel *c) { + SPIN_LOCK(c) + --c->ref; + if (c->ref > 0) { + SPIN_UNLOCK(c) + return c; + } + // never unlock while reference is 0 + struct command * p = c->head; + c->head = NULL; + c->tail = NULL; + while(p) { + struct command *next = p->next; + free(p); + p = next; + } + SPIN_UNLOCK(c) + SPIN_DESTROY(c) + free(c); + return NULL; +} + +// call free after channel_read +static struct command * +channel_read(struct channel *c, double timeout) { + struct command * ret = NULL; + SPIN_LOCK(c) + if (c->head == NULL) { + SPIN_UNLOCK(c) + int ti = (int)(timeout * 100000); + usleep(ti); + return NULL; + } + ret = c->head; + c->head = ret->next; + if (c->head == NULL) { + c->tail = NULL; + } + SPIN_UNLOCK(c) + + return ret; +} + +static void +channel_write(struct channel *c, const char * s, size_t sz) { + struct command * cmd = malloc(sizeof(*cmd)+ sz); + cmd->sz = sz; + cmd->next = NULL; + memcpy(cmd+1, s, sz); + SPIN_LOCK(c) + if (c->tail == NULL) { + c->head = c->tail = cmd; + } else { + c->tail->next = cmd; + c->tail = cmd; + } + SPIN_UNLOCK(c) +} + +struct channel_box { + struct channel *c; +}; + +static int +lread(lua_State *L) { + struct channel_box *cb = luaL_checkudata(L,1, METANAME); + double ti = luaL_optnumber(L, 2, 0); + struct command * c = channel_read(cb->c, ti); + if (c == NULL) + return 0; + lua_pushlstring(L, (const char *)(c+1), c->sz); + free(c); + return 1; +} + +static int +lwrite(lua_State *L) { + struct channel_box *cb = luaL_checkudata(L,1, METANAME); + size_t sz; + const char * str = luaL_checklstring(L, 2, &sz); + channel_write(cb->c, str, sz); + return 0; +} + +static int +lrelease(lua_State *L) { + struct channel_box *cb = lua_touserdata(L, 1); + if (cb) { + if (channel_release(cb->c) == NULL) { + cb->c = NULL; + } + } + + return 0; +} + +static struct channel * +new_channel(lua_State *L, struct channel *c) { + if (c == NULL) { + c = channel_new(); + } else { + c = channel_connect(c); + } + if (c == NULL) { + luaL_error(L, "new channel failed"); + // never go here + } + struct channel_box * cb = lua_newuserdata(L, sizeof(*cb)); + cb->c = c; + if (luaL_newmetatable(L, METANAME)) { + luaL_Reg l[]={ + { "read", lread }, + { "write", lwrite }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, lrelease); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + return c; +} + +static int +lcreate(lua_State *L) { + struct channel *c = new_channel(L, NULL); + lua_pushlightuserdata(L, c); + return 2; +} + +static int +lconnect(lua_State *L) { + struct channel *c = lua_touserdata(L, 1); + if (c == NULL) + return luaL_error(L, "Invalid channel pointer"); + new_channel(L, c); + + return 1; +} + +static const int HOOKKEY = 0; + +/* +** Auxiliary function used by several library functions: check for +** an optional thread as function's first argument and set 'arg' with +** 1 if this argument is present (so that functions can skip it to +** access their other arguments) +*/ +static lua_State *getthread (lua_State *L, int *arg) { + if (lua_isthread(L, 1)) { + *arg = 1; + return lua_tothread(L, 1); + } + else { + *arg = 0; + return L; /* function will operate over current thread */ + } +} + +/* +** Call hook function registered at hook table for the current +** thread (if there is one) +*/ +static void hookf (lua_State *L, lua_Debug *ar) { + static const char *const hooknames[] = + {"call", "return", "line", "count", "tail call"}; + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_pushthread(L); + if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ + lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ + if (ar->currentline >= 0) + lua_pushinteger(L, ar->currentline); /* push current line */ + else lua_pushnil(L); + lua_call(L, 2, 1); /* call hook function */ + int yield = lua_toboolean(L, -1); + lua_pop(L,1); + if (yield) { + lua_yield(L, 0); + } + } +} + +/* +** Convert a string mask (for 'sethook') into a bit mask +*/ +static int makemask (const char *smask, int count) { + int mask = 0; + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; + if (strchr(smask, 'r')) mask |= LUA_MASKRET; + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; + if (count > 0) mask |= LUA_MASKCOUNT; + return mask; +} + +static int db_sethook (lua_State *L) { + int arg, mask, count; + lua_Hook func; + lua_State *L1 = getthread(L, &arg); + if (lua_isnoneornil(L, arg+1)) { /* no hook? */ + lua_settop(L, arg+1); + func = NULL; mask = 0; count = 0; /* turn off hooks */ + } + else { + const char *smask = luaL_checkstring(L, arg+2); + luaL_checktype(L, arg+1, LUA_TFUNCTION); + count = (int)luaL_optinteger(L, arg + 3, 0); + func = hookf; mask = makemask(smask, count); + } + if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { + lua_createtable(L, 0, 2); /* create a hook table */ + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ + lua_pushstring(L, "k"); + lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ + lua_pushvalue(L, -1); + lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + } + lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ + lua_pushvalue(L, arg + 1); /* value (hook function) */ + lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ + lua_sethook(L1, func, mask, count); + return 0; +} + +LUAMOD_API int +luaopen_skynet_debugchannel(lua_State *L) { + luaL_Reg l[] = { + { "create", lcreate }, // for write + { "connect", lconnect }, // for read + { "release", lrelease }, + { "sethook", db_sethook }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L,l); + return 1; +} diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index fa7c026c..5fc75752 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -1,12 +1,15 @@ +#define LUA_LIB + #include #include #include "malloc_hook.h" +#include "luashrtbl.h" static int ltotal(lua_State *L) { size_t t = malloc_used_memory(); - lua_pushunsigned(L, t); + lua_pushinteger(L, (lua_Integer)t); return 1; } @@ -14,7 +17,7 @@ ltotal(lua_State *L) { static int lblock(lua_State *L) { size_t t = malloc_memory_block(); - lua_pushunsigned(L, t); + lua_pushinteger(L, (lua_Integer)t); return 1; } @@ -33,8 +36,21 @@ ldump(lua_State *L) { return 0; } -int -luaopen_memory(lua_State *L) { +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()); + return 1; +} + +LUAMOD_API int +luaopen_skynet_memory(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { @@ -42,6 +58,10 @@ luaopen_memory(lua_State *L) { { "block", lblock }, { "dumpinfo", ldumpinfo }, { "dump", ldump }, + { "info", dump_mem_lua }, + { "ssinfo", luaS_shrinfo }, + { "ssexpand", lexpandshrtbl }, + { "current", lcurrent }, { NULL, NULL }, }; diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index a0d312fd..a8ca0b0c 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet_malloc.h" #include @@ -260,6 +262,13 @@ op_reply(lua_State *L) { lua_pushnil(L); lua_rawseti(L, 2, i); } + } else { + if (sz >= 4) { + sz -= get_length((document)doc); + } + } + if (sz != 0) { + return luaL_error(L, "Invalid result bson document"); } lua_pushboolean(L,1); lua_pushinteger(L, id); @@ -499,9 +508,9 @@ op_insert(lua_State *L) { int i; for (i=1;i<=s;i++) { lua_rawgeti(L,3,i); - document doc = lua_touserdata(L,3); + document doc = lua_touserdata(L,-1); + lua_pop(L,1); // must call lua_pop before luaL_addlstring, because addlstring may change stack top luaL_addlstring(&b, (const char *)doc, get_length(doc)); - lua_pop(L,1); } } @@ -522,8 +531,8 @@ reply_length(lua_State *L) { return 1; } -int -luaopen_mongo_driver(lua_State *L) { +LUAMOD_API int +luaopen_skynet_mongo_driver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] ={ { "query", op_query }, diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 25b9d936..8903b36e 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet.h" #include @@ -5,6 +7,8 @@ #include #include +#include "atomic.h" + struct mc_package { int reference; uint32_t size; @@ -33,7 +37,7 @@ pack(lua_State *L, void *data, size_t size) { static int mc_packlocal(lua_State *L) { void * data = lua_touserdata(L, 1); - size_t size = luaL_checkunsigned(L, 2); + size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } @@ -49,7 +53,7 @@ mc_packlocal(lua_State *L) { static int mc_packremote(lua_State *L) { void * data = lua_touserdata(L, 1); - size_t size = luaL_checkunsigned(L, 2); + size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } @@ -58,18 +62,6 @@ mc_packremote(lua_State *L) { return pack(L, msg, size); } -static int -mc_packstring(lua_State *L) { - size_t size; - const char * msg = luaL_checklstring(L, 1, &size); - if (size != (uint32_t)size) { - return luaL_error(L, "string is too long"); - } - void * data = skynet_malloc(size); - memcpy(data, msg, size); - return pack(L, data, size); -} - /* lightuserdata struct mc_package ** integer size (must be sizeof(struct mc_package *) @@ -80,18 +72,20 @@ static int mc_unpacklocal(lua_State *L) { struct mc_package ** pack = lua_touserdata(L,1); int sz = luaL_checkinteger(L,2); - if (sz != sizeof(*pack)) { + if (sz != sizeof(pack)) { return luaL_error(L, "Invalid multicast package size %d", sz); } - lua_settop(L, 1); + lua_pushlightuserdata(L, *pack); lua_pushlightuserdata(L, (*pack)->data); - lua_pushunsigned(L, (*pack)->size); + lua_pushinteger(L, (lua_Integer)((*pack)->size)); return 3; } /* lightuserdata struct mc_package ** integer reference + + return mc_package * */ static int mc_bindrefer(lua_State *L) { @@ -102,18 +96,21 @@ mc_bindrefer(lua_State *L) { } (*pack)->reference = ref; - return 0; + lua_pushlightuserdata(L, *pack); + + skynet_free(pack); + + return 1; } /* - lightuserdata struct mc_package ** + lightuserdata struct mc_package * */ static int mc_closelocal(lua_State *L) { - struct mc_package **ptr = lua_touserdata(L,1); - struct mc_package *pack = *ptr; + struct mc_package *pack = lua_touserdata(L,1); - int ref = __sync_sub_and_fetch(&pack->reference, 1); + int ref = ATOM_DEC(&pack->reference); if (ref <= 0) { skynet_free(pack->data); skynet_free(pack); @@ -134,29 +131,28 @@ mc_remote(lua_State *L) { struct mc_package **ptr = lua_touserdata(L,1); struct mc_package *pack = *ptr; lua_pushlightuserdata(L, pack->data); - lua_pushunsigned(L, pack->size); + lua_pushinteger(L, (lua_Integer)(pack->size)); skynet_free(pack); return 2; } static int mc_nextid(lua_State *L) { - uint32_t id = luaL_checkunsigned(L, 1); + uint32_t id = (uint32_t)luaL_checkinteger(L, 1); id += 256; - lua_pushunsigned(L, id); + lua_pushinteger(L, (uint32_t)id); return 1; } -int -luaopen_multicast_c(lua_State *L) { +LUAMOD_API int +luaopen_skynet_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, { "unpack", mc_unpacklocal }, { "bind", mc_bindrefer }, { "close", mc_closelocal }, { "remote", mc_remote }, - { "packstring", mc_packstring }, { "packremote", mc_packremote }, { "nextid", mc_nextid }, { NULL, NULL }, diff --git a/lualib-src/lua-mysqlaux.c b/lualib-src/lua-mysqlaux.c new file mode 100644 index 00000000..46fbc67d --- /dev/null +++ b/lualib-src/lua-mysqlaux.c @@ -0,0 +1,166 @@ +#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-src/lua-netpack.c b/lualib-src/lua-netpack.c index a95ed4d7..7f9a7b64 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet_malloc.h" #include "skynet_socket.h" @@ -19,6 +21,7 @@ #define TYPE_ERROR 3 #define TYPE_OPEN 4 #define TYPE_CLOSE 5 +#define TYPE_WARNING 6 /* Each package is uint16 + data , uint16 (serialized in big-endian) is the number of bytes comprising the data . @@ -48,6 +51,7 @@ struct queue { static void clear_list(struct uncomplete * uc) { while (uc) { + skynet_free(uc->pack.buffer); void * tmp = uc; uc = uc->next; skynet_free(tmp); @@ -210,6 +214,16 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) { } } +static void +close_uncomplete(lua_State *L, int fd) { + struct queue *q = lua_touserdata(L,1); + struct uncomplete * uc = find_uncomplete(q, fd); + if (uc) { + skynet_free(uc->pack.buffer); + skynet_free(uc); + } +} + static int filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { struct queue *q = lua_touserdata(L,1); @@ -302,9 +316,9 @@ filter_data(lua_State *L, int fd, uint8_t * buffer, int size) { } static void -pushstring(lua_State *L, const char * msg) { +pushstring(lua_State *L, const char * msg, int size) { if (msg) { - lua_pushstring(L, msg); + lua_pushlstring(L, msg, size); } else { lua_pushliteral(L, ""); } @@ -343,6 +357,8 @@ lfilter(lua_State *L) { // ignore listen fd connect return 1; case SKYNET_SOCKET_TYPE_CLOSE: + // no more data in fd (message->id) + close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE)); lua_pushinteger(L, message->id); return 3; @@ -350,12 +366,19 @@ lfilter(lua_State *L) { lua_pushvalue(L, lua_upvalueindex(TYPE_OPEN)); // ignore listen id (message->id); lua_pushinteger(L, message->ud); - pushstring(L, buffer); + pushstring(L, buffer, size); return 4; case SKYNET_SOCKET_TYPE_ERROR: + // no more data in fd (message->id) + close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR)); lua_pushinteger(L, message->id); - pushstring(L, buffer); + pushstring(L, buffer, size); + return 4; + case SKYNET_SOCKET_TYPE_WARNING: + lua_pushvalue(L, lua_upvalueindex(TYPE_WARNING)); + lua_pushinteger(L, message->id); + lua_pushinteger(L, message->ud); return 4; default: // never get here @@ -393,13 +416,13 @@ lpop(lua_State *L) { */ static const char * -tolstring(lua_State *L, size_t *sz) { +tolstring(lua_State *L, size_t *sz, int index) { const char * ptr; - if (lua_isuserdata(L,1)) { - ptr = (const char *)lua_touserdata(L,1); - *sz = (size_t)luaL_checkinteger(L, 2); + if (lua_isuserdata(L,index)) { + ptr = (const char *)lua_touserdata(L,index); + *sz = (size_t)luaL_checkinteger(L, index+1); } else { - ptr = luaL_checklstring(L, 1, sz); + ptr = luaL_checklstring(L, index, sz); } return ptr; } @@ -413,8 +436,8 @@ write_size(uint8_t * buffer, int len) { static int lpack(lua_State *L) { size_t len; - const char * ptr = tolstring(L, &len); - if (len > 0x10000) { + const char * ptr = tolstring(L, &len, 1); + if (len >= 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); } @@ -428,29 +451,6 @@ lpack(lua_State *L) { return 2; } -static int -lpack_string(lua_State *L) { - uint8_t tmp[SMALLSTRING+2]; - size_t len; - uint8_t *buffer; - const char * ptr = tolstring(L, &len); - if (len > 0x10000) { - return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); - } - - if (len <= SMALLSTRING) { - buffer = tmp; - } else { - buffer = lua_newuserdata(L, len + 2); - } - - write_size(buffer, len); - memcpy(buffer+2, ptr, len); - lua_pushlstring(L, (const char *)buffer, len+2); - - return 1; -} - static int ltostring(lua_State *L) { void * ptr = lua_touserdata(L, 1); @@ -464,13 +464,12 @@ ltostring(lua_State *L) { return 1; } -int -luaopen_netpack(lua_State *L) { +LUAMOD_API int +luaopen_skynet_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop }, { "pack", lpack }, - { "pack_string", lpack_string }, { "clear", lclear }, { "tostring", ltostring }, { NULL, NULL }, @@ -483,8 +482,9 @@ luaopen_netpack(lua_State *L) { lua_pushliteral(L, "error"); lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); + lua_pushliteral(L, "warning"); - lua_pushcclosure(L, lfilter, 5); + lua_pushcclosure(L, lfilter, 6); lua_setfield(L, -2, "filter"); return 1; diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index f0f11270..9d9b261d 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -1,3 +1,6 @@ +#define LUA_LIB + +#include #include #include @@ -11,6 +14,8 @@ #define NANOSEC 1000000000 #define MICROSEC 1000000 +// #define DEBUG_LOG + static double get_time() { #if !defined(__APPLE__) @@ -47,17 +52,27 @@ diff_time(double start) { static int lstart(lua_State *L) { - lua_pushthread(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_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnumber(L, 0); lua_rawset(L, lua_upvalueindex(2)); - lua_pushthread(L); - lua_pushnumber(L, get_time()); + 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; @@ -65,37 +80,51 @@ lstart(lua_State *L) { static int lstop(lua_State *L) { - lua_pushthread(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)); - luaL_checktype(L, -1, LUA_TNUMBER); + if (lua_type(L, -1) != LUA_TNUMBER) { + return luaL_error(L, "Call profile.start() before profile.stop()"); + } double ti = diff_time(lua_tonumber(L, -1)); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_rawget(L, lua_upvalueindex(2)); double total_time = lua_tonumber(L, -1); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(1)); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(2)); - lua_pushnumber(L, ti + total_time); + total_time += ti; + lua_pushnumber(L, total_time); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] stop (%lf/%lf)\n", lua_tothread(L,1), ti, total_time); +#endif return 1; } static int -lresume(lua_State *L) { - lua_pushvalue(L,1); +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,1); + lua_pop(L,2); // pop from coroutine } else { lua_pop(L,1); - lua_pushvalue(L,1); double ti = get_time(); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] resume %lf\n", lua_tothread(L, -1), ti); +#endif lua_pushnumber(L, ti); lua_rawset(L, lua_upvalueindex(1)); // set start time } @@ -106,25 +135,48 @@ lresume(lua_State *L) { } static int -lyield(lua_State *L) { - lua_pushthread(L); +lresume(lua_State *L) { + lua_pushvalue(L,1); + + return timing_resume(L); +} + +static int +lresume_co(lua_State *L) { + luaL_checktype(L, 2, LUA_TTHREAD); + lua_rotate(L, 2, -1); // '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,1); + lua_pop(L,2); } else { double ti = lua_tonumber(L, -1); lua_pop(L,1); - lua_pushthread(L); + lua_pushvalue(L, -1); // push coroutine lua_rawget(L, lua_upvalueindex(1)); double starttime = lua_tonumber(L, -1); lua_pop(L,1); - ti += diff_time(starttime); + double diff = diff_time(starttime); + ti += diff; +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", from, diff, ti); +#endif - lua_pushthread(L); + lua_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)); @@ -132,14 +184,31 @@ lyield(lua_State *L) { return co_yield(L); } -int -luaopen_profile(lua_State *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); @@ -154,7 +223,7 @@ luaopen_profile(lua_State *L) { lua_setmetatable(L, -3); lua_setmetatable(L, -3); - lua_pushnil(L); + lua_pushnil(L); // cfunction (coroutine.resume or coroutine.yield) luaL_setfuncs(L,l,3); int libtable = lua_gettop(L); @@ -166,20 +235,33 @@ luaopen_profile(lua_State *L) { if (co_resume == NULL) return luaL_error(L, "Can't get coroutine.resume"); lua_pop(L,1); + lua_getfield(L, libtable, "resume"); lua_pushcfunction(L, co_resume); lua_setupvalue(L, -2, 3); lua_pop(L,1); + lua_getfield(L, libtable, "resume_co"); + lua_pushcfunction(L, co_resume); + lua_setupvalue(L, -2, 3); + lua_pop(L,1); + lua_getfield(L, -1, "yield"); lua_CFunction co_yield = lua_tocfunction(L, -1); if (co_yield == NULL) return luaL_error(L, "Can't get coroutine.yield"); lua_pop(L,1); + lua_getfield(L, libtable, "yield"); lua_pushcfunction(L, co_yield); lua_setupvalue(L, -2, 3); + lua_pop(L,1); + + lua_getfield(L, libtable, "yield_co"); + lua_pushcfunction(L, co_yield); + lua_setupvalue(L, -2, 3); + lua_pop(L,1); lua_settop(L, libtable); diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 69e1c2af..e6dd531f 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -1,7 +1,9 @@ /* - https://github.com/cloudwu/lua-serialize + modify from https://github.com/cloudwu/lua-serialize */ +#define LUA_LIB + #include "skynet_malloc.h" #include @@ -15,7 +17,14 @@ #define TYPE_BOOLEAN 1 // hibits 0 false 1 true #define TYPE_NUMBER 2 -// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 8 : double +// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double +#define TYPE_NUMBER_ZERO 0 +#define TYPE_NUMBER_BYTE 1 +#define TYPE_NUMBER_WORD 2 +#define TYPE_NUMBER_DWORD 4 +#define TYPE_NUMBER_QWORD 6 +#define TYPE_NUMBER_REAL 8 + #define TYPE_USERDATA 3 #define TYPE_SHORT_STRING 4 // hibits 0~31 : len @@ -35,14 +44,13 @@ struct block { struct write_block { struct block * head; - int len; struct block * current; + int len; int ptr; }; struct read_block { char * buffer; - struct block * current; int len; int ptr; }; @@ -78,38 +86,17 @@ _again: static void wb_init(struct write_block *wb , struct block *b) { - if (b==NULL) { - wb->head = blk_alloc(); - wb->len = 0; - wb->current = wb->head; - wb->ptr = 0; - wb_push(wb, &wb->len, sizeof(wb->len)); - } else { - wb->head = b; - int * plen = (int *)b->buffer; - int sz = *plen; - wb->len = sz; - while (b->next) { - sz -= BLOCK_SIZE; - b = b->next; - } - wb->current = b; - wb->ptr = sz; - } -} - -static struct block * -wb_close(struct write_block *b) { - b->current = b->head; - b->ptr = 0; - wb_push(b, &b->len, sizeof(b->len)); - b->current = NULL; - return b->head; + wb->head = b; + assert(b->next == NULL); + wb->len = 0; + wb->current = wb->head; + wb->ptr = 0; } static void wb_free(struct write_block *wb) { struct block *blk = wb->head; + blk = blk->next; // the first block is on stack while (blk) { struct block * next = blk->next; skynet_free(blk); @@ -124,124 +111,78 @@ wb_free(struct write_block *wb) { static void rball_init(struct read_block * rb, char * buffer, int size) { rb->buffer = buffer; - rb->current = NULL; rb->len = size; rb->ptr = 0; } static void * -rb_read(struct read_block *rb, void *buffer, int sz) { +rb_read(struct read_block *rb, int sz) { if (rb->len < sz) { return NULL; } - if (rb->buffer) { - int ptr = rb->ptr; - rb->ptr += sz; - rb->len -= sz; - return rb->buffer + ptr; - } - - if (rb->ptr == BLOCK_SIZE) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - rb->ptr = 0; - } - - int copy = BLOCK_SIZE - rb->ptr; - - if (sz <= copy) { - void * ret = rb->current->buffer + rb->ptr; - rb->ptr += sz; - rb->len -= sz; - return ret; - } - - char * tmp = buffer; - - memcpy(tmp, rb->current->buffer + rb->ptr, copy); - sz -= copy; - tmp += copy; - rb->len -= copy; - - for (;;) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - - if (sz < BLOCK_SIZE) { - memcpy(tmp, rb->current->buffer, sz); - rb->ptr = sz; - rb->len -= sz; - return buffer; - } - memcpy(tmp, rb->current->buffer, BLOCK_SIZE); - sz -= BLOCK_SIZE; - tmp += BLOCK_SIZE; - rb->len -= BLOCK_SIZE; - } -} - -static void -rb_close(struct read_block *rb) { - while (rb->current) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - } - rb->len = 0; - rb->ptr = 0; + int ptr = rb->ptr; + rb->ptr += sz; + rb->len -= sz; + return rb->buffer + ptr; } static inline void wb_nil(struct write_block *wb) { - int n = TYPE_NIL; + uint8_t n = TYPE_NIL; wb_push(wb, &n, 1); } static inline void wb_boolean(struct write_block *wb, int boolean) { - int n = COMBINE_TYPE(TYPE_BOOLEAN , boolean ? 1 : 0); + uint8_t n = COMBINE_TYPE(TYPE_BOOLEAN , boolean ? 1 : 0); wb_push(wb, &n, 1); } static inline void -wb_integer(struct write_block *wb, int v, int type) { +wb_integer(struct write_block *wb, lua_Integer v) { + int type = TYPE_NUMBER; if (v == 0) { - int n = COMBINE_TYPE(type , 0); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_ZERO); wb_push(wb, &n, 1); - } else if (v<0) { - int n = COMBINE_TYPE(type , 4); + } else if (v != (int32_t)v) { + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_QWORD); + int64_t v64 = v; wb_push(wb, &n, 1); - wb_push(wb, &v, 4); + wb_push(wb, &v64, sizeof(v64)); + } else if (v < 0) { + int32_t v32 = (int32_t)v; + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); + wb_push(wb, &n, 1); + wb_push(wb, &v32, sizeof(v32)); } else if (v<0x100) { - int n = COMBINE_TYPE(type , 1); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_BYTE); wb_push(wb, &n, 1); uint8_t byte = (uint8_t)v; - wb_push(wb, &byte, 1); + wb_push(wb, &byte, sizeof(byte)); } else if (v<0x10000) { - int n = COMBINE_TYPE(type , 2); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_WORD); wb_push(wb, &n, 1); uint16_t word = (uint16_t)v; - wb_push(wb, &word, 2); + wb_push(wb, &word, sizeof(word)); } else { - int n = COMBINE_TYPE(type , 4); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); wb_push(wb, &n, 1); - wb_push(wb, &v, 4); + uint32_t v32 = (uint32_t)v; + wb_push(wb, &v32, sizeof(v32)); } } static inline void -wb_number(struct write_block *wb, double v) { - int n = COMBINE_TYPE(TYPE_NUMBER , 8); +wb_real(struct write_block *wb, double v) { + uint8_t n = COMBINE_TYPE(TYPE_NUMBER , TYPE_NUMBER_REAL); wb_push(wb, &n, 1); - wb_push(wb, &v, 8); + wb_push(wb, &v, sizeof(v)); } static inline void wb_pointer(struct write_block *wb, void *v) { - int n = TYPE_USERDATA; + uint8_t n = TYPE_USERDATA; wb_push(wb, &n, 1); wb_push(wb, &v, sizeof(v)); } @@ -249,13 +190,13 @@ wb_pointer(struct write_block *wb, void *v) { static inline void wb_string(struct write_block *wb, const char *str, int len) { if (len < MAX_COOKIE) { - int n = COMBINE_TYPE(TYPE_SHORT_STRING, len); + uint8_t n = COMBINE_TYPE(TYPE_SHORT_STRING, len); wb_push(wb, &n, 1); if (len > 0) { wb_push(wb, str, len); } } else { - int n; + uint8_t n; if (len < 0x10000) { n = COMBINE_TYPE(TYPE_LONG_STRING, 2); wb_push(wb, &n, 1); @@ -271,24 +212,24 @@ wb_string(struct write_block *wb, const char *str, int len) { } } -static void _pack_one(lua_State *L, struct write_block *b, int index, int depth); +static void pack_one(lua_State *L, struct write_block *b, int index, int depth); static int wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { int array_size = lua_rawlen(L,index); if (array_size >= MAX_COOKIE-1) { - int n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); + uint8_t n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); wb_push(wb, &n, 1); - wb_integer(wb, array_size,TYPE_NUMBER); + wb_integer(wb, array_size); } else { - int n = COMBINE_TYPE(TYPE_TABLE, array_size); + uint8_t n = COMBINE_TYPE(TYPE_TABLE, array_size); wb_push(wb, &n, 1); } int i; for (i=1;i<=array_size;i++) { lua_rawgeti(L,index,i); - _pack_one(L, wb, -1, depth); + pack_one(L, wb, -1, depth); lua_pop(L,1); } @@ -300,15 +241,39 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a lua_pushnil(L); while (lua_next(L, index) != 0) { if (lua_type(L,-2) == LUA_TNUMBER) { - lua_Number k = lua_tonumber(L,-2); - int32_t x = (int32_t)lua_tointeger(L,-2); - if (k == (lua_Number)x && x>0 && x<=array_size) { - lua_pop(L,1); - continue; + if (lua_isinteger(L, -2)) { + lua_Integer x = lua_tointeger(L,-2); + if (x>0 && x<=array_size) { + lua_pop(L,1); + continue; + } } } - _pack_one(L,wb,-2,depth); - _pack_one(L,wb,-1,depth); + pack_one(L,wb,-2,depth); + pack_one(L,wb,-1,depth); + lua_pop(L, 1); + } + wb_nil(wb); +} + +static void +wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { + uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); + wb_push(wb, &n, 1); + lua_pushvalue(L, index); + lua_call(L, 1, 3); + for(;;) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_copy(L, -5, -3); + lua_call(L, 2, 2); + int type = lua_type(L, -2); + if (type == LUA_TNIL) { + lua_pop(L, 4); + break; + } + pack_one(L, wb, -2, depth); + pack_one(L, wb, -1, depth); lua_pop(L, 1); } wb_nil(wb); @@ -316,15 +281,20 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a static void wb_table(lua_State *L, struct write_block *wb, int index, int depth) { + luaL_checkstack(L, LUA_MINSTACK, NULL); if (index < 0) { index = lua_gettop(L) + index + 1; } - int array_size = wb_table_array(L, wb, index, depth); - wb_table_hash(L, wb, index, depth, array_size); + if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { + 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); + } } static void -_pack_one(lua_State *L, struct write_block *b, int index, int depth) { +pack_one(lua_State *L, struct write_block *b, int index, int depth) { if (depth > MAX_DEPTH) { wb_free(b); luaL_error(L, "serialize can't pack too depth table"); @@ -335,12 +305,12 @@ _pack_one(lua_State *L, struct write_block *b, int index, int depth) { wb_nil(b); break; case LUA_TNUMBER: { - int32_t x = (int32_t)lua_tointeger(L,index); - lua_Number n = lua_tonumber(L,index); - if ((lua_Number)x==n) { - wb_integer(b, x, TYPE_NUMBER); + if (lua_isinteger(L, index)) { + lua_Integer x = lua_tointeger(L,index); + wb_integer(b, x); } else { - wb_number(b,n); + lua_Number n = lua_tonumber(L,index); + wb_real(b,n); } break; } @@ -370,121 +340,132 @@ _pack_one(lua_State *L, struct write_block *b, int index, int depth) { } static void -_pack_from(lua_State *L, struct write_block *b, int from) { +pack_from(lua_State *L, struct write_block *b, int from) { int n = lua_gettop(L) - from; int i; for (i=1;i<=n;i++) { - _pack_one(L, b , from + i, 0); + pack_one(L, b , from + i, 0); } } static inline void -__invalid_stream(lua_State *L, struct read_block *rb, int line) { +invalid_stream_line(lua_State *L, struct read_block *rb, int line) { int len = rb->len; - if (rb->buffer == NULL) { - rb_close(rb); - } luaL_error(L, "Invalid serialize stream %d (line:%d)", len, line); } -#define _invalid_stream(L,rb) __invalid_stream(L,rb,__LINE__) +#define invalid_stream(L,rb) invalid_stream_line(L,rb,__LINE__) -static int -_get_integer(lua_State *L, struct read_block *rb, int cookie) { +static lua_Integer +get_integer(lua_State *L, struct read_block *rb, int cookie) { switch (cookie) { - case 0: + case TYPE_NUMBER_ZERO: return 0; - case 1: { - uint8_t n = 0; - uint8_t * pn = rb_read(rb,&n,1); + case TYPE_NUMBER_BYTE: { + uint8_t n; + uint8_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) - _invalid_stream(L,rb); - return *pn; + invalid_stream(L,rb); + n = *pn; + return n; } - case 2: { - uint16_t n = 0; - uint16_t * pn = rb_read(rb,&n,2); + case TYPE_NUMBER_WORD: { + uint16_t n; + uint16_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) - _invalid_stream(L,rb); - return *pn; + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; } - case 4: { - int n = 0; - int * pn = rb_read(rb,&n,4); + case TYPE_NUMBER_DWORD: { + int32_t n; + int32_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) - _invalid_stream(L,rb); - return *pn; + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; + } + case TYPE_NUMBER_QWORD: { + int64_t n; + int64_t * pn = rb_read(rb,sizeof(n)); + if (pn == NULL) + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; } default: - _invalid_stream(L,rb); + invalid_stream(L,rb); return 0; } } static double -_get_number(lua_State *L, struct read_block *rb, int cookie) { - if (cookie == 8) { - double n = 0; - double * pn = rb_read(rb,&n,8); - if (pn == NULL) - _invalid_stream(L,rb); - return *pn; - } else { - return (double)_get_integer(L,rb,cookie); - } +get_real(lua_State *L, struct read_block *rb) { + double n; + double * pn = rb_read(rb,sizeof(n)); + if (pn == NULL) + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; } static void * -_get_pointer(lua_State *L, struct read_block *rb) { +get_pointer(lua_State *L, struct read_block *rb) { void * userdata = 0; - void ** v = (void **)rb_read(rb,&userdata,sizeof(userdata)); + void ** v = (void **)rb_read(rb,sizeof(userdata)); if (v == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - return *v; + memcpy(&userdata, v, sizeof(userdata)); + return userdata; } static void -_get_buffer(lua_State *L, struct read_block *rb, int len) { - char tmp[len]; - char * p = rb_read(rb,tmp,len); +get_buffer(lua_State *L, struct read_block *rb, int len) { + char * p = rb_read(rb,len); if (p == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } lua_pushlstring(L,p,len); } -static void _unpack_one(lua_State *L, struct read_block *rb); +static void unpack_one(lua_State *L, struct read_block *rb); static void -_unpack_table(lua_State *L, struct read_block *rb, int array_size) { +unpack_table(lua_State *L, struct read_block *rb, int array_size) { if (array_size == MAX_COOKIE-1) { - uint8_t type = 0; - uint8_t *t = rb_read(rb, &type, 1); - if (t==NULL || (*t & 7) != TYPE_NUMBER) { - _invalid_stream(L,rb); + uint8_t type; + uint8_t *t = rb_read(rb, sizeof(type)); + if (t==NULL) { + invalid_stream(L,rb); } - array_size = (int)_get_number(L,rb,*t >> 3); + type = *t; + int cookie = type >> 3; + if ((type & 7) != TYPE_NUMBER || cookie == TYPE_NUMBER_REAL) { + invalid_stream(L,rb); + } + array_size = get_integer(L,rb,cookie); } + luaL_checkstack(L,LUA_MINSTACK,NULL); lua_createtable(L,array_size,0); int i; for (i=1;i<=array_size;i++) { - _unpack_one(L,rb); + unpack_one(L,rb); lua_rawseti(L,-2,i); } for (;;) { - _unpack_one(L,rb); + unpack_one(L,rb); if (lua_isnil(L,-1)) { lua_pop(L,1); return; } - _unpack_one(L,rb); + unpack_one(L,rb); lua_rawset(L,-3); } } static void -_push_value(lua_State *L, struct read_block *rb, int type, int cookie) { +push_value(lua_State *L, struct read_block *rb, int type, int cookie) { switch(type) { case TYPE_NIL: lua_pushnil(L); @@ -493,82 +474,77 @@ _push_value(lua_State *L, struct read_block *rb, int type, int cookie) { lua_pushboolean(L,cookie); break; case TYPE_NUMBER: - lua_pushnumber(L,_get_number(L,rb,cookie)); + if (cookie == TYPE_NUMBER_REAL) { + lua_pushnumber(L,get_real(L,rb)); + } else { + lua_pushinteger(L, get_integer(L, rb, cookie)); + } break; case TYPE_USERDATA: - lua_pushlightuserdata(L,_get_pointer(L,rb)); + lua_pushlightuserdata(L,get_pointer(L,rb)); break; case TYPE_SHORT_STRING: - _get_buffer(L,rb,cookie); + get_buffer(L,rb,cookie); break; case TYPE_LONG_STRING: { - uint32_t len; if (cookie == 2) { - uint16_t *plen = rb_read(rb, &len, 2); + uint16_t *plen = rb_read(rb, 2); if (plen == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - _get_buffer(L,rb,(int)*plen); + uint16_t n; + memcpy(&n, plen, sizeof(n)); + get_buffer(L,rb,n); } else { if (cookie != 4) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - uint32_t *plen = rb_read(rb, &len, 4); + uint32_t *plen = rb_read(rb, 4); if (plen == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - _get_buffer(L,rb,(int)*plen); + uint32_t n; + memcpy(&n, plen, sizeof(n)); + get_buffer(L,rb,n); } break; } case TYPE_TABLE: { - _unpack_table(L,rb,cookie); + unpack_table(L,rb,cookie); break; } default: { - _invalid_stream(L,rb); + invalid_stream(L,rb); break; } } } static void -_unpack_one(lua_State *L, struct read_block *rb) { - uint8_t type = 0; - uint8_t *t = rb_read(rb, &type, 1); +unpack_one(lua_State *L, struct read_block *rb) { + uint8_t type; + uint8_t *t = rb_read(rb, sizeof(type)); if (t==NULL) { - _invalid_stream(L, rb); + invalid_stream(L, rb); } - _push_value(L, rb, *t & 0x7, *t>>3); + type = *t; + push_value(L, rb, type & 0x7, type>>3); } static void -_seri(lua_State *L, struct block *b) { - uint32_t len = 0; - memcpy(&len, b->buffer ,sizeof(len)); - - len -= 4; +seri(lua_State *L, struct block *b, int len) { uint8_t * buffer = skynet_malloc(len); uint8_t * ptr = buffer; int sz = len; - if (len < BLOCK_SIZE - 4) { - memcpy(ptr, b->buffer+4, len); - } else { - memcpy(ptr, b->buffer+4, BLOCK_SIZE-4); - ptr += BLOCK_SIZE-4; - len -= BLOCK_SIZE-4; - b = b->next; - - while(len>0) { - if (len >= BLOCK_SIZE) { - memcpy(ptr, b->buffer, BLOCK_SIZE); - ptr += BLOCK_SIZE; - len -= BLOCK_SIZE; - } else { - memcpy(ptr, b->buffer, len); - break; - } + while(len>0) { + if (len >= BLOCK_SIZE) { + memcpy(ptr, b->buffer, BLOCK_SIZE); + ptr += BLOCK_SIZE; + len -= BLOCK_SIZE; b = b->next; + } else { + memcpy(ptr, b->buffer, len); + break; } } @@ -577,12 +553,20 @@ _seri(lua_State *L, struct block *b) { } int -_luaseri_unpack(lua_State *L) { +luaseri_unpack(lua_State *L) { if (lua_isnoneornil(L,1)) { return 0; } - void * buffer = lua_touserdata(L,1); - int len = luaL_checkinteger(L,2); + void * buffer; + int len; + if (lua_type(L,1) == LUA_TSTRING) { + size_t sz; + buffer = (void *)lua_tolstring(L,1,&sz); + len = (int)sz; + } else { + buffer = lua_touserdata(L,1); + len = luaL_checkinteger(L,2); + } if (len == 0) { return 0; } @@ -590,40 +574,39 @@ _luaseri_unpack(lua_State *L) { return luaL_error(L, "deserialize null pointer"); } - lua_settop(L,0); + lua_settop(L,1); struct read_block rb; rball_init(&rb, buffer, len); int i; for (i=0;;i++) { - if (i%16==15) { - lua_checkstack(L,i); + if (i%8==7) { + luaL_checkstack(L,LUA_MINSTACK,NULL); } uint8_t type = 0; - uint8_t *t = rb_read(&rb, &type, 1); + uint8_t *t = rb_read(&rb, sizeof(type)); if (t==NULL) break; - _push_value(L, &rb, *t & 0x7, *t>>3); + type = *t; + push_value(L, &rb, type & 0x7, type>>3); } // Need not free buffer - return lua_gettop(L); + return lua_gettop(L) - 1; } -int -_luaseri_pack(lua_State *L) { +LUAMOD_API int +luaseri_pack(lua_State *L) { + struct block temp; + temp.next = NULL; struct write_block wb; - wb_init(&wb, NULL); - _pack_from(L,&wb,0); - struct block * b = wb_close(&wb); - _seri(L,b); + wb_init(&wb, &temp); + pack_from(L,&wb,0); + assert(wb.head == &temp); + seri(L, &temp, wb.len); - while (b) { - struct block * next = b->next; - skynet_free(b); - b = next; - } + wb_free(&wb); return 2; } diff --git a/lualib-src/lua-seri.h b/lualib-src/lua-seri.h index 23eb4cf9..6102239e 100644 --- a/lualib-src/lua-seri.h +++ b/lualib-src/lua-seri.h @@ -3,7 +3,7 @@ #include -int _luaseri_pack(lua_State *L); -int _luaseri_unpack(lua_State *L); +int luaseri_pack(lua_State *L); +int luaseri_unpack(lua_State *L); #endif diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c new file mode 100644 index 00000000..16b20872 --- /dev/null +++ b/lualib-src/lua-sharedata.c @@ -0,0 +1,793 @@ +#define LUA_LIB + +#include +#include +#include +#include +#include +#include +#include "atomic.h" + +#define KEYTYPE_INTEGER 0 +#define KEYTYPE_STRING 1 + +#define VALUETYPE_NIL 0 +#define VALUETYPE_REAL 1 +#define VALUETYPE_STRING 2 +#define VALUETYPE_BOOLEAN 3 +#define VALUETYPE_TABLE 4 +#define VALUETYPE_INTEGER 5 + +struct table; + +union value { + lua_Number n; + lua_Integer d; + struct table * tbl; + int string; + int boolean; +}; + +struct node { + union value v; + int key; // integer key or index of string table + int next; // next slot index + uint32_t keyhash; + uint8_t keytype; // key type must be integer or string + uint8_t valuetype; // value type can be number/string/boolean/table + uint8_t nocolliding; // 0 means colliding slot +}; + +struct state { + int dirty; + int ref; + struct table * root; +}; + +struct table { + int sizearray; + int sizehash; + uint8_t *arraytype; + union value * array; + struct node * hash; + lua_State * L; +}; + +struct context { + lua_State * L; + struct table * tbl; + int string_index; +}; + +struct ctrl { + struct table * root; + struct table * update; +}; + +static int +countsize(lua_State *L, int sizearray) { + int n = 0; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int type = lua_type(L, -2); + ++n; + if (type == LUA_TNUMBER) { + if (!lua_isinteger(L, -2)) { + luaL_error(L, "Invalid key %f", lua_tonumber(L, -2)); + } + lua_Integer nkey = lua_tointeger(L, -2); + if (nkey > 0 && nkey <= sizearray) { + --n; + } + } else if (type != LUA_TSTRING && type != LUA_TTABLE) { + luaL_error(L, "Invalid key type %s", lua_typename(L, type)); + } + lua_pop(L, 1); + } + + return n; +} + +static uint32_t +calchash(const char * str, size_t l) { + uint32_t h = (uint32_t)l; + size_t l1; + size_t step = (l >> 5) + 1; + for (l1 = l; l1 >= step; l1 -= step) { + h = h ^ ((h<<5) + (h>>2) + (uint8_t)(str[l1 - 1])); + } + return h; +} + +static int +stringindex(struct context *ctx, const char * str, size_t sz) { + lua_State *L = ctx->L; + lua_pushlstring(L, str, sz); + lua_pushvalue(L, -1); + lua_rawget(L, 1); + int index; + // stringmap(1) str index + if (lua_isnil(L, -1)) { + index = ++ctx->string_index; + lua_pop(L, 1); + lua_pushinteger(L, index); + lua_rawset(L, 1); + } else { + index = lua_tointeger(L, -1); + lua_pop(L, 2); + } + return index; +} + +static int convtable(lua_State *L); + +static void +setvalue(struct context * ctx, lua_State *L, int index, struct node *n) { + int vt = lua_type(L, index); + switch(vt) { + case LUA_TNIL: + n->valuetype = VALUETYPE_NIL; + break; + case LUA_TNUMBER: + if (lua_isinteger(L, index)) { + n->v.d = lua_tointeger(L, index); + n->valuetype = VALUETYPE_INTEGER; + } else { + n->v.n = lua_tonumber(L, index); + n->valuetype = VALUETYPE_REAL; + } + break; + case LUA_TSTRING: { + size_t sz = 0; + const char * str = lua_tolstring(L, index, &sz); + n->v.string = stringindex(ctx, str, sz); + n->valuetype = VALUETYPE_STRING; + break; + } + case LUA_TBOOLEAN: + n->v.boolean = lua_toboolean(L, index); + n->valuetype = VALUETYPE_BOOLEAN; + break; + case LUA_TTABLE: { + struct table *tbl = ctx->tbl; + ctx->tbl = (struct table *)malloc(sizeof(struct table)); + if (ctx->tbl == NULL) { + ctx->tbl = tbl; + luaL_error(L, "memory error"); + // never get here + } + memset(ctx->tbl, 0, sizeof(struct table)); + int absidx = lua_absindex(L, index); + + lua_pushcfunction(L, convtable); + lua_pushvalue(L, absidx); + lua_pushlightuserdata(L, ctx); + + lua_call(L, 2, 0); + + n->v.tbl = ctx->tbl; + n->valuetype = VALUETYPE_TABLE; + + ctx->tbl = tbl; + + break; + } + default: + luaL_error(L, "Unsupport value type %s", lua_typename(L, vt)); + break; + } +} + +static void +setarray(struct context *ctx, lua_State *L, int index, int key) { + struct node n; + setvalue(ctx, L, index, &n); + struct table *tbl = ctx->tbl; + --key; // base 0 + tbl->arraytype[key] = n.valuetype; + tbl->array[key] = n.v; +} + +static int +ishashkey(struct context * ctx, lua_State *L, int index, int *key, uint32_t *keyhash, int *keytype) { + int sizearray = ctx->tbl->sizearray; + int kt = lua_type(L, index); + if (kt == LUA_TNUMBER) { + *key = lua_tointeger(L, index); + if (*key > 0 && *key <= sizearray) { + return 0; + } + *keyhash = (uint32_t)*key; + *keytype = KEYTYPE_INTEGER; + } else { + size_t sz = 0; + const char * s = lua_tolstring(L, index, &sz); + *keyhash = calchash(s, sz); + *key = stringindex(ctx, s, sz); + *keytype = KEYTYPE_STRING; + } + return 1; +} + +static void +fillnocolliding(lua_State *L, struct context *ctx) { + struct table * tbl = ctx->tbl; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int key; + int keytype; + uint32_t keyhash; + if (!ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { + setarray(ctx, L, -1, key); + } else { + struct node * n = &tbl->hash[keyhash % tbl->sizehash]; + if (n->valuetype == VALUETYPE_NIL) { + n->key = key; + n->keytype = keytype; + n->keyhash = keyhash; + n->next = -1; + n->nocolliding = 1; + setvalue(ctx, L, -1, n); // set n->v , n->valuetype + } + } + lua_pop(L,1); + } +} + +static void +fillcolliding(lua_State *L, struct context *ctx) { + struct table * tbl = ctx->tbl; + int sizehash = tbl->sizehash; + int emptyslot = 0; + int i; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int key; + int keytype; + uint32_t keyhash; + if (ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { + struct node * mainpos = &tbl->hash[keyhash % tbl->sizehash]; + if (!(mainpos->keytype == keytype && mainpos->key == key)) { + // the key has not insert + struct node * n = NULL; + for (i=emptyslot;ihash[i].valuetype == VALUETYPE_NIL) { + n = &tbl->hash[i]; + break; + } + } + assert(n); + n->next = mainpos->next; + mainpos->next = n - tbl->hash; + mainpos->nocolliding = 0; + n->key = key; + n->keytype = keytype; + n->keyhash = keyhash; + n->nocolliding = 0; + setvalue(ctx, L, -1, n); // set n->v , n->valuetype + } + } + lua_pop(L,1); + } +} + +// table need convert +// struct context * ctx +static int +convtable(lua_State *L) { + int i; + struct context *ctx = lua_touserdata(L,2); + struct table *tbl = ctx->tbl; + + tbl->L = ctx->L; + + int sizearray = lua_rawlen(L, 1); + if (sizearray) { + tbl->arraytype = (uint8_t *)malloc(sizearray * sizeof(uint8_t)); + if (tbl->arraytype == NULL) { + goto memerror; + } + for (i=0;iarraytype[i] = VALUETYPE_NIL; + } + tbl->array = (union value *)malloc(sizearray * sizeof(union value)); + if (tbl->array == NULL) { + goto memerror; + } + tbl->sizearray = sizearray; + } + int sizehash = countsize(L, sizearray); + if (sizehash) { + tbl->hash = (struct node *)malloc(sizehash * sizeof(struct node)); + if (tbl->hash == NULL) { + goto memerror; + } + for (i=0;ihash[i].valuetype = VALUETYPE_NIL; + tbl->hash[i].nocolliding = 0; + } + tbl->sizehash = sizehash; + + fillnocolliding(L, ctx); + fillcolliding(L, ctx); + } else { + int i; + for (i=1;i<=sizearray;i++) { + lua_rawgeti(L, 1, i); + setarray(ctx, L, -1, i); + lua_pop(L,1); + } + } + + return 0; +memerror: + return luaL_error(L, "memory error"); +} + +static void +delete_tbl(struct table *tbl) { + int i; + for (i=0;isizearray;i++) { + if (tbl->arraytype[i] == VALUETYPE_TABLE) { + delete_tbl(tbl->array[i].tbl); + } + } + for (i=0;isizehash;i++) { + if (tbl->hash[i].valuetype == VALUETYPE_TABLE) { + delete_tbl(tbl->hash[i].v.tbl); + } + } + free(tbl->arraytype); + free(tbl->array); + free(tbl->hash); + free(tbl); +} + +static int +pconv(lua_State *L) { + struct context *ctx = lua_touserdata(L,1); + lua_State * pL = lua_touserdata(L, 2); + int ret; + + lua_settop(L, 0); + + // init L (may throw memory error) + // create a table for string map + lua_newtable(L); + + lua_pushcfunction(pL, convtable); + lua_pushvalue(pL,1); + lua_pushlightuserdata(pL, ctx); + + ret = lua_pcall(pL, 2, 0, 0); + if (ret != LUA_OK) { + size_t sz = 0; + const char * error = lua_tolstring(pL, -1, &sz); + lua_pushlstring(L, error, sz); + lua_error(L); + // never get here + } + + luaL_checkstack(L, ctx->string_index + 3, NULL); + lua_settop(L,1); + + return 1; +} + +static void +convert_stringmap(struct context *ctx, struct table *tbl) { + lua_State *L = ctx->L; + lua_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)); + s->dirty = 0; + s->ref = 0; + s->root = tbl; + lua_replace(L, 1); + lua_replace(L, -2); + + lua_pushnil(L); + // ... stringmap nil + while (lua_next(L, -2) != 0) { + int idx = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_pushvalue(L, -1); + lua_replace(L, idx); + } + + lua_pop(L, 1); + + lua_gc(L, LUA_GCCOLLECT, 0); +} + +static int +lnewconf(lua_State *L) { + int ret; + struct context ctx; + struct table * tbl = NULL; + luaL_checktype(L,1,LUA_TTABLE); + ctx.L = luaL_newstate(); + ctx.tbl = NULL; + ctx.string_index = 1; // 1 reserved for dirty flag + if (ctx.L == NULL) { + lua_pushliteral(L, "memory error"); + goto error; + } + tbl = (struct table *)malloc(sizeof(struct table)); + if (tbl == NULL) { + // lua_pushliteral may fail because of memory error, close first. + lua_close(ctx.L); + ctx.L = NULL; + lua_pushliteral(L, "memory error"); + goto error; + } + memset(tbl, 0, sizeof(struct table)); + ctx.tbl = tbl; + + lua_pushcfunction(ctx.L, pconv); + lua_pushlightuserdata(ctx.L , &ctx); + lua_pushlightuserdata(ctx.L , L); + + ret = lua_pcall(ctx.L, 2, 1, 0); + + if (ret != LUA_OK) { + size_t sz = 0; + const char * error = lua_tolstring(ctx.L, -1, &sz); + lua_pushlstring(L, error, sz); + goto error; + } + + convert_stringmap(&ctx, tbl); + + lua_pushlightuserdata(L, tbl); + + return 1; +error: + if (ctx.L) { + lua_close(ctx.L); + } + if (tbl) { + delete_tbl(tbl); + } + lua_error(L); + return -1; +} + +static struct table * +get_table(lua_State *L, int index) { + struct table *tbl = lua_touserdata(L,index); + if (tbl == NULL) { + luaL_error(L, "Need a conf object"); + } + return tbl; +} + +static int +ldeleteconf(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_close(tbl->L); + delete_tbl(tbl); + return 0; +} + +static void +pushvalue(lua_State *L, lua_State *sL, uint8_t vt, union value *v) { + switch(vt) { + case VALUETYPE_REAL: + lua_pushnumber(L, v->n); + break; + case VALUETYPE_INTEGER: + lua_pushinteger(L, v->d); + break; + case VALUETYPE_STRING: { + size_t sz = 0; + const char *str = lua_tolstring(sL, v->string, &sz); + lua_pushlstring(L, str, sz); + break; + } + case VALUETYPE_BOOLEAN: + lua_pushboolean(L, v->boolean); + break; + case VALUETYPE_TABLE: + lua_pushlightuserdata(L, v->tbl); + break; + default: + lua_pushnil(L); + break; + } +} + +static struct node * +lookup_key(struct table *tbl, uint32_t keyhash, int key, int keytype, const char *str, size_t sz) { + if (tbl->sizehash == 0) + return NULL; + struct node *n = &tbl->hash[keyhash % tbl->sizehash]; + if (keyhash != n->keyhash && n->nocolliding) + return NULL; + for (;;) { + if (keyhash == n->keyhash) { + if (n->keytype == KEYTYPE_INTEGER) { + if (keytype == KEYTYPE_INTEGER && n->key == key) { + return n; + } + } else { + // n->keytype == KEYTYPE_STRING + if (keytype == KEYTYPE_STRING) { + size_t sz2 = 0; + const char * str2 = lua_tolstring(tbl->L, n->key, &sz2); + if (sz == sz2 && memcmp(str,str2,sz) == 0) { + return n; + } + } + } + } + if (n->next < 0) { + return NULL; + } + n = &tbl->hash[n->next]; + } +} + +static int +lindexconf(lua_State *L) { + struct table *tbl = get_table(L,1); + int kt = lua_type(L,2); + uint32_t keyhash; + int key = 0; + int keytype; + size_t sz = 0; + const char * str = NULL; + if (kt == LUA_TNUMBER) { + if (!lua_isinteger(L, 2)) { + return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2)); + } + key = (int)lua_tointeger(L, 2); + if (key > 0 && key <= tbl->sizearray) { + --key; + pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]); + return 1; + } + keytype = KEYTYPE_INTEGER; + keyhash = (uint32_t)key; + } else { + str = luaL_checklstring(L, 2, &sz); + keyhash = calchash(str, sz); + keytype = KEYTYPE_STRING; + } + + struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); + if (n) { + pushvalue(L, tbl->L, n->valuetype, &n->v); + return 1; + } else { + return 0; + } +} + +static void +pushkey(lua_State *L, lua_State *sL, struct node *n) { + if (n->keytype == KEYTYPE_INTEGER) { + lua_pushinteger(L, n->key); + } else { + size_t sz = 0; + const char * str = lua_tolstring(sL, n->key, &sz); + lua_pushlstring(L, str, sz); + } +} + +static int +pushfirsthash(lua_State *L, struct table * tbl) { + if (tbl->sizehash) { + pushkey(L, tbl->L, &tbl->hash[0]); + return 1; + } else { + return 0; + } +} + +static int +lnextkey(lua_State *L) { + struct table *tbl = get_table(L,1); + if (lua_isnoneornil(L,2)) { + if (tbl->sizearray > 0) { + int i; + for (i=0;isizearray;i++) { + if (tbl->arraytype[i] != VALUETYPE_NIL) { + lua_pushinteger(L, i+1); + return 1; + } + } + } + return pushfirsthash(L, tbl); + } + int kt = lua_type(L,2); + uint32_t keyhash; + int key = 0; + int keytype; + size_t sz=0; + const char *str = NULL; + int sizearray = tbl->sizearray; + if (kt == LUA_TNUMBER) { + if (!lua_isinteger(L, 2)) { + return 0; + } + key = (int)lua_tointeger(L, 2); + if (key > 0 && key <= sizearray) { + lua_Integer i; + for (i=key;iarraytype[i] != VALUETYPE_NIL) { + lua_pushinteger(L, i+1); + return 1; + } + } + return pushfirsthash(L, tbl); + } + keyhash = (uint32_t)key; + keytype = KEYTYPE_INTEGER; + } else { + str = luaL_checklstring(L, 2, &sz); + keyhash = calchash(str, sz); + keytype = KEYTYPE_STRING; + } + + struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); + if (n) { + ++n; + int index = n-tbl->hash; + if (index == tbl->sizehash) { + return 0; + } + pushkey(L, tbl->L, n); + return 1; + } else { + return 0; + } +} + +static int +llen(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_pushinteger(L, tbl->sizearray); + return 1; +} + +static int +lhashlen(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_pushinteger(L, tbl->sizehash); + return 1; +} + +static int +releaseobj(lua_State *L) { + struct ctrl *c = lua_touserdata(L, 1); + struct table *tbl = c->root; + struct state *s = lua_touserdata(tbl->L, 1); + ATOM_DEC(&s->ref); + c->root = NULL; + c->update = NULL; + + return 0; +} + +static int +lboxconf(lua_State *L) { + struct table * tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + ATOM_INC(&s->ref); + + struct ctrl * c = lua_newuserdata(L, sizeof(*c)); + c->root = tbl; + c->update = NULL; + if (luaL_newmetatable(L, "confctrl")) { + lua_pushcfunction(L, releaseobj); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + + return 1; +} + +static int +lmarkdirty(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + s->dirty = 1; + return 0; +} + +static int +lisdirty(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int d = s->dirty; + lua_pushboolean(L, d); + + return 1; +} + +static int +lgetref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + lua_pushinteger(L , s->ref); + + return 1; +} + +static int +lincref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int ref = ATOM_INC(&s->ref); + lua_pushinteger(L , ref); + + return 1; +} + +static int +ldecref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int ref = ATOM_DEC(&s->ref); + lua_pushinteger(L , ref); + + return 1; +} + +static int +lneedupdate(lua_State *L) { + struct ctrl * c = lua_touserdata(L, 1); + if (c->update) { + lua_pushlightuserdata(L, c->update); + lua_getuservalue(L, 1); + return 2; + } + return 0; +} + +static int +lupdate(lua_State *L) { + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); + luaL_checktype(L, 3, LUA_TTABLE); + struct ctrl * c= lua_touserdata(L, 1); + struct table *n = lua_touserdata(L, 2); + if (c->root == n) { + return luaL_error(L, "You should update a new object"); + } + lua_settop(L, 3); + lua_setuservalue(L, 1); + c->update = n; + + return 0; +} + +LUAMOD_API int +luaopen_skynet_sharedata_core(lua_State *L) { + luaL_Reg l[] = { + // used by host + { "new", lnewconf }, + { "delete", ldeleteconf }, + { "markdirty", lmarkdirty }, + { "getref", lgetref }, + { "incref", lincref }, + { "decref", ldecref }, + + // used by client + { "box", lboxconf }, + { "index", lindexconf }, + { "nextkey", lnextkey }, + { "len", llen }, + { "hashlen", lhashlen }, + { "isdirty", lisdirty }, + { "needupdate", lneedupdate }, + { "update", lupdate }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L, l); + + return 1; +} diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 0025dd52..daab81ed 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet.h" #include "lua-seri.h" @@ -16,7 +18,7 @@ struct snlua { const char * preload; }; -static int +static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) @@ -45,7 +47,7 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t lua_pushlightuserdata(L, (void *)msg); lua_pushinteger(L,sz); lua_pushinteger(L, session); - lua_pushnumber(L, source); + lua_pushinteger(L, source); r = lua_pcall(L, 5, 0 , trace); @@ -74,9 +76,16 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t } static int -_callback(lua_State *L) { - struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); +forward_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { + _cb(context, ud, type, session, source, msg, sz); + // don't delete msg in forward mode. + return 1; +} +static int +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); lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); @@ -84,13 +93,17 @@ _callback(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); - skynet_callback(context, gL, _cb); + if (forward) { + skynet_callback(context, gL, forward_cb); + } else { + skynet_callback(context, gL, _cb); + } return 0; } static int -_command(lua_State *L) { +lcommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; @@ -108,55 +121,118 @@ _command(lua_State *L) { } static int -_genid(lua_State *L) { +lintcommand(lua_State *L) { + struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); + const char * cmd = luaL_checkstring(L,1); + const char * result; + const char * parm = NULL; + char tmp[64]; // for integer parm + if (lua_gettop(L) == 2) { + if (lua_isnumber(L, 2)) { + int32_t n = (int32_t)luaL_checkinteger(L,2); + sprintf(tmp, "%d", n); + parm = tmp; + } else { + parm = luaL_checkstring(L,2); + } + } + + result = skynet_command(context, cmd, parm); + if (result) { + char *endptr = NULL; + lua_Integer r = strtoll(result, &endptr, 0); + if (endptr == NULL || *endptr != '\0') { + // may be real number + double n = strtod(result, &endptr); + if (endptr == NULL || *endptr != '\0') { + return luaL_error(L, "Invalid result %s", result); + } else { + lua_pushnumber(L, n); + } + } else { + lua_pushinteger(L, r); + } + return 1; + } + return 0; +} + +static int +lgenid(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int session = skynet_send(context, 0, 0, PTYPE_TAG_ALLOCSESSION , 0 , NULL, 0); lua_pushinteger(L, session); return 1; } -// copy from _send +static const char * +get_dest_string(lua_State *L, int index) { + const char * dest_string = lua_tostring(L, index); + if (dest_string == NULL) { + luaL_error(L, "dest address type (%s) must be a string or number.", lua_typename(L, lua_type(L,index))); + } + return dest_string; +} static int -_sendname(lua_State *L, struct skynet_context * context, const char * dest) { - int type = luaL_checkinteger(L, 2); - int session = 0; - if (lua_isnil(L,3)) { - type |= PTYPE_TAG_ALLOCSESSION; - } else { - session = luaL_checkinteger(L,3); +send_message(lua_State *L, int source, int idx_type) { + struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); + uint32_t dest = (uint32_t)lua_tointeger(L, 1); + const char * dest_string = NULL; + if (dest == 0) { + if (lua_type(L,1) == LUA_TNUMBER) { + return luaL_error(L, "Invalid service address 0"); + } + dest_string = get_dest_string(L, 1); } - int mtype = lua_type(L,4); + int type = luaL_checkinteger(L, idx_type+0); + int session = 0; + if (lua_isnil(L,idx_type+1)) { + type |= PTYPE_TAG_ALLOCSESSION; + } else { + session = luaL_checkinteger(L,idx_type+1); + } + + int mtype = lua_type(L,idx_type+2); switch (mtype) { case LUA_TSTRING: { size_t len = 0; - void * msg = (void *)lua_tolstring(L,4,&len); - session = skynet_sendname(context, dest, type, session , msg, len); + void * msg = (void *)lua_tolstring(L,idx_type+2,&len); + if (len == 0) { + msg = NULL; + } + if (dest_string) { + session = skynet_sendname(context, source, dest_string, type, session , msg, len); + } else { + session = skynet_send(context, source, dest, type, session , msg, len); + } break; } - case LUA_TNIL : - session = skynet_sendname(context, dest, type, session , NULL, 0); - break; case LUA_TLIGHTUSERDATA: { - luaL_checktype(L, 4, LUA_TLIGHTUSERDATA); - void * msg = lua_touserdata(L,4); - int size = luaL_checkinteger(L,5); - session = skynet_sendname(context, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); + void * msg = lua_touserdata(L,idx_type+2); + int size = luaL_checkinteger(L,idx_type+3); + if (dest_string) { + session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); + } else { + session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); + } break; } default: - luaL_error(L, "skynet.send invalid param %s", lua_typename(L,lua_type(L,4))); + luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } if (session < 0) { - luaL_error(L, "skynet.send session (%d) < 0", session); + // send to invalid address + // todo: maybe throw an error would be better + return 0; } lua_pushinteger(L,session); return 1; } /* - unsigned address + uint32 address string address integer type integer session @@ -165,109 +241,53 @@ _sendname(lua_State *L, struct skynet_context * context, const char * dest) { integer len */ static int -_send(lua_State *L) { +lsend(lua_State *L) { + return send_message(L, 0, 2); +} + +/* + uint32 address + string address + integer source_address + integer type + integer session + string message + lightuserdata message_ptr + integer len + */ +static int +lredirect(lua_State *L) { + uint32_t source = (uint32_t)luaL_checkinteger(L,2); + return send_message(L, source, 3); +} + +static int +lerror(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - int addr_type = lua_type(L,1); - uint32_t dest = 0; - switch(addr_type) { - case LUA_TNUMBER: - dest = lua_tounsigned(L,1); - break; - case LUA_TSTRING: { - const char * addrname = lua_tostring(L,1); - if (addrname[0] == '.' || addrname[0] == ':') { - dest = skynet_queryname(context, addrname); - if (dest == 0) { - luaL_error(L, "Invalid name %s", addrname); - } - } else if ('0' <= addrname[0] && addrname[0] <= '9') { - luaL_error(L, "Invalid name %s: must not start with a digit", addrname); - } else { - return _sendname(L, context, addrname); - } - break; - } - default: - return luaL_error(L, "address must be number or string, got %s",lua_typename(L,addr_type)); - } - - int type = luaL_checkinteger(L, 2); - int session = 0; - if (lua_isnil(L,3)) { - type |= PTYPE_TAG_ALLOCSESSION; - } else { - session = luaL_checkinteger(L,3); - } - - int mtype = lua_type(L,4); - switch (mtype) { - case LUA_TSTRING: { - size_t len = 0; - void * msg = (void *)lua_tolstring(L,4,&len); - if (len == 0) { - msg = NULL; - } - session = skynet_send(context, 0, dest, type, session , msg, len); - break; - } - case LUA_TLIGHTUSERDATA: { - void * msg = lua_touserdata(L,4); - int size = luaL_checkinteger(L,5); - session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); - break; - } - default: - luaL_error(L, "skynet.send invalid param %s", lua_typename(L, lua_type(L,4))); - } - if (session < 0) { - // send to invalid address - // todo: maybe throw error is better + int n = lua_gettop(L); + if (n <= 1) { + lua_settop(L, 1); + const char * s = luaL_tolstring(L, 1, NULL); + skynet_error(context, "%s", s); return 0; } - lua_pushinteger(L,session); - return 1; -} - -static int -_redirect(lua_State *L) { - struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t dest = luaL_checkunsigned(L,1); - uint32_t source = luaL_checkunsigned(L,2); - int type = luaL_checkinteger(L,3); - int session = luaL_checkinteger(L,4); - - int mtype = lua_type(L,5); - switch (mtype) { - case LUA_TSTRING: { - size_t len = 0; - void * msg = (void *)lua_tolstring(L,5,&len); - if (len == 0) { - msg = NULL; + luaL_Buffer b; + luaL_buffinit(L, &b); + int i; + for (i=1; i<=n; i++) { + luaL_tolstring(L, i, NULL); + luaL_addvalue(&b); + if (i @@ -9,11 +11,15 @@ #include #include +#include +#include + #include "skynet_socket.h" #define BACKLOG 32 // 2 ** 12 == 4096 #define LARGE_PAGE_NODE 12 +#define BUFFER_LIMIT (256 * 1024) struct buffer_node { char * msg; @@ -79,6 +85,15 @@ lnewbuffer(lua_State *L) { int size return size + + Comment: The table pool record all the buffers chunk, + and the first index [1] is a lightuserdata : free_node. We can always use this pointer for struct buffer_node . + The following ([2] ...) userdatas in table pool is the buffer chunk (for struct buffer_node), + we never free them until the VM closed. The size of first chunk ([2]) is 16 struct buffer_node, + and the second size is 32 ... The largest size of chunk is LARGE_PAGE_NODE (4096) + + lpushbbuffer will get a free struct buffer_node from table pool, and then put the msg/size in it. + lpopbuffer return the struct buffer_node back to table pool (By calling return_free_node). */ static int lpushbuffer(lua_State *L) { @@ -177,7 +192,10 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { } break; } - luaL_addlstring(&b, current->msg + sb->offset, (sz - skip < bytes) ? sz - skip : bytes); + int real_sz = sz - skip; + if (real_sz > 0) { + luaL_addlstring(&b, current->msg + sb->offset, (real_sz < bytes) ? real_sz : bytes); + } return_free_node(L,2,sb); sz-=bytes; if (sz==0) @@ -188,10 +206,29 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { luaL_pushresult(&b); } +static int +lheader(lua_State *L) { + size_t len; + const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 1, &len); + if (len > 4 || len < 1) { + return luaL_error(L, "Invalid read %s", s); + } + int i; + size_t sz = 0; + for (i=0;i<(int)len;i++) { + sz <<= 8; + sz |= s[i]; + } + + lua_pushinteger(L, (lua_Integer)sz); + + return 1; +} + /* userdata send_buffer table pool - integer sz + integer sz */ static int lpopbuffer(lua_State *L) { @@ -226,6 +263,7 @@ lclearbuffer(lua_State *L) { while(sb->head) { return_free_node(L,2,sb); } + sb->size = 0; return 0; } @@ -244,6 +282,7 @@ lreadall(lua_State *L) { return_free_node(L,2,sb); } luaL_pushresult(&b); + sb->size = 0; return 1; } @@ -350,28 +389,64 @@ lunpack(lua_State *L) { } else { lua_pushlightuserdata(L, message->buffer); } + if (message->type == SKYNET_SOCKET_TYPE_UDP) { + int addrsz = 0; + const char * addrstring = skynet_socket_udp_address(message, &addrsz); + if (addrstring) { + lua_pushlstring(L, addrstring, addrsz); + return 5; + } + } return 4; } +static const char * +address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *port) { + const char * host; + if (lua_isnoneornil(L,port_index)) { + host = strchr(addr, '['); + if (host) { + // is ipv6 + ++host; + const char * sep = strchr(addr,']'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + memcpy(tmp, host, sep-host); + tmp[sep-host] = '\0'; + host = tmp; + sep = strchr(sep + 1, ':'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + *port = strtoul(sep+1,NULL,10); + } else { + // is ipv4 + const char * sep = strchr(addr,':'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + memcpy(tmp, addr, sep-addr); + tmp[sep-addr] = '\0'; + host = tmp; + *port = strtoul(sep+1,NULL,10); + } + } else { + host = addr; + *port = luaL_optinteger(L,port_index, 0); + } + return host; +} + static int lconnect(lua_State *L) { size_t sz = 0; const char * addr = luaL_checklstring(L,1,&sz); char tmp[sz]; - int port; - const char * host; - if (lua_isnoneornil(L,2)) { - const char * sep = strchr(addr,':'); - if (sep == NULL) { - return luaL_error(L, "Connect to invalid address %s.",addr); - } - memcpy(tmp, addr, sep-addr); - tmp[sep-addr] = '\0'; - host = tmp; - port = strtoul(sep+1,NULL,10); - } else { - host = addr; - port = luaL_checkinteger(L,2); + int port = 0; + const char * host = address_port(L, tmp, addr, 2, &port); + if (port == 0) { + return luaL_error(L, "Invalid port"); } struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = skynet_socket_connect(ctx, host, port); @@ -388,6 +463,14 @@ lclose(lua_State *L) { return 0; } +static int +lshutdown(lua_State *L) { + int id = luaL_checkinteger(L,1); + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + skynet_socket_shutdown(ctx, id); + return 0; +} + static int llisten(lua_State *L) { const char * host = luaL_checkstring(L,1); @@ -403,18 +486,66 @@ llisten(lua_State *L) { return 1; } +static size_t +count_size(lua_State *L, int index) { + size_t tlen = 0; + int i; + for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { + size_t len; + luaL_checklstring(L, -1, &len); + tlen += len; + lua_pop(L,1); + } + lua_pop(L,1); + return tlen; +} + +static void +concat_table(lua_State *L, int index, void *buffer, size_t tlen) { + char *ptr = buffer; + int i; + for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { + size_t len; + const char * str = lua_tolstring(L, -1, &len); + if (str == NULL || tlen < len) { + break; + } + memcpy(ptr, str, len); + ptr += len; + tlen -= len; + lua_pop(L,1); + } + if (tlen != 0) { + skynet_free(buffer); + luaL_error(L, "Invalid strings table"); + } + lua_pop(L,1); +} + static void * -get_buffer(lua_State *L, int *sz) { +get_buffer(lua_State *L, int index, int *sz) { void *buffer; - if (lua_isuserdata(L,2)) { - buffer = lua_touserdata(L,2); - *sz = luaL_checkinteger(L,3); - } else { - size_t len = 0; - const char * str = luaL_checklstring(L, 2, &len); + 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); + break; + case LUA_TTABLE: + // concat the table as a string + len = count_size(L, index); + buffer = skynet_malloc(len); + concat_table(L, index, buffer, len); + *sz = (int)len; + break; + default: + str = luaL_checklstring(L, index, &len); buffer = skynet_malloc(len); memcpy(buffer, str, len); *sz = (int)len; + break; } return buffer; } @@ -424,7 +555,7 @@ lsend(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); int sz = 0; - void *buffer = get_buffer(L, &sz); + void *buffer = get_buffer(L, 2, &sz); int err = skynet_socket_send(ctx, id, buffer, sz); lua_pushboolean(L, !err); return 1; @@ -435,9 +566,10 @@ lsendlow(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); int sz = 0; - void *buffer = get_buffer(L, &sz); - skynet_socket_send_lowpriority(ctx, id, buffer, sz); - return 0; + void *buffer = get_buffer(L, 2, &sz); + int err = skynet_socket_send_lowpriority(ctx, id, buffer, sz); + lua_pushboolean(L, !err); + return 1; } static int @@ -457,8 +589,96 @@ lstart(lua_State *L) { return 0; } -int -luaopen_socketdriver(lua_State *L) { +static int +lnodelay(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + skynet_socket_nodelay(ctx,id); + return 0; +} + +static int +ludp(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + size_t sz = 0; + const char * addr = lua_tolstring(L,1,&sz); + char tmp[sz]; + int port = 0; + const char * host = NULL; + if (addr) { + host = address_port(L, tmp, addr, 2, &port); + } + + int id = skynet_socket_udp(ctx, host, port); + if (id < 0) { + return luaL_error(L, "udp init failed"); + } + lua_pushinteger(L, id); + return 1; +} + +static int +ludp_connect(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + size_t sz = 0; + const char * addr = luaL_checklstring(L,2,&sz); + char tmp[sz]; + int port = 0; + const char * host = NULL; + if (addr) { + host = address_port(L, tmp, addr, 3, &port); + } + + if (skynet_socket_udp_connect(ctx, id, host, port)) { + return luaL_error(L, "udp connect failed"); + } + + return 0; +} + +static int +ludp_send(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + const char * address = luaL_checkstring(L, 2); + int sz = 0; + void *buffer = get_buffer(L, 3, &sz); + int err = skynet_socket_udp_send(ctx, id, address, buffer, sz); + + lua_pushboolean(L, !err); + + return 1; +} + +static int +ludp_address(lua_State *L) { + size_t sz = 0; + const uint8_t * addr = (const uint8_t *)luaL_checklstring(L, 1, &sz); + uint16_t port = 0; + memcpy(&port, addr+1, sizeof(uint16_t)); + port = ntohs(port); + const void * src = addr+3; + char tmp[256]; + int family; + if (sz == 1+2+4) { + family = AF_INET; + } else { + if (sz != 1+2+16) { + return luaL_error(L, "Invalid udp address"); + } + family = AF_INET6; + } + if (inet_ntop(family, src, tmp, sizeof(tmp)) == NULL) { + return luaL_error(L, "Invalid udp address"); + } + lua_pushstring(L, tmp); + lua_pushinteger(L, port); + return 2; +} + +LUAMOD_API int +luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "buffer", lnewbuffer }, @@ -469,6 +689,7 @@ luaopen_socketdriver(lua_State *L) { { "clear", lclearbuffer }, { "readline", lreadline }, { "str2p", lstr2p }, + { "header", lheader }, { "unpack", lunpack }, { NULL, NULL }, @@ -477,11 +698,17 @@ luaopen_socketdriver(lua_State *L) { luaL_Reg l2[] = { { "connect", lconnect }, { "close", lclose }, + { "shutdown", lshutdown }, { "listen", llisten }, { "send", lsend }, { "lsend", lsendlow }, { "bind", lbind }, { "start", lstart }, + { "nodelay", lnodelay }, + { "udp", ludp }, + { "udp_connect", ludp_connect }, + { "udp_send", ludp_send }, + { "udp_address", ludp_address }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c new file mode 100644 index 00000000..3089594b --- /dev/null +++ b/lualib-src/lua-stm.c @@ -0,0 +1,268 @@ +#define LUA_LIB + +#include +#include +#include +#include +#include +#include + +#include "rwlock.h" +#include "skynet_malloc.h" +#include "atomic.h" + +struct stm_object { + struct rwlock lock; + int reference; + struct stm_copy * copy; +}; + +struct stm_copy { + int reference; + uint32_t sz; + void * msg; +}; + +// msg should alloc by skynet_malloc +static struct stm_copy * +stm_newcopy(void * msg, int32_t sz) { + struct stm_copy * copy = skynet_malloc(sizeof(*copy)); + copy->reference = 1; + copy->sz = sz; + copy->msg = msg; + + return copy; +} + +static struct stm_object * +stm_new(void * msg, int32_t sz) { + struct stm_object * obj = skynet_malloc(sizeof(*obj)); + rwlock_init(&obj->lock); + obj->reference = 1; + obj->copy = stm_newcopy(msg, sz); + + return obj; +} + +static void +stm_releasecopy(struct stm_copy *copy) { + if (copy == NULL) + return; + if (ATOM_DEC(©->reference) == 0) { + skynet_free(copy->msg); + skynet_free(copy); + } +} + +static void +stm_release(struct stm_object *obj) { + assert(obj->copy); + rwlock_wlock(&obj->lock); + // writer release the stm object, so release the last copy . + stm_releasecopy(obj->copy); + obj->copy = NULL; + if (--obj->reference > 0) { + // stm object grab by readers, reset the copy to NULL. + rwlock_wunlock(&obj->lock); + return; + } + // no one grab the stm object, no need to unlock wlock. + skynet_free(obj); +} + +static void +stm_releasereader(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + if (ATOM_DEC(&obj->reference) == 0) { + // last reader, no writer. so no need to unlock + assert(obj->copy == NULL); + skynet_free(obj); + return; + } + rwlock_runlock(&obj->lock); +} + +static void +stm_grab(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + int ref = ATOM_FINC(&obj->reference); + rwlock_runlock(&obj->lock); + assert(ref > 0); +} + +static struct stm_copy * +stm_copy(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + struct stm_copy * ret = obj->copy; + if (ret) { + int ref = ATOM_FINC(&ret->reference); + assert(ref > 0); + } + rwlock_runlock(&obj->lock); + + return ret; +} + +static void +stm_update(struct stm_object *obj, void *msg, int32_t sz) { + struct stm_copy *copy = stm_newcopy(msg, sz); + rwlock_wlock(&obj->lock); + struct stm_copy *oldcopy = obj->copy; + obj->copy = copy; + rwlock_wunlock(&obj->lock); + + stm_releasecopy(oldcopy); +} + +// lua binding + +struct boxstm { + struct stm_object * obj; +}; + +static int +lcopy(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + stm_grab(box->obj); + lua_pushlightuserdata(L, box->obj); + return 1; +} + +static int +lnewwriter(lua_State *L) { + void * msg; + size_t sz; + if (lua_isuserdata(L,1)) { + msg = lua_touserdata(L, 1); + sz = (size_t)luaL_checkinteger(L, 2); + } else { + const char * tmp = luaL_checklstring(L,1,&sz); + msg = skynet_malloc(sz); + memcpy(msg, tmp, sz); + } + struct boxstm * box = lua_newuserdata(L, sizeof(*box)); + box->obj = stm_new(msg,sz); + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + + return 1; +} + +static int +ldeletewriter(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + stm_release(box->obj); + box->obj = NULL; + + return 0; +} + +static int +lupdate(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + void * msg; + size_t sz; + if (lua_isuserdata(L, 2)) { + msg = lua_touserdata(L, 2); + sz = (size_t)luaL_checkinteger(L, 3); + } else { + const char * tmp = luaL_checklstring(L,2,&sz); + msg = skynet_malloc(sz); + memcpy(msg, tmp, sz); + } + stm_update(box->obj, msg, sz); + + return 0; +} + +struct boxreader { + struct stm_object *obj; + struct stm_copy *lastcopy; +}; + +static int +lnewreader(lua_State *L) { + struct boxreader * box = lua_newuserdata(L, sizeof(*box)); + box->obj = lua_touserdata(L, 1); + box->lastcopy = NULL; + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + + return 1; +} + +static int +ldeletereader(lua_State *L) { + struct boxreader * box = lua_touserdata(L, 1); + stm_releasereader(box->obj); + box->obj = NULL; + stm_releasecopy(box->lastcopy); + box->lastcopy = NULL; + + return 0; +} + +static int +lread(lua_State *L) { + struct boxreader * box = lua_touserdata(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + + struct stm_copy * copy = stm_copy(box->obj); + if (copy == box->lastcopy) { + // not update + stm_releasecopy(copy); + lua_pushboolean(L, 0); + return 1; + } + + stm_releasecopy(box->lastcopy); + box->lastcopy = copy; + if (copy) { + lua_settop(L, 3); + lua_replace(L, 1); + lua_settop(L, 2); + lua_pushlightuserdata(L, copy->msg); + lua_pushinteger(L, copy->sz); + lua_pushvalue(L, 1); + lua_call(L, 3, LUA_MULTRET); + lua_pushboolean(L, 1); + lua_replace(L, 1); + return lua_gettop(L); + } else { + lua_pushboolean(L, 0); + return 1; + } +} + +LUAMOD_API int +luaopen_skynet_stm(lua_State *L) { + luaL_checkversion(L); + lua_createtable(L, 0, 3); + + lua_pushcfunction(L, lcopy); + lua_setfield(L, -2, "copy"); + + luaL_Reg writer[] = { + { "new", lnewwriter }, + { NULL, NULL }, + }; + lua_createtable(L, 0, 2); + lua_pushcfunction(L, ldeletewriter), + lua_setfield(L, -2, "__gc"); + lua_pushcfunction(L, lupdate), + lua_setfield(L, -2, "__call"); + luaL_setfuncs(L, writer, 1); + + luaL_Reg reader[] = { + { "newcopy", lnewreader }, + { NULL, NULL }, + }; + lua_createtable(L, 0, 2); + lua_pushcfunction(L, ldeletereader), + lua_setfield(L, -2, "__gc"); + lua_pushcfunction(L, lread), + lua_setfield(L, -2, "__call"); + luaL_setfuncs(L, reader, 1); + + return 1; +} diff --git a/lualib-src/sproto/README b/lualib-src/sproto/README new file mode 100644 index 00000000..4421d78a --- /dev/null +++ b/lualib-src/sproto/README @@ -0,0 +1 @@ +Check https://github.com/cloudwu/sproto for more diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md new file mode 100644 index 00000000..9fd4c3ff --- /dev/null +++ b/lualib-src/sproto/README.md @@ -0,0 +1,496 @@ +Introduction +====== + +Sproto is an efficient serialization library for C, and focuses on lua binding. It's like Google protocol buffers, but much faster. + +The design is simple. It only supports a few types that lua supports. It can be easily bound to other dynamic languages, or be used directly in C. + +In my i5-2500 @3.3GHz CPU, the benchmark is below: + +The schema in sproto: + +``` +.Person { + name 0 : string + id 1 : integer + email 2 : string + + .PhoneNumber { + number 0 : string + type 1 : integer + } + + phone 3 : *PhoneNumber +} + +.AddressBook { + person 0 : *Person +} +``` + +It's equal to: + +``` +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + message PhoneNumber { + required string number = 1; + optional int32 type = 2 ; + } + + repeated PhoneNumber phone = 4; +} + +message AddressBook { + repeated Person person = 1; +} +``` + +Use the data: +```lua +local ab = { + person = { + { + name = "Alice", + id = 10000, + phone = { + { number = "123456789" , type = 1 }, + { number = "87654321" , type = 2 }, + } + }, + { + name = "Bob", + id = 20000, + phone = { + { number = "01234567890" , type = 3 }, + } + } + } +} +``` + +library| encode 1M times | decode 1M times | size +-------| --------------- | --------------- | ---- +sproto | 2.15s | 7.84s | 83 bytes +sproto (nopack) |1.58s | 6.93s | 130 bytes +pbc-lua | 6.94s | 16.9s | 69 bytes +lua-cjson | 4.92s | 8.30s | 183 bytes + +* pbc-lua is a google protocol buffers library https://github.com/cloudwu/pbc +* lua-cjson is a json library https://github.com/efelix/lua-cjson + +Parser +======= + +```lua +local parser = require "sprotoparser" +``` + +* `parser.parse` parses a sproto schema to a binary string. + +The parser is needed for parsing the sproto schema. You can use it to generate binary string offline. The schema text and the parser is not needed when your program is running. + +Lua API +======= + +```lua +local sproto = require "sproto" +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:exist_type(typename)` detect whether a type exist in sproto object. +* `sproto:encode(typename, luatable)` encodes a lua table with typename into a binary string. +* `sproto:decode(typename, blob [,sz])` decodes a binary string generated by sproto.encode with typename. If blob is a lightuserdata (C ptr), sz (integer) is needed. +* `sproto:pencode(typename, luatable)` The same with sproto:encode, but pack (compress) the results. +* `sproto:pdecode(typename, blob [,sz])` The same with sproto.decode, but unpack the blob (generated by sproto:pencode) first. +* `sproto:default(typename, type)` Create a table with default values of typename. Type can be nil , "REQUEST", or "RESPONSE". + +RPC API +======= + +There is a lua wrapper for the core API for RPC . + +`sproto:host([packagename])` creates a host object to deliver the rpc message. + +`host:dispatch(blob [,sz])` unpack and decode (sproto:pdecode) the binary string with type the host created (packagename). + +If .type is exist, it's a REQUEST message with .type , returns "REQUEST", protoname, message, responser, .ud. The responser is a function for encode the response message. The responser will be nil when .session is not exist. + +If .type is not exist, it's a RESPONSE message for .session . Returns "RESPONSE", .session, message, .ud . + +`host:attach(sprotoobj)` creates a function(protoname, message, session, ud) to pack and encode request message with sprotoobj. + +If you don't want to use host object, you can also use these following apis to encode and decode the rpc message: + +`sproto:request_encode(protoname, tbl)` encode a request message with protoname. + +`sproto:response_encode(protoname, tbl)` encode a response message with protoname. + +`sproto:request_decode(protoname, blob [,sz])` decode a request message with protoname. + +`sproto:response_decode(protoname, blob [,sz]` decode a response message with protoname. + +Read testrpc.lua for detail. + +Schema Language +========== + +Like Protocol Buffers (but unlike json), sproto messages are strongly-typed and are not self-describing. You must define your message structure in a special language. + +You can use sprotoparser library to parse the schema text to a binary string, so that the sproto library can use it. +You can parse them offline and save the string, or you can parse them during your program running. + +The schema text is like this: + +``` +# This is a comment. + +.Person { # . means a user defined type + name 0 : string # string is a build-in type. + id 1 : integer + email 2 : string + + .PhoneNumber { # user defined type can be nest. + number 0 : string + type 1 : integer + } + + phone 3 : *PhoneNumber # *PhoneNumber means an array of PhoneNumber. + height 4 : integer(2) # (2) means a 1/100 fixed-point number. + data 5 : binary # Some binary data +} + +.AddressBook { + person 0 : *Person(id) # (id) is optional, means Person.id is main index. +} + +foobar 1 { # define a new protocol (for RPC used) with tag 1 + request Person # Associate the type Person with foobar.request + response { # define the foobar.response type + ok 0 : boolean + } +} + +``` + +A schema text can be self-described by the sproto schema language. + +``` +.type { + .field { + name 0 : string + buildin 1 : integer + type 2 : integer # type is fixed-point number precision when buildin is SPROTO_TINTEGER; When buildin is SPROTO_TSTRING, it means binary string when type is 1. + tag 3 : integer + array 4 : boolean + key 5 : integer # If key exists, array must be true, and it's a map. + } + name 0 : string + fields 1 : *field +} + +.protocol { + name 0 : string + tag 1 : integer + request 2 : integer # index + response 3 : integer # index + confirm 4 : boolean # response nil where confirm == true +} + +.group { + type 0 : *type + protocol 1 : *protocol +} +``` + +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. +* **boolean** : true or false + +You can add * before the typename to declare an array. + +You can also specify a main index, the array whould be encode as an unordered map. + +User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported. + +* 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. + +* Where is enum? + +In lua, enum types are not very useful. You can use integer to define an enum table in lua. + +Wire protocol +======== + +Each integer number must be serialized in little-endian format. + +The sproto message must be a user defined type struct, and a struct is encoded in three parts. The header, the field part, and the data part. +The tag and small integer or boolean will be encoded in field part, and others are in data part. + +All the fields must be encoded in ascending order (by tag, base 0). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message. + +The header is a 16bit integer. It is the number of fields. + +Each field in field part is a 16bit integer (n). If n is zero, that means the field data is encoded in data part ; + +If n is even (and not zero), the value of this field is n/2-1 , and the tag increases 1; + +If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 . + +Arrays are always encode in data part, 4 bytes header for the size, and the following bytes is the contents. See the example 2 for the struct array; example 3/4 for the integer array ; example 5 for the boolean array. + +Fot integer array, an additional byte (4 or 8) to indicate the value is 32bit or 64bit. + +Read the examples below to see more details. + +Notice: If the tag is not declared in schema, the decoder will simply ignore the field for protocol version compatibility. + +``` +.Person { + name 0 : string + age 1 : integer + marital 2 : boolean + children 3 : *Person +} + +.Data { + numbers 0 : *integer + bools 1 : *boolean + number 2 : integer + bignumber 3 : integer +} +``` + +Example 1: + +``` +person { name = "Alice" , age = 13, marital = false } + +03 00 (fn = 3) +00 00 (id = 0, value in data part) +1C 00 (id = 1, value = 13) +02 00 (id = 2, value = false) +05 00 00 00 (sizeof "Alice") +41 6C 69 63 65 ("Alice") +``` + +Example 2: + +``` +person { + name = "Bob", + age = 40, + children = { + { name = "Alice" , age = 13 }, + { name = "Carol" , age = 5 }, + } +} + +04 00 (fn = 4) +00 00 (id = 0, value in data part) +52 00 (id = 1, value = 40) +01 00 (skip id = 2) +00 00 (id = 3, value in data part) + +03 00 00 00 (sizeof "Bob") +42 6F 62 ("Bob") + +26 00 00 00 (sizeof children) + +0F 00 00 00 (sizeof child 1) +02 00 (fn = 2) +00 00 (id = 0, value in data part) +1C 00 (id = 1, value = 13) +05 00 00 00 (sizeof "Alice") +41 6C 69 63 65 ("Alice") + +0F 00 00 00 (sizeof child 2) +02 00 (fn = 2) +00 00 (id = 0, value in data part) +0C 00 (id = 1, value = 5) +05 00 00 00 (sizeof "Carol") +43 61 72 6F 6C ("Carol") +``` + +Example 3: + +``` +data { + numbers = { 1,2,3,4,5 } +} + +01 00 (fn = 1) +00 00 (id = 0, value in data part) + +15 00 00 00 (sizeof numbers) +04 ( sizeof int32 ) +01 00 00 00 (1) +02 00 00 00 (2) +03 00 00 00 (3) +04 00 00 00 (4) +05 00 00 00 (5) +``` + +Example 4: +``` +data { + numbers = { + (1<<32)+1, + (1<<32)+2, + (1<<32)+3, + } +} + +01 00 (fn = 1) +00 00 (id = 0, value in data part) + +19 00 00 00 (sizeof numbers) +08 ( sizeof int64 ) +01 00 00 00 01 00 00 00 ( (1<32) + 1) +02 00 00 00 01 00 00 00 ( (1<32) + 2) +03 00 00 00 01 00 00 00 ( (1<32) + 3) +``` + +Example 5: +``` +data { + bools = { false, true, false } +} + +02 00 (fn = 2) +01 00 (skip id = 0) +00 00 (id = 1, value in data part) + +03 00 00 00 (sizeof bools) +00 (false) +01 (true) +00 (false) +``` + +Example 6: +``` +data { + number = 100000, + bignumber = -10000000000, +} + +03 00 (fn = 3) +03 00 (skip id = 1) +00 00 (id = 2, value in data part) +00 00 (id = 3, value in data part) + +04 00 00 00 (sizeof number, data part) +A0 86 01 00 (100000, 32bit integer) + +08 00 00 00 (sizeof bignumber, data part) +00 1C F4 AB FD FF FF FF (-10000000000, 64bit integer) +``` + +0 Packing +======= + +The algorithm is very similar to [Cap'n proto](http://kentonv.github.io/capnproto/), but 0x00 is not treated specially. + +In packed format, the message is padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. +The bits of the tag byte correspond to the bytes of the unpacked word, with the least-significant bit corresponding to the first byte. +Each zero bit indicates that the corresponding byte is zero. The non-zero bytes are packed following the tag. + +For example: + +``` +unpacked (hex): 08 00 00 00 03 00 02 00 19 00 00 00 aa 01 00 00 +packed (hex): 51 08 03 02 31 19 aa 01 +``` + +Tag 0xff is treated specially. A number N is following the 0xff tag. N means (N+1)\*8 bytes should be copied directly. +The bytes may or may not contain zeros. Because of this rule, the worst-case space overhead of packing is 2 bytes per 2 KiB of input. + +For example: + +``` +unpacked (hex): 8a (x 30 bytes) +packed (hex): ff 03 8a (x 30 bytes) 00 00 +``` + +C API +===== + +```C +struct sproto * sproto_create(const void * proto, size_t sz); +``` + +Create a sproto object with a schema string encoded by sprotoparser: + +```C +void sproto_release(struct sproto *); +``` + +Release the sproto object: + +```C +int sproto_prototag(struct sproto *, const char * name); +const char * sproto_protoname(struct sproto *, int proto); +// SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response +struct sproto_type * sproto_protoquery(struct sproto *, int proto, int what); +``` + +Convert between tag and name of a protocol, and query the type object of it: + +```C +struct sproto_type * sproto_type(struct sproto *, const char * typename); +``` + +Query the type object from a sproto object: + +```C +struct sproto_arg { + void *ud; + const char *tagname; + int tagid; + int type; + struct sproto_type *subtype; + void *value; + int length; + int index; // array base 1 + int mainindex; // for map + int extra; // SPROTO_TINTEGER: fixed-point presision ; SPROTO_TSTRING 0:utf8 string 1:binary +}; + +typedef int (*sproto_callback)(const struct sproto_arg *args); + +int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); +int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); +``` + +encode and decode the sproto message with a user defined callback function. Read the implementation of lsproto.c for more details. + +```C +int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); +int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); +``` + +pack and unpack the message with the 0 packing algorithm. + +Other Implementions and bindings +===== +See Wiki https://github.com/cloudwu/sproto/wiki + +Question? +========== + +* Send me an email: http://www.codingnow.com/2000/gmail.gif +* My Blog: http://blog.codingnow.com +* Design: http://blog.codingnow.com/2014/07/ejoyproto.html (in Chinese) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c new file mode 100644 index 00000000..d7160a62 --- /dev/null +++ b/lualib-src/sproto/lsproto.c @@ -0,0 +1,708 @@ +#define LUA_LIB + +#include +#include +#include "msvcint.h" + +#include "lua.h" +#include "lauxlib.h" +#include "sproto.h" + +#define MAX_GLOBALSPROTO 16 +#define ENCODE_BUFFERSIZE 2050 + +#define ENCODE_MAXSIZE 0x1000000 +#define ENCODE_DEEPLEVEL 64 + +#ifndef luaL_newlib /* using LuaJIT */ +/* +** set functions from list 'l' into table at top - 'nup'; each +** function gets the 'nup' elements at the top as upvalues. +** Returns with only the table at the stack. +*/ +LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { +#ifdef luaL_checkversion + luaL_checkversion(L); +#endif + luaL_checkstack(L, nup, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_setfield(L, -(nup + 2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ +} + +#define luaL_newlibtable(L,l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) +#endif + +#if LUA_VERSION_NUM < 503 + +#if LUA_VERSION_NUM < 502 +static lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum) { + if (lua_isnumber(L, idx)) { + if (isnum) *isnum = 1; + return lua_tointeger(L, idx); + } + else { + if (isnum) *isnum = 0; + return 0; + } +} +#endif + +// work around , use push & lua_gettable may be better +#define lua_geti lua_rawgeti +#define lua_seti lua_rawseti + +#endif + +static int +lnewproto(lua_State *L) { + struct sproto * sp; + size_t sz; + void * buffer = (void *)luaL_checklstring(L,1,&sz); + sp = sproto_create(buffer, sz); + if (sp) { + lua_pushlightuserdata(L, sp); + return 1; + } + return 0; +} + +static int +ldeleteproto(lua_State *L) { + struct sproto * sp = lua_touserdata(L,1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto object"); + } + sproto_release(sp); + return 0; +} + +static int +lquerytype(lua_State *L) { + const char * type_name; + struct sproto *sp = lua_touserdata(L,1); + struct sproto_type *st; + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto object"); + } + type_name = luaL_checkstring(L,2); + st = sproto_type(sp, type_name); + if (st) { + lua_pushlightuserdata(L, st); + return 1; + } + return 0; +} + +struct encode_ud { + lua_State *L; + struct sproto_type *st; + int tbl_index; + const char * array_tag; + int array_index; + int deep; + int iter_index; +}; + +static int +encode(const struct sproto_arg *args) { + struct encode_ud *self = args->ud; + lua_State *L = self->L; + if (self->deep >= ENCODE_DEEPLEVEL) + return luaL_error(L, "The table is too deep"); + if (args->index > 0) { + if (args->tagname != self->array_tag) { + // a new array + self->array_tag = args->tagname; + lua_getfield(L, self->tbl_index, args->tagname); + if (lua_isnil(L, -1)) { + if (self->array_index) { + lua_replace(L, self->array_index); + } + 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 (args->mainindex >= 0) { + // use lua_next to iterate the table + // todo: check the key is equal to mainindex value + + lua_pushvalue(L,self->iter_index); + if (!lua_next(L, self->array_index)) { + // iterate end + lua_pushnil(L); + lua_replace(L, self->iter_index); + return SPROTO_CB_NIL; + } + lua_insert(L, -2); + lua_replace(L, self->iter_index); + } 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) { + case SPROTO_TINTEGER: { + lua_Integer v; + lua_Integer vh; + int isnum; + if (args->extra) { + // It's decimal. + lua_Number vn = lua_tonumber(L, -1); + v = (lua_Integer)(vn * args->extra + 0.5); + } else { + v = lua_tointegerx(L, -1, &isnum); + if(!isnum) { + return luaL_error(L, ".%s[%d] is not an integer (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } + } + lua_pop(L,1); + // notice: in lua 5.2, lua_Integer maybe 52bit + vh = v >> 31; + if (vh == 0 || vh == -1) { + *(uint32_t *)args->value = (uint32_t)v; + return 4; + } + else { + *(uint64_t *)args->value = (uint64_t)v; + return 8; + } + } + case SPROTO_TBOOLEAN: { + int v = lua_toboolean(L, -1); + if (!lua_isboolean(L,-1)) { + return luaL_error(L, ".%s[%d] is not a boolean (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } + *(int *)args->value = v; + lua_pop(L,1); + return 4; + } + case SPROTO_TSTRING: { + size_t sz = 0; + const char * str; + if (!lua_isstring(L, -1)) { + return luaL_error(L, ".%s[%d] is not a string (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } else { + str = lua_tolstring(L, -1, &sz); + } + if (sz > args->length) + return SPROTO_CB_ERROR; + memcpy(args->value, str, sz); + lua_pop(L,1); + return sz; + } + case SPROTO_TSTRUCT: { + 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; + r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); + lua_settop(L, top-1); // pop the value + if (r < 0) + return SPROTO_CB_ERROR; + return r; + } + default: + return luaL_error(L, "Invalid field type %d", args->type); + } +} + +static void * +expand_buffer(lua_State *L, int osz, int nsz) { + void *output; + do { + osz *= 2; + } while (osz < nsz); + if (osz > ENCODE_MAXSIZE) { + luaL_error(L, "object is too large (>%d)", ENCODE_MAXSIZE); + return NULL; + } + output = lua_newuserdata(L, osz); + lua_replace(L, lua_upvalueindex(1)); + lua_pushinteger(L, osz); + lua_replace(L, lua_upvalueindex(2)); + + return output; +} + +/* + lightuserdata sproto_type + table source + + return string + */ +static int +lencode(lua_State *L) { + struct encode_ud self; + void * buffer = lua_touserdata(L, lua_upvalueindex(1)); + int sz = lua_tointeger(L, lua_upvalueindex(2)); + int tbl_index = 2; + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + luaL_checktype(L, tbl_index, LUA_TNIL); + lua_pushstring(L, ""); + return 1; // response nil + } + luaL_checktype(L, tbl_index, LUA_TTABLE); + luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); + self.L = L; + self.st = st; + self.tbl_index = tbl_index; + for (;;) { + int r; + self.array_tag = NULL; + self.array_index = 0; + self.deep = 0; + + lua_settop(L, tbl_index); + lua_pushnil(L); // for iterator (stack slot 3) + self.iter_index = tbl_index+1; + + r = sproto_encode(st, buffer, sz, encode, &self); + if (r<0) { + buffer = expand_buffer(L, sz, sz*2); + sz *= 2; + } else { + lua_pushlstring(L, buffer, r); + return 1; + } + } +} + +struct decode_ud { + lua_State *L; + const char * array_tag; + int array_index; + int result_index; + int deep; + int mainindex_tag; + int key_index; +}; + +static int +decode(const struct sproto_arg *args) { + struct decode_ud * self = args->ud; + lua_State *L = self->L; + if (self->deep >= ENCODE_DEEPLEVEL) + return luaL_error(L, "The table is too deep"); + if (args->index != 0) { + // It's array + if (args->tagname != self->array_tag) { + self->array_tag = args->tagname; + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, self->result_index, args->tagname); + if (self->array_index) { + lua_replace(L, self->array_index); + } else { + self->array_index = lua_gettop(L); + } + if (args->index < 0) { + // It's a empty array, return now. + return 0; + } + } + } + switch (args->type) { + case SPROTO_TINTEGER: { + // notice: in lua 5.2, 52bit integer support (not 64) + if (args->extra) { + // lua_Integer is 32bit in small lua. + uint64_t v = *(uint64_t*)args->value; + lua_Number vn = (lua_Number)v; + vn /= args->extra; + lua_pushnumber(L, vn); + } else { + lua_Integer v = *(uint64_t*)args->value; + lua_pushinteger(L, v); + } + break; + } + case SPROTO_TBOOLEAN: { + int v = *(uint64_t*)args->value; + lua_pushboolean(L,v); + break; + } + case SPROTO_TSTRING: { + lua_pushlstring(L, args->value, args->length); + break; + } + case SPROTO_TSTRUCT: { + struct decode_ud sub; + int r; + lua_newtable(L); + sub.L = L; + sub.result_index = lua_gettop(L); + sub.deep = self->deep + 1; + sub.array_index = 0; + sub.array_tag = NULL; + if (args->mainindex >= 0) { + // This struct will set into a map, so mark the main index tag. + sub.mainindex_tag = args->mainindex; + lua_pushnil(L); + sub.key_index = lua_gettop(L); + + r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); + if (r < 0) + 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); + } + 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; + sub.key_index = 0; + r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); + if (r < 0) + return SPROTO_CB_ERROR; + if (r != args->length) + return r; + lua_settop(L, sub.result_index); + break; + } + } + default: + luaL_error(L, "Invalid type"); + } + if (args->index > 0) { + lua_seti(L, self->array_index, args->index); + } else { + if (self->mainindex_tag == args->tagid) { + // This tag is marked, save the value to key_index + // assert(self->key_index > 0); + lua_pushvalue(L,-1); + lua_replace(L, self->key_index); + } + lua_setfield(L, self->result_index, args->tagname); + } + + return 0; +} + +static const void * +getbuffer(lua_State *L, int index, size_t *sz) { + const void * buffer = NULL; + int t = lua_type(L, index); + if (t == LUA_TSTRING) { + buffer = lua_tolstring(L, index, sz); + } else { + if (t != LUA_TUSERDATA && t != LUA_TLIGHTUSERDATA) { + luaL_argerror(L, index, "Need a string or userdata"); + return NULL; + } + buffer = lua_touserdata(L, index); + *sz = luaL_checkinteger(L, index+1); + } + return buffer; +} + +/* + lightuserdata sproto_type + string source / (lightuserdata , integer) + return table + */ +static int +ldecode(lua_State *L) { + struct sproto_type * st = lua_touserdata(L, 1); + const void * buffer; + struct decode_ud self; + size_t sz; + int r; + if (st == NULL) { + // return nil + return 0; + } + sz = 0; + buffer = getbuffer(L, 2, &sz); + 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; + self.array_tag = NULL; + self.deep = 0; + self.mainindex_tag = -1; + self.key_index = 0; + r = sproto_decode(st, buffer, (int)sz, decode, &self); + if (r < 0) { + return luaL_error(L, "decode error"); + } + lua_settop(L, self.result_index); + lua_pushinteger(L, r); + return 2; +} + +static int +ldumpproto(lua_State *L) { + struct sproto * sp = lua_touserdata(L, 1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + sproto_dump(sp); + + return 0; +} + + +/* + string source / (lightuserdata , integer) + return string + */ +static int +lpack(lua_State *L) { + size_t sz=0; + const void * buffer = getbuffer(L, 1, &sz); + // the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB). + size_t maxsz = (sz + 2047) / 2048 * 2 + sz + 2; + void * output = lua_touserdata(L, lua_upvalueindex(1)); + int bytes; + int osz = lua_tointeger(L, lua_upvalueindex(2)); + if (osz < maxsz) { + output = expand_buffer(L, osz, maxsz); + } + bytes = sproto_pack(buffer, sz, output, maxsz); + if (bytes > maxsz) { + return luaL_error(L, "packing error, return size = %d", bytes); + } + lua_pushlstring(L, output, bytes); + + return 1; +} + +static int +lunpack(lua_State *L) { + size_t sz=0; + const void * buffer = getbuffer(L, 1, &sz); + void * output = lua_touserdata(L, lua_upvalueindex(1)); + int osz = lua_tointeger(L, lua_upvalueindex(2)); + int r = sproto_unpack(buffer, sz, output, osz); + if (r < 0) + return luaL_error(L, "Invalid unpack stream"); + if (r > osz) { + output = expand_buffer(L, osz, r); + r = sproto_unpack(buffer, sz, output, r); + if (r < 0) + return luaL_error(L, "Invalid unpack stream"); + } + lua_pushlstring(L, output, r); + return 1; +} + +static void +pushfunction_withbuffer(lua_State *L, const char * name, lua_CFunction func) { + lua_newuserdata(L, ENCODE_BUFFERSIZE); + lua_pushinteger(L, ENCODE_BUFFERSIZE); + lua_pushcclosure(L, func, 2); + lua_setfield(L, -2, name); +} + +static int +lprotocol(lua_State *L) { + struct sproto * sp = lua_touserdata(L, 1); + struct sproto_type * request; + struct sproto_type * response; + int t; + int tag; + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + t = lua_type(L,2); + if (t == LUA_TNUMBER) { + const char * name; + tag = lua_tointeger(L, 2); + name = sproto_protoname(sp, tag); + if (name == NULL) + return 0; + lua_pushstring(L, name); + } else { + const char * name = lua_tostring(L, 2); + if (name == NULL) { + return luaL_argerror(L, 2, "Should be number or string"); + } + tag = sproto_prototag(sp, name); + if (tag < 0) + return 0; + lua_pushinteger(L, tag); + } + request = sproto_protoquery(sp, tag, SPROTO_REQUEST); + if (request == NULL) { + lua_pushnil(L); + } else { + lua_pushlightuserdata(L, request); + } + response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); + if (response == NULL) { + if (sproto_protoresponse(sp, tag)) { + lua_pushlightuserdata(L, NULL); // response nil + } else { + lua_pushnil(L); + } + } else { + lua_pushlightuserdata(L, response); + } + return 3; +} + +/* global sproto pointer for multi states + NOTICE : It is not thread safe + */ +static struct sproto * G_sproto[MAX_GLOBALSPROTO]; + +static int +lsaveproto(lua_State *L) { + struct sproto * sp = lua_touserdata(L, 1); + int index = luaL_optinteger(L, 2, 0); + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); + } + /* TODO : release old object (memory leak now, but thread safe)*/ + G_sproto[index] = sp; + return 0; +} + +static int +lloadproto(lua_State *L) { + int index = luaL_optinteger(L, 1, 0); + struct sproto * sp; + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); + } + sp = G_sproto[index]; + if (sp == NULL) { + return luaL_error(L, "nil sproto at index %d", index); + } + + lua_pushlightuserdata(L, sp); + + return 1; +} + +static int +encode_default(const struct sproto_arg *args) { + lua_State *L = args->ud; + lua_pushstring(L, args->tagname); + if (args->index > 0) { + lua_newtable(L); + lua_rawset(L, -3); + return SPROTO_CB_NOARRAY; + } else { + switch(args->type) { + case SPROTO_TINTEGER: + lua_pushinteger(L, 0); + break; + case SPROTO_TBOOLEAN: + lua_pushboolean(L, 0); + break; + case SPROTO_TSTRING: + lua_pushliteral(L, ""); + break; + case SPROTO_TSTRUCT: + lua_createtable(L, 0, 1); + lua_pushstring(L, sproto_name(args->subtype)); + lua_setfield(L, -2, "__type"); + break; + } + lua_rawset(L, -3); + return SPROTO_CB_NIL; + } +} + +/* + lightuserdata sproto_type + return default table + */ +static int +ldefault(lua_State *L) { + int ret; + // 64 is always enough for dummy buffer, except the type has many fields ( > 27). + char dummy[64]; + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + lua_newtable(L); + ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L); + if (ret<0) { + // try again + int sz = sizeof(dummy) * 2; + void * tmp = lua_newuserdata(L, sz); + lua_insert(L, -2); + for (;;) { + ret = sproto_encode(st, tmp, sz, encode_default, L); + if (ret >= 0) + break; + sz *= 2; + tmp = lua_newuserdata(L, sz); + lua_replace(L, -3); + } + } + return 1; +} + +LUAMOD_API int +luaopen_sproto_core(lua_State *L) { +#ifdef luaL_checkversion + luaL_checkversion(L); +#endif + luaL_Reg l[] = { + { "newproto", lnewproto }, + { "deleteproto", ldeleteproto }, + { "dumpproto", ldumpproto }, + { "querytype", lquerytype }, + { "decode", ldecode }, + { "protocol", lprotocol }, + { "loadproto", lloadproto }, + { "saveproto", lsaveproto }, + { "default", ldefault }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + pushfunction_withbuffer(L, "encode", lencode); + pushfunction_withbuffer(L, "pack", lpack); + pushfunction_withbuffer(L, "unpack", lunpack); + return 1; +} diff --git a/lualib-src/sproto/msvcint.h b/lualib-src/sproto/msvcint.h new file mode 100644 index 00000000..a0caee97 --- /dev/null +++ b/lualib-src/sproto/msvcint.h @@ -0,0 +1,32 @@ +#ifndef msvc_int_h +#define msvc_int_h + +#ifdef _MSC_VER +# define inline __inline +# ifndef _MSC_STDINT_H_ +# if (_MSC_VER < 1300) +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +# else +typedef signed __int8 int8_t; +typedef signed __int16 int16_t; +typedef signed __int32 int32_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +# endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; +# endif + +#else + +#include + +#endif + +#endif diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c new file mode 100644 index 00000000..713f1569 --- /dev/null +++ b/lualib-src/sproto/sproto.c @@ -0,0 +1,1382 @@ +#include +#include +#include +#include +#include "msvcint.h" + +#include "sproto.h" + +#define SPROTO_TARRAY 0x80 +#define CHUNK_SIZE 1000 +#define SIZEOF_LENGTH 4 +#define SIZEOF_HEADER 2 +#define SIZEOF_FIELD 2 + +struct field { + int tag; + int type; + const char * name; + struct sproto_type * st; + int key; + int extra; +}; + +struct sproto_type { + const char * name; + int n; + int base; + int maxn; + struct field *f; +}; + +struct protocol { + const char *name; + int tag; + int confirm; // confirm == 1 where response nil + struct sproto_type * p[2]; +}; + +struct chunk { + struct chunk * next; +}; + +struct pool { + struct chunk * header; + struct chunk * current; + int current_used; +}; + +struct sproto { + struct pool memory; + int type_n; + int protocol_n; + struct sproto_type * type; + struct protocol * proto; +}; + +static void +pool_init(struct pool *p) { + p->header = NULL; + p->current = NULL; + p->current_used = 0; +} + +static void +pool_release(struct pool *p) { + struct chunk * tmp = p->header; + while (tmp) { + struct chunk * n = tmp->next; + free(tmp); + tmp = n; + } +} + +static void * +pool_newchunk(struct pool *p, size_t sz) { + struct chunk * t = malloc(sz + sizeof(struct chunk)); + if (t == NULL) + return NULL; + t->next = p->header; + p->header = t; + return t+1; +} + +static void * +pool_alloc(struct pool *p, size_t sz) { + // align by 8 + sz = (sz + 7) & ~7; + if (sz >= CHUNK_SIZE) { + return pool_newchunk(p, sz); + } + if (p->current == NULL) { + if (pool_newchunk(p, CHUNK_SIZE) == NULL) + return NULL; + p->current = p->header; + } + if (sz + p->current_used <= CHUNK_SIZE) { + void * ret = (char *)(p->current+1) + p->current_used; + p->current_used += sz; + return ret; + } + + if (sz >= p->current_used) { + return pool_newchunk(p, sz); + } else { + void * ret = pool_newchunk(p, CHUNK_SIZE); + p->current = p->header; + p->current_used = sz; + return ret; + } +} + +static inline int +toword(const uint8_t * p) { + return p[0] | p[1]<<8; +} + +static inline uint32_t +todword(const uint8_t *p) { + return p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24; +} + +static int +count_array(const uint8_t * stream) { + uint32_t length = todword(stream); + int n = 0; + stream += SIZEOF_LENGTH; + while (length > 0) { + uint32_t nsz; + if (length < SIZEOF_LENGTH) + return -1; + nsz = todword(stream); + nsz += SIZEOF_LENGTH; + if (nsz > length) + return -1; + ++n; + stream += nsz; + length -= nsz; + } + + return n; +} + +static int +struct_field(const uint8_t * stream, size_t sz) { + const uint8_t * field; + int fn, header, i; + if (sz < SIZEOF_LENGTH) + return -1; + fn = toword(stream); + header = SIZEOF_HEADER + SIZEOF_FIELD * fn; + if (sz < header) + return -1; + field = stream + SIZEOF_HEADER; + sz -= header; + stream += header; + for (i=0;imemory, sz+1); + memcpy(buffer, stream+SIZEOF_LENGTH, sz); + buffer[sz] = '\0'; + return buffer; +} + +static int +calc_pow(int base, int n) { + int r; + if (n == 0) + return 1; + r = calc_pow(base * base , n / 2); + if (n&1) { + r *= base; + } + return r; +} + +static const uint8_t * +import_field(struct sproto *s, struct field *f, const uint8_t * stream) { + uint32_t sz; + const uint8_t * result; + int fn; + int i; + int array = 0; + int tag = -1; + f->tag = -1; + f->type = -1; + f->name = NULL; + f->st = NULL; + f->key = -1; + f->extra = 0; + + sz = todword(stream); + stream += SIZEOF_LENGTH; + result = stream + sz; + fn = struct_field(stream, sz); + if (fn < 0) + return NULL; + stream += SIZEOF_HEADER; + for (i=0;iname = import_string(s, stream + fn * SIZEOF_FIELD); + continue; + } + if (value == 0) + return NULL; + value = value/2 - 1; + switch(tag) { + case 1: // buildin + if (value >= SPROTO_TSTRUCT) + return NULL; // invalid buildin type + f->type = value; + break; + case 2: // type index + if (f->type == SPROTO_TINTEGER) { + f->extra = calc_pow(10, value); + } else if (f->type == SPROTO_TSTRING) { + f->extra = value; // string if 0 ; binary is 1 + } else { + if (value >= s->type_n) + return NULL; // invalid type index + if (f->type >= 0) + return NULL; + f->type = SPROTO_TSTRUCT; + f->st = &s->type[value]; + } + break; + case 3: // tag + f->tag = value; + break; + case 4: // array + if (value) + array = SPROTO_TARRAY; + break; + case 5: // key + f->key = value; + break; + default: + return NULL; + } + } + if (f->tag < 0 || f->type < 0 || f->name == NULL) + return NULL; + f->type |= array; + + return result; +} + +/* +.type { + .field { + name 0 : string + buildin 1 : integer + type 2 : integer + tag 3 : integer + array 4 : boolean + } + name 0 : string + fields 1 : *field +} +*/ +static const uint8_t * +import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { + const uint8_t * result; + uint32_t sz = todword(stream); + int i; + int fn; + int n; + int maxn; + int last; + stream += SIZEOF_LENGTH; + result = stream + sz; + fn = struct_field(stream, sz); + if (fn <= 0 || fn > 2) + return NULL; + for (i=0;iname = import_string(s, stream); + if (fn == 1) { + return result; + } + stream += todword(stream)+SIZEOF_LENGTH; // second data + n = count_array(stream); + if (n<0) + return NULL; + stream += SIZEOF_LENGTH; + maxn = n; + last = -1; + t->n = n; + t->f = pool_alloc(&s->memory, sizeof(struct field) * n); + for (i=0;if[i]; + stream = import_field(s, f, stream); + if (stream == NULL) + return NULL; + tag = f->tag; + if (tag <= last) + return NULL; // tag must in ascending order + if (tag > last+1) { + ++maxn; + } + last = tag; + } + t->maxn = maxn; + t->base = t->f[0].tag; + n = t->f[n-1].tag - t->base + 1; + if (n != t->n) { + t->base = -1; + } + return result; +} + +/* +.protocol { + name 0 : string + tag 1 : integer + request 2 : integer + response 3 : integer +} +*/ +static const uint8_t * +import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { + const uint8_t * result; + uint32_t sz = todword(stream); + int fn; + int i; + int tag; + stream += SIZEOF_LENGTH; + result = stream + sz; + fn = struct_field(stream, sz); + stream += SIZEOF_HEADER; + p->name = NULL; + p->tag = -1; + p->p[SPROTO_REQUEST] = NULL; + p->p[SPROTO_RESPONSE] = NULL; + p->confirm = 0; + tag = 0; + for (i=0;iname = import_string(s, stream + SIZEOF_FIELD *fn); + break; + case 1: // tag + if (value < 0) { + return NULL; + } + p->tag = value; + break; + case 2: // request + if (value < 0 || value>=s->type_n) + return NULL; + p->p[SPROTO_REQUEST] = &s->type[value]; + break; + case 3: // response + if (value < 0 || value>=s->type_n) + return NULL; + p->p[SPROTO_RESPONSE] = &s->type[value]; + break; + case 4: // confirm + p->confirm = value; + break; + default: + return NULL; + } + } + + if (p->name == NULL || p->tag<0) { + return NULL; + } + + return result; +} + +static struct sproto * +create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) { + const uint8_t * content; + const uint8_t * typedata = NULL; + const uint8_t * protocoldata = NULL; + int fn = struct_field(stream, sz); + int i; + if (fn < 0 || fn > 2) + return NULL; + + stream += SIZEOF_HEADER; + content = stream + fn*SIZEOF_FIELD; + + for (i=0;itype_n = n; + s->type = pool_alloc(&s->memory, n * sizeof(*s->type)); + } else { + protocoldata = content+SIZEOF_LENGTH; + s->protocol_n = n; + s->proto = pool_alloc(&s->memory, n * sizeof(*s->proto)); + } + content += todword(content) + SIZEOF_LENGTH; + } + + for (i=0;itype_n;i++) { + typedata = import_type(s, &s->type[i], typedata); + if (typedata == NULL) { + return NULL; + } + } + for (i=0;iprotocol_n;i++) { + protocoldata = import_protocol(s, &s->proto[i], protocoldata); + if (protocoldata == NULL) { + return NULL; + } + } + + return s; +} + +struct sproto * +sproto_create(const void * proto, size_t sz) { + struct pool mem; + struct sproto * s; + pool_init(&mem); + s = pool_alloc(&mem, sizeof(*s)); + if (s == NULL) + return NULL; + memset(s, 0, sizeof(*s)); + s->memory = mem; + if (create_from_bundle(s, proto, sz) == NULL) { + pool_release(&s->memory); + return NULL; + } + return s; +} + +void +sproto_release(struct sproto * s) { + if (s == NULL) + return; + pool_release(&s->memory); +} + +void +sproto_dump(struct sproto *s) { + int i,j; + printf("=== %d types ===\n", s->type_n); + for (i=0;itype_n;i++) { + struct sproto_type *t = &s->type[i]; + printf("%s\n", t->name); + for (j=0;jn;j++) { + char array[2] = { 0, 0 }; + const char * type_name = NULL; + struct field *f = &t->f[j]; + int type = f->type & ~SPROTO_TARRAY; + if (f->type & SPROTO_TARRAY) { + array[0] = '*'; + } else { + array[0] = 0; + } + 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); + if (type == SPROTO_TINTEGER && f->extra > 0) { + printf("(%d)", f->extra); + } + if (f->key >= 0) { + printf("[%d]", f->key); + } + printf("\n"); + } + } + printf("=== %d protocol ===\n", s->protocol_n); + for (i=0;iprotocol_n;i++) { + struct protocol *p = &s->proto[i]; + if (p->p[SPROTO_REQUEST]) { + printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name); + } else { + printf("\t%s (%d) request:(null)", p->name, p->tag); + } + if (p->p[SPROTO_RESPONSE]) { + printf(" response:%s", p->p[SPROTO_RESPONSE]->name); + } else if (p->confirm) { + printf(" response nil"); + } + printf("\n"); + } +} + +// query +int +sproto_prototag(const struct sproto *sp, const char * name) { + int i; + for (i=0;iprotocol_n;i++) { + if (strcmp(name, sp->proto[i].name) == 0) { + return sp->proto[i].tag; + } + } + return -1; +} + +static struct protocol * +query_proto(const struct sproto *sp, int tag) { + int begin = 0, end = sp->protocol_n; + while(beginproto[mid].tag; + if (t==tag) { + return &sp->proto[mid]; + } + if (tag > t) { + begin = mid+1; + } else { + end = mid; + } + } + return NULL; +} + +struct sproto_type * +sproto_protoquery(const struct sproto *sp, int proto, int what) { + struct protocol * p; + if (what <0 || what >1) { + return NULL; + } + p = query_proto(sp, proto); + if (p) { + return p->p[what]; + } + return NULL; +} + +int +sproto_protoresponse(const struct sproto * sp, int proto) { + struct protocol * p = query_proto(sp, proto); + return (p!=NULL && (p->p[SPROTO_RESPONSE] || p->confirm)); +} + +const char * +sproto_protoname(const struct sproto *sp, int proto) { + struct protocol * p = query_proto(sp, proto); + if (p) { + return p->name; + } + return NULL; +} + +struct sproto_type * +sproto_type(const struct sproto *sp, const char * type_name) { + int i; + for (i=0;itype_n;i++) { + if (strcmp(type_name, sp->type[i].name) == 0) { + return &sp->type[i]; + } + } + return NULL; +} + +const char * +sproto_name(struct sproto_type * st) { + return st->name; +} + +static struct field * +findtag(const struct sproto_type *st, int tag) { + int begin, end; + if (st->base >=0 ) { + tag -= st->base; + if (tag < 0 || tag >= st->n) + return NULL; + return &st->f[tag]; + } + begin = 0; + end = st->n; + while (begin < end) { + int mid = (begin+end)/2; + struct field *f = &st->f[mid]; + int t = f->tag; + if (t == tag) { + return f; + } + if (tag > t) { + begin = mid + 1; + } else { + end = mid; + } + } + return NULL; +} + +// encode & decode +// sproto_callback(void *ud, int tag, int type, struct sproto_type *, void *value, int length) +// return size, -1 means error + +static inline int +fill_size(uint8_t * data, int sz) { + data[0] = sz & 0xff; + data[1] = (sz >> 8) & 0xff; + data[2] = (sz >> 16) & 0xff; + data[3] = (sz >> 24) & 0xff; + return sz + SIZEOF_LENGTH; +} + +static int +encode_integer(uint32_t v, uint8_t * data, int size) { + if (size < SIZEOF_LENGTH + sizeof(v)) + return -1; + data[4] = v & 0xff; + data[5] = (v >> 8) & 0xff; + data[6] = (v >> 16) & 0xff; + data[7] = (v >> 24) & 0xff; + return fill_size(data, sizeof(v)); +} + +static int +encode_uint64(uint64_t v, uint8_t * data, int size) { + if (size < SIZEOF_LENGTH + sizeof(v)) + return -1; + data[4] = v & 0xff; + data[5] = (v >> 8) & 0xff; + data[6] = (v >> 16) & 0xff; + data[7] = (v >> 24) & 0xff; + data[8] = (v >> 32) & 0xff; + data[9] = (v >> 40) & 0xff; + data[10] = (v >> 48) & 0xff; + data[11] = (v >> 56) & 0xff; + return fill_size(data, sizeof(v)); +} + +/* +//#define CB(tagname,type,index,subtype,value,length) cb(ud, tagname,type,index,subtype,value,length) + +static int +do_cb(sproto_callback cb, void *ud, const char *tagname, int type, int index, struct sproto_type *subtype, void *value, int length) { + if (subtype) { + if (type >= 0) { + printf("callback: tag=%s[%d], subtype[%s]:%d\n",tagname,index, subtype->name, type); + } else { + printf("callback: tag=%s[%d], subtype[%s]\n",tagname,index, subtype->name); + } + } else if (index > 0) { + printf("callback: tag=%s[%d]\n",tagname,index); + } else if (index == 0) { + printf("callback: tag=%s\n",tagname); + } else { + printf("callback: tag=%s [mainkey]\n",tagname); + } + return cb(ud, tagname,type,index,subtype,value,length); +} +#define CB(tagname,type,index,subtype,value,length) do_cb(cb,ud, tagname,type,index,subtype,value,length) +*/ + +static int +encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { + int sz; + if (size < SIZEOF_LENGTH) + return -1; + args->value = data+SIZEOF_LENGTH; + args->length = size-SIZEOF_LENGTH; + sz = cb(args); + if (sz < 0) { + if (sz == SPROTO_CB_NIL) + return 0; + return -1; // sz == SPROTO_CB_ERROR + } + assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow + return fill_size(data, sz); +} + +static inline void +uint32_to_uint64(int negative, uint8_t *buffer) { + if (negative) { + buffer[4] = 0xff; + buffer[5] = 0xff; + buffer[6] = 0xff; + buffer[7] = 0xff; + } else { + buffer[4] = 0; + buffer[5] = 0; + buffer[6] = 0; + buffer[7] = 0; + } +} + +static uint8_t * +encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) { + uint8_t * header = buffer; + int intlen; + int index; + if (size < 1) + return NULL; + buffer++; + size--; + intlen = sizeof(uint32_t); + index = 1; + *noarray = 0; + + for (;;) { + int sz; + union { + uint64_t u64; + uint32_t u32; + } u; + args->value = &u; + args->length = sizeof(u); + args->index = index; + sz = cb(args); + if (sz <= 0) { + if (sz == SPROTO_CB_NIL) // nil object, end of array + break; + if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it + *noarray = 1; + break; + } + return NULL; // sz == SPROTO_CB_ERROR + } + if (size < sizeof(uint64_t)) + return NULL; + if (sz == sizeof(uint32_t)) { + uint32_t v = u.u32; + buffer[0] = v & 0xff; + buffer[1] = (v >> 8) & 0xff; + buffer[2] = (v >> 16) & 0xff; + buffer[3] = (v >> 24) & 0xff; + + if (intlen == sizeof(uint64_t)) { + uint32_to_uint64(v & 0x80000000, buffer); + } + } else { + uint64_t v; + if (sz != sizeof(uint64_t)) + return NULL; + if (intlen == sizeof(uint32_t)) { + int i; + // rearrange + size -= (index-1) * sizeof(uint32_t); + if (size < sizeof(uint64_t)) + return NULL; + buffer += (index-1) * sizeof(uint32_t); + for (i=index-2;i>=0;i--) { + int negative; + memcpy(header+1+i*sizeof(uint64_t), header+1+i*sizeof(uint32_t), sizeof(uint32_t)); + negative = header[1+i*sizeof(uint64_t)+3] & 0x80; + uint32_to_uint64(negative, header+1+i*sizeof(uint64_t)); + } + intlen = sizeof(uint64_t); + } + + v = u.u64; + buffer[0] = v & 0xff; + buffer[1] = (v >> 8) & 0xff; + buffer[2] = (v >> 16) & 0xff; + buffer[3] = (v >> 24) & 0xff; + buffer[4] = (v >> 32) & 0xff; + buffer[5] = (v >> 40) & 0xff; + buffer[6] = (v >> 48) & 0xff; + buffer[7] = (v >> 56) & 0xff; + } + + size -= intlen; + buffer += intlen; + index++; + } + + if (buffer == header + 1) { + return header; + } + *header = (uint8_t)intlen; + return buffer; +} + +static int +encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { + uint8_t * buffer; + int sz; + if (size < SIZEOF_LENGTH) + return -1; + size -= SIZEOF_LENGTH; + buffer = data + SIZEOF_LENGTH; + switch (args->type) { + case SPROTO_TINTEGER: { + int noarray; + buffer = encode_integer_array(cb,args,buffer,size, &noarray); + if (buffer == NULL) + return -1; + + if (noarray) { + return 0; + } + break; + } + case SPROTO_TBOOLEAN: + args->index = 1; + for (;;) { + int v = 0; + args->value = &v; + args->length = sizeof(v); + sz = cb(args); + if (sz < 0) { + if (sz == SPROTO_CB_NIL) // nil object , end of array + break; + if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it + return 0; + return -1; // sz == SPROTO_CB_ERROR + } + if (size < 1) + return -1; + buffer[0] = v ? 1: 0; + size -= 1; + buffer += 1; + ++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; + } + break; + } + sz = buffer - (data + SIZEOF_LENGTH); + return fill_size(data, sz); +} + +int +sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { + struct sproto_arg args; + uint8_t * header = buffer; + uint8_t * data; + int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD; + int i; + int index; + int lasttag; + int datasz; + if (size < header_sz) + return -1; + args.ud = ud; + data = header + header_sz; + size -= header_sz; + index = 0; + lasttag = -1; + for (i=0;in;i++) { + struct field *f = &st->f[i]; + int type = f->type; + int value = 0; + int sz = -1; + args.tagname = f->name; + args.tagid = f->tag; + args.subtype = f->st; + args.mainindex = f->key; + args.extra = f->extra; + if (type & SPROTO_TARRAY) { + args.type = type & ~SPROTO_TARRAY; + sz = encode_array(cb, &args, data, size); + } else { + args.type = type; + args.index = 0; + switch(type) { + case SPROTO_TINTEGER: + case SPROTO_TBOOLEAN: { + union { + uint64_t u64; + uint32_t u32; + } u; + args.value = &u; + args.length = sizeof(u); + sz = cb(&args); + if (sz < 0) { + if (sz == SPROTO_CB_NIL) + continue; + if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it + return 0; + return -1; // sz == SPROTO_CB_ERROR + } + if (sz == sizeof(uint32_t)) { + if (u.u32 < 0x7fff) { + value = (u.u32+1) * 2; + sz = 2; // sz can be any number > 0 + } else { + sz = encode_integer(u.u32, data, size); + } + } else if (sz == sizeof(uint64_t)) { + sz= encode_uint64(u.u64, data, size); + } else { + return -1; + } + break; + } + case SPROTO_TSTRUCT: + case SPROTO_TSTRING: + sz = encode_object(cb, &args, data, size); + break; + } + } + if (sz < 0) + return -1; + if (sz > 0) { + uint8_t * record; + int tag; + if (value == 0) { + data += sz; + size -= sz; + } + record = header+SIZEOF_HEADER+SIZEOF_FIELD*index; + tag = f->tag - lasttag - 1; + if (tag > 0) { + // skip tag + tag = (tag - 1) * 2 + 1; + if (tag > 0xffff) + return -1; + record[0] = tag & 0xff; + record[1] = (tag >> 8) & 0xff; + ++index; + record += SIZEOF_FIELD; + } + ++index; + record[0] = value & 0xff; + record[1] = (value >> 8) & 0xff; + lasttag = f->tag; + } + } + header[0] = index & 0xff; + header[1] = (index >> 8) & 0xff; + + datasz = data - (header + header_sz); + data = header + header_sz; + if (index != st->maxn) { + memmove(header + SIZEOF_HEADER + index * SIZEOF_FIELD, data, datasz); + } + return SIZEOF_HEADER + index * SIZEOF_FIELD + datasz; +} + +static int +decode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t * stream, int sz) { + uint32_t hsz; + int index = 1; + while (sz > 0) { + if (sz < SIZEOF_LENGTH) + return -1; + hsz = todword(stream); + stream += SIZEOF_LENGTH; + sz -= SIZEOF_LENGTH; + if (hsz > sz) + return -1; + args->index = index; + args->value = stream; + args->length = hsz; + if (cb(args)) + return -1; + sz -= hsz; + stream += hsz; + ++index; + } + return 0; +} + +static inline uint64_t +expand64(uint32_t v) { + uint64_t value = v; + if (value & 0x80000000) { + value |= (uint64_t)~0 << 32 ; + } + return value; +} + +static int +decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { + uint32_t sz = todword(stream); + int type = args->type; + int i; + if (sz == 0) { + // It's empty array, call cb with index == -1 to create the empty array. + args->index = -1; + args->value = NULL; + args->length = 0; + cb(args); + return 0; + } + stream += SIZEOF_LENGTH; + switch (type) { + case SPROTO_TINTEGER: { + int len = *stream; + ++stream; + --sz; + if (len == sizeof(uint32_t)) { + if (sz % sizeof(uint32_t) != 0) + return -1; + for (i=0;iindex = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); + } + } else if (len == sizeof(uint64_t)) { + if (sz % sizeof(uint64_t) != 0) + return -1; + for (i=0;iindex = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); + } + } else { + return -1; + } + break; + } + case SPROTO_TBOOLEAN: + for (i=0;iindex = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); + } + break; + case SPROTO_TSTRING: + case SPROTO_TSTRUCT: + return decode_array_object(cb, args, stream, sz); + default: + return -1; + } + return 0; +} + +int +sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { + struct sproto_arg args; + int total = size; + uint8_t * stream; + uint8_t * datastream; + int fn; + int i; + int tag; + if (size < SIZEOF_HEADER) + return -1; + // debug print + // printf("sproto_decode[%p] (%s)\n", ud, st->name); + stream = (void *)data; + fn = toword(stream); + stream += SIZEOF_HEADER; + size -= SIZEOF_HEADER ; + if (size < fn * SIZEOF_FIELD) + return -1; + datastream = stream + fn * SIZEOF_FIELD; + size -= fn * SIZEOF_FIELD; + args.ud = ud; + + tag = -1; + for (i=0;iname; + args.tagid = f->tag; + args.type = f->type & ~SPROTO_TARRAY; + args.subtype = f->st; + args.index = 0; + args.mainindex = f->key; + args.extra = f->extra; + if (value < 0) { + if (f->type & SPROTO_TARRAY) { + if (decode_array(cb, &args, currentdata)) { + return -1; + } + } else { + switch (f->type) { + case SPROTO_TINTEGER: { + uint32_t sz = todword(currentdata); + if (sz == sizeof(uint32_t)) { + uint64_t v = expand64(todword(currentdata + SIZEOF_LENGTH)); + args.value = &v; + args.length = sizeof(v); + cb(&args); + } else if (sz != sizeof(uint64_t)) { + return -1; + } else { + uint32_t low = todword(currentdata + SIZEOF_LENGTH); + uint32_t hi = todword(currentdata + SIZEOF_LENGTH + sizeof(uint32_t)); + uint64_t v = (uint64_t)low | (uint64_t) hi << 32; + args.value = &v; + args.length = sizeof(v); + cb(&args); + } + break; + } + case SPROTO_TSTRING: + case SPROTO_TSTRUCT: { + uint32_t sz = todword(currentdata); + args.value = currentdata+SIZEOF_LENGTH; + args.length = sz; + if (cb(&args)) + return -1; + break; + } + default: + return -1; + } + } + } else if (f->type != SPROTO_TINTEGER && f->type != SPROTO_TBOOLEAN) { + return -1; + } else { + uint64_t v = value; + args.value = &v; + args.length = sizeof(v); + cb(&args); + } + } + return total - size; +} + +// 0 pack + +static int +pack_seg(const uint8_t *src, uint8_t * buffer, int sz, int n) { + uint8_t header = 0; + int notzero = 0; + int i; + uint8_t * obuffer = buffer; + ++buffer; + --sz; + if (sz < 0) + obuffer = NULL; + + for (i=0;i<8;i++) { + if (src[i] != 0) { + notzero++; + header |= 1< 0) { + *buffer = src[i]; + ++buffer; + --sz; + } + } + } + if ((notzero == 7 || notzero == 6) && n > 0) { + notzero = 8; + } + if (notzero == 8) { + if (n > 0) { + return 8; + } else { + return 10; + } + } + if (obuffer) { + *obuffer = header; + } + return notzero + 1; +} + +static inline void +write_ff(const uint8_t * src, uint8_t * des, int n) { + int i; + int align8_n = (n+7)&(~7); + + 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; + } +} + +int +sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { + uint8_t tmp[8]; + int i; + const uint8_t * ff_srcstart = NULL; + uint8_t * ff_desstart = NULL; + int ff_n = 0; + int size = 0; + const uint8_t * src = srcv; + uint8_t * buffer = bufferv; + for (i=0;i 0) { + int j; + memcpy(tmp, src, 8-padding); + for (j=0;j0) { + ++ff_n; + if (ff_n == 256) { + if (bufsz >= 0) { + write_ff(ff_srcstart, ff_desstart, 256*8); + } + ff_n = 0; + } + } else { + if (ff_n > 0) { + if (bufsz >= 0) { + write_ff(ff_srcstart, ff_desstart, ff_n*8); + } + ff_n = 0; + } + } + src += 8; + 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)); + } + return size; +} + +int +sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { + const uint8_t * src = srcv; + uint8_t * buffer = bufferv; + int size = 0; + while (srcsz > 0) { + uint8_t header = src[0]; + --srcsz; + ++src; + if (header == 0xff) { + int n; + if (srcsz < 0) { + return -1; + } + n = (src[0] + 1) * 8; + if (srcsz < n + 1) + return -1; + srcsz -= n + 1; + ++src; + if (bufsz >= n) { + memcpy(buffer, src, n); + } + bufsz -= n; + buffer += n; + src += n; + size += n; + } else { + int i; + for (i=0;i<8;i++) { + int nz = (header >> i) & 1; + if (nz) { + if (srcsz < 0) + return -1; + if (bufsz > 0) { + *buffer = *src; + --bufsz; + ++buffer; + } + ++src; + --srcsz; + } else { + if (bufsz > 0) { + *buffer = 0; + --bufsz; + ++buffer; + } + } + ++size; + } + } + } + return size; +} diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h new file mode 100644 index 00000000..f70309ec --- /dev/null +++ b/lualib-src/sproto/sproto.h @@ -0,0 +1,62 @@ +#ifndef sproto_h +#define sproto_h + +#include + +struct sproto; +struct sproto_type; + +#define SPROTO_REQUEST 0 +#define SPROTO_RESPONSE 1 + +// type (sproto_arg.type) +#define SPROTO_TINTEGER 0 +#define SPROTO_TBOOLEAN 1 +#define SPROTO_TSTRING 2 +#define SPROTO_TSTRUCT 3 + +// sub type of string (sproto_arg.extra) +#define SPROTO_TSTRING_STRING 0 +#define SPROTO_TSTRING_BINARY 1 + +#define SPROTO_CB_ERROR -1 +#define SPROTO_CB_NIL -2 +#define SPROTO_CB_NOARRAY -3 + +struct sproto * sproto_create(const void * proto, size_t sz); +void sproto_release(struct sproto *); + +int sproto_prototag(const struct sproto *, const char * name); +const char * sproto_protoname(const struct sproto *, int proto); +// SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response +struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what); +int sproto_protoresponse(const struct sproto *, int proto); + +struct sproto_type * sproto_type(const struct sproto *, const char * type_name); + +int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); +int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); + +struct sproto_arg { + void *ud; + const char *tagname; + int tagid; + int type; + struct sproto_type *subtype; + void *value; + int length; + int index; // array base 1 + int mainindex; // for map + int extra; // SPROTO_TINTEGER: decimal ; SPROTO_TSTRING 0:utf8 string 1:binary +}; + +typedef int (*sproto_callback)(const struct sproto_arg *args); + +int sproto_decode(const struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); +int sproto_encode(const struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); + +// for debug use +void sproto_dump(struct sproto *); +const char * sproto_name(struct sproto_type *); + +#endif diff --git a/lualib/compat10/cluster.lua b/lualib/compat10/cluster.lua new file mode 100644 index 00000000..04c5b01c --- /dev/null +++ b/lualib/compat10/cluster.lua @@ -0,0 +1 @@ +return require "skynet.cluster" \ No newline at end of file diff --git a/lualib/compat10/crypt.lua b/lualib/compat10/crypt.lua new file mode 100644 index 00000000..7fcb351a --- /dev/null +++ b/lualib/compat10/crypt.lua @@ -0,0 +1 @@ +return require "skynet.crypt" \ No newline at end of file diff --git a/lualib/compat10/datacenter.lua b/lualib/compat10/datacenter.lua new file mode 100644 index 00000000..1529e7bc --- /dev/null +++ b/lualib/compat10/datacenter.lua @@ -0,0 +1 @@ +return require "skynet.datacenter" \ No newline at end of file diff --git a/lualib/compat10/dns.lua b/lualib/compat10/dns.lua new file mode 100644 index 00000000..fd9e7096 --- /dev/null +++ b/lualib/compat10/dns.lua @@ -0,0 +1 @@ +return require "skynet.dns" \ No newline at end of file diff --git a/lualib/compat10/memory.lua b/lualib/compat10/memory.lua new file mode 100644 index 00000000..07c4e506 --- /dev/null +++ b/lualib/compat10/memory.lua @@ -0,0 +1 @@ +return require "skynet.memory" \ No newline at end of file diff --git a/lualib/compat10/mongo.lua b/lualib/compat10/mongo.lua new file mode 100644 index 00000000..eee821ae --- /dev/null +++ b/lualib/compat10/mongo.lua @@ -0,0 +1 @@ +return require "skynet.db.mongo" \ No newline at end of file diff --git a/lualib/compat10/mqueue.lua b/lualib/compat10/mqueue.lua new file mode 100644 index 00000000..a1ca6102 --- /dev/null +++ b/lualib/compat10/mqueue.lua @@ -0,0 +1 @@ +return require "skynet.mqueue" \ No newline at end of file diff --git a/lualib/compat10/multicast.lua b/lualib/compat10/multicast.lua new file mode 100644 index 00000000..93a38bc7 --- /dev/null +++ b/lualib/compat10/multicast.lua @@ -0,0 +1 @@ +return require "skynet.multicast" \ No newline at end of file diff --git a/lualib/compat10/mysql.lua b/lualib/compat10/mysql.lua new file mode 100644 index 00000000..f0b9dd27 --- /dev/null +++ b/lualib/compat10/mysql.lua @@ -0,0 +1 @@ +return require "skynet.db.mysql" \ No newline at end of file diff --git a/lualib/compat10/netpack.lua b/lualib/compat10/netpack.lua new file mode 100644 index 00000000..eb1350ab --- /dev/null +++ b/lualib/compat10/netpack.lua @@ -0,0 +1 @@ +return require "skynet.netpack" \ No newline at end of file diff --git a/lualib/compat10/profile.lua b/lualib/compat10/profile.lua new file mode 100644 index 00000000..3d8dc69c --- /dev/null +++ b/lualib/compat10/profile.lua @@ -0,0 +1 @@ +return require "skynet.profile" \ No newline at end of file diff --git a/lualib/compat10/redis.lua b/lualib/compat10/redis.lua new file mode 100644 index 00000000..8ca502fd --- /dev/null +++ b/lualib/compat10/redis.lua @@ -0,0 +1 @@ +return require "skynet.db.redis" \ No newline at end of file diff --git a/lualib/compat10/sharedata.lua b/lualib/compat10/sharedata.lua new file mode 100644 index 00000000..59d9c942 --- /dev/null +++ b/lualib/compat10/sharedata.lua @@ -0,0 +1 @@ +return require "skynet.sharedata" \ No newline at end of file diff --git a/lualib/compat10/sharemap.lua b/lualib/compat10/sharemap.lua new file mode 100644 index 00000000..f377542b --- /dev/null +++ b/lualib/compat10/sharemap.lua @@ -0,0 +1 @@ +return require "skynet.sharemap" \ No newline at end of file diff --git a/lualib/compat10/snax.lua b/lualib/compat10/snax.lua new file mode 100644 index 00000000..bdeda17d --- /dev/null +++ b/lualib/compat10/snax.lua @@ -0,0 +1 @@ +return require "skynet.snax" \ No newline at end of file diff --git a/lualib/compat10/socket.lua b/lualib/compat10/socket.lua new file mode 100644 index 00000000..99eec22f --- /dev/null +++ b/lualib/compat10/socket.lua @@ -0,0 +1 @@ +return require "skynet.socket" \ No newline at end of file diff --git a/lualib/compat10/socketchannel.lua b/lualib/compat10/socketchannel.lua new file mode 100644 index 00000000..b74f8a4f --- /dev/null +++ b/lualib/compat10/socketchannel.lua @@ -0,0 +1 @@ +return require "skynet.socketchannel" \ No newline at end of file diff --git a/lualib/compat10/socketdriver.lua b/lualib/compat10/socketdriver.lua new file mode 100644 index 00000000..45ddf996 --- /dev/null +++ b/lualib/compat10/socketdriver.lua @@ -0,0 +1 @@ +return require "skynet.socketdriver" \ No newline at end of file diff --git a/lualib/compat10/stm.lua b/lualib/compat10/stm.lua new file mode 100644 index 00000000..933c651d --- /dev/null +++ b/lualib/compat10/stm.lua @@ -0,0 +1 @@ +return require "skynet.stm" \ No newline at end of file diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua new file mode 100644 index 00000000..79e7db74 --- /dev/null +++ b/lualib/http/httpc.lua @@ -0,0 +1,147 @@ +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 +local table = table + +local httpc = {} + +local function request(fd, method, host, url, recvheader, header, content) + local read = socket.readfunc(fd) + local write = socket.writefunc(fd) + local header_content = "" + if header then + 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 + else + -- no content-length, read all + body = body .. socket.readall(fd) + end + end + + return code, body +end + +local async_dns + +function httpc.dns(server,port) + async_dns = true + dns.server(server,port) +end + +function httpc.request(method, host, url, recvheader, header, content) + local timeout = httpc.timeout -- get httpc.timeout before any blocked api + local hostname, port = host:match"([^:]+):?(%d*)$" + if port == "" then + port = 80 + else + port = tonumber(port) + end + if async_dns and not hostname:match(".*%d+$") then + hostname = dns.resolve(hostname) + end + local fd = socket.connect(hostname, port, timeout) + local finish + if timeout then + skynet.timeout(timeout, function() + if not finish then + local temp = fd + fd = nil + socket.close(temp) + end + end) + end + local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) + finish = true + if fd then -- may close by skynet.timeout + socket.close(fd) + end + if ok then + return statuscode, body + else + error(statuscode) + end +end + +function httpc.get(...) + return httpc.request("GET", ...) +end + +local function escape(s) + return (string.gsub(s, "([^A-Za-z0-9_])", function(c) + return string.format("%%%02X", string.byte(c)) + end)) +end + +function httpc.post(host, url, form, recvheader) + local header = { + ["content-type"] = "application/x-www-form-urlencoded" + } + local body = {} + for k,v in pairs(form) do + table.insert(body, string.format("%s=%s",escape(k),escape(v))) + end + + return httpc.request("POST", host, url, recvheader, header, table.concat(body , "&")) +end + +return httpc diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua new file mode 100644 index 00000000..0131db0a --- /dev/null +++ b/lualib/http/httpd.lua @@ -0,0 +1,154 @@ +local internal = require "http.internal" + +local table = table +local string = string +local type = type + +local httpd = {} + +local http_status_msg = { + [100] = "Continue", + [101] = "Switching Protocols", + [200] = "OK", + [201] = "Created", + [202] = "Accepted", + [203] = "Non-Authoritative Information", + [204] = "No Content", + [205] = "Reset Content", + [206] = "Partial Content", + [300] = "Multiple Choices", + [301] = "Moved Permanently", + [302] = "Found", + [303] = "See Other", + [304] = "Not Modified", + [305] = "Use Proxy", + [307] = "Temporary Redirect", + [400] = "Bad Request", + [401] = "Unauthorized", + [402] = "Payment Required", + [403] = "Forbidden", + [404] = "Not Found", + [405] = "Method Not Allowed", + [406] = "Not Acceptable", + [407] = "Proxy Authentication Required", + [408] = "Request Time-out", + [409] = "Conflict", + [410] = "Gone", + [411] = "Length Required", + [412] = "Precondition Failed", + [413] = "Request Entity Too Large", + [414] = "Request-URI Too Large", + [415] = "Unsupported Media Type", + [416] = "Requested range not satisfiable", + [417] = "Expectation Failed", + [500] = "Internal Server Error", + [501] = "Not Implemented", + [502] = "Bad Gateway", + [503] = "Service Unavailable", + [504] = "Gateway Time-out", + [505] = "HTTP Version not supported", +} + +local function readall(readbytes, bodylimit) + local tmpline = {} + local body = internal.recvheader(readbytes, tmpline, "") + if not body then + return 413 -- Request Entity Too Large + end + local request = assert(tmpline[1]) + local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" + assert(method and url and httpver) + httpver = assert(tonumber(httpver)) + if httpver < 1.0 or httpver > 1.1 then + return 505 -- HTTP Version not supported + end + local header = internal.parseheader(tmpline,2,{}) + if not header then + return 400 -- Bad request + 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 + return 501 -- Not Implemented + end + end + + if mode == "chunked" then + body, header = internal.recvchunkedbody(readbytes, bodylimit, header, body) + if not body then + return 413 + end + else + -- identity mode + if length then + if bodylimit and length > bodylimit then + return 413 + end + if #body >= length then + body = body:sub(1,length) + else + local padding = readbytes(length - #body) + body = body .. padding + end + end + end + + return 200, url, method, header, body +end + +function httpd.read_request(...) + local ok, code, url, method, header, body = pcall(readall, ...) + if ok then + return code, url, method, header, body + else + return nil, code + end +end + +local function writeall(writefunc, statuscode, bodyfunc, header) + local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "") + writefunc(statusline) + if header then + for k,v in pairs(header) do + if type(v) == "table" then + for _,v in ipairs(v) do + writefunc(string.format("%s: %s\r\n", k,v)) + end + else + writefunc(string.format("%s: %s\r\n", k,v)) + end + end + end + local t = type(bodyfunc) + if t == "string" then + writefunc(string.format("content-length: %d\r\n\r\n", #bodyfunc)) + writefunc(bodyfunc) + elseif t == "function" then + writefunc("transfer-encoding: chunked\r\n") + while true do + local s = bodyfunc() + if s then + if s ~= "" then + writefunc(string.format("\r\n%x\r\n", #s)) + writefunc(s) + end + else + writefunc("\r\n0\r\n\r\n") + break + end + end + else + assert(t == "nil") + writefunc("\r\n") + end +end + +function httpd.write_response(...) + return pcall(writeall, ...) +end + +return httpd diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua new file mode 100644 index 00000000..5bb35752 --- /dev/null +++ b/lualib/http/internal.lua @@ -0,0 +1,144 @@ +local table = table +local type = type + +local M = {} + +local LIMIT = 8192 + +local function chunksize(readbytes, body) + while true do + local f,e = body:find("\r\n",1,true) + if f then + return tonumber(body:sub(1,f-1),16), body:sub(e+1) + end + if #body > 128 then + -- pervent the attacker send very long stream without \r\n + return + end + body = body .. readbytes() + end +end + +local function readcrln(readbytes, body) + if #body >= 2 then + if body:sub(1,2) ~= "\r\n" then + return + end + return body:sub(3) + else + body = body .. readbytes(2-#body) + if body ~= "\r\n" then + return + end + return "" + end +end + +function M.recvheader(readbytes, lines, header) + if #header >= 2 then + if header:find "^\r\n" then + return header:sub(3) + end + end + local result + local e = header:find("\r\n\r\n", 1, true) + if e then + result = header:sub(e+4) + else + while true do + local bytes = readbytes() + header = header .. bytes + if #header > LIMIT then + return + end + e = header:find("\r\n\r\n", -#bytes-3, true) + if e then + result = header:sub(e+4) + break + end + if header:find "^\r\n" then + return header:sub(3) + end + end + end + for v in header:gmatch("(.-)\r\n") do + if v == "" then + break + end + table.insert(lines, v) + end + return result +end + +function M.parseheader(lines, from, header) + local name, value + for i=from,#lines do + local line = lines[i] + if line:byte(1) == 9 then -- tab, append last line + if name == nil then + return + end + header[name] = header[name] .. line:sub(2) + else + name, value = line:match "^(.-):%s*(.*)" + if name == nil or value == nil then + return + end + name = name:lower() + if header[name] then + local v = header[name] + if type(v) == "table" then + table.insert(v, value) + else + header[name] = { v , value } + end + else + header[name] = value + end + end + end + return header +end + +function M.recvchunkedbody(readbytes, bodylimit, header, body) + local result = "" + local size = 0 + + while true do + local sz + sz , body = chunksize(readbytes, body) + if not sz then + return + end + if sz == 0 then + break + end + size = size + sz + if bodylimit and size > bodylimit then + return + end + if #body >= sz then + result = result .. body:sub(1,sz) + body = body:sub(sz+1) + else + result = result .. body .. readbytes(sz - #body) + body = "" + end + body = readcrln(readbytes, body) + if not body then + return + end + end + + local tmpline = {} + body = M.recvheader(readbytes, tmpline, body) + if not body then + return + end + + header = M.parseheader(tmpline,1,header) + + return result, header +end + +return M diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua new file mode 100644 index 00000000..f5d6ae80 --- /dev/null +++ b/lualib/http/sockethelper.lua @@ -0,0 +1,105 @@ +local socket = require "skynet.socket" +local skynet = require "skynet" + +local readbytes = socket.read +local writebytes = socket.write + +local sockethelper = {} +local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end }) + +sockethelper.socket_error = socket_error + +local function preread(fd, str) + return function (sz) + if str then + if sz == #str or sz == nil then + local ret = str + str = nil + return ret + else + if sz < #str then + local ret = str:sub(1,sz) + str = str:sub(sz + 1) + return ret + else + sz = sz - #str + local ret = readbytes(fd, sz) + if ret then + return str .. ret + else + error(socket_error) + end + end + end + else + local ret = readbytes(fd, sz) + if ret then + return ret + else + error(socket_error) + end + end + end +end + +function sockethelper.readfunc(fd, pre) + if pre then + return preread(fd, pre) + end + return function (sz) + local ret = readbytes(fd, sz) + if ret then + return ret + else + error(socket_error) + end + end +end + +sockethelper.readall = socket.readall + +function sockethelper.writefunc(fd) + return function(content) + local ok = writebytes(fd, content) + if not ok then + error(socket_error) + end + end +end + +function sockethelper.connect(host, port, timeout) + local fd + if timeout then + local drop_fd + local co = coroutine.running() + -- asynchronous connect + skynet.fork(function() + fd = socket.open(host, port) + if drop_fd then + -- sockethelper.connect already return, and raise socket_error + socket.close(fd) + else + -- socket.open before sleep, wakeup. + skynet.wakeup(co) + end + end) + skynet.sleep(timeout) + if not fd then + -- not connect yet + drop_fd = true + end + else + -- block connect + fd = socket.open(host, port) + end + if fd then + return fd + end + error(socket_error) +end + +function sockethelper.close(fd) + socket.close(fd) +end + +return sockethelper diff --git a/lualib/http/url.lua b/lualib/http/url.lua new file mode 100644 index 00000000..ae74b099 --- /dev/null +++ b/lualib/http/url.lua @@ -0,0 +1,28 @@ +local url = {} + +local function decode_func(c) + return string.char(tonumber(c, 16)) +end + +local function decode(str) + local str = str:gsub('+', ' ') + return str:gsub("%%(..)", decode_func) +end + +function url.parse(u) + local path,query = u:match "([^?]*)%??(.*)" + if path then + path = decode(path) + end + return path, query +end + +function url.parse_query(q) + local r = {} + for k,v in q:gmatch "(.-)=([^&]*)&?" do + r[decode(k)] = decode(v) + end + return r +end + +return url diff --git a/lualib/jsonpack.lua b/lualib/jsonpack.lua deleted file mode 100644 index b1da80cd..00000000 --- a/lualib/jsonpack.lua +++ /dev/null @@ -1,19 +0,0 @@ -local cjson = require "cjson" - -local jsonpack = {} - -function jsonpack.pack(session, v) - return string.format("%d+%s", session, cjson.encode(v)) -end - -function jsonpack.response(session, v) - return string.format("%d-%s",session, cjson.encode(v)) -end - -function jsonpack.unpack(msg) - local session,t,str = string.match(msg, "(%d+)(.)(.*)") - assert(t == '+') - return tonumber(session) , cjson.decode(str) -end - -return jsonpack \ No newline at end of file diff --git a/lualib/loader.lua b/lualib/loader.lua index 05224b92..1c4ee58a 100644 --- a/lualib/loader.lua +++ b/lualib/loader.lua @@ -3,11 +3,13 @@ for word in string.gmatch(..., "%S+") do table.insert(args, word) end +SERVICE_NAME = args[1] + local main, pattern local err = {} for pat in string.gmatch(LUA_SERVICE, "([^;]+);*") do - local filename = string.gsub(pat, "?", args[1]) + local filename = string.gsub(pat, "?", SERVICE_NAME) local f, msg = loadfile(filename) if not f then table.insert(err, msg) diff --git a/lualib/md5.lua b/lualib/md5.lua index 26f5cfa6..90e28739 100644 --- a/lualib/md5.lua +++ b/lualib/md5.lua @@ -15,4 +15,24 @@ function core.sumhexa (k) end)) end -return core \ No newline at end of file +local function get_ipad(c) + return string.char(c:byte() ~ 0x36) +end + +local function get_opad(c) + return string.char(c:byte() ~ 0x5c) +end + +function core.hmacmd5(data,key) + if #key>64 then + key=core.sum(key) + key=key:sub(1,16) + end + local ipad_s=key:gsub(".", get_ipad)..string.rep("6",64-#key) + local opad_s=key:gsub(".", get_opad)..string.rep("\\",64-#key) + local istr=core.sum(ipad_s..data) + local ostr=core.sumhexa(opad_s..istr) + return ostr +end + +return core diff --git a/lualib/mongo.lua b/lualib/mongo.lua deleted file mode 100644 index 7b1c6f69..00000000 --- a/lualib/mongo.lua +++ /dev/null @@ -1,322 +0,0 @@ -local bson = require "bson" -local socket = require "socket" -local socketchannel = require "socketchannel" -local skynet = require "skynet" -local driver = require "mongo.driver" -local md5 = require "md5" -local rawget = rawget -local assert = assert - -local bson_encode = bson.encode -local bson_encode_order = bson.encode_order -local bson_decode = bson.decode -local empty_bson = bson_encode {} - -local mongo = {} -mongo.null = assert(bson.null) -mongo.maxkey = assert(bson.maxkey) -mongo.minkey = assert(bson.minkey) -mongo.type = assert(bson.type) - -local mongo_cursor = {} -local cursor_meta = { - __index = mongo_cursor, -} - -local mongo_client = {} - -local client_meta = { - __index = function(self, key) - return rawget(mongo_client, key) or self:getDB(key) - end, - __tostring = function (self) - local port_string - if self.port then - port_string = ":" .. tostring(self.port) - else - port_string = "" - end - - return "[mongo client : " .. self.host .. port_string .."]" - end, - -- DO NOT need disconnect, because channel will shutdown during gc -} - -local mongo_db = {} - -local db_meta = { - __index = function (self, key) - return rawget(mongo_db, key) or self:getCollection(key) - end, - __tostring = function (self) - return "[mongo db : " .. self.name .. "]" - end -} - -local mongo_collection = {} -local collection_meta = { - __index = function(self, key) - return rawget(mongo_collection, key) or self:getCollection(key) - end , - __tostring = function (self) - return "[mongo collection : " .. self.full_name .. "]" - end -} - -local function dispatch_reply(so) - local len_reply = so:read(4) - local reply = so:read(driver.length(len_reply)) - local result = { result = {} } - local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, result.result) - result.document = document - result.cursor_id = cursor_id - result.startfrom = startfrom - result.data = reply - return reply_id, succ, result -end - -local function mongo_auth(mongoc) - local user = rawget(mongoc, "username") - local pass = rawget(mongoc, "password") - - if user == nil or pass == nil then - return - end - return function() - assert(mongoc:auth(user, pass)) - end -end - -function mongo.client( conf ) - local obj = { - host = conf.host, - port = conf.port or 27017, - } - - obj.__id = 0 - obj.__sock = socketchannel.channel { - host = obj.host, - port = obj.port, - response = dispatch_reply, - auth = mongo_auth(conf), - } - setmetatable(obj, client_meta) - obj.__sock:connect(true) -- try connect only once - return obj -end - -function mongo_client:getDB(dbname) - local db = { - connection = self, - name = dbname, - full_name = dbname, - database = false, - __cmd = dbname .. "." .. "$cmd", - } - db.database = db - - return setmetatable(db, db_meta) -end - -function mongo_client:disconnect() - if self.__sock then - local so = self.__sock - self.__sock = false - so:close() - end -end - -function mongo_client:genId() - local id = self.__id + 1 - self.__id = id - return id -end - -function mongo_client:runCommand(...) - if not self.admin then - self.admin = self:getDB "admin" - end - return self.admin:runCommand(...) -end - -function mongo_client:auth(user,password) - local password = md5.sumhexa(string.format("%s:mongo:%s",user,password)) - local result= self:runCommand "getnonce" - if result.ok ~=1 then - return false - end - - local key = md5.sumhexa(string.format("%s%s%s",result.nonce,user,password)) - local result= self:runCommand ("authenticate",1,"user",user,"nonce",result.nonce,"key",key) - return result.ok == 1 -end - -function mongo_client:logout() - local result = self:runCommand "logout" - return result.ok == 1 -end - -function mongo_db:runCommand(cmd,cmd_v,...) - local conn = self.connection - local request_id = conn:genId() - local sock = conn.__sock - local bson_cmd - if not cmd_v then - bson_cmd = bson_encode_order(cmd,1) - else - bson_cmd = bson_encode_order(cmd,cmd_v,...) - end - local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd) - local doc = sock:request(pack, request_id).document - return bson_decode(doc) -end - -function mongo_db:getCollection(collection) - local col = { - connection = self.connection, - name = collection, - full_name = self.full_name .. "." .. collection, - database = self.database, - } - self[collection] = setmetatable(col, collection_meta) - return col -end - -mongo_collection.getCollection = mongo_db.getCollection - -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) -end - -function mongo_collection: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 sock = self.connection.__sock - local pack = driver.insert(0, self.full_name, docs) - sock:request(pack) -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) -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) -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)) - local doc = sock:request(pack, request_id).document - return bson_decode(doc) -end - -function mongo_collection:find(query, selector) - return setmetatable( { - __collection = self, - __query = query and bson_encode(query) or empty_bson, - __selector = selector and bson_encode(selector), - __ptr = nil, - __data = nil, - __cursor = nil, - __document = {}, - __flags = 0, - } , cursor_meta) -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 - if self.__data == nil then - pack = driver.query(request_id, self.__flags, self.__collection.full_name,0,0,self.__query,self.__selector) - else - if self.__cursor then - pack = driver.more(request_id, self.__collection.full_name,0,self.__cursor) - else - -- no more - self.__document = nil - self.__data = nil - return false - 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 - self.__document = result.result - self.__data = result.data - self.__ptr = 1 - self.__cursor = cursor - return true - else - self.__document = nil - self.__data = nil - self.__cursor = nil - return false - end - else - 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") - end - end - end - - return true -end - -function mongo_cursor:next() - if self.__ptr == nil then - error "Call hasNext first" - end - local r = bson_decode(self.__document[self.__ptr]) - self.__ptr = self.__ptr + 1 - if self.__ptr > #self.__document then - self.__ptr = nil - end - - return r -end - -function mongo_cursor:close() - -- todo: warning hasNext after close - if self.__cursor then - local sock = self.__collection.connection.__sock - local pack = driver.kill(self.__cursor) - sock:request(pack) - end -end - -return mongo diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 1d936052..caa07db6 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -1,15 +1,17 @@ -local c = require "skynet.c" +-- 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 local pcall = pcall +local table = table -local profile = require "profile" +local profile = require "skynet.profile" -coroutine.resume = profile.resume -coroutine.yield = profile.yield +local coroutine_resume = profile.resume +local coroutine_yield = profile.yield local proto = {} local skynet = { @@ -22,7 +24,7 @@ local skynet = { PTYPE_HARBOR = 5, PTYPE_SOCKET = 6, PTYPE_ERROR = 7, - PTYPE_QUEUE = 8, + PTYPE_QUEUE = 8, -- used in deprecated mqueue, use skynet.queue instead PTYPE_DEBUG = 9, PTYPE_LUA = 10, PTYPE_SNAX = 11, @@ -34,7 +36,7 @@ skynet.cache = require "skynet.codecache" function skynet.register_protocol(class) local name = class.name local id = class.id - assert(proto[name] == nil) + assert(proto[name] == nil and proto[id] == nil) assert(type(name) == "string" and type(id) == "number" and id >=0 and id <=255) proto[name] = class proto[id] = class @@ -43,13 +45,17 @@ end local session_id_coroutine = {} local session_coroutine_id = {} local session_coroutine_address = {} +local session_response = {} +local unresponse = {} -local wakeup_session = {} +local wakeup_queue = {} local sleep_session = {} local watching_service = {} local watching_session = {} +local dead_service = {} local error_queue = {} +local fork_queue = {} -- suspend is function local suspend @@ -65,7 +71,7 @@ local function dispatch_error_queue() if session then local co = session_id_coroutine[session] session_id_coroutine[session] = nil - return suspend(co, coroutine.resume(co, false)) + return suspend(co, coroutine_resume(co, false)) end end @@ -73,7 +79,9 @@ local function _error_dispatch(error_session, error_source) if error_session == 0 then -- service is down -- Don't remove from watching_service , because user may call dead service - watching_service[error_source] = false + if watching_service[error_source] then + dead_service[error_source] = true + end for session, srv in pairs(watching_session) do if srv == error_source then table.insert(error_queue, session) @@ -89,8 +97,7 @@ end -- coroutine reuse -local coroutine_pool = {} -local coroutine_yield = coroutine.yield +local coroutine_pool = setmetatable({}, { __mode = "kv" }) local function co_create(f) local co = table.remove(coroutine_pool) @@ -105,19 +112,30 @@ local function co_create(f) end end) else - coroutine.resume(co, f) + coroutine_resume(co, f) end return co end local function dispatch_wakeup() - local co = next(wakeup_session) + local co = table.remove(wakeup_queue,1) if co then - wakeup_session[co] = nil local session = sleep_session[co] if session then session_id_coroutine[session] = "BREAK" - return suspend(co, coroutine.resume(co, true)) + return suspend(co, coroutine_resume(co, false, "BREAK")) + end + 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 @@ -126,12 +144,15 @@ end function suspend(co, result, command, param, size) if not result then local session = session_coroutine_id[co] - local addr = session_coroutine_address[co] - if session and session ~= 0 then - c.send(addr, skynet.PTYPE_ERROR, session, "") + if session then -- coroutine may fork by others (session is nil) + local addr = session_coroutine_address[co] + if session ~= 0 then + -- only call response error + c.send(addr, skynet.PTYPE_ERROR, session, "") + end + session_coroutine_id[co] = nil + session_coroutine_address[co] = nil end - session_coroutine_id[co] = nil - session_coroutine_address[co] = nil error(debug.traceback(co,tostring(command))) end if command == "CALL" then @@ -141,16 +162,97 @@ function suspend(co, result, command, param, size) sleep_session[co] = param elseif command == "RETURN" then local co_session = session_coroutine_id[co] + if co_session == 0 then + if size ~= nil then + c.trash(param, size) + end + return suspend(co, coroutine_resume(co, false)) -- send don't need ret + end local co_address = session_coroutine_address[co] - if param == nil then + if param == nil or session_response[co] then error(debug.traceback(co)) end - c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) - return suspend(co, coroutine.resume(co)) + session_response[co] = true + local ret + if not dead_service[co_address] then + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) ~= nil + if not ret then + -- If the package is too large, returns nil. so we should report error back + c.send(co_address, skynet.PTYPE_ERROR, co_session, "") + end + elseif size ~= nil then + c.trash(param, size) + ret = false + end + return suspend(co, coroutine_resume(co, ret)) + elseif command == "RESPONSE" then + local co_session = session_coroutine_id[co] + local co_address = session_coroutine_address[co] + if session_response[co] then + error(debug.traceback(co)) + end + local f = param + local function response(ok, ...) + 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 + session_response[co] = true + unresponse[response] = true + return suspend(co, coroutine_resume(co, response)) elseif command == "EXIT" then -- coroutine exit - session_coroutine_id[co] = nil - session_coroutine_address[co] = nil + local address = session_coroutine_address[co] + if address then + release_watching(address) + session_coroutine_id[co] = nil + session_coroutine_address[co] = nil + session_response[co] = nil + end + elseif command == "QUIT" then + -- service exit + return + elseif command == "USER" then + -- See skynet.coutine for detail + error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. debug.traceback(co)) + elseif command == nil then + -- debug trace + return else error("Unknown command : " .. command .. "\n" .. debug.traceback(co)) end @@ -159,45 +261,40 @@ function suspend(co, result, command, param, size) end function skynet.timeout(ti, func) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local co = co_create(func) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co end function skynet.sleep(ti) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local succ, ret = coroutine_yield("SLEEP", session) sleep_session[coroutine.running()] = nil - if ret == true then + if succ then + return + end + if ret == "BREAK" then return "BREAK" + else + error(ret) end end function skynet.yield() - return skynet.sleep("0") + return skynet.sleep(0) end -function skynet.wait() +function skynet.wait(co) local session = c.genid() - coroutine_yield("SLEEP", session) - local co = coroutine.running() + local ret, msg = coroutine_yield("SLEEP", session) + co = co or coroutine.running() sleep_session[co] = nil session_id_coroutine[session] = nil end -function skynet.register(name) - c.command("REG", name) -end - -function skynet.name(name, handle) - c.command("NAME", name .. " " .. skynet.address(handle)) -end - local self_handle function skynet.self() if self_handle then @@ -208,44 +305,55 @@ function skynet.self() end function skynet.localname(name) - return string_to_handle(c.command("QUERY", name)) -end - -function skynet.launch(...) - local addr = c.command("LAUNCH", table.concat({...}," ")) + local addr = c.command("QUERY", name) if addr then return string_to_handle(addr) end end -function skynet.now() - return tonumber(c.command("NOW")) -end +skynet.now = c.now + +local starttime function skynet.starttime() - return tonumber(c.command("STARTTIME")) + if not starttime then + starttime = c.intcommand("STARTTIME") + end + return starttime +end + +function skynet.time() + return skynet.now()/100 + (starttime or skynet.starttime()) end function skynet.exit() - skynet.send(".launcher","lua","REMOVE",skynet.self()) - c.command("EXIT") -end - -function skynet.kill(name) - if type(name) == "number" then - skynet.send(".launcher","lua","REMOVE",name) - name = skynet.address(name) + fork_queue = {} -- 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 + local address = session_coroutine_address[co] + if session~=0 and address then + c.send(address, skynet.PTYPE_ERROR, session, "") + end end - c.command("KILL",name) + for resp in pairs(unresponse) do + resp(false) + end + -- report the sources I call but haven't return + local tmp = {} + for session, address in pairs(watching_session) do + tmp[address] = true + end + for address in pairs(tmp) do + c.send(address, skynet.PTYPE_ERROR, 0, "") + end + c.command("EXIT") + -- quit service + coroutine_yield "QUIT" end function skynet.getenv(key) - local ret = c.command("GETENV",key) - if ret == "" then - return - else - return ret - end + return (c.command("GETENV",key)) end function skynet.setenv(key, value) @@ -254,12 +362,14 @@ end function skynet.send(addr, typename, ...) local p = proto[typename] - if watching_service[addr] == false then - error("Service is dead") - end return c.send(addr, p.id, 0 , p.pack(...)) end +function skynet.rawsend(addr, typename, msg, sz) + local p = proto[typename] + return c.send(addr, p.id, 0 , msg, sz) +end + skynet.genid = assert(c.genid) skynet.redirect = function(dest,source,typename,...) @@ -267,22 +377,23 @@ skynet.redirect = function(dest,source,typename,...) end skynet.pack = assert(c.pack) +skynet.packstring = assert(c.packstring) skynet.unpack = assert(c.unpack) skynet.tostring = assert(c.tostring) +skynet.trash = assert(c.trash) local function yield_call(service, session) watching_session[session] = service local succ, msg, sz = coroutine_yield("CALL", session) watching_session[session] = nil - assert(succ, debug.traceback()) + if not succ then + error "call failed" + end return msg,sz end function skynet.call(addr, typename, ...) local p = proto[typename] - if watching_service[addr] == false then - error("Service is dead") - end local session = c.send(addr, p.id , nil , p.pack(...)) if session == nil then error("call to invalid address " .. skynet.address(addr)) @@ -298,7 +409,12 @@ end function skynet.ret(msg, sz) msg = msg or "" - coroutine_yield("RETURN", msg, sz) + return coroutine_yield("RETURN", msg, sz) +end + +function skynet.response(pack) + pack = pack or skynet.pack + return coroutine_yield("RESPONSE", pack) end function skynet.retpack(...) @@ -306,20 +422,25 @@ function skynet.retpack(...) end function skynet.wakeup(co) - if sleep_session[co] and wakeup_session[co] == nil then - wakeup_session[co] = true + if sleep_session[co] then + table.insert(wakeup_queue, co) return true end end function skynet.dispatch(typename, func) - local p = assert(proto[typename],tostring(typename)) - assert(p.dispatch == nil, tostring(typename)) - p.dispatch = func + local p = proto[typename] + if func then + local ret = p.dispatch + p.dispatch = func + return ret + else + return p and p.dispatch + end end -local function unknown_request(session, address, msg, sz) - print("Unknown request :" , c.tostring(msg,sz)) +local function unknown_request(session, address, msg, sz, prototype) + skynet.error(string.format("Unknown request (%s): %s", prototype, c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end @@ -330,7 +451,7 @@ function skynet.dispatch_unknown_request(unknown) end local function unknown_response(session, address, msg, sz) - print("Response message :" , c.tostring(msg,sz)) + skynet.error(string.format("Response message : %s" , c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end @@ -340,19 +461,16 @@ function skynet.dispatch_unknown_response(unknown) return prev end -local fork_queue = {} - -local tunpack = table.unpack - function skynet.fork(func,...) - local args = { ... } + local args = table.pack(...) local co = co_create(function() - func(tunpack(args)) + func(table.unpack(args,1,args.n)) end) table.insert(fork_queue, co) + return co end -local function raw_dispatch_message(prototype, msg, sz, session, source, ...) +local function raw_dispatch_message(prototype, msg, sz, session, source) -- skynet.PTYPE_RESPONSE = 1, read skynet.h if prototype == 1 then local co = session_id_coroutine[session] @@ -362,23 +480,39 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) unknown_response(session, source, msg, sz) else session_id_coroutine[session] = nil - suspend(co, coroutine.resume(co, true, msg, sz)) + suspend(co, coroutine_resume(co, true, msg, sz)) end else - local p = assert(proto[prototype], prototype) + local p = proto[prototype] + if p == nil then + if session ~= 0 then + c.send(source, skynet.PTYPE_ERROR, session, "") + else + unknown_request(session, source, msg, sz, prototype) + end + return + end local f = p.dispatch if f then + local ref = watching_service[source] + 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 - suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...))) + 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) + unknown_request(session, source, msg, sz, proto[prototype].name) end end end -local function dispatch_message(...) +function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do local key,co = next(fork_queue) @@ -386,7 +520,7 @@ local function dispatch_message(...) break end fork_queue[key] = nil - local fork_succ, fork_err = pcall(suspend,co,coroutine.resume(co)) + local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co)) if not fork_succ then if succ then succ = false @@ -400,12 +534,7 @@ local function dispatch_message(...) end function skynet.newservice(name, ...) - local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...))) - if handle == "" then - return nil - else - return string_to_handle(handle) - end + return skynet.call(".launcher", "lua" , "LAUNCH", "snlua", name, ...) end function skynet.uniqueservice(global, ...) @@ -426,7 +555,7 @@ end function skynet.address(addr) if type(addr) == "number" then - return string.format(":%x",addr) + return string.format(":%08x",addr) else return tostring(addr) end @@ -436,13 +565,7 @@ function skynet.harbor(addr) return c.harbor(addr) end -function skynet.error(...) - local t = {...} - for i=1,#t do - t[i] = tostring(t[i]) - end - return c.error(table.concat(t, " ")) -end +skynet.error = c.error ----- register protocol do @@ -475,9 +598,9 @@ function skynet.init(f, name) if init_func == nil then f() else - if name == nil then - table.insert(init_func, f) - else + table.insert(init_func, f) + if name then + assert(type(name) == "string") assert(init_func[name] == nil) init_func[name] = f end @@ -487,22 +610,32 @@ end local function init_all() local funcs = init_func init_func = nil - for k,v in pairs(funcs) do - v() + if funcs then + for _,f in ipairs(funcs) do + f() + end end end -local function init_template(start) - init_all() - init_func = {} - start() - init_all() +local function ret(f, ...) + f() + return ... end -local function init_service(start) - local ok, err = xpcall(init_template, debug.traceback, start) +local function init_template(start, ...) + init_all() + init_func = {} + return ret(init_all, start(...)) +end + +function skynet.pcall(start, ...) + return xpcall(init_template, debug.traceback, start, ...) +end + +function skynet.init_service(start) + local ok, err = skynet.pcall(start) if not ok then - print("init service failed:", err) + skynet.error("init service failed: " .. tostring(err)) skynet.send(".launcher","lua", "ERROR") skynet.exit() else @@ -511,46 +644,49 @@ local function init_service(start) end function skynet.start(start_func) - c.callback(dispatch_message) + c.callback(skynet.dispatch_message) skynet.timeout(0, function() - init_service(start_func) - end) -end - -function skynet.filter(f ,start_func) - c.callback(function(...) - dispatch_message(f(...)) - end) - skynet.timeout(0, function() - init_service(start_func) + skynet.init_service(start_func) end) end function skynet.endless() - return c.command("ENDLESS")~=nil -end - -function skynet.abort() - c.command("ABORT") -end - -function skynet.monitor(service, query) - local monitor - if query then - monitor = skynet.queryservice(true, service) - else - monitor = skynet.uniqueservice(true, service) - end - assert(monitor, "Monitor launch failed") - c.command("MONITOR", string.format(":%08x", monitor)) + return (c.intcommand("STAT", "endless") == 1) end function skynet.mqlen() - return tonumber(c.command "MQLEN") + return c.intcommand("STAT", "mqlen") +end + +function skynet.stat(what) + return c.intcommand("STAT", what) +end + +function skynet.task(ret) + local t = 0 + for session,co in pairs(session_id_coroutine) do + if ret then + ret[session] = debug.traceback(co) + end + t = t + 1 + end + return t +end + +function skynet.term(service) + return _error_dispatch(0, service) +end + +function skynet.memlimit(bytes) + debug.getregistry().memlimit = bytes + skynet.memlimit = nil -- set only once end -- Inject internal debug framework local debug = require "skynet.debug" -debug(skynet) +debug.init(skynet, { + dispatch = skynet.dispatch_message, + suspend = suspend, +}) return skynet diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua new file mode 100644 index 00000000..dd3b9dde --- /dev/null +++ b/lualib/skynet/cluster.lua @@ -0,0 +1,55 @@ +local skynet = require "skynet" + +local clusterd +local cluster = {} + +function cluster.call(node, address, ...) + -- skynet.pack(...) will free by cluster.core.packrequest + return skynet.call(clusterd, "lua", "req", node, 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(...)) +end + +function cluster.open(port) + if type(port) == "string" then + skynet.call(clusterd, "lua", "listen", port) + else + skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + end +end + +function cluster.reload(config) + skynet.call(clusterd, "lua", "reload", config) +end + +function cluster.proxy(node, name) + return skynet.call(clusterd, "lua", "proxy", node, name) +end + +function cluster.snax(node, name, address) + local snax = require "skynet.snax" + if not address then + address = cluster.call(node, ".service", "QUERY", "snaxd" , name) + end + local handle = skynet.call(clusterd, "lua", "proxy", node, address) + return snax.bind(handle, name) +end + +function cluster.register(name, addr) + assert(type(name) == "string") + assert(addr == nil or type(addr) == "number") + return skynet.call(clusterd, "lua", "register", name, addr) +end + +function cluster.query(node, name) + return skynet.call(clusterd, "lua", "req", node, 0, skynet.pack(name)) +end + +skynet.init(function() + clusterd = skynet.uniqueservice("clusterd") +end) + +return cluster diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua new file mode 100644 index 00000000..b9280894 --- /dev/null +++ b/lualib/skynet/coroutine.lua @@ -0,0 +1,131 @@ +-- You should use this module (skynet.coroutine) instead of origin lua coroutine in skynet framework + +local coroutine = coroutine +-- origin lua coroutine module +local coroutine_resume = coroutine.resume +local coroutine_yield = coroutine.yield +local coroutine_status = coroutine.status +local coroutine_running = coroutine.running + +local select = select +local skynetco = {} + +skynetco.isyieldable = coroutine.isyieldable +skynetco.running = coroutine.running +skynetco.status = coroutine.status + +local skynet_coroutines = setmetatable({}, { __mode = "kv" }) + +function skynetco.create(f) + local co = coroutine.create(f) + -- mark co as a skynet coroutine + skynet_coroutines[co] = true + return co +end + +do -- begin skynetco.resume + + local profile = require "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, ...) + skynet_coroutines[co] = false + return unlock(co, skynet_resume(co, from, skynet_yield(from, ...))) + end + + local function resume(co, from, ok, ...) + if not ok then + return ok, ... + elseif coroutine_status(co) == "dead" then + -- the main function exit + skynet_coroutines[co] = nil + return true, ... + elseif (...) == "USER" then + return true, select(2, ...) + else + -- blocked in skynet framework, so raise the yielding message + return resume(co, from, skynet_yielding(co, from, ...)) + end + end + + -- record the root of coroutine caller (It should be a skynet thread) + local coroutine_caller = setmetatable({} , { __mode = "kv" }) + +function skynetco.resume(co, ...) + local co_status = skynet_coroutines[co] + if not co_status then + 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, caller, coroutine_resume(co, ...)) +end + +function skynetco.thread(co) + co = co or coroutine_running() + if skynet_coroutines[co] ~= nil then + return coroutine_caller[co] , false + else + return co, true + end +end + +end -- end of skynetco.resume + +function skynetco.status(co) + local status = coroutine.status(co) + if status == "suspended" then + if skynet_coroutines[co] == false then + return "blocked" + else + return "suspended" + end + else + return status + end +end + +function skynetco.yield(...) + return coroutine_yield("USER", ...) +end + +do -- begin skynetco.wrap + + local function wrap_co(ok, ...) + if ok then + return ... + else + error(...) + end + end + +function skynetco.wrap(f) + local co = skynetco.create(function(...) + return f(...) + end) + return function(...) + return wrap_co(skynetco.resume(co, ...)) + end +end + +end -- end of skynetco.wrap + +return skynetco diff --git a/lualib/datacenter.lua b/lualib/skynet/datacenter.lua similarity index 74% rename from lualib/datacenter.lua rename to lualib/skynet/datacenter.lua index 347c140e..ebf68ba1 100644 --- a/lualib/datacenter.lua +++ b/lualib/skynet/datacenter.lua @@ -10,5 +10,9 @@ function datacenter.set(...) return skynet.call("DATACENTER", "lua", "UPDATE", ...) end +function datacenter.wait(...) + return skynet.call("DATACENTER", "lua", "WAIT", ...) +end + return datacenter diff --git a/lualib/skynet/datasheet/builder.lua b/lualib/skynet/datasheet/builder.lua new file mode 100644 index 00000000..2d14ccd4 --- /dev/null +++ b/lualib/skynet/datasheet/builder.lua @@ -0,0 +1,167 @@ +local skynet = require "skynet" +local dump = require "skynet.datasheet.dump" +local core = require "skynet.datasheet.core" +local service = require "skynet.service" + +local builder = {} + +local cache = {} +local dataset = {} + +local function monitor(pointer) + skynet.fork(function() + skynet.call(service.address, "lua", "collect", pointer) + for k,v in pairs(cache) do + if v == pointer then + cache[k] = nil + return + end + end + end) +end + +local function dumpsheet(v) + if type(v) == "string" then + return v + else + return dump.dump(v) + end +end + +function builder.new(name, v) + assert(dataset[name] == nil) + local datastring = dumpsheet(v) + local pointer = core.stringpointer(datastring) + skynet.call(service.address, "lua", "update", name, pointer) + cache[datastring] = pointer + dataset[name] = datastring + monitor(pointer) +end + +function builder.update(name, v) + local lastversion = assert(dataset[name]) + local newversion = dumpsheet(v) + local diff = dump.diff(lastversion, newversion) + local pointer = core.stringpointer(diff) + skynet.call(service.address, "lua", "update", name, pointer) + cache[diff] = pointer + local lp = assert(cache[lastversion]) + skynet.send(service.address, "lua", "release", lp) + dataset[name] = diff + monitor(pointer) +end + +function builder.compile(v) + return dump.dump(v) +end + +local function datasheet_service() + +local skynet = require "skynet" + +local datasheet = {} +local handles = {} -- handle:{ ref:count , name:name , collect:resp } +local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} } + +local function releasehandle(handle) + local h = handles[handle] + h.ref = h.ref - 1 + if h.ref == 0 and h.collect then + h.collect(true) + h.collect = nil + handles[handle] = nil + end +end + +-- from builder, create or update handle +function datasheet.update(name, handle) + local t = dataset[name] + if not t then + -- new datasheet + t = { handle = handle, monitor = {} } + dataset[name] = t + handles[handle] = { ref = 1, name = name } + else + t.handle = handle + -- report update to customers + handles[handle] = { ref = 1 + #t.monitor, name = name } + + for k,v in ipairs(t.monitor) do + v(true, handle) + t.monitor[k] = nil + end + end + skynet.ret() +end + +-- from customers +function datasheet.query(name) + local t = assert(dataset[name], "create data first") + local handle = t.handle + local h = handles[handle] + h.ref = h.ref + 1 + skynet.ret(skynet.pack(handle)) +end + +-- from customers, monitor handle change +function datasheet.monitor(handle) + local h = assert(handles[handle], "Invalid data handle") + local t = dataset[h.name] + if t.handle ~= handle then -- already changes + skynet.ret(skynet.pack(t.handle)) + else + h.ref = h.ref + 1 + table.insert(t.monitor, skynet.response()) + end +end + +-- from customers, release handle , ref count - 1 +function datasheet.release(handle) + -- send message, don't ret + releasehandle(handle) +end + +-- from builder, monitor handle release +function datasheet.collect(handle) + local h = assert(handles[handle], "Invalid data handle") + if h.ref == 0 then + handles[handle] = nil + skynet.ret() + else + assert(h.collect == nil, "Only one collect allows") + h.collect = skynet.response() + end +end + +skynet.dispatch("lua", function(_,_,cmd,...) + datasheet[cmd](...) +end) + +skynet.info_func(function() + local info = {} + local tmp = {} + for k,v in pairs(handles) do + tmp[k] = v + end + for k,v in pairs(dataset) do + local h = handles[v.handle] + tmp[v.handle] = nil + info[k] = { + handle = v.handle, + monitors = #v.monitor, + } + end + for k,v in pairs(tmp) do + info[k] = v.ref + end + + return info +end) + +end + +skynet.init(function() + service.new("datasheet", datasheet_service) +end) + +return builder diff --git a/lualib/skynet/datasheet/dump.lua b/lualib/skynet/datasheet/dump.lua new file mode 100644 index 00000000..c8d07a00 --- /dev/null +++ b/lualib/skynet/datasheet/dump.lua @@ -0,0 +1,270 @@ +--[[ file format +document : + int32 strtbloffset + int32 n + int32*n index table + table*n + strings + +table: + int32 array + int32 dict + int8*(array+dict) type (align 4) + value*array + kvpair*dict + +kvpair: + string k + value v + +value: (union) + int32 integer + float real + int32 boolean + int32 table index + int32 string offset + +type: (enum) + 0 nil + 1 integer + 2 real + 3 boolean + 4 table + 5 string +]] + +local ctd = {} +local math = math +local table = table +local string = string + +function ctd.dump(root) + local doc = { + table_n = 0, + table = {}, + strings = {}, + offset = 0, + } + local function dump_table(t) + local index = doc.table_n + 1 + doc.table_n = index + doc.table[index] = false -- place holder + local array_n = 0 + local array = {} + local kvs = {} + local types = {} + local function encode(v) + local t = type(v) + if t == "table" then + local index = dump_table(v) + return '\4', string.pack(" 0 and ik <= array_n) + end + end + -- encode table + local typeset = table.concat(types) + local align = string.rep("\0", (4 - #typeset & 3) & 3) + local tmp = { + string.pack(" #self.__document then + self.__ptr = nil + end + + return r +end + +function mongo_cursor:close() + -- todo: warning hasNext after close + if self.__cursor then + local sock = self.__collection.connection.__sock + local pack = driver.kill(self.__cursor) + sock:request(pack) + end +end + +return mongo diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua new file mode 100644 index 00000000..b5d97e2c --- /dev/null +++ b/lualib/skynet/db/mysql.lua @@ -0,0 +1,683 @@ +-- 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 mysqlaux = require "skynet.mysqlaux.c" +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 new_tab = function (narr, nrec) return {} end + + +local _M = { _VERSION = '0.13' } +-- constants + +local STATE_CONNECTED = 1 +local STATE_COMMAND_SENT = 2 + +local COM_QUERY = 0x03 + +local SERVER_MORE_RESULTS_EXISTS = 8 + +-- 16MB - 1, the default max allowed packet size used by libmysqlclient +local FULL_PACKET_SIZE = 16777215 + + +local mt = { __index = _M } + + +-- mysql field value type converters +local converters = new_tab(0, 8) + +for i = 0x01, 0x05 do + -- tiny, short, long, float, double + converters[i] = tonumber +end +converters[0x08] = tonumber -- long long +converters[0x09] = tonumber -- int24 +converters[0x0d] = tonumber -- year +converters[0xf6] = tonumber -- newdecimal + + +local function _get_byte2(data, i) + return strunpack(" self._max_packet_size then + return nil, nil, "packet size too big: " .. len + end + + local num = strbyte(data, pos) + + self.packet_no = num + + data = sock:read(len) + + if not data then + return nil, nil, "failed to read packet content: " + end + + + local field_count = strbyte(data, 1) + local typ + if field_count == 0x00 then + typ = "OK" + elseif field_count == 0xff then + typ = "ERR" + elseif field_count == 0xfe then + typ = "EOF" + elseif field_count <= 250 then + typ = "DATA" + end + + return data, typ +end + + +local function _from_length_coded_bin(data, pos) + local first = strbyte(data, pos) + + if not first then + return nil, pos + end + + if first >= 0 and first <= 250 then + return first, pos + 1 + end + + if first == 251 then + return 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 = new_tab(0, 5) + 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 = new_tab(0, 2) + 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 + if compact then + row = new_tab(ncols, 0) + else + row = new_tab(0, ncols) + end + for i = 1, ncols do + local value + value, pos = _from_length_coded_str(data, pos) + local col = cols[i] + local typ = col.type + local name = col.name + + 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) + -- 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 + 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 + end + + if typ == 'EOF' then + error "old pre-4.1 authentication protocol not supported" + end + + if typ ~= 'OK' then + error "bad packet type: " + end + return true, true + end +end + + +local function _mysql_login(self,user,password,database,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 + + 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, "pipeline is null") + + local fd = self[1] + + local cmds = {} + for _, cmd in ipairs(ops) do + compose_table(cmds, cmd) + end + + if resp then + return fd:request(cmds, function (fd) + for i=1, #ops do + local ok, out = read_response(fd) + table.insert(resp, {ok = ok, out = out}) + end + return true, resp + end) + else + return fd:request(cmds, function (fd) + local ok, out + for i=1, #ops do + ok, out = read_response(fd) + end + -- return last response + return ok,out + end) + end end --- watch mode @@ -153,13 +233,13 @@ local watchmeta = { local function watch_login(obj, auth) return function(so) if auth then - so:request("AUTH "..auth.."\r\n", read_response) + so:request(compose_message("AUTH", auth), read_response) end for k in pairs(obj.__psubscribe) do - so:request(compose_message { "PSUBSCRIBE", k }) + so:request(compose_message ("PSUBSCRIBE", k)) end for k in pairs(obj.__subscribe) do - so:request(compose_message { "SUBSCRIBE", k }) + so:request(compose_message("SUBSCRIBE", k)) end end end @@ -173,6 +253,7 @@ function redis.watch(db_conf) host = db_conf.host, port = db_conf.port or 6379, auth = watch_login(obj, db_conf.auth), + nodelay = true, } obj.__sock = channel @@ -192,7 +273,7 @@ local function watch_func( name ) local so = self.__sock for i = 1, select("#", ...) do local v = select(i, ...) - so:request(compose_message { NAME, v }) + so:request(compose_message(NAME, v)) end end end diff --git a/lualib/skynet/db/redis/cluster.lua b/lualib/skynet/db/redis/cluster.lua new file mode 100644 index 00000000..99e37577 --- /dev/null +++ b/lualib/skynet/db/redis/cluster.lua @@ -0,0 +1,383 @@ +-- a simple redis-cluster client +-- rewrite from https://github.com/antirez/redis-rb-cluster + +local skynet = require "skynet" +local redis = require "skynet.db.redis" +local crc16 = require "skynet.db.redis.crc16" + +local RedisClusterHashSlots = 16384 +local RedisClusterRequestTTL = 16 + + +local _M = {} + +local rediscluster = {} +rediscluster.__index = rediscluster +_M.rediscluster = rediscluster + +function _M.new(startup_nodes,opt) + if #startup_nodes == 0 then + startup_nodes = {startup_nodes,} + end + opt = opt or {} + local self = { + startup_nodes = startup_nodes, + max_connections = opt.max_connections or 16, + connections = setmetatable({},{__mode = "kv"}), + opt = opt, + refresh_table_asap = false, + } + setmetatable(self,rediscluster) + self:initialize_slots_cache() + return self +end + +local function nodename(node) + return string.format("%s:%s",node.host,node.port) +end + +function rediscluster:get_redis_link(node) + local conf = { + host = node.host, + port = node.port, + auth = self.opt.auth, + db = self.opt.db or 0, + } + return redis.connect(conf) +end + +-- Given a node (that is just a Ruby hash) give it a name just +-- concatenating the host and port. We use the node name as a key +-- to cache connections to that node. +function rediscluster:set_node_name(node) + if not node.name then + node.name = nodename(node) + end + if not node.slaves then + local oldnode = self.name_node[node.name] + if oldnode then + node.slaves = oldnode.slaves + end + end + self.name_node[node.name] = node +end + +-- Contact the startup nodes and try to fetch the hash slots -> instances +-- map in order to initialize the @slots hash. +function rediscluster:initialize_slots_cache() + self.slots = {} + self.nodes = {} + self.name_node = {} + for _,startup_node in ipairs(self.startup_nodes) do + local ok = pcall(function () + local name = nodename(startup_node) + local conn = self.connections[name] or self:get_redis_link(startup_node) + local list = conn:cluster("slots") + for _,result in ipairs(list) do + local ip,port,runid = table.unpack(result[3]) + local master_node = { + host = ip, + port = port, + runid = runid, + slaves = {}, + } + self:set_node_name(master_node) + for i=4,#result do + local ip,port,runid = table.unpack(result[i]) + local slave_node = { + host = ip, + port = port, + runid = runid, + } + self:set_node_name(slave_node) + table.insert(master_node.slaves,slave_node) + end + for slot=tonumber(result[1]),tonumber(result[2]) do + table.insert(self.nodes,master_node) + self.slots[slot] = master_node + end + end + self.refresh_table_asap = false + if not self.connections[name] then + self.connections[name] = conn + end + end) + -- Exit the loop as long as the first node replies + if ok then + break + end + end +end + +-- Flush the cache, mostly useful for debugging when we want to force +-- redirection. +function rediscluster:flush_slots_cache() + self.slots = {} +end + +-- Return the hash slot from the key. +function rediscluster:keyslot(key) + -- Only hash what is inside {...} if there is such a pattern in the key. + -- Note that the specification requires the content that is between + -- the first { and the first } after the first {. If we found {} without + -- nothing in the middle, the whole key is hashed as usually. + local startpos = string.find(key,"{",1,true) + if startpos then + local endpos = string.find(key,"}",startpos+1,true) + if endpos and endpos ~= startpos + 1 then + key = string.sub(key,startpos+1,endpos-1) + end + end + return crc16(key) % RedisClusterHashSlots +end + +-- Return the first key in the command arguments. +-- +-- Currently we just return argv[1], that is, the first argument +-- after the command name. +-- +-- This is indeed the key for most commands, and when it is not true +-- the cluster redirection will point us to the right node anyway. +-- +-- For commands we want to explicitly bad as they don't make sense +-- in the context of cluster, nil is returned. +function rediscluster:get_key_from_command(argv) + local cmd,key = table.unpack(argv) + cmd = string.lower(cmd) + if cmd == "info" or + cmd == "multi" or + cmd == "exec" or + cmd == "slaveof" or + cmd == "config" or + cmd == "shutdown" then + return nil + end + -- Unknown commands, and all the commands having the key + -- as first argument are handled here: + -- set, get, ... + return key +end + +-- If the current number of connections is already the maximum number +-- allowed, close a random connection. This should be called every time +-- we cache a new connection in the @connections hash. +function rediscluster:close_existing_connection() + local length = 0 + for name,conn in pairs(self.connections) do + length = length + 1 + end + if length >= self.max_connections then + pcall(function () + local name,conn = next(self.connections) + self.connections[name] = nil + conn:disconnect() + end) + end +end + +function rediscluster:close_all_connection() + local connections = self.connections + self.connections = setmetatable({},{__mode = "kv"}) + for name,conn in pairs(connections) do + pcall(conn.disconnect,conn) + end +end + +function rediscluster:get_connection(node) + if type(node) == "string" then + local ip,port = string.match(node,"^([^:]+):([^:]+)$") + node = {host=ip,port=port} + end + local name = node.name or nodename(node) + local conn = self.connections[name] + if not conn then + conn = self:get_redis_link(node) + self.connections[name] = conn + end + return self.connections[name] +end + +-- Return a link to a random node, or raise an error if no node can be +-- contacted. This function is only called when we can't reach the node +-- associated with a given hash slot, or when we don't know the right +-- mapping. +-- The function will try to get a successful reply to the PING command, +-- otherwise the next node is tried. +function rediscluster:get_random_connection() + -- shuffle + local shuffle_idx = {} + local startpos = 1 + local endpos = #self.nodes + for i=startpos,endpos do + shuffle_idx[i] = i + end + for i=startpos,endpos do + local idx = math.random(i,endpos) + local tmp = shuffle_idx[i] + shuffle_idx[i] = shuffle_idx[idx] + shuffle_idx[idx] = tmp + end + for i,idx in ipairs(shuffle_idx) do + local ok,conn = pcall(function () + local node = self.nodes[idx] + local conn = self.connections[node.name] + if not conn then + -- Connect the node if it is not connected + conn = self:get_redis_link(node) + if conn:ping() == "PONG" then + self:close_existing_connection() + self.connections[node.name] = conn + return conn + else + -- If the connection is not good close it ASAP in order + -- to avoid waiting for the GC finalizer. File + -- descriptors are a rare resource. + conn:disconnect() + end + else + -- The node was already connected, test the connection. + if conn:ping() == "PONG" then + return conn + end + end + end) + if ok and conn then + return conn + end + end + error("Can't reach a single startup node.") +end + +-- Given a slot return the link (Redis instance) to the mapped node. +-- Make sure to create a connection with the node if we don't have +-- one. +function rediscluster:get_connection_by_slot(slot) + local node = self.slots[slot] + -- If we don't know what the mapping is, return a random node. + if not node then + return self:get_random_connection() + end + if not self.connections[node.name] then + local ok = pcall(function () + self:close_existing_connection() + self.connections[node.name] = self:get_redis_link(node) + end) + if not ok then + if self.opt.read_slave and node.slaves and #node.slaves > 0 then + local slave_node = node.slaves[math.random(1,#node.slaves)] + local ok2,conn = pcall(self.get_connection,self,slave_node) + if ok2 then + conn:readonly() -- allow this connection read-slave + return conn + end + end + -- This will probably never happen with recent redis-rb + -- versions because the connection is enstablished in a lazy + -- way only when a command is called. However it is wise to + -- handle an instance creation error of some kind. + return self:get_random_connection() + end + end + return self.connections[node.name] +end + +-- Dispatch commands. +function rediscluster:call(...) + local argv = table.pack(...) + if self.refresh_table_asap then + self:initialize_slots_cache() + end + local ttl = RedisClusterRequestTTL -- Max number of redirections + local err + local asking = false + local try_random_node = false + while ttl > 0 do + ttl = ttl - 1 + local key = self:get_key_from_command(argv) + if not key then + error("No way to dispatch this command to Redis Cluster: " .. tostring(argv[1])) + end + local conn + local slot = self:keyslot(key) + if asking then + conn = self:get_connection(asking) + elseif try_random_node then + conn = self:get_random_connection() + try_random_node = false + else + conn = self:get_connection_by_slot(slot) + end + local result = {pcall(function () + -- TODO: use pipelining to send asking and save a rtt. + if asking then + conn:asking() + end + asking = false + local cmd = argv[1] + local func = conn[cmd] + return func(conn,table.unpack(argv,2)) + end)} + local ok = result[1] + if not ok then + err = table.unpack(result,2) + err = tostring(err) + if err == "[Error: socket]" then + -- may be nerver come here? + try_random_node = true + if ttl < RedisClusterRequestTTL/2 then + skynet.sleep(10) + end + else + -- err: ./lualib/skynet/socketchannel.lua:371: xxx + err = string.match(err,".+:%d+:%s(.*)$") or err + local errlist = {} + for e in string.gmatch(err,"([^%s]+)%s?") do + table.insert(errlist,e) + end + if (errlist[1] ~= "MOVED" and errlist[1] ~= "ASK") then + error(err) + else + if errlist[1] == "ASK" then + asking = true + else + -- Serve replied with MOVED. It's better for us to + -- ask for CLUSTER SLOTS the next time. + self.refresh_table_asap = true + end + local newslot = tonumber(errlist[2]) + local node_ip,node_port = string.match(errlist[3],"^([^:]+):([^:]+)$") + local node = { + host = node_ip, + port = tonumber(node_port), + } + if not asking then + self:set_node_name(node) + self.slots[newslot] = node + else + asking = node + end + end + end + else + return table.unpack(result,2) + end + end + error(string.format("Too many Cluster redirections?,maybe node is disconnected (last error: %q)",err)) +end + +-- Currently we handle all the commands using method_missing for +-- simplicity. For a Cluster client actually it will be better to have +-- every single command as a method with the right arity and possibly +-- additional checks (example: RPOPLPUSH with same src/dst key, SORT +-- without GET or BY, and so forth). +setmetatable(rediscluster,{ + __index = function (t,cmd) + t[cmd] = function (self,...) + return self:call(cmd,...) + end + return t[cmd] + end, +}) + + +return _M diff --git a/lualib/skynet/db/redis/crc16.lua b/lualib/skynet/db/redis/crc16.lua new file mode 100644 index 00000000..1acab9f5 --- /dev/null +++ b/lualib/skynet/db/redis/crc16.lua @@ -0,0 +1,62 @@ +--/* +-- This is the CRC16 algorithm used by Redis Cluster to hash keys. +-- Implementation according to CCITT standards. +-- +-- This is actually the XMODEM CRC 16 algorithm, using the +-- following parameters: +-- +-- Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN" +-- Width : 16 bit +-- Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) +-- Initialization : 0000 +-- Reflect Input byte : False +-- Reflect Output CRC : False +-- Xor constant to output CRC : 0000 +-- Output for "123456789" : 31C3 +--*/ + + + +local XMODEMCRC16Lookup = { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +} + +return function(bytes) + local crc = 0 + for i=1,#bytes do + local b = string.byte(bytes,i,i) + crc = ((crc<<8) & 0xffff) ~ XMODEMCRC16Lookup[(((crc>>8)~b) & 0xff) + 1] + end + return tonumber(crc) +end diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 807ee2ae..f776c96e 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -1,49 +1,107 @@ -return function (skynet) +local table = table +local extern_dbgcmd = {} -local internal_info_func +local function init(skynet, export) + local internal_info_func -function skynet.info_func(func) - internal_info_func = func -end - -local dbgcmd = {} - -function dbgcmd.MEM() - local kb, bytes = collectgarbage "count" - skynet.ret(skynet.pack(kb,bytes)) -end - -function dbgcmd.GC() - coroutine_pool = {} - collectgarbage "collect" -end - -function dbgcmd.STAT() - local stat = {} - stat.mqlen = skynet.mqlen() - skynet.ret(skynet.pack(stat)) -end - -function dbgcmd.INFO() - if internal_info_func then - skynet.ret(skynet.pack(internal_info_func())) - else - skynet.ret(skynet.pack(nil)) + function skynet.info_func(func) + internal_info_func = func end + + local dbgcmd + + local function init_dbgcmd() + dbgcmd = {} + + function dbgcmd.MEM() + local kb, bytes = collectgarbage "count" + skynet.ret(skynet.pack(kb,bytes)) + end + + function dbgcmd.GC() + + collectgarbage "collect" + end + + function dbgcmd.STAT() + local stat = {} + stat.task = skynet.task() + stat.mqlen = skynet.stat "mqlen" + stat.cpu = skynet.stat "cpu" + stat.message = skynet.stat "message" + skynet.ret(skynet.pack(stat)) + end + + function dbgcmd.TASK() + local task = {} + skynet.task(task) + skynet.ret(skynet.pack(task)) + end + + function dbgcmd.INFO(...) + if internal_info_func then + skynet.ret(skynet.pack(internal_info_func(...))) + else + skynet.ret(skynet.pack(nil)) + end + end + + function dbgcmd.EXIT() + skynet.exit() + end + + function dbgcmd.RUN(source, filename) + local inject = require "skynet.inject" + local ok, output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) + collectgarbage "collect" + skynet.ret(skynet.pack(ok, table.concat(output, "\n"))) + end + + function dbgcmd.TERM(service) + skynet.term(service) + end + + function dbgcmd.REMOTEDEBUG(...) + local remotedebug = require "skynet.remotedebug" + remotedebug.start(export, ...) + end + + function dbgcmd.SUPPORT(pname) + return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) + end + + function dbgcmd.PING() + return skynet.ret() + end + + function dbgcmd.LINK() + skynet.response() -- get response , but not return. raise error when exit + end + + return dbgcmd + end -- function init_dbgcmd + + local function _debug_dispatch(session, address, cmd, ...) + dbgcmd = dbgcmd or init_dbgcmd() -- lazy init dbgcmd + local f = dbgcmd[cmd] or extern_dbgcmd[cmd] + assert(f, cmd) + f(...) + end + + skynet.register_protocol { + name = "debug", + id = assert(skynet.PTYPE_DEBUG), + pack = assert(skynet.pack), + unpack = assert(skynet.unpack), + dispatch = _debug_dispatch, + } end -local function _debug_dispatch(session, address, cmd, ...) - local f = dbgcmd[cmd] - assert(f, cmd) - f(...) +local function reg_debugcmd(name, fn) + extern_dbgcmd[name] = fn end -skynet.register_protocol { - name = "debug", - id = assert(skynet.PTYPE_DEBUG), - pack = assert(skynet.pack), - unpack = assert(skynet.unpack), - dispatch = _debug_dispatch, +return { + init = init, + reg_debugcmd = reg_debugcmd, } - -end \ No newline at end of file diff --git a/lualib/skynet/dns.lua b/lualib/skynet/dns.lua new file mode 100644 index 00000000..0b96f214 --- /dev/null +++ b/lualib/skynet/dns.lua @@ -0,0 +1,362 @@ +--[[ + lua dns resolver library + See https://github.com/xjdrew/levent/blob/master/levent/dns.lua for more detail + +-- resource record type: +-- TYPE value and meaning +-- A 1 a host address +-- NS 2 an authoritative name server +-- MD 3 a mail destination (Obsolete - use MX) +-- MF 4 a mail forwarder (Obsolete - use MX) +-- CNAME 5 the canonical name for an alias +-- SOA 6 marks the start of a zone of authority +-- MB 7 a mailbox domain name (EXPERIMENTAL) +-- MG 8 a mail group member (EXPERIMENTAL) +-- MR 9 a mail rename domain name (EXPERIMENTAL) +-- NULL 10 a null RR (EXPERIMENTAL) +-- WKS 11 a well known service description +-- PTR 12 a domain name pointer +-- HINFO 13 host information +-- MINFO 14 mailbox or mail list information +-- MX 15 mail exchange +-- TXT 16 text strings +-- AAAA 28 a ipv6 host address +-- only appear in the question section: +-- AXFR 252 A request for a transfer of an entire zone +-- MAILB 253 A request for mailbox-related records (MB, MG or MR) +-- MAILA 254 A request for mail agent RRs (Obsolete - see MX) +-- * 255 A request for all records +-- +-- resource recode class: +-- IN 1 the Internet +-- CS 2 the CSNET class (Obsolete - used only for examples in some obsolete RFCs) +-- CH 3 the CHAOS class +-- HS 4 Hesiod [Dyer 87] +-- only appear in the question section: +-- * 255 any class +-- ]] + +--[[ +-- struct header { +-- uint16_t tid # identifier assigned by the program that generates any kind of query. +-- uint16_t flags # flags +-- uint16_t qdcount # the number of entries in the question section. +-- uint16_t ancount # the number of resource records in the answer section. +-- uint16_t nscount # the number of name server resource records in the authority records section. +-- uint16_t arcount # the number of resource records in the additional records section. +-- } +-- +-- request body: +-- struct request { +-- string name +-- uint16_t atype +-- uint16_t class +-- } +-- +-- response body: +-- struct response { +-- string name +-- uint16_t atype +-- uint16_t class +-- uint16_t ttl +-- uint16_t rdlength +-- string rdata +-- } +--]] + +local skynet = require "skynet" +local socket = require "skynet.socket" + +local MAX_DOMAIN_LEN = 1024 +local MAX_LABEL_LEN = 63 +local MAX_PACKET_LEN = 2048 +local DNS_HEADER_LEN = 12 +local TIMEOUT = 30 * 100 -- 30 seconds + +local QTYPE = { + A = 1, + CNAME = 5, + AAAA = 28, +} + +local QCLASS = { + IN = 1, +} + +local weak = {__mode = "kv"} +local CACHE = {} + +local dns = {} +local request_pool = {} + +function dns.flush() + CACHE[QTYPE.A] = setmetatable({},weak) + CACHE[QTYPE.AAAA] = setmetatable({},weak) +end + +dns.flush() + +local function verify_domain_name(name) + if #name > MAX_DOMAIN_LEN then + return false + end + if not name:match("^[_%l%d%-%.]+$") then + return false + end + for w in name:gmatch("([_%w%-]+)%.?") do + if #w > MAX_LABEL_LEN then + return false + end + end + return true +end + +local next_tid = 1 +local function gen_tid() + local tid = next_tid + if request_pool[tid] then + tid = nil + for i = 1, 65535 do + -- find available tid + if not request_pool[i] then + tid = i + break + end + end + assert(tid) + end + next_tid = tid + 1 + if next_tid > 65535 then + next_tid = 1 + end + return tid +end + +local function pack_header(t) + return string.pack(">HHHHHH", + t.tid, t.flags, t.qdcount, t.ancount or 0, t.nscount or 0, t.arcount or 0) +end + +local function pack_question(name, qtype, qclass) + local labels = {} + for w in name:gmatch("([_%w%-]+)%.?") do + table.insert(labels, string.pack("s1",w)) + end + table.insert(labels, '\0') + table.insert(labels, string.pack(">HH", qtype, qclass)) + return table.concat(labels) +end + +local function unpack_header(chunk) + local tid, flags, qdcount, ancount, nscount, arcount, left = string.unpack(">HHHHHH", chunk) + return { + tid = tid, + flags = flags, + qdcount = qdcount, + ancount = ancount, + nscount = nscount, + arcount = arcount + }, left +end + +-- unpack a resource name +local function unpack_name(chunk, left) + local t = {} + local jump_pointer + local tag, offset, label + while true do + tag, left = string.unpack("B", chunk, left) + if tag & 0xc0 == 0xc0 then + -- pointer + offset,left = string.unpack(">H", chunk, left - 1) + offset = offset & 0x3fff + if not jump_pointer then + jump_pointer = left + end + -- offset is base 0, need to plus 1 + left = offset + 1 + elseif tag == 0 then + break + else + label, left = string.unpack("s1", chunk, left - 1) + t[#t+1] = label + end + end + return table.concat(t, "."), jump_pointer or left +end + +local function unpack_question(chunk, left) + local name, left = unpack_name(chunk, left) + local atype, class, left = string.unpack(">HH", chunk, left) + return { + name = name, + atype = atype, + class = class + }, left +end + +local function unpack_answer(chunk, left) + local name, left = unpack_name(chunk, left) + local atype, class, ttl, rdata, left = string.unpack(">HHI4s2", chunk, left) + return { + name = name, + atype = atype, + class = class, + ttl = ttl, + rdata = rdata + },left +end + +local function unpack_rdata(qtype, chunk) + if qtype == QTYPE.A then + local a,b,c,d = string.unpack("BBBB", chunk) + return string.format("%d.%d.%d.%d", a,b,c,d) + elseif qtype == QTYPE.AAAA then + local a,b,c,d,e,f,g,h = string.unpack(">HHHHHHHH", chunk) + return string.format("%x:%x:%x:%x:%x:%x:%x:%x", a, b, c, d, e, f, g, h) + else + error("Error qtype " .. qtype) + end +end + +local dns_server + +local function resolve(content) + if #content < DNS_HEADER_LEN then + -- drop + skynet.error("Recv an invalid package when dns query") + return + end + local answer_header,left = unpack_header(content) + -- verify answer + assert(answer_header.qdcount == 1, "malformed packet") + + local question,left = unpack_question(content, left) + + local ttl + local answer + local answers_ipv4 + local answers_ipv6 + + for i=1, answer_header.ancount do + answer, left = unpack_answer(content, left) + local answers + if answer.atype == QTYPE.A then + answers_ipv4 = answers_ipv4 or {} + answers = answers_ipv4 + elseif answer.atype == QTYPE.AAAA then + answers_ipv6 = answers_ipv6 or {} + answers = answers_ipv6 + end + if answers then + local ip = unpack_rdata(answer.atype, answer.rdata) + ttl = ttl and math.min(ttl, answer.ttl) or answer.ttl + answers[#answers+1] = ip + end + end + + if answers_ipv4 then + CACHE[QTYPE.A][question.name] = { answers = answers_ipv4, ttl = skynet.now() + ttl * 100 } + end + + if answers_ipv6 then + CACHE[QTYPE.AAAA][question.name] = { answers = answers_ipv6, ttl = skynet.now() + ttl * 100 } + end + + local resp = request_pool[answer_header.tid] + if not resp then + -- the resp may be timeout + return + end + + if question.name ~= resp.name then + skynet.error("Recv an invalid name when dns query") + end + + local r = CACHE[resp.qtype][resp.name] + if r then + resp.answers = r.answers + end + + skynet.wakeup(resp.co) +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 + dns_server = socket.udp(function(str, from) + resolve(str) + end) + socket.udp_connect(dns_server, server, port or 53) + return server +end + +local function lookup_cache(name, qtype, ignorettl) + local result = CACHE[qtype][name] + if result then + if ignorettl or (result.ttl > skynet.now()) then + return result.answers + end + end +end + +local function suspend(tid, name, qtype) + local req = { + name = name, + tid = tid, + qtype = qtype, + co = coroutine.running(), + } + request_pool[tid] = req + skynet.fork(function() + skynet.sleep(TIMEOUT) + local req = request_pool[tid] + if req then + -- cancel tid + skynet.error(string.format("DNS query %s timeout", name)) + request_pool[tid] = nil + skynet.wakeup(req.co) + end + end) + skynet.wait(req.co) + local answers = req.answers + request_pool[tid] = nil + if not req.answers then + local answers = lookup_cache(name, qtype, true) + if answers then + return answers[1], answers + end + error "timeout or no answer" + end + return req.answers[1], req.answers +end + +function dns.resolve(name, ipv6) + local qtype = ipv6 and QTYPE.AAAA or QTYPE.A + local name = name:lower() + assert(verify_domain_name(name) , "illegal name") + local answers = lookup_cache(name, qtype) + if answers then + return answers[1], answers + end + local question_header = { + tid = gen_tid(), + flags = 0x100, -- flags: 00000001 00000000, set RD + qdcount = 1, + } + local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN) + assert(dns_server, "Call dns.server first") + socket.write(dns_server, req) + return suspend(question_header.tid, name, qtype) +end + +return dns diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua new file mode 100644 index 00000000..a8873f1d --- /dev/null +++ b/lualib/skynet/harbor.lua @@ -0,0 +1,26 @@ +local skynet = require "skynet" + +local harbor = {} + +function harbor.globalname(name, handle) + handle = handle or skynet.self() + skynet.send(".cslave", "lua", "REGISTER", name, handle) +end + +function harbor.queryname(name) + return skynet.call(".cslave", "lua", "QUERYNAME", name) +end + +function harbor.link(id) + skynet.call(".cslave", "lua", "LINK", id) +end + +function harbor.connect(id) + skynet.call(".cslave", "lua", "CONNECT", id) +end + +function harbor.linkmaster() + skynet.call(".cslave", "lua", "LINKMASTER") +end + +return harbor diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua new file mode 100644 index 00000000..46c1c8ae --- /dev/null +++ b/lualib/skynet/inject.lua @@ -0,0 +1,66 @@ +local function getupvaluetable(u, func, unique) + local i = 1 + while true do + local name, value = debug.getupvalue(func, i) + if name == nil then + return + end + local t = type(value) + if t == "table" then + u[name] = value + elseif t == "function" then + if not unique[value] then + unique[value] = true + getupvaluetable(u, value, unique) + end + end + i=i+1 + end +end + +return function(skynet, source, filename , ...) + if filename then + filename = "@" .. filename + else + filename = "=(load)" + end + local output = {} + + local function print(...) + local value = { ... } + for k,v in ipairs(value) do + value[k] = tostring(v) + end + table.insert(output, table.concat(value, "\t")) + end + local u = {} + local unique = {} + local funcs = { ... } + for k, func in ipairs(funcs) do + getupvaluetable(u, func, unique) + end + local p = {} + local proto = u.proto + if proto then + for k,v in pairs(proto) do + local name, dispatch = v.name, v.dispatch + if name and dispatch and not p[name] then + local pp = {} + p[name] = pp + getupvaluetable(pp, dispatch, unique) + end + end + end + local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) + local func, err = load(source, filename, "bt", env) + if not func then + return false, { err } + end + local ok, err = skynet.pcall(func) + if not ok then + table.insert(output, err) + return false, output + end + + return true, output +end diff --git a/lualib/skynet/injectcode.lua b/lualib/skynet/injectcode.lua new file mode 100644 index 00000000..de85cb6c --- /dev/null +++ b/lualib/skynet/injectcode.lua @@ -0,0 +1,133 @@ +local debug = debug +local table = table + +local FUNC_TEMP=[[ +local $ARGS +return function(...) +$SOURCE +end, +function() +return {$LOCALS} +end +]] + +local temp = {} +local function wrap_locals(co, source, level, ext_funcs) + if co == coroutine.running() then + level = level + 3 + end + local f = debug.getinfo(co, level,"f").func + if f == nil then + return false, "Invalid level" + end + + local uv = {} + local locals = {} + local uv_id = {} + local local_id = {} + + if ext_funcs then + for k,v in pairs(ext_funcs) do + table.insert(uv, k) + end + end + local i = 1 + while true do + local name, value = debug.getlocal(co, level, i) + if name == nil then + break + end + if name:byte() ~= 40 then -- '(' + table.insert(uv, name) + table.insert(locals, ("[%d]=%s,"):format(i,name)) + local_id[name] = value + end + i = i + 1 + end + local i = 1 + while true do + local name = debug.getupvalue(f, i) + if name == nil then + break + end + uv_id[name] = i + table.insert(uv, name) + i = i + 1 + end + temp.ARGS = table.concat(uv, ",") + temp.SOURCE = source + temp.LOCALS = table.concat(locals) + local full_source = FUNC_TEMP:gsub("%$(%w+)",temp) + local loader, err = load(full_source, "=(debug)") + if loader == nil then + return false, err + end + local func, update = loader() + -- join func's upvalues + local i = 1 + while true do + local name = debug.getupvalue(func, i) + if name == nil then + break + end + if ext_funcs then + local v = ext_funcs[name] + if v then + debug.setupvalue(func, i, v) + end + end + + local local_value = local_id[name] + if local_value then + debug.setupvalue(func, i, local_value) + end + local upvalue_id = uv_id[name] + if upvalue_id then + debug.upvaluejoin(func, i, f, upvalue_id) + end + i=i+1 + end + local vararg, v = debug.getlocal(co, level, -1) + if vararg then + local vargs = { v } + local i = 2 + while true do + local vararg,v = debug.getlocal(co, level, -i) + if vararg then + vargs[i] = v + else + break + end + i=i+1 + end + return func, update, table.unpack(vargs) + else + return func, update + end +end + +local function exec(co, level, func, update, ...) + if not func then + return false, update + end + if co == coroutine.running() then + level = level + 2 + end + local rets = table.pack(pcall(func, ...)) + if rets[1] then + local needupdate = update() + for k,v in pairs(needupdate) do + debug.setlocal(co, level,k,v) + end + return table.unpack(rets, 1, rets.n) + else + return false, rets[2] + end +end + +return function (source, co, level, ext_funcs) + co = co or coroutine.running() + level = level or 0 + return exec(co, level, wrap_locals(co, source, level, ext_funcs)) +end + diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua new file mode 100644 index 00000000..4365d2e2 --- /dev/null +++ b/lualib/skynet/manager.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local c = require "skynet.core" + +function skynet.launch(...) + local addr = c.command("LAUNCH", table.concat({...}," ")) + if addr then + return tonumber("0x" .. string.sub(addr , 2)) + end +end + +function skynet.kill(name) + if type(name) == "number" then + skynet.send(".launcher","lua","REMOVE",name, true) + name = skynet.address(name) + end + c.command("KILL",name) +end + +function skynet.abort() + c.command("ABORT") +end + +local function globalname(name, handle) + local c = string.sub(name,1,1) + assert(c ~= ':') + if c == '.' then + return false + end + + assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h + assert(tonumber(name) == nil) -- global name can't be number + + local harbor = require "skynet.harbor" + + harbor.globalname(name, handle) + + return true +end + +function skynet.register(name) + if not globalname(name) then + c.command("REG", name) + end +end + +function skynet.name(name, handle) + if not globalname(name, handle) then + c.command("NAME", name .. " " .. skynet.address(handle)) + end +end + +local dispatch_message = skynet.dispatch_message + +function skynet.forward_type(map, start_func) + c.callback(function(ptype, msg, sz, ...) + local prototype = map[ptype] + if prototype then + dispatch_message(prototype, msg, sz, ...) + else + dispatch_message(ptype, msg, sz, ...) + c.trash(msg, sz) + end + end, true) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.filter(f ,start_func) + c.callback(function(...) + dispatch_message(f(...)) + end) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.monitor(service, query) + local monitor + if query then + monitor = skynet.queryservice(true, service) + else + monitor = skynet.uniqueservice(true, service) + end + assert(monitor, "Monitor launch failed") + c.command("MONITOR", string.format(":%08x", monitor)) + return monitor +end + +return skynet diff --git a/lualib/mqueue.lua b/lualib/skynet/mqueue.lua similarity index 94% rename from lualib/mqueue.lua rename to lualib/skynet/mqueue.lua index 11931200..13c2f327 100644 --- a/lualib/mqueue.lua +++ b/lualib/skynet/mqueue.lua @@ -1,5 +1,7 @@ +-- This is a deprecated module, use skynet.queue instead. + local skynet = require "skynet" -local c = require "skynet.c" +local c = require "skynet.core" local mqueue = {} local init_once diff --git a/lualib/multicast.lua b/lualib/skynet/multicast.lua similarity index 95% rename from lualib/multicast.lua rename to lualib/skynet/multicast.lua index 502f8d21..4b09044f 100644 --- a/lualib/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mc = require "multicast.c" +local mc = require "skynet.multicast.core" local multicastd local multicast = {} @@ -8,7 +8,9 @@ local dispatch = setmetatable({} , {__mode = "kv" }) local chan = {} local chan_meta = { __index = chan, - __gc = unsubscribe, + __gc = function(self) + self:unsubscribe() + end, __tostring = function (self) return string.format("[Multicast:%x]",self.channel) end, diff --git a/lualib/skynet/queue.lua b/lualib/skynet/queue.lua new file mode 100644 index 00000000..1df1a6bb --- /dev/null +++ b/lualib/skynet/queue.lua @@ -0,0 +1,38 @@ +local skynet = require "skynet" +local coroutine = coroutine +local xpcall = xpcall +local traceback = debug.traceback +local table = table + +function skynet.queue() + local current_thread + local ref = 0 + local thread_queue = {} + + local function xpcall_ret(ok, ...) + ref = ref - 1 + if ref == 0 then + current_thread = table.remove(thread_queue,1) + if current_thread then + skynet.wakeup(current_thread) + end + end + assert(ok, (...)) + return ... + end + + return function(f, ...) + local thread = coroutine.running() + if current_thread and current_thread ~= thread then + table.insert(thread_queue, thread) + skynet.wait() + assert(ref == 0) -- current_thread == thread + end + current_thread = thread + + ref = ref + 1 + return xpcall_ret(xpcall(f, traceback, ...)) + end +end + +return skynet.queue diff --git a/lualib/skynet/reload.lua b/lualib/skynet/reload.lua new file mode 100644 index 00000000..33e234b0 --- /dev/null +++ b/lualib/skynet/reload.lua @@ -0,0 +1,9 @@ +local core = require "skynet.reload.core" +local skynet = require "skynet" + +local function reload(...) + local args = SERVICE_NAME .. " " .. table.concat({...}, " ") + print(args) +end + +return reload diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua new file mode 100644 index 00000000..d0c99e27 --- /dev/null +++ b/lualib/skynet/remotedebug.lua @@ -0,0 +1,268 @@ +local skynet = require "skynet" +local debugchannel = require "skynet.debugchannel" +local socketdriver = require "skynet.socketdriver" +local injectrun = require "skynet.injectcode" +local table = table +local debug = debug +local coroutine = coroutine +local sethook = debugchannel.sethook + + +local M = {} + +local HOOK_FUNC = "raw_dispatch_message" +local raw_dispatcher +local print = _G.print +local skynet_suspend +local prompt +local newline + +local function change_prompt(s) + newline = true + prompt = s +end + +local function replace_upvalue(func, uvname, value) + local i = 1 + while true do + local name, uv = debug.getupvalue(func, i) + if name == nil then + break + end + if name == uvname then + if value then + debug.setupvalue(func, i, value) + end + return uv + end + i = i + 1 + end +end + +local function remove_hook(dispatcher) + assert(raw_dispatcher, "Not in debug mode") + replace_upvalue(dispatcher, HOOK_FUNC, raw_dispatcher) + raw_dispatcher = nil + print = _G.print + + skynet.error "Leave debug mode" +end + +local function gen_print(fd) + -- redirect print to socket fd + return function(...) + local tmp = table.pack(...) + for i=1,tmp.n do + tmp[i] = tostring(tmp[i]) + end + table.insert(tmp, "\n") + socketdriver.send(fd, table.concat(tmp, "\t")) + end +end + +local function run_exp(ok, ...) + if ok then + print(...) + end + return ok +end + +local function run_cmd(cmd, env, co, level) + if not run_exp(injectrun("return "..cmd, co, level, env)) then + print(select(2, injectrun(cmd,co, level,env))) + end +end + +local ctx_skynet = debug.getinfo(skynet.start,"S").short_src -- skip when enter this source file +local ctx_term = debug.getinfo(run_cmd, "S").short_src -- term when get here +local ctx_active = {} + +local linehook +local function skip_hook(mode) + local co = coroutine.running() + local ctx = ctx_active[co] + if mode == "return" then + ctx.level = ctx.level - 1 + if ctx.level == 0 then + ctx.needupdate = true + sethook(linehook, "crl") + end + else + ctx.level = ctx.level + 1 + end +end + +function linehook(mode, line) + local co = coroutine.running() + local ctx = ctx_active[co] + if mode ~= "line" then + ctx.needupdate = true + if mode ~= "return" then + if ctx.next_mode or debug.getinfo(2,"S").short_src == ctx_skynet then + ctx.level = 1 + sethook(skip_hook, "cr") + end + end + else + if ctx.needupdate then + ctx.needupdate = false + ctx.filename = debug.getinfo(2, "S").short_src + if ctx.filename == ctx_term then + ctx_active[co] = nil + sethook() + change_prompt(string.format(":%08x>", skynet.self())) + return + end + end + change_prompt(string.format("%s(%d)>",ctx.filename, line)) + return true -- yield + end +end + +local function add_watch_hook() + local co = coroutine.running() + local ctx = {} + ctx_active[co] = ctx + local level = 1 + sethook(function(mode) + if mode == "return" then + level = level - 1 + else + level = level + 1 + if level == 0 then + ctx.needupdate = true + sethook(linehook, "crl") + end + end + end, "cr") +end + +local function watch_proto(protoname, cond) + local proto = assert(replace_upvalue(skynet.register_protocol, "proto"), "Can't find proto table") + local p = proto[protoname] + if p == nil then + return "No " .. protoname + end + local dispatch = p.dispatch_origin or p.dispatch + if dispatch == nil then + return "No dispatch" + end + p.dispatch_origin = dispatch + p.dispatch = function(...) + if not cond or cond(...) then + p.dispatch = dispatch -- restore origin dispatch function + add_watch_hook() + end + dispatch(...) + end +end + +local function remove_watch() + for co in pairs(ctx_active) do + sethook(co) + end + ctx_active = {} +end + +local dbgcmd = {} + +function dbgcmd.s(co) + local ctx = ctx_active[co] + ctx.next_mode = false + skynet_suspend(co, coroutine.resume(co)) +end + +function dbgcmd.n(co) + local ctx = ctx_active[co] + ctx.next_mode = true + skynet_suspend(co, coroutine.resume(co)) +end + +function dbgcmd.c(co) + sethook(co) + ctx_active[co] = nil + change_prompt(string.format(":%08x>", skynet.self())) + skynet_suspend(co, coroutine.resume(co)) +end + +local function hook_dispatch(dispatcher, resp, fd, channel) + change_prompt(string.format(":%08x>", skynet.self())) + + print = gen_print(fd) + local env = { + print = print, + watch = watch_proto + } + + local watch_env = { + print = print + } + + local function watch_cmd(cmd) + local co = next(ctx_active) + watch_env._CO = co + if dbgcmd[cmd] then + dbgcmd[cmd](co) + else + run_cmd(cmd, watch_env, co, 0) + end + end + + local function debug_hook() + while true do + if newline then + socketdriver.send(fd, prompt) + newline = false + end + local cmd = channel:read() + if cmd then + if cmd == "cont" then + -- leave debug mode + break + end + if cmd ~= "" then + if next(ctx_active) then + watch_cmd(cmd) + else + run_cmd(cmd, env, coroutine.running(),2) + end + end + newline = true + else + -- no input + return + end + end + -- exit debug mode + remove_watch() + remove_hook(dispatcher) + resp(true) + end + + local func + local function hook(...) + debug_hook() + return func(...) + end + func = replace_upvalue(dispatcher, HOOK_FUNC, hook) + if func then + local function idle() + if raw_dispatcher then + skynet.timeout(10,idle) -- idle every 0.1s + end + end + skynet.timeout(0, idle) + end + return func +end + +function M.start(import, fd, handle) + local dispatcher = import.dispatch + skynet_suspend = import.suspend + assert(raw_dispatcher == nil, "Already in debug mode") + skynet.error "Enter debug mode" + local channel = debugchannel.connect(handle) + raw_dispatcher = hook_dispatch(dispatcher, skynet.response(), fd, channel) +end + +return M diff --git a/lualib/skynet/service.lua b/lualib/skynet/service.lua new file mode 100644 index 00000000..32d665cf --- /dev/null +++ b/lualib/skynet/service.lua @@ -0,0 +1,43 @@ +local skynet = require "skynet" + +local service = {} +local cache = {} +local provider + +local function get_provider() + provider = provider or skynet.uniqueservice "service_provider" + return provider +end + +local function check(func) + local info = debug.getinfo(func, "u") + assert(info.nups == 1) + assert(debug.getupvalue(func,1) == "_ENV") +end + +function service.new(name, mainfunc, ...) + local p = get_provider() + local addr, booting = skynet.call(p, "lua", "test", name) + if addr then + service.address = addr + else + if booting then + service.address = skynet.call(p, "lua", "query", name) + else + check(mainfunc) + local code = string.dump(mainfunc) + service.address = skynet.call(p, "lua", "launch", name, code, ...) + end + end + cache[name] = service.address + return service.address +end + +function service.query(name) + if not cache[name] then + cache[name] = skynet.call(get_provider(), "lua", "query", name) + end + return cache[name] +end + +return service diff --git a/lualib/skynet/sharedata.lua b/lualib/skynet/sharedata.lua new file mode 100644 index 00000000..b955bd90 --- /dev/null +++ b/lualib/skynet/sharedata.lua @@ -0,0 +1,70 @@ +local skynet = require "skynet" +local sd = require "skynet.sharedata.corelib" + +local service + +skynet.init(function() + service = skynet.uniqueservice "sharedatad" +end) + +local sharedata = {} +local cache = setmetatable({}, { __mode = "kv" }) + +local function monitor(name, obj, cobj) + local newobj = cobj + while true do + newobj = skynet.call(service, "lua", "monitor", name, newobj) + if newobj == nil then + break + end + sd.update(obj, newobj) + end + if cache[name] == obj then + cache[name] = nil + end +end + +function sharedata.query(name) + if cache[name] then + return cache[name] + end + local obj = skynet.call(service, "lua", "query", name) + local r = sd.box(obj) + skynet.send(service, "lua", "confirm" , obj) + skynet.fork(monitor,name, r, obj) + cache[name] = r + return r +end + +function sharedata.new(name, v, ...) + skynet.call(service, "lua", "new", name, v, ...) +end + +function sharedata.update(name, v, ...) + skynet.call(service, "lua", "update", name, v, ...) +end + +function sharedata.delete(name) + skynet.call(service, "lua", "delete", name) +end + +function sharedata.flush() + for name, obj in pairs(cache) do + sd.flush(obj) + end + collectgarbage() +end + +function sharedata.deepcopy(name, ...) + if cache[name] then + local cobj = cache[name].__obj + return sd.copy(cobj, ...) + end + + local cobj = skynet.call(service, "lua", "query", name) + local ret = sd.copy(cobj, ...) + skynet.send(service, "lua", "confirm" , cobj) + return ret +end + +return sharedata diff --git a/lualib/skynet/sharedata/corelib.lua b/lualib/skynet/sharedata/corelib.lua new file mode 100644 index 00000000..cc49c2d1 --- /dev/null +++ b/lualib/skynet/sharedata/corelib.lua @@ -0,0 +1,181 @@ +local core = require "skynet.sharedata.core" +local type = type +local rawset = rawset + +local conf = {} + +conf.host = { + new = core.new, + delete = core.delete, + getref = core.getref, + markdirty = core.markdirty, + incref = core.incref, + decref = core.decref, +} + +local meta = {} + +local isdirty = core.isdirty +local index = core.index +local needupdate = core.needupdate +local len = core.len +local core_nextkey = core.nextkey + +local function findroot(self) + while self.__parent do + self = self.__parent + end + return self +end + +local function update(root, cobj, gcobj) + root.__obj = cobj + root.__gcobj = gcobj + local children = root.__cache + if children then + for k,v in pairs(children) do + local pointer = index(cobj, k) + if type(pointer) == "userdata" then + update(v, pointer, gcobj) + else + children[k] = nil + end + end + end +end + +local function genkey(self) + local key = tostring(self.__key) + while self.__parent do + self = self.__parent + key = self.__key .. "." .. key + end + return key +end + +local function getcobj(self) + local obj = self.__obj + if isdirty(obj) then + local newobj, newtbl = needupdate(self.__gcobj) + if newobj then + local newgcobj = newtbl.__gcobj + local root = findroot(self) + update(root, newobj, newgcobj) + if obj == self.__obj then + error ("The key [" .. genkey(self) .. "] doesn't exist after update") + end + obj = self.__obj + end + end + return obj +end + +function meta:__newindex(key, value) + error ("Error newindex, the key [" .. genkey(self) .. "]") +end + +function meta:__index(key) + local obj = getcobj(self) + local v = index(obj, key) + if type(v) == "userdata" then + local children = self.__cache + if children == nil then + children = {} + rawset(self, "__cache", children) + end + local r = children[key] + if r then + return r + end + r = setmetatable({ + __obj = v, + __gcobj = self.__gcobj, + __parent = self, + __key = key, + }, meta) + children[key] = r + return r + else + return v + end +end + +function meta:__len() + return len(getcobj(self)) +end + +function meta:__pairs() + return conf.next, self, nil +end + +function conf.next(obj, key) + local cobj = getcobj(obj) + local nextkey = core_nextkey(cobj, key) + if nextkey then + return nextkey, obj[nextkey] + end +end + +function conf.box(obj) + local gcobj = core.box(obj) + return setmetatable({ + __parent = false, + __obj = obj, + __gcobj = gcobj, + __key = "", + } , meta) +end + +function conf.update(self, pointer) + local cobj = self.__obj + assert(isdirty(cobj), "Only dirty object can be update") + core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) +end + +function conf.flush(obj) + getcobj(obj) +end + +local function clone_table(cobj) + local obj = {} + local key + while true do + key = core_nextkey(cobj, key) + if key == nil then + break + end + local v = index(cobj, key) + if type(v) == "userdata" then + v = clone_table(v) + end + obj[key] = v + end + return obj +end + +local function find_node(cobj, key, ...) + if key == nil then + return cobj + end + local cobj = index(cobj, key) + if cobj == nil then + return nil + end + if type(cobj) == "userdata" then + return find_node(cobj, ...) + end + return cobj +end + +function conf.copy(cobj, ...) + cobj = find_node(cobj, ...) + if cobj then + if type(cobj) == "userdata" then + return clone_table(cobj) + else + return cobj + end + end +end + +return conf diff --git a/lualib/skynet/sharemap.lua b/lualib/skynet/sharemap.lua new file mode 100644 index 00000000..6d6d16b3 --- /dev/null +++ b/lualib/skynet/sharemap.lua @@ -0,0 +1,71 @@ +local stm = require "skynet.stm" +local sprotoloader = require "sprotoloader" +local sproto = require "sproto" +local setmetatable = setmetatable + +local sharemap = {} + +function sharemap.register(protofile) + -- use global slot 0 for type define + sprotoloader.register(protofile, 0) +end + +local sprotoobj +local function loadsp() + if sprotoobj == nil then + sprotoobj = sprotoloader.load(0) + end + return sprotoobj +end + +function sharemap:commit() + self.__obj(sprotoobj:encode(self.__typename, self.__data)) +end + +function sharemap:copy() + return stm.copy(self.__obj) +end + +function sharemap.writer(typename, obj) + local sp = loadsp() + obj = obj or {} + local stmobj = stm.new(sp:encode(typename,obj)) + local ret = { + __typename = typename, + __obj = stmobj, + __data = obj, + commit = sharemap.commit, + copy = sharemap.copy, + } + return setmetatable(ret, { __index = obj, __newindex = obj }) +end + +local function decode(msg, sz, self) + local data = self.__data + for k in pairs(data) do + data[k] = nil + end + return sprotoobj:decode(self.__typename, msg, sz, data) +end + +function sharemap:update() + return self.__obj(decode, self) +end + +function sharemap.reader(typename, stmcpy) + local sp = loadsp() + local stmobj = stm.newcopy(stmcpy) + local _, data = stmobj(function(msg, sz) + return sp:decode(typename, msg, sz) + end) + + local obj = { + __typename = typename, + __obj = stmobj, + __data = data, + update = sharemap.update, + } + return setmetatable(obj, { __index = data, __newindex = error }) +end + +return sharemap diff --git a/lualib/snax.lua b/lualib/skynet/snax.lua similarity index 80% rename from lualib/snax.lua rename to lualib/skynet/snax.lua index f810e392..c21272b2 100644 --- a/lualib/snax.lua +++ b/lualib/skynet/snax.lua @@ -4,7 +4,9 @@ local snax_interface = require "snax.interface" local snax = {} local typeclass = {} -local G = { require = function() end } +local interface_g = skynet.getenv("snax_interface_g") +local G = interface_g and require (interface_g) or { require = function() end } +interface_g = nil skynet.register_protocol { name = "snax", @@ -22,6 +24,7 @@ function snax.interface(name) local si = snax_interface(name, G) local ret = { + name = name, accept = {}, response = {}, system = {}, @@ -44,7 +47,10 @@ local skynet_call = skynet.call local function gen_post(type, handle) return setmetatable({} , { __index = function( t, k ) - local id = assert(type.accept[k] , string.format("post %s no exist", k)) + local id = type.accept[k] + if not id then + error(string.format("post %s:%s no exist", type.name, k)) + end return function(...) skynet_send(handle, "snax", id, ...) end @@ -54,7 +60,10 @@ end local function gen_req(type, handle) return setmetatable({} , { __index = function( t, k ) - local id = assert(type.response[k] , string.format("request %s no exist", k)) + local id = type.response[k] + if not id then + error(string.format("request %s:%s no exist", type.name, k)) + end return function(...) return skynet_call(handle, "snax", id, ...) end @@ -132,6 +141,14 @@ function snax.kill(obj, ...) skynet_call(obj.handle, "snax", t.system.exit, ...) end +function snax.self() + return snax.bind(skynet.self(), SERVICE_NAME) +end + +function snax.exit(...) + snax.kill(snax.self(), ...) +end + local function test_result(ok, ...) if ok then return ... @@ -145,4 +162,13 @@ function snax.hotfix(obj, source, ...) return test_result(skynet_call(obj.handle, "snax", t.system.hotfix, source, ...)) end +function snax.printf(fmt, ...) + skynet.error(string.format(fmt, ...)) +end + +function snax.profile_info(obj) + local t = snax.interface(obj.type) + return skynet_call(obj.handle, "snax", t.system.profile) +end + return snax diff --git a/lualib/socket.lua b/lualib/skynet/socket.lua similarity index 61% rename from lualib/socket.lua rename to lualib/skynet/socket.lua index 93b9bf4c..26524d90 100644 --- a/lualib/socket.lua +++ b/lualib/skynet/socket.lua @@ -1,5 +1,6 @@ -local driver = require "socketdriver" +local driver = require "skynet.socketdriver" local skynet = require "skynet" +local skynet_core = require "skynet.core" local assert = assert local socket = {} -- api @@ -29,7 +30,7 @@ end local function suspend(s) assert(not s.co) s.co = coroutine.running() - skynet.wait() + skynet.wait(s.co) -- wakeup closing corouting every time suspend, -- because socket.close() will wait last socket buffer operation before clear the buffer. if s.closing then @@ -56,11 +57,19 @@ socket_message[1] = function(id, size, data) s.read_required = nil wakeup(s) end - elseif rrt == "string" then - -- read line - if driver.readline(s.buffer,nil,rr) then - s.read_required = nil - wakeup(s) + else + if s.buffer_limit and sz > s.buffer_limit then + skynet.error(string.format("socket buffer overflow: fd=%d size=%d", id , sz)) + driver.clear(s.buffer,buffer_pool) + driver.close(id) + return + end + if rrt == "string" then + -- read line + if driver.readline(s.buffer,nil,rr) then + s.read_required = nil + wakeup(s) + end end end end @@ -97,26 +106,59 @@ socket_message[4] = function(id, newid, addr) end -- SKYNET_SOCKET_TYPE_ERROR = 5 -socket_message[5] = function(id) +socket_message[5] = function(id, _, err) local s = socket_pool[id] if s == nil then - skynet.error("socket: error on unknown", id) + skynet.error("socket: error on unknown", id, err) return end if s.connected then - skynet.error("socket: error on", id) + skynet.error("socket: error on", id, err) + elseif s.connecting then + s.connecting = err end s.connected = false + driver.shutdown(id) wakeup(s) end +-- SKYNET_SOCKET_TYPE_UDP = 6 +socket_message[6] = function(id, size, data, address) + local s = socket_pool[id] + if s == nil or s.callback == nil then + skynet.error("socket: drop udp package from " .. id) + driver.drop(data, size) + return + end + local str = skynet.tostring(data, size) + skynet_core.trash(data, size) + s.callback(str, address) +end + +local function default_warning(id, size) + local s = socket_pool[id] + if not s then + return + end + skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id)) +end + +-- SKYNET_SOCKET_TYPE_WARNING +socket_message[7] = function(id, size) + local s = socket_pool[id] + if s then + local warning = s.on_warning or default_warning + warning(id, size) + end +end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = driver.unpack, - dispatch = function (_, _, t, n1, n2, data) - socket_message[t](n1,n2,data) + dispatch = function (_, _, t, ...) + socket_message[t](...) end } @@ -129,14 +171,22 @@ local function connect(id, func) id = id, buffer = newbuffer, connected = false, - read_require = false, + connecting = true, + read_required = false, co = false, callback = func, + protocol = "TCP", } + assert(not socket_pool[id], "socket is not closed") socket_pool[id] = s suspend(s) + local err = s.connecting + s.connecting = nil if s.connected then return id + else + socket_pool[id] = nil + return nil, err end end @@ -159,46 +209,76 @@ function socket.start(id, func) return connect(id, func) end -function socket.shutdown(id) +local function close_fd(id, func) local s = socket_pool[id] if s then if s.buffer then driver.clear(s.buffer,buffer_pool) end if s.connected then - driver.close(id) + func(id) end end end +function socket.shutdown(id) + close_fd(id, driver.shutdown) +end + +function socket.close_fd(id) + assert(socket_pool[id] == nil,"Use socket.close instead") + driver.close(id) +end + function socket.close(id) local s = socket_pool[id] if s == nil then return end if s.connected then - driver.close(s.id) + driver.close(id) -- notice: call socket.close in __gc should be carefully, -- because skynet.wait never return in __gc, so driver.clear may not be called if s.co then - -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediatel + -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. assert(not s.closing) s.closing = coroutine.running() - skynet.wait() + skynet.wait(s.closing) else suspend(s) end s.connected = false end - socket.shutdown(id) - assert(s.lock_set == nil or next(s.lock_set) == nil) + close_fd(id) -- clear the buffer (already close fd) + assert(s.lock == nil or next(s.lock) == nil) socket_pool[id] = nil end function socket.read(id, sz) local s = socket_pool[id] assert(s) + if sz == nil then + -- read some bytes + local ret = driver.readall(s.buffer, buffer_pool) + if ret ~= "" then + return ret + end + + if not s.connected then + return false, ret + end + assert(not s.read_required) + s.read_required = 0 + suspend(s) + ret = driver.readall(s.buffer, buffer_pool) + if ret ~= "" then + return ret + else + return false, ret + end + end + local ret = driver.pop(s.buffer, buffer_pool, sz) if ret then return ret @@ -266,12 +346,19 @@ end socket.write = assert(driver.send) socket.lwrite = assert(driver.lsend) +socket.header = assert(driver.header) function socket.invalid(id) return socket_pool[id] == nil end -socket.listen = assert(driver.listen) +function socket.listen(host, port, backlog) + if port == nil then + host, port = string.match(host, "([^:]+):(.+)$") + port = tonumber(port) + end + return driver.listen(host, port, backlog) +end function socket.lock(id) local s = socket_pool[id] @@ -286,7 +373,7 @@ function socket.lock(id) else local co = coroutine.running() table.insert(lock_set, co) - skynet.wait() + skynet.wait(co) end end @@ -311,4 +398,49 @@ function socket.abandon(id) socket_pool[id] = nil end +function socket.limit(id, limit) + local s = assert(socket_pool[id]) + s.buffer_limit = limit +end + +---------------------- UDP + +local function create_udp_object(id, cb) + assert(not socket_pool[id], "socket is not closed") + socket_pool[id] = { + id = id, + connected = true, + protocol = "UDP", + callback = cb, + } +end + +function socket.udp(callback, host, port) + local id = driver.udp(host, port) + create_udp_object(id, callback) + return id +end + +function socket.udp_connect(id, addr, port, callback) + local obj = socket_pool[id] + if obj then + assert(obj.protocol == "UDP") + if callback then + obj.callback = callback + end + else + create_udp_object(id, callback) + end + driver.udp_connect(id, addr, port) +end + +socket.sendto = assert(driver.udp_send) +socket.udp_address = assert(driver.udp_address) + +function socket.warning(id, callback) + local obj = socket_pool[id] + assert(obj) + obj.on_warning = callback +end + return socket diff --git a/lualib/socketchannel.lua b/lualib/skynet/socketchannel.lua similarity index 50% rename from lualib/socketchannel.lua rename to lualib/skynet/socketchannel.lua index 32615ac0..a6474a81 100644 --- a/lualib/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" +local socketdriver = require "skynet.socketdriver" -- channel support auto reconnect , and capture socket error in request/response transaction -- { host = "", port = , auth = function(so) , response = function(so) session, data } @@ -26,6 +27,7 @@ function socket_channel.channel(desc) local c = { __host = assert(desc.host), __port = assert(desc.port), + __backup = desc.backup, __auth = desc.auth, __response = desc.response, -- It's for session mode __request = {}, -- request seq { response func or session } -- It's for order mode @@ -36,6 +38,7 @@ function socket_channel.channel(desc) __sock = false, __closed = false, __authcoroutine = false, + __nodelay = desc.nodelay, } return setmetatable(c, channel_meta) @@ -65,28 +68,51 @@ local function wakeup_all(self, errmsg) for i = 1, #self.__thread do local co = self.__thread[i] self.__thread[i] = nil - self.__result[co] = socket_error - self.__result_data[co] = errmsg - skynet.wakeup(co) + if co then -- ignore the close signal + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) + end end end end - +local function exit_thread(self) + local co = coroutine.running() + if self.__dispatch_thread == co then + self.__dispatch_thread = nil + 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 while self.__sock do - local ok , session, result_ok, result_data = pcall(response, self.__sock) + local ok , session, result_ok, result_data, padding = pcall(response, self.__sock) if ok and session then local co = self.__thread[session] - self.__thread[session] = nil if co then - self.__result[co] = result_ok - self.__result_data[co] = result_data - skynet.wakeup(co) + if padding and result_ok then + -- If padding is true, append result_data to a table (self.__result_data[co]) + local result = self.__result_data[co] or {} + self.__result_data[co] = result + table.insert(result, result_data) + else + self.__thread[session] = nil + self.__result[co] = result_ok + if result_ok and self.__result_data[co] then + table.insert(self.__result_data[co], result_data) + else + self.__result_data[co] = result_data + end + skynet.wakeup(co) + end else + self.__thread[session] = nil skynet.error("socket: unknown session :", session) end else @@ -98,10 +124,18 @@ local function dispatch_by_session(self) wakeup_all(self, errormsg) end end + exit_thread(self) end local function pop_response(self) - return table.remove(self.__request, 1), table.remove(self.__thread, 1) + while true do + local func,co = table.remove(self.__request, 1), table.remove(self.__thread, 1) + if func then + return func, co + end + self.__wait_response = coroutine.running() + skynet.wait(self.__wait_response) + end end local function push_response(self, response, co) @@ -112,33 +146,51 @@ 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 end end local function dispatch_by_order(self) while self.__sock do local func, co = pop_response(self) - if func == nil then - if not socket.block(self.__sock[1]) then - close_channel_socket(self) - wakeup_all(self) + if not co then + -- close signal + wakeup_all(self, "channel_closed") + break + end + local ok, result_ok, result_data, padding = pcall(func, self.__sock) + if ok then + if padding and result_ok then + -- if padding is true, wait for next result_data + -- self.__result_data[co] is a table + local result = self.__result_data[co] or {} + self.__result_data[co] = result + table.insert(result, result_data) + else + self.__result[co] = result_ok + if result_ok and self.__result_data[co] then + table.insert(self.__result_data[co], result_data) + else + self.__result_data[co] = result_data + end + skynet.wakeup(co) end else - local ok, result_ok, result_data = pcall(func, self.__sock) - if ok then - self.__result[co] = result_ok - self.__result_data[co] = result_data - skynet.wakeup(co) - else - close_channel_socket(self) - local errmsg - if result ~= socket_error then - errmsg = result_ok - end - wakeup_all(self, errmsg) + close_channel_socket(self) + local errmsg + if result_ok ~= socket_error then + errmsg = result_ok end + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) + wakeup_all(self, errmsg) end end + exit_thread(self) end local function dispatch_function(self) @@ -149,42 +201,80 @@ 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 connect_once(self) - assert(not self.__sock and not self.__authcoroutine) - local fd = socket.open(self.__host, self.__port) - if not fd then + if self.__closed then return false end - self.__authcoroutine = coroutine.running() + assert(not self.__sock and not self.__authcoroutine) + local fd,err = socket.open(self.__host, self.__port) + if not fd then + fd = connect_backup(self) + if not fd then + return false, err + end + end + if self.__nodelay then + socketdriver.nodelay(fd) + end + self.__sock = setmetatable( {fd} , channel_socket_meta ) - skynet.fork(dispatch_function(self), self) + self.__dispatch_thread = skynet.fork(dispatch_function(self), self) if self.__auth then + self.__authcoroutine = coroutine.running() 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 and not self.__sock then + -- auth may change host, so connect again + return connect_once(self) + end return ok end - self.__authcoroutine = false return true end local function try_connect(self , once) - local t = 100 + local t = 0 while not self.__closed do - if connect_once(self) then + local ok, err = connect_once(self) + if ok then if not once then skynet.error("socket: connect to", self.__host, self.__port) end return elseif once then - error(string.format("Connect to %s:%d failed", self.__host, self.__port)) + return err + else + skynet.error("socket: connect", err) end if t > 1000 then skynet.error("socket: try to reconnect", self.__host, self.__port) @@ -197,7 +287,7 @@ local function try_connect(self , once) end end -local function block_connect(self, once) +local function check_connection(self) if self.__sock then local authco = self.__authcoroutine if not authco then @@ -211,30 +301,50 @@ local function block_connect(self, once) if self.__closed then return false end +end + +local function block_connect(self, once) + local r = check_connection(self) + if r ~= nil then + return r + end + local err if #self.__connecting > 0 then -- connecting in other coroutine local co = coroutine.running() table.insert(self.__connecting, co) - skynet.wait() - -- check connection again - return block_connect(self, once) - end - self.__connecting[1] = true - try_connect(self, once) - self.__connecting[1] = nil - for i=2, #self.__connecting do - local co = self.__connecting[i] - self.__connecting[i] = nil - skynet.wakeup(co) + skynet.wait(co) + 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 end - -- check again - return block_connect(self, once) + r = check_connection(self) + if r == nil then + skynet.error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err)) + error(socket_error) + else + return r + end 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 @@ -244,7 +354,7 @@ end local function wait_for_response(self, response) local co = coroutine.running() push_response(self, response, co) - skynet.wait() + skynet.wait(co) local result = self.__result[co] self.__result[co] = nil @@ -252,20 +362,46 @@ local function wait_for_response(self, response) self.__result_data[co] = nil if result == socket_error then - error(socket_error) + if result_data then + error(result_data) + else + error(socket_error) + end else assert(result, result_data) return result_data end end -function channel:request(request, response) - assert(block_connect(self)) +local socket_write = socket.write +local socket_lwrite = socket.lwrite - if not socket.write(self.__sock[1], request) then - close_channel_socket(self) - wakeup_all(self) - error(socket_error) +local function sock_err(self) + close_channel_socket(self) + wakeup_all(self) + error(socket_error) +end + +function channel:request(request, response, padding) + assert(block_connect(self, true)) -- connect once + local fd = self.__sock[1] + + if padding then + -- padding may be a table, to support multi part request + -- multi part request use low priority socket write + -- now socket_lwrite returns as socket_write + if not socket_lwrite(fd , request) then + sock_err(self) + end + for _,v in ipairs(padding) do + if not socket_lwrite(fd, v) then + sock_err(self) + end + end + else + if not socket_write(fd , request) then + sock_err(self) + end end if response == nil then @@ -284,11 +420,30 @@ end function channel:close() if not self.__closed then + local thread = self.__dispatch_thread 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 +function channel:changehost(host, port) + self.__host = host + if port then + self.__port = port + end + if not self.__closed then + close_channel_socket(self) + end +end + +function channel:changebackup(backup) + self.__backup = backup +end + channel_meta.__gc = channel.close local function wrapper_socket_function(f) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua new file mode 100644 index 00000000..61e7c97e --- /dev/null +++ b/lualib/snax/gateserver.lua @@ -0,0 +1,157 @@ +local skynet = require "skynet" +local netpack = require "skynet.netpack" +local socketdriver = require "skynet.socketdriver" + +local gateserver = {} + +local socket -- listen socket +local queue -- message queue +local maxclient -- max client +local client_number = 0 +local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) +local nodelay = false + +local connection = {} + +function gateserver.openclient(fd) + if connection[fd] then + socketdriver.start(fd) + end +end + +function gateserver.closeclient(fd) + local c = connection[fd] + if c then + connection[fd] = false + socketdriver.close(fd) + end +end + +function gateserver.start(handler) + assert(handler.message) + assert(handler.connect) + + function CMD.open( source, conf ) + assert(not socket) + local address = conf.address or "0.0.0.0" + local port = assert(conf.port) + maxclient = conf.maxclient or 1024 + nodelay = conf.nodelay + skynet.error(string.format("Listen on %s:%d", address, port)) + socket = socketdriver.listen(address, port) + socketdriver.start(socket) + if handler.open then + return handler.open(source, conf) + end + end + + function CMD.close() + assert(socket) + socketdriver.close(socket) + end + + local MSG = {} + + local function dispatch_msg(fd, msg, sz) + if connection[fd] then + handler.message(fd, msg, sz) + else + skynet.error(string.format("Drop message from fd (%d) : %s", fd, netpack.tostring(msg,sz))) + end + end + + MSG.data = dispatch_msg + + local function dispatch_queue() + local fd, msg, sz = netpack.pop(queue) + if fd then + -- may dispatch even the handler.message blocked + -- If the handler.message never block, the queue should be empty, so only fork once and then exit. + skynet.fork(dispatch_queue) + dispatch_msg(fd, msg, sz) + + for fd, msg, sz in netpack.pop, queue do + dispatch_msg(fd, msg, sz) + end + end + end + + MSG.more = dispatch_queue + + function MSG.open(fd, msg) + if client_number >= maxclient then + socketdriver.close(fd) + return + end + if nodelay then + socketdriver.nodelay(fd) + end + connection[fd] = true + client_number = client_number + 1 + handler.connect(fd, msg) + end + + local function close_fd(fd) + local c = connection[fd] + if c ~= nil then + connection[fd] = nil + client_number = client_number - 1 + end + end + + function MSG.close(fd) + if fd ~= socket then + if handler.disconnect then + handler.disconnect(fd) + end + close_fd(fd) + else + socket = nil + end + end + + function MSG.error(fd, msg) + if fd == socket then + socketdriver.close(fd) + skynet.error("gateserver close listen socket, accpet error:",msg) + else + if handler.error then + handler.error(fd, msg) + end + close_fd(fd) + end + end + + function MSG.warning(fd, size) + if handler.warning then + handler.warning(fd, size) + end + end + + skynet.register_protocol { + name = "socket", + id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 + unpack = function ( msg, sz ) + return netpack.filter( queue, msg, sz) + end, + dispatch = function (_, _, q, type, ...) + queue = q + if type then + MSG[type](...) + end + end + } + + skynet.start(function() + skynet.dispatch("lua", function (_, address, cmd, ...) + local f = CMD[cmd] + if f then + skynet.ret(skynet.pack(f(address, ...))) + else + skynet.ret(skynet.pack(handler.command(cmd, address, ...))) + end + end) + end) +end + +return gateserver diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index e0cc2a7f..a73d680e 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -1,9 +1,20 @@ local si = require "snax.interface" -local io = io -local hotfix = {} +local function envid(f) + local i = 1 + while true do + local name, value = debug.getupvalue(f, i) + if name == nil then + return + end + if name == "_ENV" then + return debug.upvalueid(f, i) + end + i = i + 1 + end +end -local function collect_uv(f , uv) +local function collect_uv(f , uv, env) local i = 1 while true do local name, value = debug.getupvalue(f, i) @@ -18,7 +29,9 @@ local function collect_uv(f , uv) uv[name] = { func = f, index = i, id = id } if type(value) == "function" then - collect_uv(value, uv) + if envid(value) == env then + collect_uv(value, uv, env) + end end end @@ -30,16 +43,18 @@ local function collect_all_uv(funcs) local global = {} for _, v in pairs(funcs) do if v[4] then - collect_uv(v[4], global) + collect_uv(v[4], global, envid(v[4])) end end - + if not global["_ENV"] then + global["_ENV"] = {func = collect_uv, index = 1} + end return global end local function loader(source) - return function (filename, ...) - return load(source, "=patch", ...) + return function (path, name, G) + return load(source, "=patch", "bt", G) end end @@ -53,10 +68,10 @@ local function find_func(funcs, group , name) end local dummy_env = {} +for k,v in pairs(_ENV) do dummy_env[k] = v end -local function patch_func(funcs, global, group, name, f) - local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) - local i = 1 +local function _patch(global, f) + local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then @@ -66,9 +81,18 @@ local function patch_func(funcs, global, group, name, f) if old_uv then debug.upvaluejoin(f, i, old_uv.func, old_uv.index) end + else + if type(value) == "function" then + _patch(global, value) + end end i = i + 1 end +end + +local function patch_func(funcs, global, group, name, f) + local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) + _patch(global, f) desc[4] = f end diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 24d52c08..07c1b728 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -1,8 +1,24 @@ local skynet = require "skynet" +local function dft_loader(path, name, G) + local errlist = {} + + for pat in string.gmatch(path,"[^;]+") do + local filename = string.gsub(pat, "?", name) + local f , err = loadfile(filename, "bt", G) + if f then + return f, pat + else + table.insert(errlist, err) + end + end + + error(table.concat(errlist, "\n")) +end + return function (name , G, loader) - loader = loader or loadfile - local mainfunc + loader = loader or dft_loader + local mainfunc local function func_id(id, group) local tmp = {} @@ -35,7 +51,7 @@ return function (name , G, loader) local env = setmetatable({} , { __index = temp_global }) local func = {} - local system = { "init", "exit", "hotfix" } + local system = { "init", "exit", "hotfix", "profile"} do for k, v in ipairs(system) do @@ -59,36 +75,19 @@ 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) + setmetatable(G, { __index = env , __newindex = init_system }) - - do - local path = skynet.getenv "snax" - - local errlist = {} - - for pat in string.gmatch(path,"[^;]+") do - local filename = string.gsub(pat, "?", name) - local f , err = loader(filename, "bt", G) - if f then - mainfunc = f - break - else - table.insert(errlist, err) - end - end - - if mainfunc == nil then - error(table.concat(errlist, "\n")) - end - end - - mainfunc() - + local ok, err = xpcall(mainfunc, debug.traceback) setmetatable(G, nil) + assert(ok,err) for k,v in pairs(temp_global) do G[k] = v end - return func + return func, pattern end diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua new file mode 100644 index 00000000..be88b97a --- /dev/null +++ b/lualib/snax/loginserver.lua @@ -0,0 +1,206 @@ +local skynet = require "skynet" +require "skynet.manager" +local socket = require "skynet.socket" +local crypt = require "skynet.crypt" +local table = table +local string = string +local assert = assert + +--[[ + +Protocol: + + line (\n) based text protocol + + 1. Server->Client : base64(8bytes random challenge) + 2. Client->Server : base64(8bytes handshake client key) + 3. Server: Gen a 8bytes handshake server key + 4. Server->Client : base64(DH-Exchange(server key)) + 5. Server/Client secret := DH-Secret(client key/server key) + 6. Client->Server : base64(HMAC(challenge, secret)) + 7. Client->Server : DES(secret, base64(token)) + 8. Server : call auth_handler(token) -> server, uid (A user defined method) + 9. Server : call login_handler(server, uid, secret) ->subid (A user defined method) + 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) + +Success: + 200 base64(subid) +]] + +local socket_error = {} +local function assert_socket(service, v, fd) + if v then + return v + else + skynet.error(string.format("%s failed: socket (fd = %d) closed", service, fd)) + error(socket_error) + end +end + +local function write(service, fd, text) + assert_socket(service, socket.write(fd, text), fd) +end + +local function launch_slave(auth_handler) + local function auth(fd, addr) + -- set socket buffer limit (8K) + -- If the attacker send large package, close the socket + socket.limit(fd, 8192) + + local challenge = crypt.randomkey() + write("auth", fd, crypt.base64encode(challenge).."\n") + + local handshake = assert_socket("auth", socket.readline(fd), fd) + local clientkey = crypt.base64decode(handshake) + if #clientkey ~= 8 then + error "Invalid client key" + end + local serverkey = crypt.randomkey() + write("auth", fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") + + local secret = crypt.dhsecret(clientkey, serverkey) + + local response = assert_socket("auth", socket.readline(fd), fd) + local hmac = crypt.hmac64(challenge, secret) + + if hmac ~= crypt.base64decode(response) then + write("auth", fd, "400 Bad Request\n") + error "challenge failed" + end + + local etoken = assert_socket("auth", socket.readline(fd),fd) + + local token = crypt.desdecode(secret, crypt.base64decode(etoken)) + + local ok, server, uid = pcall(auth_handler,token) + + return ok, server, uid, secret + end + + local function ret_pack(ok, err, ...) + if ok then + return skynet.pack(err, ...) + else + if err == socket_error then + return skynet.pack(nil, "socket error") + else + return skynet.pack(false, err) + end + end + end + + local function auth_fd(fd, addr) + skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) + socket.start(fd) -- may raise error here + local msg, len = ret_pack(pcall(auth, fd, addr)) + socket.abandon(fd) -- never raise error here + return msg, len + end + + skynet.dispatch("lua", function(_,_,...) + local ok, msg, len = pcall(auth_fd, ...) + if ok then + skynet.ret(msg,len) + else + skynet.ret(skynet.pack(false, msg)) + end + end) +end + +local user_login = {} + +local function accept(conf, s, fd, addr) + -- call slave auth + local ok, server, uid, secret = skynet.call(s, "lua", fd, addr) + -- slave will accept(start) fd, so we can write to fd later + + if not ok then + if ok ~= nil then + write("response 401", fd, "401 Unauthorized\n") + end + error(server) + end + + if not conf.multilogin then + if user_login[uid] then + write("response 406", fd, "406 Not Acceptable\n") + error(string.format("User %s is already login", uid)) + end + + user_login[uid] = true + end + + local ok, err = pcall(conf.login_handler, server, uid, secret) + -- unlock login + user_login[uid] = nil + + if ok then + err = err or "" + write("response 200",fd, "200 "..crypt.base64encode(err).."\n") + else + write("response 403",fd, "403 Forbidden\n") + error(err) + end +end + +local function launch_master(conf) + local instance = conf.instance or 8 + assert(instance > 0) + local host = conf.host or "0.0.0.0" + local port = assert(tonumber(conf.port)) + local slave = {} + local balance = 1 + + skynet.dispatch("lua", function(_,source,command, ...) + skynet.ret(skynet.pack(conf.command_handler(command, ...))) + end) + + for i=1,instance do + table.insert(slave, skynet.newservice(SERVICE_NAME)) + end + + skynet.error(string.format("login server listen at : %s %d", host, port)) + local id = socket.listen(host, port) + socket.start(id , function(fd, addr) + local s = slave[balance] + balance = balance + 1 + if balance > #slave then + balance = 1 + end + local ok, err = pcall(accept, conf, s, fd, addr) + if not ok then + if err ~= socket_error then + 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. + end) +end + +local function login(conf) + local name = "." .. (conf.name or "login") + skynet.start(function() + local loginmaster = skynet.localname(name) + if loginmaster then + local auth_handler = assert(conf.auth_handler) + launch_master = nil + conf = nil + launch_slave(auth_handler) + else + launch_slave = nil + conf.auth_handler = nil + assert(conf.login_handler) + assert(conf.command_handler) + skynet.register(name) + launch_master(conf) + end + end) +end + +return login diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua new file mode 100644 index 00000000..4c6f1724 --- /dev/null +++ b/lualib/snax/msgserver.lua @@ -0,0 +1,318 @@ +local skynet = require "skynet" +local gateserver = require "snax.gateserver" +local netpack = require "skynet.netpack" +local crypt = require "skynet.crypt" +local socketdriver = require "skynet.socketdriver" +local assert = assert +local b64encode = crypt.base64encode +local b64decode = crypt.base64decode + +--[[ + +Protocol: + + All the number type is big-endian + + Shakehands (The first package) + + Client -> Server : + + base64(uid)@base64(server)#base64(subid):index:base64(hmac) + + Server -> Client + + XXX ErrorCode + 404 User Not Found + 403 Index Expired + 401 Unauthorized + 400 Bad Request + 200 OK + + Req-Resp + + Client -> Server : Request + word size (Not include self) + string content (size-4) + dword session + + Server -> Client : Response + word size (Not include self) + string content (size-5) + byte ok (1 is ok, 0 is error) + dword session + +API: + server.userid(username) + return uid, subid, server + + server.username(uid, subid, server) + return username + + server.login(username, secret) + update user secret + + server.logout(username) + user logout + + server.ip(username) + return ip when connection establish, or nil + + server.start(conf) + start server + +Supported skynet command: + kick username (may used by loginserver) + login username secret (used by loginserver) + logout username (used by agent) + +Config for server.start: + conf.expired_number : the number of the response message cached after sending out (default is 128) + conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server) + conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent) + conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server) + conf.request_handler(username, session, msg) : the function when recv a new request. + conf.register_handler(servername) : call when gate open + conf.disconnect_handler(username) : call when a connection disconnect (afk) +]] + +local server = {} + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, +} + +local user_online = {} +local handshake = {} +local connection = {} + +function server.userid(username) + -- base64(uid)@base64(server)#base64(subid) + local uid, servername, subid = username:match "([^@]*)@([^#]*)#(.*)" + return b64decode(uid), b64decode(subid), b64decode(servername) +end + +function server.username(uid, subid, servername) + return string.format("%s@%s#%s", b64encode(uid), b64encode(servername), b64encode(tostring(subid))) +end + +function server.logout(username) + local u = user_online[username] + user_online[username] = nil + if u.fd then + gateserver.closeclient(u.fd) + connection[u.fd] = nil + end +end + +function server.login(username, secret) + assert(user_online[username] == nil) + user_online[username] = { + secret = secret, + version = 0, + index = 0, + username = username, + response = {}, -- response cache + } +end + +function server.ip(username) + local u = user_online[username] + if u and u.fd then + return u.ip + end +end + +function server.start(conf) + local expired_number = conf.expired_number or 128 + + local handler = {} + + local CMD = { + login = assert(conf.login_handler), + logout = assert(conf.logout_handler), + kick = assert(conf.kick_handler), + } + + function handler.command(cmd, source, ...) + local f = assert(CMD[cmd]) + return f(...) + end + + function handler.open(source, gateconf) + local servername = assert(gateconf.servername) + return conf.register_handler(servername) + end + + function handler.connect(fd, addr) + handshake[fd] = addr + gateserver.openclient(fd) + end + + function handler.disconnect(fd) + handshake[fd] = nil + local c = connection[fd] + if c then + c.fd = nil + connection[fd] = nil + if conf.disconnect_handler then + conf.disconnect_handler(c.username) + end + end + end + + handler.error = handler.disconnect + + -- atomic , no yield + local function do_auth(fd, message, addr) + local username, index, hmac = string.match(message, "([^:]*):([^:]*):([^:]*)") + local u = user_online[username] + if u == nil then + return "404 User Not Found" + end + local idx = assert(tonumber(index)) + hmac = b64decode(hmac) + + if idx <= u.version then + return "403 Index Expired" + end + + local text = string.format("%s:%s", username, index) + local v = crypt.hmac_hash(u.secret, text) -- equivalent to crypt.hmac64(crypt.hashkey(text), u.secret) + if v ~= hmac then + return "401 Unauthorized" + end + + u.version = idx + u.fd = fd + u.ip = addr + connection[fd] = u + end + + local function auth(fd, addr, msg, sz) + local message = netpack.tostring(msg, sz) + local ok, result = pcall(do_auth, fd, message, addr) + if not ok then + skynet.error(result) + result = "400 Bad Request" + end + + local close = result ~= nil + + if result == nil then + result = "200 OK" + end + + socketdriver.send(fd, netpack.pack(result)) + + if close then + gateserver.closeclient(fd) + end + end + + local request_handler = assert(conf.request_handler) + + -- u.response is a struct { return_fd , response, version, index } + local function retire_response(u) + if u.index >= expired_number * 2 then + local max = 0 + local response = u.response + for k,p in pairs(response) do + if p[1] == nil then + -- request complete, check expired + if p[4] < expired_number then + response[k] = nil + else + p[4] = p[4] - expired_number + if p[4] > max then + max = p[4] + end + end + end + end + u.index = max + 1 + end + end + + local function do_request(fd, message) + local u = assert(connection[fd], "invalid fd") + local session = string.unpack(">I4", message, -4) + message = message:sub(1,-5) + local p = u.response[session] + if p then + -- session can be reuse in the same connection + if p[3] == u.version then + local last = u.response[session] + u.response[session] = nil + p = nil + if last[2] == nil then + local error_msg = string.format("Conflict session %s", crypt.hexencode(session)) + skynet.error(error_msg) + error(error_msg) + end + end + end + + if p == nil then + p = { fd } + u.response[session] = p + local ok, result = pcall(conf.request_handler, u.username, message) + -- NOTICE: YIELD here, socket may close. + result = result or "" + if not ok then + skynet.error(result) + result = string.pack(">BI4", 0, session) + else + result = result .. string.pack(">BI4", 1, session) + end + + p[2] = string.pack(">s2",result) + p[3] = u.version + p[4] = u.index + else + -- update version/index, change return fd. + -- resend response. + p[1] = fd + p[3] = u.version + p[4] = u.index + if p[2] == nil then + -- already request, but response is not ready + return + end + end + u.index = u.index + 1 + -- the return fd is p[1] (fd may change by multi request) check connect + fd = p[1] + if connection[fd] then + socketdriver.send(fd, p[2]) + end + p[1] = nil + retire_response(u) + end + + local function request(fd, msg, sz) + local message = netpack.tostring(msg, sz) + local ok, err = pcall(do_request, fd, message) + -- not atomic, may yield + if not ok then + skynet.error(string.format("Invalid package %s : %s", err, message)) + if connection[fd] then + gateserver.closeclient(fd) + end + end + end + + function handler.message(fd, msg, sz) + local addr = handshake[fd] + if addr then + auth(fd,addr,msg,sz) + handshake[fd] = nil + else + request(fd, msg, sz) + end + end + + return gateserver.start(handler) +end + +return server diff --git a/lualib/sproto.lua b/lualib/sproto.lua new file mode 100644 index 00000000..757140ee --- /dev/null +++ b/lualib/sproto.lua @@ -0,0 +1,251 @@ +local core = require "sproto.core" +local assert = assert + +local sproto = {} +local host = {} + +local weak_mt = { __mode = "kv" } +local sproto_mt = { __index = sproto } +local sproto_nogc = { __index = sproto } +local host_mt = { __index = host } + +function sproto_mt:__gc() + core.deleteproto(self.__cobj) +end + +function sproto.new(bin) + local cobj = assert(core.newproto(bin)) + local self = { + __cobj = cobj, + __tcache = setmetatable( {} , weak_mt ), + __pcache = setmetatable( {} , weak_mt ), + } + return setmetatable(self, sproto_mt) +end + +function sproto.sharenew(cobj) + local self = { + __cobj = cobj, + __tcache = setmetatable( {} , weak_mt ), + __pcache = setmetatable( {} , weak_mt ), + } + return setmetatable(self, sproto_nogc) +end + +function sproto.parse(ptext) + local parser = require "sprotoparser" + local pbin = parser.parse(ptext) + return sproto.new(pbin) +end + +function sproto:host( packagename ) + packagename = packagename or "package" + local obj = { + __proto = self, + __package = assert(core.querytype(self.__cobj, packagename), "type package not found"), + __session = {}, + } + return setmetatable(obj, host_mt) +end + +local function querytype(self, typename) + local v = self.__tcache[typename] + if not v then + v = assert(core.querytype(self.__cobj, typename), "type not found") + self.__tcache[typename] = v + end + + return v +end + +function sproto:exist_type(typename) + local v = self.__tcache[typename] + if not v then + return core.querytype(self.__cobj, typename) ~= nil + else + return true + end +end + +function sproto:encode(typename, tbl) + local st = querytype(self, typename) + return core.encode(st, tbl) +end + +function sproto:decode(typename, ...) + local st = querytype(self, typename) + return core.decode(st, ...) +end + +function sproto:pencode(typename, tbl) + local st = querytype(self, typename) + return core.pack(core.encode(st, tbl)) +end + +function sproto:pdecode(typename, ...) + local st = querytype(self, typename) + return core.decode(st, core.unpack(...)) +end + +local function queryproto(self, pname) + local v = self.__pcache[pname] + if not v then + local tag, req, resp = core.protocol(self.__cobj, pname) + assert(tag, pname .. " not found") + if tonumber(pname) then + pname, tag = tag, pname + end + v = { + request = req, + response =resp, + name = pname, + tag = tag, + } + self.__pcache[pname] = v + self.__pcache[tag] = v + end + + return v +end + +function sproto:exist_proto(pname) + local v = self.__pcache[pname] + if not v then + return core.protocol(self.__cobj, pname) ~= nil + else + return true + end +end + +function sproto:request_encode(protoname, tbl) + local p = queryproto(self, protoname) + local request = p.request + if request then + return core.encode(request,tbl) , p.tag + else + return "" , p.tag + end +end + +function sproto:response_encode(protoname, tbl) + local p = queryproto(self, protoname) + local response = p.response + if response then + return core.encode(response,tbl) + else + return "" + end +end + +function sproto:request_decode(protoname, ...) + local p = queryproto(self, protoname) + local request = p.request + if request then + return core.decode(request,...) , p.name + else + return nil, p.name + end +end + +function sproto:response_decode(protoname, ...) + local p = queryproto(self, protoname) + local response = p.response + if response then + return core.decode(response,...) + end +end + +sproto.pack = core.pack +sproto.unpack = core.unpack + +function sproto:default(typename, type) + if type == nil then + return core.default(querytype(self, typename)) + else + local p = queryproto(self, typename) + if type == "REQUEST" then + if p.request then + return core.default(p.request) + end + elseif type == "RESPONSE" then + if p.response then + return core.default(p.response) + end + else + error "Invalid type" + end + end +end + +local header_tmp = {} + +local function gen_response(self, response, session) + return function(args, ud) + header_tmp.type = nil + header_tmp.session = session + header_tmp.ud = ud + local header = core.encode(self.__package, header_tmp) + if response then + local content = core.encode(response, args) + return core.pack(header .. content) + else + return core.pack(header) + end + end +end + +function host:dispatch(...) + local bin = core.unpack(...) + header_tmp.type = nil + header_tmp.session = nil + header_tmp.ud = nil + local header, size = core.decode(self.__package, bin, header_tmp) + local content = bin:sub(size + 1) + if header.type then + -- request + local proto = queryproto(self.__proto, header.type) + local result + if proto.request then + result = core.decode(proto.request, content) + end + if header_tmp.session then + return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session), header.ud + else + return "REQUEST", proto.name, result, nil, header.ud + end + else + -- response + local session = assert(header_tmp.session, "session not found") + local response = assert(self.__session[session], "Unknown session") + self.__session[session] = nil + if response == true then + return "RESPONSE", session, nil, header.ud + else + local result = core.decode(response, content) + return "RESPONSE", session, result, header.ud + end + end +end + +function host:attach(sp) + return function(name, args, session, ud) + local proto = queryproto(sp, name) + header_tmp.type = proto.tag + header_tmp.session = session + header_tmp.ud = ud + local header = core.encode(self.__package, header_tmp) + + if session then + self.__session[session] = proto.response or true + end + + if proto.request then + local content = core.encode(proto.request, args) + return core.pack(header .. content) + else + return core.pack(header) + end + end +end + +return sproto diff --git a/lualib/sprotoloader.lua b/lualib/sprotoloader.lua new file mode 100644 index 00000000..e88a9260 --- /dev/null +++ b/lualib/sprotoloader.lua @@ -0,0 +1,27 @@ +local parser = require "sprotoparser" +local core = require "sproto.core" +local sproto = require "sproto" + +local loader = {} + +function loader.register(filename, index) + local f = assert(io.open(filename), "Can't open sproto file") + local data = f:read "a" + f:close() + local sp = core.newproto(parser.parse(data)) + core.saveproto(sp, index) +end + +function loader.save(bin, index) + local sp = core.newproto(bin) + core.saveproto(sp, index) +end + +function loader.load(index) + local sp = core.loadproto(index) + -- no __gc in metatable + return sproto.sharenew(sp) +end + +return loader + diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua new file mode 100644 index 00000000..4c9a7d07 --- /dev/null +++ b/lualib/sprotoparser.lua @@ -0,0 +1,488 @@ +local lpeg = require "lpeg" +local table = require "table" + +local packbytes +local packvalue + +if _VERSION == "Lua 5.3" then + function packbytes(str) + return string.pack("=0 and id < 65536) + local a = id % 256 + local b = math.floor(id / 256) + return string.char(a) .. string.char(b) + end +end + +local P = lpeg.P +local S = lpeg.S +local R = lpeg.R +local C = lpeg.C +local Ct = lpeg.Ct +local Cg = lpeg.Cg +local Cc = lpeg.Cc +local V = lpeg.V + +local function count_lines(_,pos, parser_state) + if parser_state.pos < pos then + parser_state.line = parser_state.line + 1 + parser_state.pos = pos + end + return pos +end + +local exception = lpeg.Cmt( lpeg.Carg(1) , function ( _ , pos, parser_state) + error(string.format("syntax error at [%s] line (%d)", parser_state.file or "", parser_state.line)) + return pos +end) + +local eof = P(-1) +local newline = lpeg.Cmt((P"\n" + "\r\n") * lpeg.Carg(1) ,count_lines) +local line_comment = "#" * (1 - newline) ^0 * (newline + eof) +local blank = S" \t" + newline + line_comment +local blank0 = blank ^ 0 +local blanks = blank ^ 1 +local alpha = R"az" + R"AZ" + "_" +local alnum = alpha + R"09" +local word = alpha * alnum ^ 0 +local name = C(word) +local typename = C(word * ("." * word) ^ 0) +local tag = R"09" ^ 1 / tonumber +local mainkey = "(" * blank0 * name * blank0 * ")" +local decimal = "(" * blank0 * C(tag) * blank0 * ")" + +local function multipat(pat) + return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0) +end + +local function namedpat(name, pat) + return Ct(Cg(Cc(name), "type") * Cg(pat)) +end + +local typedef = P { + "ALL", + FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * (mainkey + decimal)^0)), + STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", + TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), + SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")), + PROTOCOL = namedpat("protocol", name * blanks * tag * blank0 * P"{" * multipat(V"SUBPROTO") * P"}"), + ALL = multipat(V"TYPE" + V"PROTOCOL"), +} + +local proto = blank0 * typedef * blank0 + +local convert = {} + +function convert.protocol(all, obj) + local result = { tag = obj[2] } + for _, p in ipairs(obj[3]) do + assert(result[p[1]] == nil) + local typename = p[2] + if type(typename) == "table" then + local struct = typename + typename = obj[1] .. "." .. p[1] + all.type[typename] = convert.type(all, { typename, struct }) + end + if typename == "nil" then + if p[1] == "response" then + result.confirm = true + end + else + result[p[1]] = typename + end + end + return result +end + +function convert.type(all, obj) + local result = {} + local typename = obj[1] + local tags = {} + local names = {} + for _, f in ipairs(obj[2]) do + if f.type == "field" then + local name = f[1] + if names[name] then + error(string.format("redefine %s in type %s", name, typename)) + end + names[name] = true + local tag = f[2] + if tags[tag] then + error(string.format("redefine tag %d in type %s", tag, typename)) + end + tags[tag] = true + local field = { name = name, tag = tag } + table.insert(result, field) + local fieldtype = f[3] + if fieldtype == "*" then + field.array = true + fieldtype = f[4] + end + local mainkey = f[5] + if mainkey then + if fieldtype == "integer" then + field.decimal = mainkey + else + assert(field.array) + field.key = mainkey + end + end + field.typename = fieldtype + else + assert(f.type == "type") -- nest type + local nesttypename = typename .. "." .. f[1] + f[1] = nesttypename + assert(all.type[nesttypename] == nil, "redefined " .. nesttypename) + all.type[nesttypename] = convert.type(all, f) + end + end + table.sort(result, function(a,b) return a.tag < b.tag end) + return result +end + +local function adjust(r) + local result = { type = {} , protocol = {} } + + for _, obj in ipairs(r) do + local set = result[obj.type] + local name = obj[1] + assert(set[name] == nil , "redefined " .. name) + set[name] = convert[obj.type](result,obj) + end + + return result +end + +local buildin_types = { + integer = 0, + boolean = 1, + string = 2, + binary = 2, -- binary is a sub type of string +} + +local function checktype(types, ptype, t) + if buildin_types[t] then + return t + end + local fullname = ptype .. "." .. t + if types[fullname] then + return fullname + else + ptype = ptype:match "(.+)%..+$" + if ptype then + return checktype(types, ptype, t) + elseif types[t] then + return t + end + end +end + +local function check_protocol(r) + local map = {} + local type = r.type + for name, v in pairs(r.protocol) do + local tag = v.tag + local request = v.request + local response = v.response + local p = map[tag] + + if p then + error(string.format("redefined protocol tag %d at %s", tag, name)) + end + + if request and not type[request] then + error(string.format("Undefined request type %s in protocol %s", request, name)) + end + + if response and not type[response] then + error(string.format("Undefined response type %s in protocol %s", response, name)) + end + + map[tag] = v + end + return r +end + +local function flattypename(r) + for typename, t in pairs(r.type) do + for _, f in pairs(t) do + local ftype = f.typename + local fullname = checktype(r.type, typename, ftype) + if fullname == nil then + error(string.format("Undefined type %s in type %s", ftype, typename)) + end + f.typename = fullname + end + end + + return r +end + +local function parser(text,filename) + local state = { file = filename, pos = 0, line = 1 } + local r = lpeg.match(proto * -1 + exception , text , 1, state ) + return flattypename(check_protocol(adjust(r))) +end + +--[[ +-- The protocol of sproto +.type { + .field { + name 0 : string + buildin 1 : integer + type 2 : integer + tag 3 : integer + array 4 : boolean + key 5 : integer # If key exists, array must be true, and it's a map. + } + name 0 : string + fields 1 : *field +} + +.protocol { + name 0 : string + tag 1 : integer + request 2 : integer # index + response 3 : integer # index + confirm 4 : boolean # true means response nil +} + +.group { + type 0 : *type + protocol 1 : *protocol +} +]] + +local function packfield(f) + local strtbl = {} + if f.array then + if f.key then + table.insert(strtbl, "\6\0") -- 6 fields + else + table.insert(strtbl, "\5\0") -- 5 fields + end + else + table.insert(strtbl, "\4\0") -- 4 fields + end + table.insert(strtbl, "\0\0") -- name (tag = 0, ref an object) + if f.buildin then + table.insert(strtbl, packvalue(f.buildin)) -- buildin (tag = 1) + if f.extra then + table.insert(strtbl, packvalue(f.extra)) -- f.buildin can be integer or string + else + table.insert(strtbl, "\1\0") -- skip (tag = 2) + end + table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) + else + table.insert(strtbl, "\1\0") -- skip (tag = 1) + table.insert(strtbl, packvalue(f.type)) -- type (tag = 2) + table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) + end + if f.array then + table.insert(strtbl, packvalue(1)) -- array = true (tag = 4) + end + if f.key then + table.insert(strtbl, packvalue(f.key)) -- key tag (tag = 5) + end + table.insert(strtbl, packbytes(f.name)) -- external object (name) + return packbytes(table.concat(strtbl)) +end + +local function packtype(name, t, alltypes) + local fields = {} + local tmp = {} + for _, f in ipairs(t) do + tmp.array = f.array + tmp.name = f.name + tmp.tag = f.tag + tmp.extra = f.decimal + + tmp.buildin = buildin_types[f.typename] + if f.typename == "binary" then + tmp.extra = 1 -- binary is sub type of string + end + local subtype + if not tmp.buildin then + subtype = assert(alltypes[f.typename]) + tmp.type = subtype.id + else + tmp.type = nil + end + if f.key then + tmp.key = subtype.fields[f.key] + if not tmp.key then + error("Invalid map index :" .. f.key) + end + else + tmp.key = nil + end + + table.insert(fields, packfield(tmp)) + end + local data + if #fields == 0 then + data = { + "\1\0", -- 1 fields + "\0\0", -- name (id = 0, ref = 0) + packbytes(name), + } + else + data = { + "\2\0", -- 2 fields + "\0\0", -- name (tag = 0, ref = 0) + "\0\0", -- field[] (tag = 1, ref = 1) + packbytes(name), + packbytes(table.concat(fields)), + } + end + + return packbytes(table.concat(data)) +end + +local function packproto(name, p, alltypes) + if p.request then + local request = alltypes[p.request] + if request == nil then + error(string.format("Protocol %s request type %s not found", name, p.request)) + end + request = request.id + end + local tmp = { + "\4\0", -- 4 fields + "\0\0", -- name (id=0, ref=0) + packvalue(p.tag), -- tag (tag=1) + } + if p.request == nil and p.response == nil and p.confirm == nil then + tmp[1] = "\2\0" -- only two fields + else + if p.request then + table.insert(tmp, packvalue(alltypes[p.request].id)) -- request typename (tag=2) + else + table.insert(tmp, "\1\0") -- skip this field (request) + end + if p.response then + table.insert(tmp, packvalue(alltypes[p.response].id)) -- request typename (tag=3) + elseif p.confirm then + tmp[1] = "\5\0" -- add confirm field + table.insert(tmp, "\1\0") -- skip this field (response) + table.insert(tmp, packvalue(1)) -- confirm = true + else + tmp[1] = "\3\0" -- only three fields + end + end + + table.insert(tmp, packbytes(name)) + + return packbytes(table.concat(tmp)) +end + +local function packgroup(t,p) + if next(t) == nil then + assert(next(p) == nil) + return "\0\0" + end + local tt, tp + local alltypes = {} + for name in pairs(t) do + table.insert(alltypes, name) + end + table.sort(alltypes) -- make result stable + for idx, name in ipairs(alltypes) do + local fields = {} + for _, type_fields in ipairs(t[name]) do + if buildin_types[type_fields.typename] then + fields[type_fields.name] = type_fields.tag + end + end + alltypes[name] = { id = idx - 1, fields = fields } + end + tt = {} + for _,name in ipairs(alltypes) do + table.insert(tt, packtype(name, t[name], alltypes)) + end + tt = packbytes(table.concat(tt)) + if next(p) then + local tmp = {} + for name, tbl in pairs(p) do + table.insert(tmp, tbl) + tbl.name = name + end + table.sort(tmp, function(a,b) return a.tag < b.tag end) + + tp = {} + for _, tbl in ipairs(tmp) do + table.insert(tp, packproto(tbl.name, tbl, alltypes)) + end + tp = packbytes(table.concat(tp)) + end + local result + if tp == nil then + result = { + "\1\0", -- 1 field + "\0\0", -- type[] (id = 0, ref = 0) + tt, + } + else + result = { + "\2\0", -- 2fields + "\0\0", -- type array (id = 0, ref = 0) + "\0\0", -- protocol array (id = 1, ref =1) + + tt, + tp, + } + end + + return table.concat(result) +end + +local function encodeall(r) + return packgroup(r.type, r.protocol) +end + +local sparser = {} + +function sparser.dump(str) + local tmp = "" + for i=1,#str do + tmp = tmp .. string.format("%02X ", string.byte(str,i)) + if i % 8 == 0 then + if i % 16 == 0 then + print(tmp) + tmp = "" + else + tmp = tmp .. "- " + end + end + end + print(tmp) +end + +function sparser.parse(text, name) + local r = parser(text, name or "=text") + local data = encodeall(r) + return data +end + +return sparser diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 84d11c4d..b8da7441 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -132,10 +132,15 @@ _ctrl(struct gate * g, const void * msg, int sz) { return; } if (memcmp(command,"start",i) == 0) { - skynet_socket_start(ctx, g->listen_id); + _parm(tmp, sz, i); + int uid = strtol(command , NULL, 10); + int id = hashid_lookup(&g->hash, uid); + if (id>=0) { + skynet_socket_start(ctx, uid); + } return; } - if (memcmp(command, "close", i) == 0) { + if (memcmp(command, "close", i) == 0) { if (g->listen_id >= 0) { skynet_socket_close(ctx, g->listen_id); g->listen_id = -1; @@ -166,18 +171,18 @@ _forward(struct gate *g, struct connection * c, int size) { if (g->broker) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,temp, size); - skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, 0, temp, size); + skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, 1, temp, size); return; } if (c->agent) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,temp, size); - skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, 0 , temp, size); + skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, 1 , temp, size); } else if (g->watchdog) { char * tmp = skynet_malloc(size + 32); int n = snprintf(tmp,32,"%d data ",c->id); databuffer_read(&c->buffer,&g->mp,tmp+n,size); - skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, 0, tmp, size + n); + skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, 1, tmp, size + n); } } @@ -225,10 +230,7 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes break; } int id = hashid_lookup(&g->hash, message->id); - if (id>=0) { - struct connection *c = &g->conn[id]; - _report(g, "%d open %d %s:0",message->id,message->id,c->remote_name); - } else { + if (id<0) { skynet_error(ctx, "Close unknown connection %d", message->id); skynet_socket_close(ctx, message->id); } @@ -259,9 +261,13 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes c->id = message->ud; memcpy(c->remote_name, message+1, sz); c->remote_name[sz] = '\0'; - skynet_socket_start(ctx, message->ud); + _report(g, "%d open %d %s:0",c->id, c->id, c->remote_name); + skynet_error(ctx, "socket open: %x", c->id); } break; + case SKYNET_SOCKET_TYPE_WARNING: + skynet_error(ctx, "fd (%d) send buffer (%d)K", message->id, message->ud); + break; } } @@ -292,7 +298,6 @@ _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t sour } } case PTYPE_SOCKET: - assert(source == 0); // recv socket message from skynet_socket dispatch_socket_message(g, msg, (int)(sz-sizeof(struct skynet_socket_message))); break; @@ -325,6 +330,7 @@ start_listen(struct gate *g, char * listen_addr) { if (g->listen_id < 0) { return 1; } + skynet_socket_start(ctx, g->listen_id); return 0; } @@ -333,13 +339,12 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) { if (parm == NULL) return 1; int max = 0; - int buffer = 0; int sz = strlen(parm)+1; char watchdog[sz]; char binding[sz]; int client_tag = 0; char header; - int n = sscanf(parm, "%c %s %s %d %d %d",&header,watchdog, binding,&client_tag , &max,&buffer); + int n = sscanf(parm, "%c %s %s %d %d", &header, watchdog, binding, &client_tag, &max); if (n<4) { skynet_error(ctx, "Invalid gate parm %s",parm); return 1; diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 926ed46f..3e722fa9 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -1,6 +1,17 @@ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_socket.h" +#include "skynet_handle.h" + +/* + harbor listen the PTYPE_HARBOR (in text) + N name : update the global name + S fd id: connect to new harbor , we should send self_id to fd first , and then recv a id (check it), and at last send queue. + A fd id: accept new harbor , we should send self_id to fd , and then send queue. + + If the fd is disconnected, send message to slave in PTYPE_TEXT. D id + If we don't known a globalname, send message to slave in PTYPE_TEXT. Q name + */ #include #include @@ -13,33 +24,12 @@ #define HASH_SIZE 4096 #define DEFAULT_QUEUE_SIZE 1024 -struct msg { - uint8_t * buffer; - size_t size; -}; - -struct msg_queue { - int size; - int head; - int tail; - struct msg * data; -}; - -struct keyvalue { - struct keyvalue * next; - char key[GLOBALNAME_LENGTH]; - uint32_t hash; - uint32_t value; - struct msg_queue * queue; -}; - -struct hashmap { - struct keyvalue *node[HASH_SIZE]; -}; +// 12 is sizeof(struct remote_message_header) +#define HEADER_COOKIE_LENGTH 12 /* message type (8bits) is in destination high 8bits - harbor id (8bits) is also in that place , but remote message doesn't need harbor id. + harbor id (8bits) is also in that place , but remote message doesn't need harbor id. */ struct remote_message_header { uint32_t source; @@ -47,29 +37,63 @@ struct remote_message_header { uint32_t session; }; -// 12 is sizeof(struct remote_message_header) -#define HEADER_COOKIE_LENGTH 12 +struct harbor_msg { + struct remote_message_header header; + void * buffer; + size_t size; +}; + +struct harbor_msg_queue { + int size; + int head; + int tail; + struct harbor_msg * data; +}; + +struct keyvalue { + struct keyvalue * next; + char key[GLOBALNAME_LENGTH]; + uint32_t hash; + uint32_t value; + struct harbor_msg_queue * queue; +}; + +struct hashmap { + struct keyvalue *node[HASH_SIZE]; +}; + +#define STATUS_WAIT 0 +#define STATUS_HANDSHAKE 1 +#define STATUS_HEADER 2 +#define STATUS_CONTENT 3 +#define STATUS_DOWN 4 + +struct slave { + int fd; + struct harbor_msg_queue *queue; + int status; + int length; + int read; + uint8_t size[4]; + char * recv_buffer; +}; struct harbor { struct skynet_context *ctx; - char * local_addr; int id; + uint32_t slave; struct hashmap * map; - int master_fd; - char * master_addr; - int remote_fd[REMOTE_MAX]; - bool connected[REMOTE_MAX]; - char * remote_addr[REMOTE_MAX]; + struct slave s[REMOTE_MAX]; }; // hash table static void -_push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct remote_message_header * header) { +push_queue_msg(struct harbor_msg_queue * queue, struct harbor_msg * m) { // If there is only 1 free slot which is reserved to distinguish full/empty // of circular buffer, expand it. if (((queue->tail + 1) % queue->size) == queue->head) { - struct msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct msg)); + struct harbor_msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct harbor_msg)); int i; for (i=0;isize-1;i++) { new_buffer[i] = queue->data[(i+queue->head) % queue->size]; @@ -80,51 +104,55 @@ _push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct rem queue->tail = queue->size - 1; queue->size *= 2; } - struct msg * slot = &queue->data[queue->tail]; + struct harbor_msg * slot = &queue->data[queue->tail]; + *slot = *m; queue->tail = (queue->tail + 1) % queue->size; - - slot->buffer = skynet_malloc(sz + sizeof(*header)); - memcpy(slot->buffer, buffer, sz); - memcpy(slot->buffer + sz, header, sizeof(*header)); - slot->size = sz + sizeof(*header); } -static struct msg * -_pop_queue(struct msg_queue * queue) { +static void +push_queue(struct harbor_msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) { + struct harbor_msg m; + m.header = *header; + m.buffer = buffer; + m.size = sz; + push_queue_msg(queue, &m); +} + +static struct harbor_msg * +pop_queue(struct harbor_msg_queue * queue) { if (queue->head == queue->tail) { return NULL; } - struct msg * slot = &queue->data[queue->head]; + struct harbor_msg * slot = &queue->data[queue->head]; queue->head = (queue->head + 1) % queue->size; return slot; } -static struct msg_queue * -_new_queue() { - struct msg_queue * queue = skynet_malloc(sizeof(*queue)); +static struct harbor_msg_queue * +new_queue() { + struct harbor_msg_queue * queue = skynet_malloc(sizeof(*queue)); queue->size = DEFAULT_QUEUE_SIZE; queue->head = 0; queue->tail = 0; - queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg)); + queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct harbor_msg)); return queue; } static void -_release_queue(struct msg_queue *queue) { +release_queue(struct harbor_msg_queue *queue) { if (queue == NULL) return; - struct msg * m = _pop_queue(queue); - while (m) { + struct harbor_msg * m; + while ((m=pop_queue(queue)) != NULL) { skynet_free(m->buffer); - m = _pop_queue(queue); } skynet_free(queue->data); skynet_free(queue); } static struct keyvalue * -_hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { +hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t*) name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue * node = hash->node[h % HASH_SIZE]; @@ -142,7 +170,7 @@ _hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { // Don't support erase name yet static struct void -_hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { +hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { uint32_t *ptr = name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** ptr = &hash->node[h % HASH_SIZE]; @@ -160,7 +188,7 @@ _hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { */ static struct keyvalue * -_hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { +hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t *)name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** pkv = &hash->node[h % HASH_SIZE]; @@ -176,20 +204,20 @@ _hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { } static struct hashmap * -_hash_new() { +hash_new() { struct hashmap * h = skynet_malloc(sizeof(struct hashmap)); memset(h,0,sizeof(*h)); return h; } static void -_hash_delete(struct hashmap *hash) { +hash_delete(struct hashmap *hash) { int i; for (i=0;inode[i]; while (node) { struct keyvalue * next = node->next; - _release_queue(node->queue); + release_queue(node->queue); skynet_free(node); node = next; } @@ -199,64 +227,50 @@ _hash_delete(struct hashmap *hash) { /////////////// +static void +close_harbor(struct harbor *h, int id) { + struct slave *s = &h->s[id]; + s->status = STATUS_DOWN; + if (s->fd) { + skynet_socket_close(h->ctx, s->fd); + } + if (s->queue) { + release_queue(s->queue); + s->queue = NULL; + } +} + +static void +report_harbor_down(struct harbor *h, int id) { + char down[64]; + int n = sprintf(down, "D %d",id); + + skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, down, n); +} + struct harbor * harbor_create(void) { struct harbor * h = skynet_malloc(sizeof(*h)); - h->ctx = NULL; - h->id = 0; - h->master_fd = -1; - h->master_addr = NULL; - int i; - for (i=0;iremote_fd[i] = -1; - h->connected[i] = false; - h->remote_addr[i] = NULL; - } - h->map = _hash_new(); + memset(h,0,sizeof(*h)); + h->map = hash_new(); return h; } void harbor_release(struct harbor *h) { - struct skynet_context *ctx = h->ctx; - if (h->master_fd >= 0) { - skynet_socket_close(ctx, h->master_fd); - } - skynet_free(h->master_addr); - skynet_free(h->local_addr); int i; - for (i=0;iremote_fd[i] >= 0) { - skynet_socket_close(ctx, h->remote_fd[i]); - skynet_free(h->remote_addr[i]); + for (i=1;is[i]; + if (s->fd && s->status != STATUS_DOWN) { + close_harbor(h,i); + // don't call report_harbor_down. + // never call skynet_send during module exit, because of dead lock } } - _hash_delete(h->map); + hash_delete(h->map); skynet_free(h); } -static int -_connect_to(struct harbor *h, const char *ipaddress, bool blocking) { - char * port = strchr(ipaddress,':'); - if (port==NULL) { - return -1; - } - int sz = port - ipaddress; - char tmp[sz + 1]; - memcpy(tmp,ipaddress,sz); - tmp[sz] = '\0'; - - int portid = strtol(port+1, NULL,10); - - skynet_error(h->ctx, "Harbor(%d) connect to %s:%d", h->id, tmp, portid); - - if (blocking) { - return skynet_socket_block_connect(h->ctx, tmp, portid); - } else { - return skynet_socket_connect(h->ctx, tmp, portid); - } -} - static inline void to_bigendian(uint8_t *buffer, uint32_t n) { buffer[0] = (n >> 24) & 0xff; @@ -266,7 +280,7 @@ to_bigendian(uint8_t *buffer, uint32_t n) { } static inline void -_header_to_message(const struct remote_message_header * header, uint8_t * message) { +header_to_message(const struct remote_message_header * header, uint8_t * message) { to_bigendian(message , header->source); to_bigendian(message+4 , header->destination); to_bigendian(message+8 , header->session); @@ -283,112 +297,226 @@ from_bigendian(uint32_t n) { } static inline void -_message_to_header(const uint32_t *message, struct remote_message_header *header) { +message_to_header(const uint32_t *message, struct remote_message_header *header) { header->source = from_bigendian(message[0]); header->destination = from_bigendian(message[1]); header->session = from_bigendian(message[2]); } -static void -_send_package(struct skynet_context *ctx, int fd, const void * buffer, size_t sz) { - uint8_t * sendbuf = skynet_malloc(sz+4); - to_bigendian(sendbuf, sz); - memcpy(sendbuf+4, buffer, sz); +// socket package - if (skynet_socket_send(ctx, fd, sendbuf, sz+4)) { - skynet_error(ctx, "Send to %d error", fd); +static void +forward_local_messsage(struct harbor *h, void *msg, int sz) { + const char * cookie = msg; + cookie += sz - HEADER_COOKIE_LENGTH; + struct remote_message_header header; + message_to_header((const uint32_t *)cookie, &header); + + uint32_t destination = header.destination; + int type = destination >> HANDLE_REMOTE_SHIFT; + destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); + + if (skynet_send(h->ctx, header.source, destination, type | PTYPE_TAG_DONTCOPY , (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { + if (type != PTYPE_ERROR) { + // don't need report error when type is error + skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); + } + skynet_error(h->ctx, "Unknown destination :%x from :%x type(%d)", destination, header.source, type); } } static void -_send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { - uint32_t sz_header = sz+sizeof(*cookie); +send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { + size_t sz_header = sz+sizeof(*cookie); + if (sz_header > UINT32_MAX) { + skynet_error(ctx, "remote message from :%08x to :%08x is too large.", cookie->source, cookie->destination); + return; + } uint8_t * sendbuf = skynet_malloc(sz_header+4); - to_bigendian(sendbuf, sz_header); + to_bigendian(sendbuf, (uint32_t)sz_header); memcpy(sendbuf+4, buffer, sz); - _header_to_message(cookie, sendbuf+4+sz); + header_to_message(cookie, sendbuf+4+sz); - if (skynet_socket_send(ctx, fd, sendbuf, sz_header+4)) { - skynet_error(ctx, "Remote send to %d error", fd); - } + // ignore send error, because if the connection is broken, the mainloop will recv a message. + skynet_socket_send(ctx, fd, sendbuf, sz_header+4); } static void -_update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) { - if (harbor_id == h->id) { - return; - } - assert(harbor_id > 0 && harbor_id< REMOTE_MAX); - struct skynet_context * context = h->ctx; - if (h->remote_fd[harbor_id] >=0) { - skynet_socket_close(context, h->remote_fd[harbor_id]); - skynet_free(h->remote_addr[harbor_id]); - h->remote_addr[harbor_id] = NULL; - } - h->remote_fd[harbor_id] = _connect_to(h, ipaddr, false); - h->connected[harbor_id] = false; -} - -static void -_dispatch_queue(struct harbor *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { +dispatch_name_queue(struct harbor *h, struct keyvalue * node) { + struct harbor_msg_queue * queue = node->queue; + uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; - assert(harbor_id != 0); struct skynet_context * context = h->ctx; - int fd = h->remote_fd[harbor_id]; - if (fd < 0) { - char tmp [GLOBALNAME_LENGTH+1]; - memcpy(tmp, name , GLOBALNAME_LENGTH); - tmp[GLOBALNAME_LENGTH] = '\0'; - skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); + struct slave *s = &h->s[harbor_id]; + int fd = s->fd; + if (fd == 0) { + if (s->status == STATUS_DOWN) { + char tmp [GLOBALNAME_LENGTH+1]; + memcpy(tmp, node->key, GLOBALNAME_LENGTH); + tmp[GLOBALNAME_LENGTH] = '\0'; + skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); + } else { + if (s->queue == NULL) { + s->queue = node->queue; + node->queue = NULL; + } else { + struct harbor_msg * m; + while ((m = pop_queue(queue))!=NULL) { + push_queue_msg(s->queue, m); + } + } + if (harbor_id == (h->slave >> HANDLE_REMOTE_SHIFT)) { + // the harbor_id is local + struct harbor_msg * m; + while ((m = pop_queue(s->queue)) != NULL) { + int type = m->header.destination >> HANDLE_REMOTE_SHIFT; + skynet_send(context, m->header.source, handle , type | PTYPE_TAG_DONTCOPY, m->header.session, m->buffer, m->size); + } + release_queue(s->queue); + s->queue = NULL; + } + } return; } - struct msg * m = _pop_queue(queue); - while (m) { - struct remote_message_header cookie; - uint8_t *ptr = m->buffer + m->size - sizeof(cookie); - memcpy(&cookie, ptr, sizeof(cookie)); - cookie.destination |= (handle & HANDLE_MASK); - _header_to_message(&cookie, ptr); - _send_package(context, fd, m->buffer, m->size); - m = _pop_queue(queue); + struct harbor_msg * m; + while ((m = pop_queue(queue)) != NULL) { + m->header.destination |= (handle & HANDLE_MASK); + send_remote(context, fd, m->buffer, m->size, &m->header); + skynet_free(m->buffer); } } static void -_update_remote_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { - struct keyvalue * node = _hash_search(h->map, name); +dispatch_queue(struct harbor *h, int id) { + struct slave *s = &h->s[id]; + int fd = s->fd; + assert(fd != 0); + + struct harbor_msg_queue *queue = s->queue; + if (queue == NULL) + return; + + struct harbor_msg * m; + while ((m = pop_queue(queue)) != NULL) { + send_remote(h->ctx, fd, m->buffer, m->size, &m->header); + skynet_free(m->buffer); + } + release_queue(queue); + s->queue = NULL; +} + +static void +push_socket_data(struct harbor *h, const struct skynet_socket_message * message) { + assert(message->type == SKYNET_SOCKET_TYPE_DATA); + int fd = message->id; + int i; + int id = 0; + struct slave * s = NULL; + for (i=1;is[i].fd == fd) { + s = &h->s[i]; + id = i; + break; + } + } + if (s == NULL) { + skynet_free(message->buffer); + skynet_error(h->ctx, "Invalid socket fd (%d) data", fd); + return; + } + uint8_t * buffer = (uint8_t *)message->buffer; + int size = message->ud; + + for (;;) { + switch(s->status) { + case STATUS_HANDSHAKE: { + // check id + uint8_t remote_id = buffer[0]; + if (remote_id != id) { + skynet_error(h->ctx, "Invalid shakehand id (%d) from fd = %d , harbor = %d", id, fd, remote_id); + close_harbor(h,id); + return; + } + ++buffer; + --size; + s->status = STATUS_HEADER; + + dispatch_queue(h, id); + + if (size == 0) { + break; + } + // go though + } + case STATUS_HEADER: { + // big endian 4 bytes length, the first one must be 0. + int need = 4 - s->read; + if (size < need) { + memcpy(s->size + s->read, buffer, size); + s->read += size; + return; + } else { + memcpy(s->size + s->read, buffer, need); + buffer += need; + size -= need; + + if (s->size[0] != 0) { + skynet_error(h->ctx, "Message is too long from harbor %d", id); + close_harbor(h,id); + return; + } + s->length = s->size[1] << 16 | s->size[2] << 8 | s->size[3]; + s->read = 0; + s->recv_buffer = skynet_malloc(s->length); + s->status = STATUS_CONTENT; + if (size == 0) { + return; + } + } + } + // go though + case STATUS_CONTENT: { + int need = s->length - s->read; + if (size < need) { + memcpy(s->recv_buffer + s->read, buffer, size); + s->read += size; + return; + } + memcpy(s->recv_buffer + s->read, buffer, need); + forward_local_messsage(h, s->recv_buffer, s->length); + s->length = 0; + s->read = 0; + s->recv_buffer = NULL; + size -= need; + buffer += need; + s->status = STATUS_HEADER; + if (size == 0) + return; + break; + } + default: + return; + } + } +} + +static void +update_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { + struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { - node = _hash_insert(h->map, name); + node = hash_insert(h->map, name); } node->value = handle; if (node->queue) { - _dispatch_queue(h, node->queue, handle, name); - _release_queue(node->queue); + dispatch_name_queue(h, node); + release_queue(node->queue); node->queue = NULL; } } -static void -_request_master(struct harbor *h, const char name[GLOBALNAME_LENGTH], size_t i, uint32_t handle) { - uint8_t buffer[4+i]; - to_bigendian(buffer, handle); - memcpy(buffer+4,name,i); - - _send_package(h->ctx, h->master_fd, buffer, 4+i); -} - -/* - update global name to master - - 2 bytes (size) - 4 bytes (handle) (handle == 0 for request) - n bytes string (name) - */ - static int -_remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) { +remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) { int harbor_id = destination >> HANDLE_REMOTE_SHIFT; - assert(harbor_id != 0); struct skynet_context * context = h->ctx; if (harbor_id == h->id) { // local message @@ -396,55 +524,117 @@ _remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int return 1; } - int fd = h->remote_fd[harbor_id]; - if (fd >= 0 && h->connected[harbor_id]) { + struct slave * s = &h->s[harbor_id]; + if (s->fd == 0 || s->status == STATUS_HANDSHAKE) { + if (s->status == STATUS_DOWN) { + // throw an error return to source + // report the destination is dead + skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0); + skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); + } else { + if (s->queue == NULL) { + s->queue = new_queue(); + } + struct remote_message_header header; + header.source = source; + header.destination = (type << HANDLE_REMOTE_SHIFT) | (destination & HANDLE_MASK); + header.session = (uint32_t)session; + push_queue(s->queue, (void *)msg, sz, &header); + return 1; + } + } else { struct remote_message_header cookie; cookie.source = source; cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT); cookie.session = (uint32_t)session; - _send_remote(context, fd, msg,sz,&cookie); - } else { - // throw an error return to source - // report the destination is dead - skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0); - skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); + send_remote(context, s->fd, msg,sz,&cookie); } + return 0; } -static void -_remote_register_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { - int i; - for (i=0;imap, name); +remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) { + struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { - node = _hash_insert(h->map, name); + node = hash_insert(h->map, name); } if (node->value == 0) { if (node->queue == NULL) { - node->queue = _new_queue(); + node->queue = new_queue(); } struct remote_message_header header; header.source = source; header.destination = type << HANDLE_REMOTE_SHIFT; header.session = (uint32_t)session; - _push_queue(node->queue, msg, sz, &header); - // 0 for request - _remote_register_name(h, name, 0); + push_queue(node->queue, (void *)msg, sz, &header); + char query[2+GLOBALNAME_LENGTH+1] = "Q "; + query[2+GLOBALNAME_LENGTH] = 0; + memcpy(query+2, name, GLOBALNAME_LENGTH); + skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, query, strlen(query)); return 1; } else { - return _remote_send_handle(h, source, node->value, type, session, msg, sz); + return remote_send_handle(h, source, node->value, type, session, msg, sz); + } +} + +static 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); +} + +static void +harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint32_t source) { + const char * name = msg + 2; + int s = (int)sz; + s -= 2; + switch(msg[0]) { + case 'N' : { + if (s <=0 || s>= GLOBALNAME_LENGTH) { + skynet_error(h->ctx, "Invalid global name %s", name); + return; + } + struct remote_name rn; + memset(&rn, 0, sizeof(rn)); + memcpy(rn.name, name, s); + rn.handle = source; + update_name(h, rn.name, rn.handle); + break; + } + case 'S' : + case 'A' : { + char buffer[s+1]; + memcpy(buffer, name, s); + buffer[s] = 0; + int fd=0, id=0; + sscanf(buffer, "%d %d",&fd,&id); + if (fd == 0 || id <= 0 || id>=REMOTE_MAX) { + skynet_error(h->ctx, "Invalid command %c %s", msg[0], buffer); + return; + } + struct slave * slave = &h->s[id]; + if (slave->fd != 0) { + skynet_error(h->ctx, "Harbor %d alreay exist", id); + return; + } + slave->fd = fd; + + skynet_socket_start(h->ctx, fd); + handshake(h, id); + if (msg[0] == 'S') { + slave->status = STATUS_HANDSHAKE; + } else { + slave->status = STATUS_HEADER; + dispatch_queue(h,id); + } + break; + } + default: + skynet_error(h->ctx, "Unknown command %s", msg); + return; } } @@ -452,105 +642,64 @@ static int harbor_id(struct harbor *h, int fd) { int i; for (i=1;iremote_fd[i] == fd) + struct slave *s = &h->s[i]; + if (s->fd == fd) { return i; + } } return 0; } -static void -close_harbor(struct harbor *h, int fd) { - int id = harbor_id(h,fd); - if (id == 0) - return; - skynet_error(h->ctx, "Harbor %d closed",id); - skynet_socket_close(h->ctx, fd); - h->remote_fd[id] = -1; - h->connected[id] = false; -} - -static void -open_harbor(struct harbor *h, int fd) { - int id = harbor_id(h,fd); - if (id == 0) - return; - assert(h->connected[id] == false); - h->connected[id] = true; -} - static int -_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { +mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct harbor * h = ud; switch (type) { case PTYPE_SOCKET: { const struct skynet_socket_message * message = msg; switch(message->type) { case SKYNET_SOCKET_TYPE_DATA: + push_socket_data(h, message); skynet_free(message->buffer); - skynet_error(context, "recv invalid socket message (size=%d)", message->ud); - break; - case SKYNET_SOCKET_TYPE_ACCEPT: - skynet_error(context, "recv invalid socket accept message"); break; case SKYNET_SOCKET_TYPE_ERROR: - case SKYNET_SOCKET_TYPE_CLOSE: - close_harbor(h, message->id); + case SKYNET_SOCKET_TYPE_CLOSE: { + int id = harbor_id(h, message->id); + if (id) { + report_harbor_down(h,id); + } else { + skynet_error(context, "Unkown fd (%d) closed", message->id); + } break; + } case SKYNET_SOCKET_TYPE_CONNECT: - open_harbor(h, message->id); + // fd forward to this service + break; + case SKYNET_SOCKET_TYPE_WARNING: { + int id = harbor_id(h, message->id); + if (id) { + skynet_error(context, "message havn't send to Harbor (%d) reach %d K", id, message->ud); + } + break; + } + default: + skynet_error(context, "recv invalid socket message type %d", type); break; } return 0; } case PTYPE_HARBOR: { - // remote message in - const char * cookie = msg; - cookie += sz - HEADER_COOKIE_LENGTH; - struct remote_message_header header; - _message_to_header((const uint32_t *)cookie, &header); - if (header.source == 0) { - if (header.destination < REMOTE_MAX) { - // 1 byte harbor id (0~255) - // update remote harbor address - char ip [sz - HEADER_COOKIE_LENGTH + 1]; - memcpy(ip, msg, sz-HEADER_COOKIE_LENGTH); - ip[sz-HEADER_COOKIE_LENGTH] = '\0'; - _update_remote_address(h, header.destination, ip); - } else { - // update global name - if (sz - HEADER_COOKIE_LENGTH > GLOBALNAME_LENGTH) { - char name[sz-HEADER_COOKIE_LENGTH+1]; - memcpy(name, msg, sz-HEADER_COOKIE_LENGTH); - name[sz-HEADER_COOKIE_LENGTH] = '\0'; - skynet_error(context, "Global name is too long %s", name); - } - _update_remote_name(h, msg, header.destination); - } - } else { - uint32_t destination = header.destination; - int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY; - destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); - skynet_send(context, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH); - return 1; - } - return 0; - } - case PTYPE_SYSTEM: { - // register name message - const struct remote_message *rmsg = msg; - assert (sz == sizeof(rmsg->destination)); - _remote_register_name(h, rmsg->destination.name, rmsg->destination.handle); + harbor_command(h, msg,sz,session,source); return 0; } default: { // remote message out const struct remote_message *rmsg = msg; if (rmsg->destination.handle == 0) { - if (_remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) { + if (remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) { return 0; } } else { - if (_remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) { + if (remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) { return 0; } } @@ -560,48 +709,19 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin } } -static void -_launch_gate(struct skynet_context * ctx, const char * local_addr) { - char tmp[128]; - sprintf(tmp,"gate L ! %s %d %d 0",local_addr, PTYPE_HARBOR, REMOTE_MAX); - const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp); - if (gate_addr == NULL) { - fprintf(stderr, "Harbor : launch gate failed\n"); - exit(1); - } - uint32_t gate = strtoul(gate_addr+1 , NULL, 16); - if (gate == 0) { - fprintf(stderr, "Harbor : launch gate invalid %s", gate_addr); - exit(1); - } - const char * self_addr = skynet_command(ctx, "REG", NULL); - int n = sprintf(tmp,"broker %s",self_addr); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5); -} - int harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { h->ctx = ctx; - int sz = strlen(args)+1; - char master_addr[sz]; - char local_addr[sz]; int harbor_id = 0; - sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id); - h->master_addr = skynet_strdup(master_addr); - h->id = harbor_id; - h->master_fd = _connect_to(h, master_addr, true); - if (h->master_fd == -1) { - fprintf(stderr, "Harbor: Connect to master failed\n"); - exit(1); + uint32_t slave = 0; + sscanf(args,"%d %u", &harbor_id, &slave); + if (slave == 0) { + return 1; } + h->id = harbor_id; + h->slave = slave; + skynet_callback(ctx, h, mainloop); skynet_harbor_start(ctx); - h->local_addr = skynet_strdup(local_addr); - - _launch_gate(ctx, local_addr); - skynet_callback(ctx, h, _mainloop); - _request_master(h, local_addr, strlen(local_addr), harbor_id); - return 0; } diff --git a/service-src/service_logger.c b/service-src/service_logger.c index c78cf4d6..e73300e3 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -3,9 +3,11 @@ #include #include #include +#include struct logger { FILE * handle; + char * filename; int close; }; @@ -14,6 +16,8 @@ logger_create(void) { struct logger * inst = skynet_malloc(sizeof(*inst)); inst->handle = NULL; inst->close = 0; + inst->filename = NULL; + return inst; } @@ -22,16 +26,26 @@ logger_release(struct logger * inst) { if (inst->close) { fclose(inst->handle); } + skynet_free(inst->filename); skynet_free(inst); } static int -_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { +logger_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct logger * inst = ud; - fprintf(inst->handle, "[:%x] ",source); - fwrite(msg, sz , 1, inst->handle); - fprintf(inst->handle, "\n"); - fflush(inst->handle); + switch (type) { + case PTYPE_SYSTEM: + if (inst->filename) { + inst->handle = freopen(inst->filename, "a", inst->handle); + } + break; + case PTYPE_TEXT: + fprintf(inst->handle, "[:%08x] ",source); + fwrite(msg, sz , 1, inst->handle); + fprintf(inst->handle, "\n"); + fflush(inst->handle); + break; + } return 0; } @@ -43,12 +57,14 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) if (inst->handle == NULL) { return 1; } + inst->filename = skynet_malloc(strlen(parm)+1); + strcpy(inst->filename, parm); inst->close = 1; } else { inst->handle = stdout; } if (inst->handle) { - skynet_callback(ctx, inst, _logger); + skynet_callback(ctx, inst, logger_cb); skynet_command(ctx, "REG", ".logger"); return 0; } diff --git a/service-src/service_master.c b/service-src/service_master.c deleted file mode 100644 index b5d8a560..00000000 --- a/service-src/service_master.c +++ /dev/null @@ -1,313 +0,0 @@ -#include "skynet.h" -#include "skynet_harbor.h" -#include "skynet_socket.h" - -#include -#include -#include -#include -#include -#include - -#define HASH_SIZE 4096 - -struct name { - struct name * next; - char key[GLOBALNAME_LENGTH]; - uint32_t hash; - uint32_t value; -}; - -struct namemap { - struct name *node[HASH_SIZE]; -}; - -struct master { - struct skynet_context *ctx; - int remote_fd[REMOTE_MAX]; - bool connected[REMOTE_MAX]; - char * remote_addr[REMOTE_MAX]; - struct namemap map; -}; - -struct master * -master_create() { - struct master *m = skynet_malloc(sizeof(*m)); - int i; - for (i=0;iremote_fd[i] = -1; - m->remote_addr[i] = NULL; - m->connected[i] = false; - } - memset(&m->map, 0, sizeof(m->map)); - return m; -} - -void -master_release(struct master * m) { - int i; - struct skynet_context *ctx = m->ctx; - for (i=0;iremote_fd[i]; - if (fd >= 0) { - assert(ctx); - skynet_socket_close(ctx, fd); - } - skynet_free(m->remote_addr[i]); - } - for (i=0;imap.node[i]; - while (node) { - struct name * next = node->next; - skynet_free(node); - node = next; - } - } - skynet_free(m); -} - -static struct name * -_search_name(struct master *m, char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t *) name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct name * node = m->map.node[h % HASH_SIZE]; - while (node) { - if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { - return node; - } - node = node->next; - } - return NULL; -} - -static struct name * -_insert_name(struct master *m, char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t *)name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct name **pname = &m->map.node[h % HASH_SIZE]; - struct name * node = skynet_malloc(sizeof(*node)); - memcpy(node->key, name, GLOBALNAME_LENGTH); - node->next = *pname; - node->hash = h; - node->value = 0; - *pname = node; - return node; -} - -static void -_copy_name(char *name, const char * buffer, size_t sz) { - if (sz < GLOBALNAME_LENGTH) { - memcpy(name, buffer, sz); - memset(name+sz, 0 , GLOBALNAME_LENGTH - sz); - } else { - memcpy(name, buffer, GLOBALNAME_LENGTH); - } -} - -static void -_connect_to(struct master *m, int id) { - assert(m->connected[id] == false); - struct skynet_context * ctx = m->ctx; - const char *ipaddress = m->remote_addr[id]; - char * portstr = strchr(ipaddress,':'); - if (portstr==NULL) { - skynet_error(ctx, "Harbor %d : address invalid (%s)",id, ipaddress); - return; - } - int sz = portstr - ipaddress; - char tmp[sz + 1]; - memcpy(tmp,ipaddress,sz); - tmp[sz] = '\0'; - int port = strtol(portstr+1,NULL,10); - skynet_error(ctx, "Master connect to harbor(%d) %s:%d", id, tmp, port); - m->remote_fd[id] = skynet_socket_connect(ctx, tmp, port); -} - -static inline void -to_bigendian(uint8_t *buffer, uint32_t n) { - buffer[0] = (n >> 24) & 0xff; - buffer[1] = (n >> 16) & 0xff; - buffer[2] = (n >> 8) & 0xff; - buffer[3] = n & 0xff; -} - -static void -_send_to(struct master *m, int id, const void * buf, int sz, uint32_t handle) { - uint8_t * buffer= (uint8_t *)skynet_malloc(4 + sz + 12); - to_bigendian(buffer, sz+12); - memcpy(buffer+4, buf, sz); - to_bigendian(buffer+4+sz, 0); - to_bigendian(buffer+4+sz+4, handle); - to_bigendian(buffer+4+sz+8, 0); - - sz += 4 + 12; - - if (skynet_socket_send(m->ctx, m->remote_fd[id], buffer, sz)) { - skynet_error(m->ctx, "Harbor %d : send error", id); - } -} - -static void -_broadcast(struct master *m, const char *name, size_t sz, uint32_t handle) { - int i; - for (i=1;iremote_fd[i]; - if (fd < 0 || m->connected[i]==false) - continue; - _send_to(m, i , name, sz, handle); - } -} - -static void -_request_name(struct master *m, const char * buffer, size_t sz) { - char name[GLOBALNAME_LENGTH]; - _copy_name(name, buffer, sz); - struct name * n = _search_name(m, name); - if (n == NULL) { - return; - } - _broadcast(m, name, GLOBALNAME_LENGTH, n->value); -} - -static void -_update_name(struct master *m, uint32_t handle, const char * buffer, size_t sz) { - char name[GLOBALNAME_LENGTH]; - _copy_name(name, buffer, sz); - struct name * n = _search_name(m, name); - if (n==NULL) { - n = _insert_name(m,name); - } - n->value = handle; - _broadcast(m,name,GLOBALNAME_LENGTH, handle); -} - -static void -close_harbor(struct master *m, int harbor_id) { - if (m->connected[harbor_id]) { - struct skynet_context * context = m->ctx; - skynet_socket_close(context, m->remote_fd[harbor_id]); - m->remote_fd[harbor_id] = -1; - m->connected[harbor_id] = false; - } -} - -static void -_update_address(struct master *m, int harbor_id, const char * buffer, size_t sz) { - if (m->remote_fd[harbor_id] >= 0) { - close_harbor(m, harbor_id); - } - skynet_free(m->remote_addr[harbor_id]); - char * addr = skynet_malloc(sz+1); - memcpy(addr, buffer, sz); - addr[sz] = '\0'; - m->remote_addr[harbor_id] = addr; - _connect_to(m, harbor_id); -} - -static int -socket_id(struct master *m, int id) { - int i; - for (i=1;iremote_fd[i] == id) - return i; - } - return 0; -} - -static void -on_connected(struct master *m, int id) { - _broadcast(m, m->remote_addr[id], strlen(m->remote_addr[id]), id); - m->connected[id] = true; - int i; - for (i=1;iremote_addr[i]; - if (addr == NULL || m->connected[i] == false) { - continue; - } - _send_to(m, id , addr, strlen(addr), i); - } -} - -static void -dispatch_socket(struct master *m, const struct skynet_socket_message *msg, int sz) { - int id = socket_id(m, msg->id); - switch(msg->type) { - case SKYNET_SOCKET_TYPE_CONNECT: - assert(id); - on_connected(m, id); - break; - case SKYNET_SOCKET_TYPE_ERROR: - skynet_error(m->ctx, "socket error on harbor %d", id); - // go though, close socket - case SKYNET_SOCKET_TYPE_CLOSE: - close_harbor(m, id); - break; - default: - skynet_error(m->ctx, "Invalid socket message type %d", msg->type); - break; - } -} - - -/* - update global name to master - - 4 bytes (handle) (handle == 0 for request) - n bytes string (name) - */ - -static int -_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { - if (type == PTYPE_SOCKET) { - dispatch_socket(ud, msg, (int)sz); - return 0; - } - if (type != PTYPE_HARBOR) { - skynet_error(context, "None harbor message recv from %x (type = %d)", source, type); - return 0; - } - assert(sz >= 4); - struct master *m = ud; - const uint8_t *handlen = msg; - uint32_t handle = handlen[0]<<24 | handlen[1]<<16 | handlen[2]<<8 | handlen[3]; - sz -= 4; - const char * name = msg; - name += 4; - - if (handle == 0) { - _request_name(m , name, sz); - } else if (handle < REMOTE_MAX) { - _update_address(m , handle, name, sz); - } else { - _update_name(m , handle, name, sz); - } - - return 0; -} - -int -master_init(struct master *m, struct skynet_context *ctx, const char * args) { - char tmp[strlen(args) + 32]; - sprintf(tmp,"gate L ! %s %d %d 0",args,PTYPE_HARBOR,REMOTE_MAX); - const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp); - if (gate_addr == NULL) { - skynet_error(ctx, "Master : launch gate failed"); - return 1; - } - uint32_t gate = strtoul(gate_addr+1, NULL, 16); - if (gate == 0) { - skynet_error(ctx, "Master : launch gate invalid %s", gate_addr); - return 1; - } - const char * self_addr = skynet_command(ctx, "REG", NULL); - int n = sprintf(tmp,"broker %s",self_addr); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5); - - skynet_callback(ctx, m, _mainloop); - - m->ctx = ctx; - return 0; -} diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 33e0a5ff..6f2ea6c5 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -3,16 +3,20 @@ #include #include #include -#include #include #include #include #include +#define MEMORY_WARNING_REPORT (1024 * 1024 * 32) + struct snlua { lua_State * L; struct skynet_context * ctx; + size_t mem; + size_t mem_report; + size_t mem_limit; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -31,6 +35,7 @@ static int codecache(lua_State *L) { luaL_Reg l[] = { { "clear", cleardummy }, + { "mode", cleardummy }, { NULL, NULL }, }; luaL_newlib(L,l); @@ -53,9 +58,9 @@ traceback (lua_State *L) { } static void -_report_launcher_error(struct skynet_context *ctx) { +report_launcher_error(struct skynet_context *ctx) { // sizeof "ERROR" == 5 - skynet_sendname(ctx, ".launcher", PTYPE_TEXT, 0, "ERROR", 5); + skynet_sendname(ctx, 0, ".launcher", PTYPE_TEXT, 0, "ERROR", 5); } static const char * @@ -68,7 +73,7 @@ optstring(struct skynet_context *ctx, const char *key, const char * str) { } static int -_init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) { +init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) { lua_State *L = l->L; l->ctx = ctx; lua_gc(L, LUA_GCSTOP, 0); @@ -101,17 +106,25 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) int r = luaL_loadfile(L,loader); if (r != LUA_OK) { skynet_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1)); - _report_launcher_error(ctx); + report_launcher_error(ctx); return 1; } lua_pushlstring(L, args, sz); r = lua_pcall(L,1,0,1); if (r != LUA_OK) { skynet_error(ctx, "lua loader error : %s", lua_tostring(L, -1)); - _report_launcher_error(ctx); + report_launcher_error(ctx); return 1; } lua_settop(L,0); + if (lua_getfield(L, LUA_REGISTRYINDEX, "memlimit") == LUA_TNUMBER) { + size_t limit = lua_tointeger(L, -1); + l->mem_limit = limit; + skynet_error(ctx, "Set memory limit to %.2f M", (float)limit / (1024 * 1024)); + lua_pushnil(L); + lua_setfield(L, LUA_REGISTRYINDEX, "memlimit"); + } + lua_pop(L, 1); lua_gc(L, LUA_GCRESTART, 0); @@ -119,11 +132,11 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) } static int -_launch(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz) { +launch_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz) { assert(type == 0 && session == 0); struct snlua *l = ud; skynet_callback(context, NULL, NULL); - int err = _init(l, context, msg, sz); + int err = init_cb(l, context, msg, sz); if (err) { skynet_command(context, "EXIT", NULL); } @@ -136,7 +149,7 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { int sz = strlen(args); char * tmp = skynet_malloc(sz); memcpy(tmp, args, sz); - skynet_callback(ctx, l , _launch); + skynet_callback(ctx, l , launch_cb); const char * self = skynet_command(ctx, "REG", NULL); uint32_t handle_id = strtoul(self+1, NULL, 16); // it must be first message @@ -144,11 +157,33 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { return 0; } +static void * +lalloc(void * ud, void *ptr, size_t osize, size_t nsize) { + struct snlua *l = ud; + size_t mem = l->mem; + l->mem += nsize; + if (ptr) + l->mem -= osize; + if (l->mem_limit != 0 && l->mem > l->mem_limit) { + if (ptr == NULL || nsize > osize) { + l->mem = mem; + return NULL; + } + } + if (l->mem > l->mem_report) { + l->mem_report *= 2; + skynet_error(l->ctx, "Memory warning %.2f M", (float)l->mem / (1024 * 1024)); + } + return skynet_lalloc(ptr, osize, nsize); +} + struct snlua * snlua_create(void) { struct snlua * l = skynet_malloc(sizeof(*l)); memset(l,0,sizeof(*l)); - l->L = lua_newstate(skynet_lalloc, NULL); + l->mem_report = MEMORY_WARNING_REPORT; + l->mem_limit = 0; + l->L = lua_newstate(lalloc, l); return l; } @@ -157,3 +192,16 @@ snlua_release(struct snlua *l) { lua_close(l->L); skynet_free(l); } + +void +snlua_signal(struct snlua *l, int signal) { + skynet_error(l->ctx, "recv a signal %d", signal); + if (signal == 0) { +#ifdef lua_checksig + // If our lua support signal (modified lua version by skynet), trigger it. + skynet_sig_L = l->L; +#endif + } else if (signal == 1) { + skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); + } +} diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 51a57999..3dabfd8a 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,28 +1,48 @@ local skynet = require "skynet" +local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.launch, ... +local memory = require "skynet.memory" skynet.start(function() - assert(skynet.launch("logger", skynet.getenv "logger")) + local sharestring = tonumber(skynet.getenv "sharestring" or 4096) + memory.ssexpand(sharestring) local standalone = skynet.getenv "standalone" - local master_addr = skynet.getenv "master" - - if standalone then - assert(skynet.launch("master", master_addr)) - end - - local local_addr = skynet.getenv "address" - local harbor_id = skynet.getenv "harbor" - - assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) local launcher = assert(skynet.launch("snlua","launcher")) skynet.name(".launcher", launcher) + local harbor_id = tonumber(skynet.getenv "harbor" or 0) + if harbor_id == 0 then + assert(standalone == nil) + standalone = true + skynet.setenv("standalone", "true") + + local ok, slave = pcall(skynet.newservice, "cdummy") + if not ok then + skynet.abort() + end + skynet.name(".cslave", slave) + + else + if standalone then + if not pcall(skynet.newservice,"cmaster") then + skynet.abort() + end + end + + local ok, slave = pcall(skynet.newservice, "cslave") + if not ok then + skynet.abort() + end + skynet.name(".cslave", slave) + end + if standalone then - local datacenter = assert(skynet.newservice "datacenterd") + local datacenter = skynet.newservice "datacenterd" skynet.name("DATACENTER", datacenter) end - assert(skynet.newservice "service_mgr") - assert(skynet.newservice(skynet.getenv "start" or "main")) + skynet.newservice "service_mgr" + pcall(skynet.newservice,skynet.getenv "start" or "main") skynet.exit() end) diff --git a/service/cdummy.lua b/service/cdummy.lua new file mode 100644 index 00000000..7926e35e --- /dev/null +++ b/service/cdummy.lua @@ -0,0 +1,81 @@ +local skynet = require "skynet" +require "skynet.manager" -- import skynet.launch, ... + +local globalname = {} +local queryname = {} +local harbor = {} +local harbor_service + +skynet.register_protocol { + name = "harbor", + id = skynet.PTYPE_HARBOR, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +local function response_name(name) + local address = globalname[name] + if queryname[name] then + local tmp = queryname[name] + queryname[name] = nil + for _,resp in ipairs(tmp) do + resp(true, address) + end + end +end + +function harbor.REGISTER(name, handle) + assert(globalname[name] == nil) + globalname[name] = handle + response_name(name) + skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) +end + +function harbor.QUERYNAME(name) + if name:byte() == 46 then -- "." , local name + skynet.ret(skynet.pack(skynet.localname(name))) + return + end + local result = globalname[name] + if result then + skynet.ret(skynet.pack(result)) + return + end + local queue = queryname[name] + if queue == nil then + queue = { skynet.response() } + queryname[name] = queue + else + table.insert(queue, skynet.response()) + end +end + +function harbor.LINK(id) + skynet.ret() +end + +function harbor.CONNECT(id) + skynet.error("Can't connect to other harbor in single node mode") +end + +skynet.start(function() + local harbor_id = tonumber(skynet.getenv "harbor") + assert(harbor_id == 0) + + skynet.dispatch("lua", function (session,source,command,...) + local f = assert(harbor[command]) + f(...) + end) + skynet.dispatch("text", function(session,source,command) + -- ignore all the command + end) + + harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) +end) diff --git a/service/clusterd.lua b/service/clusterd.lua new file mode 100644 index 00000000..595b7973 --- /dev/null +++ b/service/clusterd.lua @@ -0,0 +1,205 @@ +local skynet = require "skynet" +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 command = {} + +local function read_response(sock) + local sz = socket.header(sock:read(2)) + local msg = sock:read(sz) + return cluster.unpackresponse(msg) -- session, ok, data, padding +end + +local function open_channel(t, key) + local host, port = string.match(node_address[key], "([^:]+):(.*)$") + local c = sc.channel { + host = host, + port = tonumber(port), + response = read_response, + nodelay = true, + } + assert(c:connect(true)) + t[key] = c + return c +end + +local node_channel = setmetatable({}, { __index = open_channel }) + +local function loadconfig(tmp) + if tmp == nil then + tmp = {} + if config_name then + local f = assert(io.open(config_name)) + local source = f:read "*a" + f:close() + assert(load(source, "@"..config_name, "t", tmp))() + end + end + for name,address in pairs(tmp) do + assert(type(address) == "string") + if node_address[name] ~= address then + -- address changed + if rawget(node_channel, name) then + node_channel[name] = nil -- reset connection + end + node_address[name] = address + end + end +end + +function command.reload(source, config) + loadconfig(config) + skynet.ret(skynet.pack(nil)) +end + +function command.listen(source, addr, port) + local gate = skynet.newservice("gate") + if port == nil then + addr, port = string.match(node_address[addr], "([^:]+):(.*)$") + end + skynet.call(gate, "lua", "open", { address = addr, port = 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] + + return c:request(request, session, padding) +end + +function command.req(...) + local ok, msg, sz = 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. +end + +local proxy = {} + +function command.proxy(source, node, name) + local fullname = node .. "." .. name + if proxy[fullname] == nil then + proxy[fullname] = skynet.newservice("clusterproxy", node, name) + end + skynet.ret(skynet.pack(proxy[fullname])) +end + +local register_name = {} + +function command.register(source, name, addr) + assert(register_name[name] == nil) + addr = addr or source + local old_name = register_name[addr] + if old_name then + register_name[old_name] = nil + end + register_name[addr] = name + register_name[name] = addr + skynet.ret(nil) + skynet.error(string.format("Register [%s] :%08x", name, addr)) +end + +local large_request = {} + +function command.socket(source, subcmd, fd, msg) + if subcmd == "data" then + local sz + local addr, session, msg, padding, is_push = cluster.unpackrequest(msg) + if padding then + local req = large_request[session] or { addr = addr , is_push = is_push } + large_request[session] = req + table.insert(req, msg) + return + else + local req = large_request[session] + if req then + large_request[session] = nil + table.insert(req, msg) + msg,sz = cluster.concat(req) + addr = req.addr + is_push = req.is_push + end + if not msg then + local response = cluster.packresponse(session, false, "Invalid large req") + socket.write(fd, response) + return + end + end + local ok, response + if addr == 0 then + local name = skynet.unpack(msg, sz) + local addr = register_name[name] + if addr then + ok = true + msg, sz = skynet.pack(addr) + else + ok = false + msg = "name not found" + end + elseif is_push then + skynet.rawsend(addr, "lua", msg, sz) + return -- no response + else + ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) + end + if ok then + response = cluster.packresponse(session, true, msg, sz) + if type(response) == "table" then + for _, v in ipairs(response) do + socket.lwrite(fd, v) + end + else + socket.write(fd, response) + end + else + response = cluster.packresponse(session, false, msg) + socket.write(fd, response) + end + elseif subcmd == "open" then + skynet.error(string.format("socket accept from %s", msg)) + skynet.call(source, "lua", "accept", fd) + else + large_request = {} + skynet.error(string.format("socket %s %d : %s", subcmd, fd, msg)) + end +end + +skynet.start(function() + loadconfig() + skynet.dispatch("lua", function(session , source, cmd, ...) + local f = assert(command[cmd]) + f(source, ...) + end) +end) diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua new file mode 100644 index 00000000..e0cc5daa --- /dev/null +++ b/service/clusterproxy.lua @@ -0,0 +1,32 @@ +local skynet = require "skynet" +local cluster = require "skynet.cluster" +require "skynet.manager" -- inject skynet.forward_type + +local node, address = ... + +skynet.register_protocol { + name = "system", + id = skynet.PTYPE_SYSTEM, + unpack = function (...) return ... end, +} + +local forward_map = { + [skynet.PTYPE_SNAX] = skynet.PTYPE_SYSTEM, + [skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM, + [skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message +} + +skynet.forward_type( forward_map ,function() + local clusterd = skynet.uniqueservice("clusterd") + local n = tonumber(address) + if n then + address = n + end + skynet.dispatch("system", function (session, source, msg, sz) + if session == 0 then + skynet.send(clusterd, "lua", "push", node, address, msg, sz) + else + skynet.ret(skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz))) + end + end) +end) diff --git a/service/cmaster.lua b/service/cmaster.lua new file mode 100644 index 00000000..91c75b4b --- /dev/null +++ b/service/cmaster.lua @@ -0,0 +1,125 @@ +local skynet = require "skynet" +local socket = require "skynet.socket" + +--[[ + master manage data : + 1. all the slaves address : id -> ipaddr:port + 2. all the global names : name -> address + + master hold connections from slaves . + + protocol slave->master : + package size 1 byte + type 1 byte : + 'H' : HANDSHAKE, report slave id, and address. + 'R' : REGISTER name address + 'Q' : QUERY name + + + protocol master->slave: + package size 1 byte + type 1 byte : + 'W' : WAIT n + 'C' : CONNECT slave_id slave_address + 'N' : NAME globalname address + 'D' : DISCONNECT slave_id +]] + +local slave_node = {} +local global_name = {} + +local function read_package(fd) + local sz = socket.read(fd, 1) + assert(sz, "closed") + sz = string.byte(sz) + local content = assert(socket.read(fd, sz), "closed") + return skynet.unpack(content) +end + +local function pack_package(...) + local message = skynet.packstring(...) + local size = #message + assert(size <= 255 , "too long") + return string.char(size) .. message +end + +local function report_slave(fd, slave_id, slave_addr) + local message = pack_package("C", slave_id, slave_addr) + local n = 0 + for k,v in pairs(slave_node) do + if v.fd ~= 0 then + socket.write(v.fd, message) + n = n + 1 + end + end + socket.write(fd, pack_package("W", n)) +end + +local function handshake(fd) + local t, slave_id, slave_addr = read_package(fd) + assert(t=='H', "Invalid handshake type " .. t) + assert(slave_id ~= 0 , "Invalid slave id 0") + if slave_node[slave_id] then + error(string.format("Slave %d already register on %s", slave_id, slave_node[slave_id].addr)) + end + report_slave(fd, slave_id, slave_addr) + slave_node[slave_id] = { + fd = fd, + id = slave_id, + addr = slave_addr, + } + return slave_id , slave_addr +end + +local function dispatch_slave(fd) + local t, name, address = read_package(fd) + if t == 'R' then + -- register name + assert(type(address)=="number", "Invalid request") + if not global_name[name] then + global_name[name] = address + end + local message = pack_package("N", name, address) + for k,v in pairs(slave_node) do + socket.write(v.fd, message) + end + elseif t == 'Q' then + -- query name + local address = global_name[name] + if address then + socket.write(fd, pack_package("N", name, address)) + end + else + skynet.error("Invalid slave message type " .. t) + end +end + +local function monitor_slave(slave_id, slave_address) + local fd = slave_node[slave_id].fd + skynet.error(string.format("Harbor %d (fd=%d) report %s", slave_id, fd, slave_address)) + while pcall(dispatch_slave, fd) do end + skynet.error("slave " ..slave_id .. " is down") + local message = pack_package("D", slave_id) + slave_node[slave_id].fd = 0 + for k,v in pairs(slave_node) do + socket.write(v.fd, message) + end + socket.close(fd) +end + +skynet.start(function() + local master_addr = skynet.getenv "standalone" + skynet.error("master listen socket " .. tostring(master_addr)) + local fd = socket.listen(master_addr) + socket.start(fd , function(id, addr) + skynet.error("connect from " .. addr .. " " .. id) + socket.start(id) + local ok, slave, slave_addr = pcall(handshake, id) + if ok then + skynet.fork(monitor_slave, slave, slave_addr) + else + skynet.error(string.format("disconnect fd = %d, error = %s", id, slave)) + socket.close(id) + end + end) +end) diff --git a/service/cmemory.lua b/service/cmemory.lua index 06627ae3..2d429b3c 100644 --- a/service/cmemory.lua +++ b/service/cmemory.lua @@ -1,8 +1,12 @@ local skynet = require "skynet" -local memory = require "memory" +local memory = require "skynet.memory" memory.dumpinfo() -memory.dump() +--memory.dump() +local info = memory.info() +for k,v in pairs(info) do + print(string.format(":%08x %gK",k,v/1024)) +end print("Total memory:", memory.total()) print("Total block:", memory.block()) diff --git a/service/console.lua b/service/console.lua index 86bf6cc1..1a7bc343 100644 --- a/service/console.lua +++ b/service/console.lua @@ -1,19 +1,27 @@ local skynet = require "skynet" -local socket = require "socket" +local snax = require "skynet.snax" +local socket = require "skynet.socket" + +local function split_cmdline(cmdline) + local split = {} + for i in string.gmatch(cmdline, "%S+") do + table.insert(split,i) + end + return split +end local function console_main_loop() local stdin = socket.stdin() - socket.lock(stdin) while true do local cmdline = socket.readline(stdin, "\n") - if cmdline ~= "" then - local handle = skynet.newservice(cmdline) - if handle == nil then - print("Launch error:",cmdline) - end + local split = split_cmdline(cmdline) + local command = split[1] + if command == "snax" then + pcall(snax.newservice, select(2, table.unpack(split))) + elseif cmdline ~= "" then + pcall(skynet.newservice, cmdline) end end - socket.unlock(stdin) end skynet.start(function() diff --git a/service/cslave.lua b/service/cslave.lua new file mode 100644 index 00000000..452dbaf2 --- /dev/null +++ b/service/cslave.lua @@ -0,0 +1,267 @@ +local skynet = require "skynet" +local socket = require "skynet.socket" +require "skynet.manager" -- import skynet.launch, ... +local table = table + +local slaves = {} +local connect_queue = {} +local globalname = {} +local queryname = {} +local harbor = {} +local harbor_service +local monitor = {} +local monitor_master_set = {} + +local function read_package(fd) + local sz = socket.read(fd, 1) + assert(sz, "closed") + sz = string.byte(sz) + local content = assert(socket.read(fd, sz), "closed") + return skynet.unpack(content) +end + +local function pack_package(...) + local message = skynet.packstring(...) + local size = #message + assert(size <= 255 , "too long") + return string.char(size) .. message +end + +local function monitor_clear(id) + local v = monitor[id] + if v then + monitor[id] = nil + for _, v in ipairs(v) do + v(true) + end + end +end + +local function connect_slave(slave_id, address) + local ok, err = pcall(function() + if slaves[slave_id] == nil then + local fd = assert(socket.open(address), "Can't connect to "..address) + skynet.error(string.format("Connect to harbor %d (fd=%d), %s", slave_id, fd, address)) + slaves[slave_id] = fd + monitor_clear(slave_id) + socket.abandon(fd) + skynet.send(harbor_service, "harbor", string.format("S %d %d",fd,slave_id)) + end + end) + if not ok then + skynet.error(err) + end +end + +local function ready() + local queue = connect_queue + connect_queue = nil + for k,v in pairs(queue) do + connect_slave(k,v) + end + for name,address in pairs(globalname) do + skynet.redirect(harbor_service, address, "harbor", 0, "N " .. name) + end +end + +local function response_name(name) + local address = globalname[name] + if queryname[name] then + local tmp = queryname[name] + queryname[name] = nil + for _,resp in ipairs(tmp) do + resp(true, address) + end + end +end + +local function monitor_master(master_fd) + while true do + local ok, t, id_name, address = pcall(read_package,master_fd) + if ok then + if t == 'C' then + if connect_queue then + connect_queue[id_name] = address + else + connect_slave(id_name, address) + end + elseif t == 'N' then + globalname[id_name] = address + response_name(id_name) + if connect_queue == nil then + skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name) + end + elseif t == 'D' then + local fd = slaves[id_name] + slaves[id_name] = false + if fd then + monitor_clear(id_name) + socket.close(fd) + end + end + else + skynet.error("Master disconnect") + for _, v in ipairs(monitor_master_set) do + v(true) + end + socket.close(master_fd) + break + end + end +end + +local function accept_slave(fd) + socket.start(fd) + local id = socket.read(fd, 1) + if not id then + skynet.error(string.format("Connection (fd =%d) closed", fd)) + socket.close(fd) + return + end + id = string.byte(id) + if slaves[id] ~= nil then + skynet.error(string.format("Slave %d exist (fd =%d)", id, fd)) + socket.close(fd) + return + end + slaves[id] = fd + monitor_clear(id) + socket.abandon(fd) + skynet.error(string.format("Harbor %d connected (fd = %d)", id, fd)) + skynet.send(harbor_service, "harbor", string.format("A %d %d", fd, id)) +end + +skynet.register_protocol { + name = "harbor", + id = skynet.PTYPE_HARBOR, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +local function monitor_harbor(master_fd) + return function(session, source, command) + local t = string.sub(command, 1, 1) + local arg = string.sub(command, 3) + if t == 'Q' then + -- query name + if globalname[arg] then + skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg) + else + socket.write(master_fd, pack_package("Q", arg)) + end + elseif t == 'D' then + -- harbor down + local id = tonumber(arg) + if slaves[id] then + monitor_clear(id) + end + slaves[id] = false + else + skynet.error("Unknown command ", command) + end + end +end + +function harbor.REGISTER(fd, name, handle) + assert(globalname[name] == nil) + globalname[name] = handle + response_name(name) + socket.write(fd, pack_package("R", name, handle)) + skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) +end + +function harbor.LINK(fd, id) + if slaves[id] then + if monitor[id] == nil then + monitor[id] = {} + end + table.insert(monitor[id], skynet.response()) + else + skynet.ret() + end +end + +function harbor.LINKMASTER() + table.insert(monitor_master_set, skynet.response()) +end + +function harbor.CONNECT(fd, id) + if not slaves[id] then + if monitor[id] == nil then + monitor[id] = {} + end + table.insert(monitor[id], skynet.response()) + else + skynet.ret() + end +end + +function harbor.QUERYNAME(fd, name) + if name:byte() == 46 then -- "." , local name + skynet.ret(skynet.pack(skynet.localname(name))) + return + end + local result = globalname[name] + if result then + skynet.ret(skynet.pack(result)) + return + end + local queue = queryname[name] + if queue == nil then + socket.write(fd, pack_package("Q", name)) + queue = { skynet.response() } + queryname[name] = queue + else + table.insert(queue, skynet.response()) + end +end + +skynet.start(function() + local master_addr = skynet.getenv "master" + local harbor_id = tonumber(skynet.getenv "harbor") + local slave_address = assert(skynet.getenv "address") + local slave_fd = socket.listen(slave_address) + skynet.error("slave connect to master " .. tostring(master_addr)) + local master_fd = assert(socket.open(master_addr), "Can't connect to master") + + skynet.dispatch("lua", function (_,_,command,...) + local f = assert(harbor[command]) + f(master_fd, ...) + end) + skynet.dispatch("text", monitor_harbor(master_fd)) + + harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) + + local hs_message = pack_package("H", harbor_id, slave_address) + socket.write(master_fd, hs_message) + local t, n = read_package(master_fd) + assert(t == "W" and type(n) == "number", "slave shakehand failed") + skynet.error(string.format("Waiting for %d harbors", n)) + skynet.fork(monitor_master, master_fd) + if n > 0 then + local co = coroutine.running() + socket.start(slave_fd, function(fd, addr) + skynet.error(string.format("New connection (fd = %d, %s)",fd, addr)) + if pcall(accept_slave,fd) then + local s = 0 + for k,v in pairs(slaves) do + s = s + 1 + end + if s >= n then + skynet.wakeup(co) + end + end + end) + skynet.wait() + end + socket.close(slave_fd) + skynet.error("Shakehand ready") + skynet.fork(ready) +end) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index ad431ae2..8a7b65a0 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -2,6 +2,8 @@ local skynet = require "skynet" local command = {} local database = {} +local wait_queue = {} +local mode = {} local function query(db, key, ...) if key == nil then @@ -22,7 +24,7 @@ local function update(db, key, value, ...) if select("#",...) == 0 then local ret = db[key] db[key] = value - return ret + return ret, value else if db[key] == nil then db[key] = {} @@ -31,13 +33,78 @@ local function update(db, key, value, ...) end end +local function wakeup(db, key1, ...) + if key1 == nil then + return + end + local q = db[key1] + if q == nil then + return + end + if q[mode] == "queue" then + db[key1] = nil + if select("#", ...) ~= 1 then + -- throw error because can't wake up a branch + for _,response in ipairs(q) do + response(false) + end + else + return q + end + else + -- it's branch + return wakeup(q , ...) + end +end + function command.UPDATE(...) - return update(database, ...) + local ret, value = update(database, ...) + if ret or value == nil then + return ret + end + local q = wakeup(wait_queue, ...) + if q then + for _, response in ipairs(q) do + response(true,value) + end + end +end + +local function waitfor(db, key1, key2, ...) + if key2 == nil then + -- push queue + local q = db[key1] + if q == nil then + q = { [mode] = "queue" } + db[key1] = q + else + assert(q[mode] == "queue") + end + table.insert(q, skynet.response()) + else + local q = db[key1] + if q == nil then + q = { [mode] = "branch" } + db[key1] = q + else + assert(q[mode] == "branch") + end + return waitfor(q, key2, ...) + end end skynet.start(function() - skynet.dispatch("lua", function (_, source, cmd, ...) - local f = assert(command[cmd]) - skynet.ret(skynet.pack(f(...))) + skynet.dispatch("lua", function (_, _, cmd, ...) + if cmd == "WAIT" then + local ret = command.QUERY(...) + if ret then + skynet.ret(skynet.pack(ret)) + else + waitfor(wait_queue, ...) + end + else + local f = assert(command[cmd]) + skynet.ret(skynet.pack(f(...))) + end end) end) diff --git a/service/dbg.lua b/service/dbg.lua index 25b630ef..5d5b7bf8 100644 --- a/service/dbg.lua +++ b/service/dbg.lua @@ -35,7 +35,7 @@ local function dump_list(list) end skynet.start(function() - local list = skynet.call(".launcher","lua", unpack(cmd)) + local list = skynet.call(".launcher","lua", table.unpack(cmd)) if list then dump_list(list) end diff --git a/service/debug_agent.lua b/service/debug_agent.lua new file mode 100644 index 00000000..500a34ee --- /dev/null +++ b/service/debug_agent.lua @@ -0,0 +1,36 @@ +local skynet = require "skynet" +local debugchannel = require "skynet.debugchannel" + +local CMD = {} + +local channel + +function CMD.start(address, fd) + assert(channel == nil, "start more than once") + skynet.error(string.format("Attach to :%08x", address)) + local handle + channel, handle = debugchannel.create() + local ok, err = pcall(skynet.call, address, "debug", "REMOTEDEBUG", fd, handle) + if not ok then + skynet.ret(skynet.pack(false, "Debugger attach failed")) + else + -- todo hook + skynet.ret(skynet.pack(true)) + end + skynet.exit() +end + +function CMD.cmd(cmdline) + channel:write(cmdline) +end + +function CMD.ping() + skynet.ret() +end + +skynet.start(function() + skynet.dispatch("lua", function(_,_,cmd,...) + local f = CMD[cmd] + f(...) + end) +end) diff --git a/service/debug_console.lua b/service/debug_console.lua index d49dd809..33cc06ee 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -1,17 +1,26 @@ local skynet = require "skynet" local codecache = require "skynet.codecache" -local socket = require "socket" -local snax = require "snax" +local core = require "skynet.core" +local socket = require "skynet.socket" +local snax = require "skynet.snax" +local memory = require "skynet.memory" +local httpd = require "http.httpd" +local sockethelper = require "http.sockethelper" + +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 port = tonumber(...) local COMMAND = {} +local COMMANDX = {} local function format_table(t) local index = {} for k in pairs(t) do table.insert(index, k) end - table.sort(index) + table.sort(index, function(a, b) return tostring(a) < tostring(b) end) local result = {} for _,v in ipairs(index) do table.insert(result, string.format("%s:%s",v,tostring(t[v]))) @@ -32,7 +41,7 @@ local function dump_list(print, list) for k in pairs(list) do table.insert(index, k) end - table.sort(index) + table.sort(index, function(a, b) return tostring(a) < tostring(b) end) for _,v in ipairs(index) do dump_line(print, v, list[v]) end @@ -46,14 +55,22 @@ local function split_cmdline(cmdline) return split end -local function docmd(cmdline, print) +local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) - local cmd = COMMAND[split[1]] + local command = split[1] + local cmd = COMMAND[command] local ok, list if cmd then - ok, list = pcall(cmd, select(2,table.unpack(split))) + ok, list = pcall(cmd, table.unpack(split,2)) else - ok, list = pcall(skynet.call,".launcher","lua", table.unpack(split)) + cmd = COMMANDX[command] + if cmd then + split.fd = fd + split[1] = cmdline + ok, list = pcall(cmd, split) + else + print("Invalid command, type help for command list") + end end if ok then @@ -63,35 +80,51 @@ local function docmd(cmdline, print) else dump_list(print, list) end - else - print("OK") end + print("") else - print("Error:", list) + print(list) + print("") end end local function console_main_loop(stdin, print) - socket.lock(stdin) print("Welcome to skynet console") - while true do - local cmdline = socket.readline(stdin, "\n") - if not cmdline then - break - end - if cmdline ~= "" then - docmd(cmdline, print) + skynet.error(stdin, "connected") + local ok, err = pcall(function() + while true do + local cmdline = socket.readline(stdin, "\n") + if not cmdline then + break + end + if cmdline:sub(1,4) == "GET " then + -- http + local code, url = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) + local cmdline = url:sub(2):gsub("/"," ") + docmd(cmdline, print, stdin) + break + end + if cmdline ~= "" then + docmd(cmdline, print, stdin) + end end + end) + if not ok then + skynet.error(stdin, err) end - socket.unlock(stdin) + skynet.error(stdin, "disconnected") + socket.close(stdin) end skynet.start(function() - local listen_socket = socket.listen ("127.0.0.1", port) - print("Start debug console at 127.0.0.1",port) + local listen_socket = socket.listen (ip, port) + skynet.error("Start debug console at " .. ip .. ":" .. port) socket.start(listen_socket , function(id, addr) local function print(...) local t = { ... } + for k,v in ipairs(t) do + t[k] = tostring(v) + end socket.write(id, table.concat(t,"\t")) socket.write(id, "\n") end @@ -105,7 +138,8 @@ function COMMAND.help() help = "This help message", list = "List all the service", stat = "Dump all stats", - info = "Info address : get service infomation", + info = "info address : get service infomation", + exit = "exit address : kill a lua service", kill = "kill address : kill service", mem = "mem : show memory status", gc = "gc : force every lua service do garbage collect", @@ -113,6 +147,17 @@ function COMMAND.help() snax = "lanuch a new snax service", clearcache = "clear lua code cache", service = "List unique service", + task = "task address : show service task detail", + inject = "inject address luascript.lua", + logon = "logon address", + logoff = "logoff address", + log = "launch a new lua service with log", + debug = "debug address : debug a lua service", + signal = "signal address sig", + cmem = "Show C memory info", + shrtbl = "Show shared short string table info", + ping = "ping address", + call = "call address ...", } end @@ -121,17 +166,34 @@ function COMMAND.clearcache() end function COMMAND.start(...) - local addr = skynet.newservice(...) - if addr then - return { [skynet.address(addr)] = ... } + local ok, addr = pcall(skynet.newservice, ...) + if ok then + if addr then + return { [skynet.address(addr)] = ... } + else + return "Exit" + end + else + return "Failed" + end +end + +function COMMAND.log(...) + local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) + if ok then + if addr then + return { [skynet.address(addr)] = ... } + else + return "Failed" + end else return "Failed" end end function COMMAND.snax(...) - local s = snax.newservice(...) - if s then + local ok, s = pcall(snax.newservice, ...) + if ok then local addr = s.handle return { [skynet.address(addr)] = ... } else @@ -142,3 +204,153 @@ end function COMMAND.service() return skynet.call("SERVICE", "lua", "LIST") end + +local function adjust_address(address) + if address:sub(1,1) ~= ":" then + address = assert(tonumber("0x" .. address), "Need an address") | (skynet.harbor(skynet.self()) << 24) + end + return address +end + +function COMMAND.list() + return skynet.call(".launcher", "lua", "LIST") +end + +function COMMAND.stat() + return skynet.call(".launcher", "lua", "STAT") +end + +function COMMAND.mem() + return skynet.call(".launcher", "lua", "MEM") +end + +function COMMAND.kill(address) + return skynet.call(".launcher", "lua", "KILL", address) +end + +function COMMAND.gc() + return skynet.call(".launcher", "lua", "GC") +end + +function COMMAND.exit(address) + skynet.send(adjust_address(address), "debug", "EXIT") +end + +function COMMAND.inject(address, filename) + address = adjust_address(address) + local f = io.open(filename, "rb") + if not f then + return "Can't open " .. filename + end + local source = f:read "*a" + f:close() + local ok, output = skynet.call(address, "debug", "RUN", source, filename) + if ok == false then + error(output) + end + return output +end + +function COMMAND.task(address) + address = adjust_address(address) + return skynet.call(address,"debug","TASK") +end + +function COMMAND.info(address, ...) + address = adjust_address(address) + return skynet.call(address,"debug","INFO", ...) +end + +function COMMANDX.debug(cmd) + local address = adjust_address(cmd[2]) + local agent = skynet.newservice "debug_agent" + local stop + local term_co = coroutine.running() + local function forward_cmd() + repeat + -- notice : It's a bad practice to call socket.readline from two threads (this one and console_main_loop), be careful. + skynet.call(agent, "lua", "ping") -- detect agent alive, if agent exit, raise error + local cmdline = socket.readline(cmd.fd, "\n") + cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") + if not cmdline then + skynet.send(agent, "lua", "cmd", "cont") + break + end + skynet.send(agent, "lua", "cmd", cmdline) + until stop or cmdline == "cont" + end + skynet.fork(function() + pcall(forward_cmd) + if not stop then -- block at skynet.call "start" + term_co = nil + else + skynet.wakeup(term_co) + end + end) + local ok, err = skynet.call(agent, "lua", "start", address, cmd.fd) + stop = true + if term_co then + -- wait for fork coroutine exit. + skynet.wait(term_co) + end + + if not ok then + error(err) + end +end + +function COMMAND.logon(address) + address = adjust_address(address) + core.command("LOGON", skynet.address(address)) +end + +function COMMAND.logoff(address) + address = adjust_address(address) + core.command("LOGOFF", skynet.address(address)) +end + +function COMMAND.signal(address, sig) + address = skynet.address(adjust_address(address)) + if sig then + core.command("SIGNAL", string.format("%s %d",address,sig)) + else + core.command("SIGNAL", address) + end +end + +function COMMAND.cmem() + local info = memory.info() + local tmp = {} + for k,v in pairs(info) do + tmp[skynet.address(k)] = v + end + tmp.total = memory.total() + tmp.block = memory.block() + + return tmp +end + +function COMMAND.shrtbl() + local n, total, longest, space = memory.ssinfo() + return { n = n, total = total, longest = longest, space = space } +end + +function COMMAND.ping(address) + address = adjust_address(address) + local ti = skynet.now() + skynet.call(address, "debug", "PING") + ti = skynet.now() - ti + return tostring(ti) +end + +function COMMANDX.call(cmd) + local address = adjust_address(cmd[2]) + local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments") + local args_func = assert(load("return " .. cmdline, "debug console", "t", {}), "Invalid arguments") + local args = table.pack(pcall(args_func)) + if not args[1] then + error(args[2]) + end + local rets = table.pack(skynet.call(address, "lua", table.unpack(args, 2, args.n))) + return rets +end diff --git a/service/gate.lua b/service/gate.lua index 54bb3e91..e3a210cc 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,31 +1,40 @@ local skynet = require "skynet" -local netpack = require "netpack" -local socketdriver = require "socketdriver" +local gateserver = require "snax.gateserver" +local netpack = require "skynet.netpack" -local socket -local queue local watchdog -local maxclient -local client_number = 0 -local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) - local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } local forwarding = {} -- agent -> connection -function CMD.open( source , conf ) - assert(not socket) - local address = conf.address or "0.0.0.0" - local port = assert(conf.port) - maxclient = conf.maxclient or 1024 +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, +} + +local handler = {} + +function handler.open(source, conf) watchdog = conf.watchdog or source - socket = socketdriver.listen(address, port) - socketdriver.start(socket) end -function CMD.close() - assert(socket) - socketdriver.close(socket) - socket = nil +function handler.message(fd, msg, sz) + -- recv a package, forward it + local c = connection[fd] + local agent = c.agent + if agent then + skynet.redirect(agent, c.client, "client", 1, msg, sz) + else + skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) + end +end + +function handler.connect(fd, addr) + local c = { + fd = fd, + ip = addr, + } + connection[fd] = c + skynet.send(watchdog, "lua", "socket", "open", fd, addr) end local function unforward(c) @@ -36,120 +45,52 @@ local function unforward(c) end end -local function start(c) - if not c.mode then - c.mode = "open" - socketdriver.start(c.fd) +local function close_fd(fd) + local c = connection[fd] + if c then + unforward(c) + connection[fd] = nil end end +function handler.disconnect(fd) + close_fd(fd) + skynet.send(watchdog, "lua", "socket", "close", fd) +end + +function handler.error(fd, msg) + close_fd(fd) + skynet.send(watchdog, "lua", "socket", "error", fd, msg) +end + +function handler.warning(fd, size) + skynet.send(watchdog, "lua", "socket", "warning", fd, size) +end + +local CMD = {} + function CMD.forward(source, fd, client, address) local c = assert(connection[fd]) unforward(c) - start(c) - c.client = client or 0 c.agent = address or source - forwarding[c.agent] = c + gateserver.openclient(fd) end function CMD.accept(source, fd) local c = assert(connection[fd]) unforward(c) - start(c) + gateserver.openclient(fd) end function CMD.kick(source, fd) - local c - if fd then - c = connection[fd] - else - c = forwarding[source] - end - - assert(c) - - if c.mode ~= "close" then - c.mode = "close" - socketdriver.close(c.fd) - end + gateserver.closeclient(fd) end -local MSG = {} - -function MSG.data(fd, msg, sz) - -- recv a package, forward it - local c = connection[fd] - local agent = c.agent - if agent then - skynet.redirect(agent, c.client, "client", 0, msg, sz) - else - skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) - end +function handler.command(cmd, source, ...) + local f = assert(CMD[cmd]) + return f(source, ...) end -function MSG.more() - for fd, msg, sz in netpack.pop, queue do - MSG.data(fd, msg, sz) - end -end - -function MSG.open(fd, msg) - if client_number >= maxclient then - socketdriver.close(fd) - return - end - local c = { - fd = fd, - ip = msg, - } - connection[fd] = c - client_number = client_number + 1 - skynet.send(watchdog, "lua", "socket", "open", fd, msg) -end - -local function close_fd(fd, message) - local c = connection[fd] - if c then - unforward(c) - connection[fd] = nil - client_number = client_number - 1 - end -end - -function MSG.close(fd) - close_fd(fd) - skynet.send(watchdog, "lua", "socket", "close", fd) -end - -function MSG.error(fd, msg) - close_fd(fd) - skynet.send(watchdog, "lua", "socket", "error", fd, msg) -end - -skynet.register_protocol { - name = "socket", - id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 - unpack = function ( msg, sz ) - return netpack.filter( queue, msg, sz) - end, - dispatch = function (_, _, q, type, ...) - queue = q - if type then - MSG[type](...) - end - end -} - -skynet.register_protocol { - name = "client", - id = skynet.PTYPE_CLIENT, -} - -skynet.start(function() - skynet.dispatch("lua", function (_, address, cmd, ...) - local f = assert(CMD[cmd]) - skynet.ret(skynet.pack(f(address, ...))) - end) -end) +gateserver.start(handler) diff --git a/service/launcher.lua b/service/launcher.lua index b80eadc9..272a459a 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -1,4 +1,6 @@ local skynet = require "skynet" +local core = require "skynet.core" +require "skynet.manager" -- import manager apis local string = string local services = {} @@ -22,23 +24,16 @@ end function command.STAT() local list = {} for k,v in pairs(services) do - local stat = skynet.call(k,"debug","STAT") + local ok, stat = pcall(skynet.call,k,"debug","STAT") + if not ok then + stat = string.format("ERROR (%s)",v) + end list[skynet.address(k)] = stat end return list end -function command.INFO(_, _, handle) - handle = handle_to_address(handle) - if services[handle] == nil then - return - else - local result = skynet.call(handle,"debug","INFO") - return result - end -end - -function command.KILL(_, _, handle) +function command.KILL(_, handle) handle = handle_to_address(handle) skynet.kill(handle) local ret = { [skynet.address(handle)] = tostring(services[handle]) } @@ -49,8 +44,12 @@ end function command.MEM() local list = {} for k,v in pairs(services) do - local kb, bytes = skynet.call(k,"debug","MEM") - list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v) + local ok, kb, bytes = pcall(skynet.call,k,"debug","MEM") + if not ok then + list[skynet.address(k)] = string.format("ERROR (%s)",v) + else + list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) + end end return list end @@ -62,20 +61,42 @@ function command.GC() return command.MEM() end -function command.REMOVE(_,_, handle) +function command.REMOVE(_, handle, kill) services[handle] = nil + local response = instance[handle] + if response then + -- instance is dead + response(not kill) -- return nil to caller of newservice, when kill == false + instance[handle] = nil + end + -- don't return (skynet.ret) because the handle may exit return NORET end -function command.LAUNCH(address, session, service, ...) +local function launch_service(service, ...) local param = table.concat({...}, " ") local inst = skynet.launch(service, param) + local response = skynet.response() if inst then services[inst] = service .. " " .. param - instance[inst] = { session = session, address = address } + instance[inst] = response else - skynet.ret("") -- launch failed + response(false) + return + end + return inst +end + +function command.LAUNCH(_, service, ...) + launch_service(service, ...) + return NORET +end + +function command.LOGLAUNCH(_, service, ...) + local inst = launch_service(service, ...) + if inst then + core.command("LOGON", skynet.address(inst)) end return NORET end @@ -83,9 +104,9 @@ end function command.ERROR(address) -- see serivce-src/service_lua.c -- init failed - local reply = instance[address] - if reply then - skynet.redirect(reply.address , 0, "response", reply.session, "") + local response = instance[address] + if response then + response(false) instance[address] = nil end services[address] = nil @@ -94,9 +115,9 @@ end function command.LAUNCHOK(address) -- init notice - local reply = instance[address] - if reply then - skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address)) + local response = instance[address] + if response then + response(true, address) instance[address] = nil end @@ -115,9 +136,7 @@ skynet.register_protocol { elseif cmd == "ERROR" then command.ERROR(address) else - -- launch request - local service, param = string.match(cmd,"([^ ]+) (.*)") - command.LAUNCH(address, session, service, param) + error ("Invalid text command " .. cmd) end end, } @@ -126,7 +145,7 @@ skynet.dispatch("lua", function(session, address, cmd , ...) cmd = string.upper(cmd) local f = command[cmd] if f then - local ret = f(address, session, ...) + local ret = f(address, ...) if ret ~= NORET then skynet.ret(skynet.pack(ret)) end diff --git a/service/multicastd.lua b/service/multicastd.lua index 66e4f1c4..bcc5ea97 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local mc = require "multicast.c" -local datacenter = require "datacenter" +local mc = require "skynet.multicast.core" +local datacenter = require "skynet.datacenter" local harbor_id = skynet.harbor(skynet.self()) @@ -50,8 +50,10 @@ function command.DEL(source, c) channel[c] = nil channel_n[c] = nil channel_remote[c] = nil - for node in pairs(remote) do - skynet.send(node_address[node], "lua", "DELR", c) + if remote then + for node in pairs(remote) do + skynet.send(node_address[node], "lua", "DELR", c) + end end return NORET end @@ -64,26 +66,29 @@ end -- publish a message, for local node, use the message pointer (call mc.bind to add the reference) -- for remote node, call remote_publish. (call mc.unpack and skynet.tostring to convert message pointer to string) local function publish(c , source, pack, size) - local group = channel[c] - if group == nil then - -- dead channel, delete the pack - mc.bind(pack, 1) - mc.close(pack) - return - end - mc.bind(pack, channel_n[c]) - local msg = skynet.tostring(pack, size) - for k in pairs(group) do - skynet.redirect(k, source, "multicast", c , msg) - end local remote = channel_remote[c] if remote then + -- remote publish should unpack the pack, because we should not publish the pointer out. local _, msg, sz = mc.unpack(pack, size) local msg = skynet.tostring(msg,sz) for node in pairs(remote) do remote_publish(node, c, source, msg) end end + + local group = channel[c] + if group == nil or next(group) == nil then + -- dead channel, delete the pack. mc.bind returns the pointer in pack and free the pack (struct mc_package **) + local pack = mc.bind(pack, 1) + mc.close(pack) + return + end + local msg = skynet.tostring(pack, size) -- copy (pack,size) to a string + mc.bind(pack, channel_n[c]) -- mc.bind will free the pack(struct mc_package **) + for k in pairs(group) do + -- the msg is a pointer to the real message, publish pointer in local is ok. + skynet.redirect(k, source, "multicast", c , msg) + end end skynet.register_protocol { diff --git a/service/service_mgr.lua b/service/service_mgr.lua index f49a79f7..dcd901fd 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" -local snax = require "snax" +require "skynet.manager" -- import skynet.register +local snax = require "skynet.snax" local cmd = {} local service = {} @@ -117,7 +118,6 @@ local function register_global() function cmd.REPORT(m) mgr[m] = true - skynet.watch(m) end local function add_list(all, m) @@ -142,14 +142,23 @@ local function register_global() end local function register_local() - function cmd.GLAUNCH(name, ...) + local function waitfor_remote(cmd, name, ...) local global_name = "@" .. name - return waitfor(global_name, skynet.call, "SERVICE", "lua", "LAUNCH", global_name, ...) + local local_name + if name == "snaxd" then + local_name = global_name .. "." .. (...) + else + local_name = global_name + end + return waitfor(local_name, skynet.call, "SERVICE", "lua", cmd, global_name, ...) end - function cmd.GQUERY(name, ...) - local global_name = "@" .. name - return waitfor(global_name, skynet.call, "SERVICE", "lua", "QUERY", global_name, ...) + function cmd.GLAUNCH(...) + return waitfor_remote("LAUNCH", ...) + end + + function cmd.GQUERY(...) + return waitfor_remote("QUERY", ...) end function cmd.LIST() @@ -176,7 +185,7 @@ skynet.start(function() end end) local handle = skynet.localname ".service" - if handle ~= 0 then + if handle then skynet.error(".service is already register by ", skynet.address(handle)) skynet.exit() else diff --git a/service/service_provider.lua b/service/service_provider.lua new file mode 100644 index 00000000..b04067ca --- /dev/null +++ b/service/service_provider.lua @@ -0,0 +1,113 @@ +local skynet = require "skynet" + +local provider = {} + +local function new_service(svr, name) + local s = {} + svr[name] = s + s.queue = {} + return s +end + +local svr = setmetatable({}, { __index = new_service }) + + +function provider.query(name) + local s = svr[name] + if s.queue then + table.insert(s.queue, skynet.response()) + else + if s.address then + return skynet.ret(skynet.pack(s.address)) + else + error(s.error) + end + end +end + +local function boot(addr, name, code, ...) + local s = svr[name] + skynet.call(addr, "lua", "init", code, ...) + local tmp = table.pack( ... ) + for i=1,tmp.n do + tmp[i] = tostring(tmp[i]) + end + + if tmp.n > 0 then + s.init = table.concat(tmp, ",") + end + s.time = skynet.time() +end + +function provider.launch(name, code, ...) + local s = svr[name] + if s.booting then + table.insert(s.queue, skynet.response()) + else + s.booting = true + local err + local ok, addr = pcall(skynet.newservice,"service_cell", name) + if ok then + ok, err = xpcall(boot, debug.traceback, addr, name, code, ...) + else + err = addr + addr = nil + end + s.booting = nil + if ok then + s.address = addr + for _, resp in ipairs(s.queue) do + resp(true, addr) + end + s.queue = nil + skynet.ret(skynet.pack(addr)) + else + if addr then + skynet.send(addr, "debug", "EXIT") + end + s.error = err + for _, resp in ipairs(s.queue) do + resp(false) + end + s.queue = nil + error(err) + end + end +end + +function provider.test(name) + local s = svr[name] + if s.booting then + skynet.ret(skynet.pack(nil, true)) -- booting + elseif s.address then + skynet.ret(skynet.pack(s.address)) + elseif s.error then + error(s.error) + else + skynet.ret() -- nil + end +end + +skynet.start(function() + skynet.dispatch("lua", function(session, address, cmd, ...) + provider[cmd](...) + end) + skynet.info_func(function() + local info = {} + for k,v in pairs(svr) do + local status + if v.booting then + status = "booting" + elseif v.queue then + status = "waiting(" .. #v.queue .. ")" + end + info[skynet.address(v.address)] = { + init = v.init, + name = k, + time = os.date("%Y %b %d %T %z",math.floor(v.time)), + status = status, + } + end + return info + end) +end) diff --git a/service/sharedatad.lua b/service/sharedatad.lua new file mode 100644 index 00000000..3877ec39 --- /dev/null +++ b/service/sharedatad.lua @@ -0,0 +1,166 @@ +local skynet = require "skynet" +local sharedata = require "skynet.sharedata.corelib" +local table = table +local cache = require "skynet.codecache" +cache.mode "OFF" -- turn off codecache, because CMD.new may load data file + +local NORET = {} +local pool = {} +local pool_count = {} +local objmap = {} +local collect_tick = 600 + +local function newobj(name, tbl) + assert(pool[name] == nil) + local cobj = sharedata.host.new(tbl) + sharedata.host.incref(cobj) + local v = { value = tbl , obj = cobj, watch = {} } + objmap[cobj] = v + pool[name] = v + pool_count[name] = { n = 0, threshold = 16 } +end + +local function collect10sec() + if collect_tick > 10 then + collect_tick = 10 + end +end + +local function collectobj() + while true do + skynet.sleep(100) -- sleep 1s + if collect_tick <= 0 then + collect_tick = 600 -- reset tick count to 600 sec + collectgarbage() + for obj, v in pairs(objmap) do + if v == true then + if sharedata.host.getref(obj) <= 0 then + objmap[obj] = nil + sharedata.host.delete(obj) + end + end + end + else + collect_tick = collect_tick - 1 + end + end +end + +local CMD = {} + +local env_mt = { __index = _ENV } + +function CMD.new(name, t, ...) + local dt = type(t) + local value + if dt == "table" then + value = t + elseif dt == "string" then + value = setmetatable({}, env_mt) + local f + if t:sub(1,1) == "@" then + f = assert(loadfile(t:sub(2),"bt",value)) + else + f = assert(load(t, "=" .. name, "bt", value)) + end + local _, ret = assert(skynet.pcall(f, ...)) + setmetatable(value, nil) + if type(ret) == "table" then + value = ret + end + elseif dt == "nil" then + value = {} + else + error ("Unknown data type " .. dt) + end + newobj(name, value) +end + +function CMD.delete(name) + local v = assert(pool[name]) + pool[name] = nil + pool_count[name] = nil + assert(objmap[v.obj]) + objmap[v.obj] = true + sharedata.host.decref(v.obj) + for _,response in pairs(v.watch) do + response(true) + end +end + +function CMD.query(name) + local v = assert(pool[name]) + local obj = v.obj + sharedata.host.incref(obj) + return v.obj +end + +function CMD.confirm(cobj) + if objmap[cobj] then + sharedata.host.decref(cobj) + end + return NORET +end + +function CMD.update(name, t, ...) + local v = pool[name] + local watch, oldcobj + if v then + watch = v.watch + oldcobj = v.obj + objmap[oldcobj] = true + sharedata.host.decref(oldcobj) + pool[name] = nil + pool_count[name] = nil + end + CMD.new(name, t, ...) + local newobj = pool[name].obj + if watch then + sharedata.host.markdirty(oldcobj) + for _,response in pairs(watch) do + response(true, newobj) + end + end + collect10sec() -- collect in 10 sec +end + +local function check_watch(queue) + local n = 0 + for k,response in pairs(queue) do + if not response "TEST" then + queue[k] = nil + n = n + 1 + end + end + return n +end + +function CMD.monitor(name, obj) + local v = assert(pool[name]) + if obj ~= v.obj then + return v.obj + end + + local n = pool_count[name].n + 1 + if n > pool_count[name].threshold then + n = n - check_watch(v.watch) + pool_count[name].threshold = n * 2 + end + pool_count[name].n = n + + table.insert(v.watch, skynet.response()) + + return NORET +end + +skynet.start(function() + skynet.fork(collectobj) + skynet.dispatch("lua", function (session, source ,cmd, ...) + local f = assert(CMD[cmd]) + local r = f(...) + if r ~= NORET then + skynet.ret(skynet.pack(r)) + end + end) +end) + diff --git a/service/snaxd.lua b/service/snaxd.lua index de41d6c3..6b264930 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -1,14 +1,19 @@ local skynet = require "skynet" -local c = require "skynet.c" +local c = require "skynet.core" local snax_interface = require "snax.interface" -local profile = require "profile" -local snax = require "snax" +local profile = require "skynet.profile" +local snax = require "skynet.snax" + +local snax_name = tostring(...) +local loaderpath = skynet.getenv"snax_loader" +local loader = loaderpath and assert(dofile(loaderpath)) +local func, pattern = snax_interface(snax_name, _ENV, loader) +local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/" +package.path = snax_path .. "?.lua;" .. package.path + +SERVICE_NAME = snax_name +SERVICE_PATH = snax_path -local func = snax_interface(tostring(...), _ENV) -local mode -local thread_id -local message_queue = {} -local init = false local profile_table = {} local function update_stat(name, ti) @@ -23,65 +28,6 @@ end local traceback = debug.traceback -local function do_func(f, msg) - return xpcall(f, traceback, table.unpack(msg)) -end - -local function dispatch(f, ...) - return skynet.pack(f(...)) -end - -local function message_dispatch() - while true do - if #message_queue==0 then - thread_id = coroutine.running() - skynet.wait() - else - local msg = table.remove(message_queue,1) - local method = msg.method - local f = method[4] - if f then - if method[2] == "accept" then - -- no return - profile.start() - local ok, data = xpcall(f, traceback, table.unpack(msg)) - local ti = profile.stop() - update_stat(method[3], ti) - if not ok then - print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) - end - else - profile.start() - local ok, data, size = xpcall(dispatch, traceback, f, table.unpack(msg)) - local ti = profile.stop() - update_stat(method[3], ti) - if ok then - -- skynet.PTYPE_RESPONSE == 1 - c.send(msg.source, 1, msg.session, data, size) - if method[2] == "system" then - init = false - skynet.exit() - break - end - else - -- Can't throw error, so print it directly - print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) - c.send(msg.source, skynet.PTYPE_ERROR, msg.session, "") - end - end - end - end - end -end - -local function queue( session, source, method, ...) - table.insert(message_queue, {session = session, source = source, method = method, ... }) - if thread_id then - skynet.wakeup(thread_id) - thread_id = nil - end -end - local function return_f(f, ...) return skynet.ret(skynet.pack(f(...))) end @@ -101,7 +47,8 @@ local function timing( method, ... ) end skynet.start(function() - skynet.dispatch("snax", function ( session , source , id, ...) + local init = false + local function dispatcher( session , source , id, ...) local method = func[id] if method[2] == "system" then @@ -109,20 +56,17 @@ skynet.start(function() if command == "hotfix" then local hotfix = require "snax.hotfix" skynet.ret(skynet.pack(hotfix(func, ...))) + elseif command == "profile" then + skynet.ret(skynet.pack(profile_table)) elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end - mode = initfunc(...) - if mode == "queue" then - skynet.fork(message_dispatch) - end + initfunc(...) skynet.ret() skynet.info_func(function() return profile_table end) init = true - elseif mode == "queue" then - queue( session, source, method , ...) else assert(init, "Never init") assert(command == "exit") @@ -134,11 +78,13 @@ skynet.start(function() end else assert(init, "Init first") - if mode == "queue" then - queue(session, source, method , ...) - else - timing(method, ...) - end + timing(method, ...) end - end) + end + skynet.dispatch("snax", dispatcher) + + -- set lua dispatcher + function snax.enablecluster() + skynet.dispatch("lua", dispatcher) + end end) diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h new file mode 100644 index 00000000..f79c7148 --- /dev/null +++ b/skynet-src/atomic.h @@ -0,0 +1,14 @@ +#ifndef SKYNET_ATOMIC_H +#define SKYNET_ATOMIC_H + +#define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_INC(ptr) __sync_add_and_fetch(ptr, 1) +#define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1) +#define ATOM_DEC(ptr) __sync_sub_and_fetch(ptr, 1) +#define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1) +#define ATOM_ADD(ptr,n) __sync_add_and_fetch(ptr, n) +#define ATOM_SUB(ptr,n) __sync_sub_and_fetch(ptr, n) +#define ATOM_AND(ptr,n) __sync_and_and_fetch(ptr, n) + +#endif diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h new file mode 100644 index 00000000..a9485ccd --- /dev/null +++ b/skynet-src/luashrtbl.h @@ -0,0 +1,16 @@ +#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 + +static inline int luaS_shrinfo(lua_State *L) { return 0; } +static inline void luaS_initshr() {} +static inline void luaS_exitshr() {} +static inline void luaS_expandshr(int n) {} + +#endif + +#endif diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 9b3949a6..da8a536b 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -2,9 +2,12 @@ #include #include #include +#include +#include #include "malloc_hook.h" #include "skynet.h" +#include "atomic.h" static size_t _used_memory = 0; static size_t _memory_block = 0; @@ -23,6 +26,10 @@ static mem_data mem_stats[SLOT_SIZE]; #include "jemalloc.h" +// for skynet_lalloc use +#define raw_realloc je_realloc +#define raw_free je_free + static ssize_t* get_allocated_field(uint32_t handle) { int h = (int)(handle & (SLOT_SIZE - 1)); @@ -31,11 +38,11 @@ get_allocated_field(uint32_t handle) { ssize_t old_alloc = data->allocated; if(old_handle == 0 || old_alloc <= 0) { // data->allocated may less than zero, because it may not count at start. - if(!__sync_bool_compare_and_swap(&data->handle, old_handle, handle)) { + if(!ATOM_CAS(&data->handle, old_handle, handle)) { return 0; } if (old_alloc < 0) { - __sync_bool_compare_and_swap(&data->allocated, old_alloc, 0); + ATOM_CAS(&data->allocated, old_alloc, 0); } } if(data->handle != handle) { @@ -46,21 +53,21 @@ get_allocated_field(uint32_t handle) { inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { - __sync_add_and_fetch(&_used_memory, __n); - __sync_add_and_fetch(&_memory_block, 1); + ATOM_ADD(&_used_memory, __n); + ATOM_INC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - __sync_add_and_fetch(allocated, __n); + ATOM_ADD(allocated, __n); } } inline static void update_xmalloc_stat_free(uint32_t handle, size_t __n) { - __sync_sub_and_fetch(&_used_memory, __n); - __sync_sub_and_fetch(&_memory_block, 1); + ATOM_SUB(&_used_memory, __n); + ATOM_DEC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - __sync_sub_and_fetch(allocated, __n); + ATOM_SUB(allocated, __n); } } @@ -163,6 +170,10 @@ skynet_calloc(size_t nmemb,size_t size) { #else +// for skynet_lalloc use +#define raw_realloc realloc +#define raw_free free + void memory_info_dump(void) { skynet_error(NULL, "No jemalloc"); @@ -216,11 +227,46 @@ skynet_strdup(const char *str) { } void * -skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { +skynet_lalloc(void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { - skynet_free(ptr); + raw_free(ptr); return NULL; } else { - return skynet_realloc(ptr, nsize); + return raw_realloc(ptr, nsize); } } + +int +dump_mem_lua(lua_State *L) { + int i; + lua_newtable(L); + for(i=0; ihandle != 0 && data->allocated != 0) { + lua_pushinteger(L, data->allocated); + lua_rawseti(L, -2, (lua_Integer)data->handle); + } + } + return 1; +} + +size_t +malloc_current_memory(void) { + uint32_t handle = skynet_current_handle(); + int i; + for(i=0; ihandle == (uint32_t)handle && data->allocated != 0) { + return (size_t) data->allocated; + } + } + return 0; +} + +void +skynet_debug_memory(const char *info) { + // for debug use + uint32_t handle = skynet_current_handle(); + size_t mem = malloc_current_memory(); + fprintf(stderr, "[:%08x] %s %p\n", handle, info, (void *)mem); +} diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index e7beb1a7..4da4123a 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -1,7 +1,8 @@ -#ifndef __MALLOC_HOOK_H -#define __MALLOC_HOOK_H +#ifndef SKYNET_MALLOC_HOOK_H +#define SKYNET_MALLOC_HOOK_H #include +#include extern size_t malloc_used_memory(void); extern size_t malloc_memory_block(void); @@ -9,6 +10,8 @@ extern void memory_info_dump(void); extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern void dump_c_mem(void); +extern int dump_mem_lua(lua_State *L); +extern size_t malloc_current_memory(void); -#endif /* __MALLOC_HOOK_H */ +#endif /* SKYNET_MALLOC_HOOK_H */ diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 46357764..5a995918 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -1,5 +1,7 @@ -#ifndef _RWLOCK_H_ -#define _RWLOCK_H_ +#ifndef SKYNET_RWLOCK_H +#define SKYNET_RWLOCK_H + +#ifndef USE_PTHREAD_LOCK struct rwlock { int write; @@ -45,4 +47,42 @@ rwlock_runlock(struct rwlock *lock) { __sync_sub_and_fetch(&lock->read,1); } +#else + +#include + +// only for some platform doesn't have __sync_* +// todo: check the result of pthread api + +struct rwlock { + pthread_rwlock_t lock; +}; + +static inline void +rwlock_init(struct rwlock *lock) { + pthread_rwlock_init(&lock->lock, NULL); +} + +static inline void +rwlock_rlock(struct rwlock *lock) { + pthread_rwlock_rdlock(&lock->lock); +} + +static inline void +rwlock_wlock(struct rwlock *lock) { + pthread_rwlock_wrlock(&lock->lock); +} + +static inline void +rwlock_wunlock(struct rwlock *lock) { + pthread_rwlock_unlock(&lock->lock); +} + +static inline void +rwlock_runlock(struct rwlock *lock) { + pthread_rwlock_unlock(&lock->lock); +} + +#endif + #endif diff --git a/skynet-src/skynet.h b/skynet-src/skynet.h index 9ae1759c..7d3e478a 100644 --- a/skynet-src/skynet.h +++ b/skynet-src/skynet.h @@ -30,7 +30,7 @@ void skynet_error(struct skynet_context * context, const char *msg, ...); const char * skynet_command(struct skynet_context * context, const char * cmd , const char * parm); uint32_t skynet_queryname(struct skynet_context * context, const char * name); int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * msg, size_t sz); -int skynet_sendname(struct skynet_context * context, const char * destination , int type, int session, void * msg, size_t sz); +int skynet_sendname(struct skynet_context * context, uint32_t source, const char * destination , int type, int session, void * msg, size_t sz); int skynet_isremote(struct skynet_context *, uint32_t handle, int * harbor); @@ -38,5 +38,7 @@ typedef int (*skynet_cb)(struct skynet_context * context, void *ud, int type, in void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); uint32_t skynet_current_handle(void); +uint64_t skynet_now(void); +void skynet_debug_memory(const char *info); // for debug use, output current service memory to stderr #endif diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c new file mode 100644 index 00000000..902086e3 --- /dev/null +++ b/skynet-src/skynet_daemon.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "skynet_daemon.h" + +static int +check_pid(const char *pidfile) { + int pid = 0; + FILE *f = fopen(pidfile,"r"); + if (f == NULL) + return 0; + int n = fscanf(f,"%d", &pid); + fclose(f); + + if (n !=1 || pid == 0 || pid == getpid()) { + return 0; + } + + if (kill(pid, 0) && errno == ESRCH) + return 0; + + return pid; +} + +static int +write_pid(const char *pidfile) { + FILE *f; + int pid = 0; + int fd = open(pidfile, O_RDWR|O_CREAT, 0644); + if (fd == -1) { + fprintf(stderr, "Can't create pidfile [%s].\n", pidfile); + return 0; + } + f = fdopen(fd, "r+"); + if (f == NULL) { + fprintf(stderr, "Can't open pidfile [%s].\n", pidfile); + return 0; + } + + if (flock(fd, LOCK_EX|LOCK_NB) == -1) { + int n = fscanf(f, "%d", &pid); + fclose(f); + if (n != 1) { + fprintf(stderr, "Can't lock and read pidfile.\n"); + } else { + fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid); + } + return 0; + } + + pid = getpid(); + if (!fprintf(f,"%d\n", pid)) { + fprintf(stderr, "Can't write pid.\n"); + close(fd); + return 0; + } + fflush(f); + + return pid; +} + +static int +redirect_fds() { + int nfd = open("/dev/null", O_RDWR); + if (nfd == -1) { + perror("Unable to open /dev/null: "); + return -1; + } + if (dup2(nfd, 0) < 0) { + perror("Unable to dup2 stdin(0): "); + return -1; + } + if (dup2(nfd, 1) < 0) { + perror("Unable to dup2 stdout(1): "); + return -1; + } + if (dup2(nfd, 2) < 0) { + perror("Unable to dup2 stderr(2): "); + return -1; + } + + close(nfd); + + return 0; +} + +int +daemon_init(const char *pidfile) { + int pid = check_pid(pidfile); + + if (pid) { + fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); + return 1; + } + +#ifdef __APPLE__ + fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); +#else + if (daemon(1,1)) { + fprintf(stderr, "Can't daemonize.\n"); + return 1; + } +#endif + + pid = write_pid(pidfile); + if (pid == 0) { + return 1; + } + + if (redirect_fds()) { + return 1; + } + + return 0; +} + +int +daemon_exit(const char *pidfile) { + return unlink(pidfile); +} diff --git a/skynet-src/skynet_daemon.h b/skynet-src/skynet_daemon.h new file mode 100644 index 00000000..0d99562a --- /dev/null +++ b/skynet-src/skynet_daemon.h @@ -0,0 +1,7 @@ +#ifndef skynet_daemon_h +#define skynet_daemon_h + +int daemon_init(const char *pidfile); +int daemon_exit(const char *pidfile); + +#endif diff --git a/skynet-src/skynet_env.c b/skynet-src/skynet_env.c index 9dbbcf9c..a5882f21 100644 --- a/skynet-src/skynet_env.c +++ b/skynet-src/skynet_env.c @@ -1,5 +1,6 @@ #include "skynet.h" #include "skynet_env.h" +#include "spinlock.h" #include #include @@ -8,18 +9,15 @@ #include struct skynet_env { - int lock; + struct spinlock lock; lua_State *L; }; static struct skynet_env *E = NULL; -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - const char * skynet_getenv(const char *key) { - LOCK(E) + SPIN_LOCK(E) lua_State *L = E->L; @@ -27,14 +25,14 @@ skynet_getenv(const char *key) { const char * result = lua_tostring(L, -1); lua_pop(L, 1); - UNLOCK(E) + SPIN_UNLOCK(E) return result; } void skynet_setenv(const char *key, const char *value) { - LOCK(E) + SPIN_LOCK(E) lua_State *L = E->L; lua_getglobal(L, key); @@ -43,12 +41,12 @@ skynet_setenv(const char *key, const char *value) { lua_pushstring(L,value); lua_setglobal(L,key); - UNLOCK(E) + SPIN_UNLOCK(E) } void skynet_env_init() { E = skynet_malloc(sizeof(*E)); - E->lock = 0; + SPIN_INIT(E) E->L = luaL_newstate(); } diff --git a/skynet-src/skynet_error.c b/skynet-src/skynet_error.c index b9c53efd..856c4700 100644 --- a/skynet-src/skynet_error.c +++ b/skynet-src/skynet_error.c @@ -28,7 +28,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { va_start(ap,msg); int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); va_end(ap); - if (len < LOG_MESSAGE_SIZE) { + if (len >=0 && len < LOG_MESSAGE_SIZE) { data = skynet_strdup(tmp); } else { int max_size = LOG_MESSAGE_SIZE; @@ -44,6 +44,11 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { skynet_free(data); } } + if (len < 0) { + skynet_free(data); + perror("vsnprintf error :"); + return; + } struct skynet_message smsg; @@ -54,7 +59,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { } smsg.session = 0; smsg.data = data; - smsg.sz = len | (PTYPE_TEXT << HANDLE_REMOTE_SHIFT); + smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT); skynet_context_push(logger, &smsg); } diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index 58cabc0b..d904288e 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -49,7 +49,6 @@ skynet_handle_register(struct skynet_context *ctx) { rwlock_wunlock(&s->lock); handle |= s->harbor; - skynet_context_init(ctx, handle); return handle; } } @@ -67,8 +66,9 @@ skynet_handle_register(struct skynet_context *ctx) { } } -void +int skynet_handle_retire(uint32_t handle) { + int ret = 0; struct handle_storage *s = H; rwlock_wlock(&s->lock); @@ -77,8 +77,8 @@ skynet_handle_retire(uint32_t handle) { struct skynet_context * ctx = s->slot[hash]; if (ctx != NULL && skynet_context_handle(ctx) == handle) { - skynet_context_release(ctx); s->slot[hash] = NULL; + ret = 1; int i; int j=0, n=s->name_count; for (i=0; iname_count = j; + } else { + ctx = NULL; } rwlock_wunlock(&s->lock); + + if (ctx) { + // release ctx may call skynet_handle_* , so wunlock first. + skynet_context_release(ctx); + } + + return ret; } void @@ -105,10 +114,14 @@ skynet_handle_retireall() { for (i=0;islot_size;i++) { rwlock_rlock(&s->lock); struct skynet_context * ctx = s->slot[i]; + uint32_t handle = 0; + if (ctx) + handle = skynet_context_handle(ctx); rwlock_runlock(&s->lock); - if (ctx != NULL) { - ++n; - skynet_handle_retire(skynet_context_handle(ctx)); + if (handle != 0) { + if (skynet_handle_retire(handle)) { + ++n; + } } } if (n==0) diff --git a/skynet-src/skynet_handle.h b/skynet-src/skynet_handle.h index a39b47bb..293b6571 100644 --- a/skynet-src/skynet_handle.h +++ b/skynet-src/skynet_handle.h @@ -3,12 +3,14 @@ #include -#include "skynet_harbor.h" +// reserve high 8 bits for remote id +#define HANDLE_MASK 0xffffff +#define HANDLE_REMOTE_SHIFT 24 struct skynet_context; uint32_t skynet_handle_register(struct skynet_context *); -void skynet_handle_retire(uint32_t handle); +int skynet_handle_retire(uint32_t handle); struct skynet_context * skynet_handle_grab(uint32_t handle); void skynet_handle_retireall(); diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index ae47cf45..5e39ca62 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -1,40 +1,27 @@ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_server.h" +#include "skynet_mq.h" +#include "skynet_handle.h" #include #include #include static struct skynet_context * REMOTE = 0; -static unsigned int HARBOR = 0; +static unsigned int HARBOR = ~0; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) { - int type = rmsg->sz >> HANDLE_REMOTE_SHIFT; - rmsg->sz &= HANDLE_MASK; - assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR); + int type = rmsg->sz >> MESSAGE_TYPE_SHIFT; + rmsg->sz &= MESSAGE_TYPE_MASK; + assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR && REMOTE); skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session); } -void -skynet_harbor_register(struct remote_name *rname) { - int i; - int number = 1; - for (i=0;iname[i]; - if (!(c >= '0' && c <='9')) { - number = 0; - break; - } - } - assert(number == 0); - skynet_context_send(REMOTE, rname, sizeof(*rname), 0, PTYPE_SYSTEM , 0); -} - int skynet_harbor_message_isremote(uint32_t handle) { - assert(HARBOR != 0); + assert(HARBOR != ~0); int h = (handle & ~HANDLE_MASK); return h != HARBOR && h !=0; } @@ -46,5 +33,17 @@ skynet_harbor_init(int harbor) { void skynet_harbor_start(void *ctx) { + // the HARBOR must be reserved to ensure the pointer is valid. + // It will be released at last by calling skynet_harbor_exit + skynet_context_reserve(ctx); REMOTE = ctx; } + +void +skynet_harbor_exit() { + struct skynet_context * ctx = REMOTE; + REMOTE= NULL; + if (ctx) { + skynet_context_release(ctx); + } +} diff --git a/skynet-src/skynet_harbor.h b/skynet-src/skynet_harbor.h index 0116a91a..97a76c20 100644 --- a/skynet-src/skynet_harbor.h +++ b/skynet-src/skynet_harbor.h @@ -7,10 +7,6 @@ #define GLOBALNAME_LENGTH 16 #define REMOTE_MAX 256 -// reserve high 8 bits for remote id -#define HANDLE_MASK 0xffffff -#define HANDLE_REMOTE_SHIFT 24 - struct remote_name { char name[GLOBALNAME_LENGTH]; uint32_t handle; @@ -23,9 +19,9 @@ struct remote_message { }; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session); -void skynet_harbor_register(struct remote_name *rname); int skynet_harbor_message_isremote(uint32_t handle); void skynet_harbor_init(int harbor); void skynet_harbor_start(void * ctx); +void skynet_harbor_exit(); #endif diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 81fad4c8..eef5d509 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -4,8 +4,12 @@ struct skynet_config { int thread; int harbor; + int profile; + const char * daemon; const char * module_path; const char * bootstrap; + const char * logger; + const char * logservice; }; #define THREAD_WORKER 0 diff --git a/skynet-src/skynet_log.c b/skynet-src/skynet_log.c new file mode 100644 index 00000000..4ce106ff --- /dev/null +++ b/skynet-src/skynet_log.c @@ -0,0 +1,77 @@ +#include "skynet_log.h" +#include "skynet_timer.h" +#include "skynet.h" +#include "skynet_socket.h" +#include +#include + +FILE * +skynet_log_open(struct skynet_context * ctx, uint32_t handle) { + const char * logpath = skynet_getenv("logpath"); + if (logpath == NULL) + return NULL; + size_t sz = strlen(logpath); + char tmp[sz + 16]; + sprintf(tmp, "%s/%08x.log", logpath, handle); + FILE *f = fopen(tmp, "ab"); + if (f) { + uint32_t starttime = skynet_starttime(); + uint64_t currenttime = skynet_now(); + time_t ti = starttime + currenttime/100; + skynet_error(ctx, "Open log file %s", tmp); + fprintf(f, "open time: %u %s", (uint32_t)currenttime, ctime(&ti)); + fflush(f); + } else { + skynet_error(ctx, "Open log file %s fail", tmp); + } + return f; +} + +void +skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) { + skynet_error(ctx, "Close log file :%08x", handle); + fprintf(f, "close time: %u\n", (uint32_t)skynet_now()); + fclose(f); +} + +static void +log_blob(FILE *f, void * buffer, size_t sz) { + size_t i; + uint8_t * buf = buffer; + for (i=0;i!=sz;i++) { + fprintf(f, "%02x", buf[i]); + } +} + +static void +log_socket(FILE * f, struct skynet_socket_message * message, size_t sz) { + fprintf(f, "[socket] %d %d %d ", message->type, message->id, message->ud); + + if (message->buffer == NULL) { + const char *buffer = (const char *)(message + 1); + sz -= sizeof(*message); + const char * eol = memchr(buffer, '\0', sz); + if (eol) { + sz = eol - buffer; + } + fprintf(f, "[%*s]", (int)sz, (const char *)buffer); + } else { + sz = message->ud; + log_blob(f, message->buffer, sz); + } + fprintf(f, "\n"); + fflush(f); +} + +void +skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz) { + if (type == PTYPE_SOCKET) { + log_socket(f, buffer, sz); + } else { + uint32_t ti = (uint32_t)skynet_now(); + fprintf(f, ":%08x %d %d %u ", source, type, session, ti); + log_blob(f, buffer, sz); + fprintf(f,"\n"); + fflush(f); + } +} diff --git a/skynet-src/skynet_log.h b/skynet-src/skynet_log.h new file mode 100644 index 00000000..41ddeda0 --- /dev/null +++ b/skynet-src/skynet_log.h @@ -0,0 +1,14 @@ +#ifndef skynet_log_h +#define skynet_log_h + +#include "skynet_env.h" +#include "skynet.h" + +#include +#include + +FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle); +void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle); +void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz); + +#endif \ No newline at end of file diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 3b8cf1b2..f08feef2 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -3,6 +3,7 @@ #include "skynet_imp.h" #include "skynet_env.h" #include "skynet_server.h" +#include "luashrtbl.h" #include #include @@ -11,6 +12,7 @@ #include #include #include +#include static int optint(const char *key, int opt) { @@ -24,7 +26,6 @@ optint(const char *key, int opt) { return strtol(str, NULL, 10); } -/* static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); @@ -34,7 +35,7 @@ optboolean(const char *key, int opt) { } return strcmp(str,"true")==0; } -*/ + static const char * optstring(const char *key,const char * opt) { const char * str = skynet_getenv(key); @@ -50,7 +51,6 @@ optstring(const char *key,const char * opt) { static void _init_env(lua_State *L) { - lua_pushglobaltable(L); lua_pushnil(L); /* first key */ while (lua_next(L, -2) != 0) { int keyt = lua_type(L, -2); @@ -82,12 +82,49 @@ int sigign() { return 0; } +static const char * load_config = "\ + local result = {}\n\ + local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ + local sep = package.config:sub(1,1)\n\ + local current_path = [[.]]..sep\n\ + local function include(filename)\n\ + local last_path = current_path\n\ + local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ + if path then\n\ + if path:sub(1,1) == sep then -- root\n\ + current_path = path\n\ + else\n\ + current_path = current_path .. path\n\ + end\n\ + else\n\ + name = filename\n\ + end\n\ + local f = assert(io.open(current_path .. name))\n\ + local code = assert(f:read [[*a]])\n\ + code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ + f:close()\n\ + assert(load(code,[[@]]..filename,[[t]],result))()\n\ + current_path = last_path\n\ + end\n\ + setmetatable(result, { __index = { include = include } })\n\ + local config_name = ...\n\ + include(config_name)\n\ + setmetatable(result, nil)\n\ + return result\n\ +"; + int main(int argc, char *argv[]) { - const char * config_file = "config"; + const char * config_file = NULL ; if (argc > 1) { config_file = argv[1]; + } else { + fprintf(stderr, "Need a config file. Please read skynet wiki : https://github.com/cloudwu/skynet/wiki/Config\n" + "usage: skynet configfilename\n"); + return 1; } + + luaS_initshr(); skynet_globalinit(); skynet_env_init(); @@ -95,35 +132,35 @@ main(int argc, char *argv[]) { struct skynet_config config; - struct lua_State *L = lua_newstate(skynet_lalloc, NULL); + struct lua_State *L = luaL_newstate(); luaL_openlibs(L); // link lua lib - lua_close(L); - L = luaL_newstate(); + int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); + assert(err == LUA_OK); + lua_pushstring(L, config_file); - int err = luaL_dofile(L, config_file); + err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L); return 1; - } + } _init_env(L); -#ifdef LUA_CACHELIB - printf("Skynet lua code cache enable\n"); -#endif - config.thread = optint("thread",8); config.module_path = optstring("cpath","./cservice/?.so"); config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); + config.daemon = optstring("daemon", NULL); + config.logger = optstring("logger", NULL); + config.logservice = optstring("logservice", "logger"); + config.profile = optboolean("profile", 1); lua_close(L); skynet_start(&config); skynet_globalexit(); - - printf("skynet exit\n"); + luaS_exitshr(); return 0; } diff --git a/skynet-src/skynet_malloc.h b/skynet-src/skynet_malloc.h index f7e4337f..7bafa406 100644 --- a/skynet-src/skynet_malloc.h +++ b/skynet-src/skynet_malloc.h @@ -13,6 +13,6 @@ void * skynet_calloc(size_t nmemb,size_t size); void * skynet_realloc(void *ptr, size_t size); void skynet_free(void *ptr); char * skynet_strdup(const char *str); -void * skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize); // use for lua +void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua #endif diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index 66dd4629..a473e422 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -1,6 +1,7 @@ #include "skynet.h" #include "skynet_module.h" +#include "spinlock.h" #include #include @@ -13,7 +14,7 @@ struct modules { int count; - int lock; + struct spinlock lock; const char * path; struct skynet_module m[MAX_MODULE_TYPE]; }; @@ -72,17 +73,28 @@ _query(const char * name) { return NULL; } -static int -_open_sym(struct skynet_module *mod) { +static void * +get_api(struct skynet_module *mod, const char *api_name) { size_t name_size = strlen(mod->name); - char tmp[name_size + 9]; // create/init/release , longest name is release (7) + size_t api_size = strlen(api_name); + char tmp[name_size + api_size + 1]; memcpy(tmp, mod->name, name_size); - strcpy(tmp+name_size, "_create"); - mod->create = dlsym(mod->module, tmp); - strcpy(tmp+name_size, "_init"); - mod->init = dlsym(mod->module, tmp); - strcpy(tmp+name_size, "_release"); - mod->release = dlsym(mod->module, tmp); + memcpy(tmp+name_size, api_name, api_size+1); + char *ptr = strrchr(tmp, '.'); + if (ptr == NULL) { + ptr = tmp; + } else { + ptr = ptr + 1; + } + return dlsym(mod->module, ptr); +} + +static int +open_sym(struct skynet_module *mod) { + mod->create = get_api(mod, "_create"); + mod->init = get_api(mod, "_init"); + mod->release = get_api(mod, "_release"); + mod->signal = get_api(mod, "_signal"); return mod->init == NULL; } @@ -93,7 +105,7 @@ skynet_module_query(const char * name) { if (result) return result; - while(__sync_lock_test_and_set(&M->lock,1)) {} + SPIN_LOCK(M) result = _query(name); // double check @@ -104,7 +116,7 @@ skynet_module_query(const char * name) { M->m[index].name = name; M->m[index].module = dl; - if (_open_sym(&M->m[index]) == 0) { + if (open_sym(&M->m[index]) == 0) { M->m[index].name = skynet_strdup(name); M->count ++; result = &M->m[index]; @@ -112,21 +124,22 @@ skynet_module_query(const char * name) { } } - __sync_lock_release(&M->lock); + SPIN_UNLOCK(M) return result; } void skynet_module_insert(struct skynet_module *mod) { - while(__sync_lock_test_and_set(&M->lock,1)) {} + SPIN_LOCK(M) struct skynet_module * m = _query(mod->name); assert(m == NULL && M->count < MAX_MODULE_TYPE); int index = M->count; M->m[index] = *mod; ++M->count; - __sync_lock_release(&M->lock); + + SPIN_UNLOCK(M) } void * @@ -150,12 +163,20 @@ skynet_module_instance_release(struct skynet_module *m, void *inst) { } } +void +skynet_module_instance_signal(struct skynet_module *m, void *inst, int signal) { + if (m->signal) { + m->signal(inst, signal); + } +} + void skynet_module_init(const char *path) { struct modules *m = skynet_malloc(sizeof(*m)); m->count = 0; m->path = skynet_strdup(path); - m->lock = 0; + + SPIN_INIT(m) M = m; } diff --git a/skynet-src/skynet_module.h b/skynet-src/skynet_module.h index a2d96306..52b5e181 100644 --- a/skynet-src/skynet_module.h +++ b/skynet-src/skynet_module.h @@ -6,6 +6,7 @@ struct skynet_context; typedef void * (*skynet_dl_create)(void); typedef int (*skynet_dl_init)(void * inst, struct skynet_context *, const char * parm); typedef void (*skynet_dl_release)(void * inst); +typedef void (*skynet_dl_signal)(void * inst, int signal); struct skynet_module { const char * name; @@ -13,6 +14,7 @@ struct skynet_module { skynet_dl_create create; skynet_dl_init init; skynet_dl_release release; + skynet_dl_signal signal; }; void skynet_module_insert(struct skynet_module *mod); @@ -20,6 +22,7 @@ struct skynet_module * skynet_module_query(const char * name); void * skynet_module_instance_create(struct skynet_module *); int skynet_module_instance_init(struct skynet_module *, void * inst, struct skynet_context *ctx, const char * parm); void skynet_module_instance_release(struct skynet_module *, void *inst); +void skynet_module_instance_signal(struct skynet_module *, void *inst, int signal); void skynet_module_init(const char *path); diff --git a/skynet-src/skynet_monitor.c b/skynet-src/skynet_monitor.c index 9ee302e5..8d47fc89 100644 --- a/skynet-src/skynet_monitor.c +++ b/skynet-src/skynet_monitor.c @@ -3,6 +3,7 @@ #include "skynet_monitor.h" #include "skynet_server.h" #include "skynet.h" +#include "atomic.h" #include #include @@ -30,7 +31,7 @@ void skynet_monitor_trigger(struct skynet_monitor *sm, uint32_t source, uint32_t destination) { sm->source = source; sm->destination = destination; - __sync_fetch_and_add(&sm->version , 1); + ATOM_INC(&sm->version); } void diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index c5d80f0b..0ddd9ee9 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -1,6 +1,7 @@ #include "skynet.h" #include "skynet_mq.h" #include "skynet_handle.h" +#include "spinlock.h" #include #include @@ -15,96 +16,60 @@ // 1 means mq is in global mq , or the message is dispatching. #define MQ_IN_GLOBAL 1 +#define MQ_OVERLOAD 1024 struct message_queue { + struct spinlock lock; uint32_t handle; int cap; int head; int tail; - int lock; int release; int in_global; + int overload; + int overload_threshold; struct skynet_message *queue; struct message_queue *next; }; struct global_queue { - uint32_t head; - uint32_t tail; - struct message_queue ** queue; - // We use a separated flag array to ensure the mq is pushed. - // See the comments below. - bool * flag; - struct message_queue *list; + struct message_queue *head; + struct message_queue *tail; + struct spinlock lock; }; static struct global_queue *Q = NULL; -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - -#define GP(p) ((p) % MAX_GLOBAL_MQ) - -static void +void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; - if (q->flag[GP(q->tail)]) { - // The queue may full seldom, save queue in list - assert(queue->next == NULL); - struct message_queue * last; - do { - last = q->list; - queue->next = last; - } while(!__sync_bool_compare_and_swap(&q->list, last, queue)); - - return; + SPIN_LOCK(q) + assert(queue->next == NULL); + if(q->tail) { + q->tail->next = queue; + q->tail = queue; + } else { + q->head = q->tail = queue; } - - uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1)); - // The thread would suspend here, and the q->queue[tail] is last version , - // but the queue tail is increased. - // So we set q->flag[tail] after changing q->queue[tail]. - q->queue[tail] = queue; - __sync_synchronize(); - q->flag[tail] = true; + SPIN_UNLOCK(q) } struct message_queue * skynet_globalmq_pop() { struct global_queue *q = Q; - uint32_t head = q->head; - if (head == q->tail) { - // The queue is empty. - return NULL; - } - - uint32_t head_ptr = GP(head); - - struct message_queue * list = q->list; - if (list) { - // If q->list is not empty, try to load it back to the queue - struct message_queue *newhead = list->next; - if (__sync_bool_compare_and_swap(&q->list, list, newhead)) { - // try load list only once, if success , push it back to the queue. - list->next = NULL; - skynet_globalmq_push(list); + SPIN_LOCK(q) + struct message_queue *mq = q->head; + if(mq) { + q->head = mq->next; + if(q->head == NULL) { + assert(mq == q->tail); + q->tail = NULL; } + mq->next = NULL; } - - // Check the flag first, if the flag is false, the pushing may not complete. - if(!q->flag[head_ptr]) { - return NULL; - } - - __sync_synchronize(); - - struct message_queue * mq = q->queue[head_ptr]; - if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) { - return NULL; - } - q->flag[head_ptr] = false; + SPIN_UNLOCK(q) return mq; } @@ -116,12 +81,14 @@ skynet_mq_create(uint32_t handle) { q->cap = DEFAULT_QUEUE_SIZE; q->head = 0; q->tail = 0; - q->lock = 0; + SPIN_INIT(q) // When the queue is create (always between service create and service init) , // set in_global flag to avoid push it to global queue . - // If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue. + // If the service init success, skynet_context_new will call skynet_mq_push to push it to global queue. q->in_global = MQ_IN_GLOBAL; q->release = 0; + q->overload = 0; + q->overload_threshold = MQ_OVERLOAD; q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap); q->next = NULL; @@ -131,6 +98,7 @@ skynet_mq_create(uint32_t handle) { static void _release(struct message_queue *q) { assert(q->next == NULL); + SPIN_DESTROY(q) skynet_free(q->queue); skynet_free(q); } @@ -144,11 +112,11 @@ int skynet_mq_length(struct message_queue *q) { int head, tail,cap; - LOCK(q) + SPIN_LOCK(q) head = q->head; tail = q->tail; cap = q->cap; - UNLOCK(q) + SPIN_UNLOCK(q) if (head <= tail) { return tail - head; @@ -156,24 +124,49 @@ skynet_mq_length(struct message_queue *q) { return tail + cap - head; } +int +skynet_mq_overload(struct message_queue *q) { + if (q->overload) { + int overload = q->overload; + q->overload = 0; + return overload; + } + return 0; +} + int skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { int ret = 1; - LOCK(q) + SPIN_LOCK(q) if (q->head != q->tail) { - *message = q->queue[q->head]; + *message = q->queue[q->head++]; ret = 0; - if ( ++ q->head >= q->cap) { - q->head = 0; + int head = q->head; + int tail = q->tail; + int cap = q->cap; + + if (head >= cap) { + q->head = head = 0; } + int length = tail - head; + if (length < 0) { + length += cap; + } + while (length > q->overload_threshold) { + q->overload = length; + q->overload_threshold *= 2; + } + } else { + // reset overload_threshold when queue is empty + q->overload_threshold = MQ_OVERLOAD; } if (ret) { q->in_global = 0; } - UNLOCK(q) + SPIN_UNLOCK(q) return ret; } @@ -196,7 +189,7 @@ expand_queue(struct message_queue *q) { void skynet_mq_push(struct message_queue *q, struct skynet_message *message) { assert(message); - LOCK(q) + SPIN_LOCK(q) q->queue[q->tail] = *message; if (++ q->tail >= q->cap) { @@ -212,43 +205,26 @@ skynet_mq_push(struct message_queue *q, struct skynet_message *message) { skynet_globalmq_push(q); } - UNLOCK(q) + SPIN_UNLOCK(q) } void skynet_mq_init() { struct global_queue *q = skynet_malloc(sizeof(*q)); memset(q,0,sizeof(*q)); - q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *)); - q->flag = skynet_malloc(MAX_GLOBAL_MQ * sizeof(bool)); - memset(q->flag, 0, sizeof(bool) * MAX_GLOBAL_MQ); + SPIN_INIT(q); Q=q; } -void -skynet_mq_force_push(struct message_queue * queue) { - assert(queue->in_global); - skynet_globalmq_push(queue); -} - -void -skynet_mq_pushglobal(struct message_queue *queue) { - LOCK(queue) - assert(queue->in_global); - skynet_globalmq_push(queue); - queue->in_global = MQ_IN_GLOBAL; - UNLOCK(queue) -} - void skynet_mq_mark_release(struct message_queue *q) { - LOCK(q) + SPIN_LOCK(q) assert(q->release == 0); q->release = 1; if (q->in_global != MQ_IN_GLOBAL) { skynet_globalmq_push(q); } - UNLOCK(q) + SPIN_UNLOCK(q) } static void @@ -262,13 +238,13 @@ _drop_queue(struct message_queue *q, message_drop drop_func, void *ud) { void skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) { - LOCK(q) + SPIN_LOCK(q) if (q->release) { - UNLOCK(q) + SPIN_UNLOCK(q) _drop_queue(q, drop_func, ud); } else { - skynet_mq_force_push(q); - UNLOCK(q) + skynet_globalmq_push(q); + SPIN_UNLOCK(q) } } diff --git a/skynet-src/skynet_mq.h b/skynet-src/skynet_mq.h index 5ac582a1..3721e32b 100644 --- a/skynet-src/skynet_mq.h +++ b/skynet-src/skynet_mq.h @@ -11,8 +11,13 @@ struct skynet_message { size_t sz; }; +// type is encoding in skynet_message.sz high 8bit +#define MESSAGE_TYPE_MASK (SIZE_MAX >> 8) +#define MESSAGE_TYPE_SHIFT ((sizeof(size_t)-1) * 8) + struct message_queue; +void skynet_globalmq_push(struct message_queue * queue); struct message_queue * skynet_globalmq_pop(void); struct message_queue * skynet_mq_create(uint32_t handle); @@ -29,9 +34,7 @@ void skynet_mq_push(struct message_queue *q, struct skynet_message *message); // return the length of message queue, for debug int skynet_mq_length(struct message_queue *q); - -void skynet_mq_force_push(struct message_queue *q); -void skynet_mq_pushglobal(struct message_queue *q); +int skynet_mq_overload(struct message_queue *q); void skynet_mq_init(); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4bc7b66a..d730d1dd 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -9,6 +9,10 @@ #include "skynet_env.h" #include "skynet_monitor.h" #include "skynet_imp.h" +#include "skynet_log.h" +#include "skynet_timer.h" +#include "spinlock.h" +#include "atomic.h" #include @@ -20,16 +24,18 @@ #ifdef CALLING_CHECK -#define CHECKCALLING_BEGIN(ctx) assert(__sync_lock_test_and_set(&ctx->calling,1) == 0); -#define CHECKCALLING_END(ctx) __sync_lock_release(&ctx->calling); -#define CHECKCALLING_INIT(ctx) ctx->calling = 0; -#define CHECKCALLING_DECL int calling; +#define CHECKCALLING_BEGIN(ctx) if (!(spinlock_trylock(&ctx->calling))) { assert(0); } +#define CHECKCALLING_END(ctx) spinlock_unlock(&ctx->calling); +#define CHECKCALLING_INIT(ctx) spinlock_init(&ctx->calling); +#define CHECKCALLING_DESTROY(ctx) spinlock_destroy(&ctx->calling); +#define CHECKCALLING_DECL struct spinlock calling; #else #define CHECKCALLING_BEGIN(ctx) #define CHECKCALLING_END(ctx) #define CHECKCALLING_INIT(ctx) +#define CHECKCALLING_DESTROY(ctx) #define CHECKCALLING_DECL #endif @@ -37,23 +43,30 @@ struct skynet_context { void * instance; struct skynet_module * mod; - uint32_t handle; - int ref; - char result[32]; void * cb_ud; skynet_cb cb; - int session_id; struct message_queue *queue; + FILE * logfile; + uint64_t cpu_cost; // in microsec + uint64_t cpu_start; // in microsec + char result[32]; + uint32_t handle; + int session_id; + int ref; + int message_count; bool init; bool endless; + bool profile; CHECKCALLING_DECL }; struct skynet_node { int total; + int init; uint32_t monitor_exit; pthread_key_t handle_key; + bool profile; // default is off }; static struct skynet_node G_NODE; @@ -65,18 +78,23 @@ skynet_context_total() { static void context_inc() { - __sync_fetch_and_add(&G_NODE.total,1); + ATOM_INC(&G_NODE.total); } static void context_dec() { - __sync_fetch_and_sub(&G_NODE.total,1); + ATOM_DEC(&G_NODE.total); } uint32_t skynet_current_handle(void) { - void * handle = pthread_getspecific(G_NODE.handle_key); - return (uint32_t)(uintptr_t)handle; + if (G_NODE.init) { + void * handle = pthread_getspecific(G_NODE.handle_key); + return (uint32_t)(uintptr_t)handle; + } else { + uint32_t v = (uint32_t)(-THREAD_MAIN); + return v; + } } static void @@ -123,9 +141,17 @@ skynet_context_new(const char * name, const char *param) { ctx->cb = NULL; ctx->cb_ud = NULL; ctx->session_id = 0; + ctx->logfile = NULL; ctx->init = false; ctx->endless = false; + + ctx->cpu_cost = 0; + ctx->cpu_start = 0; + ctx->message_count = 0; + ctx->profile = G_NODE.profile; + // Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle + ctx->handle = 0; ctx->handle = skynet_handle_register(ctx); struct message_queue * queue = ctx->queue = skynet_mq_create(ctx->handle); // init function maybe use ctx->handle, so it must init at last @@ -139,7 +165,7 @@ skynet_context_new(const char * name, const char *param) { if (ret) { ctx->init = true; } - skynet_mq_force_push(queue); + skynet_globalmq_push(queue); if (ret) { skynet_error(ret, "LAUNCH %s %s", name, param ? param : ""); } @@ -158,26 +184,42 @@ skynet_context_new(const char * name, const char *param) { int skynet_context_newsession(struct skynet_context *ctx) { // session always be a positive number - int session = (++ctx->session_id) & 0x7fffffff; + int session = ++ctx->session_id; + if (session <= 0) { + ctx->session_id = 1; + return 1; + } return session; } void skynet_context_grab(struct skynet_context *ctx) { - __sync_add_and_fetch(&ctx->ref,1); + ATOM_INC(&ctx->ref); +} + +void +skynet_context_reserve(struct skynet_context *ctx) { + skynet_context_grab(ctx); + // don't count the context reserved, because skynet abort (the worker threads terminate) only when the total context is 0 . + // the reserved context will be release at last. + context_dec(); } static void delete_context(struct skynet_context *ctx) { + if (ctx->logfile) { + fclose(ctx->logfile); + } skynet_module_instance_release(ctx->mod, ctx->instance); skynet_mq_mark_release(ctx->queue); + CHECKCALLING_DESTROY(ctx) skynet_free(ctx); context_dec(); } struct skynet_context * skynet_context_release(struct skynet_context *ctx) { - if (__sync_sub_and_fetch(&ctx->ref,1) == 0) { + if (ATOM_DEC(&ctx->ref) == 0) { delete_context(ctx); return NULL; } @@ -216,23 +258,48 @@ skynet_isremote(struct skynet_context * ctx, uint32_t handle, int * harbor) { } static void -_dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { +dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { assert(ctx->init); CHECKCALLING_BEGIN(ctx) pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); - int type = msg->sz >> HANDLE_REMOTE_SHIFT; - size_t sz = msg->sz & HANDLE_MASK; - if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) { + 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); + } + ++ctx->message_count; + int reserve_msg; + if (ctx->profile) { + ctx->cpu_start = skynet_thread_time(); + reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); + uint64_t cost_time = skynet_thread_time() - ctx->cpu_start; + ctx->cpu_cost += cost_time; + } else { + reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); + } + if (!reserve_msg) { skynet_free(msg->data); } CHECKCALLING_END(ctx) } -int -skynet_context_message_dispatch(struct skynet_monitor *sm) { - struct message_queue * q = skynet_globalmq_pop(); - if (q==NULL) - return 1; +void +skynet_context_dispatchall(struct skynet_context * ctx) { + // for skynet_error + struct skynet_message msg; + struct message_queue *q = ctx->queue; + while (!skynet_mq_pop(q,&msg)) { + dispatch_message(ctx, &msg); + } +} + +struct message_queue * +skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q, int weight) { + if (q == NULL) { + q = skynet_globalmq_pop(); + if (q==NULL) + return NULL; + } uint32_t handle = skynet_mq_handle(q); @@ -240,30 +307,47 @@ skynet_context_message_dispatch(struct skynet_monitor *sm) { if (ctx == NULL) { struct drop_t d = { handle }; skynet_mq_release(q, drop_message, &d); - return 0; + return skynet_globalmq_pop(); } + int i,n=1; struct skynet_message msg; - if (skynet_mq_pop(q,&msg)) { - skynet_context_release(ctx); - return 0; - } - skynet_monitor_trigger(sm, msg.source , handle); + for (i=0;i= 0) { + n = skynet_mq_length(q); + n >>= weight; + } + int overload = skynet_mq_overload(q); + if (overload) { + skynet_error(ctx, "May overload, message queue length = %d", overload); + } - if (ctx->cb == NULL) { - skynet_free(msg.data); - } else { - _dispatch_message(ctx, &msg); + skynet_monitor_trigger(sm, msg.source , handle); + + if (ctx->cb == NULL) { + skynet_free(msg.data); + } else { + dispatch_message(ctx, &msg); + } + + skynet_monitor_trigger(sm, 0,0); } assert(q == ctx->queue); - skynet_mq_pushglobal(q); + struct message_queue *nq = skynet_globalmq_pop(); + if (nq) { + // If global mq is not empty , push q back, and return next queue (nq) + // Else (global mq is empty or block, don't push q back, and return q again (for next dispatch) + skynet_globalmq_push(q); + q = nq; + } skynet_context_release(ctx); - skynet_monitor_trigger(sm, 0,0); - - return 0; + return q; } static void @@ -328,11 +412,7 @@ cmd_reg(struct skynet_context * context, const char * param) { } else if (param[0] == '.') { return skynet_handle_namehandle(context->handle, param + 1); } else { - assert(context->handle!=0); - struct remote_name *rname = skynet_malloc(sizeof(*rname)); - copy_name(rname->name, param); - rname->handle = context->handle; - skynet_harbor_register(rname); + skynet_error(context, "Can't register global name %s in C", param); return NULL; } } @@ -341,8 +421,10 @@ static const char * cmd_query(struct skynet_context * context, const char * param) { if (param[0] == '.') { uint32_t handle = skynet_handle_findname(param+1); - sprintf(context->result, ":%x", handle); - return context->result; + if (handle) { + sprintf(context->result, ":%x", handle); + return context->result; + } } return NULL; } @@ -363,38 +445,34 @@ cmd_name(struct skynet_context * context, const char * param) { if (name[0] == '.') { return skynet_handle_namehandle(handle_id, name + 1); } else { - struct remote_name *rname = skynet_malloc(sizeof(*rname)); - copy_name(rname->name, name); - rname->handle = handle_id; - skynet_harbor_register(rname); + skynet_error(context, "Can't set global name %s in C", name); } return NULL; } -static const char * -cmd_now(struct skynet_context * context, const char * param) { - uint32_t ti = skynet_gettime(); - sprintf(context->result,"%u",ti); - return context->result; -} - static const char * cmd_exit(struct skynet_context * context, const char * param) { handle_exit(context, 0); return NULL; } -static const char * -cmd_kill(struct skynet_context * context, const char * param) { +static uint32_t +tohandle(struct skynet_context * context, const char * param) { uint32_t handle = 0; if (param[0] == ':') { handle = strtoul(param+1, NULL, 16); } else if (param[0] == '.') { handle = skynet_handle_findname(param+1); } else { - skynet_error(context, "Can't kill %s",param); - // todo : kill global service + skynet_error(context, "Can't convert %s to handle",param); } + + return handle; +} + +static const char * +cmd_kill(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); if (handle) { handle_exit(context, handle); } @@ -443,21 +521,11 @@ cmd_setenv(struct skynet_context * context, const char * param) { static const char * cmd_starttime(struct skynet_context * context, const char * param) { - uint32_t sec = skynet_gettime_fixsec(); + uint32_t sec = skynet_starttime(); sprintf(context->result,"%u",sec); return context->result; } -static const char * -cmd_endless(struct skynet_context * context, const char * param) { - if (context->endless) { - strcpy(context->result, "1"); - context->endless = false; - return context->result; - } - return NULL; -} - static const char * cmd_abort(struct skynet_context * context, const char * param) { skynet_handle_retireall(); @@ -475,42 +543,122 @@ cmd_monitor(struct skynet_context * context, const char * param) { } return NULL; } else { - if (param[0] == ':') { - handle = strtoul(param+1, NULL, 16); - } else if (param[0] == '.') { - handle = skynet_handle_findname(param+1); - } else { - skynet_error(context, "Can't monitor %s",param); - // todo : monitor global service - } + handle = tohandle(context, param); } G_NODE.monitor_exit = handle; return NULL; } static const char * -cmd_mqlen(struct skynet_context * context, const char * param) { - int len = skynet_mq_length(context->queue); - sprintf(context->result, "%d", len); +cmd_stat(struct skynet_context * context, const char * param) { + if (strcmp(param, "mqlen") == 0) { + int len = skynet_mq_length(context->queue); + sprintf(context->result, "%d", len); + } else if (strcmp(param, "endless") == 0) { + if (context->endless) { + strcpy(context->result, "1"); + context->endless = false; + } else { + strcpy(context->result, "0"); + } + } else if (strcmp(param, "cpu") == 0) { + double t = (double)context->cpu_cost / 1000000.0; // microsec + sprintf(context->result, "%lf", t); + } else if (strcmp(param, "time") == 0) { + if (context->profile) { + uint64_t ti = skynet_thread_time() - context->cpu_start; + double t = (double)ti / 1000000.0; // microsec + sprintf(context->result, "%lf", t); + } else { + strcpy(context->result, "0"); + } + } else if (strcmp(param, "message") == 0) { + sprintf(context->result, "%d", context->message_count); + } else { + context->result[0] = '\0'; + } return context->result; } +static const char * +cmd_logon(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + FILE *f = NULL; + FILE * lastf = ctx->logfile; + if (lastf == NULL) { + f = skynet_log_open(context, handle); + if (f) { + if (!ATOM_CAS_POINTER(&ctx->logfile, NULL, f)) { + // logfile opens in other thread, close this one. + fclose(f); + } + } + } + skynet_context_release(ctx); + return NULL; +} + +static const char * +cmd_logoff(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + FILE * f = ctx->logfile; + if (f) { + // logfile may close in other thread + if (ATOM_CAS_POINTER(&ctx->logfile, f, NULL)) { + skynet_log_close(context, f, handle); + } + } + skynet_context_release(ctx); + return NULL; +} + +static const char * +cmd_signal(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + param = strchr(param, ' '); + int sig = 0; + if (param) { + sig = strtol(param, NULL, 0); + } + // NOTICE: the signal function should be thread safe. + skynet_module_instance_signal(ctx->mod, ctx->instance, sig); + + skynet_context_release(ctx); + return NULL; +} + static struct command_func cmd_funcs[] = { { "TIMEOUT", cmd_timeout }, { "REG", cmd_reg }, { "QUERY", cmd_query }, { "NAME", cmd_name }, - { "NOW", cmd_now }, { "EXIT", cmd_exit }, { "KILL", cmd_kill }, { "LAUNCH", cmd_launch }, { "GETENV", cmd_getenv }, { "SETENV", cmd_setenv }, { "STARTTIME", cmd_starttime }, - { "ENDLESS", cmd_endless }, { "ABORT", cmd_abort }, { "MONITOR", cmd_monitor }, - { "MQLEN", cmd_mqlen }, + { "STAT", cmd_stat }, + { "LOGON", cmd_logon }, + { "LOGOFF", cmd_logoff }, + { "SIGNAL", cmd_signal }, { NULL, NULL }, }; @@ -545,12 +693,18 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da *data = msg; } - assert((*sz & HANDLE_MASK) == *sz); - *sz |= type << HANDLE_REMOTE_SHIFT; + *sz |= (size_t)type << MESSAGE_TYPE_SHIFT; } int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { + if ((sz & MESSAGE_TYPE_MASK) != sz) { + skynet_error(context, "The message to %x is too large", destination); + if (type & PTYPE_TAG_DONTCOPY) { + skynet_free(data); + } + return -1; + } _filter_args(context, type, &session, (void **)&data, &sz); if (source == 0) { @@ -582,8 +736,10 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati } int -skynet_sendname(struct skynet_context * context, const char * addr , int type, int session, void * data, size_t sz) { - uint32_t source = context->handle; +skynet_sendname(struct skynet_context * context, uint32_t source, const char * addr , int type, int session, void * data, size_t sz) { + if (source == 0) { + source = context->handle; + } uint32_t des = 0; if (addr[0] == ':') { des = strtoul(addr+1, NULL, 16); @@ -593,7 +749,7 @@ skynet_sendname(struct skynet_context * context, const char * addr , int type, i if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } - return session; + return -1; } } else { _filter_args(context, type, &session, (void **)&data, &sz); @@ -616,11 +772,6 @@ skynet_context_handle(struct skynet_context *ctx) { return ctx->handle; } -void -skynet_context_init(struct skynet_context *ctx, uint32_t handle) { - ctx->handle = handle; -} - void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb) { context->cb = cb; @@ -633,7 +784,7 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t smsg.source = source; smsg.session = session; smsg.data = msg; - smsg.sz = sz | type << HANDLE_REMOTE_SHIFT; + smsg.sz = sz | (size_t)type << MESSAGE_TYPE_SHIFT; skynet_mq_push(ctx->queue, &smsg); } @@ -642,6 +793,7 @@ void skynet_globalinit(void) { G_NODE.total = 0; G_NODE.monitor_exit = 0; + G_NODE.init = 1; if (pthread_key_create(&G_NODE.handle_key, NULL)) { fprintf(stderr, "pthread_key_create failed"); exit(1); @@ -661,3 +813,7 @@ skynet_initthread(int m) { pthread_setspecific(G_NODE.handle_key, (void *)v); } +void +skynet_profile_enable(int enable) { + G_NODE.profile = (bool)enable; +} diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 7adc1292..0644dc4d 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -10,14 +10,15 @@ struct skynet_monitor; struct skynet_context * skynet_context_new(const char * name, const char * parm); void skynet_context_grab(struct skynet_context *); +void skynet_context_reserve(struct skynet_context *ctx); struct skynet_context * skynet_context_release(struct skynet_context *); uint32_t skynet_context_handle(struct skynet_context *); -void skynet_context_init(struct skynet_context *, uint32_t handle); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); -int skynet_context_message_dispatch(struct skynet_monitor *); // return 1 when block +struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *, int weight); // return next queue int skynet_context_total(); +void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit void skynet_context_endless(uint32_t handle); // for monitor @@ -25,4 +26,6 @@ void skynet_globalinit(void); void skynet_globalexit(void); void skynet_initthread(int m); +void skynet_profile_enable(int enable); + #endif diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 3654d591..ef255060 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -33,13 +33,16 @@ skynet_socket_free() { static void forward_message(int type, bool padding, struct socket_message * result) { struct skynet_socket_message *sm; - int sz = sizeof(*sm); + size_t sz = sizeof(*sm); if (padding) { if (result->data) { - sz += strlen(result->data) + 1; + size_t msg_sz = strlen(result->data); + if (msg_sz > 128) { + msg_sz = 128; + } + sz += msg_sz; } else { result->data = ""; - sz += 1; } } sm = (struct skynet_socket_message *)skynet_malloc(sz); @@ -48,7 +51,7 @@ forward_message(int type, bool padding, struct socket_message * result) { sm->ud = result->ud; if (padding) { sm->buffer = NULL; - strcpy((char*)(sm+1), result->data); + memcpy(sm+1, result->data, sz - sizeof(*sm)); } else { sm->buffer = result->data; } @@ -57,11 +60,12 @@ forward_message(int type, bool padding, struct socket_message * result) { message.source = 0; message.session = 0; message.data = sm; - message.sz = sz | PTYPE_SOCKET << HANDLE_REMOTE_SHIFT; + message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT); if (skynet_context_push((uint32_t)result->opaque, &message)) { // todo: report somewhere to close socket // don't call skynet_socket_close here (It will block mainloop) + skynet_free(sm->buffer); skynet_free(sm); } } @@ -85,12 +89,18 @@ skynet_socket_poll() { case SOCKET_OPEN: forward_message(SKYNET_SOCKET_TYPE_CONNECT, true, &result); break; - case SOCKET_ERROR: - forward_message(SKYNET_SOCKET_TYPE_ERROR, false, &result); + case SOCKET_ERR: + forward_message(SKYNET_SOCKET_TYPE_ERROR, true, &result); break; case SOCKET_ACCEPT: forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result); break; + case SOCKET_UDP: + forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result); + break; + case SOCKET_WARNING: + forward_message(SKYNET_SOCKET_TYPE_WARNING, false, &result); + break; default: skynet_error(NULL, "Unknown socket message type %d.",type); return -1; @@ -103,22 +113,12 @@ skynet_socket_poll() { int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { - int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); - if (wsz < 0) { - skynet_free(buffer); - return -1; - } else if (wsz > 1024 * 1024) { - int kb4 = wsz / 1024 / 4; - if (kb4 % 256 == 0) { - skynet_error(ctx, "%d Mb bytes on socket %d need to send out", (int)(wsz / (1024 * 1024)), id); - } - } - return 0; + return socket_server_send(SOCKET_SERVER, id, buffer, sz); } -void +int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { - socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); + return socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); } int @@ -133,12 +133,6 @@ skynet_socket_connect(struct skynet_context *ctx, const char *host, int port) { return socket_server_connect(SOCKET_SERVER, source, host, port); } -int -skynet_socket_block_connect(struct skynet_context *ctx, const char *host, int port) { - uint32_t source = skynet_context_handle(ctx); - return socket_server_block_connect(SOCKET_SERVER, source, host, port); -} - int skynet_socket_bind(struct skynet_context *ctx, int fd) { uint32_t source = skynet_context_handle(ctx); @@ -151,8 +145,48 @@ skynet_socket_close(struct skynet_context *ctx, int id) { socket_server_close(SOCKET_SERVER, source, id); } +void +skynet_socket_shutdown(struct skynet_context *ctx, int id) { + uint32_t source = skynet_context_handle(ctx); + socket_server_shutdown(SOCKET_SERVER, source, id); +} + void skynet_socket_start(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_start(SOCKET_SERVER, source, id); } + +void +skynet_socket_nodelay(struct skynet_context *ctx, int id) { + socket_server_nodelay(SOCKET_SERVER, id); +} + +int +skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { + uint32_t source = skynet_context_handle(ctx); + return socket_server_udp(SOCKET_SERVER, source, addr, port); +} + +int +skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port) { + return socket_server_udp_connect(SOCKET_SERVER, id, addr, port); +} + +int +skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) { + return socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz); +} + +const char * +skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) { + if (msg->type != SKYNET_SOCKET_TYPE_UDP) { + return NULL; + } + struct socket_message sm; + sm.id = msg->id; + sm.opaque = 0; + sm.ud = msg->ud; + sm.data = msg->buffer; + return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz); +} diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 3275521d..21dfcef4 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -8,6 +8,8 @@ struct skynet_context; #define SKYNET_SOCKET_TYPE_CLOSE 3 #define SKYNET_SOCKET_TYPE_ACCEPT 4 #define SKYNET_SOCKET_TYPE_ERROR 5 +#define SKYNET_SOCKET_TYPE_UDP 6 +#define SKYNET_SOCKET_TYPE_WARNING 7 struct skynet_socket_message { int type; @@ -22,12 +24,18 @@ void skynet_socket_free(); int skynet_socket_poll(); int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz); -void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz); +int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz); int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog); int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port); -int skynet_socket_block_connect(struct skynet_context *ctx, const char *host, int port); int skynet_socket_bind(struct skynet_context *ctx, int fd); void skynet_socket_close(struct skynet_context *ctx, int id); +void skynet_socket_shutdown(struct skynet_context *ctx, int id); void skynet_socket_start(struct skynet_context *ctx, int id); +void skynet_socket_nodelay(struct skynet_context *ctx, int id); + +int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); +int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); +int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz); +const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); #endif diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index d1f80e10..2f5ddea1 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -7,6 +7,8 @@ #include "skynet_timer.h" #include "skynet_monitor.h" #include "skynet_socket.h" +#include "skynet_daemon.h" +#include "skynet_harbor.h" #include #include @@ -14,6 +16,7 @@ #include #include #include +#include struct monitor { int count; @@ -21,13 +24,24 @@ struct monitor { pthread_cond_t cond; pthread_mutex_t mutex; int sleep; + int quit; }; struct worker_parm { struct monitor *m; int id; + int weight; }; +static int SIG = 0; + +static void +handle_hup(int signal) { + if (signal == SIGHUP) { + SIG = 1; + } +} + #define CHECK_ABORT if (skynet_context_total()==0) break; static void @@ -47,7 +61,7 @@ wakeup(struct monitor *m, int busy) { } static void * -_socket(void *p) { +thread_socket(void *p) { struct monitor * m = p; skynet_initthread(THREAD_SOCKET); for (;;) { @@ -77,7 +91,7 @@ free_monitor(struct monitor *m) { } static void * -_monitor(void *p) { +thread_monitor(void *p) { struct monitor * m = p; int i; int n = m->count; @@ -96,8 +110,23 @@ _monitor(void *p) { return NULL; } +static void +signal_hup() { + // make log file reopen + + struct skynet_message smsg; + smsg.source = 0; + smsg.session = 0; + smsg.data = NULL; + smsg.sz = (size_t)PTYPE_SYSTEM << MESSAGE_TYPE_SHIFT; + uint32_t logger = skynet_handle_findname("logger"); + if (logger) { + skynet_context_push(logger, &smsg); + } +} + static void * -_timer(void *p) { +thread_timer(void *p) { struct monitor * m = p; skynet_initthread(THREAD_TIMER); for (;;) { @@ -105,42 +134,52 @@ _timer(void *p) { CHECK_ABORT wakeup(m,m->count-1); usleep(2500); + if (SIG) { + signal_hup(); + SIG = 0; + } } // wakeup socket thread skynet_socket_exit(); // wakeup all worker thread + pthread_mutex_lock(&m->mutex); + m->quit = 1; pthread_cond_broadcast(&m->cond); + pthread_mutex_unlock(&m->mutex); return NULL; } static void * -_worker(void *p) { +thread_worker(void *p) { struct worker_parm *wp = p; int id = wp->id; + int weight = wp->weight; struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); - for (;;) { - if (skynet_context_message_dispatch(sm)) { - CHECK_ABORT + struct message_queue * q = NULL; + while (!m->quit) { + q = skynet_context_message_dispatch(sm, q, weight); + if (q == NULL) { if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. - pthread_cond_wait(&m->cond, &m->mutex); + if (!m->quit) + pthread_cond_wait(&m->cond, &m->mutex); -- m->sleep; if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); } } - } + } } return NULL; } static void -_start(int thread) { +start(int thread) { pthread_t pid[thread+3]; struct monitor *m = skynet_malloc(sizeof(*m)); @@ -162,15 +201,25 @@ _start(int thread) { exit(1); } - create_thread(&pid[0], _monitor, m); - create_thread(&pid[1], _timer, m); - create_thread(&pid[2], _socket, m); + create_thread(&pid[0], thread_monitor, m); + create_thread(&pid[1], thread_timer, m); + create_thread(&pid[2], thread_socket, m); + static int weight[] = { + -1, -1, -1, -1, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, }; struct worker_parm wp[thread]; for (i=0;idaemon) { + if (daemon_init(config->daemon)) { + exit(1); + } + } skynet_harbor_init(config->harbor); skynet_handle_init(config->harbor); skynet_mq_init(); skynet_module_init(config->module_path); skynet_timer_init(); skynet_socket_init(); + skynet_profile_enable(config->profile); - bootstrap(config->bootstrap); + struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger); + if (ctx == NULL) { + fprintf(stderr, "Can't launch %s service\n", config->logservice); + exit(1); + } - _start(config->thread); + bootstrap(ctx, config->bootstrap); + + start(config->thread); + + // harbor_exit may call socket send, so it should exit before socket_free + skynet_harbor_exit(); skynet_socket_free(); + if (config->daemon) { + daemon_exit(config->daemon); + } } diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 489f8d09..6e1497d3 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -4,14 +4,18 @@ #include "skynet_mq.h" #include "skynet_server.h" #include "skynet_handle.h" +#include "spinlock.h" #include #include #include #include +#include #if defined(__APPLE__) #include +#include +#include #endif typedef void (*timer_execute_func)(void *ud,void *arg); @@ -30,7 +34,7 @@ struct timer_event { struct timer_node { struct timer_node *next; - int expire; + uint32_t expire; }; struct link_list { @@ -40,18 +44,18 @@ struct link_list { struct timer { struct link_list near[TIME_NEAR]; - struct link_list t[4][TIME_LEVEL-1]; - int lock; - int time; - uint32_t current; + struct link_list t[4][TIME_LEVEL]; + struct spinlock lock; + uint32_t time; uint32_t starttime; + uint64_t current; + uint64_t current_point; }; static struct timer * TI = NULL; static inline struct timer_node * -link_clear(struct link_list *list) -{ +link_clear(struct link_list *list) { struct timer_node * ret = list->head.next; list->head.next = 0; list->tail = &(list->head); @@ -60,71 +64,95 @@ link_clear(struct link_list *list) } static inline void -link(struct link_list *list,struct timer_node *node) -{ +link(struct link_list *list,struct timer_node *node) { list->tail->next = node; list->tail = node; node->next=0; } static void -add_node(struct timer *T,struct timer_node *node) -{ - int time=node->expire; - int current_time=T->time; +add_node(struct timer *T,struct timer_node *node) { + uint32_t time=node->expire; + uint32_t current_time=T->time; if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) { link(&T->near[time&TIME_NEAR_MASK],node); - } - else { + } else { int i; - int mask=TIME_NEAR << TIME_LEVEL_SHIFT; + uint32_t mask=TIME_NEAR << TIME_LEVEL_SHIFT; for (i=0;i<3;i++) { if ((time|(mask-1))==(current_time|(mask-1))) { break; } mask <<= TIME_LEVEL_SHIFT; } - link(&T->t[i][((time>>(TIME_NEAR_SHIFT + i*TIME_LEVEL_SHIFT)) & TIME_LEVEL_MASK)-1],node); + + link(&T->t[i][((time>>(TIME_NEAR_SHIFT + i*TIME_LEVEL_SHIFT)) & TIME_LEVEL_MASK)],node); } } static void -timer_add(struct timer *T,void *arg,size_t sz,int time) -{ +timer_add(struct timer *T,void *arg,size_t sz,int time) { struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz); memcpy(node+1,arg,sz); - while (__sync_lock_test_and_set(&T->lock,1)) {}; + SPIN_LOCK(T); node->expire=time+T->time; add_node(T,node); - __sync_lock_release(&T->lock); + SPIN_UNLOCK(T); +} + +static void +move_list(struct timer *T, int level, int idx) { + struct timer_node *current = link_clear(&T->t[level][idx]); + while (current) { + struct timer_node *temp=current->next; + add_node(T,current); + current=temp; + } } static void timer_shift(struct timer *T) { int mask = TIME_NEAR; - int time = (++T->time) >> TIME_NEAR_SHIFT; - int i=0; - - while ((T->time & (mask-1))==0) { - int idx=time & TIME_LEVEL_MASK; - if (idx!=0) { - --idx; - struct timer_node *current = link_clear(&T->t[i][idx]); - while (current) { - struct timer_node *temp=current->next; - add_node(T,current); - current=temp; + uint32_t ct = ++T->time; + if (ct == 0) { + move_list(T, 3, 0); + } else { + uint32_t time = ct >> TIME_NEAR_SHIFT; + int i=0; + + while ((ct & (mask-1))==0) { + int idx=time & TIME_LEVEL_MASK; + if (idx!=0) { + move_list(T, i, idx); + break; } - break; + mask <<= TIME_LEVEL_SHIFT; + time >>= TIME_LEVEL_SHIFT; + ++i; } - mask <<= TIME_LEVEL_SHIFT; - time >>= TIME_LEVEL_SHIFT; - ++i; - } + } +} + +static inline void +dispatch_list(struct timer_node *current) { + do { + struct timer_event * event = (struct timer_event *)(current+1); + struct skynet_message message; + message.source = 0; + message.session = event->session; + message.data = NULL; + message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; + + skynet_context_push(event->handle, &message); + + struct timer_node * temp = current; + current=current->next; + skynet_free(temp); + } while (current); } static inline void @@ -133,42 +161,30 @@ timer_execute(struct timer *T) { while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); - - do { - struct timer_event * event = (struct timer_event *)(current+1); - struct skynet_message message; - message.source = 0; - message.session = event->session; - message.data = NULL; - message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; - - skynet_context_push(event->handle, &message); - - struct timer_node * temp = current; - current=current->next; - skynet_free(temp); - } while (current); + SPIN_UNLOCK(T); + // dispatch_list don't need lock T + dispatch_list(current); + SPIN_LOCK(T); } } static void -timer_update(struct timer *T) -{ - while (__sync_lock_test_and_set(&T->lock,1)) {}; +timer_update(struct timer *T) { + SPIN_LOCK(T); // try to dispatch timeout 0 (rare condition) timer_execute(T); // shift time first, and then dispatch timer message timer_shift(T); + timer_execute(T); - __sync_lock_release(&T->lock); + SPIN_UNLOCK(T); } static struct timer * -timer_create_timer() -{ +timer_create_timer() { struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer)); memset(r,0,sizeof(*r)); @@ -179,12 +195,13 @@ timer_create_timer() } for (i=0;i<4;i++) { - for (j=0;jt[i][j]); } } - r->lock = 0; + SPIN_INIT(r) + r->current = 0; return r; @@ -192,12 +209,12 @@ timer_create_timer() int skynet_timeout(uint32_t handle, int time, int session) { - if (time == 0) { + if (time <= 0) { struct skynet_message message; message.source = 0; message.session = session; message.data = NULL; - message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; + message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; if (skynet_context_push(handle, &message)) { return -1; @@ -212,18 +229,34 @@ skynet_timeout(uint32_t handle, int time, int session) { return session; } -static uint32_t -_gettime(void) { - uint32_t t; +// centisecond: 1/100 second +static void +systime(uint32_t *sec, uint32_t *cs) { +#if !defined(__APPLE__) + struct timespec ti; + clock_gettime(CLOCK_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__) struct timespec ti; clock_gettime(CLOCK_MONOTONIC, &ti); - t = (uint32_t)(ti.tv_sec & 0xffffff) * 100; + t = (uint64_t)ti.tv_sec * 100; t += ti.tv_nsec / 10000000; #else struct timeval tv; gettimeofday(&tv, NULL); - t = (uint32_t)(tv.tv_sec & 0xffffff) * 100; + t = (uint64_t)tv.tv_sec * 100; t += tv.tv_usec / 10000; #endif return t; @@ -231,10 +264,14 @@ _gettime(void) { void skynet_updatetime(void) { - uint32_t ct = _gettime(); - if (ct != TI->current) { - int diff = ct>=TI->current?ct-TI->current:(0xffffff+1)*100-TI->current+ct; - TI->current = ct; + uint64_t cp = gettime(); + if(cp < TI->current_point) { + skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); + TI->current_point = cp; + } else if (cp != TI->current_point) { + uint32_t diff = (uint32_t)(cp - TI->current_point); + TI->current_point = cp; + TI->current += diff; int i; for (i=0;istarttime; } -uint32_t -skynet_gettime(void) { +uint64_t +skynet_now(void) { return TI->current; } void skynet_timer_init(void) { TI = timer_create_timer(); - TI->current = _gettime(); - -#if !defined(__APPLE__) - struct timespec ti; - clock_gettime(CLOCK_REALTIME, &ti); - uint32_t sec = (uint32_t)ti.tv_sec; -#else - struct timeval tv; - gettimeofday(&tv, NULL); - uint32_t sec = (uint32_t)tv.tv_sec; -#endif - uint32_t mono = _gettime() / 100; - - TI->starttime = sec - mono; + uint32_t current = 0; + systime(&TI->starttime, ¤t); + TI->current = current; + TI->current_point = gettime(); +} + +// for profile + +#define NANOSEC 1000000000 +#define MICROSEC 1000000 + +uint64_t +skynet_thread_time(void) { +#if !defined(__APPLE__) + struct timespec ti; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); + + return (uint64_t)ti.tv_sec * MICROSEC + (uint64_t)ti.tv_nsec / (NANOSEC / MICROSEC); +#else + struct task_thread_times_info aTaskInfo; + mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; + if (KERN_SUCCESS != task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)) { + return 0; + } + + return (uint64_t)(aTaskInfo.user_time.seconds) + (uint64_t)aTaskInfo.user_time.microseconds; +#endif } diff --git a/skynet-src/skynet_timer.h b/skynet-src/skynet_timer.h index 4d4e2b0f..c204fff0 100644 --- a/skynet-src/skynet_timer.h +++ b/skynet-src/skynet_timer.h @@ -5,8 +5,8 @@ int skynet_timeout(uint32_t handle, int time, int session); void skynet_updatetime(void); -uint32_t skynet_gettime(void); -uint32_t skynet_gettime_fixsec(void); +uint32_t skynet_starttime(void); +uint64_t skynet_thread_time(void); // for profile, in micro second void skynet_timer_init(void); diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index 2fc8903a..cd1f9018 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -58,7 +58,8 @@ sp_wait(int efd, struct event *e, int max) { e[i].s = ev[i].data.ptr; unsigned flag = ev[i].events; e[i].write = (flag & EPOLLOUT) != 0; - e[i].read = (flag & EPOLLIN) != 0; + e[i].read = (flag & (EPOLLIN | EPOLLHUP)) != 0; + e[i].error = (flag & EPOLLERR) != 0; } return n; diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index 54884b6a..8aa31203 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -38,17 +38,17 @@ static int sp_add(int kfd, int sock, void *ud) { struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, EV_ADD, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_ADD, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL); kevent(kfd, &ke, 1, NULL, 0, NULL); return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_DISABLE, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { sp_del(kfd, sock); return 1; } @@ -59,7 +59,7 @@ static void sp_write(int kfd, int sock, void *ud, bool enable) { struct kevent ke; EV_SET(&ke, sock, EVFILT_WRITE, enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { // todo: check error } } @@ -75,6 +75,7 @@ sp_wait(int kfd, struct event *e, int max) { unsigned filter = ev[i].filter; e[i].write = (filter == EVFILT_WRITE); e[i].read = (filter == EVFILT_READ); + e[i].error = false; // kevent has not error event } return n; diff --git a/skynet-src/socket_poll.h b/skynet-src/socket_poll.h index 0f36308a..cfd7e870 100644 --- a/skynet-src/socket_poll.h +++ b/skynet-src/socket_poll.h @@ -9,6 +9,7 @@ struct event { void * s; bool read; bool write; + bool error; }; static bool sp_invalid(poll_fd fd); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f013fd1b..3841f233 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2,9 +2,11 @@ #include "socket_server.h" #include "socket_poll.h" +#include "atomic.h" #include #include +#include #include #include #include @@ -34,27 +36,56 @@ #define PRIORITY_HIGH 0 #define PRIORITY_LOW 1 +#define HASH_ID(id) (((unsigned)id) % MAX_SOCKET) + +#define PROTOCOL_TCP 0 +#define PROTOCOL_UDP 1 +#define PROTOCOL_UDPv6 2 + +#define UDP_ADDRESS_SIZE 19 // ipv6 128bit + port 16bit + 1 byte type + +#define MAX_UDP_PACKAGE 65535 + +// EAGAIN and EWOULDBLOCK may be not the same value. +#if (EAGAIN != EWOULDBLOCK) +#define AGAIN_WOULDBLOCK EAGAIN : case EWOULDBLOCK +#else +#define AGAIN_WOULDBLOCK EAGAIN +#endif + +#define WARNING_SIZE (1024*1024) + struct write_buffer { struct write_buffer * next; + void *buffer; char *ptr; int sz; - void *buffer; + bool userobject; + uint8_t udp_address[UDP_ADDRESS_SIZE]; }; +#define SIZEOF_TCPBUFFER (offsetof(struct write_buffer, udp_address[0])) +#define SIZEOF_UDPBUFFER (sizeof(struct write_buffer)) + struct wb_list { struct write_buffer * head; struct write_buffer * tail; }; struct socket { - int fd; - int id; - int type; - int size; - int64_t wb_size; uintptr_t opaque; struct wb_list high; struct wb_list low; + int64_t wb_size; + int fd; + int id; + uint16_t protocol; + uint16_t type; + int64_t warn_size; + union { + int size; + uint8_t udp_address[UDP_ADDRESS_SIZE]; + } p; }; struct socket_server { @@ -65,9 +96,11 @@ struct socket_server { int alloc_id; int event_n; int event_index; + struct socket_object_interface soi; struct event ev[MAX_EVENT]; struct socket slot[MAX_SOCKET]; char buffer[MAX_INFO]; + uint8_t udpbuffer[MAX_UDP_PACKAGE]; fd_set rfds; }; @@ -84,8 +117,19 @@ struct request_send { char * buffer; }; +struct request_send_udp { + struct request_send send; + uint8_t address[UDP_ADDRESS_SIZE]; +}; + +struct request_setudp { + int id; + uint8_t address[UDP_ADDRESS_SIZE]; +}; + struct request_close { int id; + int shutdown; uintptr_t opaque; }; @@ -107,16 +151,50 @@ struct request_start { uintptr_t opaque; }; +struct request_setopt { + int id; + int what; + int value; +}; + +struct request_udp { + int id; + int fd; + int family; + uintptr_t opaque; +}; + +/* + The first byte is TYPE + + S Start socket + B Bind socket + L Listen socket + K Close socket + O Connect to (Open) + X Exit + D Send package (high) + P Send package (low) + A Send UDP package + T Set opt + U Create UDP socket + C set udp address + */ + struct request_package { uint8_t header[8]; // 6 bytes dummy union { char buffer[256]; struct request_open open; struct request_send send; + struct request_send_udp send_udp; struct request_close close; struct request_listen listen; struct request_bind bind; struct request_start start; + struct request_setopt setopt; + struct request_udp udp; + struct request_setudp set_udp; } u; uint8_t dummy[256]; }; @@ -127,9 +205,40 @@ union sockaddr_all { struct sockaddr_in6 v6; }; +struct send_object { + void * buffer; + int sz; + void (*free_func)(void *); +}; + #define MALLOC skynet_malloc #define FREE skynet_free +static inline bool +send_object_init(struct socket_server *ss, struct send_object *so, void *object, int sz) { + if (sz < 0) { + so->buffer = ss->soi.buffer(object); + so->sz = ss->soi.size(object); + so->free_func = ss->soi.free; + return true; + } else { + so->buffer = object; + so->sz = sz; + so->free_func = FREE; + return false; + } +} + +static inline void +write_buffer_free(struct socket_server *ss, struct write_buffer *wb) { + if (wb->userobject) { + ss->soi.free(wb->buffer); + } else { + FREE(wb->buffer); + } + FREE(wb); +} + static void socket_keepalive(int fd) { int keepalive = 1; @@ -140,13 +249,15 @@ static int reserve_id(struct socket_server *ss) { int i; for (i=0;ialloc_id), 1); + int id = ATOM_INC(&(ss->alloc_id)); if (id < 0) { - id = __sync_and_and_fetch(&(ss->alloc_id), 0x7fffffff); + id = ATOM_AND(&(ss->alloc_id), 0x7fffffff); } - struct socket *s = &ss->slot[id % MAX_SOCKET]; + struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID) { - if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { + if (ATOM_CAS(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { + s->id = id; + s->fd = -1; return id; } else { // retry @@ -201,6 +312,7 @@ socket_server_create() { ss->alloc_id = 0; ss->event_n = 0; ss->event_index = 0; + memset(&ss->soi, 0, sizeof(ss->soi)); FD_ZERO(&ss->rfds); assert(ss->recvctrl_fd < FD_SETSIZE); @@ -208,13 +320,12 @@ socket_server_create() { } static void -free_wb_list(struct wb_list *list) { +free_wb_list(struct socket_server *ss, struct wb_list *list) { struct write_buffer *wb = list->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; - FREE(tmp->buffer); - FREE(tmp); + write_buffer_free(ss, tmp); } list->head = NULL; list->tail = NULL; @@ -230,13 +341,15 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_message *r return; } assert(s->type != SOCKET_TYPE_RESERVE); - free_wb_list(&s->high); - free_wb_list(&s->low); + free_wb_list(ss,&s->high); + free_wb_list(ss,&s->low); if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { sp_del(ss->event_fd, s->fd); } if (s->type != SOCKET_TYPE_BIND) { - close(s->fd); + if (close(s->fd) < 0) { + perror("close socket:"); + } } s->type = SOCKET_TYPE_INVALID; } @@ -264,8 +377,8 @@ check_wb_list(struct wb_list *s) { } static struct socket * -new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { - struct socket * s = &ss->slot[id % MAX_SOCKET]; +new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool add) { + struct socket * s = &ss->slot[HASH_ID(id)]; assert(s->type == SOCKET_TYPE_RESERVE); if (add) { @@ -277,9 +390,11 @@ new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { s->id = id; s->fd = fd; - s->size = MIN_READ_BUFFER; + s->protocol = protocol; + s->p.size = MIN_READ_BUFFER; s->opaque = opaque; s->wb_size = 0; + s->warn_size = 0; check_wb_list(&s->high); check_wb_list(&s->low); return s; @@ -287,7 +402,7 @@ new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { // return -1 when connecting static int -open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result, bool blocking) { +open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result) { int id = request->id; result->opaque = request->opaque; result->id = id; @@ -300,13 +415,14 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock struct addrinfo *ai_ptr = NULL; char port[16]; sprintf(port, "%d", request->port); - memset( &ai_hints, 0, sizeof( ai_hints ) ); + memset(&ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_STREAM; ai_hints.ai_protocol = IPPROTO_TCP; status = getaddrinfo( request->host, port, &ai_hints, &ai_list ); if ( status != 0 ) { + result->data = (void *)gai_strerror(status); goto _failed; } int sock= -1; @@ -316,28 +432,25 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock continue; } socket_keepalive(sock); - if (!blocking) { - sp_nonblocking(sock); - } + sp_nonblocking(sock); status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if ( status != 0 && errno != EINPROGRESS) { close(sock); sock = -1; continue; } - if (blocking) { - sp_nonblocking(sock); - } break; } if (sock < 0) { + result->data = strerror(errno); goto _failed; } - ns = new_fd(ss, id, sock, request->opaque, true); + ns = new_fd(ss, id, sock, PROTOCOL_TCP, request->opaque, true); if (ns == NULL) { close(sock); + result->data = "reach skynet socket number limit"; goto _failed; } @@ -359,12 +472,12 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return -1; _failed: freeaddrinfo( ai_list ); - ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID; - return SOCKET_ERROR; + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + return SOCKET_ERR; } static int -send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { +send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { while (list->head) { struct write_buffer * tmp = list->head; for (;;) { @@ -373,7 +486,7 @@ send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, stru switch(errno) { case EINTR: continue; - case EAGAIN: + case AGAIN_WOULDBLOCK: return -1; } force_close(ss,s, result); @@ -388,14 +501,81 @@ send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, stru break; } list->head = tmp->next; - FREE(tmp->buffer); - FREE(tmp); + write_buffer_free(ss,tmp); } list->tail = NULL; return -1; } +static socklen_t +udp_socket_address(struct socket *s, const uint8_t udp_address[UDP_ADDRESS_SIZE], union sockaddr_all *sa) { + int type = (uint8_t)udp_address[0]; + if (type != s->protocol) + return 0; + uint16_t port = 0; + memcpy(&port, udp_address+1, sizeof(uint16_t)); + switch (s->protocol) { + case PROTOCOL_UDP: + memset(&sa->v4, 0, sizeof(sa->v4)); + sa->s.sa_family = AF_INET; + sa->v4.sin_port = port; + memcpy(&sa->v4.sin_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v4.sin_addr)); // ipv4 address is 32 bits + return sizeof(sa->v4); + case PROTOCOL_UDPv6: + memset(&sa->v6, 0, sizeof(sa->v6)); + sa->s.sa_family = AF_INET6; + sa->v6.sin6_port = port; + memcpy(&sa->v6.sin6_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v6.sin6_addr)); // ipv6 address is 128 bits + return sizeof(sa->v6); + } + return 0; +} + +static int +send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { + while (list->head) { + struct write_buffer * tmp = list->head; + union sockaddr_all sa; + socklen_t sasz = udp_socket_address(s, tmp->udp_address, &sa); + int err = sendto(s->fd, tmp->ptr, tmp->sz, 0, &sa.s, sasz); + if (err < 0) { + switch(errno) { + case EINTR: + case AGAIN_WOULDBLOCK: + return -1; + } + fprintf(stderr, "socket-server : udp (%d) sendto error %s.\n",s->id, strerror(errno)); + return -1; +/* // ignore udp sendto error + + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + + return SOCKET_ERR; +*/ + } + + s->wb_size -= tmp->sz; + list->head = tmp->next; + write_buffer_free(ss,tmp); + } + list->tail = NULL; + + return -1; +} + +static int +send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { + if (s->protocol == PROTOCOL_TCP) { + return send_list_tcp(ss, s, list, result); + } else { + return send_list_udp(ss, s, list, result); + } +} + static inline int list_uncomplete(struct wb_list *s) { struct write_buffer *wb = s->head; @@ -422,6 +602,11 @@ raise_uncomplete(struct socket * s) { high->head = high->tail = tmp; } +static inline int +send_buffer_empty(struct socket *s) { + return (s->high.head == NULL && s->low.head == NULL); +} + /* Each socket has two write buffer list, high priority and low priority. @@ -446,26 +631,37 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *r // step 3 if (list_uncomplete(&s->low)) { raise_uncomplete(s); + return -1; } - } else { - // step 4 - sp_write(ss->event_fd, s->fd, s, false); + } + // step 4 + assert(send_buffer_empty(s) && s->wb_size == 0); + sp_write(ss->event_fd, s->fd, s, false); - if (s->type == SOCKET_TYPE_HALFCLOSE) { + if (s->type == SOCKET_TYPE_HALFCLOSE) { force_close(ss, s, result); return SOCKET_CLOSE; - } + } + if(s->warn_size > 0){ + s->warn_size = 0; + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + return SOCKET_WARNING; } } return -1; } -static int -append_sendbuffer_(struct wb_list *s, struct request_send * request, int n) { - struct write_buffer * buf = MALLOC(sizeof(*buf)); - buf->ptr = request->buffer+n; - buf->sz = request->sz - n; +static struct write_buffer * +append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_send * request, int size, int n) { + struct write_buffer * buf = MALLOC(size); + struct send_object so; + buf->userobject = send_object_init(ss, &so, request->buffer, request->sz); + buf->ptr = (char*)so.buffer+n; + buf->sz = so.sz - n; buf->buffer = request->buffer; buf->next = NULL; if (s->head == NULL) { @@ -476,24 +672,30 @@ append_sendbuffer_(struct wb_list *s, struct request_send * request, int n) { s->tail->next = buf; s->tail = buf; } - return buf->sz; + return buf; } static inline void -append_sendbuffer(struct socket *s, struct request_send * request, int n) { - s->wb_size += append_sendbuffer_(&s->high, request, n); +append_sendbuffer_udp(struct socket_server *ss, struct socket *s, int priority, struct request_send * request, const uint8_t udp_address[UDP_ADDRESS_SIZE]) { + struct wb_list *wl = (priority == PRIORITY_HIGH) ? &s->high : &s->low; + struct write_buffer *buf = append_sendbuffer_(ss, wl, request, SIZEOF_UDPBUFFER, 0); + memcpy(buf->udp_address, udp_address, UDP_ADDRESS_SIZE); + s->wb_size += buf->sz; } static inline void -append_sendbuffer_low(struct socket *s, struct request_send * request) { - s->wb_size += append_sendbuffer_(&s->low, request, 0); +append_sendbuffer(struct socket_server *ss, struct socket *s, struct request_send * request, int n) { + struct write_buffer *buf = append_sendbuffer_(ss, &s->high, request, SIZEOF_TCPBUFFER, n); + s->wb_size += buf->sz; } -static inline int -send_buffer_empty(struct socket *s) { - return (s->high.head == NULL && s->low.head == NULL); +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, 0); + s->wb_size += buf->sz; } + /* When send a package , we can assign the priority : PRIORITY_HIGH or PRIORITY_LOW @@ -502,43 +704,81 @@ send_buffer_empty(struct socket *s) { Else append package to high (PRIORITY_HIGH) or low (PRIORITY_LOW) list. */ static int -send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) { +send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority, const uint8_t *udp_address) { int id = request->id; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; + struct send_object so; + send_object_init(ss, &so, request->buffer, request->sz); if (s->type == SOCKET_TYPE_INVALID || s->id != id || s->type == SOCKET_TYPE_HALFCLOSE || s->type == SOCKET_TYPE_PACCEPT) { - FREE(request->buffer); + so.free_func(request->buffer); return -1; } - assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN); - if (send_buffer_empty(s)) { - int n = write(s->fd, request->buffer, request->sz); - if (n<0) { - switch(errno) { - case EINTR: - case EAGAIN: - n = 0; - break; - default: - fprintf(stderr, "socket-server: write to %d (fd=%d) error.",id,s->fd); - force_close(ss,s,result); - return SOCKET_CLOSE; + if (s->type == SOCKET_TYPE_PLISTEN || s->type == SOCKET_TYPE_LISTEN) { + fprintf(stderr, "socket-server: write to listen fd %d.\n", id); + so.free_func(request->buffer); + return -1; + } + if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { + if (s->protocol == PROTOCOL_TCP) { + int n = write(s->fd, so.buffer, so.sz); + if (n<0) { + switch(errno) { + case EINTR: + case AGAIN_WOULDBLOCK: + n = 0; + break; + default: + fprintf(stderr, "socket-server: write to %d (fd=%d) error :%s.\n",id,s->fd,strerror(errno)); + force_close(ss,s,result); + so.free_func(request->buffer); + return SOCKET_CLOSE; + } + } + if (n == so.sz) { + so.free_func(request->buffer); + return -1; + } + append_sendbuffer(ss, s, request, n); // add to high priority list, even priority == PRIORITY_LOW + } else { + // udp + if (udp_address == NULL) { + udp_address = s->p.udp_address; + } + union sockaddr_all sa; + socklen_t sasz = udp_socket_address(s, udp_address, &sa); + int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); + if (n != so.sz) { + append_sendbuffer_udp(ss,s,priority,request,udp_address); + } else { + so.free_func(request->buffer); + return -1; } } - if (n == request->sz) { - FREE(request->buffer); - return -1; - } - append_sendbuffer(s, request, n); // add to high priority list, even priority == PRIORITY_LOW sp_write(ss->event_fd, s->fd, s, true); } else { - if (priority == PRIORITY_LOW) { - append_sendbuffer_low(s, request); + if (s->protocol == PROTOCOL_TCP) { + if (priority == PRIORITY_LOW) { + append_sendbuffer_low(ss, s, request); + } else { + append_sendbuffer(ss, s, request, 0); + } } else { - append_sendbuffer(s, request, 0); + if (udp_address == NULL) { + udp_address = s->p.udp_address; + } + append_sendbuffer_udp(ss,s,priority,request,udp_address); } } + if (s->wb_size >= WARNING_SIZE && s->wb_size >= s->warn_size) { + s->warn_size = s->warn_size == 0 ? WARNING_SIZE *2 : s->warn_size*2; + result->opaque = s->opaque; + result->id = s->id; + result->ud = s->wb_size%1024 == 0 ? s->wb_size/1024 : s->wb_size/1024 + 1; + result->data = NULL; + return SOCKET_WARNING; + } return -1; } @@ -546,7 +786,7 @@ static int listen_socket(struct socket_server *ss, struct request_listen * request, struct socket_message *result) { int id = request->id; int listen_fd = request->fd; - struct socket *s = new_fd(ss, id, listen_fd, request->opaque, false); + struct socket *s = new_fd(ss, id, listen_fd, PROTOCOL_TCP, request->opaque, false); if (s == NULL) { goto _failed; } @@ -557,16 +797,16 @@ _failed: result->opaque = request->opaque; result->id = id; result->ud = 0; - result->data = NULL; - ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID; + result->data = "reach skynet socket number limit"; + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; - return SOCKET_ERROR; + return SOCKET_ERR; } static int close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) { int id = request->id; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id != id) { result->id = id; result->opaque = request->opaque; @@ -576,10 +816,11 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc } if (!send_buffer_empty(s)) { int type = send_buffer(ss,s,result); - if (type != -1) + // type : -1 or SOCKET_WARNING or SOCKET_CLOSE, SOCKET_WARNING means send_buffer_empty + if (type != -1 && type != SOCKET_WARNING) return type; } - if (send_buffer_empty(s)) { + if (request->shutdown || send_buffer_empty(s)) { force_close(ss,s,result); result->id = id; result->opaque = request->opaque; @@ -596,10 +837,10 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke result->id = id; result->opaque = request->opaque; result->ud = 0; - struct socket *s = new_fd(ss, id, request->fd, request->opaque, true); + struct socket *s = new_fd(ss, id, request->fd, PROTOCOL_TCP, request->opaque, true); if (s == NULL) { - result->data = NULL; - return SOCKET_ERROR; + result->data = "reach skynet socket number limit"; + return SOCKET_ERR; } sp_nonblocking(request->fd); s->type = SOCKET_TYPE_BIND; @@ -614,27 +855,42 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc result->opaque = request->opaque; result->ud = 0; result->data = NULL; - struct socket *s = &ss->slot[id % MAX_SOCKET]; + struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { - return SOCKET_ERROR; + result->data = "invalid socket"; + return SOCKET_ERR; } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { if (sp_add(ss->event_fd, s->fd, s)) { - s->type = SOCKET_TYPE_INVALID; - return SOCKET_ERROR; + force_close(ss, s, result); + result->data = strerror(errno); + return SOCKET_ERR; } s->type = (s->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) { + // todo: maybe we should send a message SOCKET_TRANSFER to s->opaque s->opaque = request->opaque; result->data = "transfer"; return SOCKET_OPEN; } + // if s->type == SOCKET_TYPE_HALFCLOSE , SOCKET_CLOSE message will send later return -1; } +static void +setopt_socket(struct socket_server *ss, struct request_setopt *request) { + int id = request->id; + struct socket *s = &ss->slot[HASH_ID(id)]; + if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + return; + } + int v = request->value; + setsockopt(s->fd, IPPROTO_TCP, request->what, &v, sizeof(v)); +} + static void block_readpipe(int pipefd, void *buffer, int sz) { for (;;) { @@ -642,7 +898,7 @@ block_readpipe(int pipefd, void *buffer, int sz) { if (n<0) { if (errno == EINTR) continue; - fprintf(stderr, "socket-server : read pipe error %s.",strerror(errno)); + fprintf(stderr, "socket-server : read pipe error %s.\n",strerror(errno)); return; } // must atomic read from a pipe @@ -665,6 +921,50 @@ has_cmd(struct socket_server *ss) { return 0; } +static void +add_udp_socket(struct socket_server *ss, struct request_udp *udp) { + int id = udp->id; + int protocol; + if (udp->family == AF_INET6) { + protocol = PROTOCOL_UDPv6; + } else { + protocol = PROTOCOL_UDP; + } + struct socket *ns = new_fd(ss, id, udp->fd, protocol, udp->opaque, true); + if (ns == NULL) { + close(udp->fd); + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + return; + } + ns->type = SOCKET_TYPE_CONNECTED; + memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address)); +} + +static int +set_udp_address(struct socket_server *ss, struct request_setudp *request, struct socket_message *result) { + int id = request->id; + struct socket *s = &ss->slot[HASH_ID(id)]; + if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + return -1; + } + int type = request->address[0]; + if (type != s->protocol) { + // protocol mismatch + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = "protocol mismatch"; + + return SOCKET_ERR; + } + if (type == PROTOCOL_UDP) { + memcpy(s->p.udp_address, request->address, 1+2+4); // 1 type, 2 port, 4 ipv4 + } else { + memcpy(s->p.udp_address, request->address, 1+2+16); // 1 type, 2 port, 16 ipv6 + } + return -1; +} + // return type static int ctrl_cmd(struct socket_server *ss, struct socket_message *result) { @@ -687,7 +987,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { case 'K': return close_socket(ss,(struct request_close *)buffer, result); case 'O': - return open_socket(ss, (struct request_open *)buffer, result, false); + return open_socket(ss, (struct request_open *)buffer, result); case 'X': result->opaque = 0; result->id = 0; @@ -695,9 +995,21 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { result->data = NULL; return SOCKET_EXIT; case 'D': - return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH); + return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH, NULL); case 'P': - return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW); + return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW, NULL); + case 'A': { + struct request_send_udp * rsu = (struct request_send_udp *)buffer; + return send_socket(ss, &rsu->send, result, PRIORITY_HIGH, rsu->address); + } + case 'C': + return set_udp_address(ss, (struct request_setudp *)buffer, result); + case 'T': + setopt_socket(ss, (struct request_setopt *)buffer); + return -1; + case 'U': + add_udp_socket(ss, (struct request_udp *)buffer); + return -1; default: fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type); return -1; @@ -708,8 +1020,8 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { // return -1 (ignore) when error static int -forward_message(struct socket_server *ss, struct socket *s, struct socket_message * result) { - int sz = s->size; +forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_message * result) { + int sz = s->p.size; char * buffer = MALLOC(sz); int n = (int)read(s->fd, buffer, sz); if (n<0) { @@ -717,13 +1029,14 @@ forward_message(struct socket_server *ss, struct socket *s, struct socket_messag switch(errno) { case EINTR: break; - case EAGAIN: + case AGAIN_WOULDBLOCK: fprintf(stderr, "socket-server: EAGAIN capture.\n"); break; default: // close when error force_close(ss, s, result); - return SOCKET_ERROR; + result->data = strerror(errno); + return SOCKET_ERR; } return -1; } @@ -740,9 +1053,9 @@ forward_message(struct socket_server *ss, struct socket *s, struct socket_messag } if (n == sz) { - s->size *= 2; + s->p.size *= 2; } else if (sz > MIN_READ_BUFFER && n*2 < sz) { - s->size /= 2; + s->p.size /= 2; } result->opaque = s->opaque; @@ -752,6 +1065,64 @@ forward_message(struct socket_server *ss, struct socket *s, struct socket_messag return SOCKET_DATA; } +static int +gen_udp_address(int protocol, union sockaddr_all *sa, uint8_t * udp_address) { + int addrsz = 1; + udp_address[0] = (uint8_t)protocol; + if (protocol == PROTOCOL_UDP) { + memcpy(udp_address+addrsz, &sa->v4.sin_port, sizeof(sa->v4.sin_port)); + addrsz += sizeof(sa->v4.sin_port); + memcpy(udp_address+addrsz, &sa->v4.sin_addr, sizeof(sa->v4.sin_addr)); + addrsz += sizeof(sa->v4.sin_addr); + } else { + memcpy(udp_address+addrsz, &sa->v6.sin6_port, sizeof(sa->v6.sin6_port)); + addrsz += sizeof(sa->v6.sin6_port); + memcpy(udp_address+addrsz, &sa->v6.sin6_addr, sizeof(sa->v6.sin6_addr)); + addrsz += sizeof(sa->v6.sin6_addr); + } + return addrsz; +} + +static int +forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_message * result) { + union sockaddr_all sa; + socklen_t slen = sizeof(sa); + int n = recvfrom(s->fd, ss->udpbuffer,MAX_UDP_PACKAGE,0,&sa.s,&slen); + if (n<0) { + switch(errno) { + case EINTR: + case AGAIN_WOULDBLOCK: + break; + default: + // close when error + force_close(ss, s, result); + result->data = strerror(errno); + return SOCKET_ERR; + } + return -1; + } + uint8_t * data; + if (slen == sizeof(sa.v4)) { + if (s->protocol != PROTOCOL_UDP) + return -1; + data = MALLOC(n + 1 + 2 + 4); + gen_udp_address(PROTOCOL_UDP, &sa, data + n); + } else { + if (s->protocol != PROTOCOL_UDPv6) + return -1; + data = MALLOC(n + 1 + 2 + 16); + gen_udp_address(PROTOCOL_UDPv6, &sa, data + n); + } + memcpy(data, ss->udpbuffer, n); + + result->opaque = s->opaque; + result->id = s->id; + result->ud = n; + result->data = (char *)data; + + return SOCKET_UDP; +} + static int report_connect(struct socket_server *ss, struct socket *s, struct socket_message *result) { int error; @@ -759,13 +1130,19 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); if (code < 0 || error) { force_close(ss,s, result); - return SOCKET_ERROR; + if (code >= 0) + result->data = strerror(error); + else + result->data = strerror(errno); + return SOCKET_ERR; } else { s->type = SOCKET_TYPE_CONNECTED; result->opaque = s->opaque; result->id = s->id; result->ud = 0; - sp_write(ss->event_fd, s->fd, s, false); + if (send_buffer_empty(s)) { + sp_write(ss->event_fd, s->fd, s, false); + } union sockaddr_all u; socklen_t slen = sizeof(u); if (getpeername(s->fd, &u.s, &slen) == 0) { @@ -780,14 +1157,22 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message } } -// return 0 when failed +// return 0 when failed, or -1 when file limit static int report_accept(struct socket_server *ss, struct socket *s, struct socket_message *result) { union sockaddr_all u; socklen_t len = sizeof(u); int client_fd = accept(s->fd, &u.s, &len); if (client_fd < 0) { - return 0; + if (errno == EMFILE || errno == ENFILE) { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = strerror(errno); + return -1; + } else { + return 0; + } } int id = reserve_id(ss); if (id < 0) { @@ -796,7 +1181,7 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message } socket_keepalive(client_fd); sp_nonblocking(client_fd); - struct socket *ns = new_fd(ss, id, client_fd, s->opaque, false); + struct socket *ns = new_fd(ss, id, client_fd, PROTOCOL_TCP, s->opaque, false); if (ns == NULL) { close(client_fd); return 0; @@ -808,7 +1193,10 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message result->data = NULL; void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr; - if (inet_ntop(u.s.sa_family, sin_addr, ss->buffer, sizeof(ss->buffer))) { + int sin_port = ntohs((u.s.sa_family == AF_INET) ? u.v4.sin_port : u.v6.sin6_port); + char tmp[INET6_ADDRSTRLEN]; + if (inet_ntop(u.s.sa_family, sin_addr, tmp, sizeof(tmp))) { + snprintf(ss->buffer, sizeof(ss->buffer), "%s:%d", tmp, sin_port); result->data = ss->buffer; } @@ -817,7 +1205,7 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message static inline void clear_closed_event(struct socket_server *ss, struct socket_message * result, int type) { - if (type == SOCKET_CLOSE || type == SOCKET_ERROR) { + if (type == SOCKET_CLOSE || type == SOCKET_ERR) { int id = result->id; int i; for (i=ss->event_index; ievent_n; i++) { @@ -826,6 +1214,7 @@ clear_closed_event(struct socket_server *ss, struct socket_message * result, int if (s) { if (s->type == SOCKET_TYPE_INVALID && s->id == id) { e->s = NULL; + break; } } } @@ -857,6 +1246,9 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int ss->event_index = 0; if (ss->event_n <= 0) { ss->event_n = 0; + if (errno == EINTR) { + continue; + } return -1; } } @@ -869,28 +1261,61 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int switch (s->type) { case SOCKET_TYPE_CONNECTING: return report_connect(ss, s, result); - case SOCKET_TYPE_LISTEN: - if (report_accept(ss, s, result)) { + case SOCKET_TYPE_LISTEN: { + int ok = report_accept(ss, s, result); + if (ok > 0) { return SOCKET_ACCEPT; - } + } if (ok < 0 ) { + return SOCKET_ERR; + } + // when ok == 0, retry break; + } case SOCKET_TYPE_INVALID: fprintf(stderr, "socket-server: invalid socket\n"); break; default: + if (e->read) { + int type; + if (s->protocol == PROTOCOL_TCP) { + type = forward_message_tcp(ss, s, result); + } else { + type = forward_message_udp(ss, s, result); + if (type == SOCKET_UDP) { + // try read again + --ss->event_index; + return SOCKET_UDP; + } + } + if (e->write && type != SOCKET_CLOSE && type != SOCKET_ERR) { + // Try to dispatch write message next step if write flag set. + e->read = false; + --ss->event_index; + } + if (type == -1) + break; + return type; + } if (e->write) { int type = send_buffer(ss, s, result); if (type == -1) break; - clear_closed_event(ss, result, type); return type; } - if (e->read) { - int type = forward_message(ss, s, result); - if (type == -1) - break; - clear_closed_event(ss, result, type); - return type; + if (e->error) { + // close when error + int error; + socklen_t len = sizeof(error); + int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); + if (code < 0) { + result->data = strerror(errno); + } else if (error != 0) { + result->data = strerror(error); + } else { + result->data = "Unknown error"; + } + force_close(ss, s, result); + return SOCKET_ERR; } break; } @@ -917,11 +1342,13 @@ send_request(struct socket_server *ss, struct request_package *request, char typ static int open_request(struct socket_server *ss, struct request_package *req, uintptr_t opaque, const char *addr, int port) { int len = strlen(addr); - if (len + sizeof(req->u.open) > 256) { + if (len + sizeof(req->u.open) >= 256) { fprintf(stderr, "socket-server : Invalid addr %s.\n",addr); - return 0; + return -1; } int id = reserve_id(ss); + if (id < 0) + return -1; req->u.open.opaque = opaque; req->u.open.id = id; req->u.open.port = port; @@ -935,31 +1362,27 @@ int socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { struct request_package request; int len = open_request(ss, &request, opaque, addr, port); + if (len < 0) + return -1; send_request(ss, &request, 'O', sizeof(request.u.open) + len); return request.u.open.id; } -int -socket_server_block_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { - struct request_package request; - struct socket_message result; - open_request(ss, &request, opaque, addr, port); - int ret = open_socket(ss, &request.u.open, &result, true); - if (ret == SOCKET_OPEN) { - return result.id; - } else { - return -1; - } +static void +free_buffer(struct socket_server *ss, const void * buffer, int sz) { + struct send_object so; + send_object_init(ss, &so, (void *)buffer, sz); + so.free_func((void *)buffer); } -// return -1 when error -int64_t +// return -1 when error, 0 when success +int socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + free_buffer(ss, buffer, sz); return -1; } - assert(s->type != SOCKET_TYPE_RESERVE); struct request_package request; request.u.send.id = id; @@ -967,16 +1390,17 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz request.u.send.buffer = (char *)buffer; send_request(ss, &request, 'D', sizeof(request.u.send)); - return s->wb_size; + return 0; } -void +// return -1 when error, 0 when success +int socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { - return; + free_buffer(ss, buffer, sz); + return -1; } - assert(s->type != SOCKET_TYPE_RESERVE); struct request_package request; request.u.send.id = id; @@ -984,6 +1408,7 @@ socket_server_send_lowpriority(struct socket_server *ss, int id, const void * bu request.u.send.buffer = (char *)buffer; send_request(ss, &request, 'P', sizeof(request.u.send)); + return 0; } void @@ -996,42 +1421,82 @@ void socket_server_close(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request.u.close.id = id; + request.u.close.shutdown = 0; request.u.close.opaque = opaque; send_request(ss, &request, 'K', sizeof(request.u.close)); } + +void +socket_server_shutdown(struct socket_server *ss, uintptr_t opaque, int id) { + struct request_package request; + request.u.close.id = id; + request.u.close.shutdown = 1; + request.u.close.opaque = opaque; + send_request(ss, &request, 'K', sizeof(request.u.close)); +} + +// return -1 means failed +// or return AF_INET or AF_INET6 +static int +do_bind(const char *host, int port, int protocol, int *family) { + int fd; + int status; + int reuse = 1; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + char portstr[16]; + if (host == NULL || host[0] == 0) { + host = "0.0.0.0"; // INADDR_ANY + } + sprintf(portstr, "%d", port); + memset( &ai_hints, 0, sizeof( ai_hints ) ); + ai_hints.ai_family = AF_UNSPEC; + if (protocol == IPPROTO_TCP) { + ai_hints.ai_socktype = SOCK_STREAM; + } else { + assert(protocol == IPPROTO_UDP); + ai_hints.ai_socktype = SOCK_DGRAM; + } + ai_hints.ai_protocol = protocol; + + status = getaddrinfo( host, portstr, &ai_hints, &ai_list ); + if ( status != 0 ) { + return -1; + } + *family = ai_list->ai_family; + fd = socket(*family, ai_list->ai_socktype, 0); + if (fd < 0) { + goto _failed_fd; + } + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { + goto _failed; + } + status = bind(fd, (struct sockaddr *)ai_list->ai_addr, ai_list->ai_addrlen); + if (status != 0) + goto _failed; + + freeaddrinfo( ai_list ); + return fd; +_failed: + close(fd); +_failed_fd: + freeaddrinfo( ai_list ); + return -1; +} + static int do_listen(const char * host, int port, int backlog) { - // only support ipv4 - // todo: support ipv6 by getaddrinfo - uint32_t addr = INADDR_ANY; - if (host[0]) { - addr=inet_addr(host); - } - int listen_fd = socket(AF_INET, SOCK_STREAM, 0); + int family = 0; + int listen_fd = do_bind(host, port, IPPROTO_TCP, &family); if (listen_fd < 0) { return -1; } - int reuse = 1; - if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { - goto _failed; - } - - struct sockaddr_in my_addr; - memset(&my_addr, 0, sizeof(struct sockaddr_in)); - my_addr.sin_family = AF_INET; - my_addr.sin_port = htons(port); - my_addr.sin_addr.s_addr = addr; - if (bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { - goto _failed; - } if (listen(listen_fd, backlog) == -1) { - goto _failed; + close(listen_fd); + return -1; } return listen_fd; -_failed: - close(listen_fd); - return -1; } int @@ -1042,6 +1507,10 @@ socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * ad } struct request_package request; int id = reserve_id(ss); + if (id < 0) { + close(fd); + return id; + } request.u.listen.opaque = opaque; request.u.listen.id = id; request.u.listen.fd = fd; @@ -1053,6 +1522,8 @@ int socket_server_bind(struct socket_server *ss, uintptr_t opaque, int fd) { struct request_package request; int id = reserve_id(ss); + if (id < 0) + return -1; request.u.bind.opaque = opaque; request.u.bind.id = id; request.u.bind.fd = fd; @@ -1068,4 +1539,140 @@ socket_server_start(struct socket_server *ss, uintptr_t opaque, int id) { send_request(ss, &request, 'S', sizeof(request.u.start)); } +void +socket_server_nodelay(struct socket_server *ss, int id) { + struct request_package request; + request.u.setopt.id = id; + request.u.setopt.what = TCP_NODELAY; + request.u.setopt.value = 1; + send_request(ss, &request, 'T', sizeof(request.u.setopt)); +} +void +socket_server_userobject(struct socket_server *ss, struct socket_object_interface *soi) { + ss->soi = *soi; +} + +// UDP + +int +socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { + int fd; + int family; + if (port != 0 || addr != NULL) { + // bind + fd = do_bind(addr, port, IPPROTO_UDP, &family); + if (fd < 0) { + return -1; + } + } else { + family = AF_INET; + fd = socket(family, SOCK_DGRAM, 0); + if (fd < 0) { + return -1; + } + } + 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_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) { + struct socket * s = &ss->slot[HASH_ID(id)]; + if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + free_buffer(ss, buffer, sz); + return -1; + } + + struct request_package request; + request.u.send_udp.send.id = id; + request.u.send_udp.send.sz = sz; + request.u.send_udp.send.buffer = (char *)buffer; + + const uint8_t *udp_address = (const uint8_t *)addr; + int addrsz; + switch (udp_address[0]) { + case PROTOCOL_UDP: + addrsz = 1+2+4; // 1 type, 2 port, 4 ipv4 + break; + case PROTOCOL_UDPv6: + addrsz = 1+2+16; // 1 type, 2 port, 16 ipv6 + break; + default: + free_buffer(ss, buffer, sz); + return -1; + } + + memcpy(request.u.send_udp.address, udp_address, addrsz); + + send_request(ss, &request, 'A', sizeof(request.u.send_udp.send)+addrsz); + return 0; +} + +int +socket_server_udp_connect(struct socket_server *ss, int id, const char * addr, int port) { + int status; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + char portstr[16]; + sprintf(portstr, "%d", port); + memset( &ai_hints, 0, sizeof( ai_hints ) ); + ai_hints.ai_family = AF_UNSPEC; + ai_hints.ai_socktype = SOCK_DGRAM; + ai_hints.ai_protocol = IPPROTO_UDP; + + status = getaddrinfo(addr, portstr, &ai_hints, &ai_list ); + if ( status != 0 ) { + return -1; + } + struct request_package request; + request.u.set_udp.id = id; + int protocol; + + if (ai_list->ai_family == AF_INET) { + protocol = PROTOCOL_UDP; + } else if (ai_list->ai_family == AF_INET6) { + protocol = PROTOCOL_UDPv6; + } else { + freeaddrinfo( ai_list ); + return -1; + } + + int addrsz = gen_udp_address(protocol, (union sockaddr_all *)ai_list->ai_addr, request.u.set_udp.address); + + freeaddrinfo( ai_list ); + + send_request(ss, &request, 'C', sizeof(request.u.set_udp) - sizeof(request.u.set_udp.address) +addrsz); + + return 0; +} + +const struct socket_udp_address * +socket_server_udp_address(struct socket_server *ss, struct socket_message *msg, int *addrsz) { + uint8_t * address = (uint8_t *)(msg->data + msg->ud); + int type = address[0]; + switch(type) { + case PROTOCOL_UDP: + *addrsz = 1+2+4; + break; + case PROTOCOL_UDPv6: + *addrsz = 1+2+16; + break; + default: + return NULL; + } + return (const struct socket_udp_address *)address; +} diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 2fbb6939..622b086f 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -7,15 +7,17 @@ #define SOCKET_CLOSE 1 #define SOCKET_OPEN 2 #define SOCKET_ACCEPT 3 -#define SOCKET_ERROR 4 +#define SOCKET_ERR 4 #define SOCKET_EXIT 5 +#define SOCKET_UDP 6 +#define SOCKET_WARNING 7 struct socket_server; struct socket_message { int id; uintptr_t opaque; - int ud; // for accept, ud is listen id ; for data, ud is size of data + int ud; // for accept, ud is new connection id ; for data, ud is size of data char * data; }; @@ -25,17 +27,41 @@ int socket_server_poll(struct socket_server *, struct socket_message *result, in void socket_server_exit(struct socket_server *); void socket_server_close(struct socket_server *, uintptr_t opaque, int id); +void socket_server_shutdown(struct socket_server *, uintptr_t opaque, int id); void socket_server_start(struct socket_server *, uintptr_t opaque, int id); // return -1 when error -int64_t socket_server_send(struct socket_server *, int id, const void * buffer, int sz); -void socket_server_send_lowpriority(struct socket_server *, int id, const void * buffer, int sz); +int socket_server_send(struct socket_server *, int id, const void * buffer, int sz); +int socket_server_send_lowpriority(struct socket_server *, int id, const void * buffer, int sz); // ctrl command below returns id int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); -int socket_server_block_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); +// for tcp +void socket_server_nodelay(struct socket_server *, int id); + +struct socket_udp_address; + +// create an udp socket handle, attach opaque with it . udp socket don't need call socket_server_start to recv message +// if port != 0, bind the socket . if addr == NULL, bind ipv4 0.0.0.0 . If you want to use ipv6, addr can be "::" and port 0. +int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int port); +// set default dest address, return 0 when success +int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port); +// If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead +// You can also use socket_server_send +int socket_server_udp_send(struct socket_server *, int id, const struct socket_udp_address *, const void *buffer, int sz); +// extract the address of the message, struct socket_message * should be SOCKET_UDP +const struct socket_udp_address * socket_server_udp_address(struct socket_server *, struct socket_message *, int *addrsz); + +struct socket_object_interface { + void * (*buffer)(void *); + int (*size)(void *); + void (*free)(void *); +}; + +// if you send package sz == -1, use soi. +void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi); #endif diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h new file mode 100644 index 00000000..514546f4 --- /dev/null +++ b/skynet-src/spinlock.h @@ -0,0 +1,78 @@ +#ifndef SKYNET_SPINLOCK_H +#define SKYNET_SPINLOCK_H + +#define SPIN_INIT(q) spinlock_init(&(q)->lock); +#define SPIN_LOCK(q) spinlock_lock(&(q)->lock); +#define SPIN_UNLOCK(q) spinlock_unlock(&(q)->lock); +#define SPIN_DESTROY(q) spinlock_destroy(&(q)->lock); + +#ifndef USE_PTHREAD_LOCK + +struct spinlock { + int lock; +}; + +static inline void +spinlock_init(struct spinlock *lock) { + lock->lock = 0; +} + +static inline void +spinlock_lock(struct spinlock *lock) { + while (__sync_lock_test_and_set(&lock->lock,1)) {} +} + +static inline int +spinlock_trylock(struct spinlock *lock) { + return __sync_lock_test_and_set(&lock->lock,1) == 0; +} + +static inline void +spinlock_unlock(struct spinlock *lock) { + __sync_lock_release(&lock->lock); +} + +static inline void +spinlock_destroy(struct spinlock *lock) { + (void) lock; +} + +#else + +#include + +// we use mutex instead of spinlock for some reason +// you can also replace to pthread_spinlock + +struct spinlock { + pthread_mutex_t lock; +}; + +static inline void +spinlock_init(struct spinlock *lock) { + pthread_mutex_init(&lock->lock, NULL); +} + +static inline void +spinlock_lock(struct spinlock *lock) { + pthread_mutex_lock(&lock->lock); +} + +static inline int +spinlock_trylock(struct spinlock *lock) { + return pthread_mutex_trylock(&lock->lock) == 0; +} + +static inline void +spinlock_unlock(struct spinlock *lock) { + pthread_mutex_unlock(&lock->lock); +} + +static inline void +spinlock_destroy(struct spinlock *lock) { + pthread_mutex_destroy(&lock->lock); +} + +#endif + +#endif diff --git a/test/pingserver.lua b/test/pingserver.lua index 635cfeab..e5213716 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,4 +1,6 @@ local skynet = require "skynet" +local queue = require "skynet.queue" +local snax = require "skynet.snax" local i = 0 local hello = "hello" @@ -8,9 +10,31 @@ function response.ping(hello) return hello end +-- response.sleep and accept.hello share one lock +local lock + +function accept.sleep(queue, n) + if queue then + lock( + function() + print("queue=",queue, n) + skynet.sleep(n) + end) + else + print("queue=",queue, n) + skynet.sleep(n) + end +end + function accept.hello() + lock(function() i = i + 1 print (i, hello) + end) +end + +function accept.exit(...) + snax.exit(...) end function response.error() @@ -19,9 +43,9 @@ end function init( ... ) print ("ping server start:", ...) - --- You can return "queue" for queue service mode --- return "queue" + snax.enablecluster() -- enable cluster call + -- init queue + lock = queue() end function exit(...) diff --git a/test/sharemap.sp b/test/sharemap.sp new file mode 100644 index 00000000..a0a77f51 --- /dev/null +++ b/test/sharemap.sp @@ -0,0 +1,5 @@ +.foobar { + x 0 : integer + y 1 : integer + s 2 : string +} diff --git a/test/testblockcall.lua b/test/testblockcall.lua deleted file mode 100644 index b0d7e76f..00000000 --- a/test/testblockcall.lua +++ /dev/null @@ -1,10 +0,0 @@ -local skynet = require "skynet" - -skynet.start(function() - local ping = skynet.newservice("pingserver") - skynet.timeout(0,function() - print(skynet.call(ping,"lua","PING","ping")) - end) - - print(skynet.blockcall(ping,"lua","HELLO")) -end) diff --git a/test/testbson.lua b/test/testbson.lua new file mode 100644 index 00000000..815ffdef --- /dev/null +++ b/test/testbson.lua @@ -0,0 +1,102 @@ +local bson = require "bson" + +local sub = bson.encode_order( "hello", 1, "world", 2 ) + +do + -- check decode encode_order + local d = bson.decode(sub) + assert(d.hello == 1 ) + assert(d.world == 2 ) +end + +local function tbl_next(...) + print("--- next.a", ...) + local k, v = next(...) + print("--- next.b", k, v) + return k, v +end + +local function tbl_pairs(obj) + return tbl_next, obj.__data, nil +end + +local obj_a = { + __data = { + [1] = 2, + [3] = 4, + [5] = 6, + } +} + +setmetatable( + obj_a, + { + __index = obj_a.__data, + __pairs = tbl_pairs, + } +) + +local obj_b = { + __data = { + [7] = 8, + [9] = 10, + [11] = obj_a, + } +} + +setmetatable( + obj_b, + { + __index = obj_b.__data, + __pairs = tbl_pairs, + } +) + +local metaarray = setmetatable({ n = 5 }, { + __len = function(self) return self.n end, + __index = function(self, idx) return tostring(idx) end, +}) + +b = bson.encode { + a = 1, + b = true, + c = bson.null, + d = { 1,2,3,4 }, + e = bson.binary "hello", + f = bson.regex ("*","i"), + g = bson.regex "hello", + h = bson.date (os.time()), + i = bson.timestamp(os.time()), + j = bson.objectid(), + k = { a = false, b = true }, + l = {}, + m = bson.minkey, + n = bson.maxkey, + o = sub, + p = 2^32-1, + q = obj_b, + r = metaarray, +} + +print "\n[before replace]" +t = b:decode() + +for k, v in pairs(t) do + print(k,type(v)) +end + +for k,v in ipairs(t.r) do + print(k,v) +end + +b:makeindex() +b.a = 2 +b.b = false +b.h = bson.date(os.time()) +b.i = bson.timestamp(os.time()) +b.j = bson.objectid() + +print "\n[after replace]" +t = b:decode() + +print("o.hello", bson.type(t.o.hello)) diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua new file mode 100644 index 00000000..910bcc0f --- /dev/null +++ b/test/testcoroutine.lua @@ -0,0 +1,53 @@ +local skynet = require "skynet" +-- You should use skynet.coroutine instead of origin coroutine in skynet +local coroutine = require "skynet.coroutine" +local profile = require "skynet.profile" + +local function status(co) + repeat + local status = coroutine.status(co) + print("STATUS", status) + skynet.sleep(100) + until status == "suspended" + + repeat + local ok, n = assert(coroutine.resume(co)) + print("status thread", n) + until not n + skynet.exit() +end + +local function test(n) + local co = coroutine.running() + print ("begin", co, coroutine.thread(co)) -- false + skynet.fork(status, co) + for i=1,n do + skynet.sleep(100) + coroutine.yield(i) + end + print ("end", co) +end + +local function main() + local f = coroutine.wrap(test) + coroutine.yield "begin" + for i=1,3 do + local n = f(5) + print("main thread",n) + end + coroutine.yield "end" + print("main thread time:", profile.stop(coroutine.thread())) +end + +skynet.start(function() + print("Main thead :", coroutine.thread()) -- true + print(coroutine.resume(coroutine.running())) -- always return false + + profile.start() + + local f = coroutine.wrap(main) + print("main step", f()) + print("main step", f()) + print("main step", f()) +-- print("main thread time:", profile.stop()) +end) diff --git a/test/testdatacenter.lua b/test/testdatacenter.lua new file mode 100644 index 00000000..44eb3875 --- /dev/null +++ b/test/testdatacenter.lua @@ -0,0 +1,23 @@ +local skynet = require "skynet" +local datacenter = require "skynet.datacenter" + +local function f1() + print("====1==== wait hello") + print("\t1>",datacenter.wait ("hello")) + print("====1==== wait key.foobar") + print("\t1>", pcall(datacenter.wait,"key")) -- will failed, because "key" is a branch + print("\t1>",datacenter.wait ("key", "foobar")) +end + +local function f2() + skynet.sleep(10) + print("====2==== set key.foobar") + datacenter.set("key", "foobar", "bingo") +end + +skynet.start(function() + datacenter.set("hello", "world") + print(datacenter.get "hello") + skynet.fork(f1) + skynet.fork(f2) +end) diff --git a/test/testdatasheet.lua b/test/testdatasheet.lua new file mode 100644 index 00000000..5b63a268 --- /dev/null +++ b/test/testdatasheet.lua @@ -0,0 +1,28 @@ +local skynet = require "skynet" +local builder = require "skynet.datasheet.builder" +local datasheet = require "skynet.datasheet" + +local function dump(t, prefix) + for k,v in pairs(t) do + print(prefix, k, v) + if type(v) == "table" then + dump(v, prefix .. "." .. k) + end + end +end + +skynet.start(function() + builder.new("foobar", {a = 1, b = 2 , c = {3} }) + local t = datasheet.query "foobar" + local c = t.c + dump(t, "[1]") + builder.update("foobar", { b = 4, c = { 5 } }) + print("sleep") + skynet.sleep(100) + dump(t, "[2]") + dump(c, "[2.c]") + builder.update("foobar", { a = 6, c = 7, d = 8 }) + print("sleep") + skynet.sleep(100) + dump(t, "[3]") +end) diff --git a/test/testdeadcall.lua b/test/testdeadcall.lua index d348a411..b34dccb8 100644 --- a/test/testdeadcall.lua +++ b/test/testdeadcall.lua @@ -11,17 +11,27 @@ skynet.start(function() end) end) +elseif mode == "dead" then + +skynet.start(function() + skynet.dispatch("lua", function (...) + skynet.sleep(100) + print("return", skynet.ret "") + end) +end) + else skynet.start(function() - local test = skynet.newservice("testdeadcall", "test") -- launch self in test mode + local test = skynet.newservice(SERVICE_NAME, "test") -- launch self in test mode print(pcall(function() - skynet.send(test,"lua", "hello world") - skynet.send(test,"lua", "never get there") - skynet.call(test,"lua", "fake call") + skynet.call(test,"lua", "dead call") end)) - skynet.exit() + local dead = skynet.newservice(SERVICE_NAME, "dead") -- launch self in dead mode + + skynet.timeout(0, skynet.exit) -- exit after a while, so the call never return + skynet.call(dead, "lua", "whould not return") end) -end \ No newline at end of file +end diff --git a/test/testdeadloop.lua b/test/testdeadloop.lua new file mode 100644 index 00000000..215700e2 --- /dev/null +++ b/test/testdeadloop.lua @@ -0,0 +1,10 @@ +local skynet = require "skynet" +local function dead_loop() + while true do + skynet.sleep(0) + end +end + +skynet.start(function() + skynet.fork(dead_loop) +end) diff --git a/test/testdns.lua b/test/testdns.lua new file mode 100644 index 00000000..01ac414a --- /dev/null +++ b/test/testdns.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" +local dns = require "skynet.dns" + +skynet.start(function() + print("nameserver:", dns.server()) -- set nameserver + -- you can specify the server like dns.server("8.8.4.4", 53) + local ip, ips = dns.resolve "github.com" + for k,v in ipairs(ips) do + print("github.com",v) + end +end) diff --git a/test/testecho.lua b/test/testecho.lua new file mode 100644 index 00000000..a6b265ef --- /dev/null +++ b/test/testecho.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "slave" then + +skynet.start(function() + skynet.dispatch("lua", function(_,_, ...) + skynet.ret(skynet.pack(...)) + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + local n = 100000 + local start = skynet.now() + print("call salve", n, "times in queue") + for i=1,n do + skynet.call(slave, "lua") + end + print("qps = ", n/ (skynet.now() - start) * 100) + + start = skynet.now() + + local worker = 10 + local task = n/worker + print("call salve", n, "times in parallel, worker = ", worker) + + for i=1,worker do + skynet.fork(function() + for i=1,task do + skynet.call(slave, "lua") + end + worker = worker -1 + if worker == 0 then + print("qps = ", n/ (skynet.now() - start) * 100) + end + end) + end +end) + +end diff --git a/test/testendless.lua b/test/testendless.lua new file mode 100644 index 00000000..928c1707 --- /dev/null +++ b/test/testendless.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" + +skynet.start(function() + for i = 1, 1000000000 do -- very long loop + if i%100000000 == 0 then + print("Endless = ", skynet.stat "endless") + print("Cost time = ", skynet.stat "time") + end + end + skynet.exit() +end) diff --git a/test/testharborlink.lua b/test/testharborlink.lua new file mode 100644 index 00000000..dfdb8613 --- /dev/null +++ b/test/testharborlink.lua @@ -0,0 +1,12 @@ +local skynet = require "skynet" +local harbor = require "skynet.harbor" + +skynet.start(function() + print("wait for harbor 2") + print("run skynet examples/config_log please") + harbor.connect(2) + print("harbor 2 connected") + print("LOG =", skynet.address(harbor.queryname "LOG")) + harbor.link(2) + print("disconnected") +end) diff --git a/test/testhttp.lua b/test/testhttp.lua new file mode 100644 index 00000000..f9524930 --- /dev/null +++ b/test/testhttp.lua @@ -0,0 +1,29 @@ +local skynet = require "skynet" +local httpc = require "http.httpc" +local dns = require "skynet.dns" + +local function main() + httpc.dns() -- set dns server + httpc.timeout = 100 -- set timeout 1 second + print("GET baidu.com") + local respheader = {} + local status, body = httpc.get("baidu.com", "/", respheader) + print("[header] =====>") + for k,v in pairs(respheader) do + print(k,v) + end + print("[body] =====>", status) + print(body) + + local respheader = {} + dns.server() + local ip = dns.resolve "baidu.com" + print(string.format("GET %s (baidu.com)", ip)) + local status, body = httpc.get("baidu.com", "/", respheader, { host = "baidu.com" }) + print(status) +end + +skynet.start(function() + print(pcall(main)) + skynet.exit() +end) diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua new file mode 100644 index 00000000..c1a7e63e --- /dev/null +++ b/test/testmemlimit.lua @@ -0,0 +1,35 @@ +local skynet = require "skynet" + +local names = { + "cluster", + "skynet.db.dns", + "skynet.db.mongo", + "skynet.db.mysql", + "skynet.db.redis", + "sharedata", + "skynet.socket", + "sproto" +} + +-- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) +skynet.memlimit(1 * 1024 * 1024) + +skynet.start(function() + local a = {} + local limit + local ok, err = pcall(function() + for i=1, 12355 do + limit = i + table.insert(a, {}) + end + end) + local libs = {} + for k,v in ipairs(names) do + local ok, m = pcall(require, v) + if ok then + libs[v] = m + end + end + skynet.error(limit, err) + skynet.exit() +end) diff --git a/test/testmongodb.lua b/test/testmongodb.lua new file mode 100644 index 00000000..138a970b --- /dev/null +++ b/test/testmongodb.lua @@ -0,0 +1,108 @@ +local skynet = require "skynet" +local mongo = require "skynet.db.mongo" +local bson = require "bson" + +local host, db_name = ... + +function test_insert_without_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) +end + +function test_insert_with_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 0) +end + +function test_find_and_remove() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {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 ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key2 = 1}) + assert(ret and ret.test_key2 == 1) + + local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1) + assert(ret:count() == 3) + assert(ret:count(true) == 1) + if ret:hasNext() then + ret = ret:next() + end + assert(ret and ret.test_key2 == 1) + + db[db_name].testdb:delete({test_key = 1}) + db[db_name].testdb:delete({test_key = 2}) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret == nil) +end + + +function test_expire_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) + db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) + + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + for i = 1, 1000 do + skynet.sleep(1); + + local ret = db[db_name].testdb:findOne({test_key = 1}) + if ret == nil then + return + end + end + + assert(false, "test expire index failed"); +end + +skynet.start(function() + print("Test insert without index") + test_insert_without_index() + print("Test insert index") + test_insert_with_index() + print("Test find and remove") + test_find_and_remove() + print("Test expire index") + test_expire_index() + print("mongodb test finish."); +end) diff --git a/test/testmulticast.lua b/test/testmulticast.lua index 88f79ccb..21303457 100644 --- a/test/testmulticast.lua +++ b/test/testmulticast.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local mc = require "multicast" -local dc = require "datacenter" +local mc = require "skynet.multicast" +local dc = require "skynet.datacenter" local mode = ... @@ -27,7 +27,7 @@ skynet.start(function() local channel = mc.new() print("New channel", channel) for i=1,10 do - local sub = skynet.newservice("testmulticast", "sub") + local sub = skynet.newservice(SERVICE_NAME, "sub") skynet.call(sub, "lua", "init", channel.channel) end diff --git a/test/testmulticast2.lua b/test/testmulticast2.lua index 0f55b77d..545b83f5 100644 --- a/test/testmulticast2.lua +++ b/test/testmulticast2.lua @@ -1,12 +1,16 @@ local skynet = require "skynet" -local dc = require "datacenter" -local mc = require "multicast" +local dc = require "skynet.datacenter" +local mc = require "skynet.multicast" skynet.start(function() print("remote start") - skynet.monitor("simplemonitor", true) local console = skynet.newservice("console") local channel = dc.get "MCCHANNEL" + if channel then + print("remote channel", channel) + else + print("create local channel") + end for i=1,10 do local sub = skynet.newservice("testmulticast", "sub") skynet.call(sub, "lua", "init", channel) diff --git a/test/testmysql.lua b/test/testmysql.lua new file mode 100644 index 00000000..0b11aeb7 --- /dev/null +++ b/test/testmysql.lua @@ -0,0 +1,131 @@ +local skynet = require "skynet" +local mysql = require "skynet.db.mysql" + +local function dump(obj) + local getIndent, quoteStr, wrapKey, wrapVal, dumpObj + getIndent = function(level) + return string.rep("\t", level) + end + quoteStr = function(str) + return '"' .. string.gsub(str, '"', '\\"') .. '"' + end + wrapKey = function(val) + if type(val) == "number" then + return "[" .. val .. "]" + elseif type(val) == "string" then + return "[" .. quoteStr(val) .. "]" + else + return "[" .. tostring(val) .. "]" + end + end + wrapVal = function(val, level) + if type(val) == "table" then + return dumpObj(val, level) + elseif type(val) == "number" then + return val + elseif type(val) == "string" then + return quoteStr(val) + else + return tostring(val) + end + end + dumpObj = function(obj, level) + if type(obj) ~= "table" then + return wrapVal(obj) + end + level = level + 1 + local tokens = {} + tokens[#tokens + 1] = "{" + for k, v in pairs(obj) do + tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," + end + tokens[#tokens + 1] = getIndent(level - 1) .. "}" + return table.concat(tokens, "\n") + end + return dumpObj(obj, 0) +end + +local function test2( db) + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) + res = db:query("select * from cats order by id asc") + print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) + + skynet.sleep(1000) + i=i+1 + end +end +local function test3( db) + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) + res = db:query("select * from cats order by id asc") + print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) + skynet.sleep(1000) + i=i+1 + end +end +skynet.start(function() + + local function on_connect(db) + db:query("set charset utf8"); + end + local db=mysql.connect({ + host="127.0.0.1", + port=3306, + database="skynet", + user="root", + password="1", + max_packet_size = 1024 * 1024, + on_connect = on_connect + }) + if not db then + print("failed to connect") + end + print("testmysql success to connect to mysql server") + + local res = db:query("drop table if exists cats") + res = db:query("create table cats " + .."(id serial primary key, ".. "name varchar(5))") + print( dump( res ) ) + + res = db:query("insert into cats (name) " + .. "values (\'Bob\'),(\'\'),(null)") + print ( dump( res ) ) + + res = db:query("select * from cats order by id asc") + print ( dump( res ) ) + + -- test in another coroutine + skynet.fork( test2, db) + skynet.fork( test3, db) + -- multiresultset test + res = db:query("select * from cats order by id asc ; select * from cats") + print ("multiresultset test result=", dump( res ) ) + + print ("escape string test result=", mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) + + -- bad sql statement + local res = db:query("select * from notexisttable" ) + print( "bad query test result=" ,dump(res) ) + + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) + + res = db:query("select * from cats order by id asc") + print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) + + + skynet.sleep(1000) + i=i+1 + end + + --db:disconnect() + --skynet.exit() +end) + diff --git a/test/testoverload.lua b/test/testoverload.lua new file mode 100644 index 00000000..10fe39a8 --- /dev/null +++ b/test/testoverload.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "slave" then + +local CMD = {} + +function CMD.sum(n) + skynet.error("for loop begin") + local s = 0 + for i = 1, n do + s = s + i + end + skynet.error("for loop end") +end + +function CMD.blackhole() +end + +skynet.start(function() + skynet.dispatch("lua", function(_,_, cmd, ...) + local f = CMD[cmd] + f(...) + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + for step = 1, 20 do + skynet.error("overload test ".. step) + for i = 1, 512 * step do + skynet.send(slave, "lua", "blackhole") + end + skynet.sleep(step) + end + local n = 1000000000 + skynet.error(string.format("endless test n=%d", n)) + skynet.send(slave, "lua", "sum", n) +end) + +end diff --git a/test/testping.lua b/test/testping.lua index c9f05e63..084729df 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -1,8 +1,8 @@ local skynet = require "skynet" -local snax = require "snax" +local snax = require "skynet.snax" skynet.start(function() - local ps = snax.uniqueservice ("pingserver", "hello world") + local ps = snax.newservice ("pingserver", "hello world") print(ps.req.ping("foobar")) print(ps.post.hello()) print(pcall(ps.req.error)) @@ -31,6 +31,6 @@ end print(string.format("%s\tcount:%d time:%f", name, v.count, v.time)) end - print(snax.kill(ps,"exit")) + print(ps.post.exit("exit")) -- == snax.kill(ps, "exit") skynet.exit() end) diff --git a/test/testpipeline.lua b/test/testpipeline.lua new file mode 100644 index 00000000..b2eea2f1 --- /dev/null +++ b/test/testpipeline.lua @@ -0,0 +1,70 @@ +local skynet = require "skynet" +local redis = require "skynet.db.redis" + +local conf = { + host = "127.0.0.1", + port = 6379, + db = 0 +} + +local function read_table(t) + local result = { } + for i = 1, #t, 2 do result[t[i]] = t[i + 1] end + return result +end + +skynet.start(function() + local db = redis.connect(conf) + + db.pipelining = function (self, block) + local ops = {} + + block(setmetatable({}, { + __index = function (_, name) + return function (_, ...) + table.insert(ops, {name, ...}) + end + end + })) + + return self:pipeline(ops) + end + + do + print("test function") + local ret = db:pipelining(function (red) + red:multi() + red:hincrby("hello", 1, 1) + red:del("hello") + red:hmset("hello", 1, 1, 2, 2, 3, 3) + red:hgetall("hello") + red:exec() + end) + -- ret is the result of red:exec() + for k, v in pairs(read_table(ret[4])) do + print(k, v) + end + end + + do + print("test table") + local ret = db:pipeline({ + {"hincrby", "hello", 1, 1}, + {"del", "hello"}, + {"hmset", "hello", 1, 1, 2, 2, 3, 3}, + {"hgetall", "hello"}, + }, {}) -- offer a {} for result + + print(ret[1].out) + print(ret[2].out) + print(ret[3].out) + + for k, v in pairs(read_table(ret[4].out)) do + print(k, v) + end + end + + db:disconnect() + skynet.exit() +end) + diff --git a/test/testqueue.lua b/test/testqueue.lua new file mode 100644 index 00000000..a9053f41 --- /dev/null +++ b/test/testqueue.lua @@ -0,0 +1,18 @@ +local skynet = require "skynet" +local snax = require "skynet.snax" + +skynet.start(function() + local ps = snax.uniqueservice ("pingserver", "test queue") + for i=1, 10 do + ps.post.sleep(true,i*10) + ps.post.hello() + end + for i=1, 10 do + ps.post.sleep(false,i*10) + ps.post.hello() + end + + skynet.exit() +end) + + diff --git a/test/testredis.lua b/test/testredis.lua index 31a13c5a..1b692e1f 100644 --- a/test/testredis.lua +++ b/test/testredis.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local redis = require "redis" +local redis = require "skynet.db.redis" local conf = { host = "127.0.0.1" , diff --git a/test/testredis2.lua b/test/testredis2.lua new file mode 100644 index 00000000..d3d50961 --- /dev/null +++ b/test/testredis2.lua @@ -0,0 +1,53 @@ +local skynet = require "skynet" +local redis = require "skynet.db.redis" + +local db + +function add1(key, count) + local t = {} + for i = 1, count do + t[2*i -1] = "key" ..i + t[2*i] = "value" .. i + end + db:hmset(key, table.unpack(t)) +end + +function add2(key, count) + local t = {} + for i = 1, count do + t[2*i -1] = "key" ..i + t[2*i] = "value" .. i + end + table.insert(t, 1, key) + db:hmset(t) +end + +function __init__() + db = redis.connect { + host = "127.0.0.1", + port = 6300, + db = 0, + auth = "foobared" + } + print("dbsize:", db:dbsize()) + local ok, msg = xpcall(add1, debug.traceback, "test1", 250000) + if not ok then + print("add1 failed", msg) + else + print("add1 succeed") + + end + + local ok, msg = xpcall(add2, debug.traceback, "test2", 250000) + if not ok then + print("add2 failed", msg) + else + print("add2 succeed") + end + print("dbsize:", db:dbsize()) + + print("redistest launched") +end + +skynet.start(__init__) + diff --git a/test/testrediscluster.lua b/test/testrediscluster.lua new file mode 100644 index 00000000..5c6efdf0 --- /dev/null +++ b/test/testrediscluster.lua @@ -0,0 +1,114 @@ +local skynet = require "skynet" +local rediscluster = require "skynet.db.redis.cluster" + +local test_more = ... + +skynet.start(function () + local db = rediscluster.new({ + {host="127.0.0.1",port=7000}, + {host="127.0.0.1",port=7001},}, + {read_slave=true,auth=nil,db=0,} + ) + db:del("list") + db:del("map") + db:rpush("list",1,2,3) + local list = db:lrange("list",0,-1) + for i,v in ipairs(list) do + print(v) + end + db:hmset("map","key1",1,"key2",2) + local map = db:hgetall("map") + for i=1,#map,2 do + local key = map[i] + local val = map[i+1] + print(key,val) + end + -- test MOVED + db:flush_slots_cache() + print(db:set("A",1)) + print(db:get("A")) + -- reconnect + local cnt = 0 + for name,conn in pairs(db.connections) do + print(name,conn) + cnt = cnt + 1 + end + print("cnt:",cnt) + db:close_all_connection() + print(db:set("A",1)) + print(db:del("A")) + + local slot = db:keyslot("{foo}") + local conn = db:get_connection_by_slot(slot) + -- You must ensure keys at one slot: use same key or hash tags + local ret = conn:pipeline({ + {"hincrby", "{foo}hello", 1, 1}, + {"del", "{foo}hello"}, + {"hmset", "{foo}hello", 1, 1, 2, 2, 3, 3}, + {"hgetall", "{foo}hello"}, + },{}) + print(ret[1].ok,ret[1].out) + print(ret[2].ok,ret[2].out) + print(ret[3].ok,ret[3].out) + print(ret[4].ok) + if ret[4].ok then + for i,v in ipairs(ret[4].out) do + print(v) + end + else + print(ret[4].out) + end + -- dbsize/info/keys + local conn = db:get_random_connection() + print("dbsize:",conn:dbsize()) + print("info:",conn:info()) + local keys = conn:keys("list*") + for i,key in ipairs(keys) do + print(key) + end + print("cluster nodes") + local nodes = db:cluster("nodes") + print(nodes) + print("cluster slots") + local slots = db:cluster("slots") + for i,slot_map in ipairs(slots) do + local start_slot = slot_map[1] + local end_slot = slot_map[2] + local master_node = slot_map[3] + print(start_slot,end_slot) + for i,v in ipairs(master_node) do + print(v) + end + for i=4,#slot_map do + local slave_node = slot_map[i] + for i,v in ipairs(slave_node) do + print(v) + end + end + end + + if not test_more then + skynet.exit() + return + end + local last = false + while not last do + last = db:get("__last__") + if last == nil then + last = 0 + end + last = tonumber(last) + end + for val=last,1000000000 do + local ok,errmsg = pcall(function () + local key = string.format("foo%s",val) + db:set(key,val) + print(key,db:get(key)) + db:set("__last__",val) + end) + if not ok then + print("error:",errmsg) + end + end + skynet.exit() +end) diff --git a/test/testresponse.lua b/test/testresponse.lua new file mode 100644 index 00000000..aabbbaba --- /dev/null +++ b/test/testresponse.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "TICK" then +-- this service whould response the request every 1s. + +local response_queue = {} + +local function response() + while true do + skynet.sleep(100) -- sleep 1s + for k,v in ipairs(response_queue) do + v(true, skynet.now()) -- true means succ, false means error + response_queue[k] = nil + end + end +end + +skynet.start(function() + skynet.fork(response) + skynet.dispatch("lua", function() + table.insert(response_queue, skynet.response()) + end) +end) + +else + +local function request(tick, i) + print(i, "call", skynet.now()) + print(i, "response", skynet.call(tick, "lua")) + print(i, "end", skynet.now()) +end + +skynet.start(function() + local tick = skynet.newservice(SERVICE_NAME, "TICK") + + for i=1,5 do + skynet.fork(request, tick, i) + skynet.sleep(10) + end +end) + +end \ No newline at end of file diff --git a/test/testservice/init.lua b/test/testservice/init.lua new file mode 100644 index 00000000..d69b8973 --- /dev/null +++ b/test/testservice/init.lua @@ -0,0 +1,7 @@ +local skynet = require "skynet" +local kvdb = require "kvdb" + +skynet.start(function() + kvdb.set("A", 1) + print(kvdb.get "A") +end) diff --git a/test/testservice/kvdb.lua b/test/testservice/kvdb.lua new file mode 100644 index 00000000..5dcd695d --- /dev/null +++ b/test/testservice/kvdb.lua @@ -0,0 +1,43 @@ +local skynet = require "skynet" +local service = require "skynet.service" + +local kvdb = {} + +-- service.address is the default address registered by itself. +function kvdb.get(key) + return skynet.call(service.address, "lua", "get", key) +end + +function kvdb.set(key, value) + skynet.call(service.address, "lua", "set", key , value) +end + +-- this function will be injected into an unique service, so don't refer any upvalues +local function service_mainfunc(...) + local skynet = require "skynet" + + skynet.error(...) -- (...) passed from service.new + + local db = {} + + local command = {} + + function command.get(key) + return db[key] + end + + function command.set(key, value) + db[key] = value + end + + -- skynet.start is compatible + skynet.dispatch("lua", function(session, address, cmd, ...) + skynet.ret(skynet.pack(command[cmd](...))) + end) +end + +skynet.init(function() + service.new("kvdb", service_mainfunc, "Service Init") +end) + +return kvdb diff --git a/test/testsha.lua b/test/testsha.lua new file mode 100644 index 00000000..3be749ae --- /dev/null +++ b/test/testsha.lua @@ -0,0 +1,217 @@ +local skynet = require "skynet" +local crypt = require "skynet.crypt" + +local function sha1(text) + local c = crypt.sha1(text) + return crypt.hexencode(c) +end + +local function hmac_sha1(key, text) + local c = crypt.hmac_sha1(key, text) + return crypt.hexencode(c) +end + +-- test case from http://regex.info/code/sha1.lua + +print(1) assert(sha1 "http://regex.info/blog/" == "7f103bf600de51dfe91062300c14738b32725db5", 1) +print(2) assert(sha1(string.rep("a", 10000)) == "a080cbda64850abb7b7f67ee875ba068074ff6fe", 2) +print(3) assert(sha1 "abc" == "a9993e364706816aba3e25717850c26c9cd0d89d", 3) +print(4) assert(sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" == "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 4) +print(5) assert(sha1 "The quick brown fox jumps over the lazy dog" == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 5) +print(6) assert(sha1 "The quick brown fox jumps over the lazy cog" == "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3", 6) +print(7) assert("efb750130b6cc9adf4be219435e575442ec68b7c" == sha1(string.char(136,43,218,202,158,86,64,140,154,173,20,184,170,125,37,54,208,68,171,24,164,89,142,111,148,235,187,181,122):rep(76)), 7) +print(8) assert("432dff9d4023e13194170287103d0377ed182d96" == sha1(string.char(20,174):rep(407)), 8) +print(9) assert("ccba5c47946530726bb86034dbee1dbf0c203e99" == sha1(string.char(20,54,149,252,176,4,96,100,223):rep(753)), 9) +print(10) assert("4d6fea4f8576cd6648ae2d2ee4dc5df0a8309115" == sha1(string.char(118,171,221,33,54,209,223,152,35,67,88,50):rep(985)), 10) +print(11) assert("f560412aabf813d01f15fdc6650489584aabd266" == sha1(string.char(23,85,29,13,146,55,164,14,206,196,109,183,53,92,97,123,242,220,112,15,43,113,22,246,114,29,209,219,190):rep(177)), 11) +print(12) assert("b56795e12f3857b3ba1cbbcedb4d92dd9c419328" == sha1(string.char(21,216):rep(131)), 12) +print(13) assert("54f292ecb7561e8ce27984685b427234c9465095" == sha1(string.char(15,205,12,181,4,114,128,118,219):rep(818)), 13) +print(14) assert("fe265c1b5a848e5f3ada94a7e1bb98a8ce319835" == sha1(string.char(136,165,10,46,167,184,86,255,58,206,237,21,255,15,198,211,145,112,228,146,26,69,92,158,182,165,244,39,152):rep(605)), 14) +print(15) assert("96f186998825075d528646059edadb55fdd96659" == sha1(string.char(100,226,28,248,132,70,221,54,92,181,82,128,191,12,250,244):rep(94)), 15) +print(16) assert("e29c68e4d6ffd3998b2180015be9caee59dd8c8a" == sha1(string.char(247,14,15,163,0,53,50,113,84,121):rep(967)), 16) +print(17) assert("6d2332a82b3600cbc5d2417f944c38be9f1081ae" == sha1(string.char(93,98,119,201,41,27,89,144,25,141,117,26,111,132):rep(632)), 17) +print(18) assert("d84a91da8fb3aa7cd59b99f347113939406ef8eb" == sha1(string.char(28,252,0,4,150,164,91):rep(568)), 18) +print(19) assert("8edf1b92ad5a90ed762def9a873a799b4bda97f9" == sha1(string.char(166,67,113,111,161,253,169,195,158,97,96,150,49,219,103,16,186,184,37,109,228,111):rep(135)), 19) +print(20) assert("d5b2c7019f9ff2f75c38dc040c827ab9d1a42157" == sha1(string.char(38,252,110,224,168,60,2,133,8,153,200,0,199,104,191,62,28,168,73,48,199,217,83):rep(168)), 20) +print(21) assert("5aeb57041bfada3b72e3514f493d7b9f4ca96620" == sha1(string.char(57):rep(738)), 21) +print(22) assert("4548238c8c2124c6398427ed447ae8abbb8ead27" == sha1(string.char(221,131,171):rep(230)), 22) +print(23) assert("ed0960b87a790a24eb2890d8ea8b18043f1a87d5" == sha1(string.char(151,113,144,19,249,148,75,51,164,233,102,232,3,58,81,99,101,255,93,231,147,150,212,216,109,62):rep(110)), 23) +print(24) assert("d7ac6233298783af55901907bb99c13b2afbca99" == sha1(string.char(44,239,189,203,196,79,82,143,99,21,125,75,167,26,108,161,9,193,72):rep(919)), 24) +print(25) assert("43600b41a3c5267e625bbdfde95027429c330c60" == sha1(string.char(122,70,129,24,192,213,205,224,62,79,81,129,22,171):rep(578)), 25) +print(26) assert("5816df09a78e4594c1b02b170aa57333162def38" == sha1(string.char(76,103,48,150,115,161,86,42,247,82,197,213,155,108,215,18,119):rep(480)), 26) +print(27) assert("ef7903b1e811a086a9a5a5142242132e1367ae1d" == sha1(string.char(143,70):rep(65)), 27) +print(28) assert("6e16b2dac71e338a4bd26f182fdd5a2de3c30e6c" == sha1(string.char(130,114,144,219,245,72,205,44,149,68,150,169,243):rep(197)), 28) +print(29) assert("6cc772f978ca5ef257273f046030f84b170f90c9" == sha1(string.char(26,49,141,64,30,61,12):rep(362)), 29) +print(30) assert("54231fc19a04a64fc1aa3dce0882678b04062012" == sha1(string.char(76,252,160,253,253,167,27,179,237,15,219,46,141,255,23,53,184,190,233,125,211,11):rep(741)), 30) +print(31) assert("5511a993b808e572999e508c3ce27d5f12bb4730" == sha1(string.char(197,139,184,188,200,31,171,236,252,147,123,75,7,138,111,167,68,114,73,80,51,233,241,233,91):rep(528)), 31) +print(32) assert("64c4577d4763c95f47ac5d21a292836a34b8b124" == sha1(string.char(177,114,100,216,18,57,2,110,108,60,81,80,253,144,179,47,228,42,105,72,86,18,30,167):rep(901)), 32) +print(33) assert("bb0467117e3b630c2b3d9cdf063a7e6766a3eae1" == sha1(string.char(249,99,174,228,15,211,121,152,203,115,197,198,66,17,196,6,159,170,116):rep(800)), 33) +print(34) assert("1f9c66fca93bc33a071eef7d25cf0b492861e679" == sha1(string.char(205,64,237,65,171,0,176,17,104,6,101,29,128,200,214,24,32,91,115,71,26,11,226,69,141,83,249,129):rep(288)), 34) +print(35) assert("55d228d9bfd522105fe1e1f1b5b09a1e8ee9f782" == sha1(string.char(96,37,252,185,137,194,215,191,190,235,73,224,125,18,146,74,32,82,58,95,49,102,85,57,241,54,55):rep(352)), 35) +print(36) assert("58c3167e666bf3b4062315a84a72172688ad08b1" == sha1(string.char(65,91,96,147,212,18,32,144,138,187,70,26,105,42,71,13,229,137,185,10,86,124,171,204,104,42,2,172):rep(413)), 36) +print(37) assert("f97936ca990d1c11a9967fd12fc717dcd10b8e9e" == sha1(string.char(253,159,59,76,230,153,22,198,15,9,223,3,31):rep(518)), 37) +print(38) assert("5d15229ad10d2276d45c54b83fc0879579c2828e" == sha1(string.char(149,20,176,144,39,216,82,80,56,38,152,49,167,120,222,20,26,51,157,131,160,52,6):rep(895)), 38) +print(39) assert("3757d3e98d46205a6b129e09b0beefaa0e453e64" == sha1(string.char(120,131,113,78,7,19,59,120,210,220,73,118,36,240,64,46,149,3,120,223,80,232,255,212,250,76,109,108,133):rep(724)), 39) +print(40) assert("5e62539caa6c16752739f4f9fd33ca9032fff7e1" == sha1(string.char(216,240,166,165,2,203,2,189,137,219,231,229):rep(61)), 40) +print(41) assert("3ff1c031417e7e9a34ce21be6d26033f66cb72c9" == sha1(string.char(4,178,215,183,17,198,184,253,137,108,178,74,244,126,32):rep(942)), 41) +print(42) assert("8c20831fc3c652e5ce53b9612878e0478ab11ee6" == sha1(string.char(115,157,59,188,221,67,52,151,147,233,84,30,243,250,109,103,101,0,219,13,176,38,21):rep(767)), 42) +print(43) assert("09c7c977cb39893c096449770e1ed75eebb9e5a1" == sha1(string.char(184,131,17,61,201,164,19,25,36,141,173,74,134,132,104,23,104,136,121,232,12,203,115,111,54,114,251,223,61,126):rep(458)), 43) +print(44) assert("9534d690768bc85d2919a059b05561ec94547fc2" == sha1(string.char(49,93,136,112,92,42,117,28,31):rep(187)), 44) +print(45) assert("7dfca0671de92a62de78f63c0921ff087f2ba61d" == sha1(string.char(194,78,252,112,175,6,26,103,4,47,195,99,78,130,40,58,84,175,240,180,255,108,3,42,51,111,35,49,217,160):rep(72)), 45) +print(46) assert("62bf20c51473c6a0f23752e369cabd6c167c9415" == sha1(string.char(28,126,243,196,155,31,158,50):rep(166)), 46) +print(47) assert("2ece95e43aba523cdbf248d07c05f569ecd0bd12" == sha1(string.char(76,230,117,248,231,228):rep(294)), 47) +print(48) assert("722752e863386b737f29a08f23a0ec21c4313519" == sha1(string.char(61,102,1,118):rep(470)), 48) +print(49) assert("a638db01b5af10a828c6e5b73f4ca881974124a0" == sha1(string.char(130,8,4):rep(768)), 49) +print(50) assert("54c7f932548703cc75877195276bc2aa9643cf9b" == sha1(string.char(193,232,122,142,213,243,224,29,201,6,127,45,4,36,92,200,148,111,106,110,221,235,197,51,66,221,123,155,222,186):rep(290)), 50) +print(51) assert("ecfc29397445d85c0f6dd6dc50a1272accba0920" == sha1(string.char(221,76,21,148,12,109,232,113,230,110,96,82,36):rep(196)), 51) +print(52) assert("31d966d9540f77b49598fa22be4b599c3ba307aa" == sha1(string.char(148,237,212,223,44,133,153):rep(53)), 52) +print(53) assert("9f97c8ace98db9f61d173bf2b705404eb2e9e283" == sha1(string.char(190,233,29,208,161,231,248,214,210):rep(451)), 53) +print(54) assert("63449cfce29849d882d9552947ebf82920392aea" == sha1(string.char(216,12,113,137,33,99,200,140,6,222,170,2,115,50,138,134,211,244,176,250,42,95):rep(721)), 54) +print(55) assert("15dc62f4469fb9eae76cd86a84d576905c4bbfe7" == sha1(string.char(50,194,13,88,156,226,39,135,165,204):rep(417)), 55) +print(56) assert("abbc342bcfdb67944466e7086d284500e25aa103" == sha1(string.char(35,39,227,31,206,163,148,163,172,253,98,21,215,43,226,227,130,151,236,177,176,63,30,47,74):rep(960)), 56) +print(57) assert("77ae3a7d3948eaa2c60d6bc165ba2812122f0cce" == sha1(string.char(166,83,82,49,153,89,58,79,131,163,125,18,43,135,120,17,48,94,136):rep(599)), 57) +print(58) assert("d62e1c4395c8657ab1b1c776b924b29a8e009de1" == sha1(string.char(196,199,71,80,10,233,9,229,91,72,73,205,75,77,122,243,219,152):rep(964)), 58) +print(59) assert("15d98d5279651f4a2566eda6eec573812d050ff7" == sha1(string.char(78,70,7,229,21,78,164,158,114,232,100,102,92,18,133,160,56,177,160,49,168,149,13,39,249,214,54,41):rep(618)), 59) +print(60) assert("c3d9bc6535736f09fbb018427c994e58bbcb98f6" == sha1(string.char(129,208,110,40,135,3):rep(618)), 60) +print(61) assert("7dc6b3f309cbe9fa3b2947c2526870a39bf96dc4" == sha1(string.char(126,184,110,39,55,177,108,179,214,200,175,118,125,212,19,147,137,133,89,209,89,189,233,164,71,81,156,215,152):rep(908)), 61) +print(62) assert("b2d4ca3e787a1e475f6608136e89134ae279be57" == sha1(string.char(182,80,145,53,128,194,228,155,53):rep(475)), 62) +print(63) assert("fbe6b9c3a7442806c5b9491642c69f8e56fdd576" == sha1(string.char(9,208,72,179,222,245,140,143,123,111,236,241,40,36,49,68,61,16,169,124,104,42,136,82,172):rep(189)), 63) +print(64) assert("f87d96380201954801004f6b82af1953427dfdcb" == sha1(string.char(153,133,37,2,24,150,93,242,223,68,202,54,118,141,76,35,100,137,13):rep(307)), 64) +print(65) assert("a953e2d054dd77f75337dd9dfa58ec4d3978cfb4" == sha1(string.char(112,215,41,50,221,94):rep(155)), 65) +print(66) assert("e5c3047f9abfdd60f0c386b9a820f11d7028bc70" == sha1(string.char(247,177,124,213,47,175,139,203,81,21,85):rep(766)), 66) +print(67) assert("ee6fe88911a13abfc0006f8809f51b9de7f5920f" == sha1(string.char(81,84,151,242,186,133,39,245,175,79,66,170,246,216,0,88,100,190,137,2,146,58):rep(10)), 67) +print(68) assert("091db84d93bb68349eecf6bfa9378251ecd85500" == sha1(string.char(180,71,122,187):rep(129)), 68) +print(69) assert("d8041c7d65201cc5a77056a5c7eb94a524494754" == sha1(string.char(188,99,87,126,152,214,159,151,234,223,199,247,213,107,63,59,12,146,175,57,79,200,132,202):rep(81)), 69) +print(70) assert("cca1d77faf56f70c82fcb7bc2c0ac9daf553adff" == sha1(string.char(199,1,184,22,113,25,95,87,169,114,130,205,125,159,170,99):rep(865)), 70) +print(71) assert("c009245d9767d4f56464a7b3d6b8ad62eba5ddeb" == sha1(string.char(39,168,16,191,95,82,184,102,242,224,15,108):rep(175)), 71) +print(72) assert("8ce115679600324b58dc8745e38d9bea0a9fb4d6" == sha1(string.char(164,247,250,142,139,131,158,241,27,36,81,239,26,233,105,64,38,249,151,75,142,17,56,217,125,74,132,213,213):rep(426)), 72) +print(73) assert("75f1e55718fd7a3c27792634e70760c6a390d40f" == sha1(string.char(32,145,170,90,188,97,251,159,244,153,21,126,2,67,36,110,31,251,66):rep(659)), 73) +print(74) assert("615722261d6ec8cef4a4e7698200582958193f26" == sha1(string.char(51,24,1,69,81,157,34,185,26,159,231,119):rep(994)), 74) +print(75) assert("4f61d2c1b60d342c51bc47641e0993d42e89c0f9" == sha1(string.char(175,25,99,122,247,217,204,100,0,35,57,65,150,182,51,79,169,99,9,33,113,113,113):rep(211)), 75) +print(76) assert("7d9842e115fc2af17a90a6d85416dbadb663cef3" == sha1(string.char(136,42,39,219,103,95,119,125,205,72,174,15,210,213,23,75,120,56,104,31,63,255,253,100,61,55):rep(668)), 76) +print(77) assert("3eccab72b375de3ba95a9f0fa715ae13cae82c08" == sha1(string.char(190,254,173,227,195,41,49,122,135,57,100):rep(729)), 77) +print(78) assert("7dd68f741731211e0442ce7251e56950e0d05f96" == sha1(string.char(101,155,117,8,143,40,192,100,162,121,142,191,92):rep(250)), 78) +print(79) assert("5e98cdb7f6d7e4e2466f9be23ec20d9177c5ddff" == sha1(string.char(84,67,182,136,89):rep(551)), 79) +print(80) assert("dba1c76e22e2c391bde336c50319fbff2d66c3bb" == sha1(string.char(99,226,185,1,192):rep(702)), 80) +print(81) assert("4b160b8bfe24147b01a247bfdc7a5296b6354e38" == sha1(string.char(144,141,254,1,166,144,129,232,203,31,192,75,145,12):rep(724)), 81) +print(82) assert("27625ad9833144f6b818ef1cf54245dd4897d8aa" == sha1(string.char(31,187,82,156,224,133,116,251,180,165,246,8):rep(661)), 82) +print(83) assert("0ce5e059d22a7ba5e2af9f0c6551d010b08ba197" == sha1(string.char(228):rep(672)), 83) +print(84) assert("290982513a7f67a876c043d3c7819facb9082ea6" == sha1(string.char(150,44,52,144,68,76,207,114,106,153,99,39,219,81,73,140,71,4,228,220,55,244,210,225,221,32):rep(881)), 84) +print(85) assert("14cf924aafceea393a8eb5dd06616c1fe1ccd735" == sha1(string.char(140,194,247,253,117,121,184,216,249,84,41,12,199):rep(738)), 85) +print(86) assert("91e9cc620573db9a692bb0171c5c11b5946ad5c3" == sha1(string.char(48,77,182,152,26,145,231,116,179,95,21,248,120,55,73,66):rep(971)), 86) +print(87) assert("3eb85ec3db474d01acafcbc5ec6e942b3a04a382" == sha1(string.char(68,211):rep(184)), 87) +print(88) assert("f9bd0e886c74d730cb2457c484d30ce6a47f7afa" == sha1(string.char(168,73,19,144,110,93,7,216,40,111,212,192,33,9,136,28,210,175,140,47,125,243,206,157,151,252,26):rep(511)), 88) +print(89) assert("64461476eb8bba70e322e4b83db2beaee5b495d4" == sha1(string.char(33,141):rep(359)), 89) +print(90) assert("761e44ffa4df3b4e28ca22020dee1e0018107d21" == sha1(string.char(254,172,185,30,245,135,14,5,186,42,47,22):rep(715)), 90) +print(91) assert("41161168b99104087bae0f5287b10a15c805596f" == sha1(string.char(79):rep(625)), 91) +print(92) assert("7088f4d88146e6e7784172c2ad1f59ec39fa7768" == sha1(string.char(20,138,80,102,138,182,54,210,38,214,125,123,157,209,215,37):rep(315)), 92) +print(93) assert("2e498e938cb3126ac1291cee8c483a91479900c1" == sha1(string.char(140,197,97,112,205,97,134,190):rep(552)), 93) +print(94) assert("81a2491b727ef2b46fb84e4da2ced84d43587f4e" == sha1(string.char(109,44,17,199,17,107,170,54,113,153,212,161,174):rep(136)), 94) +print(95) assert("0e4f8a07072968fbc4fe32deccbb95f113b32df7" == sha1(string.char(181,247,203,166,34,61,180,48,46,77,98,251,72,210,217,242,135,133,38,12,177,163,249,31,1,162):rep(282)), 95) +print(96) assert("8d15dddd48575a1a0330976b57e2104629afe559" == sha1(string.char(15,60,105,249,158,45,14,208,202,232,255,181,234,217):rep(769)), 96) +print(97) assert("98a9dd7b57418937cbd42f758baac4754d5a4a4b" == sha1(string.char(115,121,91,76,175,110,149,190,56,178,191,157,101,220,190,251,62,41,190,37):rep(879)), 97) +print(98) assert("578487979de082f69e657d165df5031f1fa84030" == sha1(string.char(189,240,198,207,102,142,241,154):rep(684)), 98) +print(99) assert("3e6667b40afb6bcc052654dd64a653ad4b4f9689" == sha1(string.char(85,82,55,80,43,17,57,20,157,10,148,85,154,58,254,254,221,132,53,105,43,234,251,110,111):rep(712)), 99) + +print(100) assert("31285f3fa3c6a086d030cf0f06b07e7a96b5cbd0" == hmac_sha1("63xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 100) +print(101) assert("2d183212abc09247e21282d366eeb14d0bc41fb4" == hmac_sha1("64xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 101) +print(102) assert("ff825333e64e696fc13d82c19071fa46dc94a066" == hmac_sha1("65xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 102) +print(103) assert("ecc8b5d3abb5182d3d570a6f060fef2ee6c11a44" == hmac_sha1(string.char(220,58,17,206,77,234,240,187,69,25,179,182,186,38,57,83,120,107,198,148,234,246,46,96,83,28,231,89,3,169,42,62,125,235,137), string.char(1,225,98,83,100,71,237,241,239,170,244,215,3,254,14,24,216,66,69,30,124,126,96,177,241,20,44,3,92,111,243,169,100,119,198,167,146,242,30,124,7,22,251,52,235,95,211,145,56,204,236,37,107,139,17,184,65,207,245,101,241,12,50,149,19,118,208,133,198,33,80,94,87,133,146,202,27,89,201,218,171,206,21,191,43,77,127,30,187,194,166,39,191,208,42,167,77,202,186,225,4,86,218,237,157,117,175,106,63,166,132,136,153,243,187)), 103) +print(104) assert("bd1577a9417d804ee2969636fa9dde838beb967d" == hmac_sha1(string.char(216,76,38,50,235,99,92,110,245,5,134,195,113,7), string.char(144,3,250,84,145,227,206,87,188,42,169,182,106,31,207,205,33,76,52,158,255,49,129,169,9,145,203,225,90,228,163,33,49,99,29,135,60,112,152,5,200,121,35,77,56,116,68,109,190,136,184,248,144,172,47,107,30,16,105,232,146,137,24,81,245,94,28,76,27,82,105,146,252,219,119,164,21,14,74,192,209,208,156,56,172,124,89,218,51,108,44,174,193,161,228,147,219,129,172,210,248,239,22,11,62,128,1,50,98,233,141,224,102,152,44,68,66,46,210,114,138,113,121,90,7,70,125,191,192,222,225,200,217,48,22,10,132,29,236,71,108,140,102,96,51,142,51,220,4)), 104) +print(105) assert("e8ddf90f0c117ee61b33db4df49d7fc31beca7f7" == hmac_sha1(string.char(189,213,184,86,24,160,198,82,138,223,71,149,249,70,183,47,0,106,27,39,85,102,57,190,237,182,2,10,21,253,252,3,68,73,154,75,79,247,28,132,229,50,2,197,204,213,170,213,255,83,100,213,60,67,202,61,137,125,16,215,254,157,106,5), string.char(78,103,249,15,43,214,87,62,52,57,135,84,44,92,142,209,120,139,76,216,33,223,203,96,218,12,6,126,241,195,47,203,196,22,113,138,228,44,122,37,215,166,184,195,91,97,175,153,115,243,37,225,82,250,54,240,20,237,149,183,5,43,142,113,214,130,100,149,83,232,70,106,152,110,25,74,46,60,159,239,193,173,235,146,173,142,110,71,1,97,54,217,52,228,250,42,223,53,105,24,87,98,117,245,101,189,147,238,48,111,68,52,169,78,151,38,21,249)), 105) +print(106) assert("e33c987c31bb45b842868d49e7636bd840a1ffd3" == hmac_sha1(string.char(154,60,91,199,157,45,32,99,248,5,163,234,114,223,234,48,238,82,43,242,52,176,243,5,135,26,141,49,105,120,220,135,192,96,219,152,126,74,58,110,200,56,108,1,68,175,82,235,149,191,67,72,195,46,35,131,237,188,177,223,145,122,26,234,136,93,34,96,71,214,55,27,13,116,235,109,58,83,175,226,59,13,218,93,5,132,43,235), string.char(182,10,154)), 106) +print(107) assert("f6039d217c16afadfcfae7c5e3d1859801a0fe22" == hmac_sha1(string.char(73,120,173,165,190,159,174,100,255,56,217,98,249,11,166,25,66,95,96,99,70,73,223,132,231,147,220,151,79,102,111,98), string.char(134,93,229,103)), 107) +print(108) assert("f6056b19cf6b070e62a0917a46f4b3aefcf6262d" == hmac_sha1(string.char(49,40,62,214,208,180,147,74,162,196,193,9,118,47,100,9,235,94,3,122,226,163,145,175,233,148,251,88,49,216,208,110,13,97,255,189,120,252,223,241,55,210,77,4), string.char(92,185,37,240,36,97,21,132,188,80,103,36,162,245,63,104,19,106,242,44,96,28,197,26,46,168,73,46,76,105,30,167,101,64,67,119,65,140,177,226,223,108,206,60,59,0,182,125,42,200,101,159,109,6,62,85,67,66,88,137,92,234,61,19,22,177,144,127,129,129,195,230,180,149,128,148,45,18,94,163,196,55,70,184,251,3,200,162,16,210,188,61,186,218,173,227,212,8,125,20,138,82,68,170,24,158,90,98,228,166,246,96,74,24,217,93,91,102,246,221,121,115,157,243,45,158,45,90,186,11,127,179,59,72,37,71,148,123,62,150,114,167,248,197,18,251,92,164,158,83,129,58,127,162,181,92,85,121,13,118,250,221,220,150,231,5,230,28,213,25,251,17,71,68,234,173,10,206,111,72,123,205,49,73,62,209,173,46,94,91,151,122)), 108) +print(109) assert("15ddfa1baa0a3723d8aff00aeefa27b5b02ede95" == hmac_sha1(string.char(91,117,129,150,117,169,221,182,193,130,126,206,129,177,184,171,174,224,91,234,60,197,243,158,59,0,122,240,145,106,192,179,96,84,46,239,170,123,120,51,142,101,45,34), string.char(27,71,38,183,232,93,23,174,151,181,121,91,142,138,180,125,75,243,160,220,225,205,205,72,13,162,104,218,47,162,77,23,16,154,31,156,122,67,227,25,190,102,200,57,116,190,131,8,208,76,174,53,204,88,227,239,71,67,208,32,57,72,165,121,80,139,80,29,90,32,229,208,115,169,238,189,206,82,180,68,157,124,119,1,75,27,122,246,197,178,90,225,138,244,73,12,226,104,191,70,53,210,245,250,239,238,3,229,196,22,28,199,122,158,9,33,109,182,109,87,25,159,159,146,217,77,37,25,173,211,38,173,70,252,18,193,7,167,160,135,63,190,149,14,221,143,173,152,184,143,157,245,137,219,74,239,185,40,14,0,29,87,169,84,67,75,255,252,201,131,75,108,43,129,210,193,108,139,60,228,29,36,150,15,86)), 109) +print(110) assert("d7cba04970a79f5d04c772fbe74bcc0951ff9c67" == hmac_sha1(string.char(159,179,206,236,86,25,53,102,163,243,239,139,134,14,243), string.char(106,160,249,31,114,205,9,66,139,182,209,218,31,151,57,132,182,85,218,220,103,15,210,218,101,244,240,227,12,184,127,137,40,127,232,195,234,182,0,214,125,122,141,207,61,244,143,202,159,229,100,76,165,44,226,137,100,61,1,107,132,142,144,158,223,249,151,78,186,194,189,83,45,66,178,41,23,172,195,137,170,216,208,27,149,112,68,188,0)), 110) +print(111) assert("84625a20dc9ada78ec9de910d37734a299f01c5d" == hmac_sha1(string.char(85,239,250,237,210,126,142,84,119,107,100,163,15,179,132,206,112,85,119,101,44,163,240,10,14,69,169,158,170,190,95,66,129,69,218,229), string.char(210,103,131,185,91,224,115,108,129,11,50,16,90,98,64,124,157,177,50,21,30,201,244,101,136,104,149,102,34,166,25,62,1,79,216,4,221,113,168,169,5,151,172,22,166,28,130,137,251,164,220,189,253,45,149,80,247,84,130,208,69,49,120,8,90,154,100,191,121,81,230,207,23,189,7,164,112,123,158,192,224,255,218,200,70,238,211,161,35,251,150,125,24,10,131,220,57,178,196,38,231,196,206,94,118,32,56,197,136,148,145,247,188,64,56,53,195,140,92,202,22,122,229,105,115,14,42,27,107,223,105)), 111) +print(112) assert("c463318ac1878cd770ec93b2710b706296316894" == hmac_sha1(string.char(162,21,153,195,254,135,203,75,152,144,200,187,226,4,248,93,161,180,219,181,99,130,122,28,179,140,152,9,21,115,34,140,162,45,88,4,33,238,179,125,58,23,108,194,158,48,191,40,3,50,81,247,114,241,0,88,147,93,57,178,73,187,11,195,254,171,106,167,245,190,117,160,1,219,200,249,107,233,58,19,122), string.char(246,179,198,163,52,106,45,38,131,22,142,185,149,121,79,211,0,16,102,52,46,176,83,7,32,42,103,184,234,107,180,128,128,130,21,27,236)), 112) +print(113) assert("e2d37df980593b3e52aadb0d99d61114fb2c1182" == hmac_sha1(string.char(43,84,125,158,182,211,26,238,222,247,6,171,184,54,70,44,169,4,74,34,98,71,118,189,138,53,8,164,117,22,76,171,57,255,122,230,110,122,228,22,252,123,174,218,222,77,80,150,159,43,236,137,234,48,122,138,100,137,112), string.char(249,234,226,86,109,2,157,76,229,42,178,223,196,247,42,194,17,17,117,3,45,6,80,202,22,105,44,242,84,25,21,189,5,216,35,200,220,192,110,81,215,145,109,179,48,44,40,35,216,240,48,240,33,210,79,35,64,189,81,15,135,228,83,14,254,32,211,229,158,79,188,230,84,106,78,126,226,106,203,59,67,134,186,52,21,48,2,142,231,116,241,167,177,175,74,188,232,234,56,41,181,118,232,190,184,76,64,109,167,178,123,118,3,50,46,254,253,83,156,116,220,247,69,27,160,167,210,205,79,60,28,253,17,219,32,44,217,223,77,153,229,55,113,75,234,154,247,13,133,49,220,200,241,111,136,205,14,78,222,55,181,250,160,143,224,37,63,227,155,12,39,173,209,45,171,93,93,70,36,129,111,173,183,112,31,231,22,146,129,171,75,128,45)), 113) +print(114) assert("666f9c282cf544c9c28006713a7461c7efbbdbda" == hmac_sha1(string.char(71,33,203,83,173,199,175,245,206,13,237,187,54,61,85,15,153,125,168,223,231,56,46,250,173,192,247,189,45,166,225,223,109,254,15,79,144,188,71,201,70,25,218,205,13,184,204,219,221,82,133,189,144,179,242,125,211,108,100,1,132,110,231,87,107,91,169,101,241,105,30), string.char(98,122,51,157,136,38,149,9,82,27,218,155,76,61,254,154,43,172,59,105,123,45,97,146,191,58,50,153,139,40,37,116)), 114) +print(115) assert("6904a4ce463002b03923c71cdac6c38f57315b63" == hmac_sha1(string.char(15,40,41,76,105,201,153,223,174,12,100,114,234,95,204,95,84,31,26,28,69,85,48,111,40,173,162,6,198,36,252,179,244,9,112,213,64,87,58,186,4,147,229,211,161,30,226,159,75,38,91,89,224,245,156,110,229,50,210), string.char(213,155,72,86,75,185,138,211,199,57,75,9,1,141,184,89,82,180,105,17,193,63,161,202,25,60,16,201,72,179,74,129,73,172,84,39,140,33,25,122,136,143,116,100,100,234,246,108,135,180,85,12,85,151,176,154,172,147,249,242,180,207,126,126,235,203,55,8,251,29,135,134,166,152,80,76,242,15,69,225,199,221,133,118,194,227,9,185,190,125,120,116,182,15,241,209,113,253,241,58,145,106,136,25,60,47,174,209,251,54,159,98,103,88,245,61,108,91,12,245,119,191,232,36)), 115) +print(116) assert("c57c742c772f92cce60cf3a1ddc263b8df0950f3" == hmac_sha1(string.char(193,178,151,175,44,234,140,18,10,183,92,17,232,95,94,198,229,188,188,131,247,236,215,129,243,171,223,83,42,173,88,157,29,46,221,57,80,179,139,80), string.char(214,95,30,179,51,95,183,198,185,9,76,215,255,122,91,103,133,117,42,33,0,145,60,129,129,237,203,59,141,214,108,79,247,244,187,250,157,177,245,72,191,63,176,211)), 116) +print(117) assert("092336f7f1767874098a54d5f27092fbb92b7c94" == hmac_sha1(string.char(208,10,217,108,72,232,56,67,6,179,160,172,29,67,115,171,118,82,124,118,61,111,21,132,209,92,212,166,181,129,43,55,198,96,169,112,86,212,68,214,81,239,122,216,104,73,114,209,60,182,248,118,74,100,26,1,176,3,166,188), string.char(42,4,235,56,198,119,175,244,93,77)), 117) +print(118) assert("c1d58ad2b611c4d9d59e339da7952bdae4a70737" == hmac_sha1(string.char(100,129,37,207,188,116,232,140,204,90,90,93,124,132,140,81,102,156,67,254,228,192,150,161,10,62,143,218,237,111,151,98,78,168,188,87,170,190,35,228,169,228,143,252,213,85,11,124,92,91,18,27,122,141,98,6,77,106,205,209,2,185,249), string.char(58,190,56,255,176,80,220,228,0,251,108,108,220,197,51,131,164,162,60,181,21,54,122,174,31,13,4,84,198,203,105,11,64,230,1,30,218,208,252,219,44,147,2,108,227,66,49,71,21,95,248,93,193,180,165,240,224,226,13,9,208,186,12,118,243,28,113,49,122,192,212,164,43,41,151,183,187,126,115,85,174,40,125,9,54,193,164,95,21,77,200,226,50,115,187,122,34,141,255,224,239,12,132,191,48,102,205,248,128,164,116,48,39,191,98,53,169,230,249,215,231,152,45,27,226,10,143,15,209,157,208,181,15,210,195,5,29,48,29,125,62,59,191,216,170,179,226,110,40,46,32,130,235,48,234,233,17)), 118) +print(119) assert("3de84c915278a7d5e7d5629ec88193fb5bbadd15" == hmac_sha1(string.char(89,175,111,33,154,12,173,230,28,117), string.char(188,233,41,180,142,157,96,76,105,212,92,202,155,167,179,28,12,156,64,73,32,253,253,166,9,240,7,0,248,43,101,135,226,231,173,221,180,43,160,217,6,38,183,186,214,217,137,83,148,148,40,141,217,98,209,12,167,102,95,166,136,231,232,84,59,112,148,201,166,104,135,124,189,85,160,183,143,122,200,190,144,205,25,254,180,188,108,225,171,131,240,185,86,243,192,173,130,50,150,57,242,180,132,193,11,110,247,121,25,24,110,199,156,121,233,149,79,103,15,173,184,143,45,125,164,242,125,10,183,189,189,135,121,59,148,106,77,9,16,123,176,100,142,246,156,180,202,87,43,102,113,123)), 119) +print(120) assert("e8cc5a50f80e7d52edd7ae4ec037414b70798598" == hmac_sha1(string.char(139,243,17,238,11,156,138,122,212,86,201,213,100,167,65,199,92,182,255,36,221,82,192,213,199,162,69,42,222,95,65,170,146,48,39,185,147,18,140,122,168,141), string.char(141,216,25,29,235,207,123,188,85,53,131,180,125,226,97,244,250,138,64,39,30,250,146,101,219,171,213,158,164,172,137,22,136,86,46,77,95,213,66,198,15,24,164,148,29,166,169,195,196,111,122,147,247,215,148,9,129,40,133,178,69,242,171,235,181,83,241,208,237,17,37,30,188,152,47,214,69,229,114,225,75,172,163,184,38,158,230,177,187,124,33,240,238,148,197,235,237,98,33,237,59,205,147,128,108,254,95,5,6,49,10,224,78,139,164,235,220,78,224,15,213,136,198,217,114,156,130,247,167,64,36,10,183,221,193,220,195,80,125,143,248,228,254,6,221,137,170,211,66)), 120) +print(121) assert("80d9a0bc9600e6a0130681158a1787ef6b9c91d6" == hmac_sha1(string.char(152,28,190,131,179,120,137,63,214,85,213,89,116,246,171,92,0,47,44,102,120,206,185,116,71,106,195), string.char(250,9,181,163,46,88,166,238,164,40,207,236,152,104,201,42,14,255,125,57,173,176,230,171,63,6,254,130,140,201,45,152,164,216,171,27,172,105,19,103,128,33,255,93,64,155,55,210,140,2,94,168,168,164,5,218,249,227,221,133,72,112,97,243,140,149,193,80)), 121) +print(122) assert("cb4182de586a30d606a057c948fe33733145790a" == hmac_sha1(string.char(193,229,65,81,159,93,162,173,74,64,195,41,80,189,104,238,119,67,255,63,209,247,146,247,210,208,86,14,102,153,246,175,242,209,42,4,243,138,217,58,206,147,19,163,152,239,154,78,5,1,175,97,251,24,185,10,67,107,174,145,178,121,36,238,108,85,214,162,78,195,107,135,114,76,180,37,99,103,78,232,29,244,11,60,236,112,18,241,39,164,107,18), string.char(222,85,11,36,12,191,252,103,180,161,84,168,21,125,50,76,4,148,195,230,210,114,35,116,225,176,16,64,44,20,17,224,227,243,175,251,204,177,41,223,76,216,228,221,54,226,150,209,145,175,142,137,140,25,196,99,252,175,125,15,39,76,47,127,188,51,88,227,194,171,249,141,12,249,225,199,241,112,153,26,107,204,191,23,241,46,223,143,9,9,46,64,197,209,23,18,208,139,148,45,146,94,80,86,49,12,122,218,14,210,133,164,39,227,102,60,129,6,43)), 122) +print(123) assert("864e291ec69124c3a43976bae4ba1788f181f027" == hmac_sha1(string.char(231,211,73,154,64,125,224,253,200,234,208,210,83,9), string.char(33,248,155,139,159,173,90,40,200,95,96,12,81,103,8,139,211,189,87,75,8,6,36,103,116,204,137,181,81,233,97,171,47,27,143,164,63,3,223,107,80,232,182,154,7,255,190,171,209,115,219,6,34,109,1,254,214,73,2)), 123) +print(124) assert("bee434914e65818f81bd07e4e0bbb58328be3ca1" == hmac_sha1(string.char(222,123,148,175,157,117,104,212,169,13,252,113,231,104,37,8,147,119,92,27,100,132,250,105,63,13,195,90,251,122,185,97,159,68,36,7,75,163,179,3,6,170,164,152,86,30,49,239,201,245,43,220,78,62,154,206,95,24,149,138,138,15,24,68,58,255,99,88,143,192,12), string.char(97,76,73,171,140,99,57,217,56,73,182,189,58,19,177,104,85,85,45,31,170,132,53,51,155,143,70,169,189,5,41,231,34,132,235,228,114,58,100,105,146,27,67,89,32,34,78,133,155,222,88,26,83,198,128,183,19,243,27,186,192,77,184,54,142,57,24)), 124) +print(125) assert("0268f79ef55c3651a66a64a21f47495c5beeb212" == hmac_sha1(string.char(181,166,229,31,47,25,64,129,95,242,221,96,73,42,243,170,15,33,14,198,78,194,235,139,177,112,191,190,52,224,93,95,107,125,245,17,170,161,53,148,19,180,83,154,45,98,76,170,51,178,114,71,34,242,184,75,1,130,199,40,197,88,203,86,169,109,206,67,86,175,201,135,101,76,130,144,248,80,212,14,119,186), string.char(53,208,22,216,93,88,59,64,135,112,49,253,34,11,210,160,117,40,219,36,226,45,207,163,134,133,199,101)), 125) +print(126) assert("bcb2473c745d7dee1ec17207d1d804d401828035" == hmac_sha1(string.char(143,117,182,35), string.char(110,95,240,139,124,96,238,255,165,146,205,18,155,244,252,176,24,19,253,39,33,248,90,95,34,115,70,63,195,91,124,144,243,91,110,80,173,47,46,231,73,228,207,37,183,28,42,144,66,248,178,255,129,52,55,232,1,33,193,185,184,237,8)), 126) +print(127) assert("402ed75be595f59519e3dd52323817a65e9ff95b" == hmac_sha1(string.char(247,77,247,124,27,156,148,227,12,21,17,94,56,14,118,47,140,239,200,249,216,139,7,172,16,211,237,94,62,201,227,42,250,8,179,21,39,85,186,145,147,106,127,67,111,250,163,89,152,115,166,254,8,246,141,238,43,45,194,131,191,202), string.char(138,227,90,40,221,37,181,54,26,20,24,125,19,101,12,120,111,20,104,10,225,140,64,218,8,33,179,95,215,142,203,127,243,231,166,76,234,159,59,160,132,56,215,143,103,75,155,158,56,126,71,252,229,54,34,240,30,133,110,67,146,213,116,73,14,160,186,169,155,196,84,132,171,200,171,56,92,38,136,90,57,155,145,67,190,198,235,112,242,120,150,191,216,41,21,36,205,42,68,145,226,246,178,70,60,157,254,206,70,84,189,36,197,48,208,249,144,223,6,72,17,253,236,106,205,245,30,1,80,121,165,103,12,202,115,227,192,64,42,223,113,200,195,156,120,202,110,131,130,39,64,217,108,23,133,140,56,144,167,77,45,236,145,161,150,229,85,231,203,159,203,85,125,221,226)), 127) +print(128) assert("20231b6abcdd7e5d5e22f83706652acf1ae5255e" == hmac_sha1(string.char(132,122,252,170,107,28,195,210,121,194,245,252,42,64,42,103,220,18,61,68,19,78,182,234,93,130,175,20,4,199,66,91,247,247,87,107,89,205,68,125,19,254,59,47,228,134,200,145,148,78,52,236,51,255,152,231,47,168,213,124,228,34,48), string.char(61,232,69,199,207,49,72,159,37,230,105,207,63,141,245,127,142,77,168,111,126,92,145,26,202,215,116,19,125,81,203,11,86,36,19,218,90,255,4,10,21,185,151,111,188,125,123,2,137,151,171,178,195,199,106,55,42,42,20,164,35,177,237,41,207,24,102,218,213,223,204,186,212,30,59,121,52,34,84,76,142,179,75,6,84,8,72,210,72,177,45,103,218,70,165,69,36,152,50,228,147,192,61,104,209,76,155,147,169,162,151,178,72,109,241,41,76,0,60,251,107,99,92,37,95,215,172,154,172,93,254,65,176,43,99,242,151,78,48,58,35,49,23,174,115,224,227,106,49,30,9)), 128) +print(129) assert("b5a24d7aadc1e8fb71f914e1fa581085a8114b27" == hmac_sha1(string.char(233,21,254,12,70,129,236,10,187,36,119,197,152,242,131,11,10,243,169,217,128,247,196,111,183,109,146,155,92,91,71,83,0,136,109,1,130,143,0,8,6,22,160,153,190,164,199,56,152,229), string.char(152,112,218,132,38,125,94,106,64,9,137,105)), 129) +print(130) assert("7fd9169b441711bef04ea08ed87b1cd8f245aeb2" == hmac_sha1(string.char(108,226,105,159), string.char(103,218,254,85,50,58,111,74,108,60,174,27,113,65,100,217,97,208,125,38,170,151,72,125,82,114,185,58,4,143,39,223,250,168,66,182,37,216,130,111,103,157,59,0,245,222,8,65,240)), 130) +print(131) assert("98038102f8476b28dd11b535b6c1b93eb8089360" == hmac_sha1(string.char(77,121,52,138,71,149,161,87,106,75,232,247,8,148,175,24,23,60,151,104,176,208,148,227,226,191,73,123,164,36,177,235,101,190,128,202,139,116,201,125,61,41,204,187,48,107,63,231,12,137,11,228,93,136,178,243,90), string.char(90,6,232,110,12,66,45,86,141,121,2,6,177,135,64,208,45,178,129,37,253,246,74,48,71,211,243,121,31,209,138,147,185,141,65,185,245,9,137,134,148,123,181,128,204,74,222,150,239,169,179,36,236,104,147,255,251,204,5,191,198,185,153,195,26,112,179,170,232,101,238,143,143)), 131) +print(132) assert("ba43c9b8e98b4b7176a1bb63fcbed5b004dc4fcd" == hmac_sha1(string.char(133,107,178,183,204,2,203,106,242,180,87,58,134), string.char(211,208,255,34,198,99,119,185,171,52,118,94,65,241,45,24,40,6,2,203,126,216,21,51,245,154,92,198,201,220,190,135,182,137,113,68,250,94,126,233,140,94,118,244,244,98,252,97,217,204,239,218,178,240,65,150,246,217,231,142,157,33,113,135,135,214,137,103,211,213,48,132,171,43,96,51,241,45,42,237,53,247,134,102,163,214,96,147,131,74,180,19,7,12,174,60,37,60,78,85,92,186,121,27,234,128,132,64,47,106,127,218,52,30,59,230,85,240,164,54,168,232,131,110,145,125,255,238,145,237,134,196,197,138,105,74,34,134,29,249,222,119,194,104,230)), 132) +print(133) assert("0c1ad0332f0c6c1f61e6cc86f0a343065635ecb3" == hmac_sha1(string.char(230,111,251,74,216,102,12,134,90,44,121,175,56,221,225,235,180,246,56,12,236,119,119,109,97,59,224,121,27,37,72,219,138,193,50,219,193,152,1,229,224,197,233,76,188,62,120,17,63,158,186,198,87,135,34,60,164,139,3,200,20,188,203,110,212,137,91,70,35,255,146,213,216,71,73,143,55,219,54,142,105,83,175,193,32,226,150,51,59), string.char(185,250,11,8,121,214,91,27,1,50,67,204,219,252,212,198,117,234,57,127,214,12,220,104,44,177,225,238,103,138,144,162,28,69,143,108,192,4,160,63,175,185,123,46,151,221,127,145,55,32,85,245,251,178,125,223,107,192,206,207,232,231,190,44,221,173,1,59,12,178,112,22,253,222,14,40,18,141,128,144,196,6,112,206,183,144,215,156,159,79,132,152,170,22,68,106,137,248,12,242,231,98,96,144,148,20,212,30,242,109,36,112,50,51,57,212,181,191,80,73,215,119,244,209,57,108,147,182,67,204,154,48,216,116)), 133) +print(134) assert("466442888b6989fd380f918d0476a16ae26279cf" == hmac_sha1(string.char(250,60,248,215,154,159,2,121), string.char(236,249,8,10,180,146,36,144,83,196,49,166,17,81,51,72,28)), 134) +print(135) assert("f9febfeda402b930ec1b95c1325788305cb9fde8" == hmac_sha1(string.char(124,195,165,216,158,253,119,86,162,36,185,72,162,141,160,225,183,25,54,123,73,61,40,165,101,154,157,200,113,14,117,144,185,64,236,219,3,87,90,49), string.char(73,1,223,35,5,159,39,85,95,170,227,61,14,227,142,222,255,253,152,123,104,35,229,6,195,106,30,59,5,36,2,173,71,161,217,189,47,193,3,216,34,10,25,30,212,255,141,94,25,72,52,58,50,56,180,99,173,155)), 135) +print(136) assert("53fe70b7e825d017ef0bdecbacdda4e8a76cbc6f" == hmac_sha1(string.char(144,144,130,150,207,183,143,100,188,177,90,5,4,95,116,105,252,118,187,251,35,113,251,86,36,234,176,165,55,165,158,4,182,85,62,255,18,146,128,67,221,183,78,71,158,184,140,14,84,44,36,79,244,158,37,1,122,43,103,228,217,253,72,113,228,121,160,29,87,5,158,64,38,253,179,122,187,157,106,99,161,79,97,176,119,86,101,40,142,241,217,85), string.char(12,89,157,112,207,206,171,254,87,106,42,113,70,72,222,239,88,94,14,41,14,175,206,56,219,244,230,40,33,154,107,249,45,70,55,104,151,193,246,133,99,59,244,179,8,145,225,100,146,142,128,74,40,155,99,93,204,253,249,222,64,99,156,121,142,49,166,62,171,223,135,55,212,183,144,218,56,12,211,99,254,58,249,219,197,189,173,141,60,196,241,157,67,151,251,172,49,105,200,88,224,175,9,79,65,40,5,255,222,14,152,149,228,83,154,207,104,132,106,96,40,229,182,120,100,207,223,81,171,141,1,171)), 136) +print(137) assert("81b1df7265954a8aee4e119800169660ff9e75c8" == hmac_sha1(string.char(232,167,4,53,90,111,79,91,163,125,230,202,52,242,160,135,66,211,246,17,131,109,109,235,91), string.char(111,177,251,180,63,13,33,205,231,130,203,167,177,156,87,70,33,148,229,163,158,224,123,37,18,204,185,89,235,2,30,252,229,202,170,157,175,157,208,254,135,190,34,14,42,211,154,243,31,100,71,53,169,76,36)), 137) +print(138) assert("caf986508402b3feb70c5b44d902f4a9428a44d2" == hmac_sha1(string.char(127,2,162,53,6,172,189,169,176,254,107,46,57,207,23,25,182,72,34,228,164,113,12,179,149,45,169,19,3,204,202,10,126,153,130,41,143,200,198,47,215,15,58,124,225,60,171,98,8,211,72,235,211,187,172,37,119,96,55,243,98,198,225,191,79,207,94,215,255,21,101,128,153,233,88,101,57,195,70,194), string.char(97,24,129,203,10,176,242,39,165,95,51,30,187,106,207,160,18,154,16,130,178,80,206,128,148,56,213,55,46,85,76,100,50,131,41,167,130,12,81,161,158,55,181,12,12,38,89,193,136,220,81,114,191,157,20,221,171,245)), 138) +print(139) assert("83bd91a9e8072010f3b54d215c56ada0cd810bb6" == hmac_sha1(string.char(215,33,57,193,51,126,38,114,7,29,93,101,78,152,222,121,42,0,236,169,48,137,202,48,90,249,235,46,44,126,78,251,15,84,200,235,43,108,8,196,210,57,152,86,254), string.char(153,35,68,230,52,95,243,220,32,101,148,76,80,168,187,172,170,254,10,70,31,117,40,131,61,155,200,189,25,198,120,41,181,53,14,175,64,89,126,94,242,61,52,1,188,255,121,254,74,88,19,10,126,225,89,31,21,104,199,82,149,60,110,194,123,236,13,38,7,213,176,182,202,211,178,185,99,8,173,220,168,166,108,208,71,152,174,5,134,167,220,47,74,177,231,90,114,208,168,47,112,74,58,73,94,224,101,64,128,255,186,179,119,159,117,110,2,188,38,73,230,39,50,73,162,22,117,185,182,168,70,252,11,242,243,165,32,104,220,211)), 139) +print(140) assert("c123d92ee67689922dfb3b68a45584e7e5dc5dc3" == hmac_sha1(string.char(167,32,181,181,249,210), string.char(249,7,96,196,48,157,109,129,192,227,82,84,196,167,224,145,61,177,60,74,217,117,199,147,147,54,198,138,5,51,135,112,29,184,49,16,0,240,172,21,214,114,146,57,2,154,205,60,113,32,113,255,165,5,178,15,159)), 140) +print(141) assert("454d9ba00ac4e0e90a2866ba2abefba40dac9aab" == hmac_sha1(string.char(116,2,170,84,236,37), string.char(219,144,86,39,237,166,110,90,213,70)), 141) +print(142) assert("e919cce3908786205511fee0283e1eb41c0c45a1" == hmac_sha1(string.char(180,76,140,201,56,232,48,183,173,36,111,94,208,153,234,255,52,18,121,118,117,77,92,74,241,103,193,164,169,1,60,46,101,73,61,221,26,253,209,124,99,154,177,218,59,238,159,191,0,65,117,78,15,12,92,35,254,40,11,112,112,91,49,248,198,229,161,247,130,170,76,146,8,220,134,96,155,68,50,168,186,135,188,186,36), string.char(26,47,201,201,197,232,196,239,204,197,162,14,53,254,88,75,43,254,11,20,52,61,136,206,162,29,223,55,125,50,200,239,135,51,154,54,78,191,188,0,137,191,149,162,191,103,0,168,52,178,147,251,253,86,98,17,15,202,231,0,122,10,131,25,15,129,48,190,60,205,185,114,193,122,19,47,52,142,124,107,45,174,221,196,18,32,79,12,129,84,40)), 142) +print(143) assert("fa770f6b76984edb0f7fa514a19d3c7ef9c82c51" == hmac_sha1(string.char(70,55,200,82,238,241,84,180,129,122,103,199,170,177), string.char(88,236,14,60,249,2,197,242,76,152,248,241,158,232,113,242,209,93,232,113,147,210,80,180,44,124,186,150,172,177,138,131,117,151,174,231,93,248,244,200,191,169,159,206,232,246,2,121,106,183,107,79,210,73,145,244,254,248,85,217,156,221,238,120,224,176,31,56,246,26,3,186,189,86,41,17,34,8,208,58,46,130,226,139,209,194,3,34,55,213,154,12,250,229,201,122,89,47,177,229,80,165,183,77,178,139,3,241,165,196,239,93,98,101,99,210,99,5,135,132,151,197,4,154,87,130,145,175,243,238,132,0,146,14,6,205,227,78,150,59,191,223,74,145,57,82,30,223,90,205,248,47,195,220,215,167,112,216,20,130,26,88,170,101,26,14,216,62,169,217,1,135,193,147,218)), 143) +print(144) assert("d3418e6e6d0a96f8ea7c511273423651a63590c8" == hmac_sha1(string.char(61,175,211,89,77,91,143,76,18,30,252,16,181,45,211,37,207,204,174,174,60,208,36,85,23,106,141,215,62,8,158,98,209,49,96,143,129,99,11,159,79,116,98,244,51,237,227,250,73,196,36,216,181,157,159,87,248,108,29,22,181,175,72,92,160,127,46,204,202,175,61,108,172,50,225,48,84,167,116,67,183), string.char(174,62,218,81,85,89,4,35,26)), 144) +print(145) assert("f2fbe13b442bff3c09dcdaeaf375cb2f5214f821" == hmac_sha1(string.char(188,80,129,208,85,53,8,122,92,99,56,251,59,108,97,145,1,97,157,181,156,243,30,204,227,88,205,151), string.char(137,30,19,71,50,239,11,212,91,234,79,248,244,118,108,245,251,78,53,136,249,55,210,231,109,207,153,83,10,63,98,225,79,113,124,64,100,16,195,9,136,28,50,215,93,93,153,25,97,77,4,13,46,34,164,59,33,206,62,125,8,79,219,42,155,39,241,96,18)), 145) +print(146) assert("aeacb03e8284a01b93e51e483df58a9492035668" == hmac_sha1(string.char(62,67,97,44,112,68,107,91,105,4,183,154,9,14,164,180,87,140,195,231,62,49,250,154,193,186,219,18,100,156,176,124,223,164,68,64,105,214,144,200,65), string.char(221,20,36,192,59,234,90,174,232,184,75,15,92,229,59,191,36,51,4,190,226,35,237,189,21,100,135,138,171,202,163,227,176,231,72,185,43,197,134,157,44,86,26,42,230,251,2,124,220,245,242,116,77,35,197,154,237,73,117,131,126,242,167,242,8,72,163,171,60,29,77,152,142,69,25,234,195,2,159,49,178,63,48,56,131,143,165,22,142,116,109,11,57,71,101,99,84,204,56,233,173,65,180,228,211,10,197,204,26,70,198,81,217,83,130,70,79,54,93,96,116,32,21,219,193,70,55,84,161,89,47,206,13,167,46,104,205,218)), 146) +print(147) assert("e163c334e08bd3fa537c1ea309ce99cfbcd97930" == hmac_sha1(string.char(238,187,29,13,100,125,10,129,46,251,49,62,207,74,243,25,25,73,196,100,114,64,141,173,143,144,70,13,162,227,33,255,83,58,254,138,23,146,90,86,17,33,152,227,15,100,200,147,81,114,166,234,47,60,70,190,208,28,16,44,71,150,101,197,182,57,143,79,29,135,45,211,194,62,234,28,244,74,56,104,187,94,146,174,190,198,55,143,134,60,186,144,190,100,102,0,171,151), string.char(177,90,20,162,64,145,130,87,137,70,153,237,51,138,248,29,166,134,168,68,133,121,161,26,228,137,145,116,243,224,201,229,61,107,250,198,214,208,72,169,248,155,247,54,163,167,71,248,69,106,105,139,224,138,85,94,31,143,4,215,83,239,21,198,28,1,120,83,91,92,58,150,110,77,111,166,222,236,170,129,2,121,86,66,42,129,146,232,46,248,136,175,130,118,126,205,184,136,6,35,180,174,194,59,89,243,199,38,97,48,80,98,45,111)), 147) +print(148) assert("6a82b3a54ed409f612a934caf96e6c4799286f19" == hmac_sha1(string.char(11,59,108,100,113,56,239,211,2,138,219,101,50,50,111,31,171,212,228,55,89,196,44,158,77,252,212,134,106,7,250,223,219,46,231,87,9,62,213,104,169,49,1,75,3,10,50,238,5,150,47,14,47,45,171,183,40,245,194,249,228,216,85), string.char(97,131,91,189,9,52,88,203,250,245,46,96,7,95,176,61,57,192,91,231,193,32,41,169,96,101,39,240,29,143,24,56,57,177,70,135,225,90,42,192,134,103,239,175,243,246,76,137,14,237,82,215,7,76,246,219,132,50,175,25,164,4,216,65,102,0,114,216,159,80,58,32,4,100,22,35,71,140,49,16,216,182,91,80,181,129,151,55,130,7,77,84,211,111,45,51,192,123,213,192,11,81,138,60,225,21,37,182,180,223,170,163,110,9,47,214,200,249,164,98,215)), 148) +print(149) assert("79caa9946fcf0ccdd882bc409abe77d3470d28f7" == hmac_sha1(string.char(202,142,21,201,45,72,11,211,67,134,42,192,96,255,210,211,252,6,101,131,25,195,101,76,250,161,148,242,30,129,241,85,68,173,236,140,120,80,71,62,55,4,33,104,52,160,143,118,252,72), string.char(100,188,93,165,75,34,224,223,6,130,52,160,83,157,253,27,200,125,117,249,41,203,19,242,4,188,209,213,211,20,162,252,215,238,221,30,219,252,246,110,117,77,89,204,221,246,69,62,160,186,37,144,43,151,39,133,254,134,24,223)), 149) +print(150) assert("bd1e60a3df42e2687746766d7d67d57e262f3c9b" == hmac_sha1(string.char(57,212,172,224,206,230,13,50,80,54,19,87,147,166,245,67,118,37,101,214,207,60,66,29,197,236,146,140,192,15,36,155,108,121,215,211,210,118,22,20,56,245,42,153,150,32,224,201,171,2,238,41,70,140,73,60,215,243,86,20,169,152,220,104,95,215,187), string.char(245,123,43,109,29,108,197,27,55,167,255,177,226,205,111,126,146,75,93,70,180,90,139,218,243,232,221,247,129,77,115,101,144,42,13,249,223,127,200,213,140,113,23,10,149,107,244,86,41,133,147,26,167,85,189,76,176,12,190,52,90,54,78,29,97,204,161,224,59,243,128,64,133,95,192,19,137,220,96,228,97,161,51,21,157,172,127,76,53,38,83,126,178,26,203,206,165,241,101,130,79,122,75,29,205,240,1,68,222,48,101,7,29,6,82,240,133,112)), 150) +print(151) assert("fe249fa66eb1e6228e1e5166d7862d119ffa0dcf" == hmac_sha1(string.char(152,13,170,129,7,65,32,194,85,196,85,12,170,227,139,215,70,137,246,105,100,111,252,221,54,174,90,169,59,11,198,33,110,94,246,195,174,13,212,47,18,94,17,67,68,56,251,213,92,67,243,114,181,166,24,182,238,146,48,196,11,238,91,213,46,184,187,199,15,221,193,36,73,244,30,222,175,132,165,177,162,54,215,130,171,58,26,144,218,62,172,120,188,20,182,242,47,178,120,175), string.char(129,126,32,58,251,104,186,25,18,204,5,240,65,194,32,205,194,18,151,173,185,249,237,55,145,8,41,18,171,28,59,234,62,163,32,173,197,176,206,224,95,176,218,168,183,82,53,172,205,6,223,190,189,16,3,34,212,201,54,152,89,231,194,95,8,238,133,56,139,157,115,35,74,58,21,162,111,36,105,135,151,30,207,33,198,186,122,221,98,36,196,251,27)), 151) +print(152) assert("88aef9bb450b75bf96e8b5fd7831ed0d16d7fe7a" == hmac_sha1(string.char(162,225,9,204,87,95,132,152,211,133,93,120,105,156,131,55,245,224,33,115,182,10,28,49,80,175,241,67,66,52,21,55,174,108,66,100,77,20), string.char(55,61,250,187,157,98,212,245,5,54,170,60,235,228,197,182,230,24,195,215,202,55,56,153,9,94,164,107,67,93,143,1,235,195,133,201,103,57,130,177,71,73,169,80,21,251,130,247,21,61,228,39,166,173,211,203,149,55,11,224,70,52,248,118,112,219,39,188,147,5,203,101,158,134,53,250,103,73,154,249,71,181,53,171,227,26,79,200,145,178,24,80,102,26,90,101,70,143,233,17,150,181,170)), 152) +print(153) assert("a057dd823478540bbe9a6fcdf2d4dcfcac663545" == hmac_sha1(string.char(83,17,204,150,211,96,88,52,225,90,61,59,28,127,135), string.char(49,226,129,162,111,75,221,186,24,219,219,178,226,132,19,126,192,69,124,114,214,31,7,127,194,134,202,147,45,176,215,141,41,94,69,38,199,231,185,223,10,104,108,50,237,20,76,194,33,43,117,117,176,3,117,117,55,68,112,207,74,72,163)), 153) +print(154) assert("b11757ccfafc8feadc4e9402c820f4903f20032b" == hmac_sha1(string.char(34,11,53,219), string.char(225,194,251,106,223,229,212,105,164,172,25,25,224,199,225,172,197,42,167,1,227,165,17,247,75,250,10,208,99,124,254,158,103,74,89,60,78,141,201,44,253,87,248,239,74,86,75,64,249,189,21,170,249,18,139,21,130,226,66,200,231,35,227,147,95,213,133,35,47,236,52,64,122,115,80,170,27,50,182,151,180,106,120,85,103,255,143,27,233,43,217,152,70,82,8,229,103,42,153,38,130,184,108,160,199,69,38,184)), 154) +print(155) assert("bfd0994350b2e1e0d6a1faed059f67f1dd8b361f" == hmac_sha1(string.char(221,156,128,63,215,109,55,123,41,27,110,127,209,144,178,143,120,12,226,47,56,212,131,228,3,40,186,208,233,135,33,206,92,214,153,77,12,220,72,240,176,53,22,37,130,58,180,4,111,124,135,31,192,71,117,87,11,41,231,101,37,164,112,3,55,141,250,131,191,117,90,159,224,244,9,251,154), string.char(35,88,113,69,231,5,150,40,123,13,197,27,49,72,161,1,212,222,252,74,197,170,137,72,231,245,117,236,29,12,36,59,225,103,44,119,136,34,241,17,142,239,46,152,129,163,107,17,165,112,187,15,122,217,67,13,215,157,212,26,103,226,237,161,213,3,152,88,247,236,130,133,84,200,132,191,166,8,96,247,4,142,14,81,179,64,88,202,156,242,181,95,162,19,97,223,51,76,176,218,22,116,24,238,204,128,241,236,204,2,56,86,77,211,4,52,221,82,94,76,9,21,182,112,199,161,29,39,183,120,13,0,124,136,95,182,241,3,73,167,51,237,152,149,84,147,41,42,241,174,12,30,226,245,139,230,127,34,205,41,166,228,242,105,251,4,150,29,106,42,27,239,89,249,230,246,242,149,4,60,240,93,13,166,102,239,81,216,137,196)), 155) +print(156) assert("15c4527be05987a9b5c33b920be4a4402f7cf941" == hmac_sha1(string.char(132,158,227,135,167,32,19,192,144,48,232,68,79,78,135,122,136,140,97,67,34,91,225,48,126,67,77,115,154,189,13,45,8,234,59,233,245,77,34,76,95,78,3,250,179,224,20,25,6,202,214,115,165,230,85,115,250,236,135,34,32,158,196,45,207,61,71,51,93,27,187,232,175,233,147,68,231,110,12,198,233,165,24,209,206,25,16,252,127,72), string.char(210,162,121,23,181,21,75,54,110,47,15,166,133,206,100,217,57,133,228,32,112,208,186,28,140,8,207,74,185,17,128,29,181,172,20,156,64,192,112,8,207,131,53,10,182,147,224,71,218,94,71,86,215,2,133,46,160,27,51,189,38,130,224,120,185,144,253,117,193,24,154,229,247,132,62,103,65,163,106,172,22,5,2,32,129,38,213,118,110,125,156,14,48,240,170,225,158,89,92,190,254,231,51,220,8,174,188,233,226,106,224,99,218,82,64,219,206,75,166,111,25,7,87,248,145,251,109,106,243,241,89,200,85,184,155,183,249,61,70,87,115,182,81,80,155,24,245,123,184,102,214,255,122,83,61,214,88,235,169,28,147,196,247,97,28,82,193,72,71,96,243,176,35,189,236,44,184,52,151,249,186,189,150,184,142,238,88,113,120,68,234,50,136,104,80,145,179,57,6,186)), 156) +print(157) assert("b9f87f5f23583bdd21b1ea18e7e647513b7b0596" == hmac_sha1(string.char(216,79,247,98,215,219,128,232,135,111,179,168,80,76,36,250,224,157,229,209,238,186,212,188,200,52,208,200,210,79,182,186,22,22,37,34,86,107,89,238,168,90,53,30,151,191,89,163,253,24,204,152,118,1,158,25,168,2,123,91,111,39,101,239,247,37,200,51,215,31,146,211,163,136,208), string.char(153,134,211,81,255,219,50,91,67,48,102,63,244,170,129,66,3,151,110,167,142,150,153,95,89,90,23,87,12,85,42,209,197,36,41,189,199,136,196,159,125,53,253,192,135,234,94,34,87,79,143,213,237,149,212,170,231,181,22,72,81,233,151,243,134,71,160,155,121,32,233,251,187,179,139,48,254,206,172,165,202,105,222,31,223,95,203,87,114,52,218,59,187,41,43,135,200,108,102,201,85,199,8,176,249,44,96,104,160,85,149,177,25,88,55,231,85,120,227,6,180,24)), 157) +print(158) assert("0b586895674ab11d3b395c07ddb01151a6e562f5" == hmac_sha1(string.char(26,110,222,147,168,18,74,206,186,238,154,250,229,207,138,175,126,82,236,148,74,180,27,168,159,89,211,34,39,115,227,239,22,199,252,96,100,16,193,117,111,102,75,70,2,114,214,196,29,26,43,189,183,0,194,217,204), string.char(90,15,17,198,223,30,20,138,203,132,6,170,107,40,167,58,194,212,244,49,174,60,209,164,26,87,174,177,41,166,95,173,40,61,47,136,147,239,125,167,146,180,97,167,33,185,5,110,128,36,61,52,140,20,189,16,216,83,164,46,74,16,251,250,72,50,47,5,86,60,56,77,101,64,11,120,124,194,212,199,136,87,157,160,90,172,101,222,235,23,111,11,36,11,68,20,43,251,65,212,206,150,0,50,146,170,183,93,89,142,161,50,110,237,95,191,22,184,62,194,81)), 158) +print(159) assert("38b25a1d9d927ba5e6b294d2aa69c0ab8ab0a0c5" == hmac_sha1(string.char(93,218,103,160,235,71,107,150,18,170,193,9,63,245,77,150,71,242,137,52,243,12,207,183,222,252,20,215,210,194,241,79), string.char(87,97,35,201,134,196,180)), 159) +print(160) assert("a72ce76565c2876e78f86ce9e137a9881328fddc" == hmac_sha1(string.char(48,215,222,182,164,136,31,53,160,28,61,171,220,159,243,154,169,101,191,193,99,28,140,59,60,215,77,170,51,136,50,145,159,135,237,149,83,154,219,223,29,200,85,193,173,201,66,142,100,21,89,144,111,5,24,229,228,154,160,32,210,71,19,63,244,230,61,185,221,158,151,51), string.char(196,237,178,172,24,232,144,251,253,31,8,63,69,58,202,88,120,219,39,34,223,173,196,164,135,93,188,69,126,58,153,37,72,219,50,152,76,235,244,223,205,60,74,12,109,203,134,239,40,181,207,99,118,149,179,84,14,53,189,201,55,110,67,81,116,140,31,247,163,135,53,254,82,230,55,162,255,230,149,144,217,62,164,59,124,59,78,177,115,47,26,169,228,18,167,232,89,161,56,105,127,111,230,93,230,181,222,212,254,85,32,117,140,80,246,117,21,140,221,198,11,196,112,69,122,125,156)), 160) +print(161) assert("cd324faf8204be01296bea85a22d991533fd353e" == hmac_sha1(string.char(25,136,140,2,222,149,164,20,148,15,90,33,106,111,10,252,67,120,57,49,251,171,99,173,114,16,102,240,206,188,231,174,51,124,38,1,217,142,4,15,78,4,5,128,207,82), string.char(84,0,42,233,157,150,105,1,216,32,170,11,100,98,103,220,137,102,32,185,55,211,207,179,252,190,248,117,253,189,196,71,112,32,4,198,87,5,133,125,238,107,210,227,149,206,12,124,31,59,213,66,105,100,240,237,149,232,174,140,127,9,65,204,162,243,100,120,6,229,71,56,140,128,228,102,121,14,43,166,252,230,172,93,96,25,214,174,151,2,50,64,152,164,132,115,109,176,25,144,171,252,231,72)), 161) +print(162) assert("fb6e40ddbf3df49d4b44ccecf9e9bc74567df49e" == hmac_sha1(string.char(31,41,38,118,207,197,83,231,45,187,105,1,246,6,34,16,214,148,97,64,139,27,73,129,71,129,183,182,228,12,74,242,94,43,121,163,188,242,92,43,154,103,20,208,89,213,63,46,81,60,69,217,238,237,97,18), string.char(126,99,64,121,143,219,76,227,119,32,135,246,48,55,214,38,179,255,33,43,32,140,228,44,218,40,187,117,172,131,225,222,72,61,213,132,167,121,190,167,66,213,199,59,212,61,69,242,46,169,89,191,74,0,228,208,46,112,7,81,88,203,95,180,179,75,116,222,115,239,1,132,178,73,139,252,77,1,151,222,108,84,196,161,79,220,80,44,223,44,131,252,58,28,201,3,77,211,33,208,237,47,251,79,134,223,48,8,0,236,139,11,232,186,188,134,249,29,208,154,137,169,218,228,203,254,106,88,35,252,155,111,119,14,147,161,19,60,145,157,120,236,108,122,218,168,105,59,10,44,99,208,86)), 162) +print(163) assert("373f21bf8fe4e855f882cc976ebed31717f4c791" == hmac_sha1(string.char(198,41,45,129,159,48,37,183,170,144,24,223,108,176,68,60,197,245,254,165,24,152,14,25,243,46,6,25,142,99,64,10,145,229,74,232,162,201,72,215,116,26,179,127,107,45,193,232,96,170,168,153,79,47), string.char(79,250,224,177,254,56,111,120,135,79,55,200,199,71,65,132,222,7,106,170,26,179,235,237,47,172,88,28,244,88,137,177,92,178,147,129,147,85,88,157,30,207,235,246,132,98,232,18,201,57,151,90,174,148,126,22,38,118,133,49,83,156,56,195,10,95,180,250,215,220,251,5,131,243,70,2,162,239,197,153,196,28,181,167,26,14,247,86,244,69,190,100,255,158,217,222,58,116,126,245,240,139,255,213,7,28,222,99,12,127,57,2,212,33,136,220,203,251,87,205,213,160,146,162,73,55,112,203,60,243,139,167,45,91,68,62,204,245,206,221,52,253,5,193,69,19,190,131,64,250,12,127,77,212,53,83,102,212,11,215,253,143,111,201)), 163) +print(164) assert("df709285c8a1917aaa6d570bd1d4225ca916b110" == hmac_sha1(string.char(193,124,34,185,160,71,134,56,178,152,165,219,223,89,174,116,11,237), string.char(47,110,24,9,42,187,179,71,114,43,155,129,158,24,130,32,208,3,46,63,16,1,192,210,72,220,200,89,120,80,82,65,199,119,167,86,57,33,105,118,215,60,18,155,17,154,63,59,189,153,236,78,219,89,4,116,208,122,56,65,253,220,57,147,162,242,193,86,45,145,151,61,30,138,105,139,99,89)), 164) +print(165) assert("66be81c24c87feeecfd805872c6cde41cb1dd732" == hmac_sha1(string.char(128,186,31,231,128,48,206,237,59), string.char(56,228,42,58,98,45,202,132,30,78,80,52,36,128,7,55,209,148,181,52,185,220,137,85,111,128,220,109,55,70,185,242,253,155,165,193,240,63,144,253,240,147,18,161,7,46,40,148,201,0,127,1,33,145,180,247,90,206,178,96,62,137,130,99,248,248,227,135,213,21,230,40,88,162,106,240,218,93,226,108,9,121,173,70,255,106,137,222,135,206,104,153,171,1,52,156,166,27,98,7,74,180,206,11,193,129,77,194,86,224,175,79,77,95,134,62,58,252,180,100,191,53,28,59,121,123,146,107,121,249,168,51,204,170,141,26,84,126,38,77,203,11,58,123,155,167,60,176,151,7,39,75,217,254,32,110,79,123,188,133,158,178,132,198,169,23,5,104,124,44,206,191,239,139,152,110,207,42)), 165) +print(166) assert("2d1dd80c3f0fc323ca46ebd0f73c628caa03f88b" == hmac_sha1(string.char(142,87,50,47,146,180,251,164,35,75,53,50,101,115,90,97,66,12), string.char(174,244,41,143,173,27,117,79,53,73,0,189,83,91,13,71,117,85,96,32,199,168,244,72,218,249,207,107,244,1,113,203,160,156,89,244,132,5,4,220,254,112,77,156,158,105,180,98,40,99,198,236,134,209,13,237,93,220,177,70,97,76,178,106,60,60,92,248)), 166) +print(167) assert("444ef9725523ad1aac3d1c20f4954cdf1550d706" == hmac_sha1(string.char(242,169,63,243,133,158,118,205,250,154,66,127,178,170,214,241,96,22,173,138,6,189,90,45,108,70,236,108,93,58,54,204,85,241,194,55,55,184), string.char(30,52,200,164,245,80,75)), 167) +print(168) assert("2170bcfb79e4ab2164287a30ee26535681e34505" == hmac_sha1(string.char(121,111,11,221,34,216,169,179,73,247,222,122,96,168,61,168,201,230,139,158,110,154,74,83,118,254,237,255,99,212,46,218,83,69,185,10,249,78,46,76,206,206,137,133,118,57,241,114,228,54,51,68,71,224,188,188,0,119,173,94,120,1,25,13,237,4,181,170,222,92,106,28,9,111,128,137,228,2,110,4,137,171,219,70), string.char(108,95,232,156,70,235,149,211,52,84,86,25,251,127,119,231,109,144,54,177,96,118,213,3,8,119,95,192,187,221,115,83,67,39,207,82,215,85,156,198,179,246,177,202,115,103,79,97,222,133,113,201,27,92,185,48,227,223,142,96,143,181,175,238,115,112,29,30,126,59,109,252,136,153,28,112,50,138,155,249,223,114,93,107,122,114,163,226,53,219,44,15,172,233,76,98,175,107,246,181,249,112,230,173,152,211,183,120,227,165,187,10,240,94)), 168) +print(169) assert("2bcd1eb6df83aed8bcdfbb36474651f466b1fb22" == hmac_sha1(string.char(178,135,218,237,192,188,60,157,46,65,76,100,66,248,152,23,200,194,67,107,95,127,67,111,149,64,60,158,199,115,159,64,45,192,212,179,46,13,171,104,139,49,185,254,235,183,65,52,140,30,89), string.char(106,32,155,133,229,130,33,200,31,51,242)), 169) +print(170) assert("505910401632c487cd9482ecd6ad16928f21d6f4" == hmac_sha1(string.char(170,34,244,62,31,230,158,102,235,63,93,33,111,253,232,211,132,160,120,156,107), string.char(43,16,194,149,208,76,252,14,73,11,1,172,79,41,132,217,125,96,221,110,199,248,129,122,45,63,77,145,21,98,40,149,154,9,127,27,168,224,204,167,203,74,218,196,236,230,47,29,122,138,65,239,123,120,29,68,236,137,130,95,114,230,185,146,32,69,201,48,251,233,103,61,132,167,245,42,68,249,145,166,137,204,186)), 170) +print(171) assert("6d5f9ae7e8a51f2e615ae3aa8525a41b0bf77282" == hmac_sha1(string.char(166,8,119,187,177,166,23,183,147,37,177,162,102,16,93,204,247,119,248,179,23,89,48,145,188,37,197,176,238,218,173,6,216,229,224,108,58,78,40,199,9,241,191,154,88,124,237,142,228,7,235,102,142,123,4,124,38,46,170,229,116,243,104,106,26,163,94,2,118,71,173,171,104), string.char(41,12,123,197,199,117,129,177,132,149,175,83,168,79,76,58,236,204,99,121,155,207,228,89,236,154,103,137,183,221,208,93,22,222,28,237,140,21,25,149,188,111,6,85,251,31,73,79,239,246,66,46,215,157,184,5,207,4)), 171) +print(172) assert("8c20120cd131e07d9fa46467602d57c9017ca22d" == hmac_sha1(string.char(12,24,169,75,70,190,156,115,112,233,245,216,85,248,62,24,91,179,204,185,0,129,209,137,116,97,242,183,35,151,91,170,41,4,81,12,120,47,78,145,193,50,237,224,62,203,171,169,6,141,126,75,29,40), string.char(47,165,0,107,170,77,37,219,45,135,102,68,144,218,213,74,35,58,246,179,171,3,148,55,216,32,29,102,83,58,29,11,166,75,243,60,21,9,202,179,236,129,212,62,217,203,6,193,18,176,156,198,86,140,135,222,109,48,195,112,167,218,92,76,71,40,128,221,235,43,81,109,198,51,77,122,183,111,173,195,8,249,40,159,212,136,180,215,171,70,60,108,26,185,11,222,67,47,142,179,158,131,135,153,168,251,199,21,44,7,164,68,212,209,54,75,18,200,117,213,16,175,152,29,146,154,151,236,143,173,86,70,224,138,71,33,33,151,122,205,128,223,166,51,149,125,178,225,234,164,180)), 172) +print(173) assert("1ef2a94a2e620a164e07590f006c5a46b722fe7e" == hmac_sha1(string.char(78,109,133,245,198,165,112,63,135,53,124,251,110,183,41,34,153,68,22,253,66,201,9,41,15,99,105,14,252,172,212,39,100,14,128,16,64,47,104,37,33,27,203,120,163,182,52,37,27,224,212,40,134,81,70,29,155,101,246,141,238,41,253,124,222,216,49,49,85,46,51,51,122,39,221,156,104,7,14,52,71,93,31,219), string.char(191,94,78,159,209,12,118,97,214,190,238,108,175,252,192,244,8,104,145,30,236,242,45,49,215,185,251,73,46,101,156,221,136,245,45,240,138,174,187,151,0,122,113,154,195,94,245,186,218,211,203,189,37,251,21,33,225,66,168,152,102,200,245,101,102,217,193,215,194,214,48,131,153,140,75,100,237,240,32,89,113,17,125,177,189,188,212,101,211,212,102,57,251,213,216,14,86,204,198,147,122,97,203,127,100,107,126,107,116,34,220,122,154,130,135,199,9,243,9,194,214)), 173) +print(174) assert("ad924bedf84061c998029fb2f168877f0b3939bb" == hmac_sha1(string.char(174,37,249,22,190,230,152,74,215,14,1,180,201,164,238,160,59,157,213,35,36,43,116,160,158,144,131,30,213,232,29,183,205,87,35,85,252,120,126,5,54,113,176), string.char(25,233,197,224,170,150,207,83,58,255,63,236,20,13,179,143,48,248,212,16,220,143,14,123,207,59,28,124,19)), 174) +print(175) assert("75a6cfbedde0f1b196209a282b25f5f8b9fef3d1" == hmac_sha1(string.char(131,192,35,70,146,214,224,190,220,240,195,81,129,33,207,253,47,230,111,169,187,136,52,244,217,178,143,140,225,154,105,95,112,201,136,90,221,255,44,31,48,178,90,178,218,122,228,165,90,189,107,45,217,249,89,213,136,139,91,187,202,204,26,250,112), string.char(41,13,202,37,1,104,116,166,245,50,221,59,115,68,187,115,80,93,202,147,10,96,209,213,76,103,143,237,217,21,124,152,82,54,147,207,164,43,89,83,118,67,43,179,79,64,127,252,9,26,125,101,80,192,111,224,104,243,45,67,54,251,238,146,154,212,193,181,138,168,231,171,203,62,93,97,46,55,109,253,214,79,60,62,126,183,235,63,121,46,99,3,223,174,223,2,208,78,7,15,161,56,160,59,205,122,138,77,47,250,107,136,65,199,98,86,131,217,96,226,11,166,185,131,231,76,28,83,140,7,146,87,158,20,102,224,86,5,232,96,34,129,172,236,146,1,139,63,252,1,48,196,242,41,145,97,254,174,88,107,169,70,158,165,246,160,4,16,212,109,236)), 175) +print(176) assert("c6f51cc53b0a341dafa4607ee834cffaa8c7c2a2" == hmac_sha1(string.char(85,193,15,80,156,124,171,95,40,75,229,181,147,237,153,83,232,67,210,131,57,69,7,226,226,195,164,46,185,83,120,177,67,77,119,201,9,123,117,111,148,176,12,145,14,80,225,95,71,136,179,94,136,6,78,230,149,135,176,131,19,125,185,48,200,244,111,8,28,170,82,121,242,200,208,145,73,59,234,19,87,61,79,137,245,148,212,94,96,253,227,60,162,170,49,102), string.char(200,235,229,185,225,227,209,209,199,11,112,58,19,202,246,237,94,60,90,176,145,87,191,138,171,88,242,239,21,146,3,92,255,188,99,146,74,134,252,19,201,111,239,76,249,231,71,109,230,240,192,234,253,52,58,173,153,24,131,161,97,222,0,203,177,179,186,160,46,206,73,176,154,39,248,22,66,164,232,120,188,5,73,245,68,8,40,157,155,93,50,207)), 176) +print(177) assert("a07ef6f8799ef46062ffea5a43bb3edd30c1fdde" == hmac_sha1(string.char(81,90,37,124,211,22,19,34,50,56,90,45,17,230,233,38,76,24,250), string.char(142,185,46,191,103,207,110,48,71,10,217,115,193,52,131,87,106,39,9,80,216,15,235,169,30,143,3,7,188,78,34,136,18,200,181,140,22,253,141,59,235,25,59,204,147,80,162,108,237,101,167,249,7,168,24,123,215,220,198,38,133,253,72,168,10,139,84,22,195,36,241,128,70,132,28,242,56,40,159,113,184,187,89,38,198,255,176,88,112,125,68,12,57,207,3,251,72,198,135,48,118,244,199,192,205,164,181,243,40,33,195,204,9,78,108,253,114,211,173,121,245,104,13,116,143,160,241,139,210,162,78,62,69,232,40,248,254,177,6,148,177,168,51,253,177,84,152,29,107,91,186,68,118,30,125,62,196,24,14,87,158,83,39,240,192,79,78,61,133,109,106,98,120,232,144,18,49,96,17,202,208,189,152,171,219,19,41,132,179,219,83,220,201,36,191)), 177) +print(178) assert("63585793821f635534879a8576194f385f4a551a" == hmac_sha1(string.char(78,213,189,105,102,116,246,215), string.char(162,53,102,158,78,125,120,189,186,214,237,16,219,220,44,24,30,62,32,177,104,217,225,27,92,128,95,202,50,177,239,152,151,161,31,152,93,108,29,125,23,63,55,243,160,169,99,102,15,188,196,129,217,239,132,180,177,251,132,173,62,172,21,139,16,137,231,6,243,185,218,60,60,156,166,153,99,149,144,192,151,249,141,165,222,254,4,111,58,46,106,78,160,215,49,128,252,4,114,236,132,108,114,189,143,45,42,84,134,81,47,140,247,146,247,179,63,14,92,136,120,139)), 178) +print(179) assert("95bb1229d26df63839ba4846a41505d48ff98290" == hmac_sha1(string.char(121,190,21,20,39,101,53,249,44,132,19,132,4,114,199,22,32,143,38,33,184,181,66,55,183,240,31,204,225,230,125,186,87,25,90,229,11,55,239,62,101,67,238,1,104,178,191,144,148,105,6,26,127,154,135,247,248,171,41,44,57,83,186,220,42,40,68,153,189,221), string.char(99,150,114,100,37,53,229,202,20,171,246,91,188,188,46,232,70,26,151,35,223,28,97,132,9,242,55,238,98,75,108,91,226,48,236,209,133,92,68,194,241,199,188,86,59,255,230,128,252,193,94,113,134,27,32,50,191,176,159,185,89,209,255,192,9,214,252,63,147,8,92,162,114,72,50,101,136,180,95,39,55,143,137,118,40,14,104,3,149,153,230,135,98,93,72,180,72,86,185,80,107,69,68,20,73)), 179) +print(180) assert("9e7c9a258a32e6b3667549cef7a61f307f49b4b9" == hmac_sha1(string.char(105,200,69,23,156,169,151,107,30,139,196,145,128,108,52,7,181,248,255,83,176,169,152,51,158,236,49,191,65,208,155,204,105,179,148,226,142,194,63,253,217,184,69,11,135,185,203,150,146,57,129,80,87,84,231,168,63,186,149,85,128,236,135,217,246,255,121,70,251,68,139,215,199,143,87,13,196,20,50,92,55,116,34,141,117,89,228,94,63,142,250,182,58,91,68,214), string.char(38,52,86,63,109,91,84,11,33,250,194,192,178,94,37,17,65,111,173,17,37,54,163,168,142,91,191,197,161,229,44,163,163,125,81,204,167,185,244,119,75,28,211,87,159)), 180) +print(181) assert("7fb01c88e7e519265c18e714efd38bc66f831071" == hmac_sha1(string.char(138,214,212,108,83,152,165,123), string.char(148,223,186,162,237,166,230,68,59,78,220,164,231,42,46,211,239,38,39,150,174,50,58,4,0,135,87,123,123,5,33,252,197,6,53,83,27,178,189,228,166,252,91,141,67,44,68,106,222,17,46,84,214,252,110,181,216,132,14,246,209,57,183,155,238,64,215,121,8,169,157,175,182,191,169,94,116,105,91,49,203,30,155,202,39,140,146,6,115,162,112,130,175,86,143,128,126,249,148,185,39,174,63,83,19,76,164,34,228,97,198,250,65,3,168,158,61,130,167,166,144,185,146,184,160,39,189,11,243,125,255,60,217,46)), 181) +print(182) assert("15ab3da2a97592c5f2e22586b4c8a8653411e756" == hmac_sha1(string.char(6,242,91,101,37,118,105,53,170,56,114,144,117,49,53,203,169,216,9,232,9,170,204,129,82,41,210,45,86,176,139,80,152,168,1,154,32,111,89,165,143,205,21,236,105,62,231,106,232,205,7,189,245,52,145,100,78,140,200,183,209,232,196,88,109,175,70,153,150,231,252,7,189,119,97,176,216,201,186,245,24,128,33,57,113,188,184,0,146,248,69,255,77,144,101), string.char(186,246,72,178,189,127,22,42,22,217,81,156,253,142,152,86,42,41,164,77,150,11,208,155,154,185,18,81,156,185,140,224,215,98,1,97,33,194,137,206,144,219,207,210,52,203,198,1,109,182,65,138,122,227,168,207,213,110,206,102,12,181,198,195,133,218,23,187,117,190,56,140,154,239,105,62,96,148,126,187,47,24,139,194,18,211,225,252,195,49,102,138,165,160,90,153,100,140,105,154,242,59,133,226,19,68,125,59,83,140,48,240,226,129,151,79,130,59,96,111,27,73,198)), 182) +print(183) assert("7b06792805f4e8062ee9dfbbaffccd787c6f98e1" == hmac_sha1(string.char(83,132,152,16,245,136,91,241,37,158,80,92,65,223,79,3,189,213,89,253,159,93,194,52,218), string.char(212,242,37,98,229,190,238,23,210,197,147,253,82,105,146,168,198,253,2,160,165,188,165,221,179,112,136,72,149,79,138,70,101,95,210,73,157,14,211,92,27,230,177,84,170,183,15,40,0,92,222,252,166,48,1,139,40,16,186,178,231,109,100,125,253,125,123,94,115,94,150,157,186,230,115,163,194,164,30,131,162,90,28,61,44,248,137,8,246,173,31,177,236,39,8,10,192,148,211,36,215,57)), 183) +print(184) assert("a28fe22ba0845ae3d57273febbbf1d13e8fde928" == hmac_sha1(string.char(149), string.char(11,139,249,120,214,252,67,75)), 184) +print(185) assert("87a9daa85402f6c4b02f1e9fd6edd7e5d178bb88" == hmac_sha1(string.char(114,92,75,169,228,153,182,206,125,139,162,232,77,38,72,129,132,81,11,153,91,92,152,253,10,79,138,142,17,191,161,194,147,213,43,214,39,32,146,46,132,40,77,192,82,54,55,239,50,47), string.char(114,8,110,153,77,155,178,113,203,30,21,41,136,111,176,255,74,214,117,12,189,194,198,37,73,152,196,108,207,188,187,31,227,237,17,1,66,200,73,83,31,61,161,61,31,149,1,152,140,202,163,175,140,136,51,59,121,28,125,192,238,96,192,65,33,177,136,135,173,13,52,101,42,55,189,201,157,244,104,235,148,114,84,154,10,99,153,41,208,213,41,83,193,129,216,46,4,226,81,184,1,249,70,93,183,173,9,29,57,75,209,67,227,49,253,132,129,13,157,55,66,191,136,67,95,108,155,116,17,125,151,217,242,204,110,143,189,201,118)), 185) +print(186) assert("6300b2f6236b83de0c84ff1816d246231a5d3b79" == hmac_sha1(string.char(215,10,19,7,73,236,63,23,34,49,11,240,186,195,32,171,67,85,7,95,190,237,77,216,115,36,230,204,139,95,229,37,201,115,81,96,217,205,49,22,169,32,90,2,90,93,137,247,83,229,39,88,235,175,191,246,80,40,228,114,188,115,14,252,35,40,130,240,20,25,202,80,184,27,227,175,149,64,110,223,25,64,57,189,153,176,143,30,76,2,68), string.char(13,245,250,139,223,128,3,6,189,59,12,139,38,153,215,76,250,198,91,117,242,118,34,26,133,244,143,5,56,182,213,167,144,71,250,40,202,81,22,14,72,201,143,10,177,4,166,75,160,156,100,5,147,138,104,98,9,125,81,202,44,120,241,221,181,0,169,155,96,1,4,205,145,105,150,122,135,231,42,165,179,83,39,122,159,193,139,28,4,201,178,41,168,11,177,61,39,14,196,60,29,131,201,91,46,35,128,63,167,48,132,134,105,224,246,244,81,144,209,123,222,190,167,138,76,194,42,152,23,125,52,156,18,217,101,207,239,63,235,3,3,49,147,172,158,97,179,86,233,85,155,245,206,205,165,195,180,223,20,62,197,143,149,126,152)), 186) +print(187) assert("35fe4e0cf2417a783d5bdbc17bbc0ab77d2699e7" == hmac_sha1(string.char(15,157,117,128,204,42,12,183,229,129,132,234,228,150,235,100,100,77,160,26,154,230,246,138,150,120,252,20,188,138,171,189,208,62,201,58,158,152,167,165,98,249,52,143,7,26,109,227,144,81,213,157,31,155,26,12,206,103,193,29,142,126,205,123,83,111,179,166,31,32,183,183,100,54,51,205,180,186,160,220,176,233,42,94,62,241,88,74,31,24,22,115,179,124,136,206,78,83), string.char(248,157,191,249,76,86,187,243,10,116,1,227,141,21,104,154,167,72,30,245,6,57,40,170,253,15,4,192,237,237,142,141,196,243,231,245,121,219,150,237,212,118,78,25,201,249,174,21,76,98,187,155,82,243,218,142,239,101,235,240,134,70,212,205,136,165,170,71,121,137,44,8,110,14,167,162,233,92,51,191,178,227,85,134,168,222,121,62,35,127,57,92,80,142,91,112,9,248,129,127,236,2,190,165,125,236,188,117,158,241,181,134,54,133,16,64,100,156,59,83,249,105,235,187,160,186,158,173,52,25,129,20,156,209,102,170,209,10,3,233,12,228,205,138,16,140,108,74,75,65,238,155,251,129,73,236,118,93,202,217,46,180,165,27,84,120,61,186,173,148,131,161,187,30,175,21,249,12,245,145,89,93,61,151,254,29,247,51,198,238,111,52,30,254)), 187) +print(188) assert("83c2a380e55ecbc6190b9817bb291a00c36de5e8" == hmac_sha1(string.char(61,128,92,30,94,101,187,67,197,177,98,100,191,221,190,196,86,62,8,238,225,3,93,127,28,126,217,206,56,192,90,9,74,34,43,228,190,162,57,99,170,238,230,196,99,213,31,6,184,11,79,82,100,68,255,40,63), string.char(78,173,248,128,215,226,77,57,233,135,163,11,241,74,59,78,169,219,125,37,182,56,139,89,32,52,80,165,233,52,179,54,216,45,205,209,173,33,12,148,231,66,126,155,191,91,155,174,23,15,14,28,77,186,139,113,48,141,153,177,252,194,23,61,181,189,66,151,45,11,119,74,236,107,206,244,41,117,161,182,233,138,57,196,90,118,28,99,186,150,54,180,105,230,2,41,246,19,80,211,173,102,72,78,82,253,5,183,248,101,173,204,2,145,105,4,160,44,110,211,63,45,46,78)), 188) +print(189) assert("bd59d0dcf812107a613a2d892322f532c16ec210" == hmac_sha1(string.char(161,183,101,160,169,208,82,58,88,198,190,85,53,240,56,6,20,136,86,18,216,63,190), string.char(39,227,142,194,37,34,121,168,239,99,70,39,179,128,26,113,47,95,70,129,255,219,63,233,44,33,19,127,22,24,208,50,19,141,185,19,54,82,147,138,44,69)), 189) +print(190) assert("6aa92cb64b2e390fbf04dc7edfe3af068109ffba" == hmac_sha1(string.char(202,230,55,100,136,80,156,240,98,73,97,72,191,195,185,105,150,97,148,33,167,140,212,100,247,154,5,152,117,24,128,221,150,49,143,86,12,211,161,75,177,75,145,46,112,143,37,181,84,50,230,81), string.char(131,200,0,161,117,217,5,10,64,79,178,147,64,248,35,147,135,103,157,181,194,123,76,13,150,118,204,63,32,203,155,222,227,148,31,28,218,83,245,17,208,200,104,78,85,31,114,194,67,159,184,167,42,199,241,6,182,195,3,79,22,236,124)), 190) +print(191) assert("a42cf08fa6d8ad6ae00251aeed0357dca80645c7" == hmac_sha1(string.char(170,52,245,172,122,225,164,248,23,35,54,243,118,35,165,16,6), string.char(245,115,59,162,179,146,39,213,64,240,80,108,190,127,116,16,154,211,33,171,150,46,77,213,99,88,160,101,236,202,70,26,61,83,79,110,127,74,97,34,18,243,140,34,244,249,225,88,132,102,7,99,220,140,46,111,96,48,93,44,47,186,86,246,244,127,236,150,35,69,111,238,54,24,71,157,254,21,2,194,69,47,190,87,72,235,60,243,40,153,111,55,220,25,91,17,156,195,57,207,107,145,6,82,104,232,209)), 191) +print(192) assert("8dd62dcf68ef2d193005921161341839b8cac487" == hmac_sha1(string.char(136,5,195,241,18,208,11,46,252,182,96,183,164,87,248,13), string.char(78,49,162,9,47,79,26,139,66,194,248,90,171,207,167,43,201,223,102,192,49,145,83,42,1,52,250,116,216,133,224,224,17,143,41,153,65,214,231,109,160,5,31,85,17,186,199,226,214,92,21,194,148,98,68,209,50,74,26,150,97,59,223,8,131,173,145,6,31,126,248,86,226,2,94,15,3,202,239,99,177,237,61,99,14,253,26,246,0,151,2,7,159,40,249,166)), 192) +print(193) assert("eff66e7a69ae7d2a90b2d80c64ce7c41fe560a19" == hmac_sha1(string.char(123,54,89,205,103,116,21,97,164,41,23,224,151,195,22,196,90,52,253,92), string.char(141,12,168,171,49,30,165,57,107,214,157,224,36,163,182,184,103,186,133,22,2,215,52,220,95,205,231,180,221,139,67,184,21,46,172,95,183,220,31,27,227,122,183,196,114,206,132,162,51,164,84,212,172,63,218,236,127,215,218,77,119,105,14,19,164,53,149,11,13,1,29,246,69,200,208,239,154,60,245,31,172,82,158,165,197,250,151,83,12,156,215,201,66,45,238,228,77,125,157,35,235,162,78,220,132,14,137,168,238,127,10,24,41,132,181,43,95,73,33,32,129,149,145,55,80,156,171,73,90,183,166,182,163,154,31,104,14,56,59,102,72,210,121,191,251,239,1,5,76,116,158,89,40,69,220,107,23,168,106,93,92,95,21,91,118,118,71,29,64,142,84,15,153,193,47,214,30,117,236,217)), 193) +print(194) assert("0f1adc518afcca2ed7ad0adaaedd544835ddb76e" == hmac_sha1(string.char(142,36,199,16,52,54,62,146,153,25,162,85,251,247), string.char(110,70,53,237,120,53,50,196,24,82,199,210,156,178,110,124,7,24,229,89,39,1,10,96,74,211,36,79,166,4,33,238,64,220,239,129,48,232,244,172,142,79,154,73,36,107,182,208,191,25,201,19,233,23,60,236,180,76,116,53,232,181,101,62,160,252,213,171,166,113,193,73,75,13,209,63,76,92,95,176,119,0,125,174,42,138,77,23,212,81,180,10,193,118,244,242,163,246,206,150,162,92,20,208,51,71,254,197,11,222,203,153,208,251,243,193,124,141,215,171,55,244,230,78,158,32,223,138,156,43,98,39,191,108,111,6,117,130,195,100,221,233,17,98,180,49,132,139,171,20,180,7,206,35,54,17,48,253,130,185,249,2,52,17,207,73,24,33)), 194) +print(195) assert("5e87f8d9a653312fd7b070a33a5d9e67b28b9a84" == hmac_sha1(string.char(255,237,33,255,4,170,95,90,88,77,217,10,94,118,134,20,33,208,17,136,77,18,169,205,112,36,203,231,67,47,186,91,7,17,29), string.char(97,211,98,53,135,238,42,101,144,12,75,185,119,223,114,81,201,94,21,118,16,152,74,215,56,61,131,151,83,96,83,59,49,199,255,207,153,90,231,39,181,4,59,9,2,150,207,124,209,120,37,236,195,66,189,209,137,9,165,78,172,142,209,243,237,145,204,210,222,153,52,127,39,174,133,17,186,219,87,142,209,212,223,60,249,57,244,251,44,254,73,238,52,99,94,237,189,185,237,1,101,166,220,170,103,209)), 195) +print(196) assert("837c7cc92e0d2c725b1d1d2f02ef787be37896fa" == hmac_sha1(string.char(150,241,64,89,79,72,88,157,113,139,190,43,211,214,202,52,124,91,111,198,47,161,120,17,120,157,112,248,245,154,254,25,233,96,216), string.char(214,251,73,193,181,158,253,28,23,48,186,168,162,26,124,103,22,195,165,63,189,196,162,229,69,1,208,85,249,24,73,101,243,149,68,236,124,147,108,67,37,75,202,143,57,124,71,176,27,216,208,183,164,173,219,170,79,10,198,1,232,35,9,19,70,131,211,60,37,94,146,98,92,31,150,95,89,189,46,38,156,118,171,137,86,87,32,173,77,14,135,233,233,114,162,136,57,113,73,115,134,87,184)), 196) +print(197) assert("64534553f76e55b890101380ad9149d3cacb5b76" == hmac_sha1(string.char(231,24,192,54,17,14,98,122,119,170,71,54,206,207,223,109,164,181,124,186,122,111,122,34,157,24,237,130,207,206,117,76,183,162,174,124,60,60,77,114,139,170,194,143,232,74,160,141,161,171,51,178,162,159,234,208,55), string.char(141,184,241,185,112,45,53,192,72,152,139,145,146,149,145,151,12,140,111,79,158,172,212,175,75,225,127,4,252,104,5,94,105,24,141,183,79,208,240,227,63,126,213,4,13,109,216,244,237,24,196,52,212,245,147,109,18,63,69,107,122,245,101,67,212,232,92,25,135,67,127,247,187,143,224,198,105,38,147,12,238,6,57,53,105,246,0,127,222,69,163,44,11,245,82,220,32,108,33,70,93,177,89,75,192,214,104,236,84,28,221,209,27,12,80,239,111,249,57,39,236,193,17,69,66,180,63,193,242,248,72,22,225,56,198,235,111,102,33,171,159,124,220,143,108,10,71,186,219,213,28,209,145,188,36,70,59,9,235,154,57,12,211,146,6,87,83)), 197) +print(198) assert("b7efbbc21ac1a746e22368e814ef5921056331ac" == hmac_sha1(string.char(137,153,151,252,88,36,165,92,194,50,19,117), string.char(155,113,35,47,22,52,144,77,130,20,178,133,75,207,168,146,132,209,160,7,123,190,117,196,147,212,142,25,182,222,56,249,192,228,6,224,250,221,89,7,176,27,37,49,215,192,74,132,127,101,32,23,34,131,23,74,37,226,208,205,162,242,102)), 198) +print(199) assert("950ad3222f4917f868d09feab237a909fb6d50b7" == hmac_sha1(string.char(78,46,85,132,231,4,243,255,22,45,240,155,151,119,94,213,50,111,10,83,40,204,49,52,17,69,132,44,213,83,54,251,211,159,123,55,17,58,162,170,210,3,35,237,165,181,217,27,7,249,158,22,158,207,77,121,37,63,37,39,204,68,99,158,78,175,73,183,47,99,134,65,74,234,154,33,14,117,126,98,167,242,106,112,145,82), string.char(144,133,184,16,9,8,227,98,190,60,141,255,87,69,63,214,12,67,14,206,32,120,59,232,176,82,32,194,115,52,148,143,126,86,82,101,167,249,17,169,9,105,228)), 199) + +skynet.start(skynet.exit) \ No newline at end of file diff --git a/test/testsm.lua b/test/testsm.lua new file mode 100644 index 00000000..bcc369d1 --- /dev/null +++ b/test/testsm.lua @@ -0,0 +1,48 @@ +local skynet = require "skynet" +local sharemap = require "skynet.sharemap" + +local mode = ... + +if mode == "slave" then +--slave + +local function dump(reader) + reader:update() + print("x=", reader.x) + print("y=", reader.y) + print("s=", reader.s) +end + +skynet.start(function() + local reader + skynet.dispatch("lua", function(_,_,cmd,...) + if cmd == "init" then + reader = sharemap.reader(...) + else + assert(cmd == "ping") + dump(reader) + end + skynet.ret() + end) +end) + +else +-- master +skynet.start(function() + -- register share type schema + sharemap.register("./test/sharemap.sp") + local slave = skynet.newservice(SERVICE_NAME, "slave") + local writer = sharemap.writer("foobar", { x=0,y=0,s="hello" }) + skynet.call(slave, "lua", "init", "foobar", writer:copy()) + writer.x = 1 + writer:commit() + skynet.call(slave, "lua", "ping") + writer.y = 2 + writer:commit() + skynet.call(slave, "lua", "ping") + writer.s = "world" + writer:commit() + skynet.call(slave, "lua", "ping") +end) + +end \ No newline at end of file diff --git a/test/testsocket.lua b/test/testsocket.lua index c465079c..179df1a8 100644 --- a/test/testsocket.lua +++ b/test/testsocket.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local mode , id = ... @@ -7,9 +7,9 @@ local function echo(id) socket.start(id) while true do - local str = socket.readline(id,"\n") + local str = socket.read(id) if str then - socket.write(id, str .. "\n") + socket.write(id, str) else socket.close(id) return @@ -30,7 +30,7 @@ else local function accept(id) socket.start(id) socket.write(id, "Hello Skynet\n") - skynet.newservice("testsocket", "agent", id) + skynet.newservice(SERVICE_NAME, "agent", id) -- notice: Some data on this connection(id) may lost before new service start. -- So, be careful when you want to use start / abandon / start . socket.abandon(id) diff --git a/test/teststm.lua b/test/teststm.lua new file mode 100644 index 00000000..1d834b35 --- /dev/null +++ b/test/teststm.lua @@ -0,0 +1,36 @@ +local skynet = require "skynet" +local stm = require "skynet.stm" + +local mode = ... + +if mode == "slave" then + +skynet.start(function() + skynet.dispatch("lua", function (_,_, obj) + local obj = stm.newcopy(obj) + print("read:", obj(skynet.unpack)) + skynet.ret() + skynet.error("sleep and read") + for i=1,10 do + skynet.sleep(10) + print("read:", obj(skynet.unpack)) + end + skynet.exit() + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + local obj = stm.new(skynet.pack(1,2,3,4,5)) + local copy = stm.copy(obj) + skynet.call(slave, "lua", copy) + for i=1,5 do + skynet.sleep(20) + print("write", i) + obj(skynet.pack("hello world", i)) + end + skynet.exit() +end) +end diff --git a/test/testterm.lua b/test/testterm.lua new file mode 100644 index 00000000..6b341ab1 --- /dev/null +++ b/test/testterm.lua @@ -0,0 +1,15 @@ +local skynet = require "skynet" + +local function term() + skynet.error("Sleep one second, and term the call to UNEXIST") + skynet.sleep(100) + local self = skynet.self() + skynet.send(skynet.self(), "debug", "TERM", "UNEXIST") +end + +skynet.start(function() + skynet.fork(term) + skynet.error("call an unexist named service UNEXIST, may block") + pcall(skynet.call, "UNEXIST", "lua", "test") + skynet.error("unblock the unexisted service call") +end) diff --git a/test/testudp.lua b/test/testudp.lua new file mode 100644 index 00000000..e62f4067 --- /dev/null +++ b/test/testudp.lua @@ -0,0 +1,25 @@ +local skynet = require "skynet" +local socket = require "skynet.socket" + +local function server() + local host + host = socket.udp(function(str, from) + print("server recv", str, socket.udp_address(from)) + socket.sendto(host, from, "OK " .. str) + end , "127.0.0.1", 8765) -- bind an address +end + +local function client() + local c = socket.udp(function(str, from) + print("client recv", str, socket.udp_address(from)) + end) + socket.udp_connect(c, "127.0.0.1", 8765) + for i=1,20 do + socket.write(c, "hello " .. i) -- write to the address by udp_connect binding + end +end + +skynet.start(function() + skynet.fork(server) + skynet.fork(client) +end) diff --git a/test/time.lua b/test/time.lua new file mode 100644 index 00000000..be02805c --- /dev/null +++ b/test/time.lua @@ -0,0 +1,22 @@ +local skynet = require "skynet" +skynet.start(function() + print(skynet.starttime()) + print(skynet.now()) + + skynet.timeout(1, function() + print("in 1", skynet.now()) + end) + skynet.timeout(2, function() + print("in 2", skynet.now()) + end) + skynet.timeout(3, function() + print("in 3", skynet.now()) + end) + + skynet.timeout(4, function() + print("in 4", skynet.now()) + end) + skynet.timeout(100, function() + print("in 100", skynet.now()) + end) +end)