-
Notifications
You must be signed in to change notification settings - Fork 262
/
upb.c
1872 lines (1598 loc) · 57.7 KB
/
upb.c
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
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2009 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* A Lua extension for upb. Exposes only the core library
* (sub-libraries are exposed in other extensions).
*
* 64-bit woes: Lua can only represent numbers of type lua_Number (which is
* double unless the user specifically overrides this). Doubles can represent
* the entire range of 64-bit integers, but lose precision once the integers are
* greater than 2^53.
*
* Lua 5.3 is adding support for integers, which will allow for 64-bit
* integers (which can be interpreted as signed or unsigned).
*
* LuaJIT supports 64-bit signed and unsigned boxed representations
* through its "cdata" mechanism, but this is not portable to regular Lua.
*
* Hopefully Lua 5.3 will come soon enough that we can either use Lua 5.3
* integer support or LuaJIT 64-bit cdata for users that need the entire
* domain of [u]int64 values.
*/
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "lauxlib.h"
#include "upb/bindings/lua/upb.h"
#include "upb/handlers.h"
#include "upb/pb/glue.h"
#include "upb/shim/shim.h"
/* Lua metatable types. */
#define LUPB_MSG "lupb.msg"
#define LUPB_ARRAY "lupb.array"
#define LUPB_MSGDEF "lupb.msgdef"
#define LUPB_ENUMDEF "lupb.enumdef"
#define LUPB_FIELDDEF "lupb.fielddef"
#define LUPB_SYMTAB "lupb.symtab"
/* Other table constants. */
#define LUPB_OBJCACHE "lupb.objcache"
static void lupb_msgdef_init(lua_State *L);
static size_t lupb_msgdef_sizeof();
/* Lua compatibility code *****************************************************/
/* Lua 5.1 and Lua 5.2 have slightly incompatible APIs. A little bit of
* compatibility code can help hide the difference. Not too many people still
* use Lua 5.1 but LuaJIT uses the Lua 5.1 API in some ways. */
#if LUA_VERSION_NUM == 501
/* taken from lua 5.2's source. */
void *luaL_testudata(lua_State *L, int ud, const char *tname) {
void *p = lua_touserdata(L, ud);
if (p != NULL) { /* value is a userdata? */
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
luaL_getmetatable(L, tname); /* get correct metatable */
if (!lua_rawequal(L, -1, -2)) /* not the same? */
p = NULL; /* value is a userdata with wrong metatable */
lua_pop(L, 2); /* remove both metatables */
return p;
}
}
return NULL; /* value is not a userdata with a metatable */
}
static void lupb_newlib(lua_State *L, const char *name, const luaL_Reg *funcs) {
luaL_register(L, name, funcs);
}
#define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
#elif LUA_VERSION_NUM == 502
int luaL_typerror(lua_State *L, int narg, const char *tname) {
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg);
}
static void lupb_newlib(lua_State *L, const char *name, const luaL_Reg *funcs) {
/* Lua 5.2 modules are not expected to set a global variable, so "name" is
* unused. */
UPB_UNUSED(name);
/* Can't use luaL_newlib(), because funcs is not the actual array.
* Could (micro-)optimize this a bit to count funcs for initial table size. */
lua_createtable(L, 0, 8);
luaL_setfuncs(L, funcs, 0);
}
#define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
#else
#error Only Lua 5.1 and 5.2 are supported
#endif
/* Shims for upcoming Lua 5.3 functionality. */
bool lua_isinteger(lua_State *L, int argn) {
UPB_UNUSED(L);
UPB_UNUSED(argn);
return false;
}
/* Utility functions **********************************************************/
/* We store our module table in the registry, keyed by ptr.
* For more info about the motivation/rationale, see this thread:
* http://thread.gmane.org/gmane.comp.lang.lua.general/110632 */
bool lupb_openlib(lua_State *L, void *ptr, const char *name,
const luaL_Reg *funcs) {
/* Lookup cached module table. */
lua_pushlightuserdata(L, ptr);
lua_rawget(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) {
return true;
}
lupb_newlib(L, name, funcs);
/* Save module table in cache. */
lua_pushlightuserdata(L, ptr);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
return false;
}
/* Pushes a new userdata with the given metatable and ensures that it has a
* uservalue. */
static void *newudata_with_userval(lua_State *L, size_t size,
const char *type) {
void *ret = lua_newuserdata(L, size);
/* Set metatable. */
luaL_getmetatable(L, type);
assert(!lua_isnil(L, -1)); /* Should have been created by luaopen_upb. */
lua_setmetatable(L, -2);
lua_newtable(L);
lua_setuservalue(L, -2);
return ret;
}
const char *lupb_checkname(lua_State *L, int narg) {
size_t len;
const char *name = luaL_checklstring(L, narg, &len);
if (strlen(name) != len)
luaL_error(L, "names cannot have embedded NULLs");
return name;
}
bool lupb_checkbool(lua_State *L, int narg) {
if (!lua_isboolean(L, narg)) {
luaL_error(L, "must be true or false");
}
return lua_toboolean(L, narg);
}
/* Unlike luaL_checkstring(), this does not allow implicit conversion to
* string. */
void lupb_checkstring(lua_State *L, int narg) {
if (lua_type(L, narg) != LUA_TSTRING)
luaL_error(L, "Expected string");
}
/* Unlike luaL_checkinteger, these do not implicitly convert from string or
* round an existing double value. We allow floating-point input, but only if
* the actual value is integral. */
#define INTCHECK(type, ctype) \
ctype lupb_check##type(lua_State *L, int narg) { \
double n; \
ctype i; \
if (lua_isinteger(L, narg)) { \
return lua_tointeger(L, narg); \
} \
\
/* Prevent implicit conversion from string. */ \
luaL_checktype(L, narg, LUA_TNUMBER); \
n = lua_tonumber(L, narg); \
\
i = (ctype)n; \
if ((double)i != n) { \
/* double -> ctype truncated or rounded. */ \
luaL_error(L, "number %f was not an integer or out of range for " #type, \
n); \
} \
return i; \
} \
void lupb_push##type(lua_State *L, ctype val) { \
/* TODO: push integer for Lua >= 5.3, 64-bit cdata for LuaJIT. */ \
/* This is lossy for some [u]int64 values, which isn't great, but */ \
/* crashing when we encounter these values seems worse. */ \
lua_pushnumber(L, val); \
}
INTCHECK(int64, int64_t)
INTCHECK(int32, int32_t)
INTCHECK(uint64, uint64_t)
INTCHECK(uint32, uint32_t)
double lupb_checkdouble(lua_State *L, int narg) {
/* If we were being really hard-nosed here, we'd check whether the input was
* an integer that has no precise double representation. But doubles aren't
* generally expected to be exact like integers are, and worse this could
* cause data-dependent runtime errors: one run of the program could work fine
* because the integer calculations happened to be exactly representable in
* double, while the next could crash because of subtly different input. */
luaL_checktype(L, narg, LUA_TNUMBER); /* lua_tonumber() auto-converts. */
return lua_tonumber(L, narg);
}
float lupb_checkfloat(lua_State *L, int narg) {
/* We don't worry about checking whether the input can be exactly converted to
* float -- see above. */
luaL_checktype(L, narg, LUA_TNUMBER); /* lua_tonumber() auto-converts. */
return lua_tonumber(L, narg);
}
void lupb_pushdouble(lua_State *L, double d) {
lua_pushnumber(L, d);
}
void lupb_pushfloat(lua_State *L, float d) {
lua_pushnumber(L, d);
}
static void lupb_checkval(lua_State *L, int narg, upb_fieldtype_t type) {
switch(type) {
case UPB_TYPE_INT32:
case UPB_TYPE_ENUM:
lupb_checkint32(L, narg);
break;
case UPB_TYPE_INT64:
lupb_checkint64(L, narg);
break;
case UPB_TYPE_UINT32:
lupb_checkuint32(L, narg);
break;
case UPB_TYPE_UINT64:
lupb_checkuint64(L, narg);
break;
case UPB_TYPE_DOUBLE:
lupb_checkdouble(L, narg);
break;
case UPB_TYPE_FLOAT:
lupb_checkfloat(L, narg);
break;
case UPB_TYPE_BOOL:
lupb_checkbool(L, narg);
break;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
lupb_checkstring(L, narg);
break;
case UPB_TYPE_MESSAGE:
lupb_assert(L, false);
}
}
void lupb_checkstatus(lua_State *L, upb_status *s) {
if (!upb_ok(s)) {
lua_pushstring(L, upb_status_errmsg(s));
lua_error(L);
}
}
static upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg) {
int type = luaL_checkint(L, narg);
if (!upb_fielddef_checktype(type))
luaL_argerror(L, narg, "invalid field type");
return type;
}
#define CHK(pred) do { \
upb_status status = UPB_STATUS_INIT; \
pred; \
lupb_checkstatus(L, &status); \
} while (0)
/* lupb_refcounted ************************************************************/
/* All upb objects that use upb_refcounted have a userdata that begins with a
* pointer to that object. Each type has its own metatable. Objects are cached
* in a weak table indexed by the C pointer of the object they are caching.
*
* Note that we consistently use memcpy() to read to/from the object. This
* allows the userdata to use its own struct without violating aliasing, as
* long as it begins with a pointer. */
/* Checks type; if it matches, pulls the pointer out of the wrapper. */
void *lupb_refcounted_check(lua_State *L, int narg, const char *type) {
void *ud = luaL_checkudata(L, narg, type);
void *ret;
memcpy(&ret, ud, sizeof ret);
if (!ret) luaL_error(L, "called into dead object");
return ret;
}
bool lupb_refcounted_pushwrapper(lua_State *L, const upb_refcounted *obj,
const char *type, const void *ref_donor,
size_t size) {
bool create;
void *ud;
if (obj == NULL) {
lua_pushnil(L);
return false;
}
/* Lookup our cache in the registry (we don't put our objects in the registry
* directly because we need our cache to be a weak table). */
lua_getfield(L, LUA_REGISTRYINDEX, LUPB_OBJCACHE);
assert(!lua_isnil(L, -1)); /* Should have been created by luaopen_upb. */
lua_pushlightuserdata(L, (void*)obj);
lua_rawget(L, -2);
/* Stack is now: objcache, cached value. */
create = false;
if (lua_isnil(L, -1)) {
create = true;
} else {
void *ud = lua_touserdata(L, -1);
void *ud_obj;
lupb_assert(L, ud);
memcpy(&ud_obj, ud, sizeof(void*));
/* A corner case: it is possible for the value to be GC'd
* already, in which case we should evict this entry and create
* a new one. */
if (ud_obj == NULL) {
create = true;
}
}
ud = NULL;
if (create) {
/* Remove bad cached value and push new value. */
lua_pop(L, 1);
/* All of our userdata begin with a pointer to the obj. */
ud = lua_newuserdata(L, size);
memcpy(ud, &obj, sizeof(void*));
upb_refcounted_donateref(obj, ref_donor, ud);
luaL_getmetatable(L, type);
/* Should have been created by luaopen_upb. */
lupb_assert(L, !lua_isnil(L, -1));
lua_setmetatable(L, -2);
/* Set it in the cache. */
lua_pushlightuserdata(L, (void*)obj);
lua_pushvalue(L, -2);
lua_rawset(L, -4);
} else {
/* Existing wrapper obj already has a ref. */
ud = lua_touserdata(L, -1);
upb_refcounted_checkref(obj, ud);
if (ref_donor)
upb_refcounted_unref(obj, ref_donor);
}
lua_insert(L, -2);
lua_pop(L, 1);
return create;
}
void lupb_refcounted_pushnewrapper(lua_State *L, const upb_refcounted *obj,
const char *type, const void *ref_donor) {
bool created =
lupb_refcounted_pushwrapper(L, obj, type, ref_donor, sizeof(void *));
UPB_ASSERT_VAR(created, created == true);
}
static int lupb_refcounted_gc(lua_State *L) {
void *ud = lua_touserdata(L, 1);
void *nullp;
upb_refcounted *obj;
memcpy(&obj, ud, sizeof(obj));
upb_refcounted_unref(obj, ud);
/* Zero out pointer so we can detect a call into a GC'd object. */
nullp = NULL;
memcpy(ud, &nullp, sizeof(nullp));
return 0;
}
static const struct luaL_Reg lupb_refcounted_mm[] = {
{"__gc", lupb_refcounted_gc},
{NULL, NULL}
};
/* lupb_def *******************************************************************/
static const upb_def *lupb_def_check(lua_State *L, int narg) {
upb_def *ret;
void *ud = luaL_testudata(L, narg, LUPB_MSGDEF);
if (!ud) ud = luaL_testudata(L, narg, LUPB_ENUMDEF);
if (!ud) ud = luaL_testudata(L, narg, LUPB_FIELDDEF);
if (!ud) luaL_typerror(L, narg, "upb def");
memcpy(&ret, ud, sizeof ret);
if (!ret) luaL_error(L, "called into dead object");
return ret;
}
static upb_def *lupb_def_checkmutable(lua_State *L, int narg) {
const upb_def *def = lupb_def_check(L, narg);
if (upb_def_isfrozen(def))
luaL_error(L, "not allowed on frozen value");
return (upb_def*)def;
}
bool lupb_def_pushwrapper(lua_State *L, const upb_def *def,
const void *ref_donor) {
const char *type = NULL;
size_t size = sizeof(void*);
bool created;
if (def == NULL) {
lua_pushnil(L);
return false;
}
switch (upb_def_type(def)) {
case UPB_DEF_MSG: {
type = LUPB_MSGDEF;
size = lupb_msgdef_sizeof();
break;
}
case UPB_DEF_ENUM: type = LUPB_ENUMDEF; break;
case UPB_DEF_FIELD: type = LUPB_FIELDDEF; break;
default: luaL_error(L, "unknown deftype %d", upb_def_type(def));
}
created =
lupb_refcounted_pushwrapper(L, upb_def_upcast(def), type, ref_donor, size);
if (created && upb_def_type(def) == UPB_DEF_MSG) {
lupb_msgdef_init(L);
}
return created;
}
void lupb_def_pushnewrapper(lua_State *L, const upb_def *def,
const void *ref_donor) {
bool created = lupb_def_pushwrapper(L, def, ref_donor);
UPB_ASSERT_VAR(created, created == true);
}
static int lupb_def_type(lua_State *L) {
const upb_def *def = lupb_def_check(L, 1);
lua_pushinteger(L, upb_def_type(def));
return 1;
}
static int lupb_def_freeze(lua_State *L) {
upb_def *def = lupb_def_checkmutable(L, 1);
CHK(upb_def_freeze(&def, 1, &status));
return 0;
}
static int lupb_def_isfrozen(lua_State *L) {
const upb_def *def = lupb_def_check(L, 1);
lua_pushboolean(L, upb_def_isfrozen(def));
return 1;
}
static int lupb_def_fullname(lua_State *L) {
const upb_def *def = lupb_def_check(L, 1);
lua_pushstring(L, upb_def_fullname(def));
return 1;
}
static int lupb_def_setfullname(lua_State *L) {
const char *name = lupb_checkname(L, 2);
CHK(upb_def_setfullname(lupb_def_checkmutable(L, 1), name, &status));
return 0;
}
#define LUPB_COMMON_DEF_METHODS \
{"def_type", lupb_def_type}, \
{"full_name", lupb_def_fullname}, \
{"freeze", lupb_def_freeze}, \
{"is_frozen", lupb_def_isfrozen}, \
{"set_full_name", lupb_def_setfullname}, \
/* lupb_fielddef **************************************************************/
const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg) {
return lupb_refcounted_check(L, narg, LUPB_FIELDDEF);
}
static upb_fielddef *lupb_fielddef_checkmutable(lua_State *L, int narg) {
const upb_fielddef *f = lupb_fielddef_check(L, narg);
if (upb_fielddef_isfrozen(f))
luaL_error(L, "not allowed on frozen value");
return (upb_fielddef*)f;
}
static int lupb_fielddef_new(lua_State *L) {
upb_fielddef *f = upb_fielddef_new(&f);
lupb_def_pushnewrapper(L, upb_fielddef_upcast(f), &f);
return 1;
}
/* Getters */
static int lupb_fielddef_containingtype(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lupb_def_pushwrapper(L, upb_msgdef_upcast(upb_fielddef_containingtype(f)),
NULL);
return 1;
}
static int lupb_fielddef_containingtypename(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
lua_pushstring(L, upb_fielddef_containingtypename(f));
return 1;
}
static int lupb_fielddef_default(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
switch (upb_fielddef_type(f)) {
case UPB_TYPE_INT32:
int32:
lupb_pushint32(L, upb_fielddef_defaultint32(f)); break;
case UPB_TYPE_INT64:
lupb_pushint64(L, upb_fielddef_defaultint64(f)); break;
case UPB_TYPE_UINT32:
lupb_pushuint32(L, upb_fielddef_defaultuint32(f)); break;
case UPB_TYPE_UINT64:
lupb_pushuint64(L, upb_fielddef_defaultuint64(f)); break;
case UPB_TYPE_DOUBLE:
lua_pushnumber(L, upb_fielddef_defaultdouble(f)); break;
case UPB_TYPE_FLOAT:
lua_pushnumber(L, upb_fielddef_defaultfloat(f)); break;
case UPB_TYPE_BOOL:
lua_pushboolean(L, upb_fielddef_defaultbool(f)); break;
case UPB_TYPE_ENUM:
if (upb_fielddef_enumhasdefaultstr(f)) {
goto str;
} else if (upb_fielddef_enumhasdefaultint32(f)) {
goto int32;
} else {
lua_pushnil(L);
}
break;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
str: {
size_t len;
const char *data = upb_fielddef_defaultstr(f, &len);
lua_pushlstring(L, data, len);
break;
}
case UPB_TYPE_MESSAGE:
return luaL_error(L, "Message fields do not have explicit defaults.");
}
return 1;
}
static int lupb_fielddef_getsel(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
upb_selector_t sel;
if (upb_handlers_getselector(f, luaL_checknumber(L, 2), &sel)) {
lua_pushinteger(L, sel);
return 1;
} else {
return 0;
}
}
static int lupb_fielddef_hassubdef(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushboolean(L, upb_fielddef_hassubdef(f));
return 1;
}
static int lupb_fielddef_index(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushinteger(L, upb_fielddef_index(f));
return 1;
}
static int lupb_fielddef_intfmt(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushinteger(L, upb_fielddef_intfmt(f));
return 1;
}
static int lupb_fielddef_isextension(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushboolean(L, upb_fielddef_isextension(f));
return 1;
}
static int lupb_fielddef_istagdelim(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushboolean(L, upb_fielddef_istagdelim(f));
return 1;
}
static int lupb_fielddef_label(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushinteger(L, upb_fielddef_label(f));
return 1;
}
static int lupb_fielddef_lazy(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushboolean(L, upb_fielddef_lazy(f));
return 1;
}
static int lupb_fielddef_name(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushstring(L, upb_fielddef_name(f));
return 1;
}
static int lupb_fielddef_number(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
int32_t num = upb_fielddef_number(f);
if (num)
lua_pushinteger(L, num);
else
lua_pushnil(L);
return 1;
}
static int lupb_fielddef_packed(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
lua_pushboolean(L, upb_fielddef_packed(f));
return 1;
}
static int lupb_fielddef_subdef(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
const upb_def *def;
if (!upb_fielddef_hassubdef(f))
luaL_error(L, "Tried to get subdef of non-message field");
def = upb_fielddef_subdef(f);
lupb_def_pushwrapper(L, def, NULL);
return 1;
}
static int lupb_fielddef_subdefname(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
if (!upb_fielddef_hassubdef(f))
luaL_error(L, "Tried to get subdef name of non-message field");
lua_pushstring(L, upb_fielddef_subdefname(f));
return 1;
}
static int lupb_fielddef_type(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
if (upb_fielddef_typeisset(f))
lua_pushinteger(L, upb_fielddef_type(f));
else
lua_pushnil(L);
return 1;
}
/* Setters */
static int lupb_fielddef_setcontainingtypename(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
const char *name = NULL;
if (!lua_isnil(L, 2))
name = lupb_checkname(L, 2);
CHK(upb_fielddef_setcontainingtypename(f, name, &status));
return 0;
}
static int lupb_fielddef_setdefault(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
switch (upb_fielddef_type(f)) {
case UPB_TYPE_INT32:
upb_fielddef_setdefaultint32(f, lupb_checkint32(L, 2));
break;
case UPB_TYPE_INT64:
upb_fielddef_setdefaultint64(f, lupb_checkint64(L, 2));
break;
case UPB_TYPE_UINT32:
upb_fielddef_setdefaultuint32(f, lupb_checkuint32(L, 2));
break;
case UPB_TYPE_UINT64:
upb_fielddef_setdefaultuint64(f, lupb_checkuint64(L, 2));
break;
case UPB_TYPE_DOUBLE:
upb_fielddef_setdefaultdouble(f, lupb_checkdouble(L, 2));
break;
case UPB_TYPE_FLOAT:
upb_fielddef_setdefaultfloat(f, lupb_checkfloat(L, 2));
break;
case UPB_TYPE_BOOL:
upb_fielddef_setdefaultbool(f, lupb_checkbool(L, 2));
break;
case UPB_TYPE_MESSAGE:
return luaL_error(L, "Message types cannot have defaults.");
case UPB_TYPE_ENUM:
if (lua_type(L, 2) != LUA_TSTRING) {
upb_fielddef_setdefaultint32(f, lupb_checkint32(L, 2));
break;
}
/* Else fall through and set string default. */
case UPB_TYPE_BYTES:
case UPB_TYPE_STRING: {
size_t len;
const char *str = lua_tolstring(L, 2, &len);
CHK(upb_fielddef_setdefaultstr(f, str, len, &status));
}
}
return 0;
}
static int lupb_fielddef_setisextension(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
CHK(upb_fielddef_setisextension(f, lupb_checkbool(L, 2)));
return 0;
}
static int lupb_fielddef_setlabel(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
int label = luaL_checkint(L, 2);
if (!upb_fielddef_checklabel(label))
luaL_argerror(L, 2, "invalid field label");
upb_fielddef_setlabel(f, label);
return 0;
}
static int lupb_fielddef_setlazy(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
upb_fielddef_setlazy(f, lupb_checkbool(L, 2));
return 0;
}
static int lupb_fielddef_setname(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
CHK(upb_fielddef_setname(f, lupb_checkname(L, 2), &status));
return 0;
}
static int lupb_fielddef_setnumber(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
CHK(upb_fielddef_setnumber(f, luaL_checkint(L, 2), &status));
return 0;
}
static int lupb_fielddef_setpacked(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
upb_fielddef_setpacked(f, lupb_checkbool(L, 2));
return 0;
}
static int lupb_fielddef_setsubdef(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
const upb_def *def = NULL;
if (!lua_isnil(L, 2))
def = lupb_def_check(L, 2);
CHK(upb_fielddef_setsubdef(f, def, &status));
return 0;
}
static int lupb_fielddef_setsubdefname(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
const char *name = NULL;
if (!lua_isnil(L, 2))
name = lupb_checkname(L, 2);
CHK(upb_fielddef_setsubdefname(f, name, &status));
return 0;
}
static int lupb_fielddef_settype(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
upb_fielddef_settype(f, lupb_checkfieldtype(L, 2));
return 0;
}
static int lupb_fielddef_setintfmt(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
int32_t intfmt = luaL_checknumber(L, 2);
if (!upb_fielddef_checkintfmt(intfmt))
luaL_argerror(L, 2, "invalid intfmt");
upb_fielddef_setintfmt(f, intfmt);
return 0;
}
static int lupb_fielddef_settagdelim(lua_State *L) {
upb_fielddef *f = lupb_fielddef_checkmutable(L, 1);
bool is_tag_delim = lupb_checkbool(L, 2);
CHK(upb_fielddef_settagdelim(f, is_tag_delim));
return 0;
}
static const struct luaL_Reg lupb_fielddef_m[] = {
LUPB_COMMON_DEF_METHODS
{"containing_type", lupb_fielddef_containingtype},
{"containing_type_name", lupb_fielddef_containingtypename},
{"default", lupb_fielddef_default},
{"getsel", lupb_fielddef_getsel},
{"has_subdef", lupb_fielddef_hassubdef},
{"index", lupb_fielddef_index},
{"intfmt", lupb_fielddef_intfmt},
{"is_extension", lupb_fielddef_isextension},
{"istagdelim", lupb_fielddef_istagdelim},
{"label", lupb_fielddef_label},
{"lazy", lupb_fielddef_lazy},
{"name", lupb_fielddef_name},
{"number", lupb_fielddef_number},
{"packed", lupb_fielddef_packed},
{"subdef", lupb_fielddef_subdef},
{"subdef_name", lupb_fielddef_subdefname},
{"type", lupb_fielddef_type},
{"set_containing_type_name", lupb_fielddef_setcontainingtypename},
{"set_default", lupb_fielddef_setdefault},
{"set_is_extension", lupb_fielddef_setisextension},
{"set_label", lupb_fielddef_setlabel},
{"set_lazy", lupb_fielddef_setlazy},
{"set_name", lupb_fielddef_setname},
{"set_number", lupb_fielddef_setnumber},
{"set_packed", lupb_fielddef_setpacked},
{"set_subdef", lupb_fielddef_setsubdef},
{"set_subdef_name", lupb_fielddef_setsubdefname},
{"set_type", lupb_fielddef_settype},
{"set_intfmt", lupb_fielddef_setintfmt},
{"set_tagdelim", lupb_fielddef_settagdelim},
{NULL, NULL}
};
/* lupb_msgdef ****************************************************************/
typedef struct {
const upb_msgdef *md;
/* These members are initialized lazily the first time a message is created
* for this def. */
uint16_t *field_offsets;
size_t msg_size;
size_t hasbits_size;
lua_State *L;
} lupb_msgdef;
static size_t lupb_msgdef_sizeof() {
return sizeof(lupb_msgdef);
}
const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) {
return lupb_refcounted_check(L, narg, LUPB_MSGDEF);
}
lupb_msgdef *lupb_msgdef_check2(lua_State *L, int narg) {
return luaL_checkudata(L, narg, LUPB_MSGDEF);
}
static upb_msgdef *lupb_msgdef_checkmutable(lua_State *L, int narg) {
const upb_msgdef *m = lupb_msgdef_check(L, narg);
if (upb_msgdef_isfrozen(m))
luaL_error(L, "not allowed on frozen value");
return (upb_msgdef*)m;
}
static int lupb_msgdef_new(lua_State *L) {
upb_msgdef *md = upb_msgdef_new(&md);
lupb_def_pushnewrapper(L, upb_msgdef_upcast(md), &md);
return 1;
}
/* Unlike other refcounted types we need a custom __gc so that we free our field
* offsets. */
static int lupb_msgdef_gc(lua_State *L) {
lupb_msgdef *lmd = luaL_checkudata(L, 1, LUPB_MSGDEF);
free(lmd->field_offsets);
lupb_refcounted_gc(L);
return 0;
}
static void lupb_msgdef_init(lua_State *L) {
lupb_msgdef *lmd = luaL_checkudata(L, -1, LUPB_MSGDEF);
lmd->L = L;
lmd->field_offsets = NULL;
}
static int lupb_msgdef_add(lua_State *L) {
upb_msgdef *m = lupb_msgdef_checkmutable(L, 1);
upb_fielddef *f = lupb_fielddef_checkmutable(L, 2);
CHK(upb_msgdef_addfield(m, f, NULL, &status));
return 0;
}
static int lupb_msgdef_len(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
lua_pushinteger(L, upb_msgdef_numfields(m));
return 1;
}
static int lupb_msgdef_field(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
int type = lua_type(L, 2);
const upb_fielddef *f;
if (type == LUA_TNUMBER) {
f = upb_msgdef_itof(m, lua_tointeger(L, 2));
} else if (type == LUA_TSTRING) {
f = upb_msgdef_ntofz(m, lua_tostring(L, 2));
} else {
const char *msg = lua_pushfstring(L, "number or string expected, got %s",
luaL_typename(L, 2));
return luaL_argerror(L, 2, msg);
}
lupb_def_pushwrapper(L, upb_fielddef_upcast(f), NULL);
return 1;
}
static int lupb_msgiter_next(lua_State *L) {
upb_msg_field_iter *i = lua_touserdata(L, lua_upvalueindex(1));
if (upb_msg_field_done(i)) return 0;
lupb_def_pushwrapper(L, upb_fielddef_upcast(upb_msg_iter_field(i)), NULL);
upb_msg_field_next(i);
return 1;
}
static int lupb_msgdef_fields(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
upb_msg_field_iter *i = lua_newuserdata(L, sizeof(upb_msg_field_iter));
upb_msg_field_begin(i, m);
/* Need to guarantee that the msgdef outlives the iter. */
lua_pushvalue(L, 1);
lua_pushcclosure(L, &lupb_msgiter_next, 2);
return 1;
}
static const struct luaL_Reg lupb_msgdef_mm[] = {
{"__gc", lupb_msgdef_gc},
{"__len", lupb_msgdef_len},
{NULL, NULL}
};
static const struct luaL_Reg lupb_msgdef_m[] = {
LUPB_COMMON_DEF_METHODS
{"add", lupb_msgdef_add},
{"field", lupb_msgdef_field},
{"fields", lupb_msgdef_fields},
{NULL, NULL}
};
/* lupb_enumdef ***************************************************************/
const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg) {
return lupb_refcounted_check(L, narg, LUPB_ENUMDEF);
}
static upb_enumdef *lupb_enumdef_checkmutable(lua_State *L, int narg) {
const upb_enumdef *f = lupb_enumdef_check(L, narg);
if (upb_enumdef_isfrozen(f))
luaL_error(L, "not allowed on frozen value");
return (upb_enumdef*)f;
}
static int lupb_enumdef_new(lua_State *L) {
upb_enumdef *e = upb_enumdef_new(&e);
lupb_def_pushnewrapper(L, upb_enumdef_upcast(e), &e);
return 1;
}
static int lupb_enumdef_add(lua_State *L) {
upb_enumdef *e = lupb_enumdef_checkmutable(L, 1);
const char *name = lupb_checkname(L, 2);
int32_t val = lupb_checkint32(L, 3);
CHK(upb_enumdef_addval(e, name, val, &status));
return 0;
}
static int lupb_enumdef_len(lua_State *L) {