lua.odin 21 KB

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