Commit e3beef06 authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Extract frozen checking to function

parent ec168880
...@@ -1143,6 +1143,7 @@ MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc); ...@@ -1143,6 +1143,7 @@ MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc);
MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg); MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg);
MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...); MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...); MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj);
MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...); MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...); MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...);
MRB_API void mrb_print_backtrace(mrb_state *mrb); MRB_API void mrb_print_backtrace(mrb_state *mrb);
...@@ -1196,6 +1197,11 @@ MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val); ...@@ -1196,6 +1197,11 @@ MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val); MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t); MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
static inline void mrb_check_frozen(mrb_state *mrb, void *o)
{
if (MRB_FROZEN_P((struct RBasic*)o)) mrb_frozen_error(mrb, o);
}
typedef enum call_type { typedef enum call_type {
CALL_PUBLIC, CALL_PUBLIC,
CALL_FCALL, CALL_FCALL,
......
...@@ -87,10 +87,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) ...@@ -87,10 +87,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
static void static void
mrb_struct_modify(mrb_state *mrb, mrb_value strct) mrb_struct_modify(mrb_state *mrb, mrb_value strct)
{ {
if (MRB_FROZEN_P(mrb_basic_ptr(strct))) { mrb_check_frozen(mrb, mrb_basic_ptr(strct));
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen struct");
}
mrb_write_barrier(mrb, mrb_basic_ptr(strct)); mrb_write_barrier(mrb, mrb_basic_ptr(strct));
} }
......
...@@ -120,9 +120,7 @@ ary_fill_with_nil(mrb_value *ptr, mrb_int size) ...@@ -120,9 +120,7 @@ ary_fill_with_nil(mrb_value *ptr, mrb_int size)
static void static void
ary_modify_check(mrb_state *mrb, struct RArray *a) ary_modify_check(mrb_state *mrb, struct RArray *a)
{ {
if (MRB_FROZEN_P(a)) { mrb_check_frozen(mrb, a);
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen array");
}
} }
static void static void
......
...@@ -441,12 +441,7 @@ mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_method_ ...@@ -441,12 +441,7 @@ mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_method_
MRB_CLASS_ORIGIN(c); MRB_CLASS_ORIGIN(c);
h = c->mt; h = c->mt;
if (MRB_FROZEN_P(c)) { mrb_check_frozen(mrb, c);
if (c->tt == MRB_TT_MODULE)
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen module");
else
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen class");
}
if (!h) h = c->mt = kh_init(mt, mrb); if (!h) h = c->mt = kh_init(mt, mrb);
k = kh_put(mt, mrb, h, mid); k = kh_put(mt, mrb, h, mid);
kh_value(h, k) = m; kh_value(h, k) = m;
......
...@@ -484,6 +484,13 @@ mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, char const* fmt, ...@@ -484,6 +484,13 @@ mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, char const* fmt,
mrb_exc_raise(mrb, exc); mrb_exc_raise(mrb, exc);
} }
MRB_API mrb_noreturn void
mrb_frozen_error(mrb_state *mrb, void *frozen_obj)
{
mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %S",
mrb_obj_value(mrb_class(mrb, mrb_obj_value(frozen_obj))));
}
void void
mrb_init_exception(mrb_state *mrb) mrb_init_exception(mrb_state *mrb)
{ {
......
...@@ -746,10 +746,7 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val) ...@@ -746,10 +746,7 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
static void static void
mrb_hash_modify(mrb_state *mrb, mrb_value hash) mrb_hash_modify(mrb_state *mrb, mrb_value hash)
{ {
if (MRB_FROZEN_P(mrb_hash_ptr(hash))) { mrb_check_frozen(mrb, mrb_hash_ptr(hash));
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen hash");
}
if (!RHASH_TBL(hash)) { if (!RHASH_TBL(hash)) {
RHASH_TBL(hash) = ht_new(mrb); RHASH_TBL(hash) = ht_new(mrb);
} }
......
...@@ -493,20 +493,12 @@ str_index_str(mrb_state *mrb, mrb_value str, mrb_value str2, mrb_int offset) ...@@ -493,20 +493,12 @@ str_index_str(mrb_state *mrb, mrb_value str, mrb_value str2, mrb_int offset)
return mrb_str_index(mrb, str, ptr, len, offset); return mrb_str_index(mrb, str, ptr, len, offset);
} }
static void
check_frozen(mrb_state *mrb, struct RString *s)
{
if (MRB_FROZEN_P(s)) {
mrb_raise(mrb, E_FROZEN_ERROR, "can't modify frozen string");
}
}
static mrb_value static mrb_value
str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2) str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
{ {
mrb_int len; mrb_int len;
check_frozen(mrb, s1); mrb_check_frozen(mrb, s1);
if (s1 == s2) return mrb_obj_value(s1); if (s1 == s2) return mrb_obj_value(s1);
s1->flags &= ~MRB_STR_NO_UTF; s1->flags &= ~MRB_STR_NO_UTF;
s1->flags |= s2->flags&MRB_STR_NO_UTF; s1->flags |= s2->flags&MRB_STR_NO_UTF;
...@@ -646,7 +638,7 @@ mrb_locale_from_utf8(const char *utf8, int len) ...@@ -646,7 +638,7 @@ mrb_locale_from_utf8(const char *utf8, int len)
MRB_API void MRB_API void
mrb_str_modify(mrb_state *mrb, struct RString *s) mrb_str_modify(mrb_state *mrb, struct RString *s)
{ {
check_frozen(mrb, s); mrb_check_frozen(mrb, s);
s->flags &= ~MRB_STR_NO_UTF; s->flags &= ~MRB_STR_NO_UTF;
if (RSTR_SHARED_P(s)) { if (RSTR_SHARED_P(s)) {
mrb_shared_string *shared = s->as.heap.aux.shared; mrb_shared_string *shared = s->as.heap.aux.shared;
......
...@@ -346,9 +346,7 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) ...@@ -346,9 +346,7 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{ {
iv_tbl *t; iv_tbl *t;
if (MRB_FROZEN_P(obj)) { mrb_check_frozen(mrb, obj);
mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %S", mrb_obj_value(obj));
}
assign_class_name(mrb, obj, sym, v); assign_class_name(mrb, obj, sym, v);
if (!obj->iv) { if (!obj->iv) {
obj->iv = iv_new(mrb); obj->iv = iv_new(mrb);
......
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