-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglua.go
1413 lines (1074 loc) · 28.5 KB
/
glua.go
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package glua
// #include "c/glua.h"
import "C"
import (
"errors"
"fmt"
"path/filepath"
"runtime/cgo"
"strconv"
"unsafe"
)
func LoadLuaShared() *string {
err := C.load_lua_shared()
if err != nil {
errStr := C.GoString(err)
return &errStr
}
return nil
}
func UnloadLuaShared() {
C.unload_lua_shared()
}
func GetLuaSharedPath() string {
return C.GoString(C.get_lua_shared_path())
}
// Creates a new Lua state.
//
// # Example
//
// L := glua.NewState()
func NewState() State {
return State(C.luaL_newstate_wrap())
}
// Creates a new coroutine
//
// # Example
//
// L := glua.NewState()
// L.NewCoroutine()
func (L State) NewCoroutine() State {
return State(C.lua_newthread_wrap(L.c()))
}
// Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the
// number of elements in the stack (and so 0 means an empty stack).
func (L State) GetTop() int {
return int(C.lua_gettop_wrap(L.c()))
}
/*
Sets the stack top to the given index.
If the new top is larger than the old one, the new elements are filled with nil.
If index is 0, then all stack elements are removed.
# Example
L.PushString("Hello, world!")
fmt.Println("Stack size before SetTop:", L.GetTop())
L.SetTop(0)
fmt.Println("Stack size after SetTop:", L.GetTop())
*/
func (L State) SetTop(idx int) {
C.lua_settop_wrap(L.c(), C.int(idx))
}
/*
Pushes a copy of the element at the given index onto the stack.
# Example
// Push a string onto the stack
L.PushString("Hello, world!")
// Duplicate the value on top of the stack (the string)
L.PushValue(-1)
// The stack now has two copies of the string
L.DumpStack()
*/
func (L State) PushValue(idx int) {
C.lua_pushvalue_wrap(L.c(), C.int(idx))
}
/*
Removes the element at the given valid index, shifting down the elements above this index to fill the gap.
Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.
# Example
// Push a string onto the stack
L.PushString("Hello, world!")
// Push a number onto the stack
glua.PushNumber(L, 123)
// Remove the string
L.Remove(-2)
// The stack now has only the number
L.DumpStack()
*/
func (L State) Remove(idx int) {
C.lua_remove_wrap(L.c(), C.int(idx))
}
/*
Moves the top element into the given valid index, shifting up the elements above this index to open space.
Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.
# Example
L.PushString("first")
L.PushString("second")
L.PushString("third")
// Stack is:
// 3. third
// 2. second
// 1. first
// Insert the top element ("third") at position 1
L.Insert(1)
// Stack is:
// 3. second
// 2. first
// 1. third
*/
func (L State) Insert(idx int) {
C.lua_insert_wrap(L.c(), C.int(idx))
}
/*
Moves the top element into the given position (and pops it), without shifting any
element (therefore replacing the value at the given position).
# Example
L.PushString("original") // stack: 1. "original"
L.PushString("new") // stack: 2. "new", 1. "original"
// Replace the value at position 1 with the top element ("new")
L.Replace(1)
// Stack is:
// 1. new
*/
func (L State) Replace(idx int) {
C.lua_replace_wrap(L.c(), C.int(idx))
}
/*
Ensures that there are at least extra free stack slots in the stack.
It returns false if it cannot grow the stack to that size.
This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged.
# Example
// Ensure there are at least 10 free stack slots
if !L.CheckStack(10) {
fmt.Println("Couldn't grow the stack")
}
*/
func (L State) CheckStack(extra int) bool {
return C.lua_checkstack_wrap(L.c(), C.int(extra)) != 0
}
/*
Returns the type of the value in the given index.
It returns LUA_TNONE for a non-valid index (That is, an index to an "empty" stack position).
The types returned by this function are:
- LUA_TNIL
- LUA_TBOOLEAN
- LUA_TLIGHTUSERDATA
- LUA_TNUMBER
- LUA_TSTRING
- LUA_TTABLE
- LUA_TFUNCTION
- LUA_TUSERDATA
- LUA_TTHREAD
# Example
L.PushString("Hello, world!")
fmt.Println(L.TypeName(L.Type(-1)))
*/
func (L State) Type(idx int) int {
return int(C.lua_type_wrap(L.c(), C.int(idx)))
}
/*
Returns the name of the type encoded by the value typeid.
# Example
L.PushString("Hello, world!")
fmt.Println(L.TypeName(L.Type(-1)))
*/
func (L State) TypeName(typeid int) string {
return C.GoString(C.lua_typename_wrap(L.c(), C.int(typeid)))
}
/*
Returns true if the two values in acceptable indices index1 and index2 are equal,
following the semantics of the Lua == operator (that is, may call metamethods).
Otherwise returns false. Also returns false if any of the indices is non valid.
# Example
L.PushString("Hello, world!")
L.PushString("Hello, world!")
if L.AreEqual(-1, -2) {
fmt.Println("The strings are equal")
}
*/
func (L State) AreEqual(idx1, idx2 int) bool {
return C.lua_equal_wrap(L.c(), C.int(idx1), C.int(idx2)) != 0
}
/*
Returns true if the two values in acceptable indices index1 and index2 are primitively equal
(that is, without calling metamethods).
Otherwise returns false. Also returns false if any of the indices are non valid.
# Example
L.PushString("Hello, world!")
L.PushString("Hello, world!")
if L.AreRawEqual(-1, -2) {
fmt.Println("The strings are equal")
}
*/
func (L State) AreRawEqual(idx1, idx2 int) bool {
return C.lua_rawequal_wrap(L.c(), C.int(idx1), C.int(idx2)) != 0
}
/*
Returns true if the value at acceptable index index1 is smaller than the value at
acceptable index index2, following the semantics of the Lua < operator (that is, may call metamethods).
Otherwise returns false. Also returns false if any of the indices is non valid.
# Example
glua.PushNumber(L, 123)
glua.PushNumber(L, 456)
if L.IsLessThan(-1, -2) {
fmt.Println("123 is less than 456")
}
*/
func (L State) IsLessThan(idx1, idx2 int) bool {
return C.lua_lessthan_wrap(L.c(), C.int(idx1), C.int(idx2)) != 0
}
/*
Returns the number at the given index.
The value must be a number or a string convertible to a number, otherwise it returns 0.
# Example
glua.PushNumber(L, 123.456)
fmt.Println(L.GetNumber(-1))
*/
func (L State) GetNumber(idx int) LUA_NUMBER {
return LUA_NUMBER(C.lua_tonumber_wrap(L.c(), C.int(idx)))
}
/*
Returns the boolean value at the given index.
If the value is a number or a string that is convertible to a number, it returns true for non-zero numbers.
It also returns 0 when called with a non-valid index.
If you want to accept only true or false, use IsBool.
# Example
L.PushBool(true)
fmt.Println(L.GetBool(-1))
*/
func (L State) GetBool(idx int) bool {
return C.lua_toboolean_wrap(L.c(), C.int(idx)) != 0
}
/*
Returns the string at the given index in the Lua stack.
# Example
L.PushString("Hello, world!")
str := L.GetString(-1)
if str != nil {
fmt.Println(*str)
}
*/
func (L State) GetString(idx int) string {
if !L.IsString(idx) {
return ""
}
size := C.size_t(0)
str := C.lua_tolstring_wrap(L.c(), C.int(idx), &size)
if str == nil {
return ""
}
result := goStringN(str, size)
return result
}
/*
Returns the binary string at the given index in the Lua stack.
# Example
L.PushString("Hello, world!")
str := L.GetBinaryString(-1)
if str != nil {
fmt.Println(string(str))
}
*/
func (L State) GetBinaryString(idx int) []byte {
if !L.IsString(idx) {
return nil
}
size := C.size_t(0)
str := C.lua_tolstring_wrap(L.c(), C.int(idx), &size)
if str == nil {
return nil
}
return goBytes(unsafe.Pointer(str), size)
}
func (L State) GetLength(idx int) int {
return int(C.lua_objlen_wrap(L.c(), C.int(idx)))
}
func (L State) GetFunction(idx int) (func(State) int, error) {
ptr := C.lua_tocfunction_wrap(L.c(), C.int(idx))
if ptr == nil {
return nil, errors.New("not a function")
}
return func(L State) int {
return int(C.luaCFunctionWrapper(ptr, L.c()))
}, nil
}
/*
Returns the userdata at the given index.
If the value at the given index is not a userdata, it returns nil.
If the value is a light userdata, it returns the pointer.
*/
func (L State) GetUserData(idx int, metatable *string) cgo.Handle {
if !L.IsUserData(idx) {
var metaMessage string
if metatable != nil {
metaMessage = " of type: " + *metatable
}
panic("expected a userdata" + metaMessage)
}
if metatable != nil {
L.GetMetatable(idx)
L.GetMetatableByName(*metatable)
res := L.AreRawEqual(-1, -2)
L.PopN(2)
if !res {
panic("expected a userdata of type: " + *metatable)
}
}
ud := C.lua_touserdata_wrap(L.c(), C.int(idx))
if ud == nil {
panic("invalid userdata pointer")
}
handle := *(*cgo.Handle)(ud)
return handle
}
func (L State) GetLightUserData(idx int) uintptr {
if !L.IsUserData(idx) {
panic("expected a light userdata")
}
return uintptr(C.lua_touserdata_wrap(L.c(), C.int(idx)))
}
/*
Returns the thread at the given index.
If the value at the given index is not a thread, it returns nil.
*/
func (L State) GetThread(idx int) State {
return State(C.lua_tothread_wrap(L.c(), C.int(idx)))
}
/*
Gets a pointer to the value at the given index.
*/
func (L State) GetPointer(idx int) unsafe.Pointer {
return unsafe.Pointer(C.lua_topointer_wrap(L.c(), C.int(idx)))
}
/*
Pushes a nil value onto the stack.
*/
func (L State) PushNil() {
C.lua_pushnil_wrap(L.c())
}
/*
Pushes a boolean value onto the stack.
*/
func (L State) PushBool(b bool) {
if b {
C.lua_pushboolean_wrap(L.c(), 1)
} else {
C.lua_pushboolean_wrap(L.c(), 0)
}
}
/*
Pushes a string onto the stack.
*/
func (L State) PushString(str string) {
if len(str) == 0 {
C.lua_pushlstring_wrap(L.c(), nil, 0)
return
}
strPtr := unsafe.Pointer(&[]byte(str)[0])
C.lua_pushlstring_wrap(L.c(), (*C.char)(strPtr), C.size_t(len(str)))
}
/*
Pushes a string onto the stack with a given length.
*/
func (L State) PushBinaryString(data []byte) {
if len(data) == 0 {
C.lua_pushlstring_wrap(L.c(), nil, 0)
return
}
strPtr := unsafe.Pointer(&data[0])
C.lua_pushlstring_wrap(L.c(), (*C.char)(strPtr), C.size_t(len(data)))
}
// this is not same as lua_pushfstring, it just mimics the behavior
func (L State) PushFString(fmtstr string, args ...any) {
if len(args) == 0 {
L.PushString(fmtstr)
return
}
L.PushString(fmt.Sprintf(fmtstr, args...))
}
/*
Pushes a light userdata onto the stack.
Light userdata is a pointer that is not managed by Lua.
*/
func (L State) PushLightUserData(p uintptr) {
C.lua_pushlightuserdata_wrap(L.c(), unsafe.Pointer(p))
}
/*
Pushes the current thread (i.e, the coroutine) onto the stack,
and returns whether the thread is the main thread or not.
Returns 1 if the thread is the main thread, otherwise 0.
*/
func (L State) PushThread() int {
return int(C.lua_pushthread_wrap(L.c()))
}
/*
Pushes onto the stack the value t[k], where t is the table at the given index and k is the value at the top of the stack.
This function pops the key from the stack.
# Example
L.NewTable()
L.PushString("message")
L.PushString("Hello, world!")
L.SetTable(-3)
L.PushString("message")
L.GetTable(-2)
fmt.Println(L.GetString(-1))
*/
func (L State) GetTable(idx int) {
C.lua_gettable_wrap(L.c(), C.int(idx))
}
/*
Pushes onto the stack the value t[k], where t is the table at the given index and k is the value at the top of the stack.
# Example
L.NewTable()
L.PushString("message")
L.PushString("Hello, world!")
L.SetTable(-3)
L.GetField(-1, "message")
fmt.Println(L.GetString(-1))
*/
func (L State) GetField(idx int, key string) {
cKey := CStr(key)
defer cKey.free()
C.lua_getfield_wrap(L.c(), C.int(idx), cKey.c)
}
/*
Pushes onto the stack the value of the global name.
# Example
L.GetGlobal("print")
*/
func (L State) GetGlobal(name string) {
L.GetField(LUA_GLOBALSINDEX, name)
}
/*
Similar to GetTable, but does not perform any metamethods.
*/
func (L State) RawGet(idx int) {
C.lua_rawget_wrap(L.c(), C.int(idx))
}
/*
Pushes onto the stack the value t[n], where t is the table at the given index.
The access is raw, that is, it does not invoke metamethods.
# Example
L.NewTable()
L.PushString("Hello, world!")
L.RawSetI(-2, 1)
L.RawGetI(-1, 1)
fmt.Println(L.GetString(-1))
*/
func (L State) RawGetI(idx int, n int) {
C.lua_rawgeti_wrap(L.c(), C.int(idx), C.int(n))
}
func (L State) CreateTable(narr, nrec int) {
C.lua_createtable_wrap(L.c(), C.int(narr), C.int(nrec))
}
func (L State) NewTable() {
L.CreateTable(0, 0)
}
/*
Creates a new userdata and pushes it onto the stack.
It returns a cgo.Handle that can be used to retrieve the value.
You need to call handle.Delete() when __gc is called.
# Example
type MyStruct struct {
Message string
}
myStruct := &MyStruct{"Hello, world!"}
h := L.NewUserData(myStruct, nil)
*/
func (L State) NewUserData(value any, metatable *string) cgo.Handle {
const goUserDataSize = C.size_t(unsafe.Sizeof(uintptr(0)))
h := cgo.NewHandle(value)
ptr := C.lua_newuserdata_wrap(L.c(), goUserDataSize)
if metatable != nil {
L.GetMetatableByName(*metatable)
if L.Type(-1) != LUA_TTABLE {
panic("metatable not found")
}
L.SetMetatable(-2)
}
*(*cgo.Handle)(ptr) = h
return h
}
/*
Pushes onto the stack the metatable of the value at the given index.
If the value does not have a metatable, the function returns 0 and pushes nothing.
*/
func (L State) GetMetatable(idx int) int {
return int(C.lua_getmetatable_wrap(L.c(), C.int(idx)))
}
func (L State) GetMetatableByName(name string) {
L.GetField(LUA_REGISTRYINDEX, name)
}
/*
Pushes onto the stack the environment table of the value at the given index.
*/
func (L State) GetFenv(idx int) {
C.lua_getfenv_wrap(L.c(), C.int(idx))
}
/*
Does the equivalent of t[k] = v, where t is the table at the given index and v is the value at the top of the stack, and k is the value just below the top.
This function pops the key and the value from the stack.
# Example
L.NewTable()
L.PushString("message")
L.PushString("Hello, world!")
L.SetTable(-3)
L.SetGlobal("myTable")
L.RunString("print(myTable.message)")
*/
func (L State) SetTable(idx int) {
C.lua_settable_wrap(L.c(), C.int(idx))
}
/*
Does the equivalent of t[k] = v, where t is the table at the given index and v is the value at the top of the stack.
This function pops the value from the stack.
# Example
L.NewTable()
L.PushString("Hello, world!")
L.SetField(-2, "message")
L.SetGlobal("myTable")
L.RunString("print(myTable.message)")
*/
func (L State) SetField(idx int, key string) {
cKey := CStr(key)
defer cKey.free()
C.lua_setfield_wrap(L.c(), C.int(idx), cKey.c)
}
/*
Similar to SetTable, but does not perform any metamethods.
# Example
L.NewTable()
L.PushString("message")
L.PushString("Hello, world!")
L.RawSet(-3)
L.SetGlobal("myTable")
L.RunString("print(myTable.message)")
*/
func (L State) RawSet(idx int) {
C.lua_rawset_wrap(L.c(), C.int(idx))
}
/*
Does the equivalent of t[n] = v, where t is the table at the given index and v is the value at the top of the stack.
This function pops the value from the stack.
The assignment is raw, that is, it does not invoke metamethods.
# Example
L.NewTable()
L.PushString("Hello, world!")
L.RawSetI(-2, 1)
L.SetGlobal("myTable")
L.RunString("print(myTable[1])")
*/
func (L State) RawSetI(idx int, n int) {
C.lua_rawseti_wrap(L.c(), C.int(idx), C.int(n))
}
/*
Sets the metatable for the object at the given index.
# Example
L.NewTable()
L.NewTable()
L.SetMetatable(-2)
L.SetGlobal("myTable")
L.RunString("print(getmetatable(myTable))")
*/
func (L State) SetMetatable(idx int) {
C.lua_setmetatable_wrap(L.c(), C.int(idx))
}
/*
Pops a table from the stack and sets it as the new environment for the value at the given index.
If the value at the given index is neither a function nor a thread nor a userdata, it returns 0.
Otherwise, it returns 1.
# You cannot set the environment of a C function. It will return 1 but won't work.
# Example
L.RunString("function myfunc() print(a) end")
L.GetGlobal("myfunc")
L.NewTable()
L.PushString("a")
glua.PushNumber(L, 123)
L.SetTable(-3)
L.PushString("print")
L.GetGlobal("print")
L.SetTable(-3)
L.SetFEnv(-2)
L.Call(0, 0)
*/
func (L State) SetFEnv(idx int) int {
return int(C.lua_setfenv_wrap(L.c(), C.int(idx)))
}
/*
Calls a function.
nargs is the number of arguments in the stack.
nresults is the number of results to be returned.
# Example
L.CompileString("print('Hello, world!')")
L.Call(0, 0)
*/
func (L State) Call(nargs, nresults int) {
C.lua_call_wrap(L.c(), C.int(nargs), C.int(nresults))
}
/*
Calls a function (which is on top of the Lua stack) in protected mode.
If there are no errors, PCall returns LUA_OK.
If errFunc is 0, the original error message is returned on the stack.
If errFunc is a valid stack index, it acts as an error handler function, and the error message is returned on top of the stack.
# Examples
1- no error handler
err := L.CompileString("doesntexist()")
if err != nil {
fmt.Println(err)
return 0
}
err = L.PCall(0, 0, 0)
if err != nil {
fmt.Println(err)
return 0
}
----
2- with error handler
// we use PushOneTimeGoFunc because we only want to call this function once, it will be unregistered after it's called
L.PushOneTimeGoFunc(func(L glua.State) int {
fmt.Println(L.GetErrorString())
return 0
})
errFuncIdx := L.GetTop()
L.CompileString("doesntexist()")
err := L.PCall(0, 0, errFuncIdx)
if err != 0 {
// error handler already printed the error
return 0
}
*/
func (L State) PCall(nargs, nresults, errfunc int) error {
status := C.lua_pcall_wrap(L.c(), C.int(nargs), C.int(nresults), C.int(errfunc))
if status != LUA_OK {
return errors.New(L.GetErrorMessage(int(status)))
}
return nil
}
/*
Calls a function in protected mode.
If there are errors it returns false and prints the error message.
If there are no errors it returns true.
# Example
err := L.RunString("doesntexist()")
if err != nil {
fmt.Println(err)
}
L.TryCall(0, 0)
*/
func (L State) TryCall(nargs, nresults int) bool {
if err := L.PCall(nargs, nresults, 0); err != nil {
L.ErrorNoHalt(err.Error())
return false
}
return true
}
func (L State) CPCall(funcPtr unsafe.Pointer, ud uintptr) error {
status := C.lua_cpcall_wrap(L.c(), funcPtr, unsafe.Pointer(ud))
if status != LUA_OK {
return errors.New(L.GetErrorMessage(int(status)))
}
return nil
}
func (L State) TryCPCall(funcPtr unsafe.Pointer, ud uintptr) bool {
if err := L.CPCall(funcPtr, ud); err != nil {
L.ErrorNoHalt(err.Error())
return false
}
return true
}
// TODO lua_yield
// TODO lua_resume
// TODO lua_status
/*
Opens all standard Lua libraries into the given Lua state.
# Example
L.OpenLibs()
*/
func (L State) OpenLibs() {
C.luaL_openlibs_wrap(L.c())
}
/*
Calls a metamethod.
If the object at index obj has a metatable with a field e,
this function calls it, passing the object as its argument.
It returns 1 and pushes the call's return value onto the stack.
If there is no metatable or field e, it returns 0 without
pushing any value.
# Example
L.RunString(`
myObject = {}
mt = { __tostring = function() return 'Hello from __tostring!' end }
setmetatable(myObject, mt)
`)
L.GetGlobal("myObject")
if L.CallMeta(-1, "__tostring") == 1 {
fmt.Println(L.GetString(-1))
} else {
fmt.Println("No metatable or field __tostring")
}
*/
func (L State) CallMeta(objIdx int, e string) int {
cEvent := CStr(e)
defer cEvent.free()
status := C.luaL_callmeta_wrap(L.c(), C.int(objIdx), cEvent.c)
return int(status)
}
/*
If the registry already has the key tname, returns false. Otherwise,
creates a new table to be used as a metatable for userdata, adds it to the registry with key tname, and returns true.
In both cases pushes onto the stack the final value associated with tname in the registry.
*/
func (L State) NewMetaTable(name string) bool {
cName := CStr(name)
defer cName.free()
return C.luaL_newmetatable_wrap(L.c(), cName.c) != 0
}
/*
Creates and returns a reference in the registry for the object at the top of the stack.
It pops the object from the stack.
# Example
L.PushString("Hello, world!")
ref := L.CreateRef()
if L.FromRef(ref) {
fmt.Println(L.GetString(-1))
}
*/
func (L State) CreateRef() int {
return int(C.luaL_ref_wrap(L.c(), LUA_REGISTRYINDEX))
}
/*
Gets the value associated with ref in the registry and pushes it onto the stack.
If the reference is invalid/nil, it returns false and does not push anything.
# Example
L.PushString("Hello, world!")
ref := L.CreateRef()
if L.FromRef(ref) {
fmt.Println(L.GetString(-1))
}
*/
func (L State) FromRef(ref int) bool {
if ref == LUA_REFNIL || ref == LUA_NOREF {
return false
}
L.RawGetI(LUA_REGISTRYINDEX, ref)
return true
}
/*
Deletes the reference ref from the registry.
If ref is LUA_REFNIL or LUA_NOREF, this function does nothing.
# Example
L.PushString("Hello, world!")