diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-06-21 11:04:36 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-06-21 11:04:36 +0900 |
| commit | 396397bce17a0f03eab8ed6512651e665aa89c8a (patch) | |
| tree | d45f8e9ea6bcff1146740aa23db410cfc16a708c | |
| parent | 8b8151a60e53e3182382acca8b128c6d1d7f0b4f (diff) | |
| download | mruby-396397bce17a0f03eab8ed6512651e665aa89c8a.tar.gz mruby-396397bce17a0f03eab8ed6512651e665aa89c8a.zip | |
move KHASH_DECLARE to header files
| -rw-r--r-- | include/mruby/khash.h | 6 | ||||
| -rw-r--r-- | include/mruby/proc.h | 3 | ||||
| -rw-r--r-- | include/mruby/variable.h | 7 | ||||
| -rw-r--r-- | src/class.c | 6 | ||||
| -rw-r--r-- | src/kernel.c | 37 | ||||
| -rw-r--r-- | src/variable.c | 47 |
6 files changed, 60 insertions, 46 deletions
diff --git a/include/mruby/khash.h b/include/mruby/khash.h index 6cb2b383d..e236f0bea 100644 --- a/include/mruby/khash.h +++ b/include/mruby/khash.h @@ -216,12 +216,6 @@ static inline khint_t __ac_X31_hash_string(const char *s) typedef const char *kh_cstr_t; -/* declare common hash types. */ -#include "mruby.h" - -KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1) -KHASH_DECLARE(iv, mrb_sym, mrb_value, 1) - #if defined(__cplusplus) } /* extern "C" { */ #endif diff --git a/include/mruby/proc.h b/include/mruby/proc.h index 52dc5a98e..8f178790e 100644 --- a/include/mruby/proc.h +++ b/include/mruby/proc.h @@ -50,6 +50,9 @@ struct RProc *mrb_proc_new(mrb_state*, mrb_irep*); struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t); struct RProc *mrb_closure_new(mrb_state*, mrb_irep*); +#include "mruby/khash.h" +KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1); + #if defined(__cplusplus) } /* extern "C" { */ #endif diff --git a/include/mruby/variable.h b/include/mruby/variable.h index fb686fd47..440c4cc8b 100644 --- a/include/mruby/variable.h +++ b/include/mruby/variable.h @@ -20,6 +20,7 @@ typedef struct global_variable { //int block_trace; //struct trace_var *trace; } global_variable; + struct global_entry { global_variable *var; mrb_sym id; @@ -41,7 +42,8 @@ mrb_value mrb_obj_iv_get(mrb_state*, struct RObject*, mrb_sym); void mrb_obj_iv_set(mrb_state*, struct RObject*, mrb_sym, mrb_value); const char * mrb_class2name(mrb_state *mrb, struct RClass* klass); 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); /* mrb_iv_set */ +void mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v); +mrb_value mrb_iv_remove(mrb_state *mrb, mrb_value obj, mrb_sym sym); void mrb_copy_generic_ivar(mrb_value clone, mrb_value obj); int mrb_const_defined_at(mrb_state *mrb, struct RClass *klass, mrb_sym id); mrb_value mrb_f_global_variables(mrb_state *mrb, mrb_value self); @@ -55,6 +57,9 @@ void mrb_gc_mark_iv(mrb_state*, struct RObject*); size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*); void mrb_gc_free_iv(mrb_state*, struct RObject*); +#include "mruby/khash.h" +KHASH_DECLARE(iv, mrb_sym, mrb_value, 1) + #if defined(__cplusplus) } /* extern "C" { */ #endif diff --git a/src/class.c b/src/class.c index 72fef70aa..56ab8c06f 100644 --- a/src/class.c +++ b/src/class.c @@ -15,7 +15,8 @@ #include "mruby/array.h" #include "error.h" -#include "mruby/khash.h" +KHASH_DEFINE(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal); +KHASH_DEFINE(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal); typedef struct fc_result { mrb_sym name; @@ -25,9 +26,6 @@ typedef struct fc_result { struct fc_result *prev; } fcresult_t; -int kiv_lookup(khash_t(iv) *table, mrb_sym key, mrb_value *value); -extern struct kh_iv *mrb_class_tbl; - void mrb_gc_mark_mt(mrb_state *mrb, struct RClass *c) { diff --git a/src/kernel.c b/src/kernel.c index a9f4792d7..fd0440e05 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -10,14 +10,12 @@ #include <stdio.h> #include <stdlib.h> #include "mruby/proc.h" - #include "mruby/range.h" #include "mruby/array.h" #include "mruby/hash.h" #include "mruby/class.h" #include "mruby/struct.h" #include "mruby/variable.h" -#include "mruby/khash.h" #include "error.h" typedef enum { @@ -1057,39 +1055,14 @@ mrb_value mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self) { mrb_sym sym; - mrb_value name; - khash_t(iv) *h; - khiter_t k; mrb_value val; - mrb_value Qundef = mrb_undef_value(); - mrb_get_args(mrb, "o", &name); - sym = mrb_to_id(mrb, name); - //if (OBJ_FROZEN(obj)) mrb_error_frozen("object"); - //if (!mrb_is_instance_id(id)) { - // mrb_name_error(mrb, id, "`%s' is not allowed as an instance variable name", mrb_sym2name(mrb, id)); - //} - switch (mrb_type(self)) { - case MRB_TT_OBJECT: - case MRB_TT_CLASS: - case MRB_TT_MODULE: - if (!mrb_obj_ptr(self)->iv) break; - h = mrb_obj_ptr(self)->iv; - if (!h) break; - k = kh_get(iv, h, sym); - if (k != kh_end(h)) { - val = kh_value(h, k); - if (!mrb_obj_equal(mrb, val, Qundef)) { - kh_value(h, k) = Qundef; - return val; - } - } - break; - default: - break; + mrb_get_args(mrb, "n", &sym); + val = mrb_iv_remove(mrb, self, sym); + if (UNDEF_P(val)) { + mrb_name_error(mrb, sym, "instance variable %s not defined", mrb_sym2name(mrb, sym)); } - mrb_name_error(mrb, sym, "instance variable %s not defined", mrb_sym2name(mrb, sym)); - return mrb_nil_value(); /* not reached */ + return val; } static inline int diff --git a/src/variable.c b/src/variable.c index d89e9d6bb..051c971d6 100644 --- a/src/variable.c +++ b/src/variable.c @@ -94,10 +94,28 @@ mrb_obj_iv_get(mrb_state *mrb, struct RObject *obj, mrb_sym sym) return ivget(mrb, obj->iv, sym); } +static int +obj_iv_p(mrb_value obj) +{ + switch (mrb_type(obj)) { + case MRB_TT_OBJECT: + case MRB_TT_CLASS: + case MRB_TT_MODULE: + case MRB_TT_HASH: + case MRB_TT_DATA: + return TRUE; + default: + return FALSE; + } +} + mrb_value mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym) { - return mrb_obj_iv_get(mrb, mrb_obj_ptr(obj), sym); + if (obj_iv_p(obj)) { + return mrb_obj_iv_get(mrb, mrb_obj_ptr(obj), sym); + } + return mrb_nil_value(); } static void @@ -127,7 +145,30 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) void mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v) /* mrb_ivar_set */ { - mrb_obj_iv_set(mrb, mrb_obj_ptr(obj), sym, v); + if (obj_iv_p(obj)) { + mrb_obj_iv_set(mrb, mrb_obj_ptr(obj), sym, v); + } +} + +mrb_value +mrb_iv_remove(mrb_state *mrb, mrb_value obj, mrb_sym sym) +{ + mrb_value val; + + if (obj_iv_p(obj)) { + khash_t(iv) *h = mrb_obj_ptr(obj)->iv; + khiter_t k; + + if (h) { + k = kh_get(iv, h, sym); + if (k != kh_end(h)) { + val = kh_value(h, k); + kh_del(iv, h, k); + return val; + } + } + } + return mrb_undef_value(); } mrb_value @@ -366,7 +407,7 @@ mrb_st_lookup(struct kh_iv *table, mrb_sym id, khiter_t *value) } } -int +static int kiv_lookup(khash_t(iv)* table, mrb_sym key, mrb_value *value) { khash_t(iv) *h=table; |
