summaryrefslogtreecommitdiff
path: root/lang_ext/lua/upb.c
blob: 8adf14d3a689e8f80c22f6a1bcee854f1cba983e (plain)
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
/*
 * 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.
 */

#include <stdlib.h>
#include <math.h>
#include <float.h>
#include "lauxlib.h"
#include "upb/def.h"
#include "upb/msg.h"
#include "upb/pb/glue.h"

static bool streql(const char *a, const char *b) { return strcmp(a, b) == 0; }

static uint8_t lupb_touint8(lua_State *L, int narg, const char *name) {
  lua_Number n = lua_tonumber(L, narg);
  if (n > UINT8_MAX || n < 0 || rint(n) != n)
    luaL_error(L, "Invalid %s", name);
  return n;
}

static uint32_t lupb_touint32(lua_State *L, int narg, const char *name) {
  lua_Number n = lua_tonumber(L, narg);
  if (n > UINT32_MAX || n < 0 || rint(n) != n)
    luaL_error(L, "Invalid %s", name);
  return n;
}

//static void lupb_msg_getorcreate(lua_State *L, upb_msg *msg, upb_msgdef *md);
static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f);

// All the def types share the same C layout, even though they are different Lua
// types with different metatables.
typedef struct {
  upb_def *def;
} lupb_def;

typedef struct {
  upb_fielddef *field;
} lupb_fielddef;

static lupb_def *lupb_def_check(lua_State *L, int narg) {
  void *ldef = luaL_checkudata(L, narg, "upb.msgdef");
  if (!ldef) ldef = luaL_checkudata(L, narg, "upb.enumdef");
  luaL_argcheck(L, ldef != NULL, narg, "upb def expected");
  return ldef;
}

static lupb_fielddef *lupb_fielddef_check(lua_State *L, int narg) {
  lupb_fielddef *f = luaL_checkudata(L, narg, "upb.fielddef");
  luaL_argcheck(L, f != NULL, narg, "upb fielddef expected");
  return f;
}

void lupb_checkstatus(lua_State *L, upb_status *s) {
  if (!upb_ok(s)) {
    upb_status_print(s, stderr);
    // Need to copy the string to the stack, so we can free it and not leak
    // it (since luaL_error() does not return).
    char buf[strlen(s->str)+1];
    strcpy(buf, s->str);
    upb_status_uninit(s);
    luaL_error(L, "%s", buf);
  }
  upb_status_uninit(s);
}


/* object cache ***************************************************************/

// We cache all the lua objects (userdata) we vend in a weak table, indexed by
// the C pointer of the object they are caching.

static void *lupb_cache_getorcreate_size(
    lua_State *L, void *cobj, const char *type, size_t size) {
  // 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).
  void **obj = NULL;
  lua_getfield(L, LUA_REGISTRYINDEX, "upb.objcache");
  assert(!lua_isnil(L, -1));  // Should have been created by luaopen_upb.
  lua_pushlightuserdata(L, cobj);
  lua_rawget(L, -2);
  // Stack: objcache, cached value.
  if (lua_isnil(L, -1)) {
    // Remove bad cached value and push new value.
    lua_pop(L, 1);
    // We take advantage of the fact that all of our objects are currently a
    // single pointer, and thus have the same layout.
    obj = lua_newuserdata(L, size);
    *obj = cobj;
    luaL_getmetatable(L, type);
    assert(!lua_isnil(L, -1));  // Should have been created by luaopen_upb.
    lua_setmetatable(L, -2);

    // Set it in the cache.
    lua_pushlightuserdata(L, cobj);
    lua_pushvalue(L, -2);
    lua_rawset(L, -4);
  }
  lua_insert(L, -2);
  lua_pop(L, 1);
  return obj;
}

// Most types are just 1 pointer and can use this helper.
static bool lupb_cache_getorcreate(lua_State *L, void *cobj, const char *type) {
  return lupb_cache_getorcreate_size(L, cobj, type, sizeof(void*)) != NULL;
}

