From 636b1d8a5a566fac0c9c61fed44049c659435a56 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 8 May 2017 18:26:14 +1000 Subject: Introduce a TString class --- src/lstring.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/lstring.js') diff --git a/src/lstring.js b/src/lstring.js index af434c6..3e94171 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -1,20 +1,41 @@ "use strict"; +const assert = require("assert"); + const defs = require('./defs.js'); +class TString { + + constructor(L, str) { + this.realstring = str; + } + + getstr() { + return this.realstring; + } + + tsslen() { + return this.realstring.length; + } + +} + const luaS_eqlngstr = function(a, b) { - return a == b || (a.length == b.length && a.join() == b.join()); + assert(a instanceof TString); + assert(b instanceof TString); + return a == b || (a.realstring.length == b.realstring.length && a.realstring.join() == b.realstring.join()); }; /* converts strings (arrays) to a consistent map key */ const luaS_hash = function(str) { - return str.map(e => `${e}|`).join(''); + assert(str instanceof TString); + return str.realstring.map(e => `${e}|`).join(''); }; /* variant that takes ownership of array */ const luaS_bless = function(L, str) { Object.freeze(str); - return str; + return new TString(L, str); }; /* makes a copy */ @@ -32,3 +53,4 @@ 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; -- cgit v1.2.3-54-g00ecf