Permalink
Browse files

Support freeze in `Struct`.

  • Loading branch information...
take-cheeze committed Apr 21, 2017
1 parent 94bcdca commit da5b7fa769442b41a3dc67f2a8ab9db399910508
Showing with 25 additions and 2 deletions.
  1. +12 −2 mrbgems/mruby-struct/src/struct.c
  2. +13 −0 mrbgems/mruby-struct/test/struct.rb
@@ -84,6 +84,16 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
return ary;
}
static void
mrb_struct_modify(mrb_state *mrb, mrb_value strct)
{
if (MRB_FROZEN_P(mrb_basic_ptr(strct))) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't modify frozen struct");
}
mrb_write_barrier(mrb, mrb_basic_ptr(strct));
}
/* 15.2.18.4.6 */
/*
* call-seq:
@@ -448,8 +458,8 @@ mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
ptr_members = RARRAY_PTR(members);
for (i=0; i<len; i++) {
if (mrb_symbol(ptr_members[i]) == id) {
mrb_struct_modify(mrb, s);
ptr[i] = val;
mrb_write_barrier(mrb, (struct RBasic*)mrb_ptr(s));
return val;
}
}
@@ -512,7 +522,7 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s)
"offset %S too large for struct(size:%S)",
mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
}
mrb_write_barrier(mrb, (struct RBasic*)mrb_ptr(s));
mrb_struct_modify(mrb, s);
return RSTRUCT_PTR(s)[i] = val;
}
@@ -197,3 +197,16 @@
Struct = original_struct
end
end
assert 'Struct#freeze' do
c = Struct.new :m
o = c.new
o.m = :test
assert_equal :test, o.m
o.freeze
assert_raise(RuntimeError) { o.m = :modify }
assert_raise(RuntimeError) { o[:m] = :modify }
assert_equal :test, o.m
end

0 comments on commit da5b7fa

Please sign in to comment.