From 3a77114269b53fc57ff00342af18e71f97dcf590 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 16 Feb 2017 07:35:26 +0100 Subject: luaL_newstate, lua_pushnil, lua_gettop, luaL_typename --- src/lapi.js | 24 +++++++++++++++++++++++- src/lauxlib.js | 8 +++++++- src/ltm.js | 21 ++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 9e20d7f..0cb0a4b 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -5,6 +5,7 @@ const assert = require('assert'); const ldo = require('./ldo.js'); const lobject = require('./lobject.js'); +const ltm = require('./ltm.js'); const lfunc = require('./lfunc.js'); const lua = require('./lua.js'); const lstate = require('./lstate.js'); @@ -49,6 +50,14 @@ const index2addr = function(L, idx) { }; +/* +** basic stack manipulation +*/ + +const lua_gettop = function(L) { + return L.top - 1; +}; + const lua_pushvalue = function(L, idx) { L.stack[L.top] = L.stack[index2addr(L, idx)]; @@ -164,6 +173,16 @@ const f_call = function(L, ud) { ldo.luaD_callnoyield(L, ud.func, ud.nresults); }; +const lua_type = function(L, idx) { + let o = index2addr(L, idx); + return o.ttnov(); // TODO: isvalid ? luaO_nilobject != nil tvalue ? +}; + +const lua_typename = function(L, t) { + assert(CT.LUA_TNONE <= t && t < CT.LUA_NUMTAGS, "invalid tag"); + return ltm.ttypename(t); +}; + /* ** 'load' and 'call' functions (run Lua code) @@ -225,4 +244,7 @@ module.exports.lua_pushinteger = lua_pushinteger; module.exports.lua_pushlstring = lua_pushlstring; module.exports.lua_pushstring = lua_pushstring; module.exports.lua_version = lua_version; -module.exports.lua_atpanic = lua_atpanic; \ No newline at end of file +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 diff --git a/src/lauxlib.js b/src/lauxlib.js index 3ff346e..6cc3d3c 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -18,4 +18,10 @@ const luaL_newstate = function() { }; -module.exports.luaL_newstate = luaL_newstate; \ No newline at end of file +const luaL_typename = function(L, i) { + return lapi.lua_typename(L, lapi.lua_type(L, i)); +} + + +module.exports.luaL_newstate = luaL_newstate; +module.exports.luaL_typename = luaL_typename; \ No newline at end of file diff --git a/src/ltm.js b/src/ltm.js index 8b4f4f5..77d65e4 100644 --- a/src/ltm.js +++ b/src/ltm.js @@ -40,6 +40,24 @@ const TMS = { TM_N: 26 }; +const luaT_typenames_ = [ + "no value", + "nil", + "boolean", + "userdata", + "number", + "string", + "table", + "function", + "userdata", + "thread", + "proto" /* this last case is used for tests only */ +]; + +const ttypename = function(t) { + return luaT_typenames_[t + 1]; +}; + const luaT_init = function(L) { L.l_G.tmname = []; for (let event in TMS) { @@ -112,4 +130,5 @@ module.exports.luaT_callbinTM = luaT_callbinTM; module.exports.luaT_trybinTM = luaT_trybinTM; module.exports.luaT_callorderTM = luaT_callorderTM; module.exports.luaT_gettmbyobj = luaT_gettmbyobj; -module.exports.luaT_init = luaT_init; \ No newline at end of file +module.exports.luaT_init = luaT_init; +module.exports.ttypename = ttypename; \ No newline at end of file -- cgit v1.2.3-70-g09d2