forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.c
More file actions
1071 lines (957 loc) · 29.8 KB
/
Copy pathtypes.c
File metadata and controls
1071 lines (957 loc) · 29.8 KB
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
/*
This file is part of darktable,
copyright (c) 2012 Jeremy Rosen
darktable 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.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/darktable.h"
#include "common/file_location.h"
#include "control/control.h"
#include "lua/types.h"
#include "lua/call.h"
#include <string.h>
#include <stdarg.h>
#include <math.h>
/*************/
/* TYPES */
/*************/
static void to_char_array(lua_State *L, luaA_Type type_id, void *c_out, int index, int size)
{
size_t tgt_size;
const char *value = luaL_checklstring(L, index, &tgt_size);
if(tgt_size > size)
{
luaL_error(L, "string '%s' too long (max is %d)", value, size);
}
strncpy(c_out, value, size);
}
static int push_char_array(lua_State *L, luaA_Type type_id, const void *c_in)
{
lua_pushstring(L, c_in);
return 1;
}
static void to_char20(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 20);
}
static void to_char32(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 32);
}
static void to_char52(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 52);
}
static void to_char64(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 64);
}
static void to_char128(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 128);
}
static void to_char256(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 256);
}
static void to_char512(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 512);
}
static void to_char1024(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, 1024);
}
static void to_charfilename_length(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, DT_MAX_FILENAME_LEN);
}
static void to_charpath_length(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
to_char_array(L, type_id, c_out, index, PATH_MAX);
}
static int push_protected_double(lua_State *L, luaA_Type type_id, const void *c_in)
{
double value = *(double *)c_in;
if(!isnormal(value))
{
lua_pushnil(L);
}
else
{
lua_pushnumber(L, value);
}
return 1;
}
static int push_progress_double(lua_State *L, luaA_Type type_id, const void *c_in)
{
double value = *(double *)c_in;
if(value < 0.0) value = 0.0;
if(value > 1.0) value = 1.0;
lua_pushnumber(L, value);
return 1;
}
static void to_progress_double(lua_State *L, luaA_Type type_id, void *c_out, int index)
{
luaA_to_double(L, type_id, c_out, index);
if(*(double *)c_out < 0.0) *(double *)c_out = 0.0;
if(*(double *)c_out > 1.0) *(double *)c_out = 1.0;
}
/************************************/
/* METATBLE CALLBACSK FOR AUTOTYPES */
/************************************/
static int autotype_inext(lua_State *L)
{
luaL_getmetafield(L, 1, "__len");
lua_pushvalue(L, -3);
lua_call(L, 1, 1);
int length = lua_tonumber(L, -1);
lua_pop(L, 1);
int key = 0;
if(length == 0)
{
lua_pop(L, 1);
lua_pushnil(L);
return 1;
}
else if(lua_isnil(L, -1))
{
key = 1;
lua_pop(L, 1);
lua_pushnumber(L, key);
lua_pushnumber(L, key);
lua_gettable(L, -3);
return 2;
}
else if(luaL_checknumber(L, -1) < length)
{
key = lua_tonumber(L, -1) + 1;
lua_pop(L, 1);
lua_pushnumber(L, key);
lua_pushnumber(L, key);
lua_gettable(L, -3);
return 2;
}
else
{
// we reached the end of ipairs
lua_pop(L, 1);
lua_pushnil(L);
return 1;
}
}
static int autotype_next(lua_State *L)
{
/* CONVENTION
each block has the following stack on entry and exit
1 : the object
2 : the last entry ("next" convention)
each block should return according to "next" convention on success
each block should leave the key untouched if it doesn't know about it
each block should replace the key with "nil" if the key was the last entry it can handle
*/
// printf("aaaaa %s %d\n",__FUNCTION__,__LINE__);
if(luaL_getmetafield(L, 1, "__len"))
{
lua_pushvalue(L, -3);
lua_call(L, 1, 1);
int length = lua_tonumber(L, -1);
lua_pop(L, 1);
int key = 0;
if(lua_isnil(L, -1) && length > 0)
{
key = 1;
}
else if(lua_isnumber(L, -1) && lua_tonumber(L, -1) < length)
{
key = lua_tonumber(L, -1) + 1;
}
else if(lua_isnumber(L, -1) && lua_tonumber(L, -1) == length)
{
// numbers are done, move-on to something else
lua_pop(L, 1);
lua_pushnil(L);
}
if(key)
{
lua_pop(L, 1);
lua_pushnumber(L, key);
lua_pushnumber(L, key);
lua_gettable(L, -3);
return 2;
}
}
// stack at this point : {object,key}
int key_in_get = false;
luaL_getmetafield(L, 1, "__get");
if(lua_isnil(L, -2))
{
key_in_get = true;
}
else
{
lua_pushvalue(L, -2);
lua_gettable(L, -2);
if(lua_isnil(L, -1))
{
key_in_get = false;
lua_pop(L, 2);
}
else
{
key_in_get = true;
lua_pop(L, 1);
}
}
if(key_in_get)
{
lua_pushvalue(L, -2);
int nil_found = false;
while(!nil_found)
{
if(lua_next(L, -2))
{
// we have a next
lua_pop(L, 1);
lua_pushvalue(L, -4);
lua_pushvalue(L, -2);
// hacky way to avoid a subfunction just to do a pcall around getting a value in a table
int result = dt_lua_dostring(L, "args ={...}; return args[1][args[2]]", 2, 1);
if(result == LUA_OK)
{
return 2;
}
else
{
lua_pop(L, 1);
// and loop to find the next possible value
}
}
else
{
// key was the last for __get
lua_pop(L, 2);
lua_pushnil(L);
nil_found = true;
}
}
}
// stack at this point : {object,key}
if(lua_isnil(L, -1))
{
return 1;
}
else
{
return luaL_error(L, "invalid key to 'next' : %s", lua_tostring(L, 2));
}
}
static int autotype_ipairs(lua_State *L)
{
luaL_getmetafield(L, 1, "__inext");
lua_pushvalue(L, -2);
lua_pushinteger(L, 0); // index set to 0 for reset
return 3;
}
static int autotype_pairs(lua_State *L)
{
luaL_getmetafield(L, 1, "__next");
lua_pushvalue(L, -2);
lua_pushnil(L); // index set to null for reset
return 3;
}
static int autotype_index(lua_State *L)
{
luaL_getmetafield(L, 1, "__get");
int pos_get = lua_gettop(L); // points at __get
lua_pushvalue(L, -2);
lua_gettable(L, -2);
if(lua_isnil(L, -1) && lua_isnumber(L, -3))
{
if(luaL_getmetafield(L, 1, "__number_index"))
{
lua_remove(L, -2);
}
}
if(lua_isnil(L, -1))
{
lua_pop(L, 1);
luaL_getmetafield(L, -3, "__luaA_TypeName");
return luaL_error(L, "field \"%s\" not found for type %s\n", lua_tostring(L, -3), lua_tostring(L, -1));
}
lua_pushvalue(L, -4);
lua_pushvalue(L, -4);
lua_call(L, 2, LUA_MULTRET);
lua_remove(L, pos_get);
return (lua_gettop(L) - pos_get + 1);
}
static int autotype_newindex(lua_State *L)
{
luaL_getmetafield(L, 1, "__set");
int pos_set = lua_gettop(L); // points at __get
lua_pushvalue(L, -3);
lua_gettable(L, -2);
if(lua_isnil(L, -1) && lua_isnumber(L, -4))
{
if(luaL_getmetafield(L, -5, "__number_newindex"))
{
lua_remove(L, -2);
}
}
if(lua_isnil(L, -1))
{
lua_pop(L, 1);
luaL_getmetafield(L, -4, "__luaA_TypeName");
return luaL_error(L, "field \"%s\" can't be written for type %s\n", lua_tostring(L, -4),
lua_tostring(L, -1));
}
lua_pushvalue(L, -5);
lua_pushvalue(L, -5);
lua_pushvalue(L, -5);
lua_call(L, 3, LUA_MULTRET);
lua_remove(L, pos_set);
return (lua_gettop(L) - pos_set + 1);
}
static int autotype_tostring(lua_State *L)
{
if(luaL_getmetafield(L,1,"__real_tostring")) {
lua_insert(L,1);
lua_call(L,1,1);
return 1;
} else {
char tmp[256];
luaL_getmetafield(L,1,"__luaA_TypeName");
snprintf(tmp,sizeof(tmp),"%s (%p)",lua_tostring(L,-1),lua_topointer(L,1));
lua_pushstring(L,tmp);
return 1;
}
}
/*************************/
/* PUSH AND TO FUNCTIONS */
/*************************/
static int full_pushfunc(lua_State *L, luaA_Type type_id, const void *cin)
{
size_t type_size = luaA_typesize(L, type_id);
void *udata = lua_newuserdata(L, type_size);
lua_newtable(L);
lua_setuservalue(L, -2);
if(cin)
{
memcpy(udata, cin, type_size);
}
else
{
memset(udata, 0, type_size);
}
luaL_setmetatable(L, luaA_typename(L, type_id));
if(luaL_getmetafield(L, -1, "__init"))
{
lua_pushvalue(L, -2); // the new alocated object
lua_pushlightuserdata(L, (void *)cin); // forced to cast..
lua_call(L, 2, 0);
}
return 1;
}
static void full_tofunc(lua_State *L, luaA_Type type_id, void *cout, int index)
{
if(!dt_lua_isa_type(L,index,type_id)) {
char error_msg[256];
snprintf(error_msg,sizeof(error_msg),"%s expected",luaA_typename(L,type_id));
luaL_argerror(L,index,error_msg);
}
void* udata = lua_touserdata(L,index);
memcpy(cout, udata, luaA_typesize(L, type_id));
}
static int int_pushfunc(lua_State *L, luaA_Type type_id, const void *cin)
{
luaL_getmetatable(L, luaA_typename(L, type_id));
luaL_getsubtable(L, -1, "__values");
int singleton = *(int *)cin;
lua_pushnumber(L, singleton);
lua_gettable(L, -2);
if(lua_isnoneornil(L, -1))
{
lua_pop(L, 1);
int *udata = lua_newuserdata(L, sizeof(int));
*udata = singleton;
luaL_setmetatable(L, luaA_typename(L, type_id));
lua_pushinteger(L, singleton);
// warning : no uservalue
lua_pushvalue(L, -2);
lua_settable(L, -4);
if(luaL_getmetafield(L, -1, "__init"))
{
lua_pushvalue(L, -2); // the new alocated object
lua_pushlightuserdata(L, (void *)cin); // forced to cast..
lua_call(L, 2, 0);
}
}
lua_remove(L, -2); //__values
lua_remove(L, -2); // metatable
return 1;
}
static void int_tofunc(lua_State *L, luaA_Type type_id, void *cout, int index)
{
if(!dt_lua_isa_type(L,index,type_id)) {
char error_msg[256];
snprintf(error_msg,sizeof(error_msg),"%s expected",luaA_typename(L,type_id));
luaL_argerror(L,index,error_msg);
}
void* udata = lua_touserdata(L,index);
memcpy(cout, udata, sizeof(int));
}
static int gpointer_pushfunc(lua_State *L, luaA_Type type_id, const void *cin)
{
gpointer singleton = *(gpointer *)cin;
if(!singleton) {
lua_pushnil(L);
return 1;
}
luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values");
lua_pushlightuserdata(L, singleton);
lua_gettable(L, -2);
if(lua_isnoneornil(L, -1))
{
lua_pop(L, 1);
gpointer *udata = lua_newuserdata(L, sizeof(gpointer));
lua_newtable(L);
lua_setuservalue(L, -2);
*udata = singleton;
luaL_setmetatable(L, luaA_typename(L, type_id));
lua_pushlightuserdata(L, singleton);
lua_pushvalue(L, -2);
lua_settable(L, -4);
if(luaL_getmetafield(L, -1, "__init"))
{
lua_pushvalue(L, -2); // the new alocated object
lua_pushlightuserdata(L, (void *)cin); // forced to cast..
lua_call(L, 2, 0);
}
}
lua_remove(L, -2); //dt_lua_gpointer_values
return 1;
}
static void gpointer_tofunc(lua_State *L, luaA_Type type_id, void *cout, int index)
{
if(!dt_lua_isa_type(L,index,type_id)) {
char error_msg[256];
snprintf(error_msg,sizeof(error_msg),"%s expected",luaA_typename(L,type_id));
luaL_argerror(L,index,error_msg);
}
gpointer* udata = lua_touserdata(L,index);
memcpy(cout, udata, sizeof(gpointer));
if(!*udata) {
luaL_error(L,"Attempting to access of type %s after its destruction\n",luaA_typename(L,type_id));
}
}
static int unknown_pushfunc(lua_State *L, luaA_Type type_id, const void *cin)
{
gpointer singleton = *(gpointer *)cin;
if(!singleton) {
lua_pushnil(L);
return 1;
}
luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values");
lua_pushlightuserdata(L, singleton);
lua_gettable(L, -2);
if(lua_isnoneornil(L, -1))
{
return luaL_error(L,"Attempting to push a pointer of unknown type on the stack\n");
}
lua_remove(L, -2); //dt_lua_gpointer_values
return 1;
}
/*****************/
/* TYPE CREATION */
/*****************/
void dt_lua_type_register_type(lua_State *L, luaA_Type type_id, const char *name)
{
luaL_getmetatable(L, luaA_typename(L, type_id)); // gets the metatable since it's supposed to exist
luaL_getsubtable(L, -1, "__get");
lua_pushvalue(L, -3);
lua_setfield(L, -2, name);
lua_pop(L, 1);
luaL_getsubtable(L, -1, "__set");
lua_pushvalue(L, -3);
lua_setfield(L, -2, name);
lua_pop(L, 3);
}
void dt_lua_type_register_const_type(lua_State *L, luaA_Type type_id, const char *name)
{
luaL_getmetatable(L, luaA_typename(L, type_id)); // gets the metatable since it's supposed to exist
luaL_getsubtable(L, -1, "__get");
lua_pushvalue(L, -3);
lua_setfield(L, -2, name);
lua_pop(L, 3);
}
void dt_lua_type_register_number_const_type(lua_State *L, luaA_Type type_id)
{
luaL_getmetatable(L, luaA_typename(L, type_id)); // gets the metatable since it's supposed to exist
lua_pushvalue(L, -2);
lua_setfield(L, -2, "__number_index");
if(!lua_isnil(L, -3))
{
lua_pushvalue(L, -3);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, autotype_ipairs);
lua_setfield(L, -2, "__ipairs");
lua_pushcfunction(L, autotype_inext);
lua_setfield(L, -2, "__inext");
}
lua_pop(L, 3);
}
void dt_lua_type_register_number_type(lua_State *L, luaA_Type type_id)
{
luaL_getmetatable(L, luaA_typename(L, type_id)); // gets the metatable since it's supposed to exist
lua_pushvalue(L, -2);
lua_setfield(L, -2, "__number_index");
lua_pushvalue(L, -2);
lua_setfield(L, -2, "__number_newindex");
if(!lua_isnil(L, -3))
{
lua_pushvalue(L, -3);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, autotype_ipairs);
lua_setfield(L, -2, "__ipairs");
lua_pushcfunction(L, autotype_inext);
lua_setfield(L, -2, "__inext");
}
lua_pop(L, 3);
}
int dt_lua_type_member_luaautoc(lua_State *L)
{
const char *member_name = luaL_checkstring(L, 2);
luaL_getmetafield(L, 1, "__luaA_Type");
luaA_Type my_type = luaL_checkint(L, -1);
lua_pop(L, 1);
void *object = lua_touserdata(L, 1);
if(lua_gettop(L) != 3)
{
luaA_struct_push_member_name_type(L, my_type, member_name, object);
return 1;
}
else
{
luaA_struct_to_member_name_type(L, my_type, member_name, object, 3);
return 0;
}
}
void dt_lua_type_register_struct_type(lua_State *L, luaA_Type type_id)
{
const char *member_name = luaA_struct_next_member_name_type(L, type_id, LUAA_INVALID_MEMBER_NAME);
while(member_name != LUAA_INVALID_MEMBER_NAME)
{
lua_pushvalue(L, -1);
luaA_Type member_type = luaA_struct_typeof_member_name_type(L, type_id, member_name);
if(luaA_conversion_to_registered_type(L, member_type) || luaA_struct_registered_type(L, member_type)
|| luaA_enum_registered_type(L, member_type))
{
dt_lua_type_register_type(L, type_id, member_name);
}
else
{
dt_lua_type_register_const_type(L, type_id, member_name);
}
member_name = luaA_struct_next_member_name_type(L, type_id, member_name);
}
lua_pop(L, 1);
}
int dt_lua_type_member_common(lua_State *L)
{
if(lua_gettop(L) != 2)
{
luaL_getmetafield(L, 1, "__luaA_TypeName");
return luaL_error(L, "field \"%s\" can't be written for type %s\n", lua_tostring(L, 2),
lua_tostring(L, -1));
}
lua_pushvalue(L, lua_upvalueindex(1));
return 1;
}
void dt_lua_type_register_parent_type(lua_State *L, luaA_Type type_id, luaA_Type parent_type_id)
{
luaL_getmetatable(L, luaA_typename(L, type_id)); // gets the metatable since it's supposed to exist
luaL_getmetatable(L, luaA_typename(L, parent_type_id)); // gets the metatable since it's supposed to exist
lua_pushvalue(L, -1);
lua_setfield(L, -3, "__luaA_ParentMetatable");
lua_getfield(L, -2, "__get");
lua_getfield(L, -2, "__get");
lua_pushnil(L); /* first key */
while(lua_next(L, -2) != 0)
{
lua_getfield(L,-4,lua_tostring(L,-2));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_setfield(L, -4, lua_tostring(L,-2));
} else {
lua_pop(L,2);
}
}
lua_pop(L, 2);
lua_getfield(L, -2, "__set");
lua_getfield(L, -2, "__set");
lua_pushnil(L); /* first key */
while(lua_next(L, -2) != 0)
{
lua_getfield(L,-4,lua_tostring(L,-2));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_setfield(L, -4, lua_tostring(L,-2));
} else {
lua_pop(L,2);
}
}
lua_pop(L, 2);
lua_pushnil(L); /* first key */
while(lua_next(L, -2) != 0)
{
lua_getfield(L,-4,lua_tostring(L,-2));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_setfield(L, -4, lua_tostring(L,-2));
} else {
lua_pop(L,2);
}
}
lua_pop(L, 2);
}
static void init_metatable(lua_State *L, luaA_Type type_id)
{
luaL_newmetatable(L, luaA_typename(L, type_id));
lua_pushstring(L, luaA_typename(L, type_id));
lua_setfield(L, -2, "__luaA_TypeName");
lua_pushnumber(L, type_id);
lua_setfield(L, -2, "__luaA_Type");
lua_pushvalue(L, -1);
lua_pushcclosure(L, autotype_next, 1);
lua_setfield(L, -2, "__next");
lua_pushvalue(L, -1);
lua_pushcclosure(L, autotype_pairs, 1);
lua_setfield(L, -2, "__pairs");
lua_pushvalue(L, -1);
lua_pushcclosure(L, autotype_index, 1);
lua_setfield(L, -2, "__index");
lua_pushvalue(L, -1);
lua_pushcclosure(L, autotype_newindex, 1);
lua_setfield(L, -2, "__newindex");
lua_newtable(L);
lua_setfield(L, -2, "__get");
lua_newtable(L);
lua_setfield(L, -2, "__set");
lua_pushvalue(L, -1);
lua_pushcclosure(L, autotype_tostring, 1);
lua_setfield(L, -2, "__tostring");
// leave metatable on top of stack
}
luaA_Type dt_lua_init_type_type(lua_State *L, luaA_Type type_id)
{
init_metatable(L, type_id);
lua_pop(L, 1);
luaA_conversion_type(L, type_id, full_pushfunc, full_tofunc);
return type_id;
}
luaA_Type dt_lua_init_singleton(lua_State *L, const char *unique_name, void *data)
{
char tmp_name[1024];
snprintf(tmp_name, sizeof(tmp_name), "dt_lua_singleton_%s", unique_name);
luaA_Type type_id = luaA_type_add(L, tmp_name, sizeof(void *));
init_metatable(L, type_id);
void **udata = lua_newuserdata(L, sizeof(void *));
lua_newtable(L);
lua_setuservalue(L, -2);
if(!data)
{
memset(udata, 0, sizeof(void *));
}
else
{
*udata = data;
luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values");
lua_pushlightuserdata(L, data);
lua_pushvalue(L,-3);
lua_settable(L,-3);
lua_pop(L,1);
}
lua_pushvalue(L, -1);
luaL_setmetatable(L, tmp_name);
lua_setfield(L, -3, "__singleton");
if(luaL_getmetafield(L, -1, "__init"))
{
lua_pushvalue(L, -2); // the new alocated object
lua_pushlightuserdata(L, (void *)data); // forced to cast..
lua_call(L, 2, 0);
}
lua_remove(L, -2);
return type_id;
}
static int wrapped_index(lua_State *L)
{
luaL_getmetafield(L, 1, "__pusher");
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
lua_pushvalue(L, 2);
lua_gettable(L, -2);
lua_remove(L, 1);
lua_remove(L, 1);
return 1;
}
static int wrapped_pairs(lua_State *L)
{
luaL_getmetafield(L, 1, "__pusher");
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
luaL_getmetafield(L, -1, "__pairs");
lua_pushvalue(L, -2);
lua_call(L, 1, 3);
return 3;
}
static int wrapped_newindex(lua_State *L)
{
return luaL_error(L, "TBSL");
}
static int wrapped_ipairs(lua_State *L)
{
return luaL_error(L, "TBSL");
}
static int wrapped_tostring(lua_State *L)
{
return luaL_error(L, "TBSL");
}
luaA_Type dt_lua_init_wrapped_singleton(lua_State *L, lua_CFunction pusher, lua_CFunction getter,
const char *unique_name, void *data)
{
luaA_Type result = dt_lua_init_singleton(L, unique_name, data);
lua_getmetatable(L, -1);
lua_pushcfunction(L, wrapped_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, wrapped_newindex);
lua_setfield(L, -2, "__newindex");
lua_pushcfunction(L, wrapped_pairs);
lua_setfield(L, -2, "__pairs");
lua_pushcfunction(L, wrapped_ipairs);
lua_setfield(L, -2, "__ipairs");
lua_pushcfunction(L, wrapped_tostring);
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, pusher);
lua_setfield(L, -2, "__pusher");
lua_pushcfunction(L, getter);
lua_setfield(L, -2, "__getter");
lua_pop(L, 1);
return result;
}
luaA_Type dt_lua_init_int_type_type(lua_State *L, luaA_Type type_id)
{
init_metatable(L, type_id);
lua_newtable(L);
// metatable of __values
lua_newtable(L);
lua_pushstring(L, "kv");
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "__values");
lua_pop(L, 1);
luaA_conversion_type(L, type_id, int_pushfunc, int_tofunc);
return type_id;
}
static int gpointer_wrapper(lua_State*L)
{
gpointer *udata = (gpointer*)lua_touserdata(L,1);
if(!*udata) {
luaL_getmetafield(L,1,"__luaA_TypeName");
luaL_error(L,"Attempting to access an invalid object of type %s",lua_tostring(L,-1));
}
lua_CFunction callback = lua_tocfunction(L,lua_upvalueindex(1));
return callback(L);
}
luaA_Type dt_lua_init_gpointer_type_type(lua_State *L, luaA_Type type_id)
{
init_metatable(L, type_id);
lua_getfield(L,-1,"__next");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__next");
lua_getfield(L,-1,"__index");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__index");
lua_getfield(L,-1,"__newindex");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__newindex");
lua_getfield(L,-1,"__pairs");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__pairs");
lua_getfield(L,-1,"__ipairs");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__ipairs");
lua_getfield(L,-1,"__tostring");
lua_pushcclosure(L, gpointer_wrapper,1);
lua_setfield(L, -2, "__tostring");
lua_pop(L, 1);
luaA_conversion_type(L, type_id, gpointer_pushfunc, gpointer_tofunc);
return type_id;
}
void dt_lua_type_gpointer_alias_type(lua_State*L,luaA_Type type_id,void* pointer,void* alias)
{
luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values");
lua_pushlightuserdata(L, pointer);
lua_gettable(L, -2);
if(lua_isnoneornil(L, -1))
{
luaL_error(L,"Adding an alias to an unknown object for type %s",luaA_typename(L,type_id));
}
lua_pushlightuserdata(L,alias);
lua_insert(L,-2);
lua_settable(L,-3);
lua_pop(L,1);
}
void dt_lua_type_gpointer_drop(lua_State*L, void* pointer)
{
luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values");
lua_pushlightuserdata(L, pointer);
lua_gettable(L,-2);
gpointer *udata = (gpointer*)lua_touserdata(L,-1);
if(lua_isnil(L,-1)) {
lua_pop(L,2);
return; // this table is weak, the object has been gc
}
*udata = NULL;
lua_pop(L,1);
lua_pushlightuserdata(L, pointer);
lua_pushnil(L);
lua_settable(L,-3);
lua_pop(L,1);
}
gboolean dt_lua_isa_type(lua_State *L, int index, luaA_Type type_id)
{
if(!luaL_getmetafield(L, index, "__luaA_Type")) return false;
int obj_type = luaL_checkinteger(L, -1);
lua_pop(L, 1);
return dt_lua_typeisa_type(L, obj_type, type_id);
}
gboolean dt_lua_typeisa_type(lua_State *L, luaA_Type obj_type, luaA_Type type_id)
{
if(obj_type == type_id) return true;
luaL_getmetatable(L, luaA_typename(L, obj_type));
lua_getfield(L, -1, "__luaA_ParentMetatable");
if(lua_isnil(L, -1))
{
lua_pop(L, 2);
return false;
}
lua_getfield(L, -1, "__luaA_Type");
int parent_type = luaL_checkinteger(L, -1);
lua_pop(L, 3);
return dt_lua_typeisa_type(L, parent_type, type_id);
}
void dt_lua_type_setmetafield_type(lua_State*L,luaA_Type type_id,const char* method_name)
{
// These metafields should never be overridden by user code
if(
!strcmp(method_name,"__index") ||
!strcmp(method_name,"__newindex") ||
!strcmp(method_name,"__number_index") ||
!strcmp(method_name,"__number_newindex") ||
!strcmp(method_name,"__pairs") ||
!strcmp(method_name,"__ipairs") ||
!strcmp(method_name,"__next") ||
!strcmp(method_name,"__inext") ||
!strcmp(method_name,"__get") ||
!strcmp(method_name,"__set") ||
!strcmp(method_name,"__len") ||
!strcmp(method_name,"__luaA_Type") ||
!strcmp(method_name,"__luaA_TypeName") ||
!strcmp(method_name,"__luaA_ParentMetatable") ||
!strcmp(method_name,"__init") ||
!strcmp(method_name,"__values") ||
!strcmp(method_name,"__singleton") ||
!strcmp(method_name,"__pusher") ||
!strcmp(method_name,"__getter") ||
!strcmp(method_name,"__mode") ||
0) {
luaL_error(L,"non-core lua code is not allowed to change meta-field %s\n",method_name);
} else if(!strcmp(method_name,"__tostring")) {
luaL_getmetatable(L, luaA_typename(L, type_id));
lua_pushvalue(L,-2);
lua_setfield(L, -2, "__real_tostring");
lua_pop(L, 2); // pop the metatable and the value
return;
// whitelist for specific types
} else if(
// if you add a type here, make sure it handles inheritence of metamethods itself
// typically, set the metamethod not for the parent type but just after inheritence
( !strcmp(method_name,"__associated_object")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"dt_imageio_module_format_t"))) ||
( !strcmp(method_name,"__associated_object")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"dt_imageio_module_storage_t"))) ||
( !strcmp(method_name,"__gc")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"dt_style_t"))) ||
( !strcmp(method_name,"__gc")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"dt_style_item_t"))) ||
( !strcmp(method_name,"__gc")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"lua_widget"))) ||
( !strcmp(method_name,"__call")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"lua_widget"))) ||
( !strcmp(method_name,"__gtk_signals")&& dt_lua_typeisa_type(L,type_id,luaA_type_find(L,"lua_widget"))) ||
0) {