static void lupb_cache_create(lua_State *L, void *cobj, const char *type) {
  bool created =
      lupb_cache_getorcreate_size(L, cobj, type, sizeof(void*)) != NULL;
  (void)created;  // For NDEBUG
  assert(created);
}


/* lupb_msg********************************************************************/

// Messages are userdata where we store primitive values (numbers and bools)
// right in the userdata.  We also use integer entries in the environment table
// like so:
//   {msgdef, <string, submessage, or array fields>}

static void *lupb_msg_check(lua_State *L, int narg, upb_msgdef **md) {
  void *msg = luaL_checkudata(L, narg, "upb.msg");
  luaL_argcheck(L, msg != NULL, narg, "msg expected");
  // If going all the way to the environment table for the msgdef is an
  // efficiency issue, we could put the pointer right in the userdata.
  lua_getfenv(L, narg);
  lua_rawgeti(L, -1, 1);
  // Shouldn't have to check msgdef userdata validity, environment table can't
  // be accessed from Lua.
  lupb_def *lmd = lua_touserdata(L, -1);
  *md = upb_downcast_msgdef(lmd->def);
  return msg;
}

static void lupb_msg_pushnew(lua_State *L, upb_msgdef *md) {
  void *msg = lua_newuserdata(L, upb_msgdef_size(md));
  luaL_getmetatable(L, "upb.msg");
  assert(!lua_isnil(L, -1));  // Should have been created by luaopen_upb.
  lua_setmetatable(L, -2);
  upb_msg_clear(msg, md);
  lua_getfenv(L, -1);
  lupb_cache_getorcreate(L, md, "upb.msgdef");
  lua_rawseti(L, -2, 1);
  lua_pop(L, 1);  // Pop the fenv.
}

static void lupb_pushvalue(lua_State *L, upb_value val, upb_fielddef *f) {
  switch (f->type) {
    case UPB_TYPE(INT32):
    case UPB_TYPE(SINT32):
    case UPB_TYPE(SFIXED32):
    case UPB_TYPE(ENUM):
      lua_pushnumber(L, upb_value_getint32(val)); break;
    case UPB_TYPE(INT64):
    case UPB_TYPE(SINT64):
    case UPB_TYPE(SFIXED64):
      lua_pushnumber(L, upb_value_getint64(val)); break;
    case UPB_TYPE(UINT32):
    case UPB_TYPE(FIXED32):
      lua_pushnumber(L, upb_value_getuint32(val)); break;
    case UPB_TYPE(UINT64):
    case UPB_TYPE(FIXED64):
      lua_pushnumber(L, upb_value_getuint64(val)); break;
    case UPB_TYPE(DOUBLE):
      lua_pushnumber(L, upb_value_getdouble(val)); break;
    case UPB_TYPE(FLOAT):
      lua_pushnumber(L, upb_value_getfloat(val)); break;
    case UPB_TYPE(BOOL):
      lua_pushboolean(L, upb_value_getbool(val)); break;
    default: luaL_error(L, "internal error");
  }
}

