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
3 changes: 3 additions & 0 deletions c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json-c/
json-c-build/
wasi-sdk-20.0/
25 changes: 25 additions & 0 deletions c/Makefile
Original file line number Diff line number Diff line change
@@ -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 *
13 changes: 13 additions & 0 deletions c/README.md
Original file line number Diff line number Diff line change
@@ -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"}'
31 changes: 31 additions & 0 deletions c/function_wasm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifdef __wasm32__
#include <string.h>
#include <stdlib.h>

__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
13 changes: 13 additions & 0 deletions c/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "macro.c"
#include <stdio.h>
#include "json-c/json.h"

int main()
{
return 0;
}

char *fl_main()
{
return "{\"payload\": \"Hello from C\"}";
}
27 changes: 27 additions & 0 deletions c/macro.c
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.