Skip to content

Commit 5b7d998

Browse files
committed
External strings are as good as internal ones
A '__mode' metafield and an "n" key both can be external strings.
1 parent 81f4def commit 5b7d998

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

lgc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,10 @@ static void traversestrongtable (global_State *g, Table *h) {
594594
*/
595595
static int getmode (global_State *g, Table *h) {
596596
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
597-
if (mode == NULL || !ttisshrstring(mode))
598-
return 0; /* ignore non-(short)string modes */
597+
if (mode == NULL || !ttisstring(mode))
598+
return 0; /* ignore non-string modes */
599599
else {
600-
const char *smode = getshrstr(tsvalue(mode));
600+
const char *smode = getstr(tsvalue(mode));
601601
const char *weakkey = strchr(smode, 'k');
602602
const char *weakvalue = strchr(smode, 'v');
603603
return ((weakkey != NULL) << 1) | (weakvalue != NULL);

ltm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) {
287287
return;
288288
}
289289
}
290-
else if (ttisshrstring(rc)) { /* short-string value? */
290+
else if (ttisstring(rc)) { /* string value? */
291291
size_t len;
292292
const char *s = getlstr(tsvalue(rc), len);
293293
if (len == 1 && s[0] == 'n') { /* key is "n"? */

lvm.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
657657
#define tostring(L,o) \
658658
(ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
659659

660+
/*
661+
** Check whether object is a short empty string to optimize concatenation.
662+
** (External strings can be empty too; they will be concatenated like
663+
** non-empty ones.)
664+
*/
660665
#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
661666

662667
/* copy strings in stack from top - n up to top - 1 to buffer */
@@ -691,8 +696,8 @@ void luaV_concat (lua_State *L, int total) {
691696
setobjs2s(L, top - 2, top - 1); /* result is second op. */
692697
}
693698
else {
694-
/* at least two non-empty string values; get as many as possible */
695-
size_t tl = tsslen(tsvalue(s2v(top - 1)));
699+
/* at least two string values; get as many as possible */
700+
size_t tl = tsslen(tsvalue(s2v(top - 1))); /* total length */
696701
TString *ts;
697702
/* collect total length and number of strings */
698703
for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {

testes/strings.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,23 @@ else
540540
assert(y == x)
541541
local z = T.externstr(x) -- external allocated long string
542542
assert(z == y)
543+
544+
local e = T.externstr("") -- empty external string
545+
assert(e .. "x" == "x" and "x" .. e == "x")
546+
assert(e .. e == "" and #e == 0)
547+
548+
-- external string as the "n" key in vararg table
549+
local n = T.externstr("n")
550+
local n0 = T.externstr("n\0")
551+
local function aux (...t) assert(t[n0] == nil); return t[n] end
552+
assert(aux(10, 20, 30) == 3)
553+
554+
-- external string as mode in weak table
555+
local t = setmetatable({}, {__mode = T.externstr("kv")})
556+
t[{}] = {}
557+
assert(next(t))
558+
collectgarbage()
559+
assert(next(t) == nil)
543560
end
544561

545562
print('OK')

0 commit comments

Comments
 (0)