lua.odin 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package lua_5_2
  2. import "base:intrinsics"
  3. import "base:builtin"
  4. import c "core:c/libc"
  5. #assert(size_of(c.int) == size_of(b32))
  6. LUA_SHARED :: #config(LUA_SHARED, false)
  7. when LUA_SHARED {
  8. when ODIN_OS == .Windows {
  9. // Does nothing special on windows
  10. foreign import lib "windows/lua52dll.lib"
  11. } else when ODIN_OS == .Linux {
  12. foreign import lib "linux/liblua52.so"
  13. } else {
  14. foreign import lib "system:lua5.2"
  15. }
  16. } else {
  17. when ODIN_OS == .Windows {
  18. foreign import lib "windows/lua52dll.lib"
  19. } else when ODIN_OS == .Linux {
  20. foreign import lib "linux/liblua52.a"
  21. } else {
  22. foreign import lib "system:lua5.2"
  23. }
  24. }
  25. VERSION_MAJOR :: "5"
  26. VERSION_MINOR :: "2"
  27. VERSION_NUM :: 502
  28. VERSION_RELEASE :: "4"
  29. VERSION :: "Lua " + VERSION_MAJOR + "." + VERSION_MINOR
  30. RELEASE :: VERSION + "." + VERSION_RELEASE
  31. COPYRIGHT :: RELEASE + " Copyright (C) 1994-2015 Lua.org, PUC-Rio"
  32. AUTHORS :: "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
  33. /* mark for precompiled code ('<esc>Lua') */
  34. SIGNATURE :: "\x1bLua"
  35. /* option for multiple returns in 'lua_pcall' and 'lua_call' */
  36. MULTRET :: -1
  37. FIRSTPSEUDOIDX :: -MAXSTACK - 1000
  38. REGISTRYINDEX :: -FIRSTPSEUDOIDX
  39. /*
  40. @@ LUAI_MAXSTACK limits the size of the Lua stack.
  41. ** CHANGE it if you need a different limit. This limit is arbitrary;
  42. ** its only purpose is to stop Lua from consuming unlimited stack
  43. ** space (and to reserve some numbers for pseudo-indices).
  44. ** (It must fit into max(size_t)/32.)
  45. */
  46. MAXSTACK :: 1000000
  47. /*
  48. @@ LUA_EXTRASPACE defines the size of a raw memory area associated with
  49. ** a Lua state with very fast access.
  50. ** CHANGE it if you need a different size.
  51. */
  52. EXTRASPACE :: size_of(rawptr)
  53. /*
  54. @@ LUA_IDSIZE gives the maximum size for the description of the source
  55. @@ of a function in debug information.
  56. ** CHANGE it if you want a different size.
  57. */
  58. IDSIZE :: 60
  59. /*
  60. @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
  61. */
  62. L_BUFFERSIZE :: c.int(16 * size_of(rawptr) * size_of(Number))
  63. MAXALIGNVAL :: max(align_of(Number), align_of(f64), align_of(rawptr), align_of(Integer), align_of(c.long))
  64. Status :: enum c.int {
  65. OK = 0,
  66. YIELD = 1,
  67. ERRRUN = 2,
  68. ERRSYNTAX = 3,
  69. ERRMEM = 4,
  70. ERRERR = 5,
  71. ERRGCMM = 6,
  72. ERRFILE = 7,
  73. }
  74. /* thread status */
  75. OK :: Status.OK
  76. YIELD :: Status.YIELD
  77. ERRRUN :: Status.ERRRUN
  78. ERRSYNTAX :: Status.ERRSYNTAX
  79. ERRMEM :: Status.ERRMEM
  80. ERRERR :: Status.ERRERR
  81. ERRFILE :: Status.ERRFILE
  82. /*
  83. ** basic types
  84. */
  85. Type :: enum c.int {
  86. NONE = -1,
  87. NIL = 0,
  88. BOOLEAN = 1,
  89. LIGHTUSERDATA = 2,
  90. NUMBER = 3,
  91. STRING = 4,
  92. TABLE = 5,
  93. FUNCTION = 6,
  94. USERDATA = 7,
  95. THREAD = 8,
  96. }
  97. TNONE :: Type.NONE
  98. TNIL :: Type.NIL
  99. TBOOLEAN :: Type.BOOLEAN
  100. TLIGHTUSERDATA :: Type.LIGHTUSERDATA
  101. TNUMBER :: Type.NUMBER
  102. TSTRING :: Type.STRING
  103. TTABLE :: Type.TABLE
  104. TFUNCTION :: Type.FUNCTION
  105. TUSERDATA :: Type.USERDATA
  106. TTHREAD :: Type.THREAD
  107. NUMTYPES :: 9
  108. ArithOp :: enum c.int {
  109. ADD = 0, /* ORDER TM, ORDER OP */
  110. SUB = 1,
  111. MUL = 2,
  112. DIV = 3,
  113. MOD = 4,
  114. POW = 5,
  115. UNM = 6,
  116. }
  117. CompareOp :: enum c.int {
  118. EQ = 0,
  119. LT = 1,
  120. LE = 2,
  121. }
  122. OPADD :: ArithOp.ADD
  123. OPSUB :: ArithOp.SUB
  124. OPMUL :: ArithOp.MUL
  125. OPDIV :: ArithOp.DIV
  126. OPMOD :: ArithOp.MOD
  127. OPPOW :: ArithOp.POW
  128. OPUNM :: ArithOp.UNM
  129. OPEQ :: CompareOp.EQ
  130. OPLT :: CompareOp.LT
  131. OPLE :: CompareOp.LE
  132. /* minimum Lua stack available to a C function */
  133. MINSTACK :: 20
  134. /* predefined values in the registry */
  135. RIDX_MAINTHREAD :: 1
  136. RIDX_GLOBALS :: 2
  137. RIDX_LAST :: RIDX_GLOBALS
  138. /* type of numbers in Lua */
  139. Number :: distinct (f32 when size_of(uintptr) == 4 else f64)
  140. /* type for integer functions */
  141. Integer :: distinct (i32 when size_of(uintptr) == 4 else i64)
  142. /* unsigned integer type */
  143. Unsigned :: distinct (u32 when size_of(uintptr) == 4 else u64)
  144. /*
  145. ** Type for C functions registered with Lua
  146. */
  147. CFunction :: #type proc "c" (L: ^State) -> c.int
  148. /*
  149. ** Type for functions that read/write blocks when loading/dumping Lua chunks
  150. */
  151. Reader :: #type proc "c" (L: ^State, ud: rawptr, sz: ^c.size_t) -> cstring
  152. Writer :: #type proc "c" (L: ^State, p: rawptr, sz: ^c.size_t, ud: rawptr) -> c.int
  153. /*
  154. ** Type for memory-allocation functions
  155. */
  156. Alloc :: #type proc "c" (ud: rawptr, ptr: rawptr, osize, nsize: c.size_t) -> rawptr
  157. GCWhat :: enum c.int {
  158. STOP = 0,
  159. RESTART = 1,
  160. COLLECT = 2,
  161. COUNT = 3,
  162. COUNTB = 4,
  163. STEP = 5,
  164. SETPAUSE = 6,
  165. SETSTEPMUL = 7,
  166. SETMAJORINC = 8,
  167. ISRUNNING = 9,
  168. GEN = 10,
  169. INC = 11,
  170. }
  171. GCSTOP :: GCWhat.STOP
  172. GCRESTART :: GCWhat.RESTART
  173. GCCOLLECT :: GCWhat.COLLECT
  174. GCCOUNT :: GCWhat.COUNT
  175. GCCOUNTB :: GCWhat.COUNTB
  176. GCSTEP :: GCWhat.STEP
  177. GCSETPAUSE :: GCWhat.SETPAUSE
  178. GCSETSTEPMUL :: GCWhat.SETSTEPMUL
  179. GCSETMAJORINC :: GCWhat.SETMAJORINC
  180. GCISRUNNING :: GCWhat.ISRUNNING
  181. GCGEN :: GCWhat.GEN
  182. GCINC :: GCWhat.INC
  183. /*
  184. ** Event codes
  185. */
  186. HookEvent :: enum c.int {
  187. CALL = 0,
  188. RET = 1,
  189. LINE = 2,
  190. COUNT = 3,
  191. TAILCALL = 4,
  192. }
  193. HOOKCALL :: HookEvent.CALL
  194. HOOKRET :: HookEvent.RET
  195. HOOKLINE :: HookEvent.LINE
  196. HOOKCOUNT :: HookEvent.COUNT
  197. HOOKTAILCALL :: HookEvent.TAILCALL
  198. /*
  199. ** Event masks
  200. */
  201. HookMask :: distinct bit_set[HookEvent; c.int]
  202. MASKCALL :: HookMask{.CALL}
  203. MASKRET :: HookMask{.RET}
  204. MASKLINE :: HookMask{.LINE}
  205. MASKCOUNT :: HookMask{.COUNT}
  206. /* activation record */
  207. Debug :: struct {
  208. event: HookEvent,
  209. name: cstring, /* (n) */
  210. namewhat: cstring, /* (n) 'global', 'local', 'field', 'method' */
  211. what: cstring, /* (S) 'Lua', 'C', 'main', 'tail' */
  212. source: cstring, /* (S) */
  213. currentline: c.int, /* (l) */
  214. linedefined: c.int, /* (S) */
  215. lastlinedefined: c.int, /* (S) */
  216. nups: u8, /* (u) number of upvalues */
  217. nparams: u8, /* (u) number of parameters */
  218. isvararg: bool, /* (u) */
  219. istailcall: bool, /* (t) */
  220. short_src: [IDSIZE]u8 `fmt:"s"`, /* (S) */
  221. /* private part */
  222. i_ci: rawptr, /* active function */
  223. }
  224. /* Functions to be called by the debugger in specific events */
  225. Hook :: #type proc "c" (L: ^State, ar: ^Debug)
  226. State :: struct {} // opaque data type
  227. @(link_prefix="lua_")
  228. @(default_calling_convention="c")
  229. foreign lib {
  230. /*
  231. ** RCS ident string
  232. */
  233. ident: [^]u8 // TODO(bill): is this correct?
  234. /*
  235. ** state manipulation
  236. */
  237. newstate :: proc(f: Alloc, ud: rawptr) -> ^State ---
  238. close :: proc(L: ^State) ---
  239. newthread :: proc(L: ^State) -> ^State ---
  240. atpanic :: proc(L: ^State, panicf: CFunction) -> CFunction ---
  241. version :: proc(L: ^State) -> ^Number ---
  242. /*
  243. ** basic stack manipulation
  244. */
  245. absindex :: proc (L: ^State, idx: c.int) -> c.int ---
  246. gettop :: proc (L: ^State) -> c.int ---
  247. settop :: proc (L: ^State, idx: c.int) ---
  248. pushvalue :: proc (L: ^State, idx: c.int) ---
  249. remove :: proc (L: ^State, idx: c.int) ---
  250. insert :: proc (L: ^State, idx: c.int) ---
  251. replace :: proc (L: ^State, idx: c.int) ---
  252. copy :: proc (L: ^State, fromidx, toidx: c.int) ---
  253. checkstack :: proc (L: ^State, sz: c.int) -> c.int ---
  254. xmove :: proc(from, to: ^State, n: c.int) ---
  255. /*
  256. ** access functions (stack -> C)
  257. */
  258. isnumber :: proc(L: ^State, idx: c.int) -> b32 ---
  259. isstring :: proc(L: ^State, idx: c.int) -> b32 ---
  260. iscfunction :: proc(L: ^State, idx: c.int) -> b32 ---
  261. isinteger :: proc(L: ^State, idx: c.int) -> b32 ---
  262. isuserdata :: proc(L: ^State, idx: c.int) -> b32 ---
  263. type :: proc(L: ^State, idx: c.int) -> Type ---
  264. typename :: proc(L: ^State, tp: Type) -> cstring ---
  265. @(link_name="lua_tonumberx")
  266. tonumber :: proc(L: ^State, idx: c.int, isnum: ^b32 = nil) -> Number ---
  267. @(link_name="lua_tointegerx")
  268. tointeger :: proc(L: ^State, idx: c.int, isnum: ^b32 = nil) -> Integer ---
  269. @(link_name="lua_tounsignedx")
  270. tounsigned :: proc(L: ^State, idx: c.int, isnum: ^b32 = nil) -> Unsigned ---
  271. toboolean :: proc(L: ^State, idx: c.int) -> b32 ---
  272. tolstring :: proc(L: ^State, idx: c.int, len: ^c.size_t) -> cstring ---
  273. rawlen :: proc(L: ^State, idx: c.int) -> c.size_t ---
  274. tocfunction :: proc(L: ^State, idx: c.int) -> CFunction ---
  275. touserdata :: proc(L: ^State, idx: c.int) -> rawptr ---
  276. tothread :: proc(L: ^State, idx: c.int) -> ^State ---
  277. topointer :: proc(L: ^State, idx: c.int) -> rawptr ---
  278. /*
  279. ** Comparison and arithmetic functions
  280. */
  281. arith :: proc(L: ^State, op: ArithOp) ---
  282. rawequal :: proc(L: ^State, idx1, idx2: c.int) -> b32 ---
  283. compare :: proc(L: ^State, idx1, idx2: c.int, op: CompareOp) -> b32 ---
  284. /*
  285. ** push functions (C -> stack)
  286. */
  287. pushnil :: proc(L: ^State) ---
  288. pushnumber :: proc(L: ^State, n: Number) ---
  289. pushinteger :: proc(L: ^State, n: Integer) ---
  290. pushunsigned :: proc(L: ^State, n: Unsigned) ---
  291. pushlstring :: proc(L: ^State, s: cstring, l: c.size_t) -> cstring ---
  292. pushstring :: proc(L: ^State, s: cstring) -> cstring ---
  293. pushvfstring :: proc(L: ^State, fmt: cstring, argp: c.va_list) -> cstring ---
  294. pushfstring :: proc(L: ^State, fmt: cstring, #c_vararg args: ..any) -> cstring ---
  295. pushcclosure :: proc(L: ^State, fn: CFunction, n: c.int) ---
  296. pushboolean :: proc(L: ^State, b: b32) ---
  297. pushlightuserdata :: proc(L: ^State, p: rawptr) ---
  298. pushthread :: proc(L: ^State) -> Status ---
  299. /*
  300. ** get functions (Lua -> stack)
  301. */
  302. getglobal :: proc(L: ^State, name: cstring) ---
  303. gettable :: proc(L: ^State, idx: c.int) ---
  304. getfield :: proc(L: ^State, idx: c.int, k: cstring) ---
  305. geti :: proc(L: ^State, idx: c.int, n: Integer) ---
  306. rawget :: proc(L: ^State, idx: c.int) ---
  307. rawgeti :: proc(L: ^State, idx: c.int, n: Integer) ---
  308. rawgetp :: proc(L: ^State, idx: c.int, p: rawptr) ---
  309. createtable :: proc(L: ^State, narr, nrec: c.int) ---
  310. newuserdata :: proc(L: ^State, sz: c.size_t) -> rawptr ---
  311. getmetatable :: proc(L: ^State, objindex: c.int) -> c.int ---
  312. getuservalue :: proc(L: ^State, idx: c.int) ---
  313. /*
  314. ** set functions (stack -> Lua)
  315. */
  316. setglobal :: proc(L: ^State, var: cstring) ---
  317. settable :: proc(L: ^State, idx: c.int) ---
  318. setfield :: proc(L: ^State, idx: c.int, k: cstring) ---
  319. rawset :: proc(L: ^State, idx: c.int) ---
  320. rawseti :: proc(L: ^State, idx: c.int, n: c.int) ---
  321. rawsetp :: proc(L: ^State, idx: c.int, p: rawptr) ---
  322. setmetatable :: proc(L: ^State, objindex: c.int) -> c.int ---
  323. setuservalue :: proc(L: ^State, idx: c.int) -> c.int ---
  324. /*
  325. ** 'load' and 'call' functions (load and run Lua code)
  326. */
  327. @(link_name="lua_callk")
  328. call :: proc(L: ^State, nargs, nresults: c.int, ctx: c.int = 0,
  329. k: CFunction = nil) ---
  330. getctx :: proc(L: ^State, ctx: ^c.int) -> c.int ---
  331. @(link_name="lua_pcallk")
  332. pcall :: proc(L: ^State, nargs, nresults: c.int, errfunc: c.int,
  333. ctx: c.int = 0, k: CFunction = nil) -> c.int ---
  334. load :: proc(L: ^State, reader: Reader, dt: rawptr,
  335. chunkname, mode: cstring) -> Status ---
  336. dump :: proc(L: ^State, writer: Writer, data: rawptr) -> Status ---
  337. /*
  338. ** coroutine functions
  339. */
  340. @(link_name="lua_yieldk")
  341. yield :: proc(L: ^State, nresults: c.int, ctx: c.int = 0, k: CFunction = nil) -> Status ---
  342. resume :: proc(L: ^State, from: ^State, narg: c.int) -> Status ---
  343. status :: proc(L: ^State) -> Status ---
  344. /*
  345. ** garbage-collection function and options
  346. */
  347. gc :: proc(L: ^State, what: GCWhat, data: c.int) -> c.int ---
  348. /*
  349. ** miscellaneous functions
  350. */
  351. error :: proc(L: ^State) -> Status ---
  352. next :: proc(L: ^State, idx: c.int) -> c.int ---
  353. concat :: proc(L: ^State, n: c.int) ---
  354. len :: proc(L: ^State, idx: c.int) ---
  355. getallocf :: proc(L: State, ud: ^rawptr) -> Alloc ---
  356. setallocf :: proc(L: ^State, f: Alloc, ud: rawptr) ---
  357. /*
  358. ** {======================================================================
  359. ** Debug API
  360. ** =======================================================================
  361. */
  362. getstack :: proc(L: ^State, level: c.int, ar: ^Debug) -> c.int ---
  363. getinfo :: proc(L: ^State, what: cstring, ar: ^Debug) -> c.int ---
  364. getlocal :: proc(L: ^State, ar: ^Debug, n: c.int) -> cstring ---
  365. setlocal :: proc(L: ^State, ar: ^Debug, n: c.int) -> cstring ---
  366. getupvalue :: proc(L: ^State, funcindex: c.int, n: c.int) -> cstring ---
  367. setupvalue :: proc(L: ^State, funcindex: c.int, n: c.int) -> cstring ---
  368. upvalueid :: proc(L: ^State, fidx, n: c.int) -> rawptr ---
  369. upvaluejoin :: proc(L: ^State, fidx1, n1, fidx2, n2: c.int) ---
  370. sethook :: proc(L: ^State, func: Hook, mask: HookMask, count: c.int) -> c.int ---
  371. gethook :: proc(L: ^State) -> Hook ---
  372. gethookmask :: proc(L: ^State) -> HookMask ---
  373. gethookcount :: proc(L: ^State) -> c.int ---
  374. /* }============================================================== */
  375. }
  376. /* version suffix for environment variable names */
  377. VERSUFFIX :: "_" + VERSION_MAJOR + "_" + VERSION_MINOR
  378. COLIBNAME :: "coroutine"
  379. TABLIBNAME :: "table"
  380. IOLIBNAME :: "io"
  381. OSLIBNAME :: "os"
  382. STRLIBNAME :: "string"
  383. UTF8LIBNAME :: "utf8"
  384. BITLIBNAME :: "bit32"
  385. MATHLIBNAME :: "math"
  386. DBLIBNAME :: "debug"
  387. LOADLIBNAME :: "package"
  388. @(link_prefix="lua")
  389. @(default_calling_convention="c")
  390. foreign lib {
  391. open_base :: proc(L: ^State) -> c.int ---
  392. open_coroutine :: proc(L: ^State) -> c.int ---
  393. open_table :: proc(L: ^State) -> c.int ---
  394. open_io :: proc(L: ^State) -> c.int ---
  395. open_os :: proc(L: ^State) -> c.int ---
  396. open_string :: proc(L: ^State) -> c.int ---
  397. open_utf8 :: proc(L: ^State) -> c.int ---
  398. open_bit32 :: proc(L: ^State) -> c.int ---
  399. open_math :: proc(L: ^State) -> c.int ---
  400. open_debug :: proc(L: ^State) -> c.int ---
  401. open_package :: proc(L: ^State) -> c.int ---
  402. /* open all previous libraries */
  403. L_openlibs :: proc(L: ^State) ---
  404. }
  405. GNAME :: "_G"
  406. L_Reg :: struct {
  407. name: cstring,
  408. func: CFunction,
  409. }
  410. /* predefined references */
  411. NOREF :: -2
  412. REFNIL :: -1
  413. @(link_prefix="lua")
  414. @(default_calling_convention="c")
  415. foreign lib {
  416. @(link_name="luaL_checkversion_")
  417. L_checkversion :: proc(L: ^State, ver: Number = VERSION_NUM) ---
  418. L_getmetafield :: proc(L: ^State, obj: c.int, e: cstring) -> c.int ---
  419. L_callmeta :: proc(L: ^State, obj: c.int, e: cstring) -> c.int ---
  420. @(link_name="luaL_tolstring")
  421. L_tostring :: proc(L: ^State, idx: c.int, len: ^c.size_t = nil) -> cstring ---
  422. L_argerror :: proc(L: ^State, numarg: c.int, extramsg: cstring) -> c.int ---
  423. @(link_name="luaL_checklstring")
  424. L_checkstring :: proc(L: ^State, numArg: c.int, l: ^c.size_t = nil) -> cstring ---
  425. @(link_name="luaL_optlstring")
  426. L_optstring :: proc(L: ^State, numArg: c.int, def: cstring, l: ^c.size_t = nil) -> cstring ---
  427. L_checknumber :: proc(L: ^State, numArg: c.int) -> Number ---
  428. L_optnumber :: proc(L: ^State, nArg: c.int, def: Number) -> Number ---
  429. L_checkinteger :: proc(L: ^State, numArg: c.int) -> Integer ---
  430. L_optinteger :: proc(L: ^State, nArg: c.int, def: Integer) -> Integer ---
  431. L_checkunsigned :: proc(L: ^State, numArg: c.int) -> Unsigned ---
  432. L_optunsigned :: proc(L: ^State, nArg: c.int, def: Unsigned) -> Unsigned ---
  433. L_checkstack :: proc(L: ^State, sz: c.int, msg: cstring) ---
  434. L_checktype :: proc(L: ^State, narg: c.int, t: c.int) ---
  435. L_checkany :: proc(L: ^State, narg: c.int) ---
  436. L_newmetatable :: proc(L: ^State, tname: cstring) -> c.int ---
  437. L_setmetatable :: proc(L: ^State, tname: cstring) ---
  438. L_testudata :: proc(L: ^State, ud: c.int, tname: cstring) -> rawptr ---
  439. L_checkudata :: proc(L: ^State, ud: c.int, tname: cstring) -> rawptr ---
  440. L_where :: proc(L: ^State, lvl: c.int) ---
  441. L_error :: proc(L: ^State, fmt: cstring, #c_vararg args: ..any) -> Status ---
  442. L_checkoption :: proc(L: ^State, narg: c.int, def: cstring, lst: [^]cstring) -> c.int ---
  443. L_fileresult :: proc(L: ^State, stat: c.int, fname: cstring) -> c.int ---
  444. L_execresult :: proc(L: ^State, stat: c.int) -> c.int ---
  445. L_ref :: proc(L: ^State, t: c.int) -> c.int ---
  446. L_unref :: proc(L: ^State, t: c.int, ref: c.int) ---
  447. @(link_name="luaL_loadfilex")
  448. L_loadfile :: proc (L: ^State, filename: cstring, mode: cstring = nil) -> Status ---
  449. @(link_name="luaL_loadbufferx")
  450. L_loadbuffer :: proc(L: ^State, buff: [^]byte, sz: c.size_t, name: cstring, mode: cstring = nil) -> Status ---
  451. L_loadstring :: proc(L: ^State, s: cstring) -> Status ---
  452. L_newstate :: proc() -> ^State ---
  453. L_len :: proc(L: ^State, idx: c.int) -> c.int ---
  454. L_gsub :: proc(L: ^State, s, p, r: cstring) -> cstring ---
  455. L_setfuncs :: proc(L: ^State, l: [^]L_Reg, nup: c.int) ---
  456. L_getsubtable :: proc(L: ^State, idx: c.int, fname: cstring) -> c.int ---
  457. L_traceback :: proc(L: ^State, L1: ^State, msg: cstring, level: c.int) ---
  458. L_requiref :: proc(L: ^State, modname: cstring, openf: CFunction, glb: c.int) ---
  459. }
  460. /*
  461. ** {======================================================
  462. ** Generic Buffer manipulation
  463. ** =======================================================
  464. */
  465. L_Buffer :: struct {
  466. b: [^]byte, /* buffer address */
  467. size: c.size_t, /* buffer size */
  468. n: c.size_t, /* number of characters in buffer */
  469. L: ^State,
  470. initb: [L_BUFFERSIZE]byte, /* initial buffer */
  471. }
  472. L_addchar :: #force_inline proc "c" (B: ^L_Buffer, c: byte) {
  473. if B.n < B.size {
  474. L_prepbuffsize(B, 1)
  475. }
  476. B.b[B.n] = c
  477. B.n += 1
  478. }
  479. L_addsize :: #force_inline proc "c" (B: ^L_Buffer, s: c.size_t) -> c.size_t {
  480. B.n += s
  481. return B.n
  482. }
  483. L_prepbuffer :: #force_inline proc "c" (B: ^L_Buffer) -> [^]byte {
  484. return L_prepbuffsize(B, c.size_t(L_BUFFERSIZE))
  485. }
  486. @(link_prefix="lua")
  487. @(default_calling_convention="c")
  488. foreign lib {
  489. L_buffinit :: proc(L: ^State, B: ^L_Buffer) ---
  490. L_prepbuffsize :: proc(B: ^L_Buffer, sz: c.size_t) -> [^]byte ---
  491. L_addlstring :: proc(B: ^L_Buffer, s: cstring, l: c.size_t) ---
  492. L_addstring :: proc(B: ^L_Buffer, s: cstring) ---
  493. L_addvalue :: proc(B: ^L_Buffer) ---
  494. L_pushresult :: proc(B: ^L_Buffer) ---
  495. L_pushresultsize :: proc(B: ^L_Buffer, sz: c.size_t) ---
  496. L_buffinitsize :: proc(L: ^State, B: ^L_Buffer, sz: c.size_t) -> [^]byte ---
  497. }
  498. /* }====================================================== */
  499. /*
  500. ** {==============================================================
  501. ** some useful macros
  502. ** ===============================================================
  503. */
  504. pop :: #force_inline proc "c" (L: ^State, n: c.int) {
  505. settop(L, -n-1)
  506. }
  507. newtable :: #force_inline proc "c" (L: ^State) {
  508. createtable(L, 0, 0)
  509. }
  510. register :: #force_inline proc "c" (L: ^State, n: cstring, f: CFunction) {
  511. pushcfunction(L, f)
  512. setglobal(L, n)
  513. }
  514. pushcfunction :: #force_inline proc "c" (L: ^State, f: CFunction) {
  515. pushcclosure(L, f, 0)
  516. }
  517. isfunction :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .FUNCTION }
  518. istable :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .TABLE }
  519. islightuserdata :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .LIGHTUSERDATA }
  520. isnil :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .NIL }
  521. isboolean :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .BOOLEAN }
  522. isthread :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .THREAD }
  523. isnone :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .NONE }
  524. isnoneornil :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) <= .NIL }
  525. pushliteral :: pushstring
  526. pushglobaltable :: #force_inline proc "c" (L: ^State) {
  527. rawgeti(L, REGISTRYINDEX, RIDX_GLOBALS)
  528. }
  529. tostring :: #force_inline proc "c" (L: ^State, i: c.int) -> cstring {
  530. return tolstring(L, i, nil)
  531. }
  532. L_newlibtable :: #force_inline proc "c" (L: ^State, l: []L_Reg) {
  533. createtable(L, 0, c.int(builtin.len(l) - 1))
  534. }
  535. L_newlib :: proc(L: ^State, l: []L_Reg) {
  536. L_newlibtable(L, l)
  537. L_setfuncs(L, raw_data(l), 0)
  538. }
  539. L_argcheck :: #force_inline proc "c" (L: ^State, cond: bool, numarg: c.int, extramsg: cstring) {
  540. if cond {
  541. L_argerror(L, numarg, extramsg)
  542. }
  543. }
  544. L_typename :: #force_inline proc "c" (L: ^State, i: c.int) -> cstring {
  545. return typename(L, type(L, i))
  546. }
  547. L_dofile :: #force_inline proc "c" (L: ^State, s: cstring) -> c.int {
  548. err := L_loadfile(L, s)
  549. return pcall(L, 0, MULTRET, 0) if err == nil else c.int(err)
  550. }
  551. L_dostring :: #force_inline proc "c" (L: ^State, s: cstring) -> c.int {
  552. err := L_loadstring(L, s)
  553. return pcall(L, 0, MULTRET, 0) if err == nil else c.int(err)
  554. }
  555. L_getmetatable :: #force_inline proc "c" (L: ^State, n: cstring) {
  556. getfield(L, REGISTRYINDEX, n)
  557. }
  558. L_opt :: #force_inline proc "c" (L: ^State, f: $F, n: c.int, d: $T) -> T where intrinsics.type_is_proc(F) {
  559. return d if isnoneornil(L, n) else f(L, n)
  560. }
  561. /* }============================================================== */