mirror of
https://github.com/extism/extism.git
synced 2026-01-10 22:37:58 -05:00
This PR updates `extism_output_get` to return an actual pointer to the output value (`const uint8_t* extism_output_get(PluginIndex plugin)` instead of `void extism_output_get(PluginIndex plugin, uint8_t *buffer, uint64_t length)`), this pointer will only be valid until the next call, but it makes it possible to access the output data without copying. The input buffer is also not copied and the same issue applies: the input buffer must not change during `call`. Co-authored-by: Steve Manuel <steve@dylib.so>
57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
extern "C" {
|
|
#include "extism.h"
|
|
}
|
|
|
|
namespace extism {
|
|
class Error : public std::exception {
|
|
private:
|
|
std::string message;
|
|
|
|
public:
|
|
Error(std::string msg) : message(msg) {}
|
|
const char *what() { return message.c_str(); }
|
|
};
|
|
|
|
class Plugin {
|
|
ExtismPlugin plugin;
|
|
|
|
public:
|
|
Plugin(const uint8_t *wasm, size_t length, bool with_wasi = false) {
|
|
this->plugin = extism_plugin_register(wasm, length, with_wasi);
|
|
if (this->plugin < 0) {
|
|
throw Error("Unable to load plugin");
|
|
}
|
|
}
|
|
|
|
Plugin(const std::string &s, bool with_wasi = false)
|
|
: Plugin((const uint8_t *)s.c_str(), s.size(), with_wasi) {}
|
|
Plugin(const std::vector<uint8_t> &s, bool with_wasi = false)
|
|
: Plugin(s.data(), s.size(), with_wasi) {}
|
|
|
|
std::vector<uint8_t> call(const std::string &func,
|
|
std::vector<uint8_t> input) {
|
|
|
|
int32_t rc =
|
|
extism_call(this->plugin, func.c_str(), input.data(), input.size());
|
|
if (rc != 0) {
|
|
const char *error = extism_error(this->plugin);
|
|
if (error == nullptr) {
|
|
throw Error("extism_call failed");
|
|
}
|
|
|
|
throw Error(error);
|
|
}
|
|
|
|
ExtismSize length = extism_output_length(this->plugin);
|
|
const uint8_t *ptr = extism_output_get(this->plugin);
|
|
std::vector<uint8_t> out = std::vector<uint8_t>(ptr, ptr + length);
|
|
return out;
|
|
}
|
|
};
|
|
} // namespace extism
|