Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

LUA := $(shell luarocks config variables.LUA)

all: gen check test
all: gen check test perf

dev:
luarocks install tl
Expand Down
38 changes: 30 additions & 8 deletions inspect.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type
local inspect = { Options = {} }


Expand Down Expand Up @@ -54,6 +54,33 @@ local char = string.char
local gsub = string.gsub
local fmt = string.format


local sbavailable, stringbuffer = pcall(require, "string.buffer")
local buffnew
local puts
local render

if sbavailable then
buffnew = stringbuffer.new
puts = function(buf, str)
buf:put(str)
end
render = function(buf)
return buf:get()
end
else
buffnew = function()
return { n = 0 }
end
puts = function(buf, str)
buf.n = buf.n + 1
buf[buf.n] = str
end
render = function(buf)
return table.concat(buf)
end
end

local _rawget
if rawget then
_rawget = rawget
Expand Down Expand Up @@ -211,11 +238,6 @@ local function processRecursive(process,
return processed
end

local function puts(buf, str)
buf.n = buf.n + 1
buf[buf.n] = str
end



local Inspector = {}
Expand Down Expand Up @@ -332,7 +354,7 @@ function inspect.inspect(root, options)
countCycles(root, cycles, depth)

local inspector = setmetatable({
buf = { n = 0 },
buf = buffnew(),
ids = {},
cycles = cycles,
depth = depth,
Expand All @@ -343,7 +365,7 @@ function inspect.inspect(root, options)

inspector:putValue(root)

return table.concat(inspector.buf)
return render(inspector.buf)
end

setmetatable(inspect, {
Expand Down
36 changes: 29 additions & 7 deletions inspect.tl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ local char = string.char
local gsub = string.gsub
local fmt = string.format

-- String buffer support (LuaJIT optimization)
local sbavailable, stringbuffer = pcall(require, "string.buffer")
local buffnew: function(): table
local puts: function(table, string)
local render: function(table): string

if sbavailable then
buffnew = stringbuffer.new
puts = function(buf: table, str: string)
buf:put(str)
end
render = function(buf: table): string
return buf:get()
end
else
buffnew = function(): table
return { n = 0 }
end
puts = function(buf: table, str: string)
buf.n = buf.n as integer + 1
buf[buf.n as integer] = str
end
render = function(buf: table): string
return table.concat(buf as {string})
end
end

local _rawget: function(table, any): any
if rawget then
_rawget = rawget
Expand Down Expand Up @@ -211,11 +238,6 @@ local function processRecursive(process: inspect.ProcessFunction,
return processed
end

local function puts(buf: table, str:string): nil
buf.n = buf.n as integer + 1
buf[buf.n as integer] = str
end

-------------------------------------------------------------------

local type Inspector = record
Expand Down Expand Up @@ -332,7 +354,7 @@ function inspect.inspect(root: any, options: inspect.Options): string
countCycles(root, cycles, depth)

local inspector = setmetatable({
buf = { n = 0 },
buf = buffnew(),
ids = {},
cycles = cycles,
depth = depth,
Expand All @@ -343,7 +365,7 @@ function inspect.inspect(root: any, options: inspect.Options): string

inspector:putValue(root)

return table.concat(inspector.buf as {string})
return render(inspector.buf)
end

setmetatable(inspect, {
Expand Down
Loading