** join the elements of the ktable from pattern 'p1' into the ktable of ** the pattern at the top of the stack ('p'). If 'p1' has no elements, ** 'p' keeps its original ktable. If 'p' has no elements, it shares ** 'p1' ktable. Otherwise, this function creates a new ktable for 'p'. ** Return the offset of original 'p' elements in the new ktable. */
| 964 | ** Return the offset of original 'p' elements in the new ktable. |
| 965 | */ |
| 966 | static int jointable (lua_State *L, int p1) { |
| 967 | int n, n1, i; |
| 968 | lua_getfenv(L, p1); |
| 969 | n1 = ktablelen(L, -1); /* number of elements in p1's env */ |
| 970 | lua_getfenv(L, -2); |
| 971 | if (n1 == 0 || lua_equal(L, -2, -1)) { |
| 972 | lua_pop(L, 2); |
| 973 | return 0; /* no need to change anything */ |
| 974 | } |
| 975 | n = ktablelen(L, -1); /* number of elements in p's env */ |
| 976 | if (n == 0) { |
| 977 | lua_pop(L, 1); /* removes p env */ |
| 978 | lua_setfenv(L, -2); /* p now shares p1's env */ |
| 979 | return 0; /* no need to correct anything */ |
| 980 | } |
| 981 | lua_createtable(L, n + n1, 0); |
| 982 | /* stack: p; p1 env; p env; new p env */ |
| 983 | for (i = 1; i <= n; i++) { |
| 984 | lua_rawgeti(L, -2, i); |
| 985 | lua_rawseti(L, -2, i); |
| 986 | } |
| 987 | for (i = 1; i <= n1; i++) { |
| 988 | lua_rawgeti(L, -3, i); |
| 989 | lua_rawseti(L, -2, n + i); |
| 990 | } |
| 991 | lua_setfenv(L, -4); /* new table becomes p env */ |
| 992 | lua_pop(L, 2); /* remove p1 env and old p env */ |
| 993 | return n; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | #define copypatt(p1,p2,sz) memcpy(p1, p2, (sz) * sizeof(Instruction)); |
no test coverage detected