Commit b3d0585f authored by take_cheeze's avatar take_cheeze

Add API `mrb_data_init` to initialize `MRB_TT_DATA` tagged instance.

parent f6f31a8e
...@@ -51,6 +51,14 @@ MRB_API void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_t ...@@ -51,6 +51,14 @@ MRB_API void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_t
*(void**)&sval = mrb_data_get_ptr(mrb, obj, type); \ *(void**)&sval = mrb_data_get_ptr(mrb, obj, type); \
} while (0) } while (0)
static inline void
mrb_data_init(mrb_value v, void *ptr, const mrb_data_type *type)
{
mrb_assert(mrb_type(v) == MRB_TT_DATA);
DATA_PTR(v) = ptr;
DATA_TYPE(v) = type;
}
#if defined(__cplusplus) #if defined(__cplusplus)
} /* extern "C" { */ } /* extern "C" { */
#endif #endif
......
...@@ -129,9 +129,7 @@ mrb_random_init(mrb_state *mrb, mrb_value self) ...@@ -129,9 +129,7 @@ mrb_random_init(mrb_state *mrb, mrb_value self)
if (t) { if (t) {
mrb_free(mrb, t); mrb_free(mrb, t);
} }
mrb_data_init(self, NULL, &mt_state_type);
DATA_TYPE(self) = &mt_state_type;
DATA_PTR(self) = NULL;
t = (mt_state *)mrb_malloc(mrb, sizeof(mt_state)); t = (mt_state *)mrb_malloc(mrb, sizeof(mt_state));
t->mti = N + 1; t->mti = N + 1;
...@@ -147,7 +145,7 @@ mrb_random_init(mrb_state *mrb, mrb_value self) ...@@ -147,7 +145,7 @@ mrb_random_init(mrb_state *mrb, mrb_value self)
t->seed = mrb_fixnum(seed); t->seed = mrb_fixnum(seed);
} }
DATA_PTR(self) = t; mrb_data_init(self, t, &mt_state_type);
return self; return self;
} }
......
...@@ -561,8 +561,7 @@ mrb_time_initialize(mrb_state *mrb, mrb_value self) ...@@ -561,8 +561,7 @@ mrb_time_initialize(mrb_state *mrb, mrb_value self)
if (tm) { if (tm) {
mrb_free(mrb, tm); mrb_free(mrb, tm);
} }
DATA_TYPE(self) = &mrb_time_type; mrb_data_init(self, NULL, &mrb_time_type);
DATA_PTR(self) = NULL;
n = mrb_get_args(mrb, "|iiiiiii", n = mrb_get_args(mrb, "|iiiiiii",
&ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
...@@ -572,7 +571,7 @@ mrb_time_initialize(mrb_state *mrb, mrb_value self) ...@@ -572,7 +571,7 @@ mrb_time_initialize(mrb_state *mrb, mrb_value self)
else { else {
tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL); tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
} }
DATA_PTR(self) = tm; mrb_data_init(self, tm, &mrb_time_type);
return self; return self;
} }
...@@ -589,8 +588,7 @@ mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy) ...@@ -589,8 +588,7 @@ mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
} }
if (!DATA_PTR(copy)) { if (!DATA_PTR(copy)) {
DATA_PTR(copy) = mrb_malloc(mrb, sizeof(struct mrb_time)); mrb_data_init(copy, mrb_malloc(mrb, sizeof(struct mrb_time)), &mrb_time_type);
DATA_TYPE(copy) = &mrb_time_type;
} }
*(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src); *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src);
return copy; return copy;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment