From b089337ee8cdf59dee581b02af3878c3de9c1918 Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 7 Mar 2024 14:42:15 +0200 Subject: [PATCH] FFI: fix "&{*}" --- src/ffi.c | 2 +- test/ffi/ffiTest.c | 11 +++++++++++ test/ffi/test.bqn | 6 ++++++ test/ffi/test.expected | 2 ++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ffi.c b/src/ffi.c index f053bfcd..131cc0c7 100644 --- a/src/ffi.c +++ b/src/ffi.c @@ -729,7 +729,7 @@ void genObj(B o, B c, void* ptr, B* sourceObjs) { // doesn't consume void* dataStruct = dataAll+sizeof(usz); *((usz*)dataAll) = ia; SGetU(c) - for (usz i = 0; i < ia; i++) genObj(t->a[0].o, GetU(c, i), dataStruct + elSz*i, sourceObjs); + for (usz i = 0; i < ia; i++) genObj(t->a[0].o, GetU(c, i), dataStruct + elSz*i, NULL); *(void**)ptr = dataStruct; *sourceObjs = vec_addN(*sourceObjs, tag(TOBJ(dataAll), OBJ_TAG)); } diff --git a/test/ffi/ffiTest.c b/test/ffi/ffiTest.c index 50463c5e..455453b1 100644 --- a/test/ffi/ffiTest.c +++ b/test/ffi/ffiTest.c @@ -334,4 +334,15 @@ StructOfPtrs operateOnStructOfPtrs(StructOfPtrs arg, StructOfPtrs ptr[2]) { .ptr2 = ptr[0].ptr1, .ptrs = {arg.ptrs[0], ptr[1].ptrs[1]} }; +} + +typedef struct PtrWrapper { + int32_t x; + char* ptr; +} PtrWrapper; +void updatePointerWithinPointer(PtrWrapper* ptr) { + for (int i = 0; i < 5; i++) { + ptr[i].x+= i*10; + ptr[i].ptr+= i; + } } \ No newline at end of file diff --git a/test/ffi/test.bqn b/test/ffi/test.bqn index fc749a35..94272e51 100644 --- a/test/ffi/test.bqn +++ b/test/ffi/test.bqn @@ -131,6 +131,12 @@ Section "# Struct of pointers" sop ← "{*,*i32,[2]*}" f ← "lib.so"•FFI ⟨sop, "operateOnStructOfPtrs", sop, "[2]"∾sop⟩ •Show {(𝕩.Cast "u8").Sub p}⚇0 F {(p.Add 𝕩).Cast ""}⚇0 ⟨1,2,3‿4⟩⊸+⚇0 ⟨10, 20‿30⟩ + + f ↩ "lib.so" •FFI "&"‿"updatePointerWithinPointer"‿">&{i32,*}" + •Show {x‿y: x ⋈ (y.Cast "u8").Sub p}¨ F {⟨𝕩×1000, p.Add 𝕩×100⟩}¨ ↕5 + + f ↩ "lib.so" •FFI "&"‿"updatePointerWithinPointer"‿">&{i32,*u8}" + •Show {x‿y: x ⋈ y.Sub p}¨ F {⟨𝕩×1000, (p.Add 𝕩×100).Cast ""⟩}¨ ↕5 } diff --git a/test/ffi/test.expected b/test/ffi/test.expected index 1fc364e0..22509783 100644 --- a/test/ffi/test.expected +++ b/test/ffi/test.expected @@ -136,3 +136,5 @@ text # Struct of pointers ⟨ 12 21 ⟨ 13 34 ⟩ ⟩ +⟨ ⟨ 0 0 ⟩ ⟨ 1010 101 ⟩ ⟨ 2020 202 ⟩ ⟨ 3030 303 ⟩ ⟨ 4040 404 ⟩ ⟩ +⟨ ⟨ 0 0 ⟩ ⟨ 1010 101 ⟩ ⟨ 2020 202 ⟩ ⟨ 3030 303 ⟩ ⟨ 4040 404 ⟩ ⟩