diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/lbaselib.js | 8 | ||||
-rw-r--r-- | tests/lbaselib.js | 53 |
3 files changed, 62 insertions, 1 deletions
@@ -210,6 +210,7 @@ - [x] rawequal - [x] rawset - [x] rawget + - [x] type - [ ] assert - [ ] collectgarbage - [ ] dofile @@ -224,7 +225,6 @@ - [ ] rawlen - [ ] select - [ ] tonumber - - [ ] type - [ ] xpcall - [ ] ... - [ ] Debug (errors) diff --git a/src/lbaselib.js b/src/lbaselib.js index a89cf9d..db6e61d 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -81,6 +81,13 @@ const luaB_rawset = function(L) { return 1; }; +const luaB_type = function(L) { + let t = lapi.lua_type(L, 1); + lauxlib.luaL_argcheck(L, t != CT.LUA_TNONE, 1, "value expected"); + lapi.lua_pushstring(L, lapi.lua_typename(L, t)); + return 1; +}; + const base_funcs = { "print": luaB_print, "tostring": luaB_tostring, @@ -89,6 +96,7 @@ const base_funcs = { "rawequal": luaB_rawequal, "rawset": luaB_rawset, "rawget": luaB_rawget, + "type": luaB_type }; const luaopen_base = function(L) { diff --git a/tests/lbaselib.js b/tests/lbaselib.js index b3af927..87009ca 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -186,4 +186,57 @@ test('rawset, rawget', function (t) { "bye", "Correct element(s) on the stack" ); +}); + + +test('type', function (t) { + let luaCode = ` + return type(1), type(true), type("hello"), type({}), type(nil) + `, L; + + t.plan(6); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-type"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -5), + "number", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -4), + "boolean", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -3), + "string", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "table", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "nil", + "Correct element(s) on the stack" + ); });
\ No newline at end of file |