picrin/extlib/benz/attr.c

49 lines
1022 B
C
Raw Normal View History

2014-09-24 20:44:21 -04:00
#include "picrin.h"
struct pic_dict *
pic_attr(pic_state *pic, pic_value obj)
{
2015-06-09 05:31:46 -04:00
struct pic_dict *dict;
2014-09-24 20:44:21 -04:00
2015-06-09 05:31:46 -04:00
if (! pic_obj_p(obj)) {
2014-09-24 20:44:21 -04:00
pic_errorf(pic, "attribute: expected heap object, but got immediate value ~s", obj);
}
2015-06-09 05:31:46 -04:00
if (! pic_reg_has(pic, pic->attrs, pic_ptr(obj))) {
dict = pic_make_dict(pic);
2014-09-24 20:44:21 -04:00
2015-06-09 05:31:46 -04:00
pic_reg_set(pic, pic->attrs, pic_ptr(obj), pic_obj_value(dict));
2014-09-24 20:44:21 -04:00
2015-06-09 05:31:46 -04:00
return dict;
2014-09-24 20:44:21 -04:00
}
2015-06-09 05:31:46 -04:00
return pic_dict_ptr(pic_reg_ref(pic, pic->attrs, pic_ptr(obj)));
2014-09-24 20:44:21 -04:00
}
pic_value
pic_attr_ref(pic_state *pic, pic_value obj, const char *key)
{
return pic_dict_ref(pic, pic_attr(pic, obj), pic_intern_cstr(pic, key));
2014-09-24 20:44:21 -04:00
}
void
pic_attr_set(pic_state *pic, pic_value obj, const char *key, pic_value v)
{
pic_dict_set(pic, pic_attr(pic, obj), pic_intern_cstr(pic, key), v);
2014-09-24 20:44:21 -04:00
}
static pic_value
pic_attr_attribute(pic_state *pic)
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_obj_value(pic_attr(pic, obj));
}
void
pic_init_attr(pic_state *pic)
{
pic_defun(pic, "attribute", pic_attr_attribute);
}