// Returns a scalar value (ie. not a submessage) as a upb_value.
static upb_value lupb_getvalue(lua_State *L, int narg, upb_fielddef *f,
                               upb_strref *ref) {
  assert(!upb_issubmsg(f));
  upb_value val;
  if (upb_fielddef_type(f) == UPB_TYPE(BOOL)) {
    if (!lua_isboolean(L, narg))
      luaL_error(L, "Must explicitly pass true or false for boolean fields");
    upb_value_setbool(&val, lua_toboolean(L, narg));
  } else if (upb_fielddef_type(f) == UPB_TYPE(STRING)) {
    size_t len;
    ref->ptr = luaL_checklstring(L, narg, &len);
    ref->len = len;
    upb_value_setstrref(&val, ref);
  } else {
    // Numeric type.
    lua_Number num = 0;
    num = luaL_checknumber(L, narg);
    switch (upb_fielddef_type(f)) {
      case UPB_TYPE(INT32):
      case UPB_TYPE(SINT32):
      case UPB_TYPE(SFIXED32):
      case UPB_TYPE(ENUM):
        if (num > INT32_MAX || num < INT32_MIN || num != rint(num))
          luaL_error(L, "Cannot convert %f to 32-bit integer", num);
        upb_value_setint32(&val, num);
        break;
      case UPB_TYPE(INT64):
      case UPB_TYPE(SINT64):
      case UPB_TYPE(SFIXED64):
        if (num > INT64_MAX || num < INT64_MIN || num != rint(num))
          luaL_error(L, "Cannot convert %f to 64-bit integer", num);
        upb_value_setint64(&val, num);
        break;
      case UPB_TYPE(UINT32):
      case UPB_TYPE(FIXED32):
        if (num > UINT32_MAX || num < 0 || num != rint(num))
          luaL_error(L, "Cannot convert %f to unsigned 32-bit integer", num);
        upb_value_setuint32(&val, num);
        break;
      case UPB_TYPE(UINT64):
      case UPB_TYPE(FIXED64):
        if (num > UINT64_MAX || num < 0 || num != rint(num))
          luaL_error(L, "Cannot convert %f to unsigned 64-bit integer", num);
        upb_value_setuint64(&val, num);
        break;
      case UPB_TYPE(DOUBLE):
        if (num > DBL_MAX || num < -DBL_MAX) {
          // This could happen if lua_Number was long double.
          luaL_error(L, "Cannot convert %f to double", num);
        }
        upb_value_setdouble(&val, num);
        break;
      case UPB_TYPE(FLOAT):
        if (num > FLT_MAX || num < -FLT_MAX)
          luaL_error(L, "Cannot convert %f to float", num);
        upb_value_setfloat(&val, num);
        break;
    }
  }
  return val;
}

static int lupb_msg_index(lua_State *L) {
  assert(lua_gettop(L) == 2);  // __index should always be called with 2 args.
  upb_msgdef *md;
  void *m = lupb_msg_check(L, 1, &md);
  const char *name = luaL_checkstring(L, 2);
  upb_fielddef *f = upb_msgdef_ntof(md, name);
  if (!f) luaL_error(L, "%s is not a field name", name);
  if (upb_isseq(f)) luaL_error(L, "NYI: access of repeated fields");
  upb_value val =
      upb_msg_has(m, f) ? upb_msg_get(m, f) : upb_fielddef_default(f);
  lupb_pushvalue(L, val, f);
  return 1;
}

static int lupb_msg_newindex(lua_State *L) {
  assert(lua_gettop(L) == 3);  // __newindex should always be called with 3 args.
  upb_msgdef *md;
  void *m = lupb_msg_check(L, 1, &md);
  const char *name = luaL_checkstring(L, 2);
  upb_fielddef *f = upb_msgdef_ntof(md, name);
  if (!f) luaL_error(L, "%s is not a field name", name);
  upb_msg_set(m, f, lupb_getvalue(L, 3, f, NULL));
  return 0;
}

#if 0
static int lupb_msg_clear(lua_State *L) {
  lupb_msg *m = lupb_msg_check(L, 1);
  upb_msg_clear(m->msg, m->msgdef);
  return 0;
}

static int lupb_msg_parse(lua_State *L) {
  lupb_msg *m = lupb_msg_check(L, 1);
  size_t len;
  const char *strbuf = luaL_checklstring(L, 2, &len);
  upb_string str = UPB_STACK_STRING_LEN(strbuf, len);
  upb_status status = UPB_STATUS_INIT;
  upb_strtomsg(&str, m->msg, m->msgdef, &status);
  lupb_checkstatus(L, &status);
  return 0;
}

