From 3e7c102eefbaae9e6bc839b11bba79aee1c5e040 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 13 Dec 2017 15:13:27 +1100 Subject: Introduce defs.string_of to create string from bytes --- src/defs.js | 3 +++ src/ldump.js | 4 ++-- src/lobject.js | 2 +- tests/defs.js | 14 +++++++------- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/defs.js b/src/defs.js index 268e76e..27498c6 100644 --- a/src/defs.js +++ b/src/defs.js @@ -131,6 +131,8 @@ class lua_Debug { } +const string_of = Array.of; + const is_luastring = function(s) { return Array.isArray(s); }; @@ -422,6 +424,7 @@ module.exports.lua_upvalueindex = lua_upvalueindex; module.exports.thread_status = thread_status; module.exports.is_luastring = is_luastring; module.exports.luastring_cmp = luastring_cmp; +module.exports.string_of = string_of; module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; module.exports.to_uristring = to_uristring; diff --git a/src/ldump.js b/src/ldump.js index 627a252..964d6ff 100644 --- a/src/ldump.js +++ b/src/ldump.js @@ -3,7 +3,7 @@ const defs = require('./defs.js'); const CT = defs.constant_types; -const LUAC_DATA = [25, 147, 13, 10, 26, 10]; +const LUAC_DATA = defs.string_of(25, 147, 13, 10, 26, 10); const LUAC_INT = 0x5678; const LUAC_NUM = 370.5; const LUAC_VERSION = Number.parseInt(defs.LUA_VERSION_MAJOR) * 16 + Number.parseInt(defs.LUA_VERSION_MINOR); @@ -30,7 +30,7 @@ const DumpLiteral = function(s, D) { }; const DumpByte = function(y, D) { - DumpBlock([y], 1, D); + DumpBlock(defs.string_of(y), 1, D); }; const DumpInt = function(x, D) { diff --git a/src/lobject.js b/src/lobject.js index a70866d..707cdf8 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -547,7 +547,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { case char['c']: { let buff = argp[a++]; if (ljstype.lisprint(buff)) - pushstr(L, [buff]); + pushstr(L, defs.string_of(buff)); else luaO_pushfstring(L, defs.to_luastring("<\\%d>", true), buff); break; diff --git a/tests/defs.js b/tests/defs.js index 8c0098a..1a935d6 100644 --- a/tests/defs.js +++ b/tests/defs.js @@ -6,37 +6,37 @@ const unicode_tests = [ { description: "Convert normal ascii string", literal: "foo", - byte_array: ["f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)] + byte_array: defs.string_of("f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)) }, { description: "Convert ascii string containing null byte", literal: "fo\0o", - byte_array: ["f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)] + byte_array: defs.string_of("f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)) }, { description: "Convert string with BMP unicode chars", literal: "Café", - byte_array: [67, 97, 102, 195, 169] + byte_array: defs.string_of(67, 97, 102, 195, 169) }, { description: "Convert string with codepoint in PUA (U+E000 to U+F8FF)", literal: "", - byte_array: [239, 163, 191] + byte_array: defs.string_of(239, 163, 191) }, { description: "Convert string with surrogate pair", literal: "❤️🍾", - byte_array: [226, 157, 164, 239, 184, 143, 240, 159, 141, 190] + byte_array: defs.string_of(226, 157, 164, 239, 184, 143, 240, 159, 141, 190) }, { description: "Convert string with broken surrogate pair", literal: "\uD800a", - byte_array: [237, 160, 128, 97] + byte_array: defs.string_of(237, 160, 128, 97) }, { description: "Convert string with broken surrogate pair at end of string", literal: "\uD823", - byte_array: [237, 160, 163] + byte_array: defs.string_of(237, 160, 163) } ]; -- cgit v1.2.3-54-g00ecf