From ef0784b2982e65af05982d8e1e18ef1d45ea96ad Mon Sep 17 00:00:00 2001
From: daurnimator <quae@daurnimator.com>
Date: Sun, 7 Jan 2018 03:28:43 +1100
Subject: Avoid Uint8Array.slice (IE compat)

---
 src/lapi.js     |  4 ++--
 src/lauxlib.js  |  4 ++--
 src/lbaselib.js |  4 +++-
 src/ldebug.js   | 29 +++++++++++++++--------------
 src/liolib.js   |  2 +-
 src/loadlib.js  |  4 ++--
 src/lobject.js  | 17 ++++++++++-------
 src/loslib.js   |  2 +-
 src/lstrlib.js  |  6 +++---
 9 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/src/lapi.js b/src/lapi.js
index b42ad92..4836e12 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -235,12 +235,12 @@ const lua_pushlstring = function(L, s, len) {
     let ts;
     if (len === 0) {
         s = defs.to_luastring("", true);
+        ts = lstring.luaS_bless(L, s);
     } else {
         s = defs.from_userstring(s);
         assert(s.length >= len, "invalid length to lua_pushlstring");
-        s = s.slice(0, len);
+        ts = lstring.luaS_new(L, s.subarray(0, len));
     }
-    ts = lstring.luaS_bless(L, s);
     lobject.pushsvalue2s(L, ts);
     assert(L.top <= L.ci.top, "stack overflow");
 
diff --git a/src/lauxlib.js b/src/lauxlib.js
index b2ee971..1eac8d3 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -678,7 +678,7 @@ const luaL_unref = function(L, t, ref) {
 
 const errfile = function(L, what, fnameindex, error) {
     let serr = error.message;
-    let filename = lua.lua_tostring(L, fnameindex).slice(1);
+    let filename = lua.lua_tostring(L, fnameindex).subarray(1);
     lua.lua_pushfstring(L, lua.to_luastring("cannot %s %s: %s"), lua.to_luastring(what), filename, lua.to_luastring(serr));
     lua.lua_remove(L, fnameindex);
     return lua.LUA_ERRFILE;
@@ -824,7 +824,7 @@ if (typeof process === "undefined") {
             lf.pos += bytes;
         }
         if (bytes > 0)
-            return lf.buff.subarray(0, bytes); /* slice on a node.js Buffer is 'free' */
+            return lf.buff.subarray(0, bytes);
         else return null;
     };
 
diff --git a/src/lbaselib.js b/src/lbaselib.js
index 9a534f2..ca03b7f 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -26,7 +26,9 @@ if (typeof process === "undefined") {
                 s = lua.to_jsstring(s);
             } catch(e) {
                 /* otherwise push copy of raw array */
-                s = s.slice(0);
+                let copy = new Uint8Array(s.length);
+                copy.set(s);
+                s = copy;
             }
             buff.push(s);
         };
diff --git a/src/ldebug.js b/src/ldebug.js
index d7a58e0..2aa8396 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -14,6 +14,7 @@ const ltm      = require('./ltm.js');
 const luaconf  = require('./luaconf.js');
 const lvm      = require('./lvm.js');
 
+const char = defs.char;
 const CT = defs.constant_types;
 const TS = defs.thread_status;
 
@@ -219,17 +220,17 @@ const getfuncname = function(L, ci) {
 
 const auxgetinfo = function(L, what, ar, f, ci) {
     let status = 1;
-    for (; what.length > 0; what = what.slice(1)) {
-        switch (String.fromCharCode(what[0])) {
-            case 'S': {
+    for (; what.length > 0; what = what.subarray(1)) {
+        switch (what[0]) {
+            case char['S']: {
                 funcinfo(ar, f);
                 break;
             }
-            case 'l': {
+            case char['l']: {
                 ar.currentline = ci && ci.callstatus & lstate.CIST_LUA ? currentline(ci) : -1;
                 break;
             }
-            case 'u': {
+            case char['u']: {
                 ar.nups = f === null ? 0 : f.nupvalues;
                 if (f === null || f instanceof lobject.CClosure) {
                     ar.isvararg = true;
@@ -240,11 +241,11 @@ const auxgetinfo = function(L, what, ar, f, ci) {
                 }
                 break;
             }
-            case 't': {
+            case char['t']: {
                 ar.istailcall = ci ? ci.callstatus & lstate.CIST_TAIL : 0;
                 break;
             }
-            case 'n': {
+            case char['n']: {
                 let r = getfuncname(L, ci);
                 if (r === null) {
                     ar.namewhat = defs.to_luastring("", true);
@@ -255,8 +256,8 @@ const auxgetinfo = function(L, what, ar, f, ci) {
                 }
                 break;
             }
-            case 'L':
-            case 'f':  /* handled by lua_getinfo */
+            case char['L']:
+            case char['f']:  /* handled by lua_getinfo */
                 break;
             default: status = 0;  /* invalid option */
         }
@@ -269,11 +270,11 @@ const lua_getinfo = function(L, what, ar) {
     what = defs.from_userstring(what);
     let status, cl, ci, func;
     swapextra(L);
-    if (what[0] === '>'.charCodeAt(0)) {
+    if (what[0] === char['>']) {
         ci = null;
         func = L.stack[L.top - 1];
         assert(L, func.ttisfunction(), "function expected");
-        what = what.slice(1);  /* skip the '>' */
+        what = what.subarray(1);  /* skip the '>' */
         L.top--;  /* pop function */
     } else {
         ci = ar.i_ci;
@@ -283,13 +284,13 @@ const lua_getinfo = function(L, what, ar) {
 
     cl = func.ttisclosure() ? func.value : null;
     status = auxgetinfo(L, what, ar, cl, ci);
-    if (defs.luastring_indexOf(what, 'f'.charCodeAt(0)) >= 0) {
+    if (defs.luastring_indexOf(what, char['f']) >= 0) {
         lobject.pushobj2s(L, func);
         assert(L.top <= L.ci.top, "stack overflow");
     }
 
     swapextra(L);
-    if (defs.luastring_indexOf(what, 'L'.charCodeAt(0)) >= 0)
+    if (defs.luastring_indexOf(what, char['L']) >= 0)
         collectvalidlines(L, cl);
 
     return status;
@@ -310,7 +311,7 @@ const kname = function(p, pc, c) {
         /* else no reasonable name found */
     } else {  /* 'c' is a register */
         let what = getobjname(p, pc, c); /* search for 'c' */
-        if (what && what.funcname[0] === 'c'.charCodeAt(0)) {  /* found a constant name? */
+        if (what && what.funcname[0] === char['c']) {  /* found a constant name? */
             return what;  /* 'name' already filled */
         }
         /* else no reasonable name found */
diff --git a/src/liolib.js b/src/liolib.js
index 3241011..66ef3db 100644
--- a/src/liolib.js
+++ b/src/liolib.js
@@ -74,7 +74,7 @@ const getiofile = function(L, findex) {
     lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, findex);
     let p = lua.lua_touserdata(L, -1);
     if (isclosed(p))
-        lauxlib.luaL_error(L, lua.to_luastring("standard %s file is closed"), findex.slice(IOPREF_LEN));
+        lauxlib.luaL_error(L, lua.to_luastring("standard %s file is closed"), findex.subarray(IOPREF_LEN));
     return p.f;
 };
 
diff --git a/src/loadlib.js b/src/loadlib.js
index c859798..2e68ffd 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -263,12 +263,12 @@ const addtoclib = function(L, path, plib) {
 };
 
 const pushnexttemplate = function(L, path) {
-    while (path[0] === lua.LUA_PATH_SEP.charCodeAt(0)) path = path.slice(1);  /* skip separators */
+    while (path[0] === lua.LUA_PATH_SEP.charCodeAt(0)) path = path.subarray(1);  /* skip separators */
     if (path.length === 0) return null;  /* no more templates */
     let l = lua.luastring_indexOf(path, lua.LUA_PATH_SEP.charCodeAt(0));  /* find next separator */
     if (l < 0) l = path.length;
     lua.lua_pushlstring(L, path, l);  /* template */
-    return path.slice(l);
+    return path.subarray(l);
 };
 
 const searchpath = function(L, name, path, sep, dirsep) {
diff --git a/src/lobject.js b/src/lobject.js
index 384facd..824fc88 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -302,15 +302,18 @@ const luaO_chunkid = function(source, bufflen) {
     let l = source.length;
     let out;
     if (source[0] === char['=']) {  /* 'literal' source */
-        if (l < bufflen)  /* small enough? */
-            out = source.slice(1);
-        else {  /* truncate it */
-            out = source.slice(1, bufflen+1);
+        if (l < bufflen) {  /* small enough? */
+            out = new Uint8Array(l-1);
+            out.set(source.subarray(1));
+        } else {  /* truncate it */
+            out = new Uint8Array(bufflen);
+            out.set(source.subarray(1, bufflen+1));
         }
     } else if (source[0] === char['@']) {  /* file name */
-        if (l <= bufflen)  /* small enough? */
-            out = source.slice(1);
-        else {  /* add '...' before rest of name */
+        if (l <= bufflen) {  /* small enough? */
+            out = new Uint8Array(l-1);
+            out.set(source.subarray(1));
+        } else {  /* add '...' before rest of name */
             out = new Uint8Array(bufflen);
             out.set(RETS);
             bufflen -= RETS.length;
diff --git a/src/loslib.js b/src/loslib.js
index ee9ddca..b5f3ddb 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -73,7 +73,7 @@ const checkoption = function(L, conv, i, buff) {
         if (option[o] === '|'.charCodeAt(0))  /* next block? */
             oplen++;  /* will check options with next length (+1) */
         else if (array_cmp(conv, i, option, o, oplen)) {  /* match? */
-            buff.set(conv.slice(i, i+oplen)); /* copy valid option to buffer */
+            buff.set(conv.subarray(i, i+oplen)); /* copy valid option to buffer */
             return i + oplen;  /* return next item */
         }
     }
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 6cb8df9..6986411 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -88,11 +88,11 @@ const L_NBFD = 1;
 const num2straux = function(x) {
     /* if 'inf' or 'NaN', format it like '%g' */
     if (Object.is(x, Infinity))
-        return lua.to_luastring('inf', true).slice(0);
+        return lua.to_luastring('inf');
     else if (Object.is(x, -Infinity))
-        return lua.to_luastring('-inf', true).slice(0);
+        return lua.to_luastring('-inf');
     else if (Number.isNaN(x))
-        return lua.to_luastring('nan', true).slice(0);
+        return lua.to_luastring('nan');
     else if (x === 0) {  /* can be -0... */
         /* create "0" or "-0" followed by exponent */
         let zero = sprintf(luaconf.LUA_NUMBER_FMT + "x0p+0", x);
-- 
cgit v1.2.3-70-g09d2