static int lupb_msg_totext(lua_State *L) {
  lupb_msg *m = lupb_msg_check(L, 1);
  upb_string *str = upb_string_new();
  upb_msgtotext(str, m->msg, m->msgdef, false);
  lupb_pushstring(L, str);
  upb_string_unref(str);
  return 1;
}
#endif

static const struct luaL_Reg lupb_msg_mm[] = {
  {"__index", lupb_msg_index},
  {"__newindex", lupb_msg_newindex},
  // Our __index mm will look up methods if the index isn't a field name.
  //{"Clear", lupb_msg_clear},
  //{"Parse", lupb_msg_parse},
  //{"ToText", lupb_msg_totext},
  {NULL, NULL}
};


/* lupb_msgdef ****************************************************************/

static upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) {
  lupb_def *ldef = luaL_checkudata(L, narg, "upb.msgdef");
  luaL_argcheck(L, ldef != NULL, narg, "upb msgdef expected");
  return upb_downcast_msgdef(ldef->def);
}

static int lupb_msgdef_gc(lua_State *L) {
  lupb_def *ldef = luaL_checkudata(L, 1, "upb.msgdef");
  upb_def_unref(ldef->def);
  return 0;
}

static int lupb_msgdef_call(lua_State *L) {
  upb_msgdef *md = lupb_msgdef_check(L, 1);
  lupb_msg_pushnew(L, md);
  return 1;
}

static int lupb_msgdef_new(lua_State *L) {
  upb_msgdef *md = upb_msgdef_new();
  lupb_cache_create(L, md, "upb.msgdef");

  if (lua_gettop(L) == 0) return 1;

  // User can specify initialization values like so:
  //   upb.MessageDef{fqname="MyMessage", extstart=8000, fields={...}}
  luaL_checktype(L, 1, LUA_TTABLE);
  // Iterate over table.
  lua_pushnil(L);  // first key
  while (lua_next(L, 1)) {
    luaL_checktype(L, -2, LUA_TSTRING);
    const char *key = lua_tostring(L, -2);

    if (streql(key, "fqname")) {  // fqname="MyMessage"
      const char *fqname = lua_tostring(L, -1);
      if (!fqname || !upb_def_setfqname(UPB_UPCAST(md), fqname))
        luaL_error(L, "Invalid fqname");
    } else if (streql(key, "fields")) {  // fields={...}
      // Iterate over the list of fields.
      lua_pushnil(L);
      luaL_checktype(L, -2, LUA_TTABLE);
      while (lua_next(L, -2)) {
        lupb_fielddef *f = lupb_fielddef_check(L, -1);
        if (!upb_msgdef_addfield(md, f->field)) {
          // TODO: more specific error.
          luaL_error(L, "Could not add field.");
        }
        lua_pop(L, 1);
      }
    } else {
      // TODO: extrange=
      luaL_error(L, "Unknown initializer key '%s'", key);
    }
    lua_pop(L, 1);
  }
  return 1;
}

static int lupb_msgdef_fqname(lua_State *L) {
  upb_msgdef *m = lupb_msgdef_check(L, 1);
  lua_pushstring(L, m->base.fqname);
  return 1;
}

