From 287e7473b47c6d1fdc8e53264272c4733fbc8204 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Apr 2017 18:12:13 +0900 Subject: [PATCH] don't expose struct weak to users --- Makefile | 2 +- lib/weak.c | 74 ++++++++++++++++++++++++++---------------------------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 1bc84673..925f7479 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ LDFLAGS += -lm prefix ?= /usr/local -all: CFLAGS += -O2 -DNDEBUG=1 +all: CFLAGS += -O2 -g -DNDEBUG=1 all: picrin debug: CFLAGS += -O0 -g diff --git a/lib/weak.c b/lib/weak.c index 7308722a..f8a7a893 100644 --- a/lib/weak.c +++ b/lib/weak.c @@ -7,6 +7,35 @@ KHASH_DEFINE(weak, struct object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) +static pic_value +weak_call(pic_state *pic) +{ + pic_value self, key, val; + int n; + + n = pic_get_args(pic, "&o|o", &self, &key, &val); + + if (! obj_p(pic, key)) { + pic_error(pic, "attempted to set a non-object key", 1, key); + } + + if (n == 1) { + if (! pic_weak_has(pic, self, key)) { + return pic_false_value(pic); + } + return pic_weak_ref(pic, self, key); + } else { + if (pic_false_p(pic, val)) { + if (pic_weak_has(pic, self, key)) { + pic_weak_del(pic, self, key); + } + } else { + pic_weak_set(pic, self, key, val); + } + return pic_undef_value(pic); + } +} + pic_value pic_make_weak(pic_state *pic) { @@ -15,14 +44,13 @@ pic_make_weak(pic_state *pic) weak = (struct weak *)pic_obj_alloc(pic, sizeof(struct weak), PIC_TYPE_WEAK); weak->prev = NULL; kh_init(weak, &weak->hash); - - return obj_value(pic, weak); + return pic_lambda(pic, weak_call, 1, obj_value(pic, weak)); } pic_value pic_weak_ref(pic_state *pic, pic_value weak, pic_value key) { - khash_t(weak) *h = &weak_ptr(pic, weak)->hash; + khash_t(weak) *h = &weak_ptr(pic, proc_ptr(pic, weak)->locals[0])->hash; int it; it = kh_get(weak, h, obj_ptr(pic, key)); @@ -35,7 +63,7 @@ pic_weak_ref(pic_state *pic, pic_value weak, pic_value key) void pic_weak_set(pic_state *pic, pic_value weak, pic_value key, pic_value val) { - khash_t(weak) *h = &weak_ptr(pic, weak)->hash; + khash_t(weak) *h = &weak_ptr(pic, proc_ptr(pic, weak)->locals[0])->hash; int ret; int it; @@ -46,7 +74,7 @@ pic_weak_set(pic_state *pic, pic_value weak, pic_value key, pic_value val) bool pic_weak_has(pic_state *pic, pic_value weak, pic_value key) { - khash_t(weak) *h = &weak_ptr(pic, weak)->hash; + khash_t(weak) *h = &weak_ptr(pic, proc_ptr(pic, weak)->locals[0])->hash; return kh_get(weak, h, obj_ptr(pic, key)) != kh_end(h); } @@ -54,7 +82,7 @@ pic_weak_has(pic_state *pic, pic_value weak, pic_value key) void pic_weak_del(pic_state *pic, pic_value weak, pic_value key) { - khash_t(weak) *h = &weak_ptr(pic, weak)->hash; + khash_t(weak) *h = &weak_ptr(pic, proc_ptr(pic, weak)->locals[0])->hash; int it; it = kh_get(weak, h, obj_ptr(pic, key)); @@ -64,44 +92,12 @@ pic_weak_del(pic_state *pic, pic_value weak, pic_value key) kh_del(weak, h, it); } - -static pic_value -weak_call(pic_state *pic) -{ - pic_value key, val, weak; - int n; - - n = pic_get_args(pic, "o|o", &key, &val); - - if (! obj_p(pic, key)) { - pic_error(pic, "attempted to set a non-object key", 1, key); - } - - weak = pic_closure_ref(pic, 0); - - if (n == 1) { - if (! pic_weak_has(pic, weak, key)) { - return pic_false_value(pic); - } - return pic_weak_ref(pic, weak, key); - } else { - if (pic_false_p(pic, val)) { - if (pic_weak_has(pic, weak, key)) { - pic_weak_del(pic, weak, key); - } - } else { - pic_weak_set(pic, weak, key, val); - } - return pic_undef_value(pic); - } -} - static pic_value pic_weak_make_ephemeron_table(pic_state *pic) { pic_get_args(pic, ""); - return pic_lambda(pic, weak_call, 1, pic_make_weak(pic)); + return pic_make_weak(pic); } void