Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/current_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "extism.hpp"
#include <cstring>
#include <extism.h>
#include <string_view>

namespace extism {
Expand Down Expand Up @@ -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
20 changes: 13 additions & 7 deletions src/extism.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(CurrentPlugin, void *user_data)> FunctionType;
Expand Down Expand Up @@ -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<uint8_t> input
Buffer call(const char *func, const std::vector<uint8_t> &input) const;
Buffer call(const char *func, const std::vector<uint8_t> &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<uint8_t> input
Buffer call(const std::string &func, const std::vector<uint8_t> &input) const;
Buffer call(const std::string &func, const std::vector<uint8_t> &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;
Expand Down
38 changes: 24 additions & 14 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "extism.hpp"
#include <extism.h>
#include <json/json.h>

namespace extism {
Expand Down Expand Up @@ -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) {
Expand All @@ -91,31 +98,34 @@ Buffer Plugin::call(const char *func, const uint8_t *input,
}

// Call a plugin function with std::vector<uint8_t> input
Buffer Plugin::call(const char *func, const std::vector<uint8_t> &input) const {
return this->call(func, input.data(), input.size());
Buffer Plugin::call(const char *func, const std::vector<uint8_t> &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<const uint8_t *>(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<uint8_t> input
Buffer Plugin::call(const std::string &func,
const std::vector<uint8_t> &input) const {
return this->call(func.c_str(), input);
Buffer Plugin::call(const std::string &func, const std::vector<uint8_t> &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
Expand Down
8 changes: 8 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ TEST(Plugin, HostFunction) {
auto t = std::vector<ValType>{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<Function>{
Expand All @@ -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) {
Expand Down