Permalink
Browse files

new function mrb_obj_iv_ifnone()

  • Loading branch information...
1 parent 2a5d828 commit f63d5c290510965e1256e4fac77709da83a6719a @matz matz committed Dec 12, 2012
Showing with 16 additions and 0 deletions.
  1. +1 −0 include/mruby/variable.h
  2. +15 −0 src/variable.c
View
@@ -41,6 +41,7 @@ int mrb_const_defined(mrb_state*, mrb_value, mrb_sym);
mrb_value mrb_obj_iv_get(mrb_state *mrb, struct RObject *obj, mrb_sym sym);
void mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v);
int mrb_obj_iv_defined(mrb_state *mrb, struct RObject *obj, mrb_sym sym);
+void mrb_obj_iv_ifnone(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v);
mrb_value mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym);
void mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v);
int mrb_iv_defined(mrb_state*, mrb_value, mrb_sym);
View
@@ -461,6 +461,21 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
}
void
+mrb_obj_iv_ifnone(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
+{
+ iv_tbl *t = obj->iv;
+
+ if (!t) {
+ t = obj->iv = iv_new(mrb);
+ }
+ else if (iv_get(mrb, t, sym, &v)) {
+ return;
+ }
+ mrb_write_barrier(mrb, (struct RBasic*)obj);
+ iv_put(mrb, t, sym, v);
+}
+
+void
mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v)
{
if (obj_iv_p(obj)) {

0 comments on commit f63d5c2

Please sign in to comment.