don't expose struct weak to users

This commit is contained in:
Yuichi Nishiwaki 2017-04-09 18:12:13 +09:00
parent 8e1d16e961
commit 287e7473b4
2 changed files with 36 additions and 40 deletions

View File

@ -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

View File

@ -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