static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink, int depth) { if (map == Qnil) return; Map* self = ruby_to_Map(map); upb_sink subsink; upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); assert(upb_fielddef_type(f) == UPB_TYPE_MESSAGE); const upb_fielddef* key_field = map_field_key(f); const upb_fielddef* value_field = map_field_value(f); Map_iter it; for (Map_begin(map, &it); !Map_done(&it); Map_next(&it)) { VALUE key = Map_iter_key(&it); VALUE value = Map_iter_value(&it); upb_sink entry_sink; upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG), &entry_sink); upb_sink_startmsg(&entry_sink); put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink); put_ruby_value(value, value_field, self->value_type_class, depth + 1, &entry_sink); upb_status status; upb_sink_endmsg(&entry_sink, &status); upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG)); } upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); }
// Returns the effective closure type for this handler (which will propagate // from outer frames if this frame has no START* handler). Not implemented for // UPB_HANDLER_STRING at the moment since this is not needed. Returns NULL is // the effective closure type is unspecified (either no handler was registered // to specify it or the handler that was registered did not specify the closure // type). const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { assert(type != UPB_HANDLER_STRING); const void *ret = h->top_closure_type; upb_selector_t sel; if (upb_fielddef_isseq(f) && type != UPB_HANDLER_STARTSEQ && type != UPB_HANDLER_ENDSEQ && h->table[sel = getsel(h, f, UPB_HANDLER_STARTSEQ)].func) { ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); } if (type == UPB_HANDLER_STRING && h->table[sel = getsel(h, f, UPB_HANDLER_STARTSTR)].func) { ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); } // The effective type of the submessage; not used yet. // if (type == SUBMESSAGE && // h->table[sel = getsel(h, f, UPB_HANDLER_STARTSUBMSG)].func) { // ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); // } return ret; }
static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink, int depth) { if (submsg == Qnil) return; upb_sink subsink; VALUE descriptor = rb_ivar_get(submsg, descriptor_instancevar_interned); Descriptor* subdesc = ruby_to_Descriptor(descriptor); upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink); putmsg(submsg, subdesc, &subsink, depth + 1); upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG)); }
static void putstr(VALUE str, const upb_fielddef *f, upb_sink *sink) { if (str == Qnil) return; assert(BUILTIN_TYPE(str) == RUBY_T_STRING); upb_sink subsink; // Ensure that the string has the correct encoding. We also check at field-set // time, but the user may have mutated the string object since then. native_slot_validate_string_encoding(upb_fielddef_type(f), str); upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), RSTRING_LEN(str), &subsink); upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), RSTRING_PTR(str), RSTRING_LEN(str), NULL); upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR)); }
static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink, int depth) { upb_sink subsink; upb_fieldtype_t type = upb_fielddef_type(f); upb_selector_t sel = 0; int size; if (ary == Qnil) return; upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); if (upb_fielddef_isprimitive(f)) { sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); } size = NUM2INT(RepeatedField_length(ary)); for (int i = 0; i < size; i++) { void* memory = RepeatedField_index_native(ary, i); switch (type) { #define T(upbtypeconst, upbtype, ctype) \ case upbtypeconst: \ upb_sink_put##upbtype(&subsink, sel, *((ctype *)memory)); \ break; T(UPB_TYPE_FLOAT, float, float) T(UPB_TYPE_DOUBLE, double, double) T(UPB_TYPE_BOOL, bool, int8_t) case UPB_TYPE_ENUM: T(UPB_TYPE_INT32, int32, int32_t) T(UPB_TYPE_UINT32, uint32, uint32_t) T(UPB_TYPE_INT64, int64, int64_t) T(UPB_TYPE_UINT64, uint64, uint64_t) case UPB_TYPE_STRING: case UPB_TYPE_BYTES: putstr(*((VALUE *)memory), f, &subsink); break; case UPB_TYPE_MESSAGE: putsubmsg(*((VALUE *)memory), f, &subsink, depth); break; #undef T } } upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); }
// Checks whether the START* handler specified by f & type is missing even // though it is required to convert the established type of an outer frame // ("closure_type") into the established type of an inner frame (represented in // the return closure type of this handler's attr. bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, upb_status *status) { upb_selector_t sel = getsel(h, f, type); if (h->table[sel].func) return true; const void *closure_type = effective_closure_type(h, f, type); const upb_handlerattr *attr = &h->table[sel].attr; const void *return_closure_type = upb_handlerattr_returnclosuretype(attr); if (closure_type && return_closure_type && closure_type != return_closure_type) { upb_status_seterrf(status, "expected start handler to return sub type for field %f", upb_fielddef_name(f)); return false; } return true; }
void mousesel(XEvent *e, int done) { int type, seltype = SEL_REGULAR; uint state = e->xbutton.state & ~(Button1Mask | forceselmod); for (type = 1; type < LEN(selmasks); ++type) { if (match(selmasks[type], state)) { seltype = type; break; } } selextend(evcol(e), evrow(e), seltype, done); if (done) setsel(getsel(), e->xbutton.time); }
static void put_ruby_value(VALUE value, const upb_fielddef *f, VALUE type_class, int depth, upb_sink *sink) { upb_selector_t sel = 0; if (upb_fielddef_isprimitive(f)) { sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); } switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: upb_sink_putint32(sink, sel, NUM2INT(value)); break; case UPB_TYPE_INT64: upb_sink_putint64(sink, sel, NUM2LL(value)); break; case UPB_TYPE_UINT32: upb_sink_putuint32(sink, sel, NUM2UINT(value)); break; case UPB_TYPE_UINT64: upb_sink_putuint64(sink, sel, NUM2ULL(value)); break; case UPB_TYPE_FLOAT: upb_sink_putfloat(sink, sel, NUM2DBL(value)); break; case UPB_TYPE_DOUBLE: upb_sink_putdouble(sink, sel, NUM2DBL(value)); break; case UPB_TYPE_ENUM: { if (TYPE(value) == T_SYMBOL) { value = rb_funcall(type_class, rb_intern("resolve"), 1, value); } upb_sink_putint32(sink, sel, NUM2INT(value)); break; } case UPB_TYPE_BOOL: upb_sink_putbool(sink, sel, value == Qtrue); break; case UPB_TYPE_STRING: case UPB_TYPE_BYTES: putstr(value, f, sink); break; case UPB_TYPE_MESSAGE: putsubmsg(value, f, sink, depth); } }
static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink *sink, int depth) { MessageHeader* msg; upb_msg_field_iter i; upb_status status; upb_sink_startmsg(sink); // Protect against cycles (possible because users may freely reassign message // and repeated fields) by imposing a maximum recursion depth. if (depth > ENCODE_MAX_NESTING) { rb_raise(rb_eRuntimeError, "Maximum recursion depth exceeded during encoding."); } TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); for (upb_msg_field_begin(&i, desc->msgdef); !upb_msg_field_done(&i); upb_msg_field_next(&i)) { upb_fielddef *f = upb_msg_iter_field(&i); uint32_t offset = desc->layout->fields[upb_fielddef_index(f)].offset + sizeof(MessageHeader); if (upb_fielddef_containingoneof(f)) { uint32_t oneof_case_offset = desc->layout->fields[upb_fielddef_index(f)].case_offset + sizeof(MessageHeader); // For a oneof, check that this field is actually present -- skip all the // below if not. if (DEREF(msg, oneof_case_offset, uint32_t) != upb_fielddef_number(f)) { continue; } // Otherwise, fall through to the appropriate singular-field handler // below. } if (is_map_field(f)) { VALUE map = DEREF(msg, offset, VALUE); if (map != Qnil) { putmap(map, f, sink, depth); } } else if (upb_fielddef_isseq(f)) { VALUE ary = DEREF(msg, offset, VALUE); if (ary != Qnil) { putary(ary, f, sink, depth); } } else if (upb_fielddef_isstring(f)) { VALUE str = DEREF(msg, offset, VALUE); if (RSTRING_LEN(str) > 0) { putstr(str, f, sink); } } else if (upb_fielddef_issubmsg(f)) { putsubmsg(DEREF(msg, offset, VALUE), f, sink, depth); } else { upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); #define T(upbtypeconst, upbtype, ctype, default_value) \ case upbtypeconst: { \ ctype value = DEREF(msg, offset, ctype); \ if (value != default_value) { \ upb_sink_put##upbtype(sink, sel, value); \ } \ } \ break; switch (upb_fielddef_type(f)) { T(UPB_TYPE_FLOAT, float, float, 0.0) T(UPB_TYPE_DOUBLE, double, double, 0.0) T(UPB_TYPE_BOOL, bool, uint8_t, 0) case UPB_TYPE_ENUM: T(UPB_TYPE_INT32, int32, int32_t, 0) T(UPB_TYPE_UINT32, uint32, uint32_t, 0) T(UPB_TYPE_INT64, int64, int64_t, 0) T(UPB_TYPE_UINT64, uint64, uint64_t, 0) case UPB_TYPE_STRING: case UPB_TYPE_BYTES: case UPB_TYPE_MESSAGE: rb_raise(rb_eRuntimeError, "Internal error."); } #undef T } } upb_sink_endmsg(sink, &status); }
void selcopy(Time t) { xsetsel(getsel(), t); }
bool upb_handlers_freeze(upb_handlers *const*handlers, int n, upb_status *s) { // TODO: verify we have a transitive closure. for (int i = 0; i < n; i++) { upb_handlers *h = handlers[i]; if (!upb_ok(&h->status_)) { upb_status_seterrf(s, "handlers for message %s had error status: %s", upb_msgdef_fullname(upb_handlers_msgdef(h)), upb_status_errmsg(&h->status_)); return false; } // Check that there are no closure mismatches due to missing Start* handlers // or subhandlers with different type-level types. upb_msg_iter j; for(upb_msg_begin(&j, h->msg); !upb_msg_done(&j); upb_msg_next(&j)) { const upb_fielddef *f = upb_msg_iter_field(&j); if (upb_fielddef_isseq(f)) { if (!checkstart(h, f, UPB_HANDLER_STARTSEQ, s)) return false; } if (upb_fielddef_isstring(f)) { if (!checkstart(h, f, UPB_HANDLER_STARTSTR, s)) return false; } if (upb_fielddef_issubmsg(f)) { bool hashandler = false; if (upb_handlers_gethandler(h, getsel(h, f, UPB_HANDLER_STARTSUBMSG)) || upb_handlers_gethandler(h, getsel(h, f, UPB_HANDLER_ENDSUBMSG))) { hashandler = true; } if (upb_fielddef_isseq(f) && (upb_handlers_gethandler(h, getsel(h, f, UPB_HANDLER_STARTSEQ)) || upb_handlers_gethandler(h, getsel(h, f, UPB_HANDLER_ENDSEQ)))) { hashandler = true; } if (hashandler && !upb_handlers_getsubhandlers(h, f)) { // For now we add an empty subhandlers in this case. It makes the // decoder code generator simpler, because it only has to handle two // cases (submessage has handlers or not) as opposed to three // (submessage has handlers in enclosing message but no subhandlers). // // This makes parsing less efficient in the case that we want to // notice a submessage but skip its contents (like if we're testing // for submessage presence or counting the number of repeated // submessages). In this case we will end up parsing the submessage // field by field and throwing away the results for each, instead of // skipping the whole delimited thing at once. If this is an issue we // can revisit it, but do remember that this only arises when you have // handlers (startseq/startsubmsg/endsubmsg/endseq) set for the // submessage but no subhandlers. The uses cases for this are // limited. upb_handlers *sub = upb_handlers_new(upb_fielddef_msgsubdef(f), &sub); upb_handlers_setsubhandlers(h, f, sub); upb_handlers_unref(sub, &sub); } // TODO(haberman): check type of submessage. // This is slightly tricky; also consider whether we should check that // they match at setsubhandlers time. } } } if (!upb_refcounted_freeze((upb_refcounted*const*)handlers, n, s, UPB_MAX_HANDLER_DEPTH)) { return false; } return true; }
int proc_settings_fileencoder(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { # define addit(h, a) (SendDlgItemMessage(hwnd, h, CB_INSERTSTRING, (WPARAM)-1, (LPARAM)a)) # define selit(h, n) (SendDlgItemMessage(hwnd, h, CB_SETCURSEL, (WPARAM)n, 0)) # define getsel(h) (SendDlgItemMessage(hwnd, h, CB_GETCURSEL, 0, 0)) # define items_i(x) (sizeof(x) / sizeof((x)[0])) # define checkset(h, s)(CheckDlgButton(hwnd, h, s ? BST_CHECKED : BST_UNCHECKED)) # define checkget(h) (IsDlgButtonChecked(hwnd, h) == BST_CHECKED) switch(uMsg) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: fsettings.plugin_settings_setnum("aac", "bitrate", (int)getsel(combo_bitrate)); fsettings.plugin_settings_setnum("aac", "quality", (int)getsel(combo_quality)); fsettings.plugin_settings_setnum("aac", "mp4", (int)checkget(check_mp4)); case IDCANCEL: EndDialog(hwnd, 0); break; } break; case WM_INITDIALOG: { unsigned int i; int v = 0; for(i = 0; i < items_i(sbitrates); i++) { addit(combo_bitrate, sbitrates[i]); } if(fsettings.plugin_settings_getnum("aac", "bitrate", &v, 0, 0))v = 0xa; selit(combo_bitrate, v); for(i = 0; i < items_i(squalities); i++) { addit(combo_quality, squalities[i]); } if(fsettings.plugin_settings_getnum("aac", "quality", &v, 0, 0))v = 0x2; selit(combo_quality, v); if(fsettings.plugin_settings_getnum("aac", "mp4", &v, 0, 0))v = 0; checkset(check_mp4, v); } break; case WM_DESTROY: EndDialog(hwnd, 0); break; } return 0; }