From 066719cf3db7f67d4373acb1398e9ca3a2dde3ed Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 2 Feb 2017 15:38:51 +0100 Subject: readConstants --- src/lua.js | 25 +++++++++++++++++++++++ src/lundump.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/lua.js b/src/lua.js index f41991e..c496c33 100644 --- a/src/lua.js +++ b/src/lua.js @@ -11,6 +11,31 @@ const thread_status = { LUA_ERRERR: 6 }; +const constant_types = { + LUA_TNONE: -1, + LUA_TNIL: 0, + LUA_TBOOLEAN: 1, + LUA_TLIGHTUSERDATA: 2, + LUA_TNUMBER: 3, + LUA_TSTRING: 4, + LUA_TTABLE: 5, + LUA_TFUNCTION: 6, + LUA_TUSERDATA: 7, + LUA_TTHREAD: 8, + LUA_NUMTAGS: 9 +}; + +constant_types.LUA_TSHRSTR = constant_types.LUA_TSTRING | (0 << 4); /* short strings */ +constant_types.LUA_TLNGSTR = constant_types.LUA_TSTRING | (1 << 4); /* long strings */ + +constant_types.LUA_TNUMFLT = constant_types.LUA_TNUMBER | (0 << 4); /* float numbers */ +constant_types.LUA_TNUMINT = constant_types.LUA_TNUMBER | (1 << 4); /* integer numbers */ + +constant_types.LUA_TLCL = constant_types.LUA_TFUNCTION | (0 << 4); /* Lua closure */ +constant_types.LUA_TLCF = constant_types.LUA_TFUNCTION | (1 << 4); /* light C function */ +constant_types.LUA_TCCL = constant_types.LUA_TFUNCTION | (2 << 4); /* C closure */ + module.exports = { + constant_types: constant_types, thread_status: thread_status }; \ No newline at end of file diff --git a/src/lundump.js b/src/lundump.js index c9481da..ce16312 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -1,13 +1,14 @@ /*jshint esversion: 6 */ "use strict"; -const DataView = require('buffer-dataview'); -const fs = require('fs'); -const assert = require('assert'); +const DataView = require('buffer-dataview'); +const fs = require('fs'); +const assert = require('assert'); -const lua_State = require('./lstate.js').lua_State; -const LClosure = require('./lobject.js').LClosure; -const Proto = require('./lfunc.js').Proto; +const lua_State = require('./lstate.js').lua_State; +const LClosure = require('./lobject.js').LClosure; +const Proto = require('./lfunc.js').Proto; +const constant_types = require('./lua.js').constant_types; const LUAI_MAXSHORTLEN = 40; @@ -113,6 +114,55 @@ class BytecodeParser { f.code.push(this.readInstruction()); } + readConstants(f) { + let n = this.readInt(); + + for (let i = 0; i < n; i++) { + let t = this.readByte(); + + switch (t) { + case constant_types.LUA_TNIL: + f.k.push({ + type: constant_types.LUA_TNIL, + value: null + }); + console.log(`LUA_TNIL : ${f.k[f.k.length - 1].value}`); + break; + case constant_types.LUA_TBOOLEAN: + f.k.push({ + type: constant_types.LUA_TBOOLEAN, + value: this.readByte() + }); + console.log(`LUA_TBOOLEAN : ${f.k[f.k.length - 1].value}`); + break; + case constant_types.LUA_TNUMFLT: + f.k.push({ + type: constant_types.LUA_TNUMFLT, + value: this.readNumber() + }); + console.log(`LUA_TNUMFLT : ${f.k[f.k.length - 1].value}`); + break; + case constant_types.LUA_TNUMINT: + f.k.push({ + type: constant_types.LUA_TNUMINT, + value: this.readInteger() + }); + 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({ + type: constant_types.LUA_TLNGSTR, + value: this.readString() + }); + console.log(`LUA_TLNGSTR : ${f.k[f.k.length - 1].value}`); + break; + default: + throw new Error(`unrecognized constant '${t}'`); + } + } + } + readFunction(f, psource) { f.source = this.readString(); if (f.source == null) /* no source in dump? */ @@ -133,7 +183,7 @@ class BytecodeParser { `); this.readCode(f); - // this.readConstants(f); + this.readConstants(f); // this.readUpvalues(f); // this.readProtos(f); // this.readDebug(f); -- cgit v1.2.3-70-g09d2