diff --git a/CMakeLists.txt b/CMakeLists.txt index f4eedf7..b0b900d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,17 @@ cmake_minimum_required(VERSION 3.18) include_guard(GLOBAL) set (CMAKE_CXX_STANDARD 17) +if(APPLE) + # cmake likes to build to the major version. + # rust likes to build to the minor version (to build libextism) + # attempt to set cmake to build to the minor version + execute_process( + COMMAND sw_vers -productVersion + OUTPUT_VARIABLE MACOS_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + set(CMAKE_OSX_DEPLOYMENT_TARGET ${MACOS_VERSION} CACHE STRING "Minimum OS X deployment version") +endif() # extism-cpp library project(extism-cpp VERSION 1.0.0 DESCRIPTION "C++ bindings for libextism") diff --git a/src/current_plugin.cpp b/src/current_plugin.cpp index 1422c4e..df78c67 100644 --- a/src/current_plugin.cpp +++ b/src/current_plugin.cpp @@ -1,6 +1,7 @@ #include "extism.hpp" #include +#include #include namespace extism { @@ -82,4 +83,8 @@ Val &CurrentPlugin::outputVal(size_t index) const { return this->outputs[index]; } +void *CurrentPlugin::hostContext() const { + return extism_current_plugin_host_context(this->pointer); +} + }; // namespace extism diff --git a/src/extism.hpp b/src/extism.hpp index aaf195c..834bff6 100644 --- a/src/extism.hpp +++ b/src/extism.hpp @@ -212,6 +212,7 @@ class CurrentPlugin { std::string_view inputStringView(size_t index = 0) const; const Val &inputVal(size_t index) const; Val &outputVal(size_t index) const; + void *hostContext() const; }; typedef std::function FunctionType; @@ -287,23 +288,28 @@ class Plugin { void config(std::string_view json); // Call a plugin - Buffer call(const char *func, const uint8_t *input, size_t inputLength) const; + Buffer call(const char *func, const uint8_t *input, size_t inputLength, + void *hostContext = nullptr) const; // Call a plugin function with std::vector input - Buffer call(const char *func, const std::vector &input) const; + Buffer call(const char *func, const std::vector &input, + void *hostContext = nullptr) const; // Call a plugin function with string input - Buffer call(const char *func, std::string_view input = "") const; + Buffer call(const char *func, std::string_view input = "", + void *hostContext = nullptr) const; // Call a plugin - Buffer call(const std::string &func, const uint8_t *input, - size_t inputLength) const; + Buffer call(const std::string &func, const uint8_t *input, size_t inputLength, + void *hostContext = nullptr) const; // Call a plugin function with std::vector input - Buffer call(const std::string &func, const std::vector &input) const; + Buffer call(const std::string &func, const std::vector &input, + void *hostContext = nullptr) const; // Call a plugin function with string input - Buffer call(const std::string &func, std::string_view input = "") const; + Buffer call(const std::string &func, std::string_view input = "", + void *hostContext = nullptr) const; // Returns true if the specified function exists bool functionExists(const char *func) const; diff --git a/src/plugin.cpp b/src/plugin.cpp index 5c84274..aeddb70 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -1,4 +1,5 @@ #include "extism.hpp" +#include #include namespace extism { @@ -73,9 +74,15 @@ void Plugin::config(std::string_view json) { } // Call a plugin -Buffer Plugin::call(const char *func, const uint8_t *input, - size_t inputLength) const { - int32_t rc = extism_plugin_call(this->plugin.get(), func, input, inputLength); +Buffer Plugin::call(const char *func, const uint8_t *input, size_t inputLength, + void *hostContext) const { + int32_t rc = -1; + if (hostContext != nullptr) { + rc = extism_plugin_call_with_host_context(this->plugin.get(), func, input, + inputLength, hostContext); + } else { + rc = extism_plugin_call(this->plugin.get(), func, input, inputLength); + } if (rc != 0) { const char *error = extism_plugin_error(this->plugin.get()); if (error == nullptr) { @@ -91,31 +98,34 @@ Buffer Plugin::call(const char *func, const uint8_t *input, } // Call a plugin function with std::vector input -Buffer Plugin::call(const char *func, const std::vector &input) const { - return this->call(func, input.data(), input.size()); +Buffer Plugin::call(const char *func, const std::vector &input, + void *hostContext) const { + return this->call(func, input.data(), input.size(), hostContext); } // Call a plugin function with string input -Buffer Plugin::call(const char *func, std::string_view input) const { +Buffer Plugin::call(const char *func, std::string_view input, + void *hostContext) const { return this->call(func, reinterpret_cast(input.data()), - input.size()); + input.size(), hostContext); } // Call a plugin Buffer Plugin::call(const std::string &func, const uint8_t *input, - size_t inputLength) const { - return this->call(func.c_str(), input, inputLength); + size_t inputLength, void *hostContext) const { + return this->call(func.c_str(), input, inputLength, hostContext); } // Call a plugin function with std::vector input -Buffer Plugin::call(const std::string &func, - const std::vector &input) const { - return this->call(func.c_str(), input); +Buffer Plugin::call(const std::string &func, const std::vector &input, + void *hostContext) const { + return this->call(func.c_str(), input, hostContext); } // Call a plugin function with string input -Buffer Plugin::call(const std::string &func, std::string_view input) const { - return this->call(func.c_str(), input); +Buffer Plugin::call(const std::string &func, std::string_view input, + void *hostContext) const { + return this->call(func.c_str(), input, hostContext); } // Returns true if the specified function exists diff --git a/test/test.cpp b/test/test.cpp index 30bb0fc..5cff2d7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -69,6 +69,10 @@ TEST(Plugin, HostFunction) { auto t = std::vector{ValType::ExtismValType_I64}; Function hello_world = Function("hello_world", t, t, [](CurrentPlugin plugin, void *user_data) { + uint64_t *ctx = (uint64_t *)plugin.hostContext(); + if (ctx != nullptr) { + ASSERT_EQ(*ctx, 12345); + } plugin.output(std::string("test")); }); auto functions = std::vector{ @@ -78,6 +82,10 @@ TEST(Plugin, HostFunction) { auto buf = plugin.call("count_vowels", "aaa"); ASSERT_EQ(buf.length, 4); ASSERT_EQ((std::string)buf, "test"); + uint64_t ctx = 12345; + auto buf1 = plugin.call("count_vowels", "aaa", (void *)&ctx); + ASSERT_EQ(buf1.length, buf.length); + ASSERT_EQ((std::string)buf, "test"); } void callThread(Plugin *plugin) {