aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-12 16:01:08 +0100
committerBenoit Giannangeli <giann@users.noreply.github.com>2017-03-13 11:03:24 +0100
commit4c404c404c281f1872f0283acbaee97657f3e08b (patch)
treea347635408110a0219e494876d9e54542175a645
parent632cf08572b35e8a350ac8b1f9d5bf77fb4e7b95 (diff)
downloadfengari-4c404c404c281f1872f0283acbaee97657f3e08b.tar.gz
fengari-4c404c404c281f1872f0283acbaee97657f3e08b.tar.bz2
fengari-4c404c404c281f1872f0283acbaee97657f3e08b.zip
[Strings] lapi.js, lcode.js, llex.js, lparser.js
-rw-r--r--src/lapi.js12
-rw-r--r--src/lcode.js4
-rw-r--r--src/llex.js13
-rw-r--r--src/lparser.js8
-rw-r--r--src/lundump.js2
-rw-r--r--tests/C/Makefile17
-rw-r--r--tests/C/lua_call-jsclosure.c30
-rw-r--r--tests/C/lua_call.c28
-rw-r--r--tests/C/lua_pop.c24
-rw-r--r--tests/C/lua_pushboolean.c21
-rw-r--r--tests/C/lua_pushcclosure-light.c25
-rw-r--r--tests/C/lua_pushinteger.c21
-rw-r--r--tests/C/lua_pushnil.c21
-rw-r--r--tests/C/lua_pushnumber.c21
-rw-r--r--tests/C/lua_pushstring.c21
-rw-r--r--tests/C/lua_pushvalue.c24
-rw-r--r--tests/C/lua_setgettable.c31
17 files changed, 22 insertions, 301 deletions
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());
diff --git a/tests/C/Makefile b/tests/C/Makefile
deleted file mode 100644
index 2b753be..0000000
--- a/tests/C/Makefile
+++ /dev/null
@@ -1,17 +0,0 @@
-CC= gcc-6 -std=gnu99
-CFLAGS= -g -Wall -Wextra
-
-LIBS= -lm -llua
-
-all:
- $(CC) $(CFLAGS) $(LIBS) lua_pushnil.c -o lua_pushnil.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushnumber.c -o lua_pushnumber.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushinteger.c -o lua_pushinteger.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushstring.c -o lua_pushstring.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushboolean.c -o lua_pushboolean.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushvalue.c -o lua_pushvalue.out
- $(CC) $(CFLAGS) $(LIBS) lua_pushcclosure-light.c -o lua_pushcclosure-light.out
- $(CC) $(CFLAGS) $(LIBS) lua_call.c -o lua_call.out
- $(CC) $(CFLAGS) $(LIBS) lua_call-jsclosure.c -o lua_call-jsclosure.out
- $(CC) $(CFLAGS) $(LIBS) lua_pop.c -o lua_pop.out
- $(CC) $(CFLAGS) $(LIBS) lua_setgettable.c -o lua_setgettable.out \ No newline at end of file
diff --git a/tests/C/lua_call-jsclosure.c b/tests/C/lua_call-jsclosure.c
deleted file mode 100644
index 98fa739..0000000
--- a/tests/C/lua_call-jsclosure.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int func(lua_State *L) {
- const char *s = lua_tostring(L, lua_upvalueindex(1));
- lua_pushstring(L, s);
- return 1;
-}
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushstring(L, "upvalue hello !");
- lua_pushcclosure(L, func, 1);
-
- lua_call(L, 0, 1);
-
- printf("Lua returned: %s\n", lua_tostring(L, -1));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_call.c b/tests/C/lua_call.c
deleted file mode 100644
index 3e0e406..0000000
--- a/tests/C/lua_call.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int func(lua_State *L) {
- lua_pushstring(L, "hello");
- return 1;
-}
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushcfunction(L, func);
-
- lua_call(L, 0, 1);
-
- printf("Lua returned: %s\n", lua_tostring(L, -1));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pop.c b/tests/C/lua_pop.c
deleted file mode 100644
index 06b8ef1..0000000
--- a/tests/C/lua_pop.c
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushstring(L, "hello");
- lua_pushstring(L, "world");
-
- lua_pop(L, 1);
-
- printf("L->top(%d): %s\n", lua_gettop(L), lua_tostring(L, -1));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushboolean.c b/tests/C/lua_pushboolean.c
deleted file mode 100644
index d592691..0000000
--- a/tests/C/lua_pushboolean.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushboolean(L, 1);
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushcclosure-light.c b/tests/C/lua_pushcclosure-light.c
deleted file mode 100644
index a891450..0000000
--- a/tests/C/lua_pushcclosure-light.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int func(lua_State *L) {
- return 0;
-}
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushcclosure(L, func, 0);
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushinteger.c b/tests/C/lua_pushinteger.c
deleted file mode 100644
index da134b6..0000000
--- a/tests/C/lua_pushinteger.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushinteger(L, 10);
-
- printf("L->top(%d): type %s, value %ld\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)), lua_tointeger(L, -1));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushnil.c b/tests/C/lua_pushnil.c
deleted file mode 100644
index bb31246..0000000
--- a/tests/C/lua_pushnil.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushnil(L);
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushnumber.c b/tests/C/lua_pushnumber.c
deleted file mode 100644
index 697258f..0000000
--- a/tests/C/lua_pushnumber.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushnumber(L, 10.5);
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushstring.c b/tests/C/lua_pushstring.c
deleted file mode 100644
index fc675c7..0000000
--- a/tests/C/lua_pushstring.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushstring(L, "hello");
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L)));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_pushvalue.c b/tests/C/lua_pushvalue.c
deleted file mode 100644
index 055ee23..0000000
--- a/tests/C/lua_pushvalue.c
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_pushstring(L, "hello");
-
- lua_pushvalue(L, -1);
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, -1));
- printf("L->top - 1(%d): %s\n", lua_gettop(L) - 1, luaL_typename(L, -2));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file
diff --git a/tests/C/lua_setgettable.c b/tests/C/lua_setgettable.c
deleted file mode 100644
index af3d9f8..0000000
--- a/tests/C/lua_setgettable.c
+++ /dev/null
@@ -1,31 +0,0 @@
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(void) {
-
- lua_State *L = luaL_newstate();
-
- luaL_openlibs(L);
-
- lua_newtable(L);
-
- lua_pushstring(L, "key");
- lua_pushstring(L, "value");
-
- lua_settable(L, -3);
-
- lua_pushstring(L, "key");
- lua_gettable(L, -2);
-
-
- printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, -1));
- printf("Table has value: %s\n", lua_tostring(L, -1));
-
- lua_close(L);
-
- return 0;
-
-} \ No newline at end of file