1
0
forked from redo/BlockLua

improve readme, improve std lib

This commit is contained in:
Redo
2025-10-04 01:44:25 -05:00
parent eaafb42317
commit 1a4c7bfefc
6 changed files with 129 additions and 75 deletions

View File

@@ -536,7 +536,7 @@ end
function bl.exec(file)
return valFromTs(_bllua_ts.call('exec', file))
end
function bl.tobool(val)
function bl.boolean(val)
return val~=nil and
val~=false and
--val~='' and
@@ -734,6 +734,9 @@ function bl.hook(pkg, name, time, func)
updateHook(pkg, name, bl._hooks[pkg][name])
activatePackage(pkg)
end
local function tableEmpty(t)
return next(t)~=nil
end
function bl.unhook(pkg, name, time)
if not isValidFuncName(pkg) then
error('bl.unhook: argument #1: invalid package name \''..tostring(pkg)..'\'', 2) end
@@ -766,7 +769,7 @@ function bl.unhook(pkg, name, time)
if time~='before' and time~='after' then
error('bl.unhook: argument #3: time must be nil, \'before\', or \'after\'', 2) end
bl._hooks[pkg][name][time] = nil
if table.empty(bl._hooks[pkg][name]) and table.empty(bl._hooks[pkg]) then
if tableEmpty(bl._hooks[pkg][name]) and tableEmpty(bl._hooks[pkg]) then
bl._hooks[pkg] = nil
deactivatePackage(pkg)
updateHook(pkg, name, {})
@@ -871,10 +874,15 @@ function bl.radiusSearch(pos, radius, mask)
end
-- Print/Talk/Echo
local maxTsArgLen = 8192
local function valsToString(vals)
local strs = {}
for i,v in ipairs(vals) do
strs[i] = table.tostring(v)
local tstr = table.tostring(v)
if #tstr>maxTsArgLen then
tstr = tostring(v)
end
strs[i] = tstr
end
return table.concat(strs, ' ')
end
@@ -889,11 +897,12 @@ bl.talk = function(...)
_bllua_ts.call('talk', str)
end
-- bl.new and bl.datablock
local function createTsObj(keyword, class, name, inherit, props)
local propsT = {}
for k,v in pairs(props) do
if not isValidFuncName(k) then
error('bl.new/datablock: invalid property name \''..k..'\'') end
error('bl.new/bl.datablock: invalid property name \''..k..'\'') end
table.insert(propsT, k..'="'..valToTs(v)..'";')
end
@@ -903,7 +912,7 @@ local function createTsObj(keyword, class, name, inherit, props)
table.concat(propsT)..'};')
local obj = toTsObject(objS)
if not obj then
error('bl.new/datablock: failed to create object', 3) end
error('bl.new/bl.datablock: failed to create object', 3) end
return obj
end
@@ -926,7 +935,7 @@ local function parseTsDecl(decl)
isValidFuncName(class) and
(name==nil or isValidFuncName(name)) and
(inherit==nil or isValidFuncName(inherit)) ) then
error('bl.new/datablock: invalid decl \''..decl..'\'\n'..
error('bl.new/bl.datablock: invalid decl \''..decl..'\'\n'..
'must be of the format: \'className\', \'className name\', '..
'\'className :inherit\', or \'className name:inherit\'', 3) end
return class, name, inherit
@@ -937,6 +946,7 @@ function bl.new(decl, props)
end
function bl.datablock(decl, props)
local class, name, inherit = parseTsDecl(decl)
if not name then error('bl.datablock: must specify a name', 2) end
return createTsObj('datablock', class, name, inherit, props)
end

View File

@@ -5,8 +5,7 @@
-- Table / List
-- Whether a table contains no keys
function table.empty(t)
for _,_ in pairs(t) do return false end
return true
return next(t)~=nil
end
-- Apply a function to each key in a table
function table.map(f, ...)
@@ -41,18 +40,6 @@ function table.reverse(l)
for i=1,#l do m[#l-i+1] = l[i] end
return m
end
-- Convert i->v to v->true
function table.values(l)
local u = {}
for _,v in ipairs(l) do u[v] = true end
return u
end
-- Make a list of keys
function table.keys(t)
local u = {}
for k,_ in pairs(t) do table.insert(u, k) end
return u
end
-- Whether a table is a list/array (has only monotonic integer keys)
function table.islist(t)
local n = 0
@@ -244,8 +231,8 @@ function string.bytes(s)
end
-- Trim leading and trailing whitespace
function string.trim(s, ws)
ws = ws or '[ \t\r\t]'
return s:gsub('^'..ws..'+', ''):gsub(ws..'+$', '')..''
ws = ws or ' \t\r\n'
return s:gsub('^['..ws..']+', ''):gsub('['..ws..']+$', '')..''
end
-- String slicing and searching using [] operator
local str_meta = getmetatable('')
@@ -325,7 +312,7 @@ end
io = io or {}
-- Read entire file at once, return nil,err if access failed
function io.readfile(filename)
function io.readall(filename)
local fi,err = io.open(filename, 'rb')
if not fi then return nil,err end
local s = fi:read("*a")
@@ -333,7 +320,7 @@ function io.readfile(filename)
return s
end
-- Write data to file all at once, return true if success / false,err if failure
function io.writefile(filename, data)
function io.writeall(filename, data)
local fi,err = io.open(filename, 'wb')
if not fi then return false,err end
fi:write(data)

View File

@@ -127,7 +127,7 @@ local vector_meta = {
for i = 1, len do
table.insert(st, tostring(v1[i]))
end
return '{ '..table.concat(st, ', ')..' }'
return 'vector{ '..table.concat(st, ', ')..' }'
end,
unpack = function(v1) return unpack(v1) end,
floor = vector_opn0n('floor', function(x1) return math.floor(x1) end),
@@ -162,10 +162,10 @@ local vector_meta = {
else error('vector rotateByAngleId: invalid rotation '..r, 2) end
return v2
end,
rotate2d = function(v, r)
rotateZ = function(v, r)
--vector_check(v, 2, 'rotate2d')
if type(r)~='number' then
error('vector rotate2d: invalid rotation '..tostring(r), 2) end
error('vector rotateZ: invalid rotation '..tostring(r), 2) end
local len = math.sqrt(v[1]^2 + v[2]^2)
local ang = math.atan2(v[2], v[1]) + r
local v2 = vector_new{ math.cos(ang)*len, math.sin(ang)*len }