From 4c404c404c281f1872f0283acbaee97657f3e08b Mon Sep 17 00:00:00 2001
From: Benoit Giannangeli <giann008@gmail.com>
Date: Sun, 12 Mar 2017 16:01:08 +0100
Subject: [Strings] lapi.js, lcode.js, llex.js, lparser.js

---
 src/lapi.js    | 12 ++++++------
 src/lcode.js   |  4 ++--
 src/llex.js    | 13 +++++++++----
 src/lparser.js |  8 ++++----
 src/lundump.js |  2 +-
 5 files changed, 22 insertions(+), 17 deletions(-)

(limited to 'src')

diff --git a/src/lapi.js b/src/lapi.js
index 2fbd777..79cd6fb 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -217,7 +217,7 @@ const lua_pushlstring = function(L, s, len) { // TODO: embedded \0
     assert(typeof s === "string");
     assert(typeof len === "number");
 
-    let ts = len === 0 ? new TValue(CT.LUA_TLNGSTR, "") : new TValue(CT.LUA_TLNGSTR, s.substr(0, len));
+    let ts = len === 0 ? L.l_G.intern(lua.to_luastring("")) : L.l_G.intern(lua.to_luastring(s.substr(0, len)));
     L.stack[L.top++] = ts;
 
     assert(L.top <= L.ci.top, "stack overflow");
@@ -229,7 +229,7 @@ const lua_pushstring = function (L, s) {
     if (typeof s !== "string")
         L.stack[L.top] = ldo.nil;
     else {
-        let ts = new TValue(CT.LUA_TLNGSTR, s);
+        let ts = L.l_G.intern(lua.to_luastring(s));
         L.stack[L.top] = ts;
         s = ts.value;
     }
@@ -307,7 +307,7 @@ const lua_pushglobaltable = function(L) {
 ** t[k] = value at the top of the stack (where 'k' is a string)
 */
 const auxsetstr = function(L, t, k) {
-    let str = new TValue(CT.LUA_TLNGSTR, k);
+    let str = L.l_G.intern(lua.to_luastring(k));
 
     assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
 
@@ -384,7 +384,7 @@ const lua_rawset = function(L, idx) {
 */
 
 const auxgetstr = function(L, t, k) {
-    let str = new TValue(CT.LUA_TLNGSTR, k);
+    let str = L.l_G.intern(lua.to_luastring(k));
     let slot = t.__index(t, k);
     if (t.ttistable() && !slot.ttisnil()) {
         L.stack[L.top++] = slot;
@@ -537,7 +537,7 @@ const lua_tolstring = function(L, idx) {
     if (!o.ttisstring() && !o.ttisnumber())
         return null;
 
-    return `${o.value}`;
+    return o.ttisstring() ? String.fromCharCode(...o.value) : `${o.value}`;
 };
 
 const lua_tostring =  lua_tolstring;
@@ -801,7 +801,7 @@ const lua_concat = function(L, n) {
     if (n >= 2)
         lvm.luaV_concat(L, n);
     else if (n === 0) {
-        L.stack[L.top++] = new TValue("", CT.LUA_TLNGSTR);
+        L.stack[L.top++] = L.l_G.intern(lua.to_luastring(""));
         assert(L.top <= L.ci.top, "stack overflow");
     }
 };
diff --git a/src/lcode.js b/src/lcode.js
index fedf1e2..c0ca1ff 100644
--- a/src/lcode.js
+++ b/src/lcode.js
@@ -513,7 +513,7 @@ const luaK_stringK = function(fs, s) {
 ** are no "precision" problems.
 */
 const luaK_intK = function(fs, n) {
-    let k = new TValue(CT.LUA_TLNGSTR, `${n}`);
+    let k = new TValue(CT.LUA_TLNGSTR, lua.to_luastring(`${n}`));
     let o = new TValue(CT.LUA_TNUMINT, n);
     return addk(fs, k, o);
 };
@@ -540,7 +540,7 @@ const boolK = function(fs, b) {
 ** Add nil to list of constants and return its index.
 */
 const nilK = function(fs) {
-    return addk(fs, new TValue(CT.LUA_TLNGSTR, `null`), new TValue(CT.LUA_TNIL, null));
+    return addk(fs, new TValue(CT.LUA_TLNGSTR, lua.to_luastring(`null`)), new TValue(CT.LUA_TNIL, null));
 };
 
 /*
diff --git a/src/llex.js b/src/llex.js
index 2e79066..fc3603f 100644
--- a/src/llex.js
+++ b/src/llex.js
@@ -218,7 +218,7 @@ const luaX_setinput = function(L, ls, z, source, firstchar) {
     ls.linenumber = 1;
     ls.lastline = 1;
     ls.source = source;
-    ls.envn = new TValue(CT.LUA_TLNGSTR, "_ENV");
+    ls.envn = L.l_G.intern(lua.to_luastring("_ENV"));
 };
 
 const check_next1 = function(ls, c) {
@@ -356,7 +356,7 @@ const read_long_string = function(ls, seminfo, sep) {
     }
 
     if (seminfo)
-        seminfo.ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.slice(2 + sep).join(''));
+        seminfo.ts = new TValue(CT.LUA_TLNGSTR, lua.to_luastring(ls.buff.buffer.slice(2 + sep).join('')));
 };
 
 const esccheck = function(ls, c, msg) {
@@ -478,7 +478,12 @@ const read_string = function(ls, del, seminfo) {
         }
     }
     save_and_next(ls);  /* skip delimiter */
-    seminfo.ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.slice(1, ls.buff.buffer.length-1).join(''));
+    seminfo.ts = new TValue(
+        CT.LUA_TLNGSTR,
+        lua.to_luastring(
+            ls.buff.buffer.slice(1, ls.buff.buffer.length-1).join('')
+        )
+    );
 };
 
 const isreserved = function(w) {
@@ -589,7 +594,7 @@ const llex = function(ls, seminfo) {
                         save_and_next(ls);
                     } while (ljstype.lislalnum(ls.current));
 
-                    let ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.join(''));
+                    let ts = new TValue(CT.LUA_TLNGSTR, lua.to_luastring(ls.buff.buffer.join('')));
                     seminfo.ts = ts;
                     let kidx = luaX_tokens.slice(0, 22).indexOf(ts.value);
                     if (kidx >= 0)  /* reserved word? */
diff --git a/src/lparser.js b/src/lparser.js
index 7fa3f16..42337cc 100644
--- a/src/lparser.js
+++ b/src/lparser.js
@@ -248,7 +248,7 @@ const new_localvar = function(ls, name) {
 };
 
 const new_localvarliteral = function(ls, name) {
-    new_localvar(ls, new TValue(lua.CT.LUA_TLNGSTR, name));
+    new_localvar(ls, new TValue(lua.CT.LUA_TLNGSTR, lua.to_luastring(name)));
 };
 
 const getlocvar = function(fs, i) {
@@ -479,7 +479,7 @@ const enterblock = function(fs, bl, isloop) {
 ** create a label named 'break' to resolve break statements
 */
 const breaklabel = function(ls) {
-    let n = new TValue(lua.CT.LUA_TLNGSTR, "break");
+    let n = new TValue(lua.CT.LUA_TLNGSTR, lua.to_luastring("break"));
     let l = newlabelentry(ls, ls.dyd.label, n, 0, ls.fs.pc);
     findgotos(ls, ls.dyd.label.arr[l]);
 };
@@ -1141,7 +1141,7 @@ const gotostat = function(ls, pc) {
         label = str_checkname(ls);
     else {
         llex.luaX_next(ls);  /* skip break */
-        label = new TValue(lua.CT.LUA_TLNGSTR, "break");
+        label = new TValue(lua.CT.LUA_TLNGSTR, lua.to_luastring("break"));
     }
     let g = newlabelentry(ls, ls.dyd.gt, label, line, pc);
     findlabel(ls, g);  /* close it if label already defined */
@@ -1547,7 +1547,7 @@ const luaY_parser = function(L, z, buff, dyd, name, firstchar) {
     lexstate.h = new Table();  /* create table for scanner */
     L.stack[L.top++] = lexstate.h;
     funcstate.f = cl.p = new Proto(L);
-    funcstate.f.source = new TValue(lua.CT.LUA_TLNGSTR, name);
+    funcstate.f.source = new TValue(lua.CT.LUA_TLNGSTR, lua.to_luastring(name));
     lexstate.buff = buff;
     lexstate.dyd = dyd;
     dyd.actvar.n = dyd.gt.n = dyd.label.n = 0;
diff --git a/src/lundump.js b/src/lundump.js
index d215c60..c44c847 100644
--- a/src/lundump.js
+++ b/src/lundump.js
@@ -80,7 +80,7 @@ class BytecodeParser {
             return null;
         }
 
-        let string = [];//new Uint8Array();
+        let string = [];
 
         for (let i = 0; i < size; i++)
             string.push(this.readByte());
-- 
cgit v1.2.3-70-g09d2