lua.odin 18 KB

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