From 2977fe002407b6f86efa4ba5216f567082f33e45 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 8 May 2017 12:32:08 +1000 Subject: Move string functions to centralised lstring.js --- src/lundump.js | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) (limited to 'src/lundump.js') diff --git a/src/lundump.js b/src/lundump.js index 1570588..3e56a84 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -7,6 +7,7 @@ const defs = require('./defs.js'); const lfunc = require('./lfunc.js'); const lobject = require('./lobject.js'); const lopcodes = require('./lopcodes.js'); +const lstring = require('./lstring.js'); const llex = require('./llex.js'); let LUAC_DATA = [0x19, 0x93, defs.char["\r"], defs.char["\n"], 0x1a, defs.char["\n"]]; @@ -36,6 +37,7 @@ class BytecodeParser { read(size) { let buffer = this.buffer.read(size); + assert(Array.isArray(buffer)); if (buffer.length < size) this.error("truncated"); return buffer; } @@ -74,8 +76,8 @@ class BytecodeParser { return this.dv.getFloat64(0, true); } - read8bitString(n) { - let size = typeof n !== 'undefined' ? n : Math.max(this.readByte() - 1, 0); + readString() { + let size = Math.max(this.readByte() - 1, 0); if (size + 1 === 0xFF) size = this.readSize_t() - 1; @@ -84,25 +86,7 @@ class BytecodeParser { return null; } - let string = []; - - for (let i = 0; i < size; i++) - string.push(this.readByte()); - - return string; - } - - readString(n) { - let size = typeof n !== 'undefined' ? n : Math.max(this.readByte() - 1, 0); - - if (size + 1 === 0xFF) - size = this.readSize_t() - 1; - - if (size === 0) { - return null; - } - - return this.read(size); + return lstring.luaS_new(this.L, this.read(size)); } /* creates a mask with 'n' 1 bits at position 'p' */ @@ -176,7 +160,7 @@ class BytecodeParser { break; case defs.CT.LUA_TSHRSTR: case defs.CT.LUA_TLNGSTR: - f.k.push(this.L.l_G.intern(this.read8bitString())); + f.k.push(new lobject.TValue(defs.CT.LUA_TLNGSTR, this.readString())); break; default: this.error(`unrecognized constant '${t}'`); @@ -229,9 +213,14 @@ class BytecodeParser { this.readDebug(f); } + checkliteral(s, msg) { + let buff = this.read(s.length); + if (buff.join() !== s.join()) + this.error(msg); + } + checkHeader() { - if (this.readString(3).join() !== defs.to_luastring(defs.LUA_SIGNATURE.substring(1)).join()) /* 1st char already checked */ - this.error("bad LUA_SIGNATURE, expected 'Lua'"); + this.checkliteral(defs.to_luastring(defs.LUA_SIGNATURE.substring(1)), "bad LUA_SIGNATURE, expected 'Lua'"); /* 1st char already checked */ if (this.readByte() !== 0x53) this.error("bad Lua version, expected 5.3"); @@ -239,8 +228,7 @@ class BytecodeParser { if (this.readByte() !== 0) this.error("supports only official PUC-Rio implementation"); - if (this.readString(6).join() !== LUAC_DATA.join()) - this.error("bytecode corrupted"); + this.checkliteral(LUAC_DATA, "bytecode corrupted"); this.intSize = this.readByte(); this.size_tSize = this.readByte(); -- cgit v1.2.3-54-g00ecf