diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 5d3c5f9d..aedd9836 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -375,6 +375,9 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } } else { gc_mark_object(pic, (struct pic_object *)proc->u.f.name); + if (proc->u.f.env) { + gc_mark_object(pic, (struct pic_object *)proc->u.f.env); + } } break; } diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h index eaac2ef7..caa49cc6 100644 --- a/extlib/benz/include/picrin/proc.h +++ b/extlib/benz/include/picrin/proc.h @@ -27,6 +27,7 @@ struct pic_proc { struct { pic_func_t func; pic_sym *name; + struct pic_dict *env; } f; struct { struct pic_irep *irep; @@ -48,6 +49,9 @@ struct pic_proc *pic_make_proc(pic_state *, pic_func_t, const char *); struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); pic_sym *pic_proc_name(struct pic_proc *); +struct pic_dict *pic_proc_env(pic_state *, struct pic_proc *); +pic_value pic_proc_env_ref(pic_state *, struct pic_proc *, const char *); +void pic_proc_env_set(pic_state *, struct pic_proc *, const char *, pic_value); #if defined(__cplusplus) } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index e8c80f7a..d730c909 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -18,6 +18,7 @@ pic_make_proc(pic_state *pic, pic_func_t func, const char *name) proc->tag = PIC_PROC_TAG_FUNC; proc->u.f.func = func; proc->u.f.name = sym; + proc->u.f.env = NULL; return proc; } @@ -45,6 +46,29 @@ pic_proc_name(struct pic_proc *proc) PIC_UNREACHABLE(); } +struct pic_dict * +pic_proc_env(pic_state *pic, struct pic_proc *proc) +{ + assert(pic_proc_func_p(proc)); + + if (! proc->u.f.env) { + proc->u.f.env = pic_make_dict(pic); + } + return proc->u.f.env; +} + +pic_value +pic_proc_env_ref(pic_state *pic, struct pic_proc *proc, const char *key) +{ + return pic_dict_ref(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key)); +} + +void +pic_proc_env_set(pic_state *pic, struct pic_proc *proc, const char *key, pic_value val) +{ + pic_dict_set(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key), val); +} + static pic_value pic_proc_proc_p(pic_state *pic) {