From 66f3914bc7bec57df742ca2126a44e4248aa7f2c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 8 May 2017 14:20:43 +1000 Subject: src/llex.js: Cache tokens in ls.h --- src/llex.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/llex.js') diff --git a/src/llex.js b/src/llex.js index 6e5b853..f2fbd02 100644 --- a/src/llex.js +++ b/src/llex.js @@ -8,6 +8,7 @@ const ldo = require('./ldo.js'); const ljstype = require('./ljstype.js'); const lobject = require('./lobject.js'); const lstring = require('./lstring.js'); +const ltable = require('./ltable.js'); const llimit = require('./llimit.js'); const TS = defs.thread_status; const char = defs.char; @@ -194,9 +195,24 @@ const save_and_next = function(ls) { next(ls); }; +/* +** creates a new string and anchors it in scanner's table so that +** it will not be collected until the end of the compilation +** (by that time it should be anchored somewhere) +*/ const luaX_newstring = function(ls, str) { - /* TODO: caching in ls.h */ - return lstring.luaS_new(ls.L, str); + let L = ls.L; + let ts = lstring.luaS_new(L, str); + let o = ltable.luaH_set(ls.h, new lobject.TValue(defs.CT.LUA_TLNGSTR, ts)); + if (o.ttisnil()) { /* not in use yet? */ + o.setbvalue(true); + } else { /* string already present */ + /* HACK: Workaround lack of ltable 'keyfromval' */ + let tpair = ls.h.strong.get(lstring.luaS_hash(ts)); + assert(tpair.value == o); + ts = tpair.key.value; /* re-use value previously stored */ + } + return ts; }; /* -- cgit v1.2.3-54-g00ecf