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"
2017-03-28 10:09:40 -04:00
#include "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
return obj_value(pic, 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
it = kh_get(weak, h, obj_ptr(pic, 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
it = kh_put(weak, h, obj_ptr(pic, 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, obj_ptr(pic, 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
it = kh_get(weak, h, obj_ptr(pic, 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
2017-03-28 18:11:27 -04:00
if (! 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_weak_ref(pic, weak, key);
2015-06-09 05:50:46 -04:00
} else {
2017-03-28 11:03:23 -04:00
if (pic_false_p(pic, val)) {
2016-02-19 14:35:15 -05:00
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
2017-03-31 02:59:22 -04:00
pic_weak_make_ephemeron_table(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
{
2017-03-31 02:59:22 -04:00
pic_defun(pic, "make-ephemeron-table", pic_weak_make_ephemeron_table);
2015-06-09 05:50:46 -04:00
}