-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1871: chore: Add C examples r=syrusakbary a=jubianchi * [x] Add basic examples * [x] Add command to test the examples * [x] Update docs.wasmer.io with the new API changes. Namely: 4df7722 # Review - [ ] Add a short description of the the change to the CHANGELOG.md file Co-authored-by: jubianchi <[email protected]> Co-authored-by: Syrus <[email protected]> Co-authored-by: Syrus Akbary <[email protected]>
- Loading branch information
Showing
12 changed files
with
695 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
$(info Using provided WASMER_DIR=$(WASMER_DIR)) | ||
|
||
ifeq (,$(wildcard $(WASMER_DIR)/bin/wasmer)) | ||
CFLAGS = -g -I$(WASMER_DIR)/include | ||
LDFLAGS = -Wl,-rpath,$(WASMER_DIR)/lib | ||
LDLIBS = -L$(WASMER_DIR)/lib -lwasmer | ||
else | ||
CFLAGS = -g -I$(shell $(WASMER_DIR)/bin/wasmer config --includedir) | ||
LDFLAGS = -Wl,-rpath,$(shell $(WASMER_DIR)/bin/wasmer config --libdir) | ||
LDLIBS = $(shell $(WASMER_DIR)/bin/wasmer config --libs) | ||
endif | ||
|
||
$(info * CFLAGS: $(CFLAGS)) | ||
$(info * LDFLAGS: $(LDFLAGS)) | ||
$(info * LDLIBS: $(LDLIBS)) | ||
|
||
ALL = instance imports-exports exports-function exports-global memory | ||
|
||
.SILENT: instance instance.o | ||
instance: instance.o | ||
|
||
.SILENT: imports-exports imports-exports.o | ||
imports-exports: imports-exports.o | ||
|
||
.SILENT: exports-function exports-function.o | ||
exports-function: exports-function.o | ||
|
||
.SILENT: exports-global exports-global.o | ||
exports-global: exports-global.o | ||
|
||
.SILENT: memory memory.o | ||
memory: memory.o | ||
|
||
.PHONY: all | ||
all: $(ALL) | ||
|
||
.PHONY: run | ||
.SILENT: run | ||
run: $(ALL) | ||
set -o errexit; \ | ||
$(foreach example,$?,echo Running \"$(example)\" example; ./$(example); echo;) | ||
|
||
.SILENT: clean | ||
.PHONY: clean | ||
clean: | ||
$(foreach file,$(ALL),rm -f $(file).o $(file)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdio.h> | ||
#include "wasmer_wasm.h" | ||
|
||
int main(int argc, const char* argv[]) { | ||
const char *wat_string = | ||
"(module\n" | ||
" (type $sum_t (func (param i32 i32) (result i32)))\n" | ||
" (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)\n" | ||
" local.get $x\n" | ||
" local.get $y\n" | ||
" i32.add)\n" | ||
" (export \"sum\" (func $sum_f)))"; | ||
|
||
wasm_byte_vec_t wat; | ||
wasm_byte_vec_new(&wat, strlen(wat_string), wat_string); | ||
wasm_byte_vec_t wasm_bytes; | ||
wat2wasm(&wat, &wasm_bytes); | ||
|
||
printf("Creating the store...\n"); | ||
wasm_engine_t* engine = wasm_engine_new(); | ||
wasm_store_t* store = wasm_store_new(engine); | ||
|
||
printf("Compiling module...\n"); | ||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes); | ||
|
||
if (!module) { | ||
printf("> Error compiling module!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
wasm_byte_vec_delete(&wasm_bytes); | ||
|
||
printf("Creating imports...\n"); | ||
wasm_extern_vec_t import_object = WASM_EMPTY_VEC; | ||
|
||
printf("Instantiating module...\n"); | ||
wasm_instance_t* instance = wasm_instance_new(store, module, &import_object, NULL); | ||
|
||
if (!instance) { | ||
printf("> Error instantiating module!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Retrieving exports...\n"); | ||
wasm_extern_vec_t exports; | ||
wasm_instance_exports(instance, &exports); | ||
|
||
if (exports.size == 0) { | ||
printf("> Error accessing exports!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Retrieving the `sum` function...\n"); | ||
wasm_func_t* sum_func = wasm_extern_as_func(exports.data[0]); | ||
|
||
if (sum_func == NULL) { | ||
printf("> Failed to get the `sum` function!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Calling `sum` function...\n"); | ||
wasm_val_t args_val[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) }; | ||
wasm_val_t results_val[1] = { WASM_INIT_VAL }; | ||
wasm_val_vec_t args = WASM_ARRAY_VEC(args_val); | ||
wasm_val_vec_t results = WASM_ARRAY_VEC(results_val); | ||
|
||
if (wasm_func_call(sum_func, &args, &results)) { | ||
printf("> Error calling the `sum` function!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Results of `sum`: %d\n", results_val[0].of.i32); | ||
|
||
wasm_func_delete(sum_func); | ||
wasm_module_delete(module); | ||
wasm_instance_delete(instance); | ||
wasm_store_delete(store); | ||
wasm_engine_delete(engine); | ||
} |
Oops, something went wrong.