Skip to content

Commit

Permalink
Updated upb and simplified ruby code a bit with new upb method.
Browse files Browse the repository at this point in the history
  • Loading branch information
haberman committed Apr 22, 2016
1 parent 4057447 commit d419ca1
Show file tree
Hide file tree
Showing 4 changed files with 1,608 additions and 1,278 deletions.
7 changes: 1 addition & 6 deletions ruby/ext/google/protobuf_c/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) {
#define STACK_ENV_STACKBYTES 4096
typedef struct {
upb_env env;
upb_seededalloc alloc;
const char* ruby_error_template;
char allocbuf[STACK_ENV_STACKBYTES];
} stackenv;
Expand All @@ -681,16 +680,12 @@ static bool env_error_func(void* ud, const upb_status* status) {

static void stackenv_init(stackenv* se, const char* errmsg) {
se->ruby_error_template = errmsg;
upb_env_init(&se->env);
upb_seededalloc_init(&se->alloc, &se->allocbuf, STACK_ENV_STACKBYTES);
upb_env_setallocfunc(
&se->env, upb_seededalloc_getallocfunc(&se->alloc), &se->alloc);
upb_env_init2(&se->env, se->allocbuf, sizeof(se->allocbuf), NULL);
upb_env_seterrorfunc(&se->env, env_error_func, se);
}

static void stackenv_uninit(stackenv* se) {
upb_env_uninit(&se->env);
upb_seededalloc_uninit(&se->alloc);
}

/*
Expand Down
38 changes: 18 additions & 20 deletions ruby/ext/google/protobuf_c/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,30 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
name_len--;
}

// Check for a oneof name first.
o = upb_msgdef_ntoo(self->descriptor->msgdef,
name, name_len);
// See if this name corresponds to either a oneof or field in this message.
if (!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, &f,
&o)) {
return rb_call_super(argc, argv);
}

if (o != NULL) {
// This is a oneof -- return which field inside the oneof is set.
if (setter) {
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
}
return which_oneof_field(self, o);
}

// Otherwise, check for a field with that name.
f = upb_msgdef_ntof(self->descriptor->msgdef,
name, name_len);

if (f == NULL) {
return rb_call_super(argc, argv);
}

if (setter) {
if (argc < 2) {
rb_raise(rb_eArgError, "No value provided to setter.");
}
layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
return Qnil;
} else {
return layout_get(self->descriptor->layout, Message_data(self), f);
// This is a field -- get or set the field's value.
assert(f);
if (setter) {
if (argc < 2) {
rb_raise(rb_eArgError, "No value provided to setter.");
}
layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
return Qnil;
} else {
return layout_get(self->descriptor->layout, Message_data(self), f);
}
}
}

Expand Down
Loading

0 comments on commit d419ca1

Please sign in to comment.