mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
258e523219 | ||
|
16848d83a5 | ||
|
ca338b8833 | ||
|
ca36473904 | ||
|
717745fe79 | ||
|
688de3368e | ||
|
7412706277 | ||
|
78144dbdb8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
||||
__*
|
||||
*.tmp
|
||||
*.swp
|
@@ -383,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
|
||||
by a space.
|
||||
```lua
|
||||
-- Assuming the file is called "example.lua" and the next line is 12:
|
||||
lume.trace("hello", 1234) -- Prints "[example.lua:12] hello 1234"
|
||||
lume.trace("hello", 1234) -- Prints "example.lua:12: hello 1234"
|
||||
```
|
||||
|
||||
### lume.dostring(str)
|
||||
|
39
lume.lua
39
lume.lua
@@ -7,7 +7,7 @@
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "2.2.0" }
|
||||
local lume = { _version = "2.2.1" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -536,19 +536,36 @@ function lume.lambda(str)
|
||||
end
|
||||
|
||||
|
||||
function lume.serialize(x)
|
||||
local f = { string = function(v) return string.format("%q", v) end,
|
||||
number = tostring, boolean = tostring }
|
||||
f.table = function(t)
|
||||
local serialize
|
||||
|
||||
local serialize_map = {
|
||||
[ "number" ] = tostring,
|
||||
[ "boolean" ] = tostring,
|
||||
[ "nil" ] = tostring,
|
||||
[ "string" ] = function(v) return string.format("%q", v) end,
|
||||
[ "table" ] = function(t, stk)
|
||||
stk = stk or {}
|
||||
if stk[t] then error("circular reference") end
|
||||
local rtn = {}
|
||||
stk[t] = true
|
||||
for k, v in pairs(t) do
|
||||
rtn[#rtn + 1] = "[" .. f[type(k)](k) .. "]=" .. f[type(v)](v) .. ","
|
||||
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
|
||||
end
|
||||
return "{" .. table.concat(rtn) .. "}"
|
||||
stk[t] = nil
|
||||
return "{" .. table.concat(rtn, ",") .. "}"
|
||||
end
|
||||
local err = function(t,k) error("unsupported serialize type: " .. k) end
|
||||
setmetatable(f, { __index = err })
|
||||
return f[type(x)](x)
|
||||
}
|
||||
|
||||
setmetatable(serialize_map, {
|
||||
__index = function(t, k) error("unsupported serialize type: " .. k) end
|
||||
})
|
||||
|
||||
serialize = function(x, stk)
|
||||
return serialize_map[type(x)](x, stk)
|
||||
end
|
||||
|
||||
function lume.serialize(x)
|
||||
return serialize(x)
|
||||
end
|
||||
|
||||
|
||||
@@ -618,7 +635,7 @@ end
|
||||
|
||||
function lume.trace(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
|
||||
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
||||
for i = 1, select("#", ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local tester = require "tester"
|
||||
local tester = require "util.tester"
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
|
||||
@@ -535,7 +535,7 @@ tests["lume.trace"] = function()
|
||||
local oldprint = print
|
||||
local file, line, msg
|
||||
print = function(x)
|
||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
||||
file, line, msg = x:match("(.-):(.-): (.*)")
|
||||
end
|
||||
lume.trace("Hi world", 123.456, 1, nil)
|
||||
print = oldprint
|
Reference in New Issue
Block a user