[DGD] LPC_mapping in kfun extension

Felix A. Croes felix at dworkin.nl
Tue Aug 1 10:24:26 CEST 2017


"nikoroleva ." <natasha.i.koroleva at gmail.com> wrote:

> I have created this kfun extension to return a mapping and in the
> returned mapping the key is the value. Is it my mistake?
>
> https://gist.github.com/november-kilo/27d9f2308d6ce8a05e446db82c0ff94c
>
> LPC_dataspace data;
> LPC_mapping mapping;
> LPC_value mapping_key, mapping_value;
> LPC_string key_str, val_str;
> char key[100];
> char val[5];
> data = lpc_frame_dataspace(f);
> mapping_key = lpc_value_temp(data);
> mapping_value = lpc_value_temp(data);
> sprintf(key, "key_%d", 1);
> sprintf(val, "val_%d", rand());
> key_str = lpc_string_new(data, key, strlen(key));
> val_str = lpc_string_new(data, val, strlen(val));
> lpc_string_putval(mapping_key, key_str);
> lpc_string_putval(mapping_value, val_str);
> mapping = lpc_mapping_new(data);
> lpc_mapping_assign(data, mapping, mapping_key, mapping_value);
> lpc_mapping_putval(retval, mapping);


Interesting.  What goes wrong here is that lpc_value_temp() does not
generate a new scratch value, but reuses the same one every time.
Obviously, to create a mapping key/value pair you need at least two
distinct scratch values, something which I overlooked.

Actually, you do have another value available, the return value.  So
the following should work: replace

    mapping_value = lpc_value_temp(data);

with

    mapping_value = return_value;

That will keep you going for now, until you reach the point where you
need 3 distinct values. :)

Regards,
Felix Croes



More information about the DGD mailing list