From e67fb5f4cb51fbf4061ab647ef08ef4893b7f0e4 Mon Sep 17 00:00:00 2001
From: daurnimator <quae@daurnimator.com>
Date: Mon, 8 May 2017 15:41:19 +1000
Subject: Add accessors to TValue for TString values

---
 src/lapi.js    |  4 ++--
 src/ldebug.js  |  4 ++--
 src/ldump.js   |  2 +-
 src/llex.js    |  2 +-
 src/lobject.js | 17 +++++++++++++++--
 src/ltm.js     |  2 +-
 src/lvm.js     | 20 ++++++++++++--------
 7 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/src/lapi.js b/src/lapi.js
index fa6da20..64cb4e1 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -624,7 +624,7 @@ const lua_tolstring = function(L, idx) {
     if ((!o.ttisstring() && !o.ttisnumber()))
         return null;
 
-    return o.ttisstring() ? o.value : defs.to_luastring(`${o.value}`);
+    return o.ttisstring() ? o.svalue() : defs.to_luastring(`${o.value}`);
 };
 
 const lua_tostring =  lua_tolstring;
@@ -658,7 +658,7 @@ const lua_rawlen = function(L, idx) {
     switch (o.ttype()) {
         case CT.LUA_TSHRSTR:
         case CT.LUA_TLNGSTR:
-            return o.value.length;
+            return o.vslen();
         case CT.LUA_TUSERDATA:
             return o.len;
         case CT.LUA_TTABLE:
diff --git a/src/ldebug.js b/src/ldebug.js
index fc5e378..af9b9a9 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -297,7 +297,7 @@ const kname = function(p, pc, c) {
     if (OC.ISK(c)) {  /* is 'c' a constant? */
         let kvalue = p.k[OC.INDEXK(c)];
         if (kvalue.ttisstring()) {  /* literal constant? */
-            r.name = kvalue.value;  /* it is its own name */
+            r.name = kvalue.svalue();  /* it is its own name */
             return r;
         }
         /* else no reasonable name found */
@@ -408,7 +408,7 @@ const getobjname = function(p, lastpc, reg) {
             case 'OP_LOADKX': {
                 let b = op === 'OP_LOADK' ? i.Bx : p.code[pc + 1].Ax;
                 if (p.k[b].ttisstring()) {
-                    r.name = p.k[b].value;
+                    r.name = p.k[b].tsvalue();
                     r.funcname = defs.to_luastring("constant", true);
                     return r;
                 }
diff --git a/src/ldump.js b/src/ldump.js
index 128b270..99780f6 100644
--- a/src/ldump.js
+++ b/src/ldump.js
@@ -106,7 +106,7 @@ const DumpConstants = function(f, D) {
                 break;
             case CT.LUA_TSHRSTR:
             case CT.LUA_TLNGSTR:
-                DumpString(o.value, D);
+                DumpString(o.tsvalue(), D);
                 break;
         }
     }
diff --git a/src/llex.js b/src/llex.js
index f2fbd02..e3349ce 100644
--- a/src/llex.js
+++ b/src/llex.js
@@ -210,7 +210,7 @@ const luaX_newstring = function(ls, str) {
         /* HACK: Workaround lack of ltable 'keyfromval' */
         let tpair = ls.h.strong.get(lstring.luaS_hash(ts));
         assert(tpair.value == o);
-        ts = tpair.key.value; /* re-use value previously stored */
+        ts = tpair.key.tsvalue(); /* re-use value previously stored */
     }
     return ts;
 };
diff --git a/src/lobject.js b/src/lobject.js
index 905eed1..77c63d0 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -155,8 +155,21 @@ class TValue {
         this.value = tv.value;
     }
 
+    tsvalue() {
+        assert(this.ttisstring());
+        return this.value;
+    }
+
+    svalue() {
+        return this.tsvalue();
+    }
+
+    vslen() {
+        return this.tsvalue().length;
+    }
+
     jsstring(from, to) {
-        return defs.to_jsstring(this.value, from, to);
+        return defs.to_jsstring(this.svalue(), from, to);
     }
 
 }
@@ -465,7 +478,7 @@ const luaO_pushvfstring = function(L, fmt, argp) {
     }
     pushstr(L, fmt.slice(i));
     if (n > 0) lvm.luaV_concat(L, n+1);
-    return L.stack[L.top-1].value;
+    return L.stack[L.top-1].svalue();
 };
 
 const luaO_pushfstring = function(L, fmt, ...argp) {
diff --git a/src/ltm.js b/src/ltm.js
index 3647308..9011273 100644
--- a/src/ltm.js
+++ b/src/ltm.js
@@ -75,7 +75,7 @@ const luaT_objtypename = function(L, o) {
         (o.ttisfulluserdata() && (mt = o.value.metatable) !== null)) {
         let name = ltable.luaH_getstr(mt, defs.to_luastring('__name', true));
         if (name.ttisstring())
-            return name.value;
+            return name.svalue();
     }
     return ttypename(o.ttnov());
 };
diff --git a/src/lvm.js b/src/lvm.js
index dc128cc..03b7e3c 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -859,7 +859,7 @@ const luaV_tointeger = function(obj, mode) {
     } else if (obj.ttisinteger()) {
         return obj.value;
     } else if (obj.ttisstring()) {
-        return luaV_tointeger(lobject.luaO_str2num(obj.value), mode);
+        return luaV_tointeger(lobject.luaO_str2num(obj.svalue()), mode);
     }
 
     return false;
@@ -874,7 +874,7 @@ const tonumber = function(v) {
         return v.value;
 
     if (v.ttnov() === CT.LUA_TSTRING)
-        return lobject.luaO_str2num(v.value);
+        return lobject.luaO_str2num(v.svalue());
 
     return false;
 };
@@ -999,6 +999,10 @@ const tostring = function(L, i) {
     return false;
 };
 
+const isemptystr = function(o) {
+    return o.ttisstring() && o.vslen() === 0;
+};
+
 /*
 ** Main operation for concatenation: concat 'total' values in the stack,
 ** from 'L->top - total' up to 'L->top - 1'.
@@ -1011,23 +1015,23 @@ const luaV_concat = function(L, total) {
 
         if (!(L.stack[top-2].ttisstring() || L.stack[top-2].ttisnumber()) || !tostring(L, top - 1))
             ltm.luaT_trybinTM(L, L.stack[top-2], L.stack[top-1], top-2, ltm.TMS.TM_CONCAT);
-        else if (L.stack[top-1].ttisstring() && L.stack[top-1].value.length === 0)
+        else if (isemptystr(L.stack[top-1])) {
             tostring(L, top - 2);
-        else if (L.stack[top-2].ttisstring() && L.stack[top-2].value.length === 0)
+        } else if (isemptystr(L.stack[top-2])) {
             L.stack[top - 2] = L.stack[top - 1];
-        else {
+        } else {
             /* at least two non-empty string values; get as many as possible */
-            let tl = L.stack[top-1].value.length;
+            let tl = L.stack[top-1].vslen();
             /* collect total length and number of strings */
             for (n = 1; n < total && tostring(L, top - n - 1); n++) {
-                let l = L.stack[top - n - 1].value.length;
+                let l = L.stack[top - n - 1].vslen();
                 // TODO: string length overflow ?
                 tl += l;
             }
 
             let ts = [];
             for (let i = n; i > 0; i--) {
-                ts = ts.concat(L.stack[top - i].value);
+                ts = ts.concat(L.stack[top - i].svalue());
             }
 
             L.stack[top - n] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, ts));
-- 
cgit v1.2.3-70-g09d2