summaryrefslogtreecommitdiff
path: root/lang_ext/lua/upb.c
blob: 460ac86a4614c13142160e5ae36e5d64607c4bd1 (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
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2009 Joshua Haberman.  See LICENSE for details.
 *
 * A Lua extension for upb.
 */

#include <stdlib.h>
#include <math.h>
#include <float.h>
#include "lauxlib.h"
#include "upb_def.h"
#include "upb_glue.h"
#include "upb_msg.h"

static void lupb_msg_getorcreate(lua_State *L, upb_msg *msg, upb_msgdef *md);

// 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;

void lupb_pushstring(lua_State *L, upb_string *str) {
  lua_pushlstring(L, upb_string_getrobuf(str), upb_string_len(str));
}

void lupb_checkstatus(lua_State *L, upb_status *s) {
  if (!upb_ok(s)) {
    upb_printerr(s);
    // Need to copy the string to the stack, so we can free it and not leak
    // it (since luaL_error() does not return).
    size_t len = upb_string_len(s->str);
    char buf[len+1];
    memcpy(buf, upb_string_getrobuf(s->str), len);
    buf[len] = '\0';
    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;
}


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

// We prefer field access syntax (foo.bar, foo.bar = 5) over method syntax
// (foo:bar(), foo:set_bar(5)) to make messages behave more like regular tables.
// However, there are methods also, like foo:CopyFrom(other_foo) or foo:Clear().

typedef struct {
  upb_msg *msg;
  upb_msgdef *msgdef;
} lupb_msg;

static lupb_msg *lupb_msg_check(lua_State *L, int narg) {
  return luaL_checkudata(L, narg, "upb.msg");
}

static void lupb_msg_pushnew(lua_State *L, upb_msgdef *md) {
  upb_msg *msg = upb_msg_new(md);
  lupb_msg *m = lupb_cache_getorcreate_size(L, msg, "upb.msg", sizeof(lupb_msg));
  assert(m);
  m->msgdef = md;
  // We need to ensure that the msgdef outlives the msg.  This performs an
  // atomic ref, if this turns out to be too expensive there are other
  // possible approaches, like creating a separate metatable for every
  // msgdef that references the msgdef.
  upb_msgdef_ref(md);
}

// Caller does *not* pass a ref.
static void lupb_msg_getorcreate(lua_State *L, upb_msg *msg, upb_msgdef *md) {
  lupb_msg *m = lupb_cache_getorcreate_size(L, msg, "upb.msg", sizeof(lupb_msg));
  if (m) {
    // New Lua object, we need to ref the message.
    m->msg = upb_msg_getref(msg);
    m->msgdef = md;
    // See comment above.
    upb_msgdef_ref(md);
  }
}

static int lupb_msg_gc(lua_State *L) {
  lupb_msg *m = lupb_msg_check(L, 1);
  upb_msg_unref(m->msg, m->msgdef);
  upb_msgdef_unref(m->msgdef);
  return 0;
}

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;
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES): {
      upb_string *str = upb_value_getstr(val);
      assert(str);
      lua_pushlstring(L, upb_string_getrobuf(str), upb_string_len(str)); break;
    }
    case UPB_TYPE(MESSAGE):
    case UPB_TYPE(GROUP): {
      upb_msg *msg = upb_value_getmsg(val);
      assert(msg);
      lupb_msg_getorcreate(L, msg, upb_downcast_msgdef(f->def));
    }
  }
}

static upb_value lupb_getvalue(lua_State *L, int narg, upb_fielddef *f) {
  upb_value val;
  lua_Number num;
  if (!upb_issubmsg(f) && !upb_isstring(f) && f->type != UPB_TYPE(BOOL)) {
    num = luaL_checknumber(L, narg);
    if (f->type != UPB_TYPE(DOUBLE) && f->type != UPB_TYPE(FLOAT) &&
        num != rint(num)) {
      luaL_error(L, "Cannot assign non-integer number %f to integer field", num);
    }
  }
  switch (f->type) {
    case UPB_TYPE(INT32):
    case UPB_TYPE(SINT32):
    case UPB_TYPE(SFIXED32):
    case UPB_TYPE(ENUM):
      if (num > INT32_MAX || num < INT32_MIN)
        luaL_error(L, "Number %f is out-of-range for 32-bit integer field.", 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)
        luaL_error(L, "Number %f is out-of-range for 64-bit integer field.", num);
      upb_value_setint64(&val, num);
      break;
    case UPB_TYPE(UINT32):
    case UPB_TYPE(FIXED32):
      if (num > UINT32_MAX || num < 0)
        luaL_error(L, "Number %f is out-of-range for unsigned 32-bit integer field.", num);
      upb_value_setuint32(&val, num);
      break;
    case UPB_TYPE(UINT64):
    case UPB_TYPE(FIXED64):
      if (num > UINT64_MAX || num < 0)
        luaL_error(L, "Number %f is out-of-range for unsigned 64-bit integer field.", 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, "Number %f is out-of-range for double field.", num);
      }
      upb_value_setdouble(&val, num);
      break;
    case UPB_TYPE(FLOAT):
      if (num > FLT_MAX || num < -FLT_MAX)
        luaL_error(L, "Number %f is out-of-range for float field.", num);
      upb_value_setfloat(&val, num);
      break;
    case 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));
      break;
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES): {
      // TODO: is there any reasonable way to avoid a copy here?
      size_t len;
      const char *str = luaL_checklstring(L, narg, &len);
      upb_value_setstr(&val, upb_strduplen(str, len));
      break;
    }
    case UPB_TYPE(MESSAGE):
    case UPB_TYPE(GROUP): {
      lupb_msg *m = lupb_msg_check(L, narg);
      if (m->msgdef != upb_downcast_msgdef(f->def))
        luaL_error(L, "Tried to assign a message of the wrong type.");
      upb_value_setmsg(&val, m->msg);
      break;
    }
  }
  return val;
}


