diff --git a/src/cfunc_pointer.c b/src/cfunc_pointer.c index 12ca494..759c390 100644 --- a/src/cfunc_pointer.c +++ b/src/cfunc_pointer.c @@ -199,16 +199,11 @@ mrb_value cfunc_pointer_to_s(mrb_state *mrb, mrb_value self) { struct cfunc_type_data *data = DATA_PTR(self); - size_t len; mrb_value str; - struct RString *s; + const char* p = (const char*)get_cfunc_pointer_data(data); - - len = strlen(p); - str = mrb_str_new(mrb, 0, len); - s = mrb_str_ptr(str); - strcpy(s->ptr, p); - s->len = strlen(s->ptr); + + str = mrb_str_new_cstr(mrb, p); return str; } @@ -252,8 +247,15 @@ cfunc_pointer_addr(mrb_state *mrb, mrb_value self) static mrb_value cfunc_string_addr(mrb_state *mrb, mrb_value self) { - mrb_value ptr = cfunc_pointer_new_with_pointer(mrb, &RSTRING_PTR(self), false); - mrb_obj_iv_set(mrb, mrb_obj_ptr(ptr), mrb_intern_cstr(mrb, "parent_pointer"), self); // keep for GC + mrb_value ptr; + + if (RSTRING(self)->flags & MRB_STR_EMBED) { + const char* p = RSTRING(self)->as.ary; + ptr = cfunc_pointer_new_with_pointer(mrb, &p, false); + } else { + ptr = cfunc_pointer_new_with_pointer(mrb, &(RSTRING(self)->as.heap.ptr), false); + } + mrb_obj_iv_set(mrb, mrb_obj_ptr(ptr), mrb_intern_cstr(mrb, "parent_pointer"), self); // keep for GC return ptr; } diff --git a/src/cfunc_rubyvm.c b/src/cfunc_rubyvm.c index eedd6a6..7a18b84 100644 --- a/src/cfunc_rubyvm.c +++ b/src/cfunc_rubyvm.c @@ -79,7 +79,7 @@ struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v) case MRB_TT_SYMBOL: { - size_t len; + mrb_int len; const char* name = mrb_sym2name_len(mrb, v.value.sym, &len); arg->value.string.len = len; arg->value.string.ptr = mrb_malloc(mrb, len + 1); @@ -89,10 +89,9 @@ struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v) case MRB_TT_STRING: { - struct RString *str = mrb_str_ptr(v); - arg->value.string.len = str->len; + arg->value.string.len = RSTRING_LEN(v); arg->value.string.ptr = mrb_malloc(mrb, arg->value.string.len+1); - memcpy(arg->value.string.ptr, str->ptr, arg->value.string.len+1); + memcpy(arg->value.string.ptr, RSTRING_PTR(v), arg->value.string.len+1); } break;