This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from Neopallium/LuaNativeObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_lua.lua
More file actions
1986 lines (1831 loc) · 58.5 KB
/
gen_lua.lua
File metadata and controls
1986 lines (1831 loc) · 58.5 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
-- Copyright (c) 2010 by Robert G. Jakabosky <bobby@neoawareness.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
-- try generating LuaJIT FFI bindings to include into the C module.
--
require("native_objects.gen_lua_ffi")
--
-- templates
--
local generated_output_header = [[
/***********************************************************************************************
************************************************************************************************
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!! Warning this file was generated from a set of *.nobj.lua definition files !!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
************************************************************************************************
***********************************************************************************************/
]]
local obj_udata_types = [[
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#ifdef _MSC_VER
#define __WINDOWS__
#else
#if defined(_WIN32)
#define __WINDOWS__
#endif
#endif
#ifdef __WINDOWS__
/* for MinGW32 compiler need to include <stdint.h> */
#ifdef __GNUC__
#include <stdint.h>
#include <stdbool.h>
#else
/* define some standard types missing on Windows. */
#ifndef __INT32_MAX__
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
#endif
#ifndef __INT64_MAX__
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
typedef int bool;
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#endif
/* wrap strerror_s(). */
#ifdef __GNUC__
#ifndef strerror_r
#define strerror_r(errno, buf, buflen) do { \
strncpy((buf), strerror(errno), (buflen)-1); \
(buf)[(buflen)-1] = '\0'; \
} while(0)
#endif
#else
#ifndef strerror_r
#define strerror_r(errno, buf, buflen) strerror_s((buf), (buflen), (errno))
#endif
#endif
#define FUNC_UNUSED
#define LUA_NOBJ_API __declspec(dllexport)
#else
#define LUA_NOBJ_API LUALIB_API
#include <stdint.h>
#include <stdbool.h>
#define FUNC_UNUSED __attribute__((unused))
#endif
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define assert_obj_type(type, obj) \
assert(__builtin_types_compatible_p(typeof(obj), type *))
#else
#define assert_obj_type(type, obj)
#endif
void *${nobj_realloc}(void *ptr, size_t osize, size_t nsize);
void *nobj_realloc(void *ptr, size_t osize, size_t nsize) {
(void)osize;
if(0 == nsize) {
free(ptr);
return NULL;
}
return realloc(ptr, nsize);
}
#define obj_type_free(type, obj) do { \
assert_obj_type(type, obj); \
${nobj_realloc}((obj), sizeof(type), 0); \
} while(0)
#define obj_type_new(type, obj) do { \
assert_obj_type(type, obj); \
(obj) = ${nobj_realloc}(NULL, 0, sizeof(type)); \
} while(0)
typedef struct obj_type obj_type;
typedef void (*base_caster_t)(void **obj);
typedef void (*dyn_caster_t)(void **obj, obj_type **type);
#define OBJ_TYPE_FLAG_WEAK_REF (1<<0)
#define OBJ_TYPE_SIMPLE (1<<1)
struct obj_type {
dyn_caster_t dcaster; /**< caster to support casting to sub-objects. */
int32_t id; /**< type's id. */
uint32_t flags; /**< type's flags (weak refs) */
const char *name; /**< type's object name. */
};
typedef struct obj_base {
int32_t id;
base_caster_t bcaster;
} obj_base;
typedef enum obj_const_type {
CONST_UNKOWN = 0,
CONST_BOOLEAN = 1,
CONST_NUMBER = 2,
CONST_STRING = 3
} obj_const_type;
typedef struct obj_const {
const char *name; /**< constant's name. */
const char *str;
double num;
obj_const_type type;
} obj_const;
typedef enum obj_field_type {
TYPE_UNKOWN = 0,
TYPE_UINT8 = 1,
TYPE_UINT16 = 2,
TYPE_UINT32 = 3,
TYPE_UINT64 = 4,
TYPE_INT8 = 5,
TYPE_INT16 = 6,
TYPE_INT32 = 7,
TYPE_INT64 = 8,
TYPE_DOUBLE = 9,
TYPE_FLOAT = 10,
TYPE_STRING = 11
} obj_field_type;
typedef struct obj_field {
const char *name; /**< field's name. */
uint32_t offset; /**< offset to field's data. */
obj_field_type type; /**< field's data type. */
uint32_t flags; /**< is_writable:1bit */
} obj_field;
typedef enum {
REG_OBJECT,
REG_PACKAGE,
REG_META,
} module_reg_type;
typedef struct reg_sub_module {
obj_type *type;
module_reg_type req_type;
const luaL_reg *pub_funcs;
const luaL_reg *methods;
const luaL_reg *metas;
const obj_base *bases;
const obj_field *fields;
const obj_const *constants;
int bidirectional_consts;
} reg_sub_module;
#define OBJ_UDATA_FLAG_OWN (1<<0)
#define OBJ_UDATA_FLAG_LOOKUP (1<<1)
#define OBJ_UDATA_LAST_FLAG (OBJ_UDATA_FLAG_LOOKUP)
typedef struct obj_udata {
void *obj;
uint32_t flags; /**< lua_own:1bit */
} obj_udata;
/* use static pointer as key to weak userdata table. */
static char obj_udata_weak_ref_key[] = "obj_udata_weak_ref_key";
/* use static pointer as key to module's private table. */
static char obj_udata_private_key[] = "obj_udata_private_key";
]]
local objHelperFunc = [[
#ifndef REG_PACKAGE_IS_CONSTRUCTOR
#define REG_PACKAGE_IS_CONSTRUCTOR 1
#endif
#ifndef REG_MODULES_AS_GLOBALS
#define REG_MODULES_AS_GLOBALS 0
#endif
#ifndef REG_OBJECTS_AS_GLOBALS
#define REG_OBJECTS_AS_GLOBALS 0
#endif
#ifndef OBJ_DATA_HIDDEN_METATABLE
#define OBJ_DATA_HIDDEN_METATABLE 1
#endif
static FUNC_UNUSED obj_udata *obj_udata_toobj(lua_State *L, int _index) {
obj_udata *ud;
size_t len;
/* make sure it's a userdata value. */
ud = (obj_udata *)lua_touserdata(L, _index);
if(ud == NULL) {
luaL_typerror(L, _index, "userdata"); /* is not a userdata value. */
}
/* verify userdata size. */
len = lua_objlen(L, _index);
if(len != sizeof(obj_udata)) {
/* This shouldn't be possible */
luaL_error(L, "invalid userdata size: size=%d, expected=%d", len, sizeof(obj_udata));
}
return ud;
}
static FUNC_UNUSED int obj_udata_is_compatible(lua_State *L, obj_udata *ud, void **obj, base_caster_t *caster, obj_type *type) {
obj_base *base;
obj_type *ud_type;
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
if(lua_rawequal(L, -1, -2)) {
*obj = ud->obj;
/* same type no casting needed. */
return 1;
} else {
/* Different types see if we can cast to the required type. */
lua_rawgeti(L, -2, type->id);
base = lua_touserdata(L, -1);
lua_pop(L, 1); /* pop obj_base or nil */
if(base != NULL) {
*caster = base->bcaster;
/* get the obj_type for this userdata. */
lua_pushliteral(L, ".type");
lua_rawget(L, -3); /* type's metatable. */
ud_type = lua_touserdata(L, -1);
lua_pop(L, 1); /* pop obj_type or nil */
if(base == NULL) {
luaL_error(L, "bad userdata, missing type info.");
return 0;
}
/* check if userdata is a simple object. */
if(ud_type->flags & OBJ_TYPE_SIMPLE) {
*obj = ud;
} else {
*obj = ud->obj;
}
return 1;
}
}
return 0;
}
static FUNC_UNUSED obj_udata *obj_udata_luacheck_internal(lua_State *L, int _index, void **obj, obj_type *type, int not_delete) {
obj_udata *ud;
base_caster_t caster = NULL;
/* make sure it's a userdata value. */
ud = (obj_udata *)lua_touserdata(L, _index);
if(ud != NULL) {
/* check object type by comparing metatables. */
if(lua_getmetatable(L, _index)) {
if(obj_udata_is_compatible(L, ud, obj, &(caster), type)) {
lua_pop(L, 2); /* pop both metatables. */
/* apply caster function if needed. */
if(caster != NULL && *obj != NULL) {
caster(obj);
}
/* check object pointer. */
if(*obj == NULL) {
if(not_delete) {
luaL_error(L, "null %s", type->name); /* object was garbage collected? */
}
return NULL;
}
return ud;
}
}
} else {
/* handle cdata. */
/* get private table. */
lua_pushlightuserdata(L, obj_udata_private_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* private table. */
/* get cdata type check function from private table. */
lua_pushlightuserdata(L, type);
lua_rawget(L, -2);
/* pass cdata value to type checking function. */
lua_pushvalue(L, _index);
lua_call(L, 1, 1);
if(!lua_isnil(L, -1)) {
/* valid type get pointer from cdata. */
lua_pop(L, 2);
*obj = *(void **)lua_topointer(L, _index);
return ud;
}
lua_pop(L, 2);
}
if(not_delete) {
luaL_typerror(L, _index, type->name); /* is not a userdata value. */
}
return NULL;
}
static FUNC_UNUSED void *obj_udata_luacheck(lua_State *L, int _index, obj_type *type) {
void *obj = NULL;
obj_udata_luacheck_internal(L, _index, &(obj), type, 1);
return obj;
}
static FUNC_UNUSED void *obj_udata_luaoptional(lua_State *L, int _index, obj_type *type) {
void *obj = NULL;
if(lua_gettop(L) < _index) {
return obj;
}
obj_udata_luacheck_internal(L, _index, &(obj), type, 1);
return obj;
}
static FUNC_UNUSED void *obj_udata_luadelete(lua_State *L, int _index, obj_type *type, int *flags) {
void *obj;
obj_udata *ud = obj_udata_luacheck_internal(L, _index, &(obj), type, 0);
if(ud == NULL) return NULL;
*flags = ud->flags;
/* null userdata. */
ud->obj = NULL;
ud->flags = 0;
return obj;
}
static FUNC_UNUSED void obj_udata_luapush(lua_State *L, void *obj, obj_type *type, int flags) {
obj_udata *ud;
/* convert NULL's into Lua nil's. */
if(obj == NULL) {
lua_pushnil(L);
return;
}
#if LUAJIT_FFI
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
if(nobj_ffi_support_enabled_hint && lua_isfunction(L, -1)) {
/* call special FFI "void *" to FFI object convertion function. */
lua_pushlightuserdata(L, obj);
lua_pushinteger(L, flags);
lua_call(L, 2, 1);
return;
}
#endif
/* check for type caster. */
if(type->dcaster) {
(type->dcaster)(&obj, &type);
}
/* create new userdata. */
ud = (obj_udata *)lua_newuserdata(L, sizeof(obj_udata));
ud->obj = obj;
ud->flags = flags;
/* get obj_type metatable. */
#if LUAJIT_FFI
lua_insert(L, -2); /* move userdata below metatable. */
#else
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
#endif
lua_setmetatable(L, -2);
}
static FUNC_UNUSED void *obj_udata_luadelete_weak(lua_State *L, int _index, obj_type *type, int *flags) {
void *obj;
obj_udata *ud = obj_udata_luacheck_internal(L, _index, &(obj), type, 0);
if(ud == NULL) return NULL;
*flags = ud->flags;
/* null userdata. */
ud->obj = NULL;
ud->flags = 0;
/* get objects weak table. */
lua_pushlightuserdata(L, obj_udata_weak_ref_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* weak ref table. */
/* remove object from weak table. */
lua_pushlightuserdata(L, obj);
lua_pushnil(L);
lua_rawset(L, -3);
return obj;
}
static FUNC_UNUSED void obj_udata_luapush_weak(lua_State *L, void *obj, obj_type *type, int flags) {
obj_udata *ud;
/* convert NULL's into Lua nil's. */
if(obj == NULL) {
lua_pushnil(L);
return;
}
/* check for type caster. */
if(type->dcaster) {
(type->dcaster)(&obj, &type);
}
/* get objects weak table. */
lua_pushlightuserdata(L, obj_udata_weak_ref_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* weak ref table. */
/* lookup userdata instance from pointer. */
lua_pushlightuserdata(L, obj);
lua_rawget(L, -2);
if(!lua_isnil(L, -1)) {
lua_remove(L, -2); /* remove objects table. */
return;
}
lua_pop(L, 1); /* pop nil. */
#if LUAJIT_FFI
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
if(nobj_ffi_support_enabled_hint && lua_isfunction(L, -1)) {
lua_remove(L, -2);
/* call special FFI "void *" to FFI object convertion function. */
lua_pushlightuserdata(L, obj);
lua_pushinteger(L, flags);
lua_call(L, 2, 1);
return;
}
#endif
/* create new userdata. */
ud = (obj_udata *)lua_newuserdata(L, sizeof(obj_udata));
/* init. obj_udata. */
ud->obj = obj;
ud->flags = flags;
/* get obj_type metatable. */
#if LUAJIT_FFI
lua_insert(L, -2); /* move userdata below metatable. */
#else
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
#endif
lua_setmetatable(L, -2);
/* add weak reference to object. */
lua_pushlightuserdata(L, obj); /* push object pointer as the 'key' */
lua_pushvalue(L, -2); /* push object's udata */
lua_rawset(L, -4); /* add weak reference to object. */
lua_remove(L, -2); /* remove objects table. */
}
/* default object equal method. */
static FUNC_UNUSED int obj_udata_default_equal(lua_State *L) {
obj_udata *ud1 = obj_udata_toobj(L, 1);
obj_udata *ud2 = obj_udata_toobj(L, 2);
lua_pushboolean(L, (ud1->obj == ud2->obj));
return 1;
}
/* default object tostring method. */
static FUNC_UNUSED int obj_udata_default_tostring(lua_State *L) {
obj_udata *ud = obj_udata_toobj(L, 1);
/* get object's metatable. */
lua_getmetatable(L, 1);
lua_remove(L, 1); /* remove userdata. */
/* get the object's name from the metatable */
lua_getfield(L, 1, ".name");
lua_remove(L, 1); /* remove metatable */
/* push object's pointer */
lua_pushfstring(L, ": %p, flags=%d", ud->obj, ud->flags);
lua_concat(L, 2);
return 1;
}
/*
* Simple userdata objects.
*/
static FUNC_UNUSED void *obj_simple_udata_toobj(lua_State *L, int _index) {
void *ud;
/* make sure it's a userdata value. */
ud = lua_touserdata(L, _index);
if(ud == NULL) {
luaL_typerror(L, _index, "userdata"); /* is not a userdata value. */
}
return ud;
}
static FUNC_UNUSED void * obj_simple_udata_luacheck(lua_State *L, int _index, obj_type *type) {
void *ud;
/* make sure it's a userdata value. */
ud = lua_touserdata(L, _index);
if(ud != NULL) {
/* check object type by comparing metatables. */
if(lua_getmetatable(L, _index)) {
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
if(lua_rawequal(L, -1, -2)) {
lua_pop(L, 2); /* pop both metatables. */
return ud;
}
}
} else {
/* handle cdata. */
/* get private table. */
lua_pushlightuserdata(L, obj_udata_private_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* private table. */
/* get cdata type check function from private table. */
lua_pushlightuserdata(L, type);
lua_rawget(L, -2);
/* pass cdata value to type checking function. */
lua_pushvalue(L, _index);
lua_call(L, 1, 1);
if(!lua_isnil(L, -1)) {
/* valid type get pointer from cdata. */
lua_pop(L, 2);
return (void *)lua_topointer(L, _index);
}
lua_pop(L, 2);
}
luaL_typerror(L, _index, type->name); /* is not a userdata value. */
return NULL;
}
static FUNC_UNUSED void * obj_simple_udata_luaoptional(lua_State *L, int _index, obj_type *type) {
if(lua_gettop(L) < _index) {
return NULL;
}
return obj_simple_udata_luacheck(L, _index, type);
}
static FUNC_UNUSED void * obj_simple_udata_luadelete(lua_State *L, int _index, obj_type *type) {
void *obj;
obj = obj_simple_udata_luacheck(L, _index, type);
/* clear the metatable to invalidate userdata. */
lua_pushnil(L);
lua_setmetatable(L, _index);
return obj;
}
static FUNC_UNUSED void *obj_simple_udata_luapush(lua_State *L, void *obj, int size, obj_type *type)
{
void *ud;
#if LUAJIT_FFI
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
if(nobj_ffi_support_enabled_hint && lua_isfunction(L, -1)) {
/* call special FFI "void *" to FFI object convertion function. */
lua_pushlightuserdata(L, obj);
lua_call(L, 1, 1);
return obj;
}
#endif
/* create new userdata. */
ud = lua_newuserdata(L, size);
memcpy(ud, obj, size);
/* get obj_type metatable. */
#if LUAJIT_FFI
lua_insert(L, -2); /* move userdata below metatable. */
#else
lua_pushlightuserdata(L, type);
lua_rawget(L, LUA_REGISTRYINDEX); /* type's metatable. */
#endif
lua_setmetatable(L, -2);
return ud;
}
/* default simple object equal method. */
static FUNC_UNUSED int obj_simple_udata_default_equal(lua_State *L) {
void *ud1 = obj_simple_udata_toobj(L, 1);
size_t len1 = lua_objlen(L, 1);
void *ud2 = obj_simple_udata_toobj(L, 2);
size_t len2 = lua_objlen(L, 2);
if(len1 == len2) {
lua_pushboolean(L, (memcmp(ud1, ud2, len1) == 0));
} else {
lua_pushboolean(L, 0);
}
return 1;
}
/* default simple object tostring method. */
static FUNC_UNUSED int obj_simple_udata_default_tostring(lua_State *L) {
void *ud = obj_simple_udata_toobj(L, 1);
/* get object's metatable. */
lua_getmetatable(L, 1);
lua_remove(L, 1); /* remove userdata. */
/* get the object's name from the metatable */
lua_getfield(L, 1, ".name");
lua_remove(L, 1); /* remove metatable */
/* push object's pointer */
lua_pushfstring(L, ": %p", ud);
lua_concat(L, 2);
return 1;
}
static int obj_constructor_call_wrapper(lua_State *L) {
/* replace '__call' table with constructor function. */
lua_pushvalue(L, lua_upvalueindex(1));
lua_replace(L, 1);
/* call constructor function with all parameters after the '__call' table. */
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
/* return all results from constructor. */
return lua_gettop(L);
}
static void obj_type_register_constants(lua_State *L, const obj_const *constants, int tab_idx,
int bidirectional) {
/* register constants. */
while(constants->name != NULL) {
lua_pushstring(L, constants->name);
switch(constants->type) {
case CONST_BOOLEAN:
lua_pushboolean(L, constants->num != 0.0);
break;
case CONST_NUMBER:
lua_pushnumber(L, constants->num);
break;
case CONST_STRING:
lua_pushstring(L, constants->str);
break;
default:
lua_pushnil(L);
break;
}
/* map values back to keys. */
if(bidirectional) {
/* check if value already exists. */
lua_pushvalue(L, -1);
lua_rawget(L, tab_idx - 3);
if(lua_isnil(L, -1)) {
lua_pop(L, 1);
/* add value->key mapping. */
lua_pushvalue(L, -1);
lua_pushvalue(L, -3);
lua_rawset(L, tab_idx - 4);
} else {
/* value already exists. */
lua_pop(L, 1);
}
}
lua_rawset(L, tab_idx - 2);
constants++;
}
}
static void obj_type_register_package(lua_State *L, const reg_sub_module *type_reg) {
const luaL_reg *reg_list = type_reg->pub_funcs;
/* create public functions table. */
if(reg_list != NULL && reg_list[0].name != NULL) {
/* register functions */
luaL_register(L, NULL, reg_list);
}
obj_type_register_constants(L, type_reg->constants, -1, type_reg->bidirectional_consts);
lua_pop(L, 1); /* drop package table */
}
static void obj_type_register_meta(lua_State *L, const reg_sub_module *type_reg) {
const luaL_reg *reg_list;
/* create public functions table. */
reg_list = type_reg->pub_funcs;
if(reg_list != NULL && reg_list[0].name != NULL) {
/* register functions */
luaL_register(L, NULL, reg_list);
}
obj_type_register_constants(L, type_reg->constants, -1, type_reg->bidirectional_consts);
/* register methods. */
luaL_register(L, NULL, type_reg->methods);
/* create metatable table. */
lua_newtable(L);
luaL_register(L, NULL, type_reg->metas); /* fill metatable */
/* setmetatable on meta-object. */
lua_setmetatable(L, -2);
lua_pop(L, 1); /* drop meta-object */
}
static void obj_type_register(lua_State *L, const reg_sub_module *type_reg, int priv_table) {
const luaL_reg *reg_list;
obj_type *type = type_reg->type;
const obj_base *base = type_reg->bases;
if(type_reg->req_type == REG_PACKAGE) {
obj_type_register_package(L, type_reg);
return;
}
if(type_reg->req_type == REG_META) {
obj_type_register_meta(L, type_reg);
return;
}
/* create public functions table. */
reg_list = type_reg->pub_funcs;
if(reg_list != NULL && reg_list[0].name != NULL) {
/* register "constructors" as to object's public API */
luaL_register(L, NULL, reg_list); /* fill public API table. */
/* make public API table callable as the default constructor. */
lua_newtable(L); /* create metatable */
lua_pushliteral(L, "__call");
lua_pushcfunction(L, reg_list[0].func); /* push first constructor function. */
lua_pushcclosure(L, obj_constructor_call_wrapper, 1); /* make __call wrapper. */
lua_rawset(L, -3); /* metatable.__call = <default constructor> */
#if OBJ_DATA_HIDDEN_METATABLE
lua_pushliteral(L, "__metatable");
lua_pushboolean(L, 0);
lua_rawset(L, -3); /* metatable.__metatable = false */
#endif
/* setmetatable on public API table. */
lua_setmetatable(L, -2);
lua_pop(L, 1); /* pop public API table, don't need it any more. */
/* create methods table. */
lua_newtable(L);
} else {
/* register all methods as public functions. */
#if OBJ_DATA_HIDDEN_METATABLE
lua_pop(L, 1); /* pop public API table, don't need it any more. */
/* create methods table. */
lua_newtable(L);
#endif
}
luaL_register(L, NULL, type_reg->methods); /* fill methods table. */
luaL_newmetatable(L, type->name); /* create metatable */
lua_pushliteral(L, ".name");
lua_pushstring(L, type->name);
lua_rawset(L, -3); /* metatable['.name'] = "<object_name>" */
lua_pushliteral(L, ".type");
lua_pushlightuserdata(L, type);
lua_rawset(L, -3); /* metatable['.type'] = lightuserdata -> obj_type */
lua_pushlightuserdata(L, type);
lua_pushvalue(L, -2); /* dup metatable. */
lua_rawset(L, LUA_REGISTRYINDEX); /* REGISTRY[type] = metatable */
/* add metatable to 'priv_table' */
lua_pushstring(L, type->name);
lua_pushvalue(L, -2); /* dup metatable. */
lua_rawset(L, priv_table); /* priv_table["<object_name>"] = metatable */
luaL_register(L, NULL, type_reg->metas); /* fill metatable */
/* add obj_bases to metatable. */
while(base->id >= 0) {
lua_pushlightuserdata(L, (void *)base);
lua_rawseti(L, -2, base->id);
base++;
}
obj_type_register_constants(L, type_reg->constants, -2, type_reg->bidirectional_consts);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3); /* dup methods table */
lua_rawset(L, -3); /* metatable.__index = methods */
#if OBJ_DATA_HIDDEN_METATABLE
lua_pushliteral(L, "__metatable");
lua_pushboolean(L, 0);
lua_rawset(L, -3); /* hide metatable:
metatable.__metatable = false */
#endif
lua_pop(L, 2); /* drop metatable & methods */
}
static FUNC_UNUSED int lua_checktype_ref(lua_State *L, int _index, int _type) {
luaL_checktype(L,_index,_type);
lua_pushvalue(L,_index);
return luaL_ref(L, LUA_REGISTRYINDEX);
}
]]
-- templates for typed *_check/*_delete/*_push macros.
local obj_type_check_delete_push = {
['simple'] = [[
#define obj_type_${object_name}_check(L, _index) \
*((${object_name} *)obj_simple_udata_luacheck(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_optional(L, _index) \
*((${object_name} *)obj_simple_udata_luaoptional(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_delete(L, _index) \
*((${object_name} *)obj_simple_udata_luadelete(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_push(L, obj) \
obj_simple_udata_luapush(L, &(obj), sizeof(${object_name}), &(obj_type_${object_name}))
]],
['simple ptr'] = [[
#define obj_type_${object_name}_check(L, _index) \
*((${object_name} **)obj_simple_udata_luacheck(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_optional(L, _index) \
*((${object_name} **)obj_simple_udata_luaoptional(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_delete(L, _index) \
*((${object_name} **)obj_simple_udata_luadelete(L, _index, &(obj_type_${object_name})))
#define obj_type_${object_name}_push(L, obj) \
obj_simple_udata_luapush(L, &(obj), sizeof(${object_name}), &(obj_type_${object_name}))
]],
['embed'] = [[
#define obj_type_${object_name}_check(L, _index) \
(${object_name} *)obj_simple_udata_luacheck(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_optional(L, _index) \
(${object_name} *)obj_simple_udata_luaoptional(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_delete(L, _index) \
(${object_name} *)obj_simple_udata_luadelete(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_push(L, obj) \
obj_simple_udata_luapush(L, obj, sizeof(${object_name}), &(obj_type_${object_name}))
]],
['object id'] = [[
#define obj_type_${object_name}_check(L, _index) \
(${object_name})(uintptr_t)obj_udata_luacheck(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_optional(L, _index) \
(${object_name})(uintptr_t)obj_udata_luaoptional(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_delete(L, _index, flags) \
(${object_name})(uintptr_t)obj_udata_luadelete(L, _index, &(obj_type_${object_name}), flags)
#define obj_type_${object_name}_push(L, obj, flags) \
obj_udata_luapush(L, (void *)((uintptr_t)obj), &(obj_type_${object_name}), flags)
]],
['generic'] = [[
#define obj_type_${object_name}_check(L, _index) \
obj_udata_luacheck(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_optional(L, _index) \
obj_udata_luaoptional(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_delete(L, _index, flags) \
obj_udata_luadelete(L, _index, &(obj_type_${object_name}), flags)
#define obj_type_${object_name}_push(L, obj, flags) \
obj_udata_luapush(L, obj, &(obj_type_${object_name}), flags)
]],
['generic_weak'] = [[
#define obj_type_${object_name}_check(L, _index) \
obj_udata_luacheck(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_optional(L, _index) \
obj_udata_luaoptional(L, _index, &(obj_type_${object_name}))
#define obj_type_${object_name}_delete(L, _index, flags) \
obj_udata_luadelete_weak(L, _index, &(obj_type_${object_name}), flags)
#define obj_type_${object_name}_push(L, obj, flags) \
obj_udata_luapush_weak(L, (void *)obj, &(obj_type_${object_name}), flags)
]],
}
-- prefix for default equal/tostring methods.
local obj_type_equal_tostring = {
['simple'] = 'obj_simple_udata_default',
['simple ptr'] = 'obj_simple_udata_default',
['embed'] = 'obj_simple_udata_default',
['object id'] = 'obj_udata_default',
['generic'] = 'obj_udata_default',
['generic_weak'] = 'obj_udata_default',
}
local create_object_instance_cache = [[
static void create_object_instance_cache(lua_State *L) {
lua_pushlightuserdata(L, obj_udata_weak_ref_key); /* key for weak table. */
lua_rawget(L, LUA_REGISTRYINDEX); /* check if weak table exists already. */
if(!lua_isnil(L, -1)) {
lua_pop(L, 1); /* pop weak table. */
return;
}
lua_pop(L, 1); /* pop nil. */
/* create weak table for object instance references. */
lua_pushlightuserdata(L, obj_udata_weak_ref_key); /* key for weak table. */
lua_newtable(L); /* weak table. */
lua_newtable(L); /* metatable for weak table. */
lua_pushliteral(L, "__mode");
lua_pushliteral(L, "v");
lua_rawset(L, -3); /* metatable.__mode = 'v' weak values. */
lua_setmetatable(L, -2); /* add metatable to weak table. */
lua_rawset(L, LUA_REGISTRYINDEX); /* create reference to weak table. */
}
]]
local luaopen_main = [[
LUA_NOBJ_API int luaopen_${module_c_name}(lua_State *L) {
const reg_sub_module *reg = reg_sub_modules;
const luaL_Reg *submodules = submodule_libs;
int priv_table = -1;
/* private table to hold reference to object metatables. */
lua_newtable(L);
priv_table = lua_gettop(L);
lua_pushlightuserdata(L, obj_udata_private_key);
lua_pushvalue(L, priv_table);
lua_rawset(L, LUA_REGISTRYINDEX); /* store private table in registry. */
/* create object cache. */
create_object_instance_cache(L);
/* module table. */
#if REG_MODULES_AS_GLOBALS
luaL_register(L, "${module_name}", ${module_c_name}_function);
#else
lua_newtable(L);
luaL_register(L, NULL, ${module_c_name}_function);
#endif
/* register module constants. */
obj_type_register_constants(L, ${module_c_name}_constants, -1, 0);
for(; submodules->func != NULL ; submodules++) {
lua_pushcfunction(L, submodules->func);
lua_pushstring(L, submodules->name);
lua_call(L, 1, 0);
}
/* register objects */
for(; reg->type != NULL ; reg++) {
lua_newtable(L); /* create public API table for object. */
lua_pushvalue(L, -1); /* dup. object's public API table. */
lua_setfield(L, -3, reg->type->name); /* module["<object_name>"] = <object public API> */
#if REG_OBJECTS_AS_GLOBALS
lua_pushvalue(L, -1); /* dup value. */
lua_setglobal(L, reg->type->name); /* global: <object_name> = <object public API> */
#endif
obj_type_register(L, reg, priv_table);
}
${module_init_src}
return 1;
}
]]
local luaopen_submodule = [[
LUA_NOBJ_API int luaopen_${module_c_name}_${object_name}(lua_State *L) {
const reg_sub_module *reg = &(submodule_${object_name}_reg);
const luaL_Reg null_reg_list = { NULL, NULL };
int priv_table = -1;
/* private table to hold reference to object metatables. */
lua_newtable(L);
priv_table = lua_gettop(L);
/* create object cache. */
create_object_instance_cache(L);
/* submodule table. */
#if REG_MODULES_AS_GLOBALS
luaL_register(L, "${module_name}.${object_name}", &(null_reg_list));
#else
lua_newtable(L);
luaL_register(L, NULL, &(null_reg_list));
#endif
/* register submodule. */
lua_pushvalue(L, -1); /* dup. submodule's table. */
obj_type_register(L, reg, priv_table);
#if REG_OBJECTS_AS_GLOBALS
lua_pushvalue(L, -1); /* dup value. */