/* Returns name of time's timezone. */ static mrb_value mrb_time_zone(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value(); if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value(); return mrb_str_new_static(mrb, timezone_names[tm->timezone].name, timezone_names[tm->timezone].len); }
MRB_API mrb_value mrb_sym2str(mrb_state *mrb, mrb_sym sym) { mrb_int len; const char *name = mrb_sym2name_len(mrb, sym, &len); if (!name) return mrb_undef_value(); /* can't happen */ if (sym&1) { /* inline symbol */ return mrb_str_new(mrb, name, len); } return mrb_str_new_static(mrb, name, len); }
static mrb_value hdb_path(mrb_state *mrb, mrb_value self) { hdb_context *context = DATA_PTR(self); const char *path; path = tchdbpath(context->hdb); if (!path) { mrb_raise(mrb, E_ARGUMENT_ERROR, "internal error"); } return mrb_str_new_static(mrb, path, strlen(path)); }
MRB_API const char* mrb_sym2name(mrb_state *mrb, mrb_sym sym) { mrb_int len; const char *name = mrb_sym2name_len(mrb, sym, &len); if (!name) return NULL; if (symname_p(name) && strlen(name) == (size_t)len) { return name; } else { mrb_value str = mrb_str_dump(mrb, mrb_str_new_static(mrb, name, len)); return RSTRING_PTR(str); } }
const char* mrb_sym2name(mrb_state *mrb, mrb_sym sym) { size_t len; const char *name = mrb_sym2name_len(mrb, sym, &len); if (!name) return NULL; if (symname_p(name) && strlen(name) == len) { return name; } else { mrb_value str = mrb_str_dump(mrb, mrb_str_new_static(mrb, name, len)); return RSTRING(str)->ptr; } }
static ssize_t mrb_wslay_event_send_callback(wslay_event_context_ptr ctx, const uint8_t *buf, size_t len, int flags, void *user_data) { mrb_wslay_user_data *data = (mrb_wslay_user_data *) user_data; mrb_state* mrb = data->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf *prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; mrb_int ret = -1; MRB_TRY(&c_jmp) { data->mrb->jmp = &c_jmp; errno = 0; mrb_value buf_obj = mrb_str_new_static(mrb, (const char *) buf, len); mrb_assert(mrb_type(data->send_callback) == MRB_TT_PROC); mrb_value sent = mrb_yield(mrb, data->send_callback, buf_obj); ret = mrb_int(mrb, sent); mrb_assert(ret >= 0&&ret <= len); mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EAGAIN"))|| mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EWOULDBLOCK"))) { mrb->exc = NULL; wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK); } else { wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE); } } MRB_END_EXC(&c_jmp); mrb_gc_arena_restore(mrb, ai); return ret; }
static void mrb_search_method_owner(mrb_state *mrb, struct RClass *c, mrb_value obj, mrb_sym name, struct RClass **owner, struct RProc **proc, mrb_bool unbound) { mrb_value ret; const char *s; *owner = c; *proc = method_search_vm(mrb, owner, name); if (!*proc) { if (unbound) { goto name_error; } if (!mrb_respond_to(mrb, obj, mrb_intern_lit(mrb, "respond_to_missing?"))) { goto name_error; } ret = mrb_funcall(mrb, obj, "respond_to_missing?", 2, mrb_symbol_value(name), mrb_true_value()); if (!mrb_test(ret)) { goto name_error; } *owner = c; } while ((*owner)->tt == MRB_TT_ICLASS) *owner = (*owner)->c; return; name_error: s = mrb_class_name(mrb, c); mrb_raisef( mrb, E_NAME_ERROR, "undefined method `%S' for class `%S'", mrb_sym2str(mrb, name), mrb_str_new_static(mrb, s, strlen(s)) ); }
static mrb_irep* read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags) { int i; const uint8_t *src = bin; ptrdiff_t diff; uint16_t tt, pool_data_len, snl; int plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); /* skip record size */ src += sizeof(uint32_t); /* number of local variable */ irep->nlocals = bin_to_uint16(src); src += sizeof(uint16_t); /* number of register variable */ irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); /* number of child irep */ irep->rlen = (size_t)bin_to_uint16(src); src += sizeof(uint16_t); /* Binary Data Section */ /* ISEQ BLOCK */ irep->ilen = (uint16_t)bin_to_uint32(src); src += sizeof(uint32_t); src += skip_padding(src); if (irep->ilen > 0) { if (SIZE_ERROR_MUL(irep->ilen, sizeof(mrb_code))) { return NULL; } if ((flags & FLAG_SRC_MALLOC) == 0 && (flags & FLAG_BYTEORDER_NATIVE)) { irep->iseq = (mrb_code*)src; src += sizeof(mrb_code) * irep->ilen; irep->flags |= MRB_ISEQ_NO_FREE; } else { size_t data_len = sizeof(mrb_code) * irep->ilen; irep->iseq = (mrb_code *)mrb_malloc(mrb, data_len); memcpy(irep->iseq, src, data_len); src += data_len; } } /* POOL BLOCK */ plen = bin_to_uint32(src); /* number of pool */ src += sizeof(uint32_t); if (plen > 0) { if (SIZE_ERROR_MUL(plen, sizeof(mrb_value))) { return NULL; } irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); for (i = 0; i < plen; i++) { mrb_value s; tt = *src++; /* pool TT */ pool_data_len = bin_to_uint16(src); /* pool data length */ src += sizeof(uint16_t); if (flags & FLAG_SRC_MALLOC) { s = mrb_str_new(mrb, (char *)src, pool_data_len); } else { s = mrb_str_new_static(mrb, (char *)src, pool_data_len); } src += pool_data_len; switch (tt) { /* pool data */ case IREP_TT_FIXNUM: { mrb_value num = mrb_str_to_inum(mrb, s, 10, FALSE); #ifdef MRB_WITHOUT_FLOAT irep->pool[i] = num; #else irep->pool[i] = mrb_float_p(num)? mrb_float_pool(mrb, mrb_float(num)) : num; #endif } break; #ifndef MRB_WITHOUT_FLOAT case IREP_TT_FLOAT: irep->pool[i] = mrb_float_pool(mrb, mrb_str_to_dbl(mrb, s, FALSE)); break; #endif case IREP_TT_STRING: irep->pool[i] = mrb_str_pool(mrb, s); break; default: /* should not happen */ irep->pool[i] = mrb_nil_value(); break; } irep->plen++; mrb_gc_arena_restore(mrb, ai); } } /* SYMS BLOCK */ irep->slen = (uint16_t)bin_to_uint32(src); /* syms length */ src += sizeof(uint32_t); if (irep->slen > 0) { if (SIZE_ERROR_MUL(irep->slen, sizeof(mrb_sym))) { return NULL; } irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); /* symbol name length */ src += sizeof(uint16_t); if (snl == MRB_DUMP_NULL_SYM_LEN) { irep->syms[i] = 0; continue; } if (flags & FLAG_SRC_MALLOC) { irep->syms[i] = mrb_intern(mrb, (char *)src, snl); } else { irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl); } src += snl + 1; mrb_gc_arena_restore(mrb, ai); } } irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen); diff = src - bin; mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX); *len = (size_t)diff; return irep; }
void grn_mrb_value_to_raw_data(mrb_state *mrb, const char *context, mrb_value mrb_value_, grn_id domain_id, grn_mrb_value_to_raw_data_buffer *buffer, void **raw_value, unsigned int *raw_value_size) { grn_ctx *ctx = (grn_ctx *)mrb->ud; enum mrb_vtype mrb_value_type; grn_bool try_cast = GRN_FALSE; grn_obj *from_bulk = NULL; if (mrb_nil_p(mrb_value_)) { *raw_value = NULL; *raw_value_size = 0; return; } mrb_value_type = mrb_type(mrb_value_); switch (mrb_value_type) { case MRB_TT_STRING : switch (domain_id) { case GRN_DB_SHORT_TEXT : case GRN_DB_TEXT : case GRN_DB_LONG_TEXT : *raw_value = RSTRING_PTR(mrb_value_); *raw_value_size = RSTRING_LEN(mrb_value_); break; default : try_cast = GRN_TRUE; break; } break; default : { struct RClass *klass; grn_mrb_data *data = &(ctx->impl->mrb); klass = mrb_class(mrb, mrb_value_); if (domain_id == GRN_DB_TIME && klass == data->builtin.time_class) { mrb_value mrb_sec; mrb_value mrb_usec; mrb_sec = mrb_funcall(mrb, mrb_value_, "to_i", 0); mrb_usec = mrb_funcall(mrb, mrb_value_, "usec", 0); buffer->value.time_value = GRN_TIME_PACK(mrb_fixnum(mrb_sec), mrb_fixnum(mrb_usec)); *raw_value = &(buffer->value.time_value); *raw_value_size = sizeof(buffer->value.time_value); } else { try_cast = GRN_TRUE; if (mrb_value_type == MRB_TT_DATA && klass == mrb_class_get_under(mrb, data->module, "Bulk")) { from_bulk = DATA_PTR(mrb_value_); } } } break; } if (!try_cast) { return; } if (!from_bulk) { from_bulk = &(buffer->from); grn_mrb_value_to_bulk(mrb, mrb_value_, from_bulk); } if (!grn_mrb_bulk_cast(mrb, from_bulk, &(buffer->to), domain_id)) { grn_obj *domain; char domain_name[GRN_TABLE_MAX_KEY_SIZE]; int domain_name_size; domain = grn_ctx_at(ctx, domain_id); domain_name_size = grn_obj_name(ctx, domain, domain_name, GRN_TABLE_MAX_KEY_SIZE); mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S: failed to convert to %S: %S", mrb_str_new_static(mrb, context, strlen(context)), mrb_str_new_static(mrb, domain_name, domain_name_size), mrb_funcall(mrb, mrb_value_, "inspect", 0)); } *raw_value = GRN_BULK_HEAD(&(buffer->to)); *raw_value_size = GRN_BULK_VSIZE(&(buffer->to)); }
static mrb_irep* read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, mrb_bool alloc) { size_t i; const uint8_t *src = bin; ptrdiff_t diff; uint16_t tt, pool_data_len, snl; size_t plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); /* skip record size */ src += sizeof(uint32_t); /* number of local variable */ irep->nlocals = bin_to_uint16(src); src += sizeof(uint16_t); /* number of register variable */ irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); /* number of child irep */ irep->rlen = (size_t)bin_to_uint16(src); src += sizeof(uint16_t); /* Binary Data Section */ /* ISEQ BLOCK */ irep->ilen = (size_t)bin_to_uint32(src); src += sizeof(uint32_t); if (irep->ilen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_code), irep->ilen)) { return NULL; } irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); for (i = 0; i < irep->ilen; i++) { irep->iseq[i] = (size_t)bin_to_uint32(src); /* iseq */ src += sizeof(uint32_t); } } /* POOL BLOCK */ plen = (size_t)bin_to_uint32(src); /* number of pool */ src += sizeof(uint32_t); if (plen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_value), plen)) { return NULL; } irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); for (i = 0; i < plen; i++) { mrb_value s; tt = *src++; /* pool TT */ pool_data_len = bin_to_uint16(src); /* pool data length */ src += sizeof(uint16_t); if (alloc) { s = mrb_str_new(mrb, (char *)src, pool_data_len); } else { s = mrb_str_new_static(mrb, (char *)src, pool_data_len); } src += pool_data_len; switch (tt) { /* pool data */ case IREP_TT_FIXNUM: irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; case IREP_TT_FLOAT: irep->pool[i] = mrb_float_pool(mrb, mrb_str_to_dbl(mrb, s, FALSE)); break; case IREP_TT_STRING: irep->pool[i] = mrb_str_pool(mrb, s); break; default: /* should not happen */ irep->pool[i] = mrb_nil_value(); break; } irep->plen++; mrb_gc_arena_restore(mrb, ai); } } /* SYMS BLOCK */ irep->slen = (size_t)bin_to_uint32(src); /* syms length */ src += sizeof(uint32_t); if (irep->slen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_sym), irep->slen)) { return NULL; } irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); /* symbol name length */ src += sizeof(uint16_t); if (snl == MRB_DUMP_NULL_SYM_LEN) { irep->syms[i] = 0; continue; } if (alloc) { irep->syms[i] = mrb_intern(mrb, (char *)src, snl); } else { irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl); } src += snl + 1; mrb_gc_arena_restore(mrb, ai); } } irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen); diff = src - bin; mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX); *len = (size_t)diff; return irep; }