forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
623 additions
and
0 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
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,27 @@ | ||
CFLAGS = -g -I$(shell $(WASMER_C_API)/bin/wasmer config --includedir) | ||
LDFLAGS = -Wl,-rpath,$(shell $(WASMER_C_API)/bin/wasmer config --libdir) | ||
LDLIBS = $(shell $(WASMER_C_API)/bin/wasmer config --libs) | ||
|
||
.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 | ||
|
||
.SILENT: clean | ||
.PHONY: clean | ||
clean: | ||
rm -f \ | ||
instance instance.o \ | ||
imports-exports imports-exports.o \ | ||
exports-function exports-function.o \ | ||
memory memory.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,83 @@ | ||
#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); | ||
|
||
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); | ||
} |
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,116 @@ | ||
#include <stdio.h> | ||
#include "wasmer_wasm.h" | ||
|
||
int main(int argc, const char* argv[]) { | ||
const char *wat_string = | ||
"(module\n" | ||
" (global $one (export \"one\") f32 (f32.const 1))\n" | ||
" (global $some (export \"some\") (mut f32) (f32.const 0))\n" | ||
" (func (export \"get_one\") (result f32) (global.get $one))\n" | ||
" (func (export \"get_some\") (result f32) (global.get $some))\n" | ||
" (func (export \"set_some\") (param f32) (global.set $some (local.get 0))))"; | ||
|
||
wasm_byte_vec_t wat; | ||
wasm_byte_vec_new(&wat, strlen(wat_string), wat_string); | ||
wasm_byte_vec_t* wasm_bytes = wat2wasm(&wat); | ||
|
||
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; | ||
} | ||
|
||
wasm_global_t* one = wasm_extern_as_global(exports.data[0]); | ||
|
||
if (one == NULL) { | ||
printf("> Failed to get the `one` global!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
wasm_global_t* some = wasm_extern_as_global(exports.data[1]); | ||
|
||
if (some == NULL) { | ||
printf("> Failed to get the `some` global!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Getting globals types information...\n"); | ||
wasm_globaltype_t* one_type = wasm_global_type(one); | ||
wasm_globaltype_t* some_type = wasm_global_type(some); | ||
|
||
wasm_mutability_t one_mutability = wasm_globaltype_mutability(one_type); | ||
const wasm_valtype_t* one_content = wasm_globaltype_content(one_type); | ||
wasm_valkind_t one_kind = wasm_valtype_kind(one_content); | ||
|
||
wasm_mutability_t some_mutability = wasm_globaltype_mutability(some_type); | ||
const wasm_valtype_t* some_content = wasm_globaltype_content(some_type); | ||
wasm_valkind_t some_kind = wasm_valtype_kind(some_content); | ||
|
||
printf("`one` type: %s %hhu\n", one_mutability == WASM_CONST ? "const" : "", one_kind); | ||
printf("`some` type: %s %hhu\n", some_mutability == WASM_CONST ? "const" : "", some_kind); | ||
|
||
printf("Getting global values..."); | ||
wasm_val_t one_value; | ||
wasm_global_get(one, &one_value); | ||
printf("`one` value: %.1f\n", one_value.of.f32); | ||
|
||
wasm_val_t some_value; | ||
wasm_global_get(some, &some_value); | ||
printf("`some` value: %.1f\n", some_value.of.f32); | ||
|
||
printf("Setting global values...\n"); | ||
wasm_val_t one_set_value = WASM_F32_VAL(42); | ||
wasm_global_set(one, &one_set_value); | ||
|
||
int error_length = wasmer_last_error_length(); | ||
if (error_length > 0) { | ||
char *error_message = malloc(error_length); | ||
wasmer_last_error_message(error_message, error_length); | ||
|
||
printf("Attempted to set an immutable global: `%s`\n", error_message); | ||
} | ||
|
||
wasm_val_t some_set_value = WASM_F32_VAL(21); | ||
wasm_global_set(some, &some_set_value); | ||
printf("`some` value: %.1f\n", some_value.of.f32); | ||
|
||
wasm_global_delete(some); | ||
wasm_global_delete(one); | ||
wasm_module_delete(module); | ||
wasm_instance_delete(instance); | ||
wasm_store_delete(store); | ||
wasm_engine_delete(engine); | ||
} |
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,133 @@ | ||
#include <stdio.h> | ||
#include "wasmer_wasm.h" | ||
|
||
wasm_trap_t* host_func_callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) { | ||
printf("Calling back...\n> "); | ||
|
||
wasm_val_t val = WASM_I32_VAL(42); | ||
wasm_val_copy(&results->data[0], &val); | ||
|
||
wasm_val_delete(&val); | ||
|
||
return NULL; | ||
} | ||
|
||
int main(int argc, const char* argv[]) { | ||
const char *wat_string = | ||
"(module\n" | ||
" (func $host_function (import \"\" \"host_function\") (result i32))\n" | ||
" (global $host_global (import \"env\" \"host_global\") i32)\n" | ||
" (func $function (export \"guest_function\") (result i32) (global.get $global))\n" | ||
" (global $global (export \"guest_global\") i32 (i32.const 42))\n" | ||
" (table $table (export \"guest_table\") 1 1 funcref)\n" | ||
" (memory $memory (export \"guest_memory\") 1))"; | ||
|
||
wasm_byte_vec_t wat; | ||
wasm_byte_vec_new(&wat, strlen(wat_string), wat_string); | ||
wasm_byte_vec_t* wasm_bytes = wat2wasm(&wat); | ||
|
||
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 the imported function...\n"); | ||
wasm_functype_t* host_func_type = wasm_functype_new_0_1(wasm_valtype_new_i32()); | ||
wasm_func_t* host_func = wasm_func_new(store, host_func_type, host_func_callback); | ||
wasm_functype_delete(host_func_type); | ||
|
||
printf("Creating the imported global...\n"); | ||
wasm_globaltype_t* host_global_type = wasm_globaltype_new(wasm_valtype_new(WASM_F32), WASM_CONST); | ||
wasm_val_t host_global_val = WASM_I32_VAL(42); | ||
wasm_global_t* host_global = wasm_global_new(store, host_global_type, &host_global_val); | ||
wasm_globaltype_delete(host_global_type); | ||
|
||
wasm_extern_t* externs[] = { | ||
wasm_func_as_extern(host_func), | ||
wasm_global_as_extern(host_global) | ||
}; | ||
|
||
wasm_extern_vec_t import_object = WASM_ARRAY_VEC(externs); | ||
|
||
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 exported function...\n"); | ||
wasm_func_t* func = wasm_extern_as_func(exports.data[0]); | ||
|
||
if (func == NULL) { | ||
printf("> Failed to get the exported function!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Got the exported function: %p\n", func); | ||
|
||
printf("Retrieving the exported global...\n"); | ||
wasm_global_t* global = wasm_extern_as_global(exports.data[1]); | ||
|
||
if (global == NULL) { | ||
printf("> Failed to get the exported global!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Got the exported global: %p\n", global); | ||
|
||
printf("Retrieving the exported table...\n"); | ||
wasm_table_t* table = wasm_extern_as_table(exports.data[2]); | ||
|
||
if (table == NULL) { | ||
printf("> Failed to get the exported table!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Got the exported table: %p\n", table); | ||
|
||
printf("Retrieving the exported memory...\n"); | ||
wasm_memory_t* memory = wasm_extern_as_memory(exports.data[3]); | ||
|
||
if (memory == NULL) { | ||
printf("> Failed to get the exported memory!\n"); | ||
|
||
return 1; | ||
} | ||
|
||
printf("Got the exported memory: %p\n", memory); | ||
|
||
wasm_func_delete(func); | ||
wasm_global_delete(global); | ||
wasm_table_delete(table); | ||
wasm_memory_delete(memory); | ||
wasm_module_delete(module); | ||
wasm_instance_delete(instance); | ||
wasm_store_delete(store); | ||
wasm_engine_delete(engine); | ||
} |
Oops, something went wrong.