diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..a47d08f --- /dev/null +++ b/c/.gitignore @@ -0,0 +1,3 @@ +json-c/ +json-c-build/ +wasi-sdk-20.0/ \ No newline at end of file diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..0af0d33 --- /dev/null +++ b/c/Makefile @@ -0,0 +1,25 @@ +# Makefile for making the game of life program + +CC = ./wasi-sdk-20.0/bin/clang +CFLAGS = -Wl,--export-all --sysroot wasi-sdk-20.0/share/wasi-sysroot/ -o hello.wasm -v + + +CFLAGS += $(shell pkg-config --cflags json-c) +LDFLAGS += $(shell pkg-config --libs json-c) + + +hello : hello.c + $(CC) $(CFLAGS) hello.c $(LDFLAGS) + +default: json + +json: json_type.c + $(CC) -O3 -o json json_type.c -lm + +.PHONY: clean +clean: + rm -Rf *.o lib/*.o json *.dSYM + +.PHONY: package +package: + tar -cvzf json.tar * \ No newline at end of file diff --git a/c/README.md b/c/README.md new file mode 100644 index 0000000..e7216da --- /dev/null +++ b/c/README.md @@ -0,0 +1,13 @@ +# Notes + +## compile with sdk + +clang --target=wasm32-unknown-wasi --sysroot wasi-sysroot hello.c -o hello.wasm + +./wasi-sdk-20.0/bin/clang -mexec-model=reactor -Wl,--export-all --sysroot wasi-sdk-20.0/share/wasi-sysroot/ hello.c -o hello.wasm + +## todo + +https://github.com/WebAssembly/wasi-sdk + +go run cmd/fl/main.go fn invoke hello9 --json='{"input":"test"}' diff --git a/c/function_wasm.c b/c/function_wasm.c new file mode 100644 index 0000000..2ccc89e --- /dev/null +++ b/c/function_wasm.c @@ -0,0 +1,31 @@ +#ifdef __wasm32__ +#include +#include + +__attribute__((import_module("fl_imps"), import_name("__console_log"))) void __console_log(char *ptr, size_t len); +__attribute__((import_module("fl_imps"), import_name("__get_input_data"))) void __get_input_data(char *ptr); +__attribute__((import_module("fl_imps"), import_name("__insert_error"))) void __insert_error(char *ptr, size_t len); +__attribute__((import_module("fl_imps"), import_name("__insert_response"))) void __insert_response(char *ptr, size_t len); + +void console_log(char *s) +{ + __console_log(s, strlen(s)); +} + +char *get_input_data(int req_len) +{ + char *req_ptr = (char *)malloc(sizeof(char) * req_len); + __get_input_data(req_ptr); + return req_ptr; +} + +void insert_response(char *s) +{ + __insert_response(s, strlen(s)); +} + +void insert_error(char *s) +{ + __insert_error(s, strlen(s)); +} +#endif \ No newline at end of file diff --git a/c/hello.c b/c/hello.c new file mode 100644 index 0000000..1a9b129 --- /dev/null +++ b/c/hello.c @@ -0,0 +1,13 @@ +#include "macro.c" +#include +#include "json-c/json.h" + +int main() +{ + return 0; +} + +char *fl_main() +{ + return "{\"payload\": \"Hello from C\"}"; +} \ No newline at end of file diff --git a/c/macro.c b/c/macro.c new file mode 100644 index 0000000..49f9802 --- /dev/null +++ b/c/macro.c @@ -0,0 +1,27 @@ +#define VERSION 1 +__attribute__((export_name("__runtime_version"))) int __runtime_version() +{ + return VERSION; +} + +#ifdef __wasm32__ +#include "function_wasm.c" +#include "json-c/json.h" + +char *fl_main(); +void console_log(char *input); +struct json_object *json_tokener_parse(const char *str); + +__attribute__((export_name("__invoke"))) char *__invoke(int size) +{ + char *input = get_input_data(size); + console_log(input); + json_object *obj = json_tokener_parse(input); + json_object *field = json_object_object_get(obj, "input"); + char *strField = json_object_to_json_string(field); + console_log(strField); + char *response = fl_main(); + insert_response(response); + return 0; +} +#endif \ No newline at end of file diff --git a/README.md b/rs/README.md similarity index 100% rename from README.md rename to rs/README.md diff --git a/examples/http_call.rs b/rs/examples/http_call.rs similarity index 100% rename from examples/http_call.rs rename to rs/examples/http_call.rs diff --git a/examples/simple_fn.rs b/rs/examples/simple_fn.rs similarity index 100% rename from examples/simple_fn.rs rename to rs/examples/simple_fn.rs diff --git a/fl-wasm-rs/Cargo.toml b/rs/fl-wasm-rs/Cargo.toml similarity index 100% rename from fl-wasm-rs/Cargo.toml rename to rs/fl-wasm-rs/Cargo.toml diff --git a/fl-wasm-rs/README.md b/rs/fl-wasm-rs/README.md similarity index 100% rename from fl-wasm-rs/README.md rename to rs/fl-wasm-rs/README.md diff --git a/fl-wasm-rs/src/error.rs b/rs/fl-wasm-rs/src/error.rs similarity index 100% rename from fl-wasm-rs/src/error.rs rename to rs/fl-wasm-rs/src/error.rs diff --git a/fl-wasm-rs/src/function_wasm.rs b/rs/fl-wasm-rs/src/function_wasm.rs similarity index 100% rename from fl-wasm-rs/src/function_wasm.rs rename to rs/fl-wasm-rs/src/function_wasm.rs diff --git a/fl-wasm-rs/src/http.rs b/rs/fl-wasm-rs/src/http.rs similarity index 100% rename from fl-wasm-rs/src/http.rs rename to rs/fl-wasm-rs/src/http.rs diff --git a/fl-wasm-rs/src/lib.rs b/rs/fl-wasm-rs/src/lib.rs similarity index 100% rename from fl-wasm-rs/src/lib.rs rename to rs/fl-wasm-rs/src/lib.rs diff --git a/fl-wasm-rs/src/prelude.rs b/rs/fl-wasm-rs/src/prelude.rs similarity index 100% rename from fl-wasm-rs/src/prelude.rs rename to rs/fl-wasm-rs/src/prelude.rs diff --git a/macros/Cargo.toml b/rs/macros/Cargo.toml similarity index 100% rename from macros/Cargo.toml rename to rs/macros/Cargo.toml diff --git a/macros/README.md b/rs/macros/README.md similarity index 100% rename from macros/README.md rename to rs/macros/README.md diff --git a/macros/src/lib.rs b/rs/macros/src/lib.rs similarity index 100% rename from macros/src/lib.rs rename to rs/macros/src/lib.rs