Stubbed out a few more methods in _msg and _context.

pull/13171/head
Joshua Haberman 16 years ago
parent 1139650320
commit 01fb1d45ed
  1. 12
      upb_context.c
  2. 30
      upb_context.h
  3. 14
      upb_msg.h

@ -30,6 +30,7 @@ bool upb_context_init(struct upb_context *c)
void upb_context_free(struct upb_context *c) void upb_context_free(struct upb_context *c)
{ {
upb_strtable_free(&c->symtab); upb_strtable_free(&c->symtab);
for(size_t i = 0; i < c->fd_len; i++) free(c->fd[i]);
} }
struct upb_symtab_entry *upb_context_lookup(struct upb_context *c, struct upb_symtab_entry *upb_context_lookup(struct upb_context *c,
@ -215,7 +216,7 @@ bool upb_context_addfd(struct upb_context *c,
else else
continue; /* No resolving necessary. */ continue; /* No resolving necessary. */
if(!ref.msg) goto error; if(!ref.msg) goto error;
upb_msg_ref(m, f, &ref); upb_msg_ref(m, f, ref);
} }
} }
} }
@ -227,5 +228,14 @@ bool upb_context_addfd(struct upb_context *c,
return true; return true;
error: error:
/* TODO */
return false; return false;
} }
bool upb_context_parsefd(struct upb_context *c, struct upb_string *fd_str) {
google_protobuf_FileDescriptorProto *fd = upb_msg_parse(c->fd_msg, fd_str);
if(!fd) return false;
if(!upb_context_addfd(c, fd)) return false;
c->fd[c->fd_len++] = fd; /* Need to keep a ref since we own it. */
return true;
}

@ -2,9 +2,10 @@
* upb - a minimalist implementation of protocol buffers. * upb - a minimalist implementation of protocol buffers.
* *
* A context represents a namespace of proto definitions, sort of like an * A context represents a namespace of proto definitions, sort of like an
* interpreter's symbol table. It is mostly empty when first constructed. * interpreter's symbol table. It is empty when first constructed, with the
* Clients add definitions to the context by supplying unserialized or * exception of built-in types (those defined in descriptor.proto). Clients
* serialized descriptors (as defined in descriptor.proto). * add definitions to the context by supplying unserialized or serialized
* descriptors (as defined in descriptor.proto).
* *
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details. * Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
*/ */
@ -27,6 +28,12 @@ struct upb_symtab_entry {
struct upb_context { struct upb_context {
struct upb_strtable symtab; struct upb_strtable symtab;
struct upb_msg *fd_msg; /* This is in symtab also, kept here for convenience. */
/* A list of the FileDescriptorProtos we own (from having parsed them
* ourselves) and must free on destruction. */
size_t fd_size, fd_len;
google_protobuf_FileDescriptorProto **fd;
}; };
/* Initializes and frees a upb_context, respectively. Newly initialized /* Initializes and frees a upb_context, respectively. Newly initialized
@ -72,20 +79,9 @@ struct upb_symtab_entry *upb_context_lookup(struct upb_context *c,
bool upb_context_addfd(struct upb_context *c, bool upb_context_addfd(struct upb_context *c,
google_protobuf_FileDescriptorProto *fd); google_protobuf_FileDescriptorProto *fd);
/* Adds the serialized FileDescriptorSet proto contained in fdss to the context, /* Like the previous, but takes a serialized FileDescriptorProto and parses
* and adds symbol table entries for all the objects defined therein. onredef * it before adding to the context. */
* controls the behavior in the case the fds attempts to define a type that is bool upb_context_parsefd(struct upb_context *c, struct upb_string *fd);
* already defined.
*
* Returns true if the protobuf in fds was parsed successfully and all
* references were successfully resolved. If false is returned, the context is
* unmodified. */
bool upb_context_parsefds(struct upb_context *c, struct upb_string *fds,
int onredef);
/* Like the previous, but for a single FileDescriptorProto instead of a set. */
bool upb_context_parsefd(struct upb_context *c, struct upb_string *fds,
int onredef);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

@ -107,6 +107,13 @@ INLINE struct upb_msg_field *upb_get_msg_field(
bool upb_msg_init(struct upb_msg *m, struct google_protobuf_DescriptorProto *d); bool upb_msg_init(struct upb_msg *m, struct google_protobuf_DescriptorProto *d);
void upb_msg_free(struct upb_msg *m); void upb_msg_free(struct upb_msg *m);
/* Clients use this function on a previously initialized upb_msg to resolve the
* "ref" field in the upb_msg_field and upb_abbrev_msg_field. Since messages
* can refer to each other in mutually-recursive ways, this step must be
* separated from initialization. The function is necessary because there are
* multiple internal maps in which the ref appears. */
void upb_msg_ref(struct upb_msg *m, struct upb_msg_field *f, union upb_symbol_ref ref);
/* While these are written to be as fast as possible, it will still be faster /* While these are written to be as fast as possible, it will still be faster
* to cache the results of this lookup if possible. These return NULL if no * to cache the results of this lookup if possible. These return NULL if no
* such field is found. */ * such field is found. */
@ -272,6 +279,13 @@ INLINE void upb_msg_clear(void *s, struct upb_msg *m)
memset(s, 0, m->set_flags_bytes); memset(s, 0, m->set_flags_bytes);
} }
/* Serialization/Deserialization. ********************************************/
/* Parses the string data in s according to the message description in m.
* Returns a newly allocated message which is now owned by the caller, or
* NULL if there was an error parsing the string. */
void *upb_msg_parse(struct upb_msg *m, struct upb_string *s);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif

Loading…
Cancel
Save