Dadido3

Lua 5.2.1

Jan 27th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ########################################## Konstanten ########################################
  2.  
  3. #LUA_VERSION_MAJOR    = "5"
  4. #LUA_VERSION_MINOR    = "2"
  5. #LUA_VERSION_NUM      = 502
  6. #LUA_VERSION_RELEASE  = "1"
  7.  
  8. #LUA_VERSION          = "Lua "+#LUA_VERSION_MAJOR+"."+#LUA_VERSION_MINOR
  9. #LUA_RELEASE          = #LUA_VERSION+"."+#LUA_VERSION_RELEASE
  10. #LUA_COPYRIGHT        = #LUA_RELEASE+"  Copyright (C) 1994-2012 Lua.org, PUC-Rio"
  11. #LUA_AUTHORS          = "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
  12.  
  13. ; mark For precompiled code ('<esc>Lua')
  14. #LUA_SIGNATURE        = "\033Lua"
  15.  
  16. ; option For multiple returns in 'lua_pcall' And 'lua_call'
  17. #LUA_MULTRET          = -1
  18.  
  19. ; thread status
  20. #LUA_OK               = 0
  21. #LUA_YIELD            = 1
  22. #LUA_ERRRUN           = 2
  23. #LUA_ERRSYNTAX        = 3
  24. #LUA_ERRMEM           = 4
  25. #LUA_ERRGCMM          = 5
  26. #LUA_ERRERR           = 6
  27.  
  28. ; basic types
  29. #LUA_TNONE            = -1
  30.  
  31. #LUA_TNIL             = 0
  32. #LUA_TBOOLEAN         = 1
  33. #LUA_TLIGHTUSERDATA   = 2
  34. #LUA_TNUMBER          = 3
  35. #LUA_TSTRING          = 4
  36. #LUA_TTABLE           = 5
  37. #LUA_TFUNCTION        = 6
  38. #LUA_TUSERDATA        = 7
  39. #LUA_TTHREAD          = 8
  40.  
  41. #LUA_NUMTAGS          = 9
  42.  
  43. ; minimum Lua stack available To a C function
  44. #LUA_MINSTACK         = 20
  45.  
  46. ; predefined values in the registry
  47. #LUA_RIDX_MAINTHREAD  = 1
  48. #LUA_RIDX_GLOBALS     = 2
  49. #LUA_RIDX_LAST        = #LUA_RIDX_GLOBALS
  50.  
  51. ; Comparison And arithmetic functions
  52. #LUA_OPADD            = 0  ; ORDER TM
  53. #LUA_OPSUB            = 1
  54. #LUA_OPMUL            = 2
  55. #LUA_OPDIV            = 3
  56. #LUA_OPMOD            = 4
  57. #LUA_OPPOW            = 5
  58. #LUA_OPUNM            = 6
  59.  
  60. #LUA_OPEQ             = 0
  61. #LUA_OPLT             = 1
  62. #LUA_OPLE             = 2
  63.  
  64. ; garbage-collection function And options
  65. #LUA_GCSTOP           = 0
  66. #LUA_GCRESTART        = 1
  67. #LUA_GCCOLLECT        = 2
  68. #LUA_GCCOUNT          = 3
  69. #LUA_GCCOUNTB         = 4
  70. #LUA_GCSTEP           = 5
  71. #LUA_GCSETPAUSE       = 6
  72. #LUA_GCSETSTEPMUL     = 7
  73. #LUA_GCSETMAJORINC    = 8
  74. #LUA_GCISRUNNING      = 9
  75. #LUA_GCGEN            = 10
  76. #LUA_GCINC            = 11
  77.  
  78. ; pseudo-indices
  79. #LUA_REGISTRYINDEX    = (-10000)
  80. #LUA_ENVIRONINDEX     = (-10001)
  81. #LUA_GLOBALSINDEX     = (-10002)
  82.  
  83. ; Event codes
  84. #LUA_HOOKCALL         = 0
  85. #LUA_HOOKRET          = 1
  86. #LUA_HOOKLINE         = 2
  87. #LUA_HOOKCOUNT        = 3
  88. #LUA_HOOKTAILRET      = 4
  89.  
  90. ;  Event masks
  91. #LUA_MASKCALL         = 1 << #LUA_HOOKCALL
  92. #LUA_MASKRET          = 1 << #LUA_HOOKRET
  93. #LUA_MASKLINE         = 1 << #LUA_HOOKLINE
  94. #LUA_MASKCOUNT        = 1 << #LUA_HOOKCOUNT
  95.  
  96. ; ########################################## Variablen ##########################################
  97.  
  98. ; ########################################## Ladekram ############################################
  99.  
  100. ; ########################################## Declares ############################################
  101.  
  102. Declare   Lua_Do_Function(Function.s, Arguments, Results)
  103.  
  104. ; ########################################## Imports ##########################################
  105.  
  106. CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  107.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  108.     ;#Lua_Import_Prefix = "_"
  109.     ImportC #Library_Path+"lua52.x86.lib" ; Windows x86
  110.   CompilerElse
  111.     ;#Lua_Import_Prefix = ""
  112.     ImportC #Library_Path+"lua52.x64.lib" ; Windows x64
  113.   CompilerEndIf
  114. CompilerElse
  115.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  116.     ;#Lua_Import_Prefix = ""
  117.     Import "/usr/lib/libm.so"
  118.     EndImport
  119.     Import "/usr/lib/libdl.so"
  120.     EndImport
  121.     ImportC "../../Librarys/lua52.x86.a" ;     Linux x86
  122.   CompilerElse
  123.     ;#Lua_Import_Prefix = ""
  124.     ImportC "../../Librarys/lua52.x64.a" ;     Linux x64
  125.   CompilerEndIf
  126. CompilerEndIf
  127.  
  128.   ; /*
  129.   ; ** state manipulation
  130.   ; */
  131.   lua_newstate    (*f, *ud)
  132.   lua_close       (lua_State.i)
  133.   lua_newthread   (lua_State.i)
  134.  
  135.   lua_atpanic     (lua_State.i, *panicf)
  136.  
  137.   ; /*
  138.   ; ** basic stack manipulation
  139.   ; */
  140.   lua_absindex    (lua_State.i, idx.l)
  141.   lua_gettop      (lua_State.i)
  142.   lua_settop      (lua_State.i, idx.l)
  143.   lua_pushvalue   (lua_State.i, idx.l)
  144.   lua_remove      (lua_State.i, idx.l)
  145.   lua_insert      (lua_State.i, idx.l)
  146.   lua_replace     (lua_State.i, idx.l)
  147.   lua_copy        (lua_State.i, fromidx.l, toidx.l)
  148.   lua_checkstack  (lua_State.i, sz.l)
  149.  
  150.   lua_xmove       (lua_State_from.i, lua_State_to.i, n.l)
  151.  
  152.   ; /*
  153.   ; ** access functions (stack -> C)
  154.   ; */
  155.  
  156.   lua_isnumber    (lua_State.i, idx.l)
  157.   lua_isstring    (lua_State.i, idx.l)
  158.   lua_iscfunction (lua_State.i, idx.l)
  159.   lua_isuserdata  (lua_State.i, idx.l)
  160.   lua_type        (lua_State.i, idx.l)
  161.   lua_typename    (lua_State.i, tp.l)
  162.  
  163.   lua_tonumberx.d (lua_State.i, idx.l, *isnum)
  164.   lua_tointegerx  (lua_State.i, idx.l, *isnum)
  165.   lua_tounsignedx (lua_State.i, idx.l, *isnum)
  166.   lua_toboolean   (lua_State.i, idx.l)
  167.   lua_tolstring   (lua_State.i, idx.l, *len)
  168.   lua_rawlen      (lua_State.i, idx.l)
  169.   lua_tocfunction (lua_State.i, idx.l)
  170.   lua_touserdata  (lua_State.i, idx.l)
  171.   lua_tothread    (lua_State.i, idx.l)
  172.   lua_topointer   (lua_State.i, idx.l)
  173.  
  174.   ; /*
  175.   ; ** Comparison And arithmetic functions
  176.   ; */
  177.  
  178.   lua_arith       (lua_State.i, op.l)
  179.  
  180.   lua_rawequal    (lua_State.i, idx1.l, idx2.l)
  181.   lua_compare     (lua_State.i, idx1.l, idx2.l, op.l)
  182.  
  183.   ; /*
  184.   ; ** push functions (C -> stack)
  185.   ; */
  186.   lua_pushnil           (lua_State.i)
  187.   lua_pushnumber        (lua_State.i, n.d)
  188.   lua_pushinteger       (lua_State.i, n.q)
  189.   lua_pushunsigned      (lua_State.i, n.q)
  190.   lua_pushlstring       (lua_State.i, string.p-ascii, size.i)
  191.   lua_pushstring        (lua_State.i, string.p-ascii)
  192.   ;lua_pushvfstring      (lua_State.i, string.p-ascii, *argp)
  193.   ;lua_pushfstring       (lua_State.i, string.p-ascii, ...)
  194.   lua_pushcclosure      (lua_State.i, *fn, n.l)
  195.   lua_pushboolean       (lua_State.i, b.l)
  196.   lua_pushlightuserdata (lua_State.i, *p)
  197.   lua_pushthread        (lua_State.i)
  198.  
  199.   ; /*
  200.   ; ** get functions (Lua -> stack)
  201.   ; */
  202.   lua_getglobal         (lua_State.i, var.p-ascii)
  203.   lua_gettable          (lua_State.i, idx.l)
  204.   lua_getfield          (lua_State.i, idx.l, k.p-ascii)
  205.   lua_rawget            (lua_State.i, idx.l)
  206.   lua_rawgeti           (lua_State.i, idx.l, n.l)
  207.   lua_rawgetp           (lua_State.i, idx.l, p.p-ascii)
  208.   lua_createtable       (lua_State.i, narr.l, nrec.l)
  209.   lua_newuserdata       (lua_State.i, sz.i)
  210.   lua_getmetatable      (lua_State.i, objindex.l)
  211.   lua_getuservalue      (lua_State.i, idx.l)
  212.  
  213.   ; /*
  214.   ; ** set functions (stack -> Lua)
  215.   ; */
  216.   lua_setglobal         (lua_State.i, var.p-ascii)
  217.   lua_settable          (lua_State.i, idx.l)
  218.   lua_setfield          (lua_State.i, idx.l, k.p-ascii)
  219.   lua_rawset            (lua_State.i, idx.l)
  220.   lua_rawseti           (lua_State.i, idx.l, n.l)
  221.   lua_rawsetp           (lua_State.i, idx.l, p.p-ascii)
  222.   lua_setmetatable      (lua_State.i, objindex.l)
  223.   lua_setuservalue      (lua_State.i, idx.l)
  224.  
  225.   ; /*
  226.   ; ** 'load' And 'call' functions (load And run Lua code)
  227.   ; */
  228.   lua_callk             (lua_State.i, nargs.l, nresults.l, ctx.l, *k)
  229.   lua_getctx            (lua_State.i, *ctx)
  230.   lua_pcallk            (lua_State.i, nargs.l, nresults.l, *errfunc, ctx.l, *k)
  231.  
  232.   lua_load              (lua_State.i, reader.i, *dt, chunkname.p-ascii, mode.p-ascii)
  233.   lua_dump              (lua_State.i, writer.i, *data_)
  234.  
  235.   ; /*
  236.   ; ** coroutine functions
  237.   ; */
  238.   lua_yieldk            (lua_State.i, nresults.l, ctx.l, *k)
  239.  
  240.   lua_resume            (lua_State.i, lua_State_from.i, narg.l)
  241.   lua_status            (lua_State.i)
  242.  
  243.   ; /*
  244.   ; ** garbage-collection function And options
  245.   ; */
  246.   lua_gc                (lua_State.i, what.l, data_.i)
  247.  
  248.   ; /*
  249.   ; ** miscellaneous functions
  250.   ; */
  251.   lua_error             (lua_State.i)
  252.  
  253.   lua_next              (lua_State.i, idx.l)
  254.  
  255.   lua_concat            (lua_State.i, n.l)
  256.   lua_len               (lua_State.i, idx.l)
  257.  
  258.   lua_getallocf         (lua_State.i, *ud)
  259.   lua_setallocf         (lua_State.i, *f, *ud)
  260.  
  261.   ; lualib.h
  262.   luaopen_base          (lua_State.i)
  263.   luaopen_coroutine     (lua_State.i)
  264.   luaopen_table         (lua_State.i)
  265.   luaopen_io            (lua_State.i)
  266.   luaopen_os            (lua_State.i)
  267.   luaopen_string        (lua_State.i)
  268.   luaopen_bit32         (lua_State.i)
  269.   luaopen_math          (lua_State.i)
  270.   luaopen_debug         (lua_State.i)
  271.   luaopen_package       (lua_State.i)
  272.  
  273.   ; open all previous libraries
  274.   luaL_openlibs         (lua_State.i)
  275.  
  276.   ;lauxlib_h
  277.   luaI_openlib          (lua_State.i, libname.p-ascii, *luaL_Reg.l, nup.l)
  278.   luaL_register         (lua_State.i, libname.p-ascii, *luaL_Reg.l)
  279.   luaL_getmetafield     (lua_State.i, obj.l, e.p-ascii)
  280.   luaL_callmeta         (lua_State.i, obj.l, e.p-ascii)
  281.   luaL_typerror         (lua_State.i, narg.l, tname.p-ascii)
  282.   luaL_argerror         (lua_State.i, numarg.l, extramsg.p-ascii)
  283.   luaL_checklstring     (lua_State.i, numarg.l, size.l)
  284.   luaL_optlstring       (lua_State.i, numarg.l, def.p-ascii, size.l)
  285.   luaL_checknumber      (lua_State.i, numarg.l)
  286.   luaL_optnumber        (lua_State.i, narg, LUA_NUMBER.d)
  287.  
  288.   luaL_checkinteger     (lua_State.i, numarg.l)
  289.   luaL_optinteger       (lua_State.i, narg.l, LUA_INTEGER.l)
  290.  
  291.   luaL_checkstack       (lua_State.i, sz.l, msg.p-ascii)
  292.   luaL_checktype        (lua_State.i, narg.l, t.l)
  293.   luaL_checkany         (lua_State.i, narg.l)
  294.  
  295.   luaL_newmetatable     (lua_State.i, tname.p-ascii)
  296.   luaL_checkudata       (lua_State.i, ud.l, tname.p-ascii)
  297.  
  298.   luaL_where            (lua_State.i, lvl.l)
  299.   ;luaL_error            (lua_State.i, const char *fmt, ...)
  300.  
  301.   luaL_checkoption      (lua_State.i, narg.l, def.p-ascii, *string_array.l)
  302.  
  303.   luaL_ref              (lua_State.i, t.l)
  304.   luaL_unref            (lua_State.i, t.l, ref.l)
  305.  
  306.   ;luaL_loadfile         (lua_State.i, filename.p-ascii)
  307.   luaL_loadfilex        (lua_State.i, filename.p-ascii, *mode)
  308.  
  309.   luaL_loadbuffer       (lua_State.i, buff.l, size.l, name.p-ascii)
  310.   luaL_loadstring       (lua_State.i, string.p-ascii)
  311.  
  312.   luaL_newstate         ()
  313.  
  314.   luaL_gsub             (lua_State.i, s.p-ascii, p.p-ascii, r.p-ascii)
  315.  
  316.   luaL_findtable        (lua_State.i, Index.l, fname.p-ascii)
  317.  
  318.   luaL_buffinit         (lua_State.i, *luaL_Buffer.l)
  319.   luaL_prepbuffer       (*luaL_Buffer.l)
  320.   luaL_addlstring       (*luaL_Buffer.l, s.p-ascii, size.l)
  321.   luaL_addstring        (*luaL_Buffer.l, s.p-ascii)
  322.   luaL_addvalue         (*luaL_Buffer.l)
  323.   luaL_pushresult       (*luaL_Buffer.l)
  324. EndImport
  325.  
  326. ; ########################################## Macros #############################################
  327.  
  328. Macro lua_call(L, n, r)
  329.   lua_callk((L), (n), (r), 0, #Null)
  330. EndMacro
  331.  
  332. Macro lua_pcall(L, n, r, f)
  333.   lua_pcallk((L), (n), (r), (f), 0, #Null)
  334. EndMacro
  335.  
  336. Macro lua_yield(L, n)
  337.   lua_yieldk((L), (n), 0, #Null)
  338. EndMacro
  339.  
  340. Procedure.d lua_tonumber(L, i)
  341.   ; Because of different ABI
  342.   Protected Value.d = lua_tonumberx((L), (i), #Null)
  343.   CompilerIf #PB_Compiler_OS = #PB_OS_Windows And #PB_Compiler_Processor = #PB_Processor_x64
  344.     EnableASM
  345.     MOVQ Value, XMM0
  346.     DisableASM
  347.   CompilerEndIf
  348.   ProcedureReturn Value
  349. EndProcedure
  350.  
  351. Macro lua_tointeger(L, i)
  352.   lua_tointegerx((L), (i), #Null)
  353. EndMacro
  354.  
  355. Macro lua_tounsigned(L, i)
  356.   lua_tounsignedx((L), (i), #Null)
  357. EndMacro
  358.  
  359. Macro lua_pop(L, n)
  360.   lua_settop((L), -(n)-1)
  361. EndMacro
  362.  
  363. Macro lua_newtable(L)
  364.   lua_createtable((L), 0, 0)
  365. EndMacro
  366.  
  367. Macro lua_register(L, n, f)
  368.   lua_pushcfunction((L), (f))
  369.   lua_setglobal((L), n)
  370. EndMacro
  371.  
  372. Macro lua_pushcfunction(L, f)
  373.   lua_pushcclosure((L), (f), 0)
  374. EndMacro
  375.  
  376. Macro lua_isfunction(L, n)
  377.   (lua_type((L), (n)) = #LUA_TFUNCTION)
  378. EndMacro
  379.  
  380. Macro lua_istable(L, n)
  381.   (lua_type((L), (n)) = #LUA_TTABLE)
  382. EndMacro
  383.  
  384. Macro lua_islightuserdata(L, n)
  385.   (lua_type((L), (n)) = #LUA_TLIGHTUSERDATA)
  386. EndMacro
  387.  
  388. Macro lua_isnil(L, n)
  389.   (lua_type((L), (n)) = #LUA_TNIL)
  390. EndMacro
  391.  
  392. Macro lua_isboolean(L, n)
  393.   (lua_type((L), (n)) = #LUA_TBOOLEAN)
  394. EndMacro
  395.  
  396. Macro lua_isthread(L, n)
  397.   (lua_type((L), (n)) = #LUA_TTHREAD)
  398. EndMacro
  399.  
  400. Macro lua_isnone(L, n)
  401.   (lua_type((L), (n)) = #LUA_TNONE)
  402. EndMacro
  403.  
  404. Macro lua_isnoneornil(L, n)
  405.   (lua_type((L), (n)) <= 0)
  406. EndMacro
  407.  
  408. ;#define lua_pushliteral(L, s)  \
  409. ;  lua_pushlstring(L, "" s, (SizeOf(s)/SizeOf(char))-1)
  410.  
  411. ;#define lua_pushglobaltable(L)  \
  412. ;  lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
  413.  
  414. Macro lua_tostring(Result_String, Lua_State, idx)
  415.   *Temp_String = lua_tolstring(Lua_State, idx, #Null)
  416.   If *Temp_String : Result_String = PeekS(*Temp_String, -1, #PB_Ascii) : Else : Result_String = "" : EndIf
  417. EndMacro
  418.  
  419. Macro luaL_dofile(Lua_State, Filename)
  420.   luaL_loadfile(Lua_State, Filename)
  421.   lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
  422. EndMacro
  423.  
  424. Macro luaL_dostring(Lua_State, String)
  425.   luaL_loadstring(Lua_State, String)
  426.   lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
  427. EndMacro
  428.  
  429. ;Macro lua_setglobal(Lua_State, String)
  430. ;  lua_setfield(Lua_State, #LUA_GLOBALSINDEX, String)
  431. ;EndMacro
  432.  
  433. ;Macro lua_getglobal(Lua_State, String)
  434. ;  lua_getfield(Lua_State, #LUA_GLOBALSINDEX, String)
  435. ;EndMacro
  436.  
  437. ;Macro lua_call(Lua_State, nargs, nresults)
  438. ;  lua_callk(Lua_State, nargs, nresults, 0, #Null)
  439. ;EndMacro
  440.  
  441. ;Macro lua_pcall(Lua_State, nargs, nresults, lua_errCFunction)
  442. ;  lua_pcallk(Lua_State, nargs, nresults, lua_errCFunction, 0, #Null)
  443. ;EndMacro
  444.  
  445. Macro luaL_loadfile(L, f)
  446.   luaL_loadfilex((L),f,#Null)
  447. EndMacro
  448.  
  449. ;Macro lua_getglobal_fixed(L, String)
  450. ;  ; Fix for memory leak problem with pseudotypes (Is probably fixed in the newer PB versions)
  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
  458.  
  459. ; #################################### Custom Functions #################################################
  460.  
  461. Procedure Lua_SetVariable_String(Name.s, String.s)
  462.   If Not Lua_Main\State
  463.     ProcedureReturn #Result_Fail
  464.   EndIf
  465.  
  466.   lua_pushstring(Lua_Main\State, String)
  467.   lua_setglobal(Lua_Main\State, Name)
  468.  
  469.   ProcedureReturn #Result_Success
  470. EndProcedure
  471.  
  472. Procedure Lua_SetVariable_Integer(Name.s, Value)
  473.   If Not Lua_Main\State
  474.     ProcedureReturn #Result_Fail
  475.   EndIf
  476.  
  477.   lua_pushinteger(Lua_Main\State, Value)
  478.   lua_setglobal(Lua_Main\State, Name)
  479.  
  480.   ProcedureReturn #Result_Success
  481. EndProcedure
  482.  
  483. Procedure.s Lua_GetVariable_String(Name.s)
  484.   Protected Result.s = ""
  485.  
  486.   If Not Lua_Main\State
  487.     ProcedureReturn ""
  488.   EndIf
  489.  
  490.   lua_getglobal(Lua_Main\State, Name)
  491.   lua_tostring(Result, Lua_Main\State, -1)
  492.   lua_pop(Lua_Main\State, 1)
  493.  
  494.   ProcedureReturn Result
  495. EndProcedure
  496.  
  497. Procedure Lua_GetVariable_Integer(Name.s)
  498.   Protected Result
  499.  
  500.   If Not Lua_Main\State
  501.     ProcedureReturn #Result_Fail
  502.   EndIf
  503.  
  504.   lua_getglobal_fixed(Lua_Main\State, Name)
  505.   Result = lua_tointeger(Lua_Main\State, -1)
  506.   Lua_pop(Lua_Main\State, 1)
  507.  
  508.   ProcedureReturn Result
  509. EndProcedure
  510.  
  511. Procedure Lua_Do_Function(Function.s, Arguments, Results)
  512.   Protected Error_Message.s
  513.  
  514.   If Not Lua_Main\State
  515.     ProcedureReturn #Result_Fail
  516.   EndIf
  517.  
  518.   Result = lua_pcall(Lua_Main\State, Arguments, Results, 0)
  519.  
  520.   If Result
  521.     lua_tostring(Error_Message, Lua_Main\State, -1)
  522.     Select Result
  523.       Case #LUA_ERRRUN : Log_Add_Warn("Runtimeerror in "+Function+" ("+Error_Message+")")
  524.       Case #LUA_ERRMEM : Log_Add_Warn("Memoryallocationerror in "+Function+" ("+Error_Message+")")
  525.       Case #LUA_ERRERR : Log_Add_Warn("Error in "+Function+" ("+Error_Message+")")
  526.       Default          : Log_Add_Warn("Undefined Error in "+Function+" ("+Error_Message+")")
  527.     EndSelect
  528.     lua_pop(Lua_Main\State, 1)
  529.     ProcedureReturn #Result_Fail
  530.   EndIf
  531.  
  532.   ; #### The caller needs to pop the Results from the stack
  533.   ProcedureReturn #Result_Success
  534. EndProcedure
  535.  
  536. Procedure Lua_Do_String(String.s)
  537.   Protected Error_Message.s
  538.  
  539.   If Not Lua_Main\State
  540.     ProcedureReturn #Result_Fail
  541.   EndIf
  542.  
  543.   luaL_loadstring(Lua_Main\State, String)
  544.   ;Result = lua_pcall(Lua_Main\State, 0, #LUA_MULTRET, 0)
  545.   Result = lua_pcall(Lua_Main\State, 0, 0, 0)
  546.  
  547.   If Result
  548.     lua_tostring(Error_Message, Lua_Main\State, -1)
  549.     Select Result
  550.       Case #LUA_ERRRUN : Log_Add_Warn("Runtimeerror with '"+String+"' ("+Error_Message+")")
  551.       Case #LUA_ERRMEM : Log_Add_Warn("Memoryallocationerror with '"+String+"' ("+Error_Message+")")
  552.       Case #LUA_ERRERR : Log_Add_Warn("Error with '"+String+"' ("+Error_Message+")")
  553.       Default          : Log_Add_Warn("Undefined Error with '"+String+"' ("+Error_Message+")")
  554.     EndSelect
  555.     lua_pop(Lua_Main\State, 1)
  556.     ProcedureReturn #Result_Fail
  557.   EndIf
  558.  
  559.   ProcedureReturn #Result_Success
  560. EndProcedure
  561.  
  562. Procedure Lua_Do_File(Filename.s)
  563.   Protected Error_Message.s
  564.  
  565.   If Not Lua_Main\State
  566.     ProcedureReturn #Result_Fail
  567.   EndIf
  568.  
  569.   luaL_loadfile(Lua_Main\State, Filename)
  570.   ;Result = lua_pcall(Lua_Main\State, 0, #LUA_MULTRET, 0)
  571.   Result = lua_pcall(Lua_Main\State, 0, 0, 0)
  572.  
  573.   If Result
  574.     lua_tostring(Error_Message, Lua_Main\State, -1)
  575.     Select Result
  576.       Case #LUA_ERRRUN : Log_Add_Warn("Runtimeerror in "+Filename+" ("+Error_Message+")")
  577.       Case #LUA_ERRMEM : Log_Add_Warn("Memoryallocationerror in "+Filename+" ("+Error_Message+")")
  578.       Case #LUA_ERRERR : Log_Add_Warn("Error in "+Filename+" ("+Error_Message+")")
  579.       Default          : Log_Add_Warn("Undefined Error in "+Filename+" ("+Error_Message+")")
  580.     EndSelect
  581.     lua_pop(Lua_Main\State, 1)
  582.     ProcedureReturn #Result_Fail
  583.   EndIf
  584.  
  585.   ProcedureReturn #Result_Success
  586. EndProcedure
  587.  
  588. Procedure Lua_Init()
  589.   Lua_Main\State = luaL_newstate()
  590.  
  591.   If Not Lua_Main\State
  592.     Log_Add_Info("Lua not loaded")
  593.     ProcedureReturn #Result_Fail
  594.   EndIf
  595.  
  596.   ;luaopen_base(Lua_Main\State)
  597.   ;luaopen_table(Lua_Main\State)
  598.  
  599.   ;luaopen_base          (Lua_Main\State)
  600.   ;luaopen_coroutine     (Lua_Main\State)
  601.   ;luaopen_table         (Lua_Main\State)
  602.   ;luaopen_io            (Lua_Main\State)
  603.   ;luaopen_os            (Lua_Main\State)
  604.   ;luaopen_string        (Lua_Main\State)
  605.   ;luaopen_bit32         (Lua_Main\State)
  606.   ;luaopen_math          (Lua_Main\State)
  607.   ;luaopen_debug         (Lua_Main\State)
  608.   ;luaopen_package       (Lua_Main\State)
  609.  
  610.   ;luaL_openlibs(Lua_Main\State)
  611.  
  612.   lua_pushcclosure(Lua_Main\State, @luaL_openlibs(), 0)
  613.   lua_call(Lua_Main\State, 0, 0)
  614.  
  615.   ;luaopen_os(Lua_Main\State)
  616.   ;luaopen_string(Lua_Main\State)
  617.   ;luaopen_math(Lua_Main\State)
  618.   ;luaopen_debug(Lua_Main\State)
  619.   ;luaopen_package(Lua_Main\State)
  620.   ;lua_pushcclosure(Lua_Main\State, @luaopen_package(), 0)
  621.   ;lua_call(Lua_Main\State, 0, 0)
  622.  
  623.   Lua_Register_All()
  624.  
  625.   Log_Add_Info("Lua loaded")
  626.  
  627.   ProcedureReturn #Result_Success
  628. EndProcedure
Advertisement
Add Comment
Please, Sign In to add comment