Lunatic: prefer to use ctype objects instead of C type strings.

Pass types via ffi.typeof() instead of declaring them in the
global namespace when possible.

git-svn-id: https://svn.eduke32.com/eduke32@3437 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-01-26 17:07:53 +00:00
parent 9d7d58d678
commit 385095be76
6 changed files with 24 additions and 31 deletions

View file

@ -1,4 +1,4 @@
-- Implementation of a bound-checked array type factory for LuaJIT. -- Implementation of a bound-checked array type factory for LuaJIT 2.0 or later.
-- --
-- Usage example: -- Usage example:
-- --
@ -36,7 +36,7 @@ function flatten_array(nelts, rng)
return table.concat(strtab) return table.concat(strtab)
end end
-- ct = bcarray.new(basetype, numelts, showname [, typename] [, rng] [, mtadd]) -- ctype = bcarray.new(basetype, numelts, showname [, typename] [, rng] [, mtadd])
-- (optional fields may be nil) -- (optional fields may be nil)
-- --
-- <numelts>: Number of elements in array (small number) -- <numelts>: Number of elements in array (small number)
@ -44,7 +44,7 @@ end
-- <typename>: If non-nil, the name under which the derived type is typedef'd -- <typename>: If non-nil, the name under which the derived type is typedef'd
-- <rng>: Random generator state + method :getu32(). If nil, then members are -- <rng>: Random generator state + method :getu32(). If nil, then members are
-- named _a1, _a2, ... -- named _a1, _a2, ...
-- <mtadd>: A table containing functions __index and/or __index. They are -- <mtadd>: A table containing functions __index and/or __newindex. They are
-- called first and the bound-checking ones are tail-called then. -- called first and the bound-checking ones are tail-called then.
function new(basetype, numelts, showname, typename, rng, mtadd) function new(basetype, numelts, showname, typename, rng, mtadd)
local eltptr_t = ffi.typeof("$ *", ffi.typeof(basetype)) local eltptr_t = ffi.typeof("$ *", ffi.typeof(basetype))

View file

@ -16,11 +16,7 @@ local tostring = tostring
module(...) module(...)
ffi.cdef[[ local bitar_ct = ffi.typeof("struct { const double maxbidx, maxidx; const intptr_t arptr; }")
struct bitar { const double maxbidx, maxidx; const intptr_t arptr; }
]]
local bitar_ct = ffi.typeof("struct bitar")
local ptr_to_int = ffi.typeof("int32_t *") local ptr_to_int = ffi.typeof("int32_t *")
local anchor = {} local anchor = {}
@ -216,7 +212,7 @@ local mt = {
end, end,
} }
local bitar = ffi.metatype("struct bitar", mt) local bitar = ffi.metatype(bitar_ct, mt)
-- Create new bit array. -- Create new bit array.
function new(maxbidx, initval) function new(maxbidx, initval)

View file

@ -140,5 +140,8 @@ if (string.dump) then
print(p-p) -- test set difference print(p-p) -- test set difference
print(-p) print(-p)
--]] --]]
-- Set difference of self with self is the same as set intersection of self
-- with complement of self:
assert(p-p == p*(-p)) assert(p-p == p*(-p))
end end

View file

