From dbcb965525833d167370fdade4ff8da7126be8a6 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 31 Mar 2017 11:15:46 +0200 Subject: Use correct sprintf package --- package.json | 6 +++--- src/lauxlib.js | 6 +++--- src/lstrlib.js | 2 +- tests/single.lua | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 0167a15..c5c0cf4 100644 --- a/package.json +++ b/package.json @@ -24,16 +24,16 @@ }, "homepage": "https://github.com/giann/fengari#readme", "devDependencies": { - "babel-tape-runner": "^2.0.1", "docdash": "^0.4.0", - "faucet": "0.0.1", "js-beautify": "^1.6.8", "jsdoc": "^3.4.3", "tap-bail": "0.0.0", + "babel-tape-runner": "^2.0.1", + "faucet": "0.0.1", "tmp": "0.0.31" }, "dependencies": { "seedrandom": "^2.4.2", - "sprintf-js": "^1.0.3" + "sprintf-js": "alexei/sprintf.js" } } diff --git a/src/lauxlib.js b/src/lauxlib.js index 90cff5c..5a13379 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -292,15 +292,15 @@ const luaL_len = function(L, idx) { lapi.lua_len(L, idx); let l = lapi.lua_tointegerx(L, -1); if (l === false) - luaL_error(L, "object length is not an integer"); + luaL_error(L, lua.to_luastring("object length is not an integer")); lapi.lua_pop(L, 1); /* remove object */ return l; }; const luaL_tolstring = function(L, idx) { - if (luaL_callmeta(L, idx, "__tostring")) { + if (luaL_callmeta(L, idx, lua.to_luastring("__tostring"))) { if (!lapi.lua_isstring(L, -1)) - luaL_error(L, "'__tostring' must return a string"); + luaL_error(L, lua.to_luastring("'__tostring' must return a string")); } else { switch(lapi.lua_type(L, idx)) { case CT.LUA_TNUMBER: diff --git a/src/lstrlib.js b/src/lstrlib.js index 9104078..90341e7 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -1,7 +1,7 @@ "use strict"; const assert = require('assert'); -const sprintf = require('sprintf'); +const sprintf = require('sprintf-js').sprintf; const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); diff --git a/tests/single.lua b/tests/single.lua index c1241de..9e7d0cf 100644 --- a/tests/single.lua +++ b/tests/single.lua @@ -190,3 +190,22 @@ end assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") checkerror("contains zeros", string.format, "%10s", "\0") + +-- format x tostring +assert(string.format("%s %s", nil, true) == "nil true") +assert(string.format("%s %.4s", false, true) == "false true") +assert(string.format("%.3s %.3s", false, true) == "fal tru") +local m = setmetatable({}, {__tostring = function () return "hello" end, + __name = "hi"}) +assert(string.format("%s %.10s", m, m) == "hello hello") +getmetatable(m).__tostring = nil -- will use '__name' from now on +assert(string.format("%.4s", m) == "hi: ") + +getmetatable(m).__tostring = function () return {} end +checkerror("'__tostring' must return a string", tostring, m) + +assert(string.format("%x", 0.0) == "0") +assert(string.format("%02x", 0.0) == "00") +assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") +assert(string.format("%+08d", 31501) == "+0031501") +assert(string.format("%+08d", -30927) == "-0030927") -- cgit v1.2.3-54-g00ecf