summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Haberman <joshua@reverberate.org>2011-08-12 15:32:53 -0700
committerJoshua Haberman <joshua@reverberate.org>2011-08-12 15:32:53 -0700
commit51d4e295a4d8e55facf0e95502cde75d488fd511 (patch)
tree7fcbe39a56a15afdddbf10e41ca978d0b50e23d1
parentfe3df2c9bc66ffe5dbc4c9410052be1bc2d7d443 (diff)
Python: fleshed out accessors.
-rw-r--r--lang_ext/python/upb.c54
-rw-r--r--upb/bytestream.h6
-rw-r--r--upb/msg.h4
3 files changed, 50 insertions, 14 deletions
diff --git a/lang_ext/python/upb.c b/lang_ext/python/upb.c
index 9e207b8..ffb3d52 100644
--- a/lang_ext/python/upb.c
+++ b/lang_ext/python/upb.c
@@ -568,33 +568,61 @@ static PyTypeObject PyUpb_SymbolTableType = {
/* Accessor and PyUpb_Message *************************************************/
-static upb_sflow_t PyUpb_Message_StartSequence(void *m, upb_value fval) {
+typedef struct {
+ PyTypeObject type;
+ PyTypeObject *alt_type;
+} PyUpb_MessageType;
+
+typedef struct {
+ PyObject_HEAD;
+ PyObject *msgdef;
+ char data[1];
+} PyUpb_Message;
+
+PyObject **PyUpb_Accessor_GetPtr(PyObject *_m, upb_value fval) {
+ PyUpb_Message *m = (PyUpb_Message*)_m;
upb_fielddef *f = upb_value_getfielddef(fval);
- (void)f;
- return UPB_CONTINUE_WITH(NULL);
+ return (PyObject**)&m->data[f->offset];
+}
+
+static upb_sflow_t PyUpb_Message_StartSequence(void *m, upb_value fval) {
+ PyObject **seq = PyUpb_Accessor_GetPtr(m, fval);
+ PyTypeObject *type = ((PyUpb_MessageType*)Py_TYPE(m))->alt_type;
+ if (!*seq) *seq = type->tp_alloc(type, 0);
+ upb_stdmsg_sethas(m, fval);
+ return UPB_CONTINUE_WITH(*seq);
}
static upb_sflow_t PyUpb_Message_StartSubmessage(void *m, upb_value fval) {
- upb_fielddef *f = upb_value_getfielddef(fval);
- (void)f;
- return UPB_CONTINUE_WITH(NULL);
+ PyObject **submsg = PyUpb_Accessor_GetPtr(m, fval);
+ PyTypeObject *type = Py_TYPE(m);
+ if (!*submsg) *submsg = type->tp_alloc(type, 0);
+ upb_stdmsg_sethas(m, fval);
+ return UPB_CONTINUE_WITH(*submsg);
}
static upb_sflow_t PyUpb_Message_StartRepeatedSubmessage(void *a, upb_value fval) {
- upb_fielddef *f = upb_value_getfielddef(fval);
- (void)f;
- return UPB_CONTINUE_WITH(NULL);
+ (void)fval;
+ PyObject **elem = upb_stdarray_append(a, sizeof(void*));
+ PyTypeObject *type = ((PyUpb_MessageType*)Py_TYPE(a))->alt_type;
+ if (!*elem) *elem = type->tp_alloc(type, 0);
+ return UPB_CONTINUE_WITH(*elem);
}
static upb_flow_t PyUpb_Message_StringValue(void *m, upb_value fval, upb_value val) {
- upb_fielddef *f = upb_value_getfielddef(fval);
- (void)f;
+ PyObject **str = PyUpb_Accessor_GetPtr(m, fval);
+ if (*str) Py_DECREF(*str);
+ *str = PyString_FromStringAndSize(NULL, upb_value_getstrref(val)->len);
+ upb_strref_read(upb_value_getstrref(val), PyString_AsString(*str));
+ upb_stdmsg_sethas(m, fval);
return UPB_CONTINUE;
}
static upb_flow_t PyUpb_Message_AppendStringValue(void *a, upb_value fval, upb_value val) {
- upb_fielddef *f = upb_value_getfielddef(fval);
- (void)f;
+ (void)fval;
+ PyObject **elem = upb_stdarray_append(a, sizeof(void*));
+ *elem = PyString_FromStringAndSize(NULL, upb_value_getstrref(val)->len);
+ upb_strref_read(upb_value_getstrref(val), PyString_AsString(*elem));
return UPB_CONTINUE;
}
diff --git a/upb/bytestream.h b/upb/bytestream.h
index 9b6f5cb..cff177b 100644
--- a/upb/bytestream.h
+++ b/upb/bytestream.h
@@ -138,6 +138,10 @@ typedef struct _upb_strref {
// string.
char *upb_strref_dup(struct _upb_strref *r);
+INLINE void upb_strref_read(struct _upb_strref *r, char *buf) {
+ upb_bytesrc_read(r->bytesrc, r->stream_offset, r->len, buf);
+}
+
/* upb_bytesink ***************************************************************/
@@ -278,7 +282,7 @@ void upb_stringsink_reset(upb_stringsink *s, char *str, size_t size);
const char *upb_stringsink_release(upb_stringsink *s, size_t *len);
// Returns the upb_bytesink* for this stringsrc. Invalidated by reset above.
-upb_bytesink *upb_stringsink_bytesink(void);
+upb_bytesink *upb_stringsink_bytesink(upb_stringsink *s);
#ifdef __cplusplus
} /* extern "C" */
diff --git a/upb/msg.h b/upb/msg.h
index 20a5cfe..7ad97d0 100644
--- a/upb/msg.h
+++ b/upb/msg.h
@@ -180,6 +180,8 @@ void *upb_stdmsg_new(upb_msgdef *md);
// Recursively frees any strings or submessages that the message refers to.
void upb_stdmsg_free(void *m, upb_msgdef *md);
+void upb_stdmsg_sethas(void *_m, upb_value fval);
+
// "hasbit" must be <= UPB_MAX_FIELDS. If it is <0, this field has no hasbit.
upb_value upb_stdmsg_packfval(int16_t hasbit, uint16_t value_offset);
upb_value upb_stdmsg_packfval_subm(int16_t hasbit, uint16_t value_offset,
@@ -208,6 +210,8 @@ typedef struct {
uint32_t size; // Number of elements allocated.
} upb_stdarray;
+void *upb_stdarray_append(upb_stdarray *a, size_t type_size);
+
upb_flow_t upb_stdmsg_setint64_r(void *c, upb_value fval, upb_value val);
upb_flow_t upb_stdmsg_setint32_r(void *c, upb_value fval, upb_value val);
upb_flow_t upb_stdmsg_setuint64_r(void *c, upb_value fval, upb_value val);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback