lua.odin 21 KB

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