diff options
Diffstat (limited to 'lib/base/lua')
-rw-r--r-- | lib/base/lua/lauxlib.nim | 224 | ||||
-rw-r--r-- | lib/base/lua/lua.nim | 384 | ||||
-rw-r--r-- | lib/base/lua/lualib.nim | 73 |
3 files changed, 681 insertions, 0 deletions
diff --git a/lib/base/lua/lauxlib.nim b/lib/base/lua/lauxlib.nim new file mode 100644 index 000000000..143a6cc46 --- /dev/null +++ b/lib/base/lua/lauxlib.nim @@ -0,0 +1,224 @@ +#***************************************************************************** +# * * +# * File: lauxlib.pas * +# * Authors: TeCGraf (C headers + actual Lua libraries) * +# * Lavergne Thomas (original translation to Pascal) * +# * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) * +# * Description: Lua auxiliary library * +# * * +# ***************************************************************************** +# +#** $Id: lauxlib.h,v 1.59 2003/03/18 12:25:32 roberto Exp $ +#** Auxiliary functions for building Lua libraries +#** See Copyright Notice in lua.h +# +# +#** Translated to pascal by Lavergne Thomas +#** Notes : +#** - Pointers type was prefixed with 'P' +#** Bug reports : +#** - thomas.lavergne@laposte.net +#** In french or in english +# + +import "lib/base/lua/lua" + +proc lua_pushstring*(L: Plua_State, s: string) + # compatibilty macros +proc luaL_getn*(L: Plua_State, n: int): int + # calls lua_objlen +proc luaL_setn*(L: Plua_State, t, n: int) + # does nothing! +type + TLuaL_reg*{.final.} = object + name*: cstring + func*: lua_CFunction + + PluaL_reg* = ptr TLuaL_reg + +proc luaL_openlib*(L: Plua_State, libname: cstring, lr: PluaL_reg, nup: int){. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_register*(L: Plua_State, libname: cstring, lr: PluaL_reg){.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_getmetafield*(L: Plua_State, obj: int, e: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_callmeta*(L: Plua_State, obj: int, e: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_typerror*(L: Plua_State, narg: int, tname: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_argerror*(L: Plua_State, numarg: int, extramsg: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_checklstring*(L: Plua_State, numArg: int, l_: Psize_t): cstring{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_optlstring*(L: Plua_State, numArg: int, def: cstring, l_: Psize_t): cstring{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_checknumber*(L: Plua_State, numArg: int): lua_Number{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_optnumber*(L: Plua_State, nArg: int, def: lua_Number): lua_Number{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_checkinteger*(L: Plua_State, numArg: int): lua_Integer{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_optinteger*(L: Plua_State, nArg: int, def: lua_Integer): lua_Integer{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_checkstack*(L: Plua_State, sz: int, msg: cstring){.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_checktype*(L: Plua_State, narg, t: int){.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_checkany*(L: Plua_State, narg: int){.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_newmetatable*(L: Plua_State, tname: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_checkudata*(L: Plua_State, ud: int, tname: cstring): Pointer{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_where*(L: Plua_State, lvl: int){.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_error*(L: Plua_State, fmt: cstring): int{.cdecl, varargs, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_checkoption*(L: Plua_State, narg: int, def: cstring, lst: cstringArray): int{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_ref*(L: Plua_State, t: int): int{.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_unref*(L: Plua_State, t, theref: int){.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_loadfile*(L: Plua_State, filename: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_loadbuffer*(L: Plua_State, buff: cstring, size: size_t, name: cstring): int{. + cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_loadstring*(L: Plua_State, s: cstring): int{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_newstate*(): Plua_State{.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc lua_open*(): Plua_State + # compatibility; moved from unit lua to lauxlib because it needs luaL_newstate + # + #** =============================================================== + #** some useful macros + #** =============================================================== + # +proc luaL_argcheck*(L: Plua_State, cond: bool, numarg: int, extramsg: cstring) +proc luaL_checkstring*(L: Plua_State, n: int): cstring +proc luaL_optstring*(L: Plua_State, n: int, d: cstring): cstring +proc luaL_checkint*(L: Plua_State, n: int): int +proc luaL_checklong*(L: Plua_State, n: int): int32 +proc luaL_optint*(L: Plua_State, n: int, d: float64): int +proc luaL_optlong*(L: Plua_State, n: int, d: float64): int32 +proc luaL_typename*(L: Plua_State, i: int): cstring +proc lua_dofile*(L: Plua_State, filename: cstring): int +proc lua_dostring*(L: Plua_State, str: cstring): int +proc lua_Lgetmetatable*(L: Plua_State, tname: cstring) + # not translated: + # #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + # + #** ======================================================= + #** Generic Buffer manipulation + #** ======================================================= + # +const # note: this is just arbitrary, as it related to the BUFSIZ defined in stdio.h ... + LUAL_BUFFERSIZE* = 4096 + +type + luaL_Buffer*{.final.} = object + p*: cstring # current position in buffer + lvl*: int # number of strings in the stack (level) + L*: Plua_State + buffer*: array[0..LUAL_BUFFERSIZE - 1, Char] # warning: see note above about LUAL_BUFFERSIZE + + PluaL_Buffer* = ptr luaL_Buffer + +proc luaL_addchar*(B: PluaL_Buffer, c: Char) + # warning: see note above about LUAL_BUFFERSIZE + # compatibility only (alias for luaL_addchar) +proc luaL_putchar*(B: PluaL_Buffer, c: Char) + # warning: see note above about LUAL_BUFFERSIZE +proc luaL_addsize*(B: PluaL_Buffer, n: int) +proc luaL_buffinit*(L: Plua_State, B: PluaL_Buffer){.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_prepbuffer*(B: PluaL_Buffer): cstring{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_addlstring*(B: PluaL_Buffer, s: cstring, L: size_t){.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_addstring*(B: PluaL_Buffer, s: cstring){.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_addvalue*(B: PluaL_Buffer){.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_pushresult*(B: PluaL_Buffer){.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaL_gsub*(L: Plua_State, s, p, r: cstring): cstring{.cdecl, + dynlib: LUA_LIB_NAME, importc.} +proc luaL_findtable*(L: Plua_State, idx: int, fname: cstring, szhint: int): cstring{. + cdecl, dynlib: LUA_LIB_NAME, importc.} + # compatibility with ref system + # pre-defined references +const + LUA_NOREF* = - 2 + LUA_REFNIL* = - 1 + +proc lua_unref*(L: Plua_State, theref: int) +proc lua_getref*(L: Plua_State, theref: int) + # + #** Compatibility macros and functions + # + +# implementation + +proc lua_pushstring(L: Plua_State, s: string) = + lua_pushlstring(L, cstring(s), len(s)) + +proc luaL_getn(L: Plua_State, n: int): int = + Result = lua_objlen(L, n) + +proc luaL_setn(L: Plua_State, t, n: int) = + # does nothing as this operation is deprecated + nil + +proc lua_open(): Plua_State = + Result = luaL_newstate() + +proc luaL_typename(L: Plua_State, i: int): cstring = + Result = lua_typename(L, lua_type(L, i)) + +proc lua_dofile(L: Plua_State, filename: cstring): int = + Result = luaL_loadfile(L, filename) + if Result == 0: Result = lua_pcall(L, 0, LUA_MULTRET, 0) + +proc lua_dostring(L: Plua_State, str: cstring): int = + Result = luaL_loadstring(L, str) + if Result == 0: Result = lua_pcall(L, 0, LUA_MULTRET, 0) + +proc lua_Lgetmetatable(L: Plua_State, tname: cstring) = + lua_getfield(L, LUA_REGISTRYINDEX, tname) + +proc luaL_argcheck(L: Plua_State, cond: bool, numarg: int, extramsg: cstring) = + if not cond: + discard luaL_argerror(L, numarg, extramsg) + +proc luaL_checkstring(L: Plua_State, n: int): cstring = + Result = luaL_checklstring(L, n, nil) + +proc luaL_optstring(L: Plua_State, n: int, d: cstring): cstring = + Result = luaL_optlstring(L, n, d, nil) + +proc luaL_checkint(L: Plua_State, n: int): int = + Result = toInt(luaL_checknumber(L, n)) + +proc luaL_checklong(L: Plua_State, n: int): int32 = + Result = int32(ToInt(luaL_checknumber(L, n))) + +proc luaL_optint(L: Plua_State, n: int, d: float64): int = + Result = int(ToInt(luaL_optnumber(L, n, d))) + +proc luaL_optlong(L: Plua_State, n: int, d: float64): int32 = + Result = int32(ToInt(luaL_optnumber(L, n, d))) + +proc luaL_addchar(B: PluaL_Buffer, c: Char) = + if cast[int](addr((B.p))) < (cast[int](addr((B.buffer[0]))) + LUAL_BUFFERSIZE): + discard luaL_prepbuffer(B) + B.p[1] = c + B.p = cast[cstring](cast[int](B.p) + 1) + +proc luaL_putchar(B: PluaL_Buffer, c: Char) = + luaL_addchar(B, c) + +proc luaL_addsize(B: PluaL_Buffer, n: int) = + B.p = cast[cstring](cast[int](B.p) + n) + +proc lua_unref(L: Plua_State, theref: int) = + luaL_unref(L, LUA_REGISTRYINDEX, theref) + +proc lua_getref(L: Plua_State, theref: int) = + lua_rawgeti(L, LUA_REGISTRYINDEX, theref) diff --git a/lib/base/lua/lua.nim b/lib/base/lua/lua.nim new file mode 100644 index 000000000..ab872fd23 --- /dev/null +++ b/lib/base/lua/lua.nim @@ -0,0 +1,384 @@ +#***************************************************************************** +# * * +# * File: lua.pas * +# * Authors: TeCGraf (C headers + actual Lua libraries) * +# * Lavergne Thomas (original translation to Pascal) * +# * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) * +# * Description: Basic Lua library * +# * * +# ***************************************************************************** +# +#** $Id: lua.h,v 1.175 2003/03/18 12:31:39 roberto Exp $ +#** Lua - An Extensible Extension Language +#** TeCGraf: Computer Graphics Technology Group, PUC-Rio, Brazil +#** http://www.lua.org mailto:info@lua.org +#** See Copyright Notice at the end of this file +# +# +#** Updated to Lua 5.1.1 by Bram Kuijvenhoven (bram at kuijvenhoven dot net), +#** Hexis BV (http://www.hexis.nl), the Netherlands +#** Notes: +#** - Only tested with FPC (FreePascal Compiler) +#** - Using LuaBinaries styled DLL/SO names, which include version names +#** - LUA_YIELD was suffixed by '_' for avoiding name collision +# +# +#** Translated to pascal by Lavergne Thomas +#** Notes : +#** - Pointers type was prefixed with 'P' +#** - lua_upvalueindex constant was transformed to function +#** - Some compatibility function was isolated because with it you must have +#** lualib. +#** - LUA_VERSION was suffixed by '_' for avoiding name collision. +#** Bug reports : +#** - thomas.lavergne@laposte.net +#** In french or in english +# + +when defined(MACOSX): + const + LUA_NAME* = "liblua5.1.dylib" + LUA_LIB_NAME* = "liblua5.1.dylib" +elif defined(UNIX): + const + LUA_NAME* = "liblua5.1.so" + LUA_LIB_NAME* = "liblua5.1.so" +else: + const + LUA_NAME* = "lua5.1.dll" + LUA_LIB_NAME* = "lua5.1.dll" +type + size_t* = int + Psize_t* = ptr size_t + +const + LUA_VERSION* = "Lua 5.1" + LUA_RELEASE* = "Lua 5.1.1" + LUA_VERSION_NUM* = 501 + LUA_COPYRIGHT* = "Copyright (C) 1994-2006 Lua.org, PUC-Rio" + LUA_AUTHORS* = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" # option for multiple returns in `lua_pcall' and `lua_call' + LUA_MULTRET* = - 1 # + #** pseudo-indices + # + LUA_REGISTRYINDEX* = - 10000 + LUA_ENVIRONINDEX* = - 10001 + LUA_GLOBALSINDEX* = - 10002 + +proc lua_upvalueindex*(I: int): int +const # thread status; 0 is OK + LUA_YIELD_* = 1 + LUA_ERRRUN* = 2 + LUA_ERRSYNTAX* = 3 + LUA_ERRMEM* = 4 + LUA_ERRERR* = 5 + +type + Plua_State* = Pointer + lua_CFunction* = proc (L: Plua_State): int{.cdecl.} # + #** functions that read/write blocks when loading/dumping Lua chunks + # + +type + lua_Reader* = proc (L: Plua_State, ud: Pointer, sz: Psize_t): cstring{.cdecl.} + lua_Writer* = proc (L: Plua_State, p: Pointer, sz: size_t, ud: Pointer): int{. + cdecl.} # + #** prototype for memory-allocation functions + # + lua_Alloc* = proc (ud, theptr: Pointer, osize, nsize: size_t){.cdecl.} + +const + LUA_TNONE* = - 1 + LUA_TNIL* = 0 + LUA_TBOOLEAN* = 1 + LUA_TLIGHTUSERDATA* = 2 + LUA_TNUMBER* = 3 + LUA_TSTRING* = 4 + LUA_TTABLE* = 5 + LUA_TFUNCTION* = 6 + LUA_TUSERDATA* = 7 + LUA_TTHREAD* = 8 # minimum Lua stack available to a C function + LUA_MINSTACK* = 20 + +type # Type of Numbers in Lua + lua_Number* = float + lua_Integer* = int + +proc lua_newstate*(f: lua_Alloc, ud: Pointer): Plua_State{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_close*(L: Plua_State){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_newthread*(L: Plua_State): Plua_State{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_atpanic*(L: Plua_State, panicf: lua_CFunction): lua_CFunction{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_gettop*(L: Plua_State): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_settop*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_pushvalue*(L: Plua_State, Idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_remove*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_insert*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_replace*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_checkstack*(L: Plua_State, sz: int): cint{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_xmove*(`from`, `to`: Plua_State, n: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_isnumber*(L: Plua_State, idx: int): cint{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_isstring*(L: Plua_State, idx: int): cint{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_iscfunction*(L: Plua_State, idx: int): cint{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_isuserdata*(L: Plua_State, idx: int): cint{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_type*(L: Plua_State, idx: int): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_typename*(L: Plua_State, tp: int): cstring{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_equal*(L: Plua_State, idx1, idx2: int): cint{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_rawequal*(L: Plua_State, idx1, idx2: int): cint{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_lessthan*(L: Plua_State, idx1, idx2: int): cint{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_tonumber*(L: Plua_State, idx: int): lua_Number{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_tointeger*(L: Plua_State, idx: int): lua_Integer{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_toboolean*(L: Plua_State, idx: int): cint{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_tolstring*(L: Plua_State, idx: int, length: Psize_t): cstring{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_objlen*(L: Plua_State, idx: int): size_t{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_tocfunction*(L: Plua_State, idx: int): lua_CFunction{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_touserdata*(L: Plua_State, idx: int): Pointer{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_tothread*(L: Plua_State, idx: int): Plua_State{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_topointer*(L: Plua_State, idx: int): Pointer{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushnil*(L: Plua_State){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_pushnumber*(L: Plua_State, n: lua_Number){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushinteger*(L: Plua_State, n: lua_Integer){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushlstring*(L: Plua_State, s: cstring, l_: size_t){.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_pushstring*(L: Plua_State, s: cstring){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushvfstring*(L: Plua_State, fmt: cstring, argp: Pointer): cstring{. + cdecl, dynlib: LUA_NAME, importc.} +proc lua_pushfstring*(L: Plua_State, fmt: cstring): cstring{.cdecl, varargs, + dynlib: LUA_NAME, importc.} +proc lua_pushcclosure*(L: Plua_State, fn: lua_CFunction, n: int){.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_pushboolean*(L: Plua_State, b: cint){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushlightuserdata*(L: Plua_State, p: Pointer){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pushthread*(L: Plua_State){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_gettable*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_getfield*(L: Plua_state, idx: int, k: cstring){.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_rawget*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_rawgeti*(L: Plua_State, idx, n: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_createtable*(L: Plua_State, narr, nrec: int){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_newuserdata*(L: Plua_State, sz: size_t): Pointer{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_getmetatable*(L: Plua_State, objindex: int): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_getfenv*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_settable*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_setfield*(L: Plua_State, idx: int, k: cstring){.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_rawset*(L: Plua_State, idx: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_rawseti*(L: Plua_State, idx, n: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_setmetatable*(L: Plua_State, objindex: int): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_setfenv*(L: Plua_State, idx: int): int{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_call*(L: Plua_State, nargs, nresults: int){.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_pcall*(L: Plua_State, nargs, nresults, errf: int): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_cpcall*(L: Plua_State, func: lua_CFunction, ud: Pointer): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_load*(L: Plua_State, reader: lua_Reader, dt: Pointer, + chunkname: cstring): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_dump*(L: Plua_State, writer: lua_Writer, data: Pointer): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_yield*(L: Plua_State, nresults: int): int{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_resume*(L: Plua_State, narg: int): int{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_status*(L: Plua_State): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_gc*(L: Plua_State, what, data: int): int{.cdecl, dynlib: LUA_NAME, + importc.} +proc lua_error*(L: Plua_State): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_next*(L: Plua_State, idx: int): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_concat*(L: Plua_State, n: int){.cdecl, dynlib: LUA_NAME, importc.} +proc lua_getallocf*(L: Plua_State, ud: ptr Pointer): lua_Alloc{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_setallocf*(L: Plua_State, f: lua_Alloc, ud: Pointer){.cdecl, + dynlib: LUA_NAME, importc.} + # + #** Garbage-collection functions and options + # +const + LUA_GCSTOP* = 0 + LUA_GCRESTART* = 1 + LUA_GCCOLLECT* = 2 + LUA_GCCOUNT* = 3 + LUA_GCCOUNTB* = 4 + LUA_GCSTEP* = 5 + LUA_GCSETPAUSE* = 6 + LUA_GCSETSTEPMUL* = 7 # + #** =============================================================== + #** some useful macros + #** =============================================================== + # + +proc lua_pop*(L: Plua_State, n: int) +proc lua_newtable*(L: Plua_state) +proc lua_register*(L: Plua_State, n: cstring, f: lua_CFunction) +proc lua_pushcfunction*(L: Plua_State, f: lua_CFunction) +proc lua_strlen*(L: Plua_state, i: int): size_t +proc lua_isfunction*(L: Plua_State, n: int): bool +proc lua_istable*(L: Plua_State, n: int): bool +proc lua_islightuserdata*(L: Plua_State, n: int): bool +proc lua_isnil*(L: Plua_State, n: int): bool +proc lua_isboolean*(L: Plua_State, n: int): bool +proc lua_isthread*(L: Plua_State, n: int): bool +proc lua_isnone*(L: Plua_State, n: int): bool +proc lua_isnoneornil*(L: Plua_State, n: int): bool +proc lua_pushliteral*(L: Plua_State, s: cstring) +proc lua_setglobal*(L: Plua_State, s: cstring) +proc lua_getglobal*(L: Plua_State, s: cstring) +proc lua_tostring*(L: Plua_State, i: int): cstring + # + #** compatibility macros and functions + # +proc lua_getregistry*(L: Plua_State) +proc lua_getgccount*(L: Plua_State): int +type + lua_Chunkreader* = lua_Reader + lua_Chunkwriter* = lua_Writer # + #** {====================================================================== + #** Debug API + #** ======================================================================= + # + +const + LUA_HOOKCALL* = 0 + LUA_HOOKRET* = 1 + LUA_HOOKLINE* = 2 + LUA_HOOKCOUNT* = 3 + LUA_HOOKTAILRET* = 4 + +const + LUA_MASKCALL* = 1 shl Ord(LUA_HOOKCALL) + LUA_MASKRET* = 1 shl Ord(LUA_HOOKRET) + LUA_MASKLINE* = 1 shl Ord(LUA_HOOKLINE) + LUA_MASKCOUNT* = 1 shl Ord(LUA_HOOKCOUNT) + +const + LUA_IDSIZE* = 60 + +type + lua_Debug*{.final.} = object # activation record + event*: int + name*: cstring # (n) + namewhat*: cstring # (n) `global', `local', `field', `method' + what*: cstring # (S) `Lua', `C', `main', `tail' + source*: cstring # (S) + currentline*: int # (l) + nups*: int # (u) number of upvalues + linedefined*: int # (S) + lastlinedefined*: int # (S) + short_src*: array[0..LUA_IDSIZE - 1, Char] # (S) + # private part + i_ci*: int # active function + + Plua_Debug* = ptr lua_Debug + lua_Hook* = proc (L: Plua_State, ar: Plua_Debug){.cdecl.} # + #** {====================================================================== + #** Debug API + #** ======================================================================= + # + +proc lua_getstack*(L: Plua_State, level: int, ar: Plua_Debug): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_getinfo*(L: Plua_State, what: cstring, ar: Plua_Debug): int{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_getlocal*(L: Plua_State, ar: Plua_Debug, n: int): cstring{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_setlocal*(L: Plua_State, ar: Plua_Debug, n: int): cstring{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_getupvalue*(L: Plua_State, funcindex: int, n: int): cstring{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_setupvalue*(L: Plua_State, funcindex: int, n: int): cstring{.cdecl, + dynlib: LUA_NAME, importc.} +proc lua_sethook*(L: Plua_State, func: lua_Hook, mask: int, count: int): int{. + cdecl, dynlib: LUA_NAME, importc.} +proc lua_gethook*(L: Plua_State): lua_Hook{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_gethookmask*(L: Plua_State): int{.cdecl, dynlib: LUA_NAME, importc.} +proc lua_gethookcount*(L: Plua_State): int{.cdecl, dynlib: LUA_NAME, importc.} +# implementation + +proc lua_upvalueindex(I: int): int = + Result = LUA_GLOBALSINDEX - i + +proc lua_pop(L: Plua_State, n: int) = + lua_settop(L, - n - 1) + +proc lua_newtable(L: Plua_State) = + lua_createtable(L, 0, 0) + +proc lua_register(L: Plua_State, n: cstring, f: lua_CFunction) = + lua_pushcfunction(L, f) + lua_setglobal(L, n) + +proc lua_pushcfunction(L: Plua_State, f: lua_CFunction) = + lua_pushcclosure(L, f, 0) + +proc lua_strlen(L: Plua_State, i: int): size_t = + Result = lua_objlen(L, i) + +proc lua_isfunction(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TFUNCTION + +proc lua_istable(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TTABLE + +proc lua_islightuserdata(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TLIGHTUSERDATA + +proc lua_isnil(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TNIL + +proc lua_isboolean(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TBOOLEAN + +proc lua_isthread(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TTHREAD + +proc lua_isnone(L: Plua_State, n: int): bool = + Result = lua_type(L, n) == LUA_TNONE + +proc lua_isnoneornil(L: Plua_State, n: int): bool = + Result = lua_type(L, n) <= 0 + +proc lua_pushliteral(L: Plua_State, s: cstring) = + lua_pushlstring(L, s, len(s)) + +proc lua_setglobal(L: Plua_State, s: cstring) = + lua_setfield(L, LUA_GLOBALSINDEX, s) + +proc lua_getglobal(L: Plua_State, s: cstring) = + lua_getfield(L, LUA_GLOBALSINDEX, s) + +proc lua_tostring(L: Plua_State, i: int): cstring = + Result = lua_tolstring(L, i, nil) + +proc lua_getregistry(L: Plua_State) = + lua_pushvalue(L, LUA_REGISTRYINDEX) + +proc lua_getgccount(L: Plua_State): int = + Result = lua_gc(L, LUA_GCCOUNT, 0) diff --git a/lib/base/lua/lualib.nim b/lib/base/lua/lualib.nim new file mode 100644 index 000000000..efd3ab923 --- /dev/null +++ b/lib/base/lua/lualib.nim @@ -0,0 +1,73 @@ +#***************************************************************************** +# * * +# * File: lualib.pas * +# * Authors: TeCGraf (C headers + actual Lua libraries) * +# * Lavergne Thomas (original translation to Pascal) * +# * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) * +# * Description: Standard Lua libraries * +# * * +# ***************************************************************************** +# +#** $Id: lualib.h,v 1.28 2003/03/18 12:24:26 roberto Exp $ +#** Lua standard libraries +#** See Copyright Notice in lua.h +# +# +#** Translated to pascal by Lavergne Thomas +#** Bug reports : +#** - thomas.lavergne@laposte.net +#** In french or in english +# + +import "lib/base/lua/lua" + +const + LUA_COLIBNAME* = "coroutine" + LUA_TABLIBNAME* = "table" + LUA_IOLIBNAME* = "io" + LUA_OSLIBNAME* = "os" + LUA_STRLINAME* = "string" + LUA_MATHLIBNAME* = "math" + LUA_DBLIBNAME* = "debug" + LUA_LOADLIBNAME* = "package" + +proc luaopen_base*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaopen_table*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaopen_io*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, importc.} +proc luaopen_string*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaopen_math*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaopen_debug*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaopen_package*(L: Plua_State): cint{.cdecl, dynlib: LUA_LIB_NAME, + importc.} +proc luaL_openlibs*(L: Plua_State){.cdecl, dynlib: LUA_LIB_NAME, importc.} + # compatibility code +proc lua_baselibopen*(L: Plua_State): Bool +proc lua_tablibopen*(L: Plua_State): Bool +proc lua_iolibopen*(L: Plua_State): Bool +proc lua_strlibopen*(L: Plua_State): Bool +proc lua_mathlibopen*(L: Plua_State): Bool +proc lua_dblibopen*(L: Plua_State): Bool +# implementation + +proc lua_baselibopen(L: Plua_State): Bool = + Result = luaopen_base(L) != 0'i32 + +proc lua_tablibopen(L: Plua_State): Bool = + Result = luaopen_table(L) != 0'i32 + +proc lua_iolibopen(L: Plua_State): Bool = + Result = luaopen_io(L) != 0'i32 + +proc lua_strlibopen(L: Plua_State): Bool = + Result = luaopen_string(L) != 0'i32 + +proc lua_mathlibopen(L: Plua_State): Bool = + Result = luaopen_math(L) != 0'i32 + +proc lua_dblibopen(L: Plua_State): Bool = + Result = luaopen_debug(L) != 0'i32 |