Skip to content

Commit

Permalink
Remove wire_expression2_debug, type check field from registerType
Browse files Browse the repository at this point in the history
  • Loading branch information
Vurv78 committed Oct 17, 2023
1 parent 7eab44e commit 126c43b
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 444 deletions.
5 changes: 1 addition & 4 deletions lua/entities/gmod_wire_expression2/core/angle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ Angle support
registerType("angle", "a", Angle(0, 0, 0),
nil,
function(self, output) return Angle(output) end,
function(retval)
if isangle(retval) then return end
error("Return value is not an Angle, but a "..type(retval).."!", 0)
end,
nil,
function(v)
return not isangle(v)
end
Expand Down
4 changes: 1 addition & 3 deletions lua/entities/gmod_wire_expression2/core/array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ registerType("array", "r", {},
return ret
end,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return !istable(v)
end
Expand Down
6 changes: 1 addition & 5 deletions lua/entities/gmod_wire_expression2/core/bone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ E2Lib.isValidBone = isValidBone
registerType("bone", "b", nil,
nil,
nil,
function(retval)
if retval == nil then return end
if type(retval) ~= "PhysObj" then error("Return value is neither nil nor a PhysObj, but a "..type(retval).."!",0) end
if not bone2entity[retval] then error("Return value is not a registered bone!",0) end
end,
nil,
function(b)
return not isValidBone(b)
end
Expand Down
5 changes: 1 addition & 4 deletions lua/entities/gmod_wire_expression2/core/complex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ __e2setcost(2)
registerType("complex", "c", { 0, 0 },
function(self, input) return { input[1], input[2] } end,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
if #retval ~= 2 then error("Return value does not have exactly 2 entries!",0) end
end,
nil,
function(v)
return !istable(v) or #v ~= 2
end
Expand Down
6 changes: 1 addition & 5 deletions lua/entities/gmod_wire_expression2/core/custom/effects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ end
registerType("effect", "xef", nil,
nil,
nil,
function(retval)
if retval == nil then return end
local _type = type(retval)
if _type~="CEffectData" then error("Return value is neither nil nor a CEffectData, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return type(v)~="CEffectData"
end
Expand Down
6 changes: 1 addition & 5 deletions lua/entities/gmod_wire_expression2/core/damage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ local M_CTakeDamageInfo = FindMetaTable("CTakeDamageInfo")

registerType("damage", "xdm", nil,
nil, nil,
function(retval)
if retval == nil then return end
if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end
if getmetatable(retval) ~= M_CTakeDamageInfo then error("Return value is not a CTakeDamageInfo!", 0) end
end,
nil,
function(v)
return not istable(v) or getmetatable(v) ~= M_CTakeDamageInfo
end
Expand Down
46 changes: 0 additions & 46 deletions lua/entities/gmod_wire_expression2/core/e2lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,52 +252,6 @@ function E2Lib.canModifyPlayer(self, ply)
return isOwner(self, vehicle)
end

-- ------------------------ type guessing ------------------------------------------

local type_lookup = {
number = "n",
string = "s",
Vector = "v",
PhysObj = "b",
}
local table_length_lookup = {
[2] = "xv2",
[3] = "v",
[4] = "xv4",
[9] = "m",
[16] = "xm4",
}

function E2Lib.guess_type(value)
local vtype = type(value)
if type_lookup[vtype] then return type_lookup[vtype] end
if IsValid(value) then return "e" end
if value.EntIndex then return "e" end
if vtype == "table" then
if table_length_lookup[#value] then return table_length_lookup[#value] end
if value.HitPos then return "xrd" end
end

for typeid, v in pairs(wire_expression_types2) do
if v[5] then
local ok = pcall(v[5], value)
if ok then return typeid end
end
end

-- TODO: more type guessing here

return "" -- empty string = unknown type, for now.
end

-- Types that cannot possibly be guessed correctly:
-- angle (will be reported as vector)
-- matrix2 (will be reported as vector4)
-- wirelink (will be reported as entity)
-- complex (will be reported as vector2)
-- quaternion (will be reported as vector4)
-- all kinds of nil stuff

-- ------------------------ list filtering -------------------------------------------------

function E2Lib.filterList(list, criterion)
Expand Down
6 changes: 1 addition & 5 deletions lua/entities/gmod_wire_expression2/core/entity.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
registerType("entity", "e", nil,
nil,
function(self,output) return output or NULL end,
function(retval)
if IsValid(retval) then return end
if retval == nil then return end
if not retval.EntIndex then error("Return value is neither nil nor an Entity, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return not isentity(v)
end
Expand Down
4 changes: 1 addition & 3 deletions lua/entities/gmod_wire_expression2/core/globalvars.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ end)
registerType( "gtable", "xgt", {},
function(self) self.entity:Error("You may not input a gtable.") end,
function(self) self.entity:Error("You may not output a gtable.") end,
function(retval)
if !istable(retval) then error("Return value is not a gtable, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return !istable(v)
end
Expand Down
80 changes: 7 additions & 73 deletions lua/entities/gmod_wire_expression2/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,6 @@ AddCSLuaFile()
Andreas "Syranide" Svensson, [email protected]
]]

-- functions to type-check function return values.

local wire_expression2_debug = CreateConVar("wire_expression2_debug", 0, 0)

if SERVER then
cvars.AddChangeCallback("wire_expression2_debug", function(CVar, PreviousValue, NewValue)
if PreviousValue == NewValue then return end
wire_expression2_reload()
end)
end

--- This function ensures that the given function shows up by the given name in stack traces.
--- It does so by eval'ing a generated block of code which invokes the actual function.
--- Tail recursion optimization is specifically avoided by introducing a local variable in the generated code block.
local function namefunc(func, name)
-- Filter the name
name = name:gsub("[^A-Za-z_0-9]", "_")

-- RunString doesn't have a return value, so we need to go via a global variable
wire_expression2_namefunc = func
RunString(([[
local %s = wire_expression2_namefunc
function wire_expression2_namefunc(...)
local ret = %s(...)
return ret
end
]]):format(name, name))
local ret = wire_expression2_namefunc
wire_expression2_namefunc = nil

-- Now ret contains the wrapped function and we can just return it.
return ret
end

-- Installs a typecheck in a function identified by the given signature.
local function makecheck(signature)
if signature == "op:seq()" then return end
local name = signature:match("^([^(]*)")
local entry = wire_expression2_funcs[signature]
local oldfunc, signature, rets, func = entry.oldfunc, unpack(entry)

if oldfunc then return end
oldfunc = namefunc(func, "e2_" .. name)

function func(...)
local retval = oldfunc(...)

local checker = wire_expression_types2[rets][5]
if not checker then return retval end

local ok, msg = pcall(checker, retval)
if ok then return retval end
debug.Trace()
local full_signature = E2Lib.generate_signature(signature, rets)
error(string.format("Type check for function %q failed: %s\n", full_signature, msg), 0)

return retval
end

entry[3] = func
entry.oldfunc = oldfunc
end

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

function wire_expression2_reset_extensions()
wire_expression_callbacks = {
construct = {},
Expand All @@ -79,13 +14,10 @@ function wire_expression2_reset_extensions()
}

wire_expression_types = {}
wire_expression_types2 = {
[""] = {
[5] = function(retval) if retval ~= nil then error("Return value of void function is not nil.", 0) end end
}
}
wire_expression_types2 = { [""] = {} } -- TODO: do we really need ""? :\
wire_expression2_funcs = {}
wire_expression2_funclist = {}

if CLIENT then wire_expression2_funclist_lowercase = {} end
wire_expression2_constants = {}
end
Expand All @@ -100,7 +32,7 @@ end
---@param def T | nil
---@param input_serialize (fun(self, input: any): T)?
---@param output_serialize (fun(self, output: any): T)?
---@param type_check (fun(v: any))?
---@param type_check (fun(v: any))? # DEPRECATED, NO LONGER USED. Can pass nil here safely.
---@param is_invalid (fun(v: any): boolean)?
function registerType(name, id, def, input_serialize, output_serialize, type_check, is_invalid, ...)
if not isValidTypeId(id) then
Expand Down Expand Up @@ -223,7 +155,6 @@ function registerOperator(name, pars, rets, func, cost, argnames, attributes)
local signature = "op:" .. name .. "(" .. pars .. ")"

wire_expression2_funcs[signature] = { signature, rets, func, cost or tempcost, argnames = argnames, attributes = attributes }
if wire_expression2_debug:GetBool() then makecheck(signature) end
end

function registerFunction(name, pars, rets, func, cost, argnames, attributes)
Expand All @@ -239,11 +170,14 @@ function registerFunction(name, pars, rets, func, cost, argnames, attributes)
wire_expression2_funcs[signature] = { signature, rets, func, cost or tempcost, argnames = argnames, extension = E2Lib.currentextension, attributes = attributes }

wire_expression2_funclist[name] = true
if wire_expression2_debug:GetBool() then makecheck(signature) end
end

function E2Lib.registerConstant(name, value)
if name:sub(1, 1) ~= "_" then name = "_" .. name end

local ty = type(value)
assert(ty == "number" or ty == "string", "Invalid value passed to registerConstant (must be number or string)")

wire_expression2_constants[name] = value
end

Expand Down
15 changes: 3 additions & 12 deletions lua/entities/gmod_wire_expression2/core/matrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ registerType("matrix2", "xm2", { 0, 0,
return ret
end,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
if #retval ~= 4 then error("Return value does not have exactly 4 entries!",0) end
end,
nil,
function(v)
return !istable(v) or #v ~= 4
end
Expand Down Expand Up @@ -353,10 +350,7 @@ registerType("matrix", "m", { 0, 0, 0,
return ret
end,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
if #retval ~= 9 then error("Return value does not have exactly 9 entries!",0) end
end,
nil,
function(v)
return !istable(v) or #v ~= 9
end
Expand Down Expand Up @@ -878,10 +872,7 @@ registerType("matrix4", "xm4", { 0, 0, 0, 0,
return ret
end,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
if #retval ~= 16 then error("Return value does not have exactly 16 entries!",0) end
end,
nil,
function(v)
return !istable(v) or #v ~= 16
end
Expand Down
6 changes: 2 additions & 4 deletions lua/entities/gmod_wire_expression2/core/number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ local tanh = math.tanh
registerType("normal", "n", 0,
nil,
nil,
function(retval)
if !isnumber(retval) then error("Return value is not a number, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return !isnumber(v)
return not isnumber(v)
end
)

Expand Down
15 changes: 4 additions & 11 deletions lua/entities/gmod_wire_expression2/core/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,16 @@ local M_CMoveData = FindMetaTable("CMoveData")

registerType("usercmd", "xuc", nil,
nil, nil,
function(retval)
if retval == nil then return end
if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end
if getmetatable(retval) ~= M_CUserCmd then error("Return value is not a CUserCmd!", 0) end
end,
nil,
function(v)
return not istable(v) or getmetatable(v) ~= M_CUserCmd
end
)

registerType("movedata", "xmv", nil,
nil, nil,
function(retval)
if retval == nil then return end
if not istable(retval) then error("Return value is neither nil nor a table, but a " .. type(retval) .. "!",0) end
if getmetatable(retval) ~= M_CMoveData then error("Return value is not a CMoveData!", 0) end
end,
nil,
nil,
nil,
function(v)
return not istable(v) or getmetatable(v) ~= M_CMoveData
end
Expand Down
5 changes: 1 addition & 4 deletions lua/entities/gmod_wire_expression2/core/quaternion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ local rad2deg = 180/math.pi
registerType("quaternion", "q", { 0, 0, 0, 0 },
nil,
nil,
function(retval)
if !istable(retval) then error("Return value is not a table, but a "..type(retval).."!",0) end
if #retval ~= 4 then error("Return value does not have exactly 4 entries!",0) end
end,
nil,
function(v)
return !istable(v) or #v ~= 4
end
Expand Down
5 changes: 1 addition & 4 deletions lua/entities/gmod_wire_expression2/core/ranger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
registerType("ranger", "xrd", nil,
nil,
nil,
function(retval)
if retval == nil then return end
if !istable(retval) then error("Return value is neither nil nor a table, but a "..type(retval).."!",0) end
end,
nil,
function(v)
return !istable(v) or not v.HitPos
end
Expand Down
Loading

0 comments on commit 126c43b

Please sign in to comment.