From f5154625ba67eeae2f79ef02174770c06e9b96d7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 21 Oct 2013 17:35:14 +0900 Subject: [PATCH] add aux object field to proc object --- include/picrin/proc.h | 6 +++++- src/codegen.c | 9 +++------ src/gc.c | 1 + src/proc.c | 27 +++++++++++++++++++++++++++ src/vm.c | 4 +--- 5 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 src/proc.c diff --git a/include/picrin/proc.h b/include/picrin/proc.h index ea540cba..776d1ac4 100644 --- a/include/picrin/proc.h +++ b/include/picrin/proc.h @@ -10,13 +10,17 @@ struct pic_proc { PIC_OBJECT_HEADER bool cfunc_p; union { - pic_value (*cfunc)(pic_state *); + pic_func_t cfunc; struct pic_irep *irep; } u; + pic_value aux; }; #define pic_proc_ptr(o) ((struct pic_proc *)o.u.data) #define pic_proc_cfunc_p(o) (pic_proc_ptr(o)->cfunc_p) +struct pic_proc *pic_proc_new(pic_state *, struct pic_irep *irep); +struct pic_proc *pic_proc_new_cfunc(pic_state *, pic_func_t cfunc, pic_value aux); + #endif diff --git a/src/codegen.c b/src/codegen.c index 5e82cc6d..d9b36e43 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -77,9 +77,7 @@ pic_defun(pic_state *pic, const char *name, pic_func_t cfunc) struct pic_proc *proc; int idx; - proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC); - proc->cfunc_p = true; - proc->u.cfunc = cfunc; + proc = pic_proc_new_cfunc(pic, cfunc, pic_undef_value()); idx = env_global_define(pic, pic_intern_cstr(pic, name)); pic->globals[idx] = pic_obj_value(proc); } @@ -458,9 +456,8 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env) struct pic_proc *proc; struct pic_irep *irep; - proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC); - proc->cfunc_p = false; - proc->u.irep = irep = new_irep(pic); + irep = new_irep(pic); + proc = pic_proc_new(pic, irep); if (! pic->jmp) { jmp_buf jmp; diff --git a/src/gc.c b/src/gc.c index ada47d86..c977a91c 100644 --- a/src/gc.c +++ b/src/gc.c @@ -148,6 +148,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TT_PROC: { + gc_mark(pic, ((struct pic_proc *)obj)->aux); break; } case PIC_TT_PORT: { diff --git a/src/proc.c b/src/proc.c new file mode 100644 index 00000000..292c4f51 --- /dev/null +++ b/src/proc.c @@ -0,0 +1,27 @@ +#include "picrin.h" +#include "picrin/proc.h" +#include "picrin/irep.h" + +struct pic_proc * +pic_proc_new(pic_state *pic, struct pic_irep *irep) +{ + struct pic_proc *proc; + + proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc *), PIC_TT_PROC); + proc->cfunc_p = false; + proc->u.irep = irep; + proc->aux = pic_undef_value(); + return proc; +} + +struct pic_proc * +pic_proc_new_cfunc(pic_state *pic, pic_func_t cfunc, pic_value aux) +{ + struct pic_proc *proc; + + proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc *), PIC_TT_PROC); + proc->cfunc_p = true; + proc->u.cfunc = cfunc; + proc->aux = aux; + return proc; +} diff --git a/src/vm.c b/src/vm.c index 46b9f3bc..30d64f79 100644 --- a/src/vm.c +++ b/src/vm.c @@ -222,9 +222,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) CASE(OP_LAMBDA) { struct pic_proc *proc; - proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc *), PIC_TT_PROC); - proc->cfunc_p = false; - proc->u.irep = pic->irep[pc->u.i]; + proc = pic_proc_new(pic, pic->irep[pc->u.i]); PUSH(pic_obj_value(proc)); pic_gc_arena_restore(pic, ai); NEXT;