-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_intf.cpp
1030 lines (945 loc) · 45.5 KB
/
lua_intf.cpp
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
//
// Copyright 2015-2022 by Kevin L. Goodwin [[email protected]]; All rights reserved
//
// This file is part of K.
//
// K is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// K is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with K. If not, see <http://www.gnu.org/licenses/>.
//
//#### lh_ = my Lua Helper functions
#include "lua_intf_common.h"
// we use some macros defined in lua .h files that contain C-style casts which generate the following warning ...
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
int LDS( PCChar tag, lua_State *L ) { // Lua Dump Stack
const auto top( lua_gettop(L) );
DBG( "+luaStack.bottom, %d els @ '%s'", top, tag );
for( auto ix(1); ix <= top; ++ix ) {
const auto type( lua_type( L, ix ) );
switch( type ) {
break;case LUA_TSTRING: DBG( " [%d]=\"%s\"", ix, lua_tostring( L, ix ) );
break;case LUA_TBOOLEAN: DBG( " [%d]=%s" , ix, lua_toboolean( L, ix ) ? "true":"false" );
break;case LUA_TNUMBER: DBG( " [%d]=%f" , ix, lua_tonumber( L, ix ) );
break;default: DBG( " [%d]={%s}" , ix, lua_typename( L, type ) );
}
}
0 && DBG( "-luaStack.top Dump @ '%s'", tag );
return 1;
}
// lua_getglobal (among other Lua APIs) panics if _G[name] == nil unless we wrap in lua_pcall
STATIC_FXN int callback_getglobal_tos( lua_State* L ) {
lua_getglobal( L, lua_tostring(L, -1) );
return 1;
}
STATIC_FXN bool lh_getglobal_failed( lua_State* L, PCChar name ) {
lua_pushcfunction( L, callback_getglobal_tos );
lua_pushstring( L, name );
return lua_pcall(L, 1, 1, 0) != 0; // true == error
}
bool lh_push_global_function( lua_State *L, const char *szFuncnm ) {
if( lh_getglobal_failed( L, szFuncnm ) ) {
return !Msg( "%s: Lua symbol '%s' is NOT A global", __func__, szFuncnm );
}
if( !lua_isfunction( L, -1 ) ) {
return !Msg( "%s: Lua symbol '%s' is NOT A FUNCTION", __func__, szFuncnm );
}
return false;
}
// 20160601 replace C varargs with typesafe variadic template approach (cost: build/link time)
STATIC_FXN int lh_push_( lua_State *L, stref in ) { lua_pushlstring( L, in.data(), in.length() ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, PFBUF in ) { l_construct_FBUF( L, in ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, PView in ) { l_construct_View( L, in ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, double in ) { lua_pushnumber( L, in ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, int in ) { lua_pushinteger( L, in ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, PCChar in ) { lua_pushstring( L, in ); return 1; }
STATIC_FXN int lh_push_( lua_State *L, bool in ) { lua_pushboolean( L, in != 0 ); return 1; }
template<typename HEAD>
int lh_push( lua_State *L, HEAD v ) { return lh_push_( L, v ); }
template<typename HEAD, typename... TAIL>
int lh_push( lua_State *L, HEAD head, TAIL... tail ) {
const auto r1( lh_push( L, head ) ); // force order of eval HEAD -> ... TAIL
return r1 + lh_push( L, tail... );
}
#define LUA_TOBOOLEAN(L, ix) ToBOOL( lua_toboolean( L, ix ) )
STATIC_FXN void lua_assigncppstring( lua_State *L, int ix, std::string &out ) {
size_t srcBytes; auto pSrc( lua_tolstring(L, ix, &srcBytes) ); 0 && DBG( "%s raw='%" PR_BSR "'", __func__, static_cast<int>(srcBytes), pSrc );
out.assign( pSrc, srcBytes ); 0 && DBG( "%s cst='%s'", __func__, out.c_str() );
}
#if 0
STATIC_FXN bool lh_pop_bool( lua_State *L ) { return 0 != lua_toboolean( L, -1 ); }
STATIC_FXN void lh_pop_( lua_State *L, int ix, bool &out ) { out = 0 != lua_toboolean( L, ix ); }
STATIC_FXN void lh_pop_( lua_State *L, int ix, int &out ) { out = lua_tointeger( L, ix ); DBG("%s =%d", __func__, out ); }
STATIC_FXN void lh_pop_( lua_State *L, int ix, double &out ) { out = lua_tonumber ( L, ix ); }
STATIC_FXN void lh_pop_( lua_State *L, int ix, std::string &out ) {
size_t srcBytes; auto pSrc( lua_tolstring(L, ix, &srcBytes) ); 0 && DBG( "%s raw='%" PR_BSR "'", __func__, static_cast<int>(srcBytes), pSrc );
out.assign( pSrc, srcBytes ); 1 && DBG( "%s cst='%s'", __func__, out.c_str() );
}
// does NOT work as intended (in all cases, v is a reference, and the referent is NOT updated
template<typename HEAD>
int lh_pop( lua_State *L, HEAD v ) { lh_pop_( L, -1, v ); return 1; }
template<typename HEAD, typename... TAIL>
int lh_pop( lua_State *L, HEAD head, TAIL... tail ) {
const auto rt( lh_pop( L, tail... ) ); // force order of eval TAIL -> ... HEAD
return rt + lh_pop( L, head ) ;
}
#endif
STATIC_VAR lua_State* L_edit;
STATIC_VAR lua_State* L_restore_save;
STIL void get_LuaRegistry( lua_State* L, PCChar varnm ) { lua_getfield( L, LUA_REGISTRYINDEX, varnm ); }
STIL void set_LuaRegistry( lua_State* L, PCChar varnm ) { lua_setfield( L, LUA_REGISTRYINDEX, varnm ); }
void set_LuaRegistryInt( lua_State* L, PCChar varnm, int val ) {
lua_pushinteger(L, val);
set_LuaRegistry( L, varnm );
}
int get_LuaRegistryInt( lua_State* L, PCChar varnm ) {
get_LuaRegistry( L, varnm );
const auto rv( luaL_optint( L, -1, -1 ) );
lua_pop(L, 1);
return rv;
}
#define LREGI(nm) \
STATIC_CONST char ksz##nm[] = #nm; \
STIL int LREGI_get_##nm( lua_State *L ) { return get_LuaRegistryInt( L, ksz##nm ); } \
STIL void LREGI_set_##nm( lua_State *L, int val ) { set_LuaRegistryInt( L, ksz##nm, val ); }
//---------------------------
void set_LuaRegistryPtr( lua_State* L, PCChar varnm, void *val ) {
lua_pushlightuserdata (L, val);
set_LuaRegistry( L, varnm );
}
void *get_LuaRegistryPtr( lua_State* L, PCChar varnm ) {
get_LuaRegistry( L, varnm );
auto rv( lua_touserdata( L, -1 ) );
lua_pop(L, 1);
return rv;
}
#define LREGP(nm,type) \
STATIC_CONST char ksz##nm[] = #nm; \
STIL type LREGP_get_##nm( lua_State *L ) { return static_cast<type>(get_LuaRegistryPtr( L, ksz##nm )); } \
STIL void LREGP_set_##nm( lua_State *L, type val ) { set_LuaRegistryPtr( L, ksz##nm, reinterpret_cast<void *>(val) ); }
//---------------------------
//######################################################################################################################
//
// Historical notes on Lua GARBAGE COLLECTION
//
// First-implementation (20060721) policy was to call "lua_gc(LUA_GCCOLLECT)"
// inside LuaCallCleanup() DTOR. This was observed to have the following
// consequence:
//
// 1. Editor invokes Lua EdFxn edhelp.
//
// 2. edhelp calls C code l_FBUF_function_new (Lua FBUF ctor), passing a
// string parameter (the filename). In this case, the string parameter
// is a Lua string expression, not a Lua variable (so there are probably
// NO references to the string parameter itself).
//
// 3. As part of filename resolution, l_FBUF_function_new calls
// CompletelyExpandFName_wEnvVars, which calls
// LuaCtxt_Edit::ExpandEnvVarsOk, a wrapper around the Lua function
// StrExpandEnvVars.
//
// 4. At the end of its execution, LuaCtxt_Edit::ExpandEnvVarsOk, being a
// good wrapper function, destroys an instance of LuaCallCleanup, which
// call_ED_ lua_gc(LUA_GCCOLLECT), which, among other things, _typically_
// blows away the Lua string parameter to l_FBUF_function_new, resulting
// in an otherwise "good" C pointer in l_FBUF_function_new pointing to junk.
//
// Two apparent solutions:
//
// 1. Strdup() ALL string function parameters coming from Lua.
//
// 2. Change my GC invocation to be "less aggressive" so Lua values aren't
// GC'd until they are no longer used by C code.
//
// 2a. Don't GC in the execution thread; GC in the Idle Handler thread (which
// is really a coroutine therefore mututally exclusive vs. the execution
// thread).
//
// #1 is undesirable for performance reasons (Strdup'ing every string
// parameter takes a lot of time) and correctness reasons (can you say "high
// liklihood of memory leak"?). I suppose I could always use ALLOCA_STRDUP,
// which causes even MORE object-code-size growth than using Strdup...
//
// #2 is a challenge to do right: the editor core can call Lua functions (EX:
// LuaCtxt_Edit::ExpandEnvVarsOk) directly (that is, with no underlaying Lua
// invocations pending) at any time. If GC is not done at those times out of
// fear of the case above, then garbage will collect over an indefinite
// period, which is undesirable (although one could say: "RAM is cheap, who
// cares?").
//
// #2a: If this can be done with a guarantee that is no concurrency hazard
// [which as noted in the updated description of 2a above, _is_ the case], it
// may be ideal. Historical caveat: If not, this will be a problem anyway
// since it's likely that _eventually_ Lua code will be called from the Idle
// thread. [20140123 I don't think this is a concern since the Idle thread
// does not preempt itself]
//
//-----------------
//
// update 20060721 Lua appears to make COPIES of function parameter values that
// are passed to C (which are immediately susceptible to be
// GC'd) even if at the source level, the parameter is (the
// value of) a persistent variable.
//
// It strikes me that, if I DO NO GC AT ALL (which I believe
// means I'll be using the background GC built into the Lua core
// itself (the refman implies this exists), it MAY decide to GC
// at a "inopportune time", causing the same sorts of problems
// described above.
//
//-----------------
//
// update 20060803
// I was pushed into adopting solution 2a (above). The story:
// TAWK has been "banished" (totally removed). This means I'm
// now using the Lua-version of tags lookup (and a Lua version
// of everything else that TAWK used to do) full-time. So I'm
// now regularly (at work) loading much larger tags-databases
// into Lua. And I also have an event-handler for GETFOCUS
// written in Lua. Meaning I was calling lua_gc(LUA_GCCOLLECT)
// after the Lua event-handler hooking occurred. What I've
// inferred is that every call to lua_gc(LUA_GCCOLLECT) causes
// the examination of every (heap) object that has been
// allocated. With a large tags database (3-deep nested table),
// this is surely many thousands of objects. Which resulted in
// a very noticable slowdown in the rate of file switching when
// I hold down the 'setfile' command's key (my famous
// "blink-compare" technique). With some experimentation, this
// delay was attributed to call to lua_gc(LUA_GCCOLLECT). So I
// decided to try the proper solution: LuaIdleGC(), which calls
// lua_gc(LUA_GCSTEP) is the result. Varying the 'luagcstep'
// switch value, it's fun to watch the Lua heapsize shrink back
// once I stop hitting keys. In terms of robustness: so far so
// good. I expected no problems, and there have been none so
// far...
//
class LuaCallCleanup_ {
lua_State *d_L;
public:
LuaCallCleanup_( lua_State *L, const char *fxn ) : d_L(L) {
const auto els( lua_gettop( d_L ) );
if( els ) { 0 && DBG( "%s @ entry stack contains %d (%s)", __func__, els, fxn );
0 && LDS( __func__, L );
}
}
~LuaCallCleanup_() {
lua_settop( d_L, 0 ); // Lua refman: "If index is 0, then all stack elements are removed."
}
};
#define LuaCallCleanup( L ) LuaCallCleanup_ lcc( L, __func__ )
//---------------------------------------------------------------------------------
GLOBAL_VAR lua_State **g_L[] = { // in prep for more L's (for special purposes such as state-file write/read)
&L_edit ,
&L_restore_save ,
}; // of course this (AAA) makes the pointers themselves (e.g. L_edit) _shared variables_
GLOBAL_VAR int g_iLuaGcStep = 10;
void LuaIdleGC() {
if( g_iLuaGcStep ) {
for( const auto &gl : g_L ) {
if( *gl ) { // AAA
lua_gc( *gl, LUA_GCSTEP, g_iLuaGcStep ); // AAA
}
}
}
}
//---------------------------------------------------------------------------------
STATIC_VAR size_t s_LuaHeapSize;
size_t LuaHeapSize() { return s_LuaHeapSize; }
enum { DBG_LUA_ALLOC=0 };
STATIC_FXN void *l_alloc( void *ud, void *ptr, size_t osize, size_t nsize ) {
static_cast<void>(ud); // mask 'not used' warning
if( nsize == osize ) { // yes this happens ?!
return ptr;
}
const auto delta( nsize - osize );
s_LuaHeapSize += delta;
if( nsize == 0 ) {
DBG_LUA_ALLOC && ptr && DBG( "%s %" PR_SIZET " free %5" PR_SIZET " (%5" PR_SIZET "/%5" PR_SIZET ") P= %p", __func__, s_LuaHeapSize, delta, osize, nsize, ptr );
free( ptr ); // ANSI requires that free(nullptr) has no effect
return nullptr;
}
// most of this conditional code is for debug/logging purposes
const PVoid rv( realloc( ptr, nsize ) ); // ANSI requires that realloc(nullptr, size) == malloc(size)
DBG_LUA_ALLOC && DBG( "%s %" PR_SIZET " %s %5" PR_SIZET " (%5" PR_SIZET "/%5" PR_SIZET ") P=%p -> %p", __func__, s_LuaHeapSize, ptr ? "real" : "new ", delta, osize, nsize, ptr, rv );
return rv;
}
// reliable reading of Lua global variables (including '.'-separated table expressions) from C
//
STIL bool getTblVal( lua_State *L, PCChar key, size_t keylen, bool gettingGlobal ) { 0 && DBG( "%s indexing %s '%" PR_BSR "'", __func__, gettingGlobal ? "G":" ", static_cast<int>(keylen), key );
// [-o, +p, x]
// o: how many elements the function pops from the stack
// p: how many elements the function pushes onto the stack
// x: tells whether the function may throw errors:
// '-' means the function never throws any error;
// 'm' means the function may throw an error only due to not enough memory;
// 'e' means the function may throw other kinds of errors;
// 'v' means the function may throw an error on purpose
//
// I _added_ a lua_getfield-like API which takes a stref-like, so we can accept and pass (effectively) a stref here.
lua_getfield_lstr( L, gettingGlobal ? LUA_GLOBALSINDEX : -1, key, keylen ); // [-0, +1, e] if field key is not defined
if( !gettingGlobal ) { // if not reading global (therefore lua_getfield of table [formerly] on top of stack)
lua_remove(L, -2); // pop table [formerly] on top of stack [-1, +0, e] which (its field having been read) is no longer needed
}
return true;
}
STATIC_FXN bool gotTblVal( lua_State *L, PCChar tbdescr ) { enum {SD=0};
const size_t tdsclen = Strlen( tbdescr );
auto pst( tbdescr );
for( auto pc(tbdescr); pc < tbdescr + tdsclen; ++pc ) {
if( *pc == '.' ) {
if( !getTblVal( L, pst, pc - pst, pst == tbdescr ) ) { SD && DBG( "%s- !getTblVal '%" PR_BSR "'", __func__, static_cast<int>(pc - tbdescr), tbdescr );
return false;
}
if( !lua_istable( L, -1 ) ) { SD && DBG( "%s- !lua_istable '%" PR_BSR "'", __func__, static_cast<int>(pc - tbdescr), tbdescr );
return false;
}
pst = pc+1;
}
}
const auto rv( getTblVal( L, pst, (tbdescr + tdsclen) - pst, pst == tbdescr ) );
SD && !rv && DBG( "%s- !last getTblVal '%" PR_BSR "'", __func__, static_cast<int>(tdsclen), tbdescr );
return rv; // *** caller is responsible for converting TOS to appropriate C value *** if table lookup failed here, lua_isnil(L,-1) is true
}
STATIC_FXN PChar CopyLuaString( PChar dest, size_t sizeof_dest, lua_State *L, int stackLevel ) {
if( sizeof_dest > 0 ) {
dest[0] = '\0';
const auto pSrc( lua_tostring( L, stackLevel ) ); // converts number to string!
if( pSrc ) {
scpy( dest, sizeof_dest, pSrc );
}
}
return dest;
}
// returns false if any errors
STATIC_FXN bool LuaTblKeyExists( lua_State *L, PCChar tableDescr ) { 0 && DBG( "+%s '%s'? (%p)", __func__ , tableDescr, L );
if( !L ) { return false; }
LuaCallCleanup( L );
const auto rv( gotTblVal( L, tableDescr ) && !lua_isnil(L,-1) ); 0 && DBG( "-%s '%s' %c", __func__ , tableDescr, rv?'t':'f' );
return rv;
}
// returns nullptr if any errors
STATIC_FXN PChar LuaTbl2S0( lua_State *L, PChar dest, size_t sizeof_dest, PCChar tableDescr ) { 0 && DBG( "+%s '%s'?", __func__ , tableDescr );
if( !L ) { return nullptr; }
LuaCallCleanup( L );
if( !gotTblVal( L, tableDescr ) ) { return nullptr; }
return CopyLuaString( dest, sizeof_dest, L, -1 );
}
enum { DBLUATBRD=0 };
// returns empty string if any errors
STATIC_FXN PChar LuaTbl2S( lua_State *L, PChar dest, size_t sizeof_dest, PCChar tableDescr, PCChar pszDflt ) {
if( sizeof_dest > 0 ) {
dest[0] = '\0';
auto rv( LuaTbl2S0( L, dest, sizeof_dest, tableDescr ) );
if( !rv ) {
if( pszDflt ) {
scpy( dest, sizeof_dest, pszDflt ); DBLUATBRD && DBG( "%s '%s' -> '%s' (dflt)" , __func__ , tableDescr, dest );
}
else { DBLUATBRD && DBG( "%s '%s' -> '%s' (no-dflt)" , __func__ , tableDescr, dest );
}
}
else { DBLUATBRD && DBG( "%s '%s' -> '%s' (from Lua)" , __func__ , tableDescr, dest );
}
}
else { DBLUATBRD && DBG( "%s '%s' -> '' (empty-dest)" , __func__ , tableDescr );
}
return dest;
}
// returns empty string if any errors
PChar LuaCtxt_Edit::Tbl2S( PChar dest, size_t sizeof_dest, PCChar tableDescr, PCChar pszDflt ) { return LuaTbl2S( L_edit, dest, sizeof_dest, tableDescr, pszDflt ); }
STATIC_FXN int lua_atpanic_handler( lua_State *L ) {
linebuf msg;
CopyLuaString( BSOB(msg), L, -1 );
DBG( "########################################## %s called ##########################################", __func__ );
DBG( "### %s", msg );
DBG( "########################################## %s called ##########################################", __func__ );
// SW_BP;
exit(1);
}
STIL void DeleteAllLuaCmds() { CmdIdxRmvCmdsByFunction( fn_runLua() ); }
STATIC_FXN void goto_file_line( PFBUF pFBuf, LINE yLine ) {
pFBuf->PutFocusOn();
g_CurView()->MoveCursor( yLine, 0 );
fExecute( "begline" );
}
// stolen from lua-5.1/src/lua.c
STATIC_FXN int traceback (lua_State *L) {
if (!lua_isstring(L, 1)) /* 'message' not a string? */
return 1; /* keep it intact */
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1); /* pass error message */
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
return 1;
}
// stolen from lua-5.1/src/lua.c, except nres param to lua_pcall is passed directly
STATIC_FXN int lh_pcall_inout (lua_State *L, int narg, int nres) { enum { SD=0 };
const auto top( lua_gettop(L) ); /* function index */
const auto base( top - narg ); /* function index */
SD && DBG( "lh_pcall_inout(%d/%d,%d,%d)", narg, top, nres, base );
SD && LDS( "+ lh_pcall_inout:pre-ins-traceback-fxn", L );
lua_pushcfunction(L, traceback); /* push traceback function */
lua_insert(L, base); /* put it under chunk and args */
//signal(SIGINT, laction);
SD && LDS( "+ lh_pcall_inout:lua_pcall", L );
const auto pcall_rv( lua_pcall(L, narg, nres, base) ); SD && LDS( "- lh_pcall_inout:lua_pcall", L );
//signal(SIGINT, SIG_DFL);
lua_remove(L, base); /* remove traceback function */ SD && LDS( "+ lh_pcall_inout:post-rmv-traceback-fxn", L );
/* force a complete garbage collection in case of errors */
if (pcall_rv != 0) { lua_gc(L, LUA_GCCOLLECT, 0); }
return pcall_rv;
}
STATIC_FXN void lh_handle_pcall_err( lua_State *L, bool fCompileErr=false ) { // c:\klg\sn\k\util.lua:107: variable 'at' is not declared
linebuf msg;
CopyLuaString( BSOB(msg), L, -1 );
DBG( "L=%p, LuaMsg='%s'", L, msg );
const auto tp( fCompileErr?"compile ":"run" );
// STATIC_CONST char rtErrTmplt[] = "*** Lua %stime error";
//Msg( rtErrTmplt, tp, msg );
s_pFbufLuaLog->PutLastLineRaw( "" );
const auto yMsgStart( s_pFbufLuaLog->LastLine()+1 );
s_pFbufLuaLog->FmtLastLine( "*** Lua %stime error\n%s", tp, msg );
#if 1
s_pFbufLuaLog->PutLastLineRaw( "" );
const auto pv( s_pFbufLuaLog->PutFocusOn() );
pv->MoveCursor( yMsgStart, 0 );
#else
const PCChar pll = strrchr( msg, '\n' );
PCChar pMsg = pll && (*(pll+1) == '\t') && *(pll+2) ? pll+2 : msg; // if Lua's emsg is multiline, the last line has a leading tab to skip
DBG( "pMsg='%s'", pMsg );
// For simple Regex string searches (vs. search-thru-file-until-next-match) ops, use Regex_Compile + CompiledRegex::Match
std::vector<stref> cs( 4 );
{
auto pre( Regex_Compile( "^(.*):(\\d+): (.*)$", true ) );
if( !pre ) {
Msg( "%s had regex COMPILE error", __func__ );
return;
}
int mc;
auto pmatch( pre->Match( 0, pMsg, Strlen(pMsg), &mc, STR_HAS_BOL_AND_EOL, &cs ) );
Delete0( pre );
if( !pmatch ) {
Msg( "%s had regex match error", __func__ );
return;
}
0 && DBG( "%d captures!", cs.Count() );
// for( int ix=0; ix < cs.Count(); ++ix )
// DBG( "cs%d='%s'", ix, cs.Str(ix) );
if( cs.Count() != 4 ) {
Msg( "%s had regexcapture-count error", __func__ );
return;
}
}
PChar endptr;
const auto yLine( strtoul( cs.Str(2), &endptr, 10 ) - 1 );
auto pFBuf( OpenFileNotDir_NoCreate( cs.Str(1) ) );
if( !pFBuf ) {
return;
}
goto_file_line( pFBuf, yLine );
PCChar emsg( cs.Str(3) );
// if possible, display a truncated msg, so user doesn't have to mentally
// discard the file location info which we've just navigated to for his
// convenience...
//
Msg( rtErrTmplt, tp, emsg );
s_pFbufLuaLog->FmtLastLine( rtErrTmplt, tp, emsg );
#endif
}
LREGI(EvtHandlerEnabled)
LREGP(cleanup,void *)
STATIC_FXN void call_EventHandler( lua_State *L, PCChar eventName ) { enum { SD=0 };
if( !L || LREGI_get_EvtHandlerEnabled( L ) < 1 ) {
return;
}
DBG_LUA_ALLOC && DBG( "%s: heapChange ...", __func__ );
const size_t s_LuaHeapBytesAtStart( LuaHeapSize() );
LuaCallCleanup( L );
if( lh_push_global_function( L, "CallEventHandlers" ) ) {
return;
}
const auto failed( lh_pcall_inout( L, lh_push( L, eventName ), 0 ) ); SD && DBG( "C calling CallEventHandlers(%s)", eventName );
if( failed ) {
LREGI_set_EvtHandlerEnabled( L, 0 ); // don't want avalanche of errors
lh_handle_pcall_err( L );
}
}
void LuaCtxt_ALL::call_EventHandler( PCChar eventName ) {
0 && DBG( "L_edit=%p, L_restore_save=%p", L_edit, L_restore_save );
call_EventHandler( L_edit , eventName );
call_EventHandler( L_restore_save, eventName );
}
bool ARG::ExecLuaFxn() { enum { SD=0 };
// Using ARG::CmdName(), we discover the name of the fxn being invoked.
// We will call the Lua function of the same name. If the Lua entity of
// that name is a table rather than a function, we will attempt to call
// table.d_argType() (e.g. function.TEXTARG()). If table.d_argType() is
// not found, we will attempt to call table.ANYARG() (e.g. function.ANYARG()).
//
// 20060309 klg
//
const auto cmdName( CmdName() ); SD && DBG( "%s: '%s'", __func__, cmdName );
const auto L( L_edit );
LuaCallCleanup( L );
// table=GetEdFxn_FROM_C(cmdName) -- 20110216 kgoodwin replaced old scheme which wrote all cmd tables into k.lua._G (!!!)
// GetEdFxn_FROM_C is implemented in k.luaedit
STATIC_CONST auto s_edfx_lookup = "GetEdFxn_FROM_C";
if( lh_push_global_function( L, s_edfx_lookup ) ) {
return false;
}
{
const auto failed( lh_pcall_inout( L, lh_push( L, cmdName ), 1 ) );
if( failed ) { SD && DBG( "%s: docall GetEdFxn_FROM_C(%s) failed", __func__, cmdName );
return Msg( "docall GetEdFxn_FROM_C(%s) failed", cmdName );
}
}
const auto funcIsTable( lua_istable( L, -1 ) ); SD && DBG( "%s: '%s' %d", __func__, "funcIsTable", funcIsTable );
auto argTypeNm( ArgTypeName() ); SD && DBG( "%s: '%s', ARG=%s, 0x%X", __func__, cmdName, argTypeNm, d_argType );
if( funcIsTable ) { /* support Lua decomposition, e.g. edfxn.BOXARG */ SD && DBG( "%s: '%s' is table", __func__, cmdName );
lua_getfield(L, -1, argTypeNm);
if( !lua_isfunction( L, -1 ) ) { // edfxn.ANYARG is last chance/catchall
lua_pop( L, 1 );
STATIC_CONST auto kszAnyarg = "ANYARG";
lua_getfield( L, -1, kszAnyarg );
if( lua_isfunction( L, -1 ) ) {
argTypeNm = kszAnyarg;
}
}
}
if( !lua_isfunction( L, -1 ) ) {
if( funcIsTable ) { return Msg( "Lua CMD %s.%s is not a function", cmdName, argTypeNm ); }
else { return Msg( "Lua CMD %s is not a function", cmdName ); }
}
// note that only a SUBSET of ARG type bits can be set here, leading to
// non-obvious Lua EdFxn mapping of attr to function-key (e.g. CURSORFUNC &
// MACROFUNC both map to NOARG functions)
//
// This is documented where NOARG is defined, in column "Actually can be set
// in ARG::Abc?": there are 6 ARG types marked "true". Also, ArgTypeName()
// can only return the names of these 6 ARG types.
//
// 20091119 kgoodwin
//
const auto actualArg( ActualArgType() );
lua_createtable(L, 0, 12); // ref os_date; 12 = number of fields
switch( actualArg ) {
case STREAMARG : return BadArg(); // not yet supported
default : return BadArg();
case LINEARG : setfield( L, "minY", 1+d_linearg.yMin );
setfield( L, "maxY", 1+d_linearg.yMax );
setfield( L, "minX", 1+0 );
setfield( L, "maxX", COL_MAX );
break;
case BOXARG : setfield( L, "minY", 1+d_boxarg.flMin.lin );
setfield( L, "maxY", 1+d_boxarg.flMax.lin );
setfield( L, "minX", 1+d_boxarg.flMin.col );
setfield( L, "maxX", 1+d_boxarg.flMax.col );
break;
case TEXTARG : setfield( L, "text" , d_textarg.pText ); SD && DBG( "TEXTARG = %s", d_textarg.pText );
ATTR_FALLTHRU;
case NOARG :
case NULLARG :{const int xAll( 1+g_CursorCol () );
const int yAll( 1+g_CursorLine() );
setfield( L, "cursorX", xAll );
setfield( L, "minX" , xAll );
setfield( L, "maxX" , xAll );
setfield( L, "cursorY", yAll );
setfield( L, "minY" , yAll );
setfield( L, "maxY" , yAll );
}break;
} SD && DBG( ".%s = %d", "type", actualArg );
setfield( L, "argCount", d_cArg );
setfield( L, "type" , actualArg );
l_construct_FBUF( L, g_CurFBuf() ); lua_setfield( L, -2, "fbuf" );
l_View_function_cur( L ); lua_setfield( L, -2, "view" );
lua_pushboolean( L, d_fMeta ); lua_setfield( L, -2, "fMeta" ); SD && DBG( ".%s = %s", "fMeta", d_fMeta?"true":"false" );
const auto failed( lh_pcall_inout( L, 1, 1 ) );
if( failed ) {
UnhideCursor();
lh_handle_pcall_err( L );
return false;
}
return LUA_TOBOOLEAN( L, -1 );
}
//######################################################################################################################
STATIC_FXN void l_hook_handler( lua_State *L, lua_Debug *ar ) {
if( ExecutionHaltRequested() ) {
lua_pushstring( L, "Asynchronous ExecutionHaltRequested" );
lua_error( L );
}
}
STATIC_FXN void cleanup_lua( lua_State *L ) {
if( L ) { 0 && DBG( "####################### CLOSING current Lua session ###################" );
auto cleanup = (void (*)(lua_State *L))LREGP_get_cleanup( L );
if( cleanup ) { cleanup( L ); }
lua_close( L );
}
}
STATIC_FXN void cleanup_plua( lua_State *&L ) {
if( L ) { 0 && DBG( "####################### CLOSING current Lua session ###################" );
cleanup_lua( L );
L = nullptr;
}
}
void LuaClose() {
for( const auto gl : g_L ) {
cleanup_plua( *gl );
}
}
STATIC_FXN lua_State *init_lua_ok( void (*cleanup)(lua_State *L), void (*openlibs)(lua_State *L) ) {
if( !openlibs ) { return nullptr; }
s_pFbufLuaLog->MakeEmpty();
s_pFbufLuaLog->MoveCursorToBofAllViews();
0 && DBG( "%s+", __func__ );
// NB: each Lua 5.1.2 lua_newstate consumes 2.5KB! 20070724 kgoodwin
const auto luaHeapBytesAtStart( LuaHeapSize() );
lua_State *L = lua_newstate( l_alloc, nullptr );
const auto heapChange( LuaHeapSize() - luaHeapBytesAtStart );
if( !L ) { DBG( "%s- lua_newstate FAILED", __func__ );
return nullptr;
}
DBG_LUA_ALLOC && DBG( "%s lua_newstate (%" PR_SIZET " bytes) ******************************************", __func__, heapChange );
lua_atpanic( L, lua_atpanic_handler );
lua_sethook( L, l_hook_handler, LUA_MASKLINE | LUA_MASKCOUNT, INT_MAX );
LuaCallCleanup( L );
openlibs( L );
// override package.path so required Lua code is only looked for in sm dir as editor exe
// (I used to setenv("LUA_PATH") but that impacted child processes that ran Lua.exe)
lua_getglobal( L, "package" ); Assert( lua_istable( L, -1 ) ); // NB: package table does not exist until l_OpenStdLibs() has been called!
setfield( L, "path", FmtStr<_MAX_PATH>( "%s?.lua", getenv( "KINIT" ) ) );
LREGP_set_cleanup( L, reinterpret_cast<void *>(cleanup) );
0 && DBG( "%s- %p", __func__, L );
return L;
}
STATIC_FXN bool loadLuaFileOk( lua_State *L, PCChar fnm ) { enum { SD=0 };
auto fLoadOK(false); SD && DBG( "%s+1 L=%p fnm='%s'", __func__, L, fnm );
const auto luaRC( luaL_loadfile( L, fnm ) ); SD && DBG( "%s+2 L=%p luaRC=%u", __func__, L, luaRC );
switch( luaRC ) {
break;case LUA_ERRFILE: DBG( "LUA_ERRFILE" ); lh_handle_pcall_err( L, true );
break;case LUA_ERRSYNTAX: DBG( "LUA_ERRSYNTAX" ); lh_handle_pcall_err( L, true );
break;case LUA_ERRMEM: Msg( "Lua memory error while loading '%s'" , fnm );
break;default: Msg( "Unknown Lua error %d loading '%s'", luaRC, fnm );
break;case 0: SD && LDS( "loadLuaFileOk luaL_loadfile() OK", L );
{
const auto failed( lh_pcall_inout( L, 0, 0 ) );
if( failed ) { SD && DBG( "%s L=%p docall() Failed", __func__, L );
// NB! The above lh_pcall_inout() runtime includes the COMPILE phase
// of any modules require()'d by 'fnm'. As such, even if
// we get here, we could be facing a compile error, so we call
// lh_handle_pcall_err( , true ) so the L gets shut down
// ASAP, preventing a double fault which causes a Lua panic.
//
// 20060922 klg
//
lh_handle_pcall_err( L, true );
}
else {
fLoadOK = true;
}
}
} SD && DBG( "%s+3 L=%p fLoadOK=%c", __func__, L, fLoadOK?'t':'f' );
!fLoadOK && DBG( "%s failed", __func__ ); SD && DBG( "%s- L=%p fLoadOK=%c", __func__, L, fLoadOK?'t':'f' );
return fLoadOK;
}
STATIC_FXN bool LuaCtxt_InitOk(
PCChar filename
, std::string &dupdFnm
, lua_State *&Linout
, void (*cleanup) (lua_State *L)
, void (*openlibs) (lua_State *L)
, void (*post_load)(lua_State *L)
) { enum {SD=0}; SD && DBG( "%s+ %s", __func__, filename );
//
// Design note: if loadLuaFileOk fails while processing source file(s) for LuaCtxt_Edit (syntax error, assert),
// *L remains non-null but defectively constructed lua_State, and a panic (causing process crash) will occur
// during execution of LuaCtxt_Edit::Tbl2S() as loadLuaFileOk attempts to perform error-logging/-display via e.g.
//
// FBUF::PutFocusOn() -> FTypeSetting::Update() -> LuaCtxt_Edit::Tbl2S()
//
// Solution: construct local L and DON'T assign to Linout UNTIL successful construction concludes
//
auto L( init_lua_ok( cleanup, openlibs ) );
if( !L ) { SD && DBG( "%s- init_lua_ok() failed", __func__ );
return false; // w/Linout undisturbed
}
if( !loadLuaFileOk( L, filename ) ) { SD && DBG( "%s- loadLuaFileOk() failed", __func__ );
cleanup_lua( L );
return false; // w/Linout undisturbed
}
cleanup_plua( Linout ); // only now are we committed... zap the now-redundant previous instance
dupdFnm.assign( filename );
Linout = L; /* post_load may depend on this being set!!! */ SD && DBG( "%s- OK", __func__ );
post_load( L ); // Lua environment-loaded hook-outs
return true;
}
STATIC_FXN void L_restore_save_post_load( lua_State *L ) { // Lua environment-loaded hook-outs
}
STATIC_FXN void L_std_openlibs( lua_State *L ) {
l_OpenStdLibs( L );
// l_register_EdLib( L ); //BUGBUG remove or trim down?
}
STATIC_VAR std::string psrc_LuaCtxt_restore_save;
bool LuaCtxt_State::InitOk( PCChar filename ) {
return LuaCtxt_InitOk(
filename
, psrc_LuaCtxt_restore_save
, L_restore_save
, nullptr
, L_std_openlibs
, L_restore_save_post_load
);
}
STATIC_FXN void L_edit_post_load( lua_State *L ) { // Lua environment-loaded hook-outs
LuaCtxt_Edit::RegisterAllLuaEdFxns();
LREGI_set_EvtHandlerEnabled( L, 1 );
Reread_FTypeSettings();
}
STATIC_FXN void L_edit_openlibs( lua_State *L ) {
l_OpenStdLibs( L );
l_register_EdLib( L );
}
STATIC_FXN void L_edit_cleanup( lua_State *L ) {
DeleteAllLuaCmds();
}
STATIC_VAR std::string psrc_LuaCtxt_Edit;
bool LuaCtxt_Edit::InitOk( PCChar filename ) {
return LuaCtxt_InitOk(
filename
, psrc_LuaCtxt_Edit
, L_edit
, L_edit_cleanup
, L_edit_openlibs
, L_edit_post_load
);
}
bool ARG::lua() {
if( d_cArg > 0 ) {
s_pFbufLuaLog->PutFocusOn();
return true;
}
WriteAllDirtyFBufs(); // In case any required Lua files are dirty. Pseudofiles are not saved.
if( !psrc_LuaCtxt_Edit.empty() ) {
const auto pFBuf( OpenFileNotDir_NoCreate( psrc_LuaCtxt_Edit.c_str() ) );
if( !pFBuf ) {
return Msg( "'%s' not found?", psrc_LuaCtxt_Edit.c_str() );
}
if( g_CurFBuf() == pFBuf ) {
return LuaCtxt_Edit::InitOk( pFBuf->Name() );
}
pFBuf->PutFocusOn();
return true;
}
return false;
}
bool LuaCtxt_Edit::ExecutedURL( stref strToExecute, bool fNullarg ) {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "ExecutedURL" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, strToExecute ) + lh_push( L, fNullarg ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return LUA_TOBOOLEAN( L, -1 );
}
// intf into Lua locn-list subsys
void LuaCtxt_Edit::LocnListInsertCursor() {
auto L( L_edit ); if( !L ) { return; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "LocnListInsertCursor" ) ) {
return;
}
if( lh_pcall_inout( L, 0, 0 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
}
}
bool LuaCtxt_Edit::nextmsg_setbufnm ( PCChar src ) {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "nextmsg_setbufnm_FROM_C" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, src ), 0 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return true;
}
bool LuaCtxt_Edit::RegisterAllLuaEdFxns() {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "RegisterAllLuaEdFxns" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, 0, 0 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return true;
}
bool LuaCtxt_Edit::nextmsg_newsection_ok( PCChar src ) {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "nextmsg_newsection_FROM_C" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, src ), 0 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return true;
}
bool Lua_ConfirmYes( PCChar prompt ) {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "MenuConfirm" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, prompt ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return LUA_TOBOOLEAN( L, -1 );
}
void FBOP::LuaSortLineRange( PFBUF fb, LINE y0, LINE y1, COL xLeft, COL xRight, bool fCaseIgnored, bool fOrdrAscending, bool fDupsKeep ) {
// SW_BP;
// callLuaOk( L_edit, "SortLineRange_from_C"
// , "Fiiiibbb>"
// , fb
// , y0 - 1
// , y1 - 1
// , xLeft
// , xRight
// , fCaseIgnored
// , fOrdrAscending
// , fDupsKeep
// );
}
COL FBOP::GetSoftcrIndentLua( PFBUF fb, LINE yLine ) {
constexpr COL rv_fail = -1;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "Softcr_col_from_C" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, fb, yLine ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
COL rv( lua_tointeger( L, -1 ) ); 0 && DBG( "%s =%d", __func__, rv-1 );
return rv - 1;
}
STIL bool Lua_S2S( lua_State *L, PCChar functionName, Path::str_t &inout, bool fClearInoutOnFail=true ) { enum { SD=0 };
constexpr bool rv_fail = false; SD && DBG( "%s+ %s %s", __func__, functionName, inout.c_str() );
if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, functionName ) ) {
if( fClearInoutOnFail ) {
inout.clear();
} SD && DBG( "%s- %s FAIL-findfxn %s", __func__, functionName, inout.c_str() );
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, inout ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
if( fClearInoutOnFail ) {
inout.clear();
} SD && DBG( "%s- %s FAIL-pcall %s", __func__, functionName, inout.c_str() );
return rv_fail;
}
lua_assigncppstring( L, -1, inout ); SD && DBG( "%s- %s %s", __func__, functionName, inout.c_str() );
return true;
}
// functionName may contain '.' (table dereferences)
STATIC_FXN bool LuaTblS2S( lua_State *L, PCChar functionName, Path::str_t &inout, bool fClearInoutOnFail=true ) { enum { SD=0 };
constexpr bool rv_fail = false; SD && DBG( "%s+ %s<-%s", __func__, functionName, inout.c_str() );
if( !L ) { return rv_fail; }
LuaCallCleanup( L );
if( !gotTblVal( L, functionName ) ) { SD && DBG( "%s- %s FAIL-gotTblVal %s", __func__, functionName, inout.c_str() );
goto FAIL;
}
if( !lua_isfunction( L, -1 ) ) { SD && DBG( "%s- %s FAIL-gotTblVal!=fxn %s", __func__, functionName, inout.c_str() );
goto FAIL;
}
if( lh_pcall_inout( L, lh_push( L, inout ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
FAIL:
if( fClearInoutOnFail ) {
inout.clear();
} SD && DBG( "%s- %s FAIL-pcall %s", __func__, functionName, inout.c_str() );
return rv_fail;
}
lua_assigncppstring( L, -1, inout ); SD && DBG( "%s- %s->%s", __func__, functionName, inout.c_str() );
return true;
}
bool LuaCtxt_Edit::ReadPseudoFileOk( PFBUF src ) {
constexpr bool rv_fail = false;
auto L( L_edit ); if( !L ) { return rv_fail; }
lua_settop( L, 0 ); // clear the stack
LuaCallCleanup( L ); // clear the stack on function return
if( lh_push_global_function( L, "ReadPseudoFileOk_FROM_C" ) ) {
return rv_fail;
}
if( lh_pcall_inout( L, lh_push( L, src ), 1 ) != 0 ) { // do the call
lh_handle_pcall_err( L );
return rv_fail;
}
return LUA_TOBOOLEAN( L, -1 );
}
bool LuaCtxt_Edit::ExpandEnvVarsOk ( Path::str_t &st ) { return Lua_S2S( L_edit, "StrExpandEnvVars" , st ); }
bool LuaCtxt_Edit::from_C_lookup_glock( std::string &st ) { return Lua_S2S( L_edit, "Lua_from_C_lookup_glock" , st ); }