lua.odin 21 KB

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