From bd56945aaead936ff077bb33ddcaac5583cb1523 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 6 May 2017 09:00:22 -0700 Subject: ffi: handle copy flag in str type's in virtual. This solves the second issue described in parent commit. When a str type is passed in-out using (ptr str) in a struct or array, the struct or array is not picking up the new string. The pointer is freed, but the old object persists. * ffi.c (ffi_str_in): Function renamed to ffi_str_in. If the copy flag is true, retrieves a string from the pointer and that string is returned instead of the incoming one, mapping a null pointer to nil. Either way, the pointer is freed. Since ffi_ptr_out_in passes 1 for the copy flag, that ensures we extract the new string and plant it into the array. --- ffi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'ffi.c') diff --git a/ffi.c b/ffi.c index b295e989..c4d3ee23 100644 --- a/ffi.c +++ b/ffi.c @@ -564,10 +564,12 @@ static mem_t *ffi_cptr_alloc(struct txr_ffi_type *tft, val ptr, val self) return coerce(mem_t *, cptr_addr_of(ptr)); } -static val ffi_freeing_in(struct txr_ffi_type *tft, int copy, - mem_t *src, val obj, val self) +static val ffi_str_in(struct txr_ffi_type *tft, int copy, + mem_t *src, val obj, val self) { - mem_t **loc = coerce(mem_t **, src); + char **loc = coerce(char **, src); + if (copy) + obj = if2(*loc, string_utf8(*loc)); free(*loc); *loc = 0; return obj; @@ -1517,7 +1519,7 @@ static void ffi_init_types(void) &ffi_type_pointer, ffi_str_put, ffi_str_get); struct txr_ffi_type *tft = ffi_type_struct(type); - tft->in = ffi_freeing_in; + tft->in = ffi_str_in; ffi_typedef(str_s, type); } -- cgit v1.2.3