Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add C API for host functions + support for C++, Python, Go, Node, OCaml #195

Merged
merged 50 commits into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8b54cf1
feat: add C api for host functions
zshipko Dec 10, 2022
aa1e8a0
feat: allow multiple user_datas in Python host functions
zshipko Dec 16, 2022
922d51c
checkpoint: update C example and Python examples to have more complic…
zshipko Dec 16, 2022
2e11f03
cleanup: fix order of python arguments, rename node Function to HostF…
zshipko Dec 17, 2022
4c05e95
fix: remove Context::current_plugin, restoring old access rules for c…
zshipko Dec 18, 2022
f72c805
feat: improve userData handling in node interface
zshipko Dec 20, 2022
df1cbfa
cleanup: Node free functions
zshipko Dec 20, 2022
d93ed5b
cleanup: Python/Node value handling
zshipko Dec 20, 2022
a1295a0
feat: get memory access working in node
zshipko Dec 20, 2022
7a1743d
cleanup: improve UserData
zshipko Dec 20, 2022
6011395
cleanup: make Function a struct with named fields
zshipko Dec 20, 2022
8fa9b43
feat: add UserData::is_null
zshipko Dec 20, 2022
9686f57
feat: initial support for host functions in go
zshipko Dec 21, 2022
8229b5b
cleanup: improve go host function API a little
zshipko Dec 21, 2022
166c3b4
fix: rust tests/lifetime issue
zshipko Dec 21, 2022
76229e5
cleanup: Add ExtismFunctionType
zshipko Dec 21, 2022
fbb6432
cleanup: remove requests from python example
zshipko Dec 21, 2022
3c92482
cleanup: add EXTISM_FUNCTION macro, clippy
zshipko Dec 21, 2022
bd25eff
feat: add host functions to c++
zshipko Dec 23, 2022
73b3887
feat: add macro to help define functions in go
zshipko Dec 23, 2022
857c048
feat: add host functions to ocaml
zshipko Dec 23, 2022
10dd9c1
cleanup: make user_data argument required
zshipko Dec 23, 2022
d15eb1f
cleanup: improve ocaml function interface
zshipko Dec 23, 2022
85e5b14
feat: implement Clone for Function
zshipko Dec 24, 2022
59f7389
cleanup: rename current_plugin OCaml functions
zshipko Dec 24, 2022
dc18c2d
fix: add old code.wasm back and new code-functions.wasm
zshipko Dec 24, 2022
42501bc
ci(node): try with registries disabled
zshipko Dec 24, 2022
a5319f3
ci(node): reorder finalization steps
zshipko Dec 24, 2022
5755b33
fix(node): remove broken pluginRegistry
zshipko Dec 24, 2022
e9e9f76
doc: add comments to new runtime functions
zshipko Dec 29, 2022
b57c122
cleanup: rename testing_123 import to hello_world
zshipko Dec 29, 2022
568c04b
cleanup: remove commented out code
zshipko Dec 30, 2022
dfb6a78
doc: add more comments
zshipko Dec 30, 2022
734f975
cleanup: unify host function API
zshipko Dec 30, 2022
245a17a
fix(ocaml): allow user_data to be any type
zshipko Dec 30, 2022
ceaf1d8
feat(js): add missing CurrentPlugin functions
zshipko Dec 30, 2022
13771f4
cleanup(ocaml): formatting
zshipko Dec 30, 2022
aa81196
cleanup(node): formatting
zshipko Dec 30, 2022
9589e42
cleanup(python): formatting
zshipko Dec 30, 2022
db7f055
fix(php): try to fix type name in generated header file
zshipko Jan 3, 2023
3aa2f7b
fix(php): update cbindgen struct/enum style
zshipko Jan 3, 2023
eaf0efc
fix(go): use correct pointer type
zshipko Jan 3, 2023
29bf955
cleanup: add functions argument to extism_plugin_{new,update} and rem…
zshipko Jan 3, 2023
6f647ef
cleanup(zig): update for host functions changes
zshipko Jan 3, 2023
e84c3bc
fix: more updates for API name change
zshipko Jan 3, 2023
3cc3263
cleanup: fix extism_plugin_new/extism_plugin_update function paramete…
zshipko Jan 4, 2023
61982e8
fix(cpp): update to const pointers
zshipko Jan 4, 2023
098b242
feat(cpp): allow lambdas to be used as host functions
zshipko Jan 7, 2023
d5788cd
cleanup(cpp): start adding some comments
zshipko Jan 7, 2023
44c93c9
feat(cpp): allow lambdas to be used as user_data free function
zshipko Jan 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions Makefile
Expand Up @@ -18,15 +18,12 @@ else
FEATURE_FLAGS=--features $(FEATURES)
endif