static int lupb_msg_index(lua_State *L) {
  assert(lua_gettop(L) == 2);  // __index should always be called with 2 args.
  lupb_msg *m = lupb_msg_check(L, 1);
  size_t len;
  const char *name = luaL_checklstring(L, 2, &len);
  upb_string namestr = UPB_STACK_STRING_LEN(name, len);
  upb_fielddef *f = upb_msgdef_ntof(m->msgdef, &namestr);
  if (f) {
    lupb_pushvalue(L, upb_msg_get(m->msg, f), f);
  } else {
    // It wasn't a field, perhaps it's a method?
    lua_getmetatable(L, 1);
    lua_pushvalue(L, 2);
    lua_rawget(L, -2);
    if (lua_isnil(L, -1)) {
      luaL_error(L, "%s is not a field name or a method name", name);
    }
  }
  return 1;
}

static int lupb_msg_newindex(lua_State *L) {
  assert(lua_gettop(L) == 3);  // __newindex should always be called with 3 args.
  lupb_msg *m = lupb_msg_check(L, 1);
  size_t len;
  const char *name = luaL_checklstring(L, 2, &len);
  upb_string namestr = UPB_STACK_STRING_LEN(name, len);
  upb_fielddef *f = upb_msgdef_ntof(m->msgdef, &namestr);
  if (f) {
    upb_value val = lupb_getvalue(L, 3, f);
    upb_msg_set(m->msg, f, val);
    if (upb_isstring(f)) {
      upb_string_unref(upb_value_getstr(val));
    }
  } else {
    luaL_error(L, "%s is not a field name", name);
  }
  return 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 const struct luaL_Reg lupb_msg_mm[] = {
  {"__gc", lupb_msg_gc},
  {"__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},
  {NULL, NULL}
};


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

static upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) {
  lupb_def *ldef = luaL_checkudata(L, narg, "upb.msgdef");
  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 void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f);

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

static int lupb_msgdef_fieldbyname(lua_State *L) {
  upb_msgdef *m = lupb_msgdef_check(L, 1);
  size_t len;
  const char *name = luaL_checklstring(L, 2, &len);
  upb_string namestr = UPB_STACK_STRING_LEN(name, len);
  upb_fielddef *f = upb_msgdef_ntof(m, &namestr);
  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},
  {"name", lupb_msgdef_name},
  {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);
  lupb_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 **************************************************************/

typedef struct {
  upb_fielddef *field;
} lupb_fielddef;

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 lupb_fielddef *lupb_fielddef_check(lua_State *L, int narg) {
  return luaL_checkudata(L, narg, "upb.fielddef");
}

static int lupb_fielddef_index(lua_State *L) {
  lupb_fielddef *f = lupb_fielddef_check(L, 1);
  const char *str = luaL_checkstring(L, 2);
  if (strcmp(str, "name") == 0) {
    lupb_pushstring(L, f->field->name);
  } else if (strcmp(str, "number") == 0) {
    lua_pushinteger(L, f->field->number);
  } else if (strcmp(str, "type") == 0) {
    lua_pushinteger(L, f->field->type);
  } else if (strcmp(str, "label") == 0) {
    lua_pushinteger(L, f->field->label);
  } else if (strcmp(str, "def") == 0) {
    lupb_def_getorcreate(L, f->field->def, false);
  } else if (strcmp(str, "msgdef") == 0) {
    lupb_def_getorcreate(L, UPB_UPCAST(f->field->msgdef), false);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

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

static const struct luaL_Reg lupb_fielddef_mm[] = {
  {"__gc", lupb_fielddef_gc},
  {"__index", lupb_fielddef_index},
  {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");
}

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);
  size_t len;
  const char *name = luaL_checklstring(L, 2, &len);
  upb_string namestr = UPB_STACK_STRING_LEN(name, len);
  upb_def *def = upb_symtab_lookup(s->symtab, &namestr);
  if (def) {
    lupb_def_getorcreate(L, def, true);
  } else {
    lua_pushnil(L);
  }
  return 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_string desc_str = UPB_STACK_STRING_LEN(str, len);
  upb_parsedesc(s->symtab, &desc_str, &status);
  lupb_checkstatus(L, &status);
  return 0;
}

static const struct luaL_Reg lupb_symtab_m[] = {
  //{"addfds", lupb_symtab_addfds},
  {"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 int lupb_symtab_new(lua_State *L) {
  upb_symtab *s = upb_symtab_new();
  bool created = lupb_cache_getorcreate(L, s, "upb.symtab");
  (void)created;  // For NDEBUG
  assert(created);  // It's new, there shouldn't be an obj for it already.
  return 1;
}

static int lupb_getfdsdef(lua_State *L) {
  upb_msgdef *fdsdef = upb_getfdsdef();  // Gets a ref on fdsdef.
  lupb_def_getorcreate(L, UPB_UPCAST(fdsdef), true);
  return 1;
}

static const struct luaL_Reg lupb_toplevel_m[] = {
  {"symtab", lupb_symtab_new},
  {"fdsdef", lupb_getfdsdef},
  {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.
}

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);
  return 1;  // Return package table.
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback