summaryrefslogtreecommitdiff
path: root/src/lauxlib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-06 22:09:01 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-06 22:09:01 +1100
commit46ac9a0d9511e4bcae87db547eee55f494833cbc (patch)
tree9601192c9eafff571ee3a05f5940b4516b6a538e /src/lauxlib.js
parenteae1d1afb8108d8c642621ffd21f228779a1d792 (diff)
downloadfengari-46ac9a0d9511e4bcae87db547eee55f494833cbc.tar.gz
fengari-46ac9a0d9511e4bcae87db547eee55f494833cbc.tar.bz2
fengari-46ac9a0d9511e4bcae87db547eee55f494833cbc.zip
src/lauxlib.js: Use a Uint8Array backed structure for luaL_Buffer
Diffstat (limited to 'src/lauxlib.js')
-rw-r--r--src/lauxlib.js44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 8f8d8e7..f7988c4 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -15,11 +15,13 @@ const LUAL_NUMSIZES = 4*16 + 8;
const __name = lua.to_luastring("__name");
const __tostring = lua.to_luastring("__tostring");
+const empty = new Uint8Array(0);
+
class luaL_Buffer {
constructor() {
- this.b = null;
this.L = null;
- this.initb = null;
+ this.b = empty;
+ this.n = 0;
}
}
@@ -363,13 +365,19 @@ const luaL_optinteger = function(L, arg, def) {
};
const luaL_prepbuffsize = function(B, sz) {
- B.initb = new Uint8Array(sz);
- return B.initb;
+ let newend = B.n + sz;
+ if (B.b.length < newend) {
+ let newsize = Math.max(B.b.length * 2, newend); /* double buffer size */
+ let newbuff = new Uint8Array(newsize); /* create larger buffer */
+ newbuff.set(B.b); /* copy original content */
+ B.b = newbuff;
+ }
+ return B.b.subarray(B.n, newend);
};
const luaL_buffinit = function(L, B) {
B.L = L;
- B.b = [];
+ B.b = empty;
};
const luaL_buffinitsize = function(L, B, sz) {
@@ -384,24 +392,31 @@ const luaL_prepbuffer = function(B) {
};
const luaL_addlstring = function(B, s, l) {
- B.b = B.b.concat(Array.from(s.subarray(0, l)));
+ if (l > 0) {
+ let b = luaL_prepbuffsize(B, l);
+ b.set(s.subarray(0, l));
+ luaL_addsize(B, l);
+ }
};
-const luaL_addstring = luaL_addlstring;
+const luaL_addstring = function(B, s) {
+ luaL_addlstring(B, s, s.length);
+};
const luaL_pushresult = function(B) {
- let L = B.L;
- lua.lua_pushstring(L, Uint8Array.from(B.b));
+ lua.lua_pushlstring(B.L, B.b, B.n);
+ /* delete old buffer */
+ B.n = 0;
+ B.b = empty;
};
const luaL_addchar = function(B, c) {
- B.b.push(c);
+ luaL_prepbuffsize(B, 1);
+ B.b[B.n++] = c;
};
const luaL_addsize = function(B, s) {
- B.b = B.b.concat(Array.from(B.initb.subarray(0, s)));
B.n += s;
- B.initb = null;
};
const luaL_pushresultsize = function(B, sz) {
@@ -412,9 +427,8 @@ const luaL_pushresultsize = function(B, sz) {
const luaL_addvalue = function(B) {
let L = B.L;
let s = lua.lua_tostring(L, -1);
- // TODO: buffonstack ? necessary ?
- luaL_addstring(B, s);
- lua.lua_remove(L, -1);
+ luaL_addlstring(B, s, s.length);
+ lua.lua_pop(L, 1); /* remove value */
};
const luaL_opt = function(L, f, n, d) {