static int lupb_msgdef_fieldbyname(lua_State *L) {
  upb_msgdef *m = lupb_msgdef_check(L, 1);
  upb_fielddef *f = upb_msgdef_ntof(m, luaL_checkstring(L, 2));
  if (f) {
    lupb_fielddef_getorcreate(L, f);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

static int lupb_msgdef_fieldbynum(lua_State *L) {
  upb_msgdef *m = lupb_msgdef_check(L, 1);
  int num = luaL_checkint(L, 2);
  upb_fielddef *f = upb_msgdef_itof(m, num);
  if (f) {
    lupb_fielddef_getorcreate(L, f);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

static const struct luaL_Reg lupb_msgdef_mm[] = {
  {"__call", lupb_msgdef_call},
  {"__gc", lupb_msgdef_gc},
  {NULL, NULL}
};

static const struct luaL_Reg lupb_msgdef_m[] = {
  {"fieldbyname", lupb_msgdef_fieldbyname},
  {"fieldbynum", lupb_msgdef_fieldbynum},
  {"fqname", lupb_msgdef_fqname},
  {NULL, NULL}
};


/* lupb_enumdef ***************************************************************/

static upb_enumdef *lupb_enumdef_check(lua_State *L, int narg) {
  lupb_def *ldef = luaL_checkudata(L, narg, "upb.enumdef");
  return upb_downcast_enumdef(ldef->def);
}

static int lupb_enumdef_gc(lua_State *L) {
  upb_enumdef *e = lupb_enumdef_check(L, 1);
  upb_def_unref(UPB_UPCAST(e));
  return 0;
}

static int lupb_enumdef_name(lua_State *L) {
  upb_enumdef *e = lupb_enumdef_check(L, 1);
  lua_pushstring(L, e->base.fqname);
  return 1;
}

static const struct luaL_Reg lupb_enumdef_mm[] = {
  {"__gc", lupb_enumdef_gc},
  {NULL, NULL}
};

static const struct luaL_Reg lupb_enumdef_m[] = {
  {"name", lupb_enumdef_name},
  {NULL, NULL}
};


/* lupb_def *******************************************************************/

static void lupb_def_getorcreate(lua_State *L, upb_def *def, int owned) {
  bool created = false;
  switch(def->type) {
    case UPB_DEF_MSG:
      created = lupb_cache_getorcreate(L, def, "upb.msgdef");
      break;
    case UPB_DEF_ENUM:
      created = lupb_cache_getorcreate(L, def, "upb.enumdef");
      break;
    default:
      luaL_error(L, "unknown deftype %d", def->type);
  }
  if (!owned && created) {
    upb_def_ref(def);
  } else if (owned && !created) {
    upb_def_unref(def);
  }
}


/* lupb_fielddef **************************************************************/

#if 0
static const upb_accessor lupb_accessor = {
  upb_startfield_handler *appendseq;     // Repeated fields only.
  upb_startfield_handler *appendsubmsg;  // Submsg fields (repeated or no).
  upb_value_handler      *set;           // Scalar fields (repeated or no).

  // Readers.
  upb_has_reader         *has;
  upb_value_reader       *get;
  upb_seqbegin_handler   *seqbegin;
  upb_seqnext_handler    *seqnext;
  upb_seqget_handler     *seqget;
};
#endif

static upb_accessor_vtbl *lupb_accessor(upb_fielddef *f) {
  return upb_stdmsg_accessor(f);
}

static int lupb_fielddef_index(lua_State *L) {
  lupb_fielddef *f = lupb_fielddef_check(L, 1);
  const char *str = luaL_checkstring(L, 2);
  if (streql(str, "name")) {
    lua_pushstring(L, upb_fielddef_name(f->field));
  } else if (streql(str, "number")) {
    lua_pushinteger(L, upb_fielddef_number(f->field));
  } else if (streql(str, "type")) {
    lua_pushinteger(L, upb_fielddef_type(f->field));
  } else if (streql(str, "label")) {
    lua_pushinteger(L, upb_fielddef_label(f->field));
  } else if (streql(str, "subdef")) {
    lupb_def_getorcreate(L, upb_fielddef_subdef(f->field), false);
  } else if (streql(str, "msgdef")) {
    lupb_def_getorcreate(L, UPB_UPCAST(upb_fielddef_msgdef(f->field)), false);
  } else {
    luaL_error(L, "Invalid fielddef member '%s'", str);
  }
  return 1;
}

static void lupb_fielddef_set(lua_State *L, upb_fielddef *f,
                              const char *field, int narg) {
  if (!upb_fielddef_ismutable(f)) luaL_error(L, "fielddef is not mutable.");
  if (streql(field, "name")) {
    const char *name = lua_tostring(L, narg);
    if (!name || !upb_fielddef_setname(f, name))
      luaL_error(L, "Invalid name");
  } else if (streql(field, "number")) {
    if (!upb_fielddef_setnumber(f, lupb_touint32(L, narg, "number")))
      luaL_error(L, "Invalid number");
  } else if (streql(field, "type")) {
    if (!upb_fielddef_settype(f, lupb_touint8(L, narg, "type")))
      luaL_error(L, "Invalid type");
  } else if (streql(field, "label")) {
    if (!upb_fielddef_setlabel(f, lupb_touint8(L, narg, "label")))
      luaL_error(L, "Invalid label");
  } else if (streql(field, "type_name")) {
    const char *name = lua_tostring(L, narg);
    if (!name || !upb_fielddef_settypename(f, name))
      luaL_error(L, "Invalid type_name");
  } else if (streql(field, "default_value")) {
    if (!upb_fielddef_type(f))
      luaL_error(L, "Must set type before setting default_value");
    upb_strref ref;
    upb_fielddef_setdefault(f, lupb_getvalue(L, narg, f, &ref));
  } else {
    luaL_error(L, "Cannot set fielddef member '%s'", field);
  }
}

static int lupb_fielddef_new(lua_State *L) {
  upb_fielddef *f = upb_fielddef_new();
  lupb_cache_create(L, f, "upb.fielddef");

  if (lua_gettop(L) == 0) return 1;

  // User can specify initialization values like so:
  //   upb.FieldDef{label=upb.LABEL_REQUIRED, name="my_field", number=5,
  //                type=upb.TYPE_INT32, default_value=12, type_name="Foo"}
  luaL_checktype(L, 1, LUA_TTABLE);
  // Iterate over table.
  lua_pushnil(L);  // first key
  while (lua_next(L, 1)) {
    luaL_checktype(L, -2, LUA_TSTRING);
    const char *key = lua_tostring(L, -2);
    lupb_fielddef_set(L, f, key, -1);
    lua_pop(L, 1);
  }
  return 1;
}

static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f) {
  bool created = lupb_cache_getorcreate(L, f, "upb.fielddef");
  if (created) {
    // Need to obtain a ref on this field's msgdef (fielddefs themselves aren't
    // refcounted, but they're kept alive by their owning msgdef).
    upb_def_ref(UPB_UPCAST(f->msgdef));
  }
}

static int lupb_fielddef_newindex(lua_State *L) {
  lupb_fielddef *f = lupb_fielddef_check(L, 1);
  lupb_fielddef_set(L, f->field, luaL_checkstring(L, 2), 3);
  return 0;
}

static int lupb_fielddef_gc(lua_State *L) {
  lupb_fielddef *lfielddef = lupb_fielddef_check(L, 1);
  upb_fielddef_unref(lfielddef->field);
  return 0;
}

static const struct luaL_Reg lupb_fielddef_mm[] = {
  {"__gc", lupb_fielddef_gc},
  {"__index", lupb_fielddef_index},
  {"__newindex", lupb_fielddef_newindex},
  {NULL, NULL}
};



/* lupb_symtab ****************************************************************/

typedef struct {
  upb_symtab *symtab;
} lupb_symtab;

// Inherits a ref on the symtab.
// Checks that narg is a proper lupb_symtab object.  If it is, leaves its
// metatable on the stack for cache lookups/updates.
lupb_symtab *lupb_symtab_check(lua_State *L, int narg) {
  return luaL_checkudata(L, narg, "upb.symtab");
}

// narg is a lua table containing a list of defs to add.
void lupb_symtab_doadd(lua_State *L, upb_symtab *s, int narg) {
  luaL_checktype(L, narg, LUA_TTABLE);
  // Iterate over table twice.  First iteration to count entries and
  // check constraints.
  int n = 0;
  lua_pushnil(L);  // first key
  while (lua_next(L, narg)) {
    lupb_def_check(L, -1);
    ++n;
    lua_pop(L, 1);
  }

  // Second iteration to build deflist and layout.
  upb_def **defs = malloc(n * sizeof(*defs));
  n = 0;
  lua_pushnil(L);  // first key
  while (lua_next(L, 1)) {
    upb_def *def = lupb_def_check(L, -1)->def;
    defs[n++] = def;
    upb_msgdef *md = upb_dyncast_msgdef(def);
    if (md) {
      upb_msg_iter i;
      for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {
        upb_fielddef *f = upb_msg_iter_field(i);
        upb_fielddef_setaccessor(f, lupb_accessor(f));
      }
      upb_msgdef_layout(md);
    }
    lua_pop(L, 1);
  }

  upb_status status = UPB_STATUS_INIT;
  upb_symtab_add(s, defs, n, &status);
  free(defs);
  lupb_checkstatus(L, &status);
}

static int lupb_symtab_new(lua_State *L) {
  upb_symtab *s = upb_symtab_new();
  lupb_cache_create(L, s, "upb.symtab");
  if (lua_gettop(L) == 0) return 1;
  lupb_symtab_doadd(L, s, 1);
  return 1;
}

static int lupb_symtab_add(lua_State *L) {
  lupb_symtab *s = lupb_symtab_check(L, 1);
  lupb_symtab_doadd(L, s->symtab, 2);
  return 0;
}

static int lupb_symtab_gc(lua_State *L) {
  lupb_symtab *s = lupb_symtab_check(L, 1);
  upb_symtab_unref(s->symtab);
  return 0;
}

static int lupb_symtab_lookup(lua_State *L) {
  lupb_symtab *s = lupb_symtab_check(L, 1);
  for (int i = 2; i <= lua_gettop(L); i++) {
    upb_def *def = upb_symtab_lookup(s->symtab, luaL_checkstring(L, i));
    if (def) {
      lupb_def_getorcreate(L, def, true);
    } else {
      lua_pushnil(L);
    }
    lua_replace(L, i);
  }
  return lua_gettop(L) - 1;
}

static int lupb_symtab_getdefs(lua_State *L) {
  lupb_symtab *s = lupb_symtab_check(L, 1);
  upb_deftype_t type = luaL_checkint(L, 2);
  int count;
  upb_def **defs = upb_symtab_getdefs(s->symtab, &count, type);

  // Create the table in which we will return the defs.
  lua_createtable(L, count, 0);
  for (int i = 0; i < count; i++) {
    upb_def *def = defs[i];
    lua_pushnumber(L, i + 1);  // 1-based array.
    lupb_def_getorcreate(L, def, true);
    // Add it to our return table.
    lua_settable(L, -3);
  }
  free(defs);
  return 1;
}

static int lupb_symtab_parsedesc(lua_State *L) {
  lupb_symtab *s = lupb_symtab_check(L, 1);
  size_t len;
  const char *str = luaL_checklstring(L, 2, &len);
  upb_status status = UPB_STATUS_INIT;
  upb_read_descriptor(s->symtab, str, len, &status);
  lupb_checkstatus(L, &status);
  return 0;
}

static const struct luaL_Reg lupb_symtab_m[] = {
  {"add", lupb_symtab_add},
  {"getdefs", lupb_symtab_getdefs},
  {"lookup", lupb_symtab_lookup},
  {"parsedesc", lupb_symtab_parsedesc},
  //{"resolve", lupb_symtab_resolve},
  {NULL, NULL}
};

static const struct luaL_Reg lupb_symtab_mm[] = {
  {"__gc", lupb_symtab_gc},
  {NULL, NULL}
};


/* lupb toplevel **************************************************************/

static const struct luaL_Reg lupb_toplevel_m[] = {
  {"SymbolTable", lupb_symtab_new},
  {"MessageDef", lupb_msgdef_new},
  {"FieldDef", lupb_fielddef_new},
  {NULL, NULL}
};

// Register the given type with the given methods and metamethods.
static void lupb_register_type(lua_State *L, const char *name,
                               const luaL_Reg *m, const luaL_Reg *mm) {
  luaL_newmetatable(L, name);
  luaL_register(L, NULL, mm);  // Register all mm in the metatable.
  lua_createtable(L, 0, 0);
  if (m) {
    // Methods go in the mt's __index method.  This implies that you can't
    // implement __index and also set methods yourself.
    luaL_register(L, NULL, m);
    lua_setfield(L, -2, "__index");
  }
  lua_pop(L, 1);  // The mt.
}

static void lupb_setfieldi(lua_State *L, const char *field, int i) {
  lua_pushnumber(L, i);
  lua_setfield(L, -2, field);
}

int luaopen_upb(lua_State *L) {
  lupb_register_type(L, "upb.msgdef", lupb_msgdef_m, lupb_msgdef_mm);
  lupb_register_type(L, "upb.enumdef", lupb_enumdef_m, lupb_enumdef_mm);
  lupb_register_type(L, "upb.fielddef", NULL, lupb_fielddef_mm);
  lupb_register_type(L, "upb.symtab", lupb_symtab_m, lupb_symtab_mm);
  lupb_register_type(L, "upb.msg", NULL, lupb_msg_mm);

  // Create our object cache.
  lua_createtable(L, 0, 0);
  lua_createtable(L, 0, 1);  // Cache metatable.
  lua_pushstring(L, "v");    // Values are weak.
  lua_setfield(L, -2, "__mode");
  lua_setfield(L, LUA_REGISTRYINDEX, "upb.objcache");

  luaL_register(L, "upb", lupb_toplevel_m);

  // Register constants.
  lupb_setfieldi(L, "LABEL_OPTIONAL", UPB_LABEL(OPTIONAL));
  lupb_setfieldi(L, "LABEL_REQUIRED", UPB_LABEL(REQUIRED));
  lupb_setfieldi(L, "LABEL_REPEATED", UPB_LABEL(REPEATED));

  lupb_setfieldi(L, "TYPE_DOUBLE", UPB_TYPE(DOUBLE));
  lupb_setfieldi(L, "TYPE_FLOAT", UPB_TYPE(FLOAT));
  lupb_setfieldi(L, "TYPE_INT64", UPB_TYPE(INT64));
  lupb_setfieldi(L, "TYPE_UINT64", UPB_TYPE(UINT64));
  lupb_setfieldi(L, "TYPE_INT32", UPB_TYPE(INT32));
  lupb_setfieldi(L, "TYPE_FIXED64", UPB_TYPE(FIXED64));
  lupb_setfieldi(L, "TYPE_FIXED32", UPB_TYPE(FIXED32));
  lupb_setfieldi(L, "TYPE_BOOL", UPB_TYPE(BOOL));
  lupb_setfieldi(L, "TYPE_STRING", UPB_TYPE(STRING));
  lupb_setfieldi(L, "TYPE_GROUP", UPB_TYPE(GROUP));
  lupb_setfieldi(L, "TYPE_MESSAGE", UPB_TYPE(MESSAGE));
  lupb_setfieldi(L, "TYPE_BYTES", UPB_TYPE(BYTES));
  lupb_setfieldi(L, "TYPE_UINT32", UPB_TYPE(UINT32));
  lupb_setfieldi(L, "TYPE_ENUM", UPB_TYPE(ENUM));
  lupb_setfieldi(L, "TYPE_SFIXED32", UPB_TYPE(SFIXED32));
  lupb_setfieldi(L, "TYPE_SFIXED64", UPB_TYPE(SFIXED64));
  lupb_setfieldi(L, "TYPE_SINT32", UPB_TYPE(SINT32));
  lupb_setfieldi(L, "TYPE_SINT64", UPB_TYPE(SINT64));

  return 1;  // Return package table.
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback