summaryrefslogtreecommitdiffstats
path: root/struct.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-10-06 21:13:45 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-10-06 21:13:45 -0700
commit8beabcd33ac984ee454f0871ffd45b940e279732 (patch)
tree624c01ce0d390ef86975d88033249d44ab6c0417 /struct.c
parent829f02b346d4060e64ec5847cbd0f066edd4a144 (diff)
downloadtxr-8beabcd33ac984ee454f0871ffd45b940e279732.tar.gz
txr-8beabcd33ac984ee454f0871ffd45b940e279732.tar.bz2
txr-8beabcd33ac984ee454f0871ffd45b940e279732.zip
New function: replace-struct.
* struct.c (replace_struct): New function. (struct_init): Register replace_struct intrinsic. * struct.h (replace_struct): Declared. * txr.1: documented replace-struct.
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/struct.c b/struct.c
index 43a30a65..064d0e21 100644
--- a/struct.c
+++ b/struct.c
@@ -111,6 +111,7 @@ void struct_init(void)
reg_fun(intern(lit("super"), user_package), func_n1(super));
reg_fun(intern(lit("make-struct"), user_package), func_n2v(make_struct));
reg_fun(intern(lit("copy-struct"), user_package), func_n1(copy_struct));
+ reg_fun(intern(lit("replace-struct"), user_package), func_n2(replace_struct));
reg_fun(intern(lit("clear-struct"), user_package), func_n2o(clear_struct, 1));
reg_fun(intern(lit("slot"), user_package), func_n2(slot));
reg_fun(intern(lit("slotset"), user_package), func_n3(slotset));
@@ -408,6 +409,22 @@ val clear_struct(val strct, val value)
return strct;
}
+val replace_struct(val target, val source)
+{
+ const val self = lit("replace-struct");
+ struct struct_inst *tsi = struct_handle(target, self);
+ struct struct_inst *ssi = struct_handle(source, self);
+ struct struct_type *sst = coerce(struct struct_type *, ssi->type->co.handle);
+ cnum nslots = sst->nslots;
+ size_t size = offsetof(struct struct_inst, slot) + sizeof (val) * nslots;
+ struct struct_inst *ssi_copy = coerce(struct struct_inst *, chk_malloc(size));
+ memcpy(ssi_copy, ssi, size);
+ free(tsi);
+ target->co.handle = coerce(mem_t *, ssi_copy);
+ target->co.cls = source->co.cls;
+ return target;
+}
+
static int cache_set_lookup(slot_cache_entry_t *set, cnum id)
{
if (set[0].id == id)