lua.odin 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. package lua_5_4
  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/lua54dll.lib"
  8. } else when ODIN_OS == .Linux {
  9. foreign import lib "linux/liblua54.a"
  10. } else {
  11. foreign import lib "system:liblua54.a"
  12. }
  13. VERSION_MAJOR :: "5"
  14. VERSION_MINOR :: "4"
  15. VERSION_RELEASE :: "2"
  16. VERSION_NUM :: 504
  17. VERSION_RELEASE_NUM :: VERSION_NUM * 100 + 0
  18. VERSION :: "Lua " + VERSION_MAJOR + "." + VERSION_MINOR
  19. RELEASE :: VERSION + "." + VERSION_RELEASE
  20. COPYRIGHT :: RELEASE + " Copyright (C) 1994-2020 Lua.org, PUC-Rio"
  21. AUTHORS :: "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
  22. /* mark for precompiled code ('<esc>Lua') */
  23. SIGNATURE :: "\x1bLua"
  24. /* option for multiple returns in 'lua_pcall' and 'lua_call' */
  25. MULTRET :: -1
  26. REGISTRYINDEX :: -MAXSTACK - 1000
  27. /*
  28. @@ LUAI_MAXSTACK limits the size of the Lua stack.
  29. ** CHANGE it if you need a different limit. This limit is arbitrary;
  30. ** its only purpose is to stop Lua from consuming unlimited stack
  31. ** space (and to reserve some numbers for pseudo-indices).
  32. ** (It must fit into max(size_t)/32.)
  33. */
  34. MAXSTACK :: 1000000
  35. /*
  36. @@ LUA_EXTRASPACE defines the size of a raw memory area associated with
  37. ** a Lua state with very fast access.
  38. ** CHANGE it if you need a different size.
  39. */
  40. EXTRASPACE :: size_of(rawptr)
  41. /*
  42. @@ LUA_IDSIZE gives the maximum size for the description of the source
  43. @@ of a function in debug information.
  44. ** CHANGE it if you want a different size.
  45. */
  46. IDSIZE :: 60
  47. /*
  48. @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
  49. */
  50. L_BUFFERSIZE :: c.int(16 * size_of(rawptr) * size_of(Number))
  51. MAXALIGNVAL :: max(align_of(Number), align_of(f64), align_of(rawptr), align_of(Integer), align_of(c.long))
  52. Status :: enum c.int {
  53. OK = 0,
  54. YIELD = 1,
  55. ERRRUN = 2,
  56. ERRSYNTAX = 3,
  57. ERRMEM = 4,
  58. ERRERR = 5,
  59. ERRFILE = 6,
  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. /*
  165. ** Type for warning functions
  166. */
  167. WarnFunction :: #type proc "c" (ud: rawptr, msg: rawptr, tocont: c.int)
  168. GCWhat :: enum c.int {
  169. STOP = 0,
  170. RESTART = 1,
  171. COLLECT = 2,
  172. COUNT = 3,
  173. COUNTB = 4,
  174. STEP = 5,
  175. SETPAUSE = 6,
  176. SETSTEPMUL = 7,
  177. ISRUNNING = 9,
  178. GEN = 10,
  179. INC = 11,
  180. }
  181. GCSTOP :: GCWhat.STOP
  182. GCRESTART :: GCWhat.RESTART
  183. GCCOLLECT :: GCWhat.COLLECT
  184. GCCOUNT :: GCWhat.COUNT
  185. GCCOUNTB :: GCWhat.COUNTB
  186. GCSTEP :: GCWhat.STEP
  187. GCSETPAUSE :: GCWhat.SETPAUSE
  188. GCSETSTEPMUL :: GCWhat.SETSTEPMUL
  189. GCISRUNNING :: GCWhat.ISRUNNING
  190. GCGEN :: GCWhat.GEN
  191. GCINC :: GCWhat.INC
  192. /*
  193. ** Event codes
  194. */
  195. HookEvent :: enum c.int {
  196. CALL = 0,
  197. RET = 1,
  198. LINE = 2,
  199. COUNT = 3,
  200. TAILCALL = 4,
  201. }
  202. HOOKCALL :: HookEvent.CALL
  203. HOOKRET :: HookEvent.RET
  204. HOOKLINE :: HookEvent.LINE
  205. HOOKCOUNT :: HookEvent.COUNT
  206. HOOKTAILCALL :: HookEvent.TAILCALL
  207. /*
  208. ** Event masks
  209. */
  210. HookMask :: distinct bit_set[HookEvent; c.int]
  211. MASKCALL :: HookMask{.CALL}
  212. MASKRET :: HookMask{.RET}
  213. MASKLINE :: HookMask{.LINE}
  214. MASKCOUNT :: HookMask{.COUNT}
  215. /* activation record */
  216. Debug :: struct {
  217. event: HookEvent,
  218. name: cstring, /* (n) */
  219. namewhat: cstring, /* (n) 'global', 'local', 'field', 'method' */
  220. what: cstring, /* (S) 'Lua', 'C', 'main', 'tail' */
  221. source: cstring, /* (S) */
  222. srclen: c.size_t, /* (S) */
  223. currentline: c.int, /* (l) */
  224. linedefined: c.int, /* (S) */
  225. lastlinedefined: c.int, /* (S) */
  226. nups: u8, /* (u) number of upvalues */
  227. nparams: u8, /* (u) number of parameters */
  228. isvararg: bool, /* (u) */
  229. istailcall: bool, /* (t) */
  230. ftransfer: u16, /* (r) index of first value transferred */
  231. ntransfer: u16, /* (r) number of transferred values */
  232. short_src: [IDSIZE]u8 `fmt:"s"`, /* (S) */
  233. /* private part */
  234. i_ci: rawptr, /* active function */
  235. }
  236. /* Functions to be called by the debugger in specific events */
  237. Hook :: #type proc "c" (L: ^State, ar: ^Debug)
  238. State :: struct {} // opaque data type
  239. @(link_prefix="lua_")
  240. @(default_calling_convention="c")
  241. foreign lib {
  242. /*
  243. ** RCS ident string
  244. */
  245. ident: [^]u8 // TODO(bill): is this correct?
  246. /*
  247. ** state manipulation
  248. */
  249. newstate :: proc(f: Alloc, ud: rawptr) -> ^State ---
  250. close :: proc(L: ^State) ---
  251. newthread :: proc(L: ^State) -> ^State ---
  252. resetthread :: proc(L: ^State) -> Status ---
  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) -> Unsigned ---
  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. newuserdatauv :: proc(L: ^State, sz: c.size_t, nuvalue: c.int) -> rawptr ---
  319. getmetatable :: proc(L: ^State, objindex: c.int) -> c.int ---
  320. getiuservalue :: proc(L: ^State, idx: c.int, n: 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. setiuservalue :: proc(L: ^State, idx: c.int, n: 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, nres: ^c.int) -> Status ---
  351. status :: proc(L: ^State) -> Status ---
  352. isyieldable :: proc(L: ^State) -> b32 ---
  353. /*
  354. ** Warning-related functions
  355. */
  356. setwarnf :: proc(L: ^State, f: WarnFunction, ud: rawptr) ---
  357. warning :: proc(L: ^State, msg: string, tocont: b32) ---
  358. /*
  359. ** garbage-collection function and options
  360. */
  361. gc :: proc(L: ^State, what: GCWhat, #c_vararg args: ..any) -> c.int ---
  362. /*
  363. ** miscellaneous functions
  364. */
  365. error :: proc(L: ^State) -> Status ---
  366. next :: proc(L: ^State, idx: c.int) -> c.int ---
  367. concat :: proc(L: ^State, n: c.int) ---
  368. len :: proc(L: ^State, idx: c.int) ---
  369. stringtonumber :: proc(L: ^State, s: cstring) -> c.size_t ---
  370. getallocf :: proc(L: State, ud: ^rawptr) -> Alloc ---
  371. setallocf :: proc(L: ^State, f: Alloc, ud: rawptr) ---
  372. toclose :: proc(L: ^State, idx: c.int) ---
  373. /*
  374. ** {======================================================================
  375. ** Debug API
  376. ** =======================================================================
  377. */
  378. getstack :: proc(L: ^State, level: c.int, ar: ^Debug) -> c.int ---
  379. getinfo :: proc(L: ^State, what: cstring, ar: ^Debug) -> c.int ---
  380. getlocal :: proc(L: ^State, ar: ^Debug, n: c.int) -> cstring ---
  381. setlocal :: proc(L: ^State, ar: ^Debug, n: c.int) -> cstring ---
  382. getupvalue :: proc(L: ^State, funcindex: c.int, n: c.int) -> cstring ---
  383. setupvalue :: proc(L: ^State, funcindex: c.int, n: c.int) -> cstring ---
  384. upvalueid :: proc(L: ^State, fidx, n: c.int) -> rawptr ---
  385. upvaluejoin :: proc(L: ^State, fidx1, n1, fidx2, n2: c.int) ---
  386. sethook :: proc(L: ^State, func: Hook, mask: HookMask, count: c.int) ---
  387. gethook :: proc(L: ^State) -> Hook ---
  388. gethookmask :: proc(L: ^State) -> HookMask ---
  389. gethookcount :: proc(L: ^State) -> c.int ---
  390. setcstacklimit :: proc(L: ^State, limit: c.uint) -> c.int ---
  391. /* }============================================================== */
  392. }
  393. /* version suffix for environment variable names */
  394. VERSUFFIX :: "_" + VERSION_MAJOR + "_" + VERSION_MINOR
  395. COLIBNAME :: "coroutine"
  396. TABLIBNAME :: "table"
  397. IOLIBNAME :: "io"
  398. OSLIBNAME :: "os"
  399. STRLIBNAME :: "string"
  400. UTF8LIBNAME :: "utf8"
  401. MATHLIBNAME :: "math"
  402. DBLIBNAME :: "debug"
  403. LOADLIBNAME :: "package"
  404. @(link_prefix="lua")
  405. @(default_calling_convention="c")
  406. foreign lib {
  407. open_base :: proc(L: ^State) -> c.int ---
  408. open_coroutine :: proc(L: ^State) -> c.int ---
  409. open_table :: proc(L: ^State) -> c.int ---
  410. open_io :: proc(L: ^State) -> c.int ---
  411. open_os :: proc(L: ^State) -> c.int ---
  412. open_string :: proc(L: ^State) -> c.int ---
  413. open_utf8 :: proc(L: ^State) -> c.int ---
  414. open_math :: proc(L: ^State) -> c.int ---
  415. open_debug :: proc(L: ^State) -> c.int ---
  416. open_package :: proc(L: ^State) -> c.int ---
  417. /* open all previous libraries */
  418. L_openlibs :: proc(L: ^State) ---
  419. }
  420. GNAME :: "_G"
  421. /* key, in the registry, for table of loaded modules */
  422. LOADED_TABLE :: "_LOADED"
  423. /* key, in the registry, for table of preloaded loaders */
  424. PRELOAD_TABLE :: "_PRELOAD"
  425. L_Reg :: struct {
  426. name: cstring,
  427. func: CFunction,
  428. }
  429. L_NUMSIZES :: size_of(Integer)*16 + size_of(Number)
  430. /* predefined references */
  431. NOREF :: -2
  432. REFNIL :: -1
  433. @(link_prefix="lua")
  434. @(default_calling_convention="c")
  435. foreign lib {
  436. @(link_name="luaL_checkversion_")
  437. L_checkversion :: proc(L: ^State, ver: Number = VERSION_NUM, sz: c.size_t = L_NUMSIZES) ---
  438. L_getmetafield :: proc(L: ^State, obj: c.int, e: cstring) -> c.int ---
  439. L_callmeta :: proc(L: ^State, obj: c.int, e: cstring) -> c.int ---
  440. @(link_name="luaL_tolstring")
  441. L_tostring :: proc(L: ^State, idx: c.int, len: ^c.size_t = nil) -> cstring ---
  442. L_argerror :: proc(L: ^State, arg: c.int, extramsg: cstring) -> c.int ---
  443. L_typeerror :: proc(L: ^State, arg: c.int, tname: cstring) -> c.int ---
  444. @(link_name="luaL_checklstring")
  445. L_checkstring :: proc(L: ^State, arg: c.int, l: ^c.size_t = nil) -> cstring ---
  446. @(link_name="luaL_optlstring")
  447. L_optstring :: proc(L: ^State, arg: c.int, def: cstring, l: ^c.size_t = nil) -> cstring ---
  448. L_checknumber :: proc(L: ^State, arg: c.int) -> Number ---
  449. L_optnumber :: proc(L: ^State, arg: c.int, def: Number) -> Number ---
  450. L_checkinteger :: proc(L: ^State, arg: c.int) -> Integer ---
  451. L_optinteger :: proc(L: ^State, arg: c.int, def: Integer) -> Integer ---
  452. L_checkstack :: proc(L: ^State, sz: c.int, msg: cstring) ---
  453. L_checktype :: proc(L: ^State, arg: c.int, t: c.int) ---
  454. L_checkany :: proc(L: ^State, arg: c.int) ---
  455. L_newmetatable :: proc(L: ^State, tname: cstring) -> c.int ---
  456. L_setmetatable :: proc(L: ^State, tname: cstring) ---
  457. L_testudata :: proc(L: ^State, ud: c.int, tname: cstring) -> rawptr ---
  458. L_checkudata :: proc(L: ^State, ud: c.int, tname: cstring) -> rawptr ---
  459. L_where :: proc(L: ^State, lvl: c.int) ---
  460. L_error :: proc(L: ^State, fmt: cstring, #c_vararg args: ..any) -> Status ---
  461. L_checkoption :: proc(L: ^State, arg: c.int, def: cstring, lst: [^]cstring) -> c.int ---
  462. L_fileresult :: proc(L: ^State, stat: c.int, fname: cstring) -> c.int ---
  463. L_execresult :: proc(L: ^State, stat: c.int) -> c.int ---
  464. L_ref :: proc(L: ^State, t: c.int) -> c.int ---
  465. L_unref :: proc(L: ^State, t: c.int, ref: c.int) ---
  466. @(link_name="luaL_loadfilex")
  467. L_loadfile :: proc (L: ^State, filename: cstring, mode: cstring = nil) -> Status ---
  468. @(link_name="luaL_loadbufferx")
  469. L_loadbuffer :: proc(L: ^State, buff: [^]byte, sz: c.size_t, name: cstring, mode: cstring = nil) -> Status ---
  470. L_loadstring :: proc(L: ^State, s: cstring) -> Status ---
  471. L_newstate :: proc() -> ^State ---
  472. L_len :: proc(L: ^State, idx: c.int) -> Integer ---
  473. L_addgsub :: proc(b: ^L_Buffer, s, p, r: cstring) ---
  474. L_gsub :: proc(L: ^State, s, p, r: cstring) -> cstring ---
  475. L_setfuncs :: proc(L: ^State, l: [^]L_Reg, nup: c.int) ---
  476. L_getsubtable :: proc(L: ^State, idx: c.int, fname: cstring) -> c.int ---
  477. L_traceback :: proc(L: ^State, L1: ^State, msg: cstring, level: c.int) ---
  478. L_requiref :: proc(L: ^State, modname: cstring, openf: CFunction, glb: c.int) ---
  479. }
  480. /*
  481. ** {======================================================
  482. ** Generic Buffer manipulation
  483. ** =======================================================
  484. */
  485. L_Buffer :: struct {
  486. b: [^]byte, /* buffer address */
  487. size: c.size_t, /* buffer size */
  488. n: c.size_t, /* number of characters in buffer */
  489. L: ^State,
  490. init: struct #raw_union {
  491. n: Number, u: f64, s: rawptr, i: Integer, l: c.long,
  492. b: [L_BUFFERSIZE]byte, /* initial buffer */
  493. },
  494. }
  495. L_bufflen :: #force_inline proc "c" (bf: ^L_Buffer) -> c.size_t {
  496. return bf.n
  497. }
  498. L_buffaddr :: #force_inline proc "c" (bf: ^L_Buffer) -> [^]byte {
  499. return bf.b
  500. }
  501. L_addchar :: #force_inline proc "c" (B: ^L_Buffer, c: byte) {
  502. if B.n < B.size {
  503. L_prepbuffsize(B, 1)
  504. }
  505. B.b[B.n] = c
  506. B.n += 1
  507. }
  508. L_addsize :: #force_inline proc "c" (B: ^L_Buffer, s: c.size_t) -> c.size_t {
  509. B.n += s
  510. return B.n
  511. }
  512. L_buffsub :: #force_inline proc "c" (B: ^L_Buffer, s: c.size_t) -> c.size_t {
  513. B.n -= s
  514. return B.n
  515. }
  516. L_prepbuffer :: #force_inline proc "c" (B: ^L_Buffer) -> [^]byte {
  517. return L_prepbuffsize(B, c.size_t(L_BUFFERSIZE))
  518. }
  519. @(link_prefix="lua")
  520. @(default_calling_convention="c")
  521. foreign lib {
  522. L_buffinit :: proc(L: ^State, B: ^L_Buffer) ---
  523. L_prepbuffsize :: proc(B: ^L_Buffer, sz: c.size_t) -> [^]byte ---
  524. L_addlstring :: proc(B: ^L_Buffer, s: cstring, l: c.size_t) ---
  525. L_addstring :: proc(B: ^L_Buffer, s: cstring) ---
  526. L_addvalue :: proc(B: ^L_Buffer) ---
  527. L_pushresult :: proc(B: ^L_Buffer) ---
  528. L_pushresultsize :: proc(B: ^L_Buffer, sz: c.size_t) ---
  529. L_buffinitsize :: proc(L: ^State, B: ^L_Buffer, sz: c.size_t) -> [^]byte ---
  530. }
  531. /* }====================================================== */
  532. /*
  533. ** {==============================================================
  534. ** some useful macros
  535. ** ===============================================================
  536. */
  537. getextraspace :: #force_inline proc "c" (L: ^State) -> rawptr {
  538. return rawptr(([^]byte)(L)[-EXTRASPACE:])
  539. }
  540. pop :: #force_inline proc "c" (L: ^State, n: c.int) {
  541. settop(L, -n-1)
  542. }
  543. newtable :: #force_inline proc "c" (L: ^State) {
  544. createtable(L, 0, 0)
  545. }
  546. register :: #force_inline proc "c" (L: ^State, n: cstring, f: CFunction) {
  547. pushcfunction(L, f)
  548. setglobal(L, n)
  549. }
  550. pushcfunction :: #force_inline proc "c" (L: ^State, f: CFunction) {
  551. pushcclosure(L, f, 0)
  552. }
  553. isfunction :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .FUNCTION }
  554. istable :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .TABLE }
  555. islightuserdata :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .LIGHTUSERDATA }
  556. isnil :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .NIL }
  557. isboolean :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .BOOLEAN }
  558. isthread :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .THREAD }
  559. isnone :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) == .NONE }
  560. isnoneornil :: #force_inline proc "c" (L: ^State, n: c.int) -> bool { return type(L, n) <= .NIL }
  561. pushliteral :: pushstring
  562. pushglobaltable :: #force_inline proc "c" (L: ^State) {
  563. rawgeti(L, REGISTRYINDEX, RIDX_GLOBALS)
  564. }
  565. tostring :: #force_inline proc "c" (L: ^State, i: c.int) -> cstring {
  566. return tolstring(L, i, nil)
  567. }
  568. insert :: #force_inline proc "c" (L: ^State, idx: c.int) {
  569. rotate(L, idx, 1)
  570. }
  571. remove :: #force_inline proc "c" (L: ^State, idx: c.int) {
  572. rotate(L, idx, -1)
  573. pop(L, 1)
  574. }
  575. replace :: #force_inline proc "c" (L: ^State, idx: c.int) {
  576. copy(L, -1, idx)
  577. pop(L, 1)
  578. }
  579. L_newlibtable :: #force_inline proc "c" (L: ^State, l: []L_Reg) {
  580. createtable(L, 0, c.int(builtin.len(l) - 1))
  581. }
  582. L_newlib :: proc(L: ^State, l: []L_Reg) {
  583. L_checkversion(L)
  584. L_newlibtable(L, l)
  585. L_setfuncs(L, raw_data(l), 0)
  586. }
  587. L_argcheck :: #force_inline proc "c" (L: ^State, cond: bool, arg: c.int, extramsg: cstring) {
  588. if cond {
  589. L_argerror(L, arg, extramsg)
  590. }
  591. }
  592. L_argexpected :: #force_inline proc "c" (L: ^State, cond: bool, arg: c.int, tname: cstring) {
  593. if cond {
  594. L_typeerror(L, arg, tname)
  595. }
  596. }
  597. L_typename :: #force_inline proc "c" (L: ^State, i: c.int) -> cstring {
  598. return typename(L, type(L, i))
  599. }
  600. L_dofile :: #force_inline proc "c" (L: ^State, s: cstring) -> c.int {
  601. err := L_loadfile(L, s)
  602. return pcall(L, 0, MULTRET, 0) if err == nil else c.int(err)
  603. }
  604. L_dostring :: #force_inline proc "c" (L: ^State, s: cstring) -> c.int {
  605. err := L_loadstring(L, s)
  606. return pcall(L, 0, MULTRET, 0) if err == nil else c.int(err)
  607. }
  608. L_getmetatable :: #force_inline proc "c" (L: ^State, n: cstring) -> c.int {
  609. return getfield(L, REGISTRYINDEX, n)
  610. }
  611. L_opt :: #force_inline proc "c" (L: ^State, f: $F, n: c.int, d: $T) -> T where intrinsics.type_is_proc(F) {
  612. return d if isnoneornil(L, n) else f(L, n)
  613. }
  614. /* push the value used to represent failure/error */
  615. pushfail :: pushnil
  616. /* }============================================================== */
  617. /*
  618. ** {==============================================================
  619. ** compatibility macros
  620. ** ===============================================================
  621. */
  622. newuserdata :: #force_inline proc "c" (L: ^State, s: c.size_t) -> rawptr {
  623. return newuserdatauv(L, s, 1)
  624. }
  625. getuservalue :: #force_inline proc "c" (L: ^State, idx: c.int) -> c.int {
  626. return getiuservalue(L, idx, 1)
  627. }
  628. setuservalue :: #force_inline proc "c" (L: ^State, idx: c.int) -> c.int {
  629. return setiuservalue(L, idx, 1)
  630. }