.PHONY: build
build:
cargo build --release $(FEATURE_FLAGS) --manifest-path libextism/Cargo.toml

lint:
cargo clippy --release --no-deps --manifest-path runtime/Cargo.toml

build:
cargo build --release $(FEATURE_FLAGS) --manifest-path libextism/Cargo.toml

debug:
RUSTFLAGS=-g $(MAKE) build

Expand Down
26 changes: 23 additions & 3 deletions c/main.c
Expand Up @@ -9,6 +9,21 @@
#include <sys/stat.h>
#include <unistd.h>

void hello_world(ExtismCurrentPlugin *plugin, const struct ExtismVal *inputs,
uint64_t n_inputs, struct ExtismVal *outputs,
uint64_t n_outputs, void *data) {
puts("Hello from C!");
puts(data);

ExtismSize ptr_offs = inputs[0].v.i64;

uint8_t *buf = extism_current_plugin_memory(plugin) + ptr_offs;
uint64_t length = extism_current_plugin_memory_length(plugin, ptr_offs);
fwrite(buf, length, 1, stdout);
fputc('\n', stdout);
outputs[0].v.i64 = inputs[0].v.i64;
}

uint8_t *read_file(const char *filename, size_t *len) {

FILE *fp = fopen(filename, "rb");
Expand Down Expand Up @@ -41,13 +56,17 @@ int main(int argc, char *argv[]) {
ExtismContext *ctx = extism_context_new();

size_t len = 0;
uint8_t *data = read_file("../wasm/code.wasm", &len);
ExtismPlugin plugin = extism_plugin_new(ctx, data, len, false);
uint8_t *data = read_file("../wasm/code-functions.wasm", &len);
ExtismValType inputs[] = {I64};
ExtismValType outputs[] = {I64};
ExtismFunction *f = extism_function_new("hello_world", inputs, 1, outputs, 1,
hello_world, "Hello, again!", NULL);
ExtismPlugin plugin = extism_plugin_new(ctx, data, len, &f, 1, true);
free(data);
if (plugin < 0) {
puts(extism_error(ctx, -1));
exit(1);
}

assert(extism_plugin_call(ctx, plugin, "count_vowels", (uint8_t *)argv[1],
strlen(argv[1])) == 0);
ExtismSize out_len = extism_plugin_output_length(ctx, plugin);
Expand All @@ -56,6 +75,7 @@ int main(int argc, char *argv[]) {
write(STDOUT_FILENO, "\n", 1);

extism_plugin_free(ctx, plugin);
extism_function_free(f);
extism_context_free(ctx);
return 0;
}
4 changes: 2 additions & 2 deletions cpp/Makefile
@@ -1,14 +1,14 @@
FLAGS=`pkg-config --cflags --libs jsoncpp gtest` -lextism -lpthread

build-example:
$(CXX) -std=c++11 -o example -I. example.cpp $(FLAGS)
$(CXX) -std=c++14 -o example -I. example.cpp $(FLAGS)

.PHONY: example
example: build-example
./example

build-test:
$(CXX) -std=c++11 -o test/test -I. test/test.cpp $(FLAGS)
$(CXX) -std=c++14 -o test/test -I. test/test.cpp $(FLAGS)

.PHONY: test
test: build-test
Expand Down
21 changes: 19 additions & 2 deletions cpp/example.cpp
Expand Up @@ -14,10 +14,27 @@ std::vector<uint8_t> read(const char *filename) {
}

int main(int argc, char *argv[]) {
auto wasm = read("../wasm/code.wasm");
auto wasm = read("../wasm/code-functions.wasm");
Context context = Context();
std::string tmp = "Testing";

Plugin plugin = context.plugin(wasm);
// A lambda can be used as a host function
auto hello_world = [&tmp](CurrentPlugin plugin,
const std::vector<Val> &inputs,
std::vector<Val> &outputs, void *user_data) {
std::cout << "Hello from C++" << std::endl;
std::cout << (const char *)user_data << std::endl;
std::cout << tmp << std::endl;
outputs[0].v = inputs[0].v;
};

std::vector<Function> functions = {
Function("hello_world", {ValType::I64}, {ValType::I64}, hello_world,
(void *)"Hello again!",
[](void *x) { std::cout << "Free user data" << std::endl; }),
};

Plugin plugin = context.plugin(wasm, true, functions);

const char *input = argc > 1 ? argv[1] : "this is a test";
ExtismSize length = strlen(input);
Expand Down