From c7ccd714ceeaabbe5ee682e361250c3cfd54f78f Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Fri, 9 Oct 2020 20:57:42 +0000 Subject: Expose Template Simplifier to the API, Adds Tests, and Adds Rust String FFI support to Python (see note) This introduces the ability to receive and free string from Rust in Python. For it to work, your exposed function name needs to begin with "BNRust", and the generator will create the appropriate code for freeing string return values and arguments. --- python/generator.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'python/generator.cpp') diff --git a/python/generator.cpp b/python/generator.cpp index ff1b153d..4f329299 100644 --- a/python/generator.cpp +++ b/python/generator.cpp @@ -350,9 +350,12 @@ int main(int argc, char* argv[]) break; } } - if (name == "BNFreeString") + if (name == "BNFreeString" || name == "BNRustFreeString") stringArgument = false; + // Rust-allocated strings are deallocated differently + bool rustFFI = name.rfind("BNRust", 0) == 0; + bool callbackConvention = false; if (name == "BNAllocString") { @@ -377,7 +380,7 @@ int main(int argc, char* argv[]) for (auto& j : i.second->GetParameters()) { fprintf(out, "\t\t"); - if (name == "BNFreeString") + if (name == "BNFreeString" || name == "BNRustFreeString") { // BNFreeString expects a pointer to a string allocated by the core, so do not use // a c_char_p here, as that would be allocated by the Python runtime. This can @@ -440,7 +443,10 @@ int main(int argc, char* argv[]) else fprintf(out, "\tresult = %s(*args)\n", funcName.c_str()); fprintf(out, "\tstring = str(pyNativeStr(ctypes.cast(result, ctypes.c_char_p).value))\n"); - fprintf(out, "\tBNFreeString(result)\n"); + if (rustFFI) + fprintf(out, "\tBNRustFreeString(result)\n"); + else + fprintf(out, "\tBNFreeString(result)\n"); fprintf(out, "\treturn string\n"); } else if (pointerResult) -- cgit v1.3.1