diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-16 12:54:22 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-16 12:55:35 +0100 |
commit | 8f913dd74957ef18e144f15b78c3e55893744218 (patch) | |
tree | a47ff729efa6474c99d5371ec06272bed47a8417 /src | |
parent | 573a9c3b39bf1570a575ce3f077a33e752439165 (diff) | |
download | fengari-8f913dd74957ef18e144f15b78c3e55893744218.tar.gz fengari-8f913dd74957ef18e144f15b78c3e55893744218.tar.bz2 fengari-8f913dd74957ef18e144f15b78c3e55893744218.zip |
lua_tonumber, lua_tointeger, lua_tostring, lua_toboolean
Diffstat (limited to 'src')
-rw-r--r-- | src/lapi.js | 32 | ||||
-rw-r--r-- | src/lvm.js | 11 |
2 files changed, 37 insertions, 6 deletions
diff --git a/src/lapi.js b/src/lapi.js index dc60984..6aba915 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -9,11 +9,11 @@ const ltm = require('./ltm.js'); const lfunc = require('./lfunc.js'); const lua = require('./lua.js'); const lstate = require('./lstate.js'); +const lvm = require('./lvm.js'); const nil = ldo.nil; const MAXUPVAL = lfunc.MAXUPVAL; const CT = lua.constant_types; const TS = lua.thread_status; -const l_isfalse = lobject.l_isfalse; const TValue = lobject.TValue; const CClosure = lobject.CClosure; @@ -169,7 +169,28 @@ const lua_pushlightuserdata = function(L, p) { const lua_toboolean = function(L, idx) { let o = index2addr(L, idx); - return !l_isfalse(o); + return !o.l_isfalse(); +}; + +const lua_tolstring = function(L, idx, len) { + let o = index2addr(L, idx); + + if (!o.ttisstring() && !o.ttisnumber()) + return null; + + return len !== null ? `${o.value}`.substr(0, len) : `${o.value}`; +}; + +const lua_tostring = function(L, idx) { + return lua_tolstring(L, idx, null); +}; + +const lua_tointeger = function(L, idx) { + return lvm.tointeger(index2addr(L, idx)) +}; + +const lua_tonumber = function(L, idx) { + return lvm.tonumber(index2addr(L, idx)) }; const f_call = function(L, ud) { @@ -251,4 +272,9 @@ module.exports.lua_version = lua_version; module.exports.lua_atpanic = lua_atpanic; module.exports.lua_gettop = lua_gettop; module.exports.lua_typename = lua_typename; -module.exports.lua_type = lua_type;
\ No newline at end of file +module.exports.lua_type = lua_type; +module.exports.lua_tonumber = lua_tonumber; +module.exports.lua_tointeger = lua_tointeger; +module.exports.lua_toboolean = lua_toboolean; +module.exports.lua_tolstring = lua_tolstring; +module.exports.lua_tostring = lua_tostring;
\ No newline at end of file @@ -785,11 +785,15 @@ const luaV_tointeger = function(obj, mode) { return false; }; +const tointeger = function(o) { + return o.ttisinteger() ? o.value : luaV_tointeger(o, 0); +}; + const tonumber = function(v) { - if (v.type === CT.LUA_TNUMFLT || v.type === CT.LUA_TNUMINT) + if (v.ttnov() === CT.LUA_TNUMBER) return v.value; - if (v.type === CT.LUA_TSHRSTR || v.type === CT.LUA_TLNGSTR) + if (v.ttnov() === CT.LUA_TSTRING) return parseFloat(v.value); // TODO: luaO_str2num return false; @@ -873,7 +877,7 @@ const tostring = function(L, i) { let o = L.stack[i]; let str = `${o.value}`; - if (o.ttisstring() || (o.ttisnumber() && !isNaN(parseFloat(`${str}`)))) { + if (o.ttisstring() || (o.ttisnumber() && !isNaN(str))) { L.stack[i] = new TValue(CT.LUA_TLNGSTR, str); return true; } @@ -1023,6 +1027,7 @@ module.exports.luaV_equalobj = luaV_equalobj; module.exports.forlimit = forlimit; module.exports.luaV_tointeger = luaV_tointeger; module.exports.tonumber = tonumber; +module.exports.tointeger = tointeger; module.exports.LTnum = LTnum; module.exports.LEnum = LEnum; module.exports.LEintfloat = LEintfloat; |