@ -290,20 +290,12 @@ typedef struct
]].. mangle_arrays(ACTOR_STRUCT) ..[[ ]].. mangle_arrays(ACTOR_STRUCT) ..[[
actor_t; actor_t;
typedef struct
]].. strip_const(ACTOR_STRUCT).. [[
actor_u_t; // The _u_t versions are unrestricted variants for internal use.
typedef struct { int32_t _p; } weaponaccess_t; typedef struct { int32_t _p; } weaponaccess_t;
typedef struct typedef struct
]].. mangle_arrays(DUKEPLAYER_STRUCT) ..[[ ]].. mangle_arrays(DUKEPLAYER_STRUCT) ..[[
DukePlayer_t; DukePlayer_t;
typedef struct
]].. strip_const(DUKEPLAYER_STRUCT) ..[[
DukePlayer_u_t;
typedef struct { typedef struct {
uint32_t bits; // 4b uint32_t bits; // 4b
int16_t fvel, svel; // 4b int16_t fvel, svel; // 4b
@ -619,8 +611,9 @@ local function check_literal_am(am, typename)
end end
end end
local actor_ptr_ct = ffi.typeof("actor_u_t *") -- an unrestricted actor_t pointer -- An unrestricted actor_t pointer, for internal use:
local player_ptr_ct = ffi.typeof("DukePlayer_u_t *") local actor_ptr_ct = ffi.typeof("$ *", ffi.typeof("struct"..strip_const(ACTOR_STRUCT)))
local player_ptr_ct = ffi.typeof("$ *", ffi.typeof("struct"..strip_const(DUKEPLAYER_STRUCT)))
local con_action_ct = ffi.typeof("con_action_t") local con_action_ct = ffi.typeof("con_action_t")
local con_move_ct = ffi.typeof("con_move_t") local con_move_ct = ffi.typeof("con_move_t")
local con_ai_ct = ffi.typeof("con_ai_t") local con_ai_ct = ffi.typeof("con_ai_t")

View file

@ -257,7 +257,7 @@ local ivec3_mt = {
return ivec3_(v.x, v.y, v.z-zofs) return ivec3_(v.x, v.y, v.z-zofs)
end, end,
} }
ivec3_ = ffi.metatype("vec3_t", ivec3_mt) ivec3_ = ffi.metatype(vec3_ct, ivec3_mt)
local xor = bit.bxor local xor = bit.bxor
local wallsofsec -- fwd-decl local wallsofsec -- fwd-decl

View file

@ -10,10 +10,9 @@ local error = error
module(...) module(...)
ffi.cdef[[ local dvec2_t = ffi.typeof("struct { double x, y; }")
typedef struct { double x, y; } dvec2_t; local dvec3_t = ffi.typeof("struct { double x, y, z; }")
typedef struct { double x, y, z; } dvec3_t;
]]
local vec2_ local vec2_
local vec2_mt = { local vec2_mt = {
@ -40,6 +39,8 @@ local vec2_mt = {
end, end,
__eq = function(a,b) __eq = function(a,b)
-- XXX: will error if <a> is not a ctype (can only happen if __eq was
-- called by <b>)
return (ffi.istype(a,b) and a.x==b.x and a.y==b.y) return (ffi.istype(a,b) and a.x==b.x and a.y==b.y)
end, end,
@ -77,6 +78,7 @@ local vec3_mt = {
end, end,
__eq = function(a,b) __eq = function(a,b)
-- XXX: see vec2
return (ffi.istype(a,b) and a.x==b.x and a.y==b.y and a.z==b.z) return (ffi.istype(a,b) and a.x==b.x and a.y==b.y and a.z==b.z)
end, end,
@ -92,7 +94,7 @@ local vec3_mt = {
-- VEC2 user data constructor. -- VEC2 user data constructor.
-- * vec2(<table>), <table> should be indexable with "x" and "y" -- * vec2(<table>), <table> should be indexable with "x" and "y"
-- * vec2(x, y), assuming that x and y are numbers -- * vec2(x, y), assuming that x and y are numbers
vec2_ = ffi.metatype("dvec2_t", vec2_mt) vec2_ = ffi.metatype(dvec2_t, vec2_mt)
vec2 = vec2_ vec2 = vec2_
-- Returns a vec2 from anything indexable with "x" and "y" -- Returns a vec2 from anything indexable with "x" and "y"
@ -100,7 +102,7 @@ vec2 = vec2_
function tovec2(t) return vec2(t.x, t.y) end function tovec2(t) return vec2(t.x, t.y) end
-- Same for vec3 -- Same for vec3
vec3_ = ffi.metatype("dvec3_t", vec3_mt) vec3_ = ffi.metatype(dvec3_t, vec3_mt)
vec3 = vec3_ vec3 = vec3_
function tovec3(t) return vec3(t.x, t.y, t.z) end function tovec3(t) return vec3(t.x, t.y, t.z) end
@ -108,11 +110,10 @@ function tovec3(t) return vec3(t.x, t.y, t.z) end
-- integer values, e.g. geom.ivec3(x, y, z) is a reasonable way to round -- integer values, e.g. geom.ivec3(x, y, z) is a reasonable way to round
-- a vec3. It can be also used as the RHS to the vec2/vec3 arithmetic -- a vec3. It can be also used as the RHS to the vec2/vec3 arithmetic
-- methods. -- methods.
ffi.cdef[[ -- NOTE: We must have a typedef with that exact name, because for
#pragma pack(push,1) -- Lunatic (i.e. not stand-alone), it is a duplicate (and ignored)
typedef struct { int32_t x, y, z; } vec3_t; -- declaration for an already metatype'd type.
#pragma pack(pop) ffi.cdef "typedef struct { int32_t x, y, z; } vec3_t;"
]]
ivec3 = ffi.typeof("vec3_t") ivec3 = ffi.typeof("vec3_t")