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