Advertisement
Dadido3

Purebasic Lua Include

Oct 11th, 2012
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  by David Vogel (Dadido3, Xaardas)
  2.  
  3. ; ########################################## Konstanten ########################################
  4.  
  5. #LUA_VERSION_MAJOR    = "5"
  6. #LUA_VERSION_MINOR    = "2"
  7. #LUA_VERSION_NUM      = 502
  8. #LUA_VERSION_RELEASE  = "1"
  9.  
  10. #LUA_VERSION          = "Lua "+#LUA_VERSION_MAJOR+"."+#LUA_VERSION_MINOR
  11. #LUA_RELEASE          = #LUA_VERSION+"."+#LUA_VERSION_RELEASE
  12. #LUA_COPYRIGHT        = #LUA_RELEASE+"  Copyright (C) 1994-2012 Lua.org, PUC-Rio"
  13. #LUA_AUTHORS          = "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
  14.  
  15. ; mark For precompiled code ('<esc>Lua')
  16. #LUA_SIGNATURE        = "\033Lua"
  17.  
  18. ; option For multiple returns in 'lua_pcall' And 'lua_call'
  19. #LUA_MULTRET          = -1
  20.  
  21. ; thread status
  22. #LUA_OK               = 0
  23. #LUA_YIELD            = 1
  24. #LUA_ERRRUN           = 2
  25. #LUA_ERRSYNTAX        = 3
  26. #LUA_ERRMEM           = 4
  27. #LUA_ERRGCMM          = 5
  28. #LUA_ERRERR           = 6
  29.  
  30. ; basic types
  31. #LUA_TNONE            = -1
  32.  
  33. #LUA_TNIL             = 0
  34. #LUA_TBOOLEAN         = 1
  35. #LUA_TLIGHTUSERDATA   = 2
  36. #LUA_TNUMBER          = 3
  37. #LUA_TSTRING          = 4
  38. #LUA_TTABLE           = 5
  39. #LUA_TFUNCTION        = 6
  40. #LUA_TUSERDATA        = 7
  41. #LUA_TTHREAD          = 8
  42.  
  43. #LUA_NUMTAGS          = 9
  44.  
  45. ; minimum Lua stack available To a C function
  46. #LUA_MINSTACK         = 20
  47.  
  48. ; predefined values in the registry
  49. #LUA_RIDX_MAINTHREAD  = 1
  50. #LUA_RIDX_GLOBALS     = 2
  51. #LUA_RIDX_LAST        = #LUA_RIDX_GLOBALS
  52.  
  53. ; Comparison And arithmetic functions
  54. #LUA_OPADD            = 0   ; ORDER TM
  55. #LUA_OPSUB            = 1
  56. #LUA_OPMUL            = 2
  57. #LUA_OPDIV            = 3
  58. #LUA_OPMOD            = 4
  59. #LUA_OPPOW            = 5
  60. #LUA_OPUNM            = 6
  61.  
  62. #LUA_OPEQ             = 0
  63. #LUA_OPLT             = 1
  64. #LUA_OPLE             = 2
  65.  
  66. ; garbage-collection function And options
  67. #LUA_GCSTOP           = 0
  68. #LUA_GCRESTART        = 1
  69. #LUA_GCCOLLECT        = 2
  70. #LUA_GCCOUNT          = 3
  71. #LUA_GCCOUNTB         = 4
  72. #LUA_GCSTEP           = 5
  73. #LUA_GCSETPAUSE       = 6
  74. #LUA_GCSETSTEPMUL     = 7
  75. #LUA_GCSETMAJORINC    = 8
  76. #LUA_GCISRUNNING      = 9
  77. #LUA_GCGEN            = 10
  78. #LUA_GCINC            = 11
  79.  
  80. ; pseudo-indices
  81. #LUA_REGISTRYINDEX    = (-10000)
  82. #LUA_ENVIRONINDEX     = (-10001)
  83. #LUA_GLOBALSINDEX     = (-10002)
  84.  
  85. ; Event codes
  86. #LUA_HOOKCALL         = 0
  87. #LUA_HOOKRET          = 1
  88. #LUA_HOOKLINE         = 2
  89. #LUA_HOOKCOUNT        = 3
  90. #LUA_HOOKTAILRET      = 4
  91.  
  92. ;  Event masks
  93. #LUA_MASKCALL         = 1 << #LUA_HOOKCALL
  94. #LUA_MASKRET          = 1 << #LUA_HOOKRET
  95. #LUA_MASKLINE         = 1 << #LUA_HOOKLINE
  96. #LUA_MASKCOUNT        = 1 << #LUA_HOOKCOUNT
  97.  
  98. ; ########################################## Variablen ##########################################
  99.  
  100. ; ########################################## Ladekram ############################################
  101.  
  102. ; ########################################## Declares ############################################
  103.  
  104. Declare   Lua_Do_Function(Function.s, Arguments, Results)
  105.  
  106. ; ########################################## Imports ##########################################
  107.  
  108. CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  109.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  110.     ;#Lua_Import_Prefix = "_"
  111.     ImportC #Library_Path+"lua52.x86.lib" ; Windows x86
  112.   CompilerElse
  113.     ;#Lua_Import_Prefix = ""
  114.     ImportC #Library_Path+"lua52.x64.lib" ; Windows x64
  115.   CompilerEndIf
  116. CompilerElse
  117.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  118.     ;#Lua_Import_Prefix = ""
  119.     Import "/usr/lib/libm.so"
  120.     EndImport
  121.     Import "/usr/lib/libdl.so"
  122.     EndImport
  123.     ImportC "../../Librarys/lua52.x86.a" ;     Linux x86
  124.   CompilerElse
  125.     ;#Lua_Import_Prefix = ""
  126.     ImportC "../../Librarys/lua52.x64.a" ;     Linux x64
  127.   CompilerEndIf
  128. CompilerEndIf
  129.  
  130.   ; /*
  131.   ; ** state manipulation
  132.   ; */
  133.   lua_newstate    (*f, *ud)
  134.   lua_close       (lua_State.i)
  135.   lua_newthread   (lua_State.i)
  136.  
  137.   lua_atpanic     (lua_State.i, *panicf)
  138.  
  139.   ; /*
  140.   ; ** basic stack manipulation
  141.   ; */
  142.   lua_absindex    (lua_State.i, idx.l)
  143.   lua_gettop      (lua_State.i)
  144.   lua_settop      (lua_State.i, idx.l)
  145.   lua_pushvalue   (lua_State.i, idx.l)
  146.   lua_remove      (lua_State.i, idx.l)
  147.   lua_insert      (lua_State.i, idx.l)
  148.   lua_replace     (lua_State.i, idx.l)
  149.   lua_copy        (lua_State.i, fromidx.l, toidx.l)
  150.   lua_checkstack  (lua_State.i, sz.l)
  151.  
  152.   lua_xmove       (lua_State_from.i, lua_State_to.i, n.l)
  153.  
  154.   ; /*
  155.   ; ** access functions (stack -> C)
  156.   ; */
  157.  
  158.   lua_isnumber    (lua_State.i, idx.l)
  159.   lua_isstring    (lua_State.i, idx.l)
  160.   lua_iscfunction (lua_State.i, idx.l)
  161.   lua_isuserdata  (lua_State.i, idx.l)
  162.   lua_type        (lua_State.i, idx.l)
  163.   lua_typename    (lua_State.i, tp.l)
  164.  
  165.   lua_tonumberx.d (lua_State.i, idx.l, *isnum)
  166.   lua_tointegerx  (lua_State.i, idx.l, *isnum)
  167.   lua_tounsignedx (lua_State.i, idx.l, *isnum)
  168.   lua_toboolean   (lua_State.i, idx.l)
  169.   lua_tolstring   (lua_State.i, idx.l, *len)
  170.   lua_rawlen      (lua_State.i, idx.l)
  171.   lua_tocfunction (lua_State.i, idx.l)
  172.   lua_touserdata  (lua_State.i, idx.l)
  173.   lua_tothread    (lua_State.i, idx.l)
  174.   lua_topointer   (lua_State.i, idx.l)
  175.  
  176.   ; /*
  177.   ; ** Comparison And arithmetic functions
  178.   ; */
  179.  
  180.   lua_arith       (lua_State.i, op.l)
  181.  
  182.   lua_rawequal    (lua_State.i, idx1.l, idx2.l)
  183.   lua_compare     (lua_State.i, idx1.l, idx2.l, op.l)
  184.  
  185.   ; /*
  186.   ; ** push functions (C -> stack)
  187.   ; */
  188.   lua_pushnil           (lua_State.i)
  189.   lua_pushnumber        (lua_State.i, n.d)
  190.   lua_pushinteger       (lua_State.i, n.q)
  191.   lua_pushunsigned      (lua_State.i, n.q)
  192.   lua_pushlstring       (lua_State.i, string.p-ascii, size.i)
  193.   lua_pushstring        (lua_State.i, string.p-ascii)
  194.   ;lua_pushvfstring      (lua_State.i, string.p-ascii, *argp)
  195.   ;lua_pushfstring       (lua_State.i, string.p-ascii, ...)
  196.   lua_pushcclosure      (lua_State.i, *fn, n.l)
  197.   lua_pushboolean       (lua_State.i, b.l)
  198.   lua_pushlightuserdata (lua_State.i, *p)
  199.   lua_pushthread        (lua_State.i)
  200.  
  201.   ; /*
  202.   ; ** get functions (Lua -> stack)
  203.   ; */
  204.   lua_getglobal         (lua_State.i, *string_var) ; Because of a memory leak with .p-ascii
  205.   lua_gettable          (lua_State.i, idx.l)
  206.   lua_getfield          (lua_State.i, idx.l, k.p-ascii)
  207.   lua_rawget            (lua_State.i, idx.l)
  208.   lua_rawgeti           (lua_State.i, idx.l, n.l)
  209.   lua_rawgetp           (lua_State.i, idx.l, p.p-ascii)
  210.   lua_createtable       (lua_State.i, narr.l, nrec.l)
  211.   lua_newuserdata       (lua_State.i, sz.i)
  212.   lua_getmetatable      (lua_State.i, objindex.l)
  213.   lua_getuservalue      (lua_State.i, idx.l)
  214.  
  215.   ; /*
  216.   ; ** set functions (stack -> Lua)
  217.   ; */
  218.   lua_setglobal         (lua_State.i, var.p-ascii)
  219.   lua_settable          (lua_State.i, idx.l)
  220.   lua_setfield          (lua_State.i, idx.l, k.p-ascii)
  221.   lua_rawset            (lua_State.i, idx.l)
  222.   lua_rawseti           (lua_State.i, idx.l, n.l)
  223.   lua_rawsetp           (lua_State.i, idx.l, p.p-ascii)
  224.   lua_setmetatable      (lua_State.i, objindex.l)
  225.   lua_setuservalue      (lua_State.i, idx.l)
  226.  
  227.   ; /*
  228.   ; ** 'load' And 'call' functions (load And run Lua code)
  229.   ; */
  230.   lua_callk             (lua_State.i, nargs.l, nresults.l, ctx.l, *k)
  231.   lua_getctx            (lua_State.i, *ctx)
  232.   lua_pcallk            (lua_State.i, nargs.l, nresults.l, *errfunc, ctx.l, *k)
  233.  
  234.   lua_load              (lua_State.i, reader.i, *dt, chunkname.p-ascii, mode.p-ascii)
  235.   lua_dump              (lua_State.i, writer.i, *data_)
  236.  
  237.   ; /*
  238.   ; ** coroutine functions
  239.   ; */
  240.   lua_yieldk            (lua_State.i, nresults.l, ctx.l, *k)
  241.  
  242.   lua_resume            (lua_State.i, lua_State_from.i, narg.l)
  243.   lua_status            (lua_State.i)
  244.  
  245.   ; /*
  246.   ; ** garbage-collection function And options
  247.   ; */
  248.   lua_gc                (lua_State.i, what.l, data_.i)
  249.  
  250.   ; /*
  251.   ; ** miscellaneous functions
  252.   ; */
  253.   lua_error             (lua_State.i)
  254.  
  255.   lua_next              (lua_State.i, idx.l)
  256.  
  257.   lua_concat            (lua_State.i, n.l)
  258.   lua_len               (lua_State.i, idx.l)
  259.  
  260.   lua_getallocf         (lua_State.i, *ud)
  261.   lua_setallocf         (lua_State.i, *f, *ud)
  262.  
  263.   ; lualib.h
  264.   luaopen_base          (lua_State.i)
  265.   luaopen_coroutine     (lua_State.i)
  266.   luaopen_table         (lua_State.i)
  267.   luaopen_io            (lua_State.i)
  268.   luaopen_os            (lua_State.i)
  269.   luaopen_string        (lua_State.i)
  270.   luaopen_bit32         (lua_State.i)
  271.   luaopen_math          (lua_State.i)
  272.   luaopen_debug         (lua_State.i)
  273.   luaopen_package       (lua_State.i)
  274.  
  275.   ; open all previous libraries
  276.   luaL_openlibs         (lua_State.i)
  277.  
  278.   ;lauxlib_h
  279.   luaI_openlib          (lua_State.i, libname.p-ascii, *luaL_Reg.l, nup.l)
  280.   luaL_register         (lua_State.i, libname.p-ascii, *luaL_Reg.l)
  281.   luaL_getmetafield     (lua_State.i, obj.l, e.p-ascii)
  282.   luaL_callmeta         (lua_State.i, obj.l, e.p-ascii)
  283.   luaL_typerror         (lua_State.i, narg.l, tname.p-ascii)
  284.   luaL_argerror         (lua_State.i, numarg.l, extramsg.p-ascii)
  285.   luaL_checklstring     (lua_State.i, numarg.l, size.l)
  286.   luaL_optlstring       (lua_State.i, numarg.l, def.p-ascii, size.l)
  287.   luaL_checknumber      (lua_State.i, numarg.l)
  288.   luaL_optnumber        (lua_State.i, narg, LUA_NUMBER.d)
  289.  
  290.   luaL_checkinteger     (lua_State.i, numarg.l)
  291.   luaL_optinteger       (lua_State.i, narg.l, LUA_INTEGER.l)
  292.  
  293.   luaL_checkstack       (lua_State.i, sz.l, msg.p-ascii)
  294.   luaL_checktype        (lua_State.i, narg.l, t.l)
  295.   luaL_checkany         (lua_State.i, narg.l)
  296.  
  297.   luaL_newmetatable     (lua_State.i, tname.p-ascii)
  298.   luaL_checkudata       (lua_State.i, ud.l, tname.p-ascii)
  299.  
  300.   luaL_where            (lua_State.i, lvl.l)
  301.   ;luaL_error            (lua_State.i, const char *fmt, ...)
  302.  
  303.   luaL_checkoption      (lua_State.i, narg.l, def.p-ascii, *string_array.l)
  304.  
  305.   luaL_ref              (lua_State.i, t.l)
  306.   luaL_unref            (lua_State.i, t.l, ref.l)
  307.  
  308.   ;luaL_loadfile         (lua_State.i, filename.p-ascii)
  309.   luaL_loadfilex        (lua_State.i, filename.p-ascii, *mode)
  310.  
  311.   luaL_loadbuffer       (lua_State.i, buff.l, size.l, name.p-ascii)
  312.   luaL_loadstring       (lua_State.i, string.p-ascii)
  313.  
  314.   luaL_newstate         ()
  315.  
  316.   luaL_gsub             (lua_State.i, s.p-ascii, p.p-ascii, r.p-ascii)
  317.  
  318.   luaL_findtable        (lua_State.i, Index.l, fname.p-ascii)
  319.  
  320.   luaL_buffinit         (lua_State.i, *luaL_Buffer.l)
  321.   luaL_prepbuffer       (*luaL_Buffer.l)
  322.   luaL_addlstring       (*luaL_Buffer.l, s.p-ascii, size.l)
  323.   luaL_addstring        (*luaL_Buffer.l, s.p-ascii)
  324.   luaL_addvalue         (*luaL_Buffer.l)
  325.   luaL_pushresult       (*luaL_Buffer.l)
  326. EndImport
  327.  
  328. ; ########################################## Macros #############################################
  329.  
  330. Macro lua_call(L, n, r)
  331.   lua_callk((L), (n), (r), 0, #Null)
  332. EndMacro
  333.  
  334. Macro lua_pcall(L, n, r, f)
  335.   lua_pcallk((L), (n), (r), (f), 0, #Null)
  336. EndMacro
  337.  
  338. Macro lua_yield(L, n)
  339.   lua_yieldk((L), (n), 0, #Null)
  340. EndMacro
  341.  
  342. Procedure.d lua_tonumber(L, i)
  343.   Protected Value.d = lua_tonumberx((L), (i), #Null)
  344.   CompilerIf #PB_Compiler_OS = #PB_OS_Windows And #PB_Compiler_Processor = #PB_Processor_x64
  345.     EnableASM
  346.     MOVQ Value, XMM0
  347.     DisableASM
  348.   CompilerEndIf
  349.   ProcedureReturn Value
  350. EndProcedure
  351.  
  352. Macro lua_tointeger(L, i)
  353.   lua_tointegerx((L), (i), #Null)
  354. EndMacro
  355.  
  356. Macro lua_tounsigned(L, i)
  357.   lua_tounsignedx((L), (i), #Null)
  358. EndMacro
  359.  
  360. Macro lua_pop(L, n)
  361.   lua_settop((L), -(n)-1)
  362. EndMacro
  363.  
  364. Macro lua_newtable(L)
  365.   lua_createtable((L), 0, 0)
  366. EndMacro
  367.  
  368. Macro lua_register(L, n, f)
  369.   lua_pushcfunction((L), (f))
  370.   lua_setglobal((L), n)
  371. EndMacro
  372.  
  373. Macro lua_pushcfunction(L, f)
  374.   lua_pushcclosure((L), (f), 0)
  375. EndMacro
  376.  
  377. Macro lua_isfunction(L, n)
  378.   (lua_type((L), (n)) = #LUA_TFUNCTION)
  379. EndMacro
  380.  
  381. Macro lua_istable(L, n)
  382.   (lua_type((L), (n)) = #LUA_TTABLE)
  383. EndMacro
  384.  
  385. Macro lua_islightuserdata(L, n)
  386.   (lua_type((L), (n)) = #LUA_TLIGHTUSERDATA)
  387. EndMacro
  388.  
  389. Macro lua_isnil(L, n)
  390.   (lua_type((L), (n)) = #LUA_TNIL)
  391. EndMacro
  392.  
  393. Macro lua_isboolean(L, n)
  394.   (lua_type((L), (n)) = #LUA_TBOOLEAN)
  395. EndMacro
  396.  
  397. Macro lua_isthread(L, n)
  398.   (lua_type((L), (n)) = #LUA_TTHREAD)
  399. EndMacro
  400.  
  401. Macro lua_isnone(L, n)
  402.   (lua_type((L), (n)) = #LUA_TNONE)
  403. EndMacro
  404.  
  405. Macro lua_isnoneornil(L, n)
  406.   (lua_type((L), (n)) <= 0)
  407. EndMacro
  408.  
  409. ;#define lua_pushliteral(L, s)  \
  410. ;   lua_pushlstring(L, "" s, (SizeOf(s)/SizeOf(char))-1)
  411.  
  412. ;#define lua_pushglobaltable(L)  \
  413. ;   lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
  414.  
  415. Macro lua_tostring(Result_String, Lua_State, idx)
  416.   *Temp_String = lua_tolstring(Lua_State, idx, #Null)
  417.   If *Temp_String : Result_String = PeekS(*Temp_String, -1, #PB_Ascii) : Else : Result_String = "" : EndIf
  418. EndMacro
  419.  
  420. Macro luaL_dofile(Lua_State, Filename)
  421.     luaL_loadfile(Lua_State, Filename)
  422.     lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
  423. EndMacro
  424.  
  425. Macro luaL_dostring(Lua_State, String)
  426.     luaL_loadstring(Lua_State, String)
  427.     lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
  428. EndMacro
  429.  
  430. ;Macro lua_setglobal(Lua_State, String)
  431. ;   lua_setfield(Lua_State, #LUA_GLOBALSINDEX, String)
  432. ;EndMacro
  433.  
  434. ;Macro lua_getglobal(Lua_State, String)
  435. ;   lua_getfield(Lua_State, #LUA_GLOBALSINDEX, String)
  436. ;EndMacro
  437.  
  438. ;Macro lua_call(Lua_State, nargs, nresults)
  439. ;  lua_callk(Lua_State, nargs, nresults, 0, #Null)
  440. ;EndMacro
  441.  
  442. ;Macro lua_pcall(Lua_State, nargs, nresults, lua_errCFunction)
  443. ;  lua_pcallk(Lua_State, nargs, nresults, lua_errCFunction, 0, #Null)
  444. ;EndMacro
  445.  
  446. Macro luaL_loadfile(L, f)
  447.   luaL_loadfilex((L),f,#Null)
  448. EndMacro
  449.  
  450. Macro lua_getglobal_fixed(L, String) ; Because of a memory leak with .p-ascii
  451.   Protected *String_Buffer = AllocateMemory(StringByteLength(String, #PB_Ascii)+1)
  452.   If *String_Buffer
  453.     PokeS(*String_Buffer, String, -1, #PB_Ascii)
  454.   EndIf
  455.   lua_getglobal(Lua_Main\State, *String_Buffer)
  456.   FreeMemory(*String_Buffer)
  457. EndMacro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement