diff options
author | daurnimator <quae@daurnimator.com> | 2017-05-08 21:00:41 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-05-09 14:25:51 +1000 |
commit | 9d1b2502045fe7d7bb86570779636c9bbc2ba19a (patch) | |
tree | 839179de719be4d0a1d94f5ed6c8c0807ef27698 /src/lstring.js | |
parent | 048234f2ceaad1801473bf8a95219d08797b5e9d (diff) | |
download | fengari-9d1b2502045fe7d7bb86570779636c9bbc2ba19a.tar.gz fengari-9d1b2502045fe7d7bb86570779636c9bbc2ba19a.tar.bz2 fengari-9d1b2502045fe7d7bb86570779636c9bbc2ba19a.zip |
Cache string hashes in TString
Diffstat (limited to 'src/lstring.js')
-rw-r--r-- | src/lstring.js | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/lstring.js b/src/lstring.js index 3e94171..cc399b8 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -7,6 +7,7 @@ const defs = require('./defs.js'); class TString { constructor(L, str) { + this.hash = null; this.realstring = str; } @@ -28,8 +29,16 @@ const luaS_eqlngstr = function(a, b) { /* converts strings (arrays) to a consistent map key */ const luaS_hash = function(str) { - assert(str instanceof TString); - return str.realstring.map(e => `${e}|`).join(''); + assert(Array.isArray(str)); + return str.map(e => `${e}|`).join(''); +}; + +const luaS_hashlongstr = function(ts) { + assert(ts instanceof TString); + if(ts.hash === null) { + ts.hash = luaS_hash(ts.getstr()); + } + return ts.hash; }; /* variant that takes ownership of array */ @@ -48,9 +57,10 @@ const luaS_newliteral = function(L, str) { return luaS_bless(L, defs.to_luastring(str)); }; -module.exports.luaS_eqlngstr = luaS_eqlngstr; -module.exports.luaS_hash = luaS_hash; -module.exports.luaS_bless = luaS_bless; -module.exports.luaS_new = luaS_new; -module.exports.luaS_newliteral = luaS_newliteral; -module.exports.TString = TString; +module.exports.luaS_eqlngstr = luaS_eqlngstr; +module.exports.luaS_hash = luaS_hash; +module.exports.luaS_hashlongstr = luaS_hashlongstr; +module.exports.luaS_bless = luaS_bless; +module.exports.luaS_new = luaS_new; +module.exports.luaS_newliteral = luaS_newliteral; +module.exports.TString = TString; |