lua.odin 18 KB

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