summaryrefslogtreecommitdiff
path: root/python/generator.cpp
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2020-10-09 20:57:42 +0000
committerKyleMiles <krm504@nyu.edu>2020-10-19 18:17:44 +0000
commitc7ccd714ceeaabbe5ee682e361250c3cfd54f78f (patch)
treee9f9a6da19ca097f82b4a8f8c00ebba4ea14a590 /python/generator.cpp
parent3083903d0ee4ccd18099cb3315e0220c7f5993fb (diff)
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.
Diffstat (limited to 'python/generator.cpp')
-rw-r--r--python/generator.cpp12
1 files changed, 9 insertions, 3 deletions
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)