aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md8
-rw-r--r--src/lapi.js24
-rw-r--r--src/lauxlib.js8
-rw-r--r--src/ltm.js21
-rw-r--r--tests/lapi.js36
5 files changed, 76 insertions, 21 deletions
diff --git a/README.md b/README.md
index ee33ad4..a21cb1b 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,9 @@
- [x] lua_atpanic
- [x] lua_newstate
- [x] lua_pushnil
+ - [x] lua_gettop
+ - [x] lua_type
+ - [x] lua_typename
- [ ] lua_absindex
- [ ] lua_arith
- [ ] lua_call
@@ -50,7 +53,6 @@
- [ ] lua_getmetatable
- [ ] lua_getstack
- [ ] lua_gettable
- - [ ] lua_gettop
- [ ] lua_getupvalue
- [ ] lua_getuservalue
- [ ] lua_insert
@@ -129,8 +131,6 @@
- [ ] lua_tostring
- [ ] lua_tothread
- [ ] lua_touserdata
- - [ ] lua_type
- - [ ] lua_typename
- [ ] lua_upvalueid
- [ ] lua_upvalueindex
- [ ] lua_upvaluejoin
@@ -140,6 +140,7 @@
- [ ] lua_yieldk
- [ ] Auxiliary library
- [x] luaL_newstate
+ - [x] luaL_typename
- [ ] luaL_Buffer
- [ ] luaL_Reg
- [ ] luaL_Stream
@@ -198,7 +199,6 @@
- [ ] luaL_testudata
- [ ] luaL_tolstring
- [ ] luaL_traceback
- - [ ] luaL_typename
- [ ] luaL_unref
- [ ] luaL_where
- [ ] Standard library
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
diff --git a/tests/lapi.js b/tests/lapi.js
index b79c519..39477b2 100644
--- a/tests/lapi.js
+++ b/tests/lapi.js
@@ -1,31 +1,39 @@
/*jshint esversion: 6 */
"use strict";
-const test = require('tape');
-const beautify = require('js-beautify').js_beautify;
+const test = require('tape');
+const beautify = require('js-beautify').js_beautify;
-const getState = require("./tests.js").getState;
+const getState = require("./tests.js").getState;
-const VM = require("../src/lvm.js");
-const ldo = require("../src/ldo.js");
-const lapi = require("../src/lapi.js");
-const lauxlib = require("../src/lauxlib.js");
-const CT = require('../src/lua.js').constant_types;
+const VM = require("../src/lvm.js");
+const ldo = require("../src/ldo.js");
+const lapi = require("../src/lapi.js");
+const lauxlib = require("../src/lauxlib.js");
+const CT = require('../src/lua.js').constant_types;
-test('luaL_newstate, lua_pushnil', function (t) {
+test('luaL_newstate, lua_pushnil, lua_gettop, luaL_typename', function (t) {
let L;
- t.plan(2);
+ t.plan(3);
t.doesNotThrow(function () {
- L = lauxlib.luaL_newstate()
+
+ L = lauxlib.luaL_newstate();
lapi.lua_pushnil(L);
+
}, "JS Lua program ran without error");
t.strictEqual(
- L.stack[L.top - 1].type,
- CT.LUA_TNIL,
- "nil is on the stack"
+ lapi.lua_gettop(L),
+ 1,
+ "top is correct"
+ );
+
+ t.strictEqual(
+ lauxlib.luaL_typename(L, lapi.lua_gettop(L)),
+ "nil",
+ "Correct element(s) on the stack"
);
}); \ No newline at end of file