From 2ac8543dfd87f4c227385d6890bfcb011fc341f1 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Sun, 5 Mar 2017 15:57:04 +0100 Subject: lstrlib, string.len --- src/lauxlib.js | 5 +++++ src/lstate.js | 2 +- src/lstrlib.js | 36 ++++++++++++++++++++++++++++++++++++ src/ltm.js | 2 +- src/lvm.js | 4 ++-- 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/lstrlib.js (limited to 'src') 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"); -- cgit v1.2.3-70-g09d2