From 739b5c1888b38b2580adbe52a42b86372a7d146b Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 16 Feb 2017 11:55:54 +0100 Subject: lua_pushnumber, lua_pushinteger, lua_pushstring, lua_pushboolean --- README.md | 8 ++-- src/lapi.js | 4 +- src/lundump.js | 68 ++++++++++++++--------------- tests/C/Makefile | 7 ++- tests/C/lua_pushboolean.c | 21 +++++++++ tests/C/lua_pushinteger.c | 21 +++++++++ tests/C/lua_pushnumber.c | 2 +- tests/C/lua_pushstring.c | 21 +++++++++ tests/lapi.js | 107 +++++++++++++++++++++++++++++++++++++++++++++- tests/tests.js | 2 +- 10 files changed, 217 insertions(+), 44 deletions(-) create mode 100644 tests/C/lua_pushboolean.c create mode 100644 tests/C/lua_pushinteger.c create mode 100644 tests/C/lua_pushstring.c diff --git a/README.md b/README.md index a21cb1b..6fcea52 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ - [x] lua_gettop - [x] lua_type - [x] lua_typename + - [x] lua_pushboolean + - [x] lua_pushinteger + - [x] lua_pushnumber + - [x] lua_pushstring - [ ] lua_absindex - [ ] lua_arith - [ ] lua_call @@ -80,17 +84,13 @@ - [ ] lua_pcall - [ ] lua_pcallk - [ ] lua_pop - - [ ] lua_pushboolean - [ ] lua_pushcclosure - [ ] lua_pushcfunction - [ ] lua_pushfstring - [ ] lua_pushglobaltable - - [ ] lua_pushinteger - [ ] lua_pushlightuserdata - [ ] lua_pushliteral - [ ] lua_pushlstring - - [ ] lua_pushnumber - - [ ] lua_pushstring - [ ] lua_pushthread - [ ] lua_pushvalue - [ ] lua_pushvfstring diff --git a/src/lapi.js b/src/lapi.js index 0cb0a4b..71cf3f6 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -94,7 +94,7 @@ const lua_pushinteger = function(L, n) { assert(L.top <= L.ci.top, "stack overflow"); }; -const lua_pushlstring = function(L, s, len) { +const lua_pushlstring = function(L, s, len) { // TODO: embedded \0 assert(typeof s === "string"); assert(typeof n === "number"); @@ -113,6 +113,7 @@ const lua_pushstring = function (L, s) { L.stack[L.top] = nil; else { let ts = new TValue(CT.LUA_TLNGSTR, s); + L.stack[L.top] = ts; s = ts.value; } @@ -243,6 +244,7 @@ module.exports.lua_pushnumber = lua_pushnumber; module.exports.lua_pushinteger = lua_pushinteger; module.exports.lua_pushlstring = lua_pushlstring; module.exports.lua_pushstring = lua_pushstring; +module.exports.lua_pushboolean = lua_pushboolean; module.exports.lua_version = lua_version; module.exports.lua_atpanic = lua_atpanic; module.exports.lua_gettop = lua_gettop; diff --git a/src/lundump.js b/src/lundump.js index 67867bb..0ff3143 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -134,7 +134,7 @@ class BytecodeParser { sBx: ((ins >> o.POS_Bx) & p.MASK1(o.SIZE_Bx, 0)) - o.MAXARG_sBx }; - console.log(` [${i}] Op: ${o.OpCodes[f.code[i].opcode]} A: ${f.code[i].A} B: ${f.code[i].B} C: ${f.code[i].C} Ax: ${f.code[i].Ax} Bx: ${f.code[i].Bx} sBx: ${f.code[i].sBx}`); + // console.log(` [${i}] Op: ${o.OpCodes[f.code[i].opcode]} A: ${f.code[i].A} B: ${f.code[i].B} C: ${f.code[i].C} Ax: ${f.code[i].Ax} Bx: ${f.code[i].Bx} sBx: ${f.code[i].sBx}`); } } @@ -148,11 +148,11 @@ class BytecodeParser { idx: this.readByte() }; - console.log(` - f.upvalues[${i}].name = ${f.upvalues[i].name} - f.upvalues[${i}].instack = ${f.upvalues[i].instack} - f.upvalues[${i}].idx = ${f.upvalues[i].idx} - `); + // console.log(` + // f.upvalues[${i}].name = ${f.upvalues[i].name} + // f.upvalues[${i}].instack = ${f.upvalues[i].instack} + // f.upvalues[${i}].idx = ${f.upvalues[i].idx} + // `); } } @@ -165,24 +165,24 @@ class BytecodeParser { switch (t) { case constant_types.LUA_TNIL: f.k.push(new TValue(constant_types.LUA_TNIL, null)); - console.log(` LUA_TNIL = ${f.k[f.k.length - 1].value}`); + // console.log(` LUA_TNIL = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TBOOLEAN: f.k.push(new TValue(constant_types.LUA_TBOOLEAN, this.readByte())); - console.log(` LUA_TBOOLEAN = ${f.k[f.k.length - 1].value}`); + // console.log(` LUA_TBOOLEAN = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TNUMFLT: f.k.push(new TValue(constant_types.LUA_TNUMFLT, this.readNumber())); - console.log(` LUA_TNUMFLT = ${f.k[f.k.length - 1].value}`); + // console.log(` LUA_TNUMFLT = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TNUMINT: f.k.push(new TValue(constant_types.LUA_TNUMINT, this.readInteger())); - console.log(` LUA_TNUMINT = ${f.k[f.k.length - 1].value}`); + // console.log(` LUA_TNUMINT = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TSHRSTR: case constant_types.LUA_TLNGSTR: f.k.push(new TValue(constant_types.LUA_TLNGSTR, this.readString())); - console.log(` LUA_TLNGSTR = ${f.k[f.k.length - 1].value}`); + // console.log(` LUA_TLNGSTR = ${f.k[f.k.length - 1].value}`); break; default: throw new Error(`unrecognized constant '${t}'`); @@ -212,20 +212,20 @@ class BytecodeParser { endpc: this.readInt() }; - console.log(` - f.locvars[${i}].varname = ${f.locvars[i].varname} - f.locvars[${i}].startpc = ${f.locvars[i].startpc} - f.locvars[${i}].endpc = ${f.locvars[i].endpc} - `); + // console.log(` + // f.locvars[${i}].varname = ${f.locvars[i].varname} + // f.locvars[${i}].startpc = ${f.locvars[i].startpc} + // f.locvars[${i}].endpc = ${f.locvars[i].endpc} + // `); } n = this.readInt(); for (let i = 0; i < n; i++) { f.upvalues[i].name = this.readString(); - console.log(` - f.upvalues[${i}].name = ${f.upvalues[i].name} - `); + // console.log(` + // f.upvalues[${i}].name = ${f.upvalues[i].name} + // `); } } @@ -239,14 +239,14 @@ class BytecodeParser { f.is_vararg = this.readByte(); f.maxstacksize = this.readByte(); - console.log(` - f.source = ${f.source} - f.linedefined = ${f.linedefined} - f.lastlinedefined = ${f.lastlinedefined} - f.numparams = ${f.numparams} - f.is_vararg = ${f.is_vararg} - f.maxstacksize = ${f.maxstacksize} - `); + // console.log(` + // f.source = ${f.source} + // f.linedefined = ${f.linedefined} + // f.lastlinedefined = ${f.lastlinedefined} + // f.numparams = ${f.numparams} + // f.is_vararg = ${f.is_vararg} + // f.maxstacksize = ${f.maxstacksize} + // `); this.readCode(f); this.readConstants(f); @@ -274,13 +274,13 @@ class BytecodeParser { this.integerSize = this.readByte(); this.numberSize = this.readByte(); - console.log(` - intSize = ${this.intSize} - size_tSize = ${this.size_tSize} - instructionSize = ${this.instructionSize} - integerSize = ${this.integerSize} - numberSize = ${this.numberSize} - `) + // console.log(` + // intSize = ${this.intSize} + // size_tSize = ${this.size_tSize} + // instructionSize = ${this.instructionSize} + // integerSize = ${this.integerSize} + // numberSize = ${this.numberSize} + // `) if (this.readInteger() !== 0x5678) throw new Error("endianness mismatch"); diff --git a/tests/C/Makefile b/tests/C/Makefile index 28ff1c2..bc153a5 100644 --- a/tests/C/Makefile +++ b/tests/C/Makefile @@ -4,5 +4,8 @@ CFLAGS= -g -Wall -Wextra LIBS= -lm -llua all: - $(CC) $(CFLAGS) $(LIBS) lua_pushnil.c -o lua_pushnil.out - $(CC) $(CFLAGS) $(LIBS) lua_pushnumber.c -o lua_pushnumber.out \ No newline at end of file + $(CC) $(CFLAGS) $(LIBS) lua_pushnil.c -o lua_pushnil.out + $(CC) $(CFLAGS) $(LIBS) lua_pushnumber.c -o lua_pushnumber.out + $(CC) $(CFLAGS) $(LIBS) lua_pushinteger.c -o lua_pushinteger.out + $(CC) $(CFLAGS) $(LIBS) lua_pushstring.c -o lua_pushstring.out + $(CC) $(CFLAGS) $(LIBS) lua_pushboolean.c -o lua_pushboolean.out \ No newline at end of file diff --git a/tests/C/lua_pushboolean.c b/tests/C/lua_pushboolean.c new file mode 100644 index 0000000..d592691 --- /dev/null +++ b/tests/C/lua_pushboolean.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int main(void) { + + lua_State *L = luaL_newstate(); + + luaL_openlibs(L); + + lua_pushboolean(L, 1); + + printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L))); + + lua_close(L); + + return 0; + +} \ No newline at end of file diff --git a/tests/C/lua_pushinteger.c b/tests/C/lua_pushinteger.c new file mode 100644 index 0000000..0c9a49f --- /dev/null +++ b/tests/C/lua_pushinteger.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int main(void) { + + lua_State *L = luaL_newstate(); + + luaL_openlibs(L); + + lua_pushinteger(L, 10); + + printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L))); + + lua_close(L); + + return 0; + +} \ No newline at end of file diff --git a/tests/C/lua_pushnumber.c b/tests/C/lua_pushnumber.c index eca2564..697258f 100644 --- a/tests/C/lua_pushnumber.c +++ b/tests/C/lua_pushnumber.c @@ -10,7 +10,7 @@ int main(void) { luaL_openlibs(L); - lua_pushnumber(L, 10); + lua_pushnumber(L, 10.5); printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L))); diff --git a/tests/C/lua_pushstring.c b/tests/C/lua_pushstring.c new file mode 100644 index 0000000..fc675c7 --- /dev/null +++ b/tests/C/lua_pushstring.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int main(void) { + + lua_State *L = luaL_newstate(); + + luaL_openlibs(L); + + lua_pushstring(L, "hello"); + + printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, lua_gettop(L))); + + lua_close(L); + + return 0; + +} \ No newline at end of file diff --git a/tests/lapi.js b/tests/lapi.js index 251c5a8..6c5d7a4 100644 --- a/tests/lapi.js +++ b/tests/lapi.js @@ -42,7 +42,40 @@ test('luaL_newstate, lua_pushnil, lua_gettop, luaL_typename', function (t) { test('lua_pushnumber', function (t) { let L; - t.plan(3); + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lapi.lua_pushnumber(L, 10.5); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_gettop(L), + 1, + "top is correct" + ); + + t.strictEqual( + lauxlib.luaL_typename(L, lapi.lua_gettop(L)), + "number", + "Correct element(s) on the stack" + ); + + t.strictEqual( + L.stack[lapi.lua_gettop(L)].value, + 10.5, + "top is correct" + ); +}); + + +test('lua_pushinteger', function (t) { + let L; + + t.plan(4); t.doesNotThrow(function () { @@ -63,4 +96,76 @@ test('lua_pushnumber', function (t) { "number", "Correct element(s) on the stack" ); + + t.strictEqual( + L.stack[lapi.lua_gettop(L)].value, + 10, + "top is correct" + ); +}); + + +test('lua_pushstring', function (t) { + let L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lapi.lua_pushstring(L, "hello"); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_gettop(L), + 1, + "top is correct" + ); + + t.strictEqual( + lauxlib.luaL_typename(L, lapi.lua_gettop(L)), + "string", + "Correct element(s) on the stack" + ); + + t.strictEqual( + L.stack[lapi.lua_gettop(L)].value, + "hello", + "top is correct" + ); +}); + + +test('lua_pushboolean', function (t) { + let L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lapi.lua_pushboolean(L, true); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_gettop(L), + 1, + "top is correct" + ); + + t.strictEqual( + lauxlib.luaL_typename(L, lapi.lua_gettop(L)), + "boolean", + "Correct element(s) on the stack" + ); + + t.strictEqual( + L.stack[lapi.lua_gettop(L)].value, + true, + "top is correct" + ); }); \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index 9994ad5..02dac80 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -21,7 +21,7 @@ const toByteCode = function (luaCode) { bclist = fs.readFileSync(`${luaFile.name}.bc.txt`, 'utf8'); - console.log(bclist); + // console.log(bclist); return { dataView: new DataView(fs.readFileSync(`${luaFile.name}.bc`)), -- cgit v1.2.3-70-g09d2