use proper types in lua

* ints changed to cint
* the source has been cleaned up with {.push/pop.}
* lua 5.2 has been removed (it is incompatible)
This commit is contained in:
Billingsly Wetherfordshire 2013-06-06 03:49:31 -06:00
commit 516e0cb71a

View file

@ -26,9 +26,9 @@ import
proc pushstring*(L: PState, s: string) proc pushstring*(L: PState, s: string)
# compatibilty macros # compatibilty macros
proc getn*(L: PState, n: int): int proc getn*(L: PState, n: cint): cint
# calls lua_objlen # calls lua_objlen
proc setn*(L: PState, t, n: int) proc setn*(L: PState, t, n: cint)
# does nothing! # does nothing!
type type
Treg*{.final.} = object Treg*{.final.} = object
@ -37,58 +37,45 @@ type
Preg* = ptr Treg Preg* = ptr Treg
proc openlib*(L: PState, libname: cstring, lr: Preg, nup: int){.cdecl,
dynlib: lua.LIB_NAME, importc: "luaL_openlib".} {.push callConv: cdecl, dynlib: lua.LIB_NAME.}
proc register*(L: PState, libname: cstring, lr: Preg){.cdecl, {.push importc: "luaL_$1".}
dynlib: lua.LIB_NAME, importc: "luaL_register".}
proc getmetafield*(L: PState, obj: int, e: cstring): int{.cdecl, proc openlib*(L: PState, libname: cstring, lr: Preg, nup: cint)
dynlib: lua.LIB_NAME, importc: "luaL_getmetafield".} proc register*(L: PState, libname: cstring, lr: Preg)
proc callmeta*(L: PState, obj: int, e: cstring): int{.cdecl,
dynlib: LIB_NAME, importc: "luaL_callmeta".} proc getmetafield*(L: PState, obj: cint, e: cstring): cint
proc typerror*(L: PState, narg: int, tname: cstring): int{.cdecl, proc callmeta*(L: PState, obj: cint, e: cstring): cint
dynlib: LIB_NAME, importc: "luaL_typerror".} proc typerror*(L: PState, narg: cint, tname: cstring): cint
proc argerror*(L: PState, numarg: int, extramsg: cstring): int{.cdecl, proc argerror*(L: PState, numarg: cint, extramsg: cstring): cint
dynlib: LIB_NAME, importc: "luaL_argerror".} proc checklstring*(L: PState, numArg: cint, len: ptr int): cstring
proc checklstring*(L: PState, numArg: int, len: ptr int): cstring{.cdecl, proc optlstring*(L: PState, numArg: cint, def: cstring, len: ptr cint): cstring
dynlib: LIB_NAME, importc: "luaL_checklstring".} proc checknumber*(L: PState, numArg: cint): Number
proc optlstring*(L: PState, numArg: int, def: cstring, len: ptr int): cstring{. proc optnumber*(L: PState, nArg: cint, def: Number): Number
cdecl, dynlib: LIB_NAME, importc: "luaL_optlstring".} proc checkinteger*(L: PState, numArg: cint): Integer
proc checknumber*(L: PState, numArg: int): Number{.cdecl, proc optinteger*(L: PState, nArg: cint, def: Integer): Integer
dynlib: LIB_NAME, importc: "luaL_checknumber".} proc checkstack*(L: PState, sz: cint, msg: cstring)
proc optnumber*(L: PState, nArg: int, def: Number): Number{.cdecl, proc checktype*(L: PState, narg, t: cint)
dynlib: LIB_NAME, importc: "luaL_optnumber".}
proc checkinteger*(L: PState, numArg: int): Integer{.cdecl, proc checkany*(L: PState, narg: cint)
dynlib: LIB_NAME, importc: "luaL_checkinteger".} proc newmetatable*(L: PState, tname: cstring): cint
proc optinteger*(L: PState, nArg: int, def: Integer): Integer{.
cdecl, dynlib: LIB_NAME, importc: "luaL_optinteger".} proc checkudata*(L: PState, ud: cint, tname: cstring): Pointer
proc checkstack*(L: PState, sz: int, msg: cstring){.cdecl, proc where*(L: PState, lvl: cint)
dynlib: LIB_NAME, importc: "luaL_checkstack".} proc error*(L: PState, fmt: cstring): cint{.varargs.}
proc checktype*(L: PState, narg, t: int){.cdecl, dynlib: LIB_NAME, proc checkoption*(L: PState, narg: cint, def: cstring, lst: cstringArray): cint
importc: "luaL_checktype".}
proc checkany*(L: PState, narg: int){.cdecl, dynlib: LIB_NAME, proc unref*(L: PState, t, theref: cint)
importc: "luaL_checkany".} proc loadfile*(L: PState, filename: cstring): cint
proc newmetatable*(L: PState, tname: cstring): int{.cdecl, proc loadbuffer*(L: PState, buff: cstring, size: cint, name: cstring): cint
dynlib: LIB_NAME, importc: "luaL_newmetatable".} proc loadstring*(L: PState, s: cstring): cint
proc checkudata*(L: PState, ud: int, tname: cstring): Pointer{.cdecl, proc newstate*(): PState
dynlib: LIB_NAME, importc: "luaL_checkudata".}
proc where*(L: PState, lvl: int){.cdecl, dynlib: LIB_NAME, {.pop.}
importc: "luaL_where".} proc reference*(L: PState, t: cint): cint{.importc: "luaL_ref".}
proc error*(L: PState, fmt: cstring): int{.cdecl, varargs,
dynlib: LIB_NAME, importc: "luaL_error".} {.pop.}
proc checkoption*(L: PState, narg: int, def: cstring, lst: cstringArray): int{.
cdecl, dynlib: LIB_NAME, importc: "luaL_checkoption".}
proc reference*(L: PState, t: int): int{.cdecl, dynlib: LIB_NAME,
importc: "luaL_ref".}
proc unref*(L: PState, t, theref: int){.cdecl, dynlib: LIB_NAME,
importc: "luaL_unref".}
proc loadfile*(L: PState, filename: cstring): int{.cdecl,
dynlib: LIB_NAME, importc: "luaL_loadfile".}
proc loadbuffer*(L: PState, buff: cstring, size: int, name: cstring): int{.
cdecl, dynlib: LIB_NAME, importc: "luaL_loadbuffer".}
proc loadstring*(L: PState, s: cstring): int{.cdecl, dynlib: LIB_NAME,
importc: "luaL_loadstring".}
proc newstate*(): PState{.cdecl, dynlib: LIB_NAME,
importc: "luaL_newstate".}
proc open*(): PState proc open*(): PState
# compatibility; moved from unit lua to lauxlib because it needs luaL_newstate # compatibility; moved from unit lua to lauxlib because it needs luaL_newstate
# #
@ -96,18 +83,18 @@ proc open*(): PState
#** some useful macros #** some useful macros
#** =============================================================== #** ===============================================================
# #
proc argcheck*(L: PState, cond: bool, numarg: int, extramsg: cstring) proc argcheck*(L: PState, cond: bool, numarg: cint, extramsg: cstring)
proc checkstring*(L: PState, n: int): cstring proc checkstring*(L: PState, n: cint): cstring
proc optstring*(L: PState, n: int, d: cstring): cstring proc optstring*(L: PState, n: cint, d: cstring): cstring
proc checkint*(L: PState, n: int): int proc checkint*(L: PState, n: cint): cint
proc checklong*(L: PState, n: int): int32 proc checklong*(L: PState, n: cint): clong
proc optint*(L: PState, n: int, d: float64): int proc optint*(L: PState, n: cint, d: float64): cint
proc optlong*(L: PState, n: int, d: float64): int32 proc optlong*(L: PState, n: cint, d: float64): clong
proc dofile*(L: PState, filename: cstring): int proc dofile*(L: PState, filename: cstring): cint
proc dostring*(L: PState, str: cstring): int proc dostring*(L: PState, str: cstring): cint
proc getmetatable*(L: PState, tname: cstring) proc getmetatable*(L: PState, tname: cstring)
# not translated: # not translated:
# #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) # #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
# #
#** ======================================================= #** =======================================================
#** Generic Buffer manipulation #** Generic Buffer manipulation
@ -119,7 +106,7 @@ const # note: this is just arbitrary, as it related to t
type type
Buffer*{.final.} = object Buffer*{.final.} = object
p*: cstring # current position in buffer p*: cstring # current position in buffer
lvl*: int # number of strings in the stack (level) lvl*: cint # number of strings in the stack (level)
L*: PState L*: PState
buffer*: array[0..BUFFERSIZE - 1, Char] # warning: see note above about LUAL_BUFFERSIZE buffer*: array[0..BUFFERSIZE - 1, Char] # warning: see note above about LUAL_BUFFERSIZE
@ -130,80 +117,76 @@ proc addchar*(B: PBuffer, c: Char)
# compatibility only (alias for luaL_addchar) # compatibility only (alias for luaL_addchar)
proc putchar*(B: PBuffer, c: Char) proc putchar*(B: PBuffer, c: Char)
# warning: see note above about LUAL_BUFFERSIZE # warning: see note above about LUAL_BUFFERSIZE
proc addsize*(B: PBuffer, n: int) proc addsize*(B: PBuffer, n: cint)
proc buffinit*(L: PState, B: PBuffer){.cdecl, dynlib: LIB_NAME,
importc: "luaL_buffinit".} {.push callConv: cdecl, dynlib: lua.LIB_NAME, importc: "luaL_$1".}
proc prepbuffer*(B: PBuffer): cstring{.cdecl, dynlib: LIB_NAME, proc buffinit*(L: PState, B: PBuffer)
importc: "luaL_prepbuffer".} proc prepbuffer*(B: PBuffer): cstring
proc addlstring*(B: PBuffer, s: cstring, L: int){.cdecl, proc addlstring*(B: PBuffer, s: cstring, L: cint)
dynlib: LIB_NAME, importc: "luaL_addlstring".} proc addstring*(B: PBuffer, s: cstring)
proc addstring*(B: PBuffer, s: cstring){.cdecl, dynlib: LIB_NAME, proc addvalue*(B: PBuffer)
importc: "luaL_addstring".} proc pushresult*(B: PBuffer)
proc addvalue*(B: PBuffer){.cdecl, dynlib: LIB_NAME, proc gsub*(L: PState, s, p, r: cstring): cstring
importc: "luaL_addvalue".} proc findtable*(L: PState, idx: cint, fname: cstring, szhint: cint): cstring
proc pushresult*(B: PBuffer){.cdecl, dynlib: LIB_NAME,
importc: "luaL_pushresult".}
proc gsub*(L: PState, s, p, r: cstring): cstring{.cdecl,
dynlib: LIB_NAME, importc: "luaL_gsub".}
proc findtable*(L: PState, idx: int, fname: cstring, szhint: int): cstring{.
cdecl, dynlib: LIB_NAME, importc: "luaL_findtable".}
# compatibility with ref system # compatibility with ref system
# pre-defined references # pre-defined references
{.pop.}
const const
NOREF* = - 2 NOREF* = - 2
REFNIL* = - 1 REFNIL* = - 1
proc unref*(L: PState, theref: int) proc unref*(L: PState, theref: cint)
proc getref*(L: PState, theref: int) proc getref*(L: PState, theref: cint)
# #
#** Compatibility macros and functions #** Compatibility macros and functions
# #
# implementation # implementation
proc pushstring(L: PState, s: string) = proc pushstring(L: PState, s: string) =
pushlstring(L, cstring(s), len(s)) pushlstring(L, cstring(s), s.len.cint)
proc getn(L: PState, n: int): int = proc getn(L: PState, n: cint): cint =
Result = objlen(L, n) Result = objlen(L, n)
proc setn(L: PState, t, n: int) = proc setn(L: PState, t, n: cint) =
# does nothing as this operation is deprecated # does nothing as this operation is deprecated
nil nil
proc open(): PState = proc open(): PState =
Result = newstate() Result = newstate()
proc dofile(L: PState, filename: cstring): int = proc dofile(L: PState, filename: cstring): cint =
Result = loadfile(L, filename) Result = loadfile(L, filename)
if Result == 0: Result = pcall(L, 0, MULTRET, 0) if Result == 0: Result = pcall(L, 0, MULTRET, 0)
proc dostring(L: PState, str: cstring): int = proc dostring(L: PState, str: cstring): cint =
Result = loadstring(L, str) Result = loadstring(L, str)
if Result == 0: Result = pcall(L, 0, MULTRET, 0) if Result == 0: Result = pcall(L, 0, MULTRET, 0)
proc getmetatable(L: PState, tname: cstring) = proc getmetatable(L: PState, tname: cstring) =
getfield(L, REGISTRYINDEX, tname) getfield(L, REGISTRYINDEX, tname)
proc argcheck(L: PState, cond: bool, numarg: int, extramsg: cstring) = proc argcheck(L: PState, cond: bool, numarg: cint, extramsg: cstring) =
if not cond: if not cond:
discard argerror(L, numarg, extramsg) discard argerror(L, numarg, extramsg)
proc checkstring(L: PState, n: int): cstring = proc checkstring(L: PState, n: cint): cstring =
Result = checklstring(L, n, nil) Result = checklstring(L, n, nil)
proc optstring(L: PState, n: int, d: cstring): cstring = proc optstring(L: PState, n: cint, d: cstring): cstring =
Result = optlstring(L, n, d, nil) Result = optlstring(L, n, d, nil)
proc checkint(L: PState, n: int): int = proc checkint(L: PState, n: cint): cint =
Result = toInt(checknumber(L, n)) Result = cint(checknumber(L, n))
proc checklong(L: PState, n: int): int32 = proc checklong(L: PState, n: cint): clong =
Result = int32(ToInt(checknumber(L, n))) Result = int32(ToInt(checknumber(L, n)))
proc optint(L: PState, n: int, d: float64): int = proc optint(L: PState, n: cint, d: float64): cint =
Result = int(ToInt(optnumber(L, n, d))) Result = optnumber(L, n, d).cint
proc optlong(L: PState, n: int, d: float64): int32 = proc optlong(L: PState, n: cint, d: float64): clong =
Result = int32(ToInt(optnumber(L, n, d))) Result = int32(ToInt(optnumber(L, n, d)))
proc addchar(B: PBuffer, c: Char) = proc addchar(B: PBuffer, c: Char) =
@ -215,11 +198,11 @@ proc addchar(B: PBuffer, c: Char) =
proc putchar(B: PBuffer, c: Char) = proc putchar(B: PBuffer, c: Char) =
addchar(B, c) addchar(B, c)
proc addsize(B: PBuffer, n: int) = proc addsize(B: PBuffer, n: cint) =
B.p = cast[cstring](cast[int](B.p) + n) B.p = cast[cstring](cast[int](B.p) + n)
proc unref(L: PState, theref: int) = proc unref(L: PState, theref: cint) =
unref(L, REGISTRYINDEX, theref) unref(L, REGISTRYINDEX, theref)
proc getref(L: PState, theref: int) = proc getref(L: PState, theref: cint) =
rawgeti(L, REGISTRYINDEX, theref) rawgeti(L, REGISTRYINDEX, theref)