From 7d58c3b7314e4a63591fa375546cfc76a042e644 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 20 Feb 2017 14:57:49 +0100 Subject: ldebug, lua_error, error --- src/lobject.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'src/lobject.js') diff --git a/src/lobject.js b/src/lobject.js index 3a519c9..0a0b615 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -203,8 +203,46 @@ class CClosure extends TValue { } +const RETS = "..."; +const PRE = "[string \""; +const POS = "\"]"; + +const luaO_chunkid = function(source, bufflen) { + let l = source.length; + let out = ""; + if (source[0] === '=') { /* 'literal' source */ + if (l < bufflen) /* small enough? */ + out = `${source.slice(1)}`; + else { /* truncate it */ + out += `${source.slice(1, bufflen)}`; + } + } else if (source[0] === '@') { /* file name */ + if (l <= bufflen) /* small enough? */ + out = `${source.slice(1)}`; + else { /* add '...' before rest of name */ + bufflen -= RETS.length; + out = `${RETS}${source.slice(1, l - bufflen)}`; + } + } else { /* string; format as [string "source"] */ + let nli = source.indexOf('\n'); /* find first new line (if any) */ + let nl = nli ? source.slice(nli) : null; + out = `${PRE}`; /* add prefix */ + bufflen -= PRE.length - RETS.length; - POS.length + 1; /* save space for prefix+suffix+'\0' */ + if (l < bufflen && nl === null) { /* small one-line source? */ + out += `${source}`; /* keep it */ + } else { + if (nl !== null) l = nl.length - source.length; /* stop at first newline */ + if (l > bufflen) l = bufflen; + out += `${source}${RETS}`; + } + out += POS; + } + + return out; +}; -module.exports.LClosure = LClosure; -module.exports.CClosure = CClosure; -module.exports.TValue = TValue; -module.exports.Table = Table; \ No newline at end of file +module.exports.LClosure = LClosure; +module.exports.CClosure = CClosure; +module.exports.TValue = TValue; +module.exports.Table = Table; +module.exports.luaO_chunkid = luaO_chunkid; \ No newline at end of file -- cgit v1.2.3-54-g00ecf