diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-02 07:36:14 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-02 07:36:14 +0100 |
commit | fa7ce109418aca2fd60fdb65b4b2451c4854dd09 (patch) | |
tree | c6469ea5d437374a7db0980422a18c32d9a6fb61 /src/lundump.js | |
parent | ebeb32400b9a2d3e7196887f7021b9bf08770fd9 (diff) | |
download | fengari-fa7ce109418aca2fd60fdb65b4b2451c4854dd09.tar.gz fengari-fa7ce109418aca2fd60fdb65b4b2451c4854dd09.tar.bz2 fengari-fa7ce109418aca2fd60fdb65b4b2451c4854dd09.zip |
Following Lua's source code as closely as possible
Diffstat (limited to 'src/lundump.js')
-rw-r--r-- | src/lundump.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/lundump.js b/src/lundump.js new file mode 100644 index 0000000..69445d4 --- /dev/null +++ b/src/lundump.js @@ -0,0 +1,67 @@ +/*jshint esversion: 6 */ +"use strict"; + +const DataView = require('buffer-dataview'); +const fs = require('fs'); +const lua_State = require('./lstate.js').lua_State; +const LClosure = require('./lobject.js').LClosure; + +/** + * Parse Lua 5.3 bytecode + * @see {@link http://www.lua.org/source/5.3/lundump.c.html|lundump.c} + */ +class BytecodeParser { + + /** + * Initilialize bytecode parser + * @constructor + * @param {DataView} dataView Contains the binary data + */ + constructor(dataView) { + this.dataView = dataView; + this.offset = 0; + } + + peekByte() { + return this.dataView.getUint8(this.offset, true); + } + + readByte() { + let byte = this.peekByte(); + this.offset++; + return byte; + } + + peekWord() { + return this.dataView.getUint32(this.offset, true); + } + + readWord() { + let word = this.peekWord(); + this.offset += 4; + + return word; + } + + checkHeader() { + if (this.readByte() !== 0x1b + || this.readByte() !== 0x4c + || this.readByte() !== 0x75 + || this.readByte() !== 0x61) + throw new Error("Bad LUA_SIGNATURE, expected [1b 4c 75 61]"); + + if (this.readByte() !== 0x53) + throw new Error("Bad Lua version, expected 5.3"); + + if (this.readByte() !== 0) + throw new Error("Supports only official PUC-Rio implementation") + } + + luaU_undump() { + checkHeader(); + let cl = new LClosure(L, this.readByte()); + + return cl; + } + +}
\ No newline at end of file |