From c18debce7c7ea94e0155a5d628f21375ebfb7218 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 Jan 2018 14:30:11 +1100 Subject: src/lutf8lib.js: Use destructuring requires --- src/lutf8lib.js | 119 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/lutf8lib.js b/src/lutf8lib.js index 9cf7f14..3e3e194 100644 --- a/src/lutf8lib.js +++ b/src/lutf8lib.js @@ -1,8 +1,33 @@ "use strict"; -const lua = require('./lua.js'); -const lauxlib = require('./lauxlib.js'); -const {luastring_of, to_luastring} = require("./fengaricore.js"); +const { + lua_gettop, + lua_pushcfunction, + lua_pushfstring, + lua_pushinteger, + lua_pushnil, + lua_pushstring, + lua_pushvalue, + lua_setfield, + lua_tointeger +} = require('./lua.js'); +const { + luaL_Buffer, + luaL_addvalue, + luaL_argcheck, + luaL_buffinit, + luaL_checkinteger, + luaL_checkstack, + luaL_checkstring, + luaL_error, + luaL_newlib, + luaL_optinteger, + luaL_pushresult +} = require('./lauxlib.js'); +const { + luastring_of, + to_luastring +} = require("./fengaricore.js"); const MAXUNICODE = 0x10FFFF; @@ -55,50 +80,50 @@ const utf8_decode = function(s, pos) { */ const utflen = function(L) { let n = 0; - let s = lauxlib.luaL_checkstring(L, 1); + let s = luaL_checkstring(L, 1); let len = s.length; - let posi = u_posrelat(lauxlib.luaL_optinteger(L, 2, 1), len); - let posj = u_posrelat(lauxlib.luaL_optinteger(L, 3, -1), len); + let posi = u_posrelat(luaL_optinteger(L, 2, 1), len); + let posj = u_posrelat(luaL_optinteger(L, 3, -1), len); - lauxlib.luaL_argcheck(L, 1 <= posi && --posi <= len, 2, to_luastring("initial position out of string")); - lauxlib.luaL_argcheck(L, --posj < len, 3, to_luastring("final position out of string")); + luaL_argcheck(L, 1 <= posi && --posi <= len, 2, to_luastring("initial position out of string")); + luaL_argcheck(L, --posj < len, 3, to_luastring("final position out of string")); while (posi <= posj) { let dec = utf8_decode(s, posi); if (dec === null) { /* conversion error? */ - lua.lua_pushnil(L); /* return nil ... */ - lua.lua_pushinteger(L, posi + 1); /* ... and current position */ + lua_pushnil(L); /* return nil ... */ + lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } posi = dec.pos; n++; } - lua.lua_pushinteger(L, n); + lua_pushinteger(L, n); return 1; }; const p_U = to_luastring("%U"); const pushutfchar = function(L, arg) { - let code = lauxlib.luaL_checkinteger(L, arg); - lauxlib.luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, to_luastring("value out of range", true)); - lua.lua_pushfstring(L, p_U, code); + let code = luaL_checkinteger(L, arg); + luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, to_luastring("value out of range", true)); + lua_pushfstring(L, p_U, code); }; /* ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... */ const utfchar = function(L) { - let n = lua.lua_gettop(L); /* number of arguments */ + let n = lua_gettop(L); /* number of arguments */ if (n === 1) /* optimize common case of single char */ pushutfchar(L, 1); else { - let b = new lauxlib.luaL_Buffer(); - lauxlib.luaL_buffinit(L, b); + let b = new luaL_Buffer(); + luaL_buffinit(L, b); for (let i = 1; i <= n; i++) { pushutfchar(L, i); - lauxlib.luaL_addvalue(b); + luaL_addvalue(b); } - lauxlib.luaL_pushresult(b); + luaL_pushresult(b); } return 1; }; @@ -108,19 +133,19 @@ const utfchar = function(L) { ** position 'i' starts; 0 means character at 'i'. */ const byteoffset = function(L) { - let s = lauxlib.luaL_checkstring(L, 1); - let n = lauxlib.luaL_checkinteger(L, 2); + let s = luaL_checkstring(L, 1); + let n = luaL_checkinteger(L, 2); let posi = n >= 0 ? 1 : s.length + 1; - posi = u_posrelat(lauxlib.luaL_optinteger(L, 3, posi), s.length); + posi = u_posrelat(luaL_optinteger(L, 3, posi), s.length); - lauxlib.luaL_argcheck(L, 1 <= posi && --posi <= s.length, 3, to_luastring("position out of range", true)); + luaL_argcheck(L, 1 <= posi && --posi <= s.length, 3, to_luastring("position out of range", true)); if (n === 0) { /* find beginning of current byte sequence */ while (posi > 0 && iscont(s[posi])) posi--; } else { if (iscont(s[posi])) - lauxlib.luaL_error(L, to_luastring("initial position is a continuation byte", true)); + luaL_error(L, to_luastring("initial position is a continuation byte", true)); if (n < 0) { while (n < 0 && posi > 0) { /* move back */ @@ -141,9 +166,9 @@ const byteoffset = function(L) { } if (n === 0) /* did it find given character? */ - lua.lua_pushinteger(L, posi + 1); + lua_pushinteger(L, posi + 1); else /* no such character */ - lua.lua_pushnil(L); + lua_pushnil(L); return 1; }; @@ -153,24 +178,24 @@ const byteoffset = function(L) { ** that start in the range [i,j] */ const codepoint = function(L) { - let s = lauxlib.luaL_checkstring(L, 1); - let posi = u_posrelat(lauxlib.luaL_optinteger(L, 2, 1), s.length); - let pose = u_posrelat(lauxlib.luaL_optinteger(L, 3, posi), s.length); + let s = luaL_checkstring(L, 1); + let posi = u_posrelat(luaL_optinteger(L, 2, 1), s.length); + let pose = u_posrelat(luaL_optinteger(L, 3, posi), s.length); - lauxlib.luaL_argcheck(L, posi >= 1, 2, to_luastring("out of range", true)); - lauxlib.luaL_argcheck(L, pose <= s.length, 3, to_luastring("out of range", true)); + luaL_argcheck(L, posi >= 1, 2, to_luastring("out of range", true)); + luaL_argcheck(L, pose <= s.length, 3, to_luastring("out of range", true)); if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= Number.MAX_SAFE_INTEGER) - return lauxlib.luaL_error(L, to_luastring("string slice too long", true)); + return luaL_error(L, to_luastring("string slice too long", true)); let n = (pose - posi) + 1; - lauxlib.luaL_checkstack(L, n, to_luastring("string slice too long", true)); + luaL_checkstack(L, n, to_luastring("string slice too long", true)); n = 0; for (posi -= 1; posi < pose;) { let dec = utf8_decode(s, posi); if (dec === null) - return lauxlib.luaL_error(L, to_luastring("invalid UTF-8 code", true)); - lua.lua_pushinteger(L, dec.code); + return luaL_error(L, to_luastring("invalid UTF-8 code", true)); + lua_pushinteger(L, dec.code); posi = dec.pos; n++; } @@ -178,9 +203,9 @@ const codepoint = function(L) { }; const iter_aux = function(L) { - let s = lauxlib.luaL_checkstring(L, 1); + let s = luaL_checkstring(L, 1); let len = s.length; - let n = lua.lua_tointeger(L, 2) - 1; + let n = lua_tointeger(L, 2) - 1; if (n < 0) /* first iteration? */ n = 0; /* start from here */ @@ -194,18 +219,18 @@ const iter_aux = function(L) { else { let dec = utf8_decode(s, n); if (dec === null || iscont(s[dec.pos])) - return lauxlib.luaL_error(L, to_luastring("invalid UTF-8 code", true)); - lua.lua_pushinteger(L, n + 1); - lua.lua_pushinteger(L, dec.code); + return luaL_error(L, to_luastring("invalid UTF-8 code", true)); + lua_pushinteger(L, n + 1); + lua_pushinteger(L, dec.code); return 2; } }; const iter_codes = function(L) { - lauxlib.luaL_checkstring(L, 1); - lua.lua_pushcfunction(L, iter_aux); - lua.lua_pushvalue(L, 1); - lua.lua_pushinteger(L, 0); + luaL_checkstring(L, 1); + lua_pushcfunction(L, iter_aux); + lua_pushvalue(L, 1); + lua_pushinteger(L, 0); return 3; }; @@ -221,9 +246,9 @@ const funcs = { const UTF8PATT = luastring_of(91, 0, 45, 127, 194, 45, 244, 93, 91, 128, 45, 191, 93, 42); const luaopen_utf8 = function(L) { - lauxlib.luaL_newlib(L, funcs); - lua.lua_pushstring(L, UTF8PATT); - lua.lua_setfield(L, -2, to_luastring("charpattern", true)); + luaL_newlib(L, funcs); + lua_pushstring(L, UTF8PATT); + lua_setfield(L, -2, to_luastring("charpattern", true)); return 1; }; -- cgit v1.2.3-70-g09d2