picrin/lib/weak.c

112 lines
2.3 KiB
C
Raw Normal View History

2015-06-09 05:31:46 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/object.h"
2015-06-09 05:31:46 -04:00
2016-02-21 06:32:00 -05:00
KHASH_DEFINE(weak, struct object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
2015-06-24 18:14:29 -04:00
2016-02-19 14:35:15 -05:00
pic_value
2016-02-10 07:50:39 -05:00
pic_make_weak(pic_state *pic)
2015-06-09 05:31:46 -04:00
{
2016-02-21 06:32:00 -05:00
struct weak *weak;
2015-06-09 05:31:46 -04:00
2016-02-21 06:32:00 -05:00
weak = (struct weak *)pic_obj_alloc(pic, sizeof(struct weak), PIC_TYPE_WEAK);
2016-02-10 07:50:39 -05:00
weak->prev = NULL;
kh_init(weak, &weak->hash);
2015-06-09 05:31:46 -04:00
2016-02-19 14:35:15 -05:00
return pic_obj_value(weak);
2015-06-09 05:31:46 -04:00
}
pic_value
2016-02-19 14:35:15 -05:00
pic_weak_ref(pic_state *pic, pic_value weak, pic_value key)
2015-06-09 05:31:46 -04:00
{
2016-02-19 14:35:15 -05:00
khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash;
2016-02-20 11:52:34 -05:00
int it;
2015-06-09 05:31:46 -04:00
2016-02-19 14:35:15 -05:00
it = kh_get(weak, h, pic_obj_ptr(key));
2015-06-24 18:14:29 -04:00
if (it == kh_end(h)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "element not found for given key", 1, key);
2015-06-09 05:31:46 -04:00
}
2015-06-24 18:14:29 -04:00
return kh_val(h, it);
2015-06-09 05:31:46 -04:00
}
void
2016-02-19 14:35:15 -05:00
pic_weak_set(pic_state *pic, pic_value weak, pic_value key, pic_value val)
2015-06-09 05:31:46 -04:00
{
2016-02-19 14:35:15 -05:00
khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash;
2015-06-24 18:14:29 -04:00
int ret;
2016-02-20 11:52:34 -05:00
int it;
2015-06-24 18:14:29 -04:00
2016-02-19 14:35:15 -05:00
it = kh_put(weak, h, pic_obj_ptr(key), &ret);
2015-06-24 18:14:29 -04:00
kh_val(h, it) = val;
2015-06-09 05:31:46 -04:00
}
bool
2016-02-19 14:35:15 -05:00
pic_weak_has(pic_state *pic, pic_value weak, pic_value key)
2015-06-09 05:31:46 -04:00
{
2016-02-19 14:35:15 -05:00
khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash;
return kh_get(weak, h, pic_obj_ptr(key)) != kh_end(h);
2015-06-09 05:31:46 -04:00
}
void
2016-02-19 14:35:15 -05:00
pic_weak_del(pic_state *pic, pic_value weak, pic_value key)
2015-06-09 05:31:46 -04:00
{
2016-02-19 14:35:15 -05:00
khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash;
2016-02-20 11:52:34 -05:00
int it;
2015-06-24 18:14:29 -04:00
2016-02-19 14:35:15 -05:00
it = kh_get(weak, h, pic_obj_ptr(key));
2015-06-24 18:14:29 -04:00
if (it == kh_end(h)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "element not found for given key", 1, key);
2015-06-09 05:31:46 -04:00
}
2016-02-10 07:50:39 -05:00
kh_del(weak, h, it);
2015-06-09 05:31:46 -04:00
}
2015-06-09 05:50:46 -04:00
static pic_value
2016-02-10 07:50:39 -05:00
weak_call(pic_state *pic)
2015-06-09 05:50:46 -04:00
{
2016-02-19 14:35:15 -05:00
pic_value key, val, weak;
2015-06-09 05:50:46 -04:00
int n;
2016-02-19 10:03:16 -05:00
n = pic_get_args(pic, "o|o", &key, &val);
2015-06-09 05:50:46 -04:00
if (! pic_obj_p(pic, key)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "attempted to set a non-object key", 1, key);
2015-06-09 05:50:46 -04:00
}
2016-02-19 14:35:15 -05:00
weak = pic_closure_ref(pic, 0);
2015-06-09 05:50:46 -04:00
if (n == 1) {
2016-02-19 14:35:15 -05:00
if (! pic_weak_has(pic, weak, key)) {
return pic_false_value(pic);
}
return pic_cons(pic, key, pic_weak_ref(pic, weak, key));
2015-06-09 05:50:46 -04:00
} else {
2016-02-19 14:35:15 -05:00
if (pic_undef_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);
2015-06-09 05:50:46 -04:00
}
}
static pic_value
2016-02-10 07:57:20 -05:00
pic_weak_make_ephemeron(pic_state *pic)
2015-06-09 05:50:46 -04:00
{
pic_get_args(pic, "");
2016-02-19 14:35:15 -05:00
return pic_lambda(pic, weak_call, 1, pic_make_weak(pic));
2015-06-09 05:50:46 -04:00
}
void
2016-02-10 07:50:39 -05:00
pic_init_weak(pic_state *pic)
2015-06-09 05:50:46 -04:00
{
2016-02-10 07:57:20 -05:00
pic_defun(pic, "make-ephemeron", pic_weak_make_ephemeron);
2015-06-09 05:50:46 -04:00
}