aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-05 15:57:04 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-03-05 15:57:04 +0100
commit2ac8543dfd87f4c227385d6890bfcb011fc341f1 (patch)
treee122a3d4c28406aadeba2557f76bb123a34025ef
parentdb136649e45a63edb8bc511e756249a9e6ca9ea7 (diff)
downloadfengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.tar.gz
fengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.tar.bz2
fengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.zip
lstrlib, string.len
-rw-r--r--src/lauxlib.js5
-rw-r--r--src/lstate.js2
-rw-r--r--src/lstrlib.js36
-rw-r--r--src/ltm.js2
-rw-r--r--src/lvm.js4
5 files changed, 45 insertions, 4 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js
index d58a171..5223b9b 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -158,6 +158,10 @@ const luaL_checktype = function(L, arg, t) {
tag_error(L, arg, t);
};
+const luaL_checkstring = function(L, n) {
+ luaL_checklstring(L, n, null);
+};
+
const luaL_checklstring = function(L, arg) {
let s = lapi.lua_tolstring(L, arg);
if (!s) tag_error(L, arg, CT.LUA_TSTRING);
@@ -394,6 +398,7 @@ module.exports.luaL_checkinteger = luaL_checkinteger;
module.exports.luaL_checklstring = luaL_checklstring;
module.exports.luaL_checknumber = luaL_checknumber;
module.exports.luaL_checkstack = luaL_checkstack;
+module.exports.luaL_checkstring = luaL_checkstring;
module.exports.luaL_checktype = luaL_checktype;
module.exports.luaL_error = luaL_error;
module.exports.luaL_getmetafield = luaL_getmetafield;
diff --git a/src/lstate.js b/src/lstate.js
index 1fc7765..29bf186 100644
--- a/src/lstate.js
+++ b/src/lstate.js
@@ -69,7 +69,7 @@ class global_State {
constructor(L) {
this.mainthread = L;
- this.strt = null // TODO: string hash table
+ this.strt = null; // TODO: string hash table
this.l_registry = nil;
this.panic = null;
this.version = null;
diff --git a/src/lstrlib.js b/src/lstrlib.js
new file mode 100644
index 0000000..194d6ba
--- /dev/null
+++ b/src/lstrlib.js
@@ -0,0 +1,36 @@
+"use strict";
+
+const assert = require('assert');
+
+const lua = require('./lua.js');
+const lapi = require('./lapi.js');
+const lauxlib = require('./lauxlib.js');
+const CT = lua.constant_types;
+const TS = lua.thread_status;
+
+const str_len = function(L) {
+ lauxlib.luaL_checkstring(L, 1);
+ lapi.lua_pushinteger(L, lapi.lua_tostring(L, 1).length);
+ return 1;
+};
+
+const strlib = {
+ "len": str_len
+};
+
+const createmetatable = function(L) {
+ lapi.lua_createtable(L, 0, 1); /* table to be metatable for strings */
+ lapi.lua_pushliteral(L, ""); /* dummy string */
+ lapi.lua_pushvalue(L, -2); /* copy table */
+ lapi.lua_setmetatable(L, -2); /* set table as metatable for strings */
+ lapi.lua_pop(L, 1); /* pop dummy string */
+ lapi.lua_pushvalue(L, -2); /* get string library */
+ lapi.lua_setfield(L, -2, "__index"); /* metatable.__index = string */
+ lapi.lua_pop(L, 1); /* pop metatable */
+};
+
+const luaopen_string = function(L) {
+ lauxlib.luaL_newlib(L, strlib);
+ createmetatable(L);
+ return 1;
+}; \ No newline at end of file
diff --git a/src/ltm.js b/src/ltm.js
index 6186066..742ce56 100644
--- a/src/ltm.js
+++ b/src/ltm.js
@@ -149,7 +149,7 @@ const luaT_gettmbyobj = function(L, o, event) {
mt = o.metatable;
break;
default:
- // TODO: mt = G(L)->mt[ttnov(o)];
+ mt = L.l_G.mt[o.ttnov()];
}
return mt ? mt.__index(mt, event) : ldo.nil;
diff --git a/src/lvm.js b/src/lvm.js
index 534ba7b..54d34e3 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -594,13 +594,13 @@ const luaV_execute = function(L) {
ldebug.luaG_runerror(L, "'for' limit must be a number");
plimit.type = CT.LUA_TNUMFLT;
- plimit.value = nlimit
+ plimit.value = nlimit;
if (nstep === false)
ldebug.luaG_runerror(L, "'for' step must be a number");
pstep.type = CT.LUA_TNUMFLT;
- pstep.value = nstep
+ pstep.value = nstep;
if (ninit === false)
ldebug.luaG_runerror(L, "'for' initial value must be a number");