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(c-api) Update wasm-c-api repository #1699

Merged
merged 30 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1872970
feat(c-api) Update `wasm-c-api` repository.
Hywan Oct 9, 2020
8b38dd5
feat(c-api) Implement `wasm_val_vec_t`.
Hywan Oct 12, 2020
d8dcb41
feat(c-api) Update the definitions of `wasm_func_callback*_t`.
Hywan Oct 12, 2020
5dffd97
feat(c-api) Update `wasm_func_new`.
Hywan Oct 12, 2020
7f09838
faet(c-api) Update `wasm_func_new_with_env`.
Hywan Oct 12, 2020
e9cd710
feat(c-api) Update `wasm_func_call`.
Hywan Oct 12, 2020
fa7fe83
feat(c-api) Update `wasm_instance_new`.
Hywan Oct 12, 2020
fea156d
feat(c-api) Update `wasi_get_imports` to use a `wasm_extern_vec_t`…
Hywan Oct 12, 2020
202ffe7
fix(c-api) Fix `wasm_func_call` when params are empty.
Hywan Oct 12, 2020
35f8e17
Merge branch 'master' into feat-c-api-update-wasm-h
Hywan Oct 12, 2020
4584a53
fix(c-api) Remove `rustc` warnings.
Hywan Oct 12, 2020
5bf9055
test(c-api) Fix the `early-exit` test.
Hywan Oct 12, 2020
4186d1c
test(c-api) Use `wasm_extern_vec_t` for `wasm_get_imports`.
Hywan Oct 12, 2020
0b4ef7a
test(c-api) Don't pass `NULL` for zero args/results to `wasm_func_call`!
Hywan Oct 12, 2020
437426d
feat(c-api) Update `wasmer_wasm.h`.
Hywan Oct 12, 2020
122ba83
Merge branch 'master' into feat-c-api-update-wasm-h
Hywan Oct 12, 2020
2023016
doc(changelog) Add #1699.
Hywan Oct 12, 2020
bf389f9
Merge branch 'master' into feat-c-api-update-wasm-h
Hywan Oct 12, 2020
4463294
doc(changelog) Add #1685.
Hywan Oct 12, 2020
e29ea25
feat(c-api) Simplify code with `unwrap_or_default`.
Hywan Oct 16, 2020
d7d40e5
Merge branch 'master' into feat-c-api-update-wasm-h
Hywan Oct 16, 2020
ddf2459
chore(changelog) Fix merge.
Hywan Oct 16, 2020
4f9932c
feat(cli) Update the `crate_exe_main.c` file to the latest Wasm C API.
Hywan Oct 16, 2020
5601ecb
test(integration) Add `-x c` to force `clang++` to treat code as C.
Hywan Oct 16, 2020
cb5afa1
test(integration) Update to use the latest Wasm C API.
Hywan Oct 16, 2020
2fc1aaa
chore(test) Format code.
Hywan Oct 16, 2020
3b05c26
fix(c-api) Fix last edit of the `wasmer_create_exe_main.c` file.
Hywan Oct 16, 2020
1cdb5b5
fix(c-api) Fix another typo. Damn.
Hywan Oct 16, 2020
9955da1
test: Remove a warning fix for Windows.
Hywan Oct 16, 2020
a73e457
Merge branch 'master' into feat-c-api-update-wasm-h
Hywan Oct 19, 2020
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
1 change: 1 addition & 0 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Currently, known implementations of this API are included in
* V8 natively (both C and C++)
* Wabt (only C?)
* Wasmtime (only C?)
* [Wasmer](https://github.com/wasmerio/wasmer/tree/master/lib/c-api) (only C, C++ coming soon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but I want to put this idea out into the universe before I forget:

When doing the release we have c-api/docs/deprecated and we currently package that as README.md in the C API that we distribute...

We should probably make a note that it's the deprecated API and file an issue to start building docs for Wasmer with the Wasm C API!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely!



### TODO
Expand Down
33 changes: 16 additions & 17 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ void wasm_val_print(wasm_val_t val) {

// A function to be called from Wasm code.
own wasm_trap_t* print_callback(
const wasm_val_t args[], wasm_val_t results[]
const wasm_val_vec_t* args, wasm_val_vec_t* results
) {
printf("Calling back...\n> ");
wasm_val_print(args[0]);
wasm_val_print(args->data[0]);
printf("\n");

wasm_val_copy(&results[0], &args[0]);
wasm_val_copy(&results->data[0], &args->data[0]);
return NULL;
}


// A function closure.
own wasm_trap_t* closure_callback(
void* env, const wasm_val_t args[], wasm_val_t results[]
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
) {
int i = *(int*)env;
printf("Calling back closure...\n");
printf("> %d\n", i);

results[0].kind = WASM_I32;
results[0].of.i32 = (int32_t)i;
results->data[0].kind = WASM_I32;
results->data[0].of.i32 = (int32_t)i;
return NULL;
}

Expand All @@ -68,7 +68,7 @@ int main(int argc, const char* argv[]) {

// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("callback.wasm", "r");
FILE* file = fopen("callback.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;
Expand Down Expand Up @@ -108,11 +108,12 @@ int main(int argc, const char* argv[]) {

// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
wasm_extern_t* externs[] = {
wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
};
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand Down Expand Up @@ -140,13 +141,11 @@ int main(int argc, const char* argv[]) {

// Call.
printf("Calling export...\n");
wasm_val_t args[2];
args[0].kind = WASM_I32;
args[0].of.i32 = 3;
args[1].kind = WASM_I32;
args[1].of.i32 = 4;
wasm_val_t results[1];
if (wasm_func_call(run_func, args, results)) {
wasm_val_t as[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
wasm_val_t rs[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(as);
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
}
Expand All @@ -155,7 +154,7 @@ int main(int argc, const char* argv[]) {

// Print result.
printf("Printing result...\n");
printf("> %u\n", results[0].of.i32);
printf("> %u\n", rs[0].of.i32);

// Shut down.
printf("Shutting down...\n");
Expand Down
11 changes: 6 additions & 5 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& {

// A function to be called from Wasm code.
auto print_callback(
const wasm::Val args[], wasm::Val results[]
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl;
results[0] = args[0].copy();
Expand All @@ -45,7 +45,7 @@ auto print_callback(

// A function closure.
auto closure_callback(
void* env, const wasm::Val args[], wasm::Val results[]
void* env, const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
auto i = *reinterpret_cast<int*>(env);
std::cout << "Calling back closure..." << std::endl;
Expand Down Expand Up @@ -103,7 +103,8 @@ void run() {

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
wasm::Extern* imports[] = {print_func.get(), closure_func.get()};
auto imports = wasm::vec<wasm::Extern*>::make(
print_func.get(), closure_func.get());
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module!" << std::endl;
Expand All @@ -121,8 +122,8 @@ void run() {

// Call.
std::cout << "Calling export..." << std::endl;
wasm::Val args[] = {wasm::Val::i32(3), wasm::Val::i32(4)};
wasm::Val results[1];
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(3), wasm::Val::i32(4));
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
if (run_func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
Expand Down
5 changes: 3 additions & 2 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void finalize(void* data) {
void run_in_store(wasm_store_t* store) {
// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("finalize.wasm", "r");
FILE* file = fopen("finalize.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
exit(1);
Expand Down Expand Up @@ -50,8 +50,9 @@ void run_in_store(wasm_store_t* store) {
printf("Instantiating modules...\n");
for (int i = 0; i <= iterations; ++i) {
if (i % (iterations / 10) == 0) printf("%d\n", i);
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
own wasm_instance_t* instance =
wasm_instance_new(store, module, NULL, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module %d!\n", i);
exit(1);
Expand Down
3 changes: 2 additions & 1 deletion lib/c-api/tests/wasm_c_api/wasm-c-api/example/finalize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void run_in_store(wasm::Store* store) {
std::cout << "Instantiating modules..." << std::endl;
for (int i = 0; i <= iterations; ++i) {
if (i % (iterations / 10) == 0) std::cout << i << std::endl;
auto instance = wasm::Instance::make(store, module.get(), nullptr);
auto imports = wasm::vec<wasm::Extern*>::make();
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module " << i << "!" << std::endl;
exit(1);
Expand Down
52 changes: 30 additions & 22 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {

#define check_call(func, type, expected) \
{ \
wasm_val_t results[1]; \
wasm_func_call(func, NULL, results); \
check(results[0], type, expected); \
wasm_val_t vs[1]; \
wasm_val_vec_t args = WASM_EMPTY_VEC; \
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
wasm_func_call(func, &args, &results); \
check(vs[0], type, expected); \
}


Expand All @@ -53,7 +55,7 @@ int main(int argc, const char* argv[]) {

// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("global.wasm", "r");
FILE* file = fopen("global.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;
Expand Down Expand Up @@ -90,16 +92,16 @@ int main(int argc, const char* argv[]) {
own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
wasm_valtype_new(WASM_I64), WASM_VAR);

wasm_val_t val_f32_1 = {.kind = WASM_F32, .of = {.f32 = 1}};
wasm_val_t val_f32_1 = WASM_F32_VAL(1);
own wasm_global_t* const_f32_import =
wasm_global_new(store, const_f32_type, &val_f32_1);
wasm_val_t val_i64_2 = {.kind = WASM_I64, .of = {.i64 = 2}};
wasm_val_t val_i64_2 = WASM_I64_VAL(2);
own wasm_global_t* const_i64_import =
wasm_global_new(store, const_i64_type, &val_i64_2);
wasm_val_t val_f32_3 = {.kind = WASM_F32, .of = {.f32 = 3}};
wasm_val_t val_f32_3 = WASM_F32_VAL(3);
own wasm_global_t* var_f32_import =
wasm_global_new(store, var_f32_type, &val_f32_3);
wasm_val_t val_i64_4 = {.kind = WASM_I64, .of = {.i64 = 4}};
wasm_val_t val_i64_4 = WASM_I64_VAL(4);
own wasm_global_t* var_i64_import =
wasm_global_new(store, var_i64_type, &val_i64_4);

Expand All @@ -110,14 +112,15 @@ int main(int argc, const char* argv[]) {

// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
wasm_extern_t* externs[] = {
wasm_global_as_extern(const_f32_import),
wasm_global_as_extern(const_i64_import),
wasm_global_as_extern(var_f32_import),
wasm_global_as_extern(var_i64_import)
};
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand Down Expand Up @@ -175,13 +178,13 @@ int main(int argc, const char* argv[]) {
check_call(get_var_i64_export, i64, 8);

// Modify variables through API and check again.
wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}};
wasm_val_t val33 = WASM_F32_VAL(33);
wasm_global_set(var_f32_import, &val33);
wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}};
wasm_val_t val34 = WASM_I64_VAL(34);
wasm_global_set(var_i64_import, &val34);
wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37}};
wasm_val_t val37 = WASM_F32_VAL(37);
wasm_global_set(var_f32_export, &val37);
wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 38}};
wasm_val_t val38 = WASM_I64_VAL(38);
wasm_global_set(var_i64_export, &val38);

check_global(var_f32_import, f32, 33);
Expand All @@ -195,14 +198,19 @@ int main(int argc, const char* argv[]) {
check_call(get_var_i64_export, i64, 38);

// Modify variables through calls and check again.
wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
wasm_func_call(set_var_f32_import, args73, NULL);
wasm_val_t args74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} };
wasm_func_call(set_var_i64_import, args74, NULL);
wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} };
wasm_func_call(set_var_f32_export, args77, NULL);
wasm_val_t args78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} };
wasm_func_call(set_var_i64_export, args78, NULL);
wasm_val_vec_t res = WASM_EMPTY_VEC;
wasm_val_t vs73[] = { WASM_F32_VAL(73) };
wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73);
wasm_func_call(set_var_f32_import, &args73, &res);
wasm_val_t vs74[] = { WASM_I64_VAL(74) };
wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74);
wasm_func_call(set_var_i64_import, &args74, &res);
wasm_val_t vs77[] = { WASM_F32_VAL(77) };
wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77);
wasm_func_call(set_var_f32_export, &args77, &res);
wasm_val_t vs78[] = { WASM_I64_VAL(78) };
wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78);
wasm_func_call(set_var_i64_export, &args78, &res);

check_global(var_f32_import, f32, 73);
check_global(var_i64_import, i64, 74);
Expand Down
14 changes: 8 additions & 6 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ void check(T actual, U expected) {
}

auto call(const wasm::Func* func) -> wasm::Val {
wasm::Val results[1];
if (func->call(nullptr, results)) {
auto args = wasm::vec<wasm::Val>::make();
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
if (func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
return results[0].copy();
}

void call(const wasm::Func* func, wasm::Val&& arg) {
wasm::Val args[1] = {std::move(arg)};
if (func->call(args)) {
auto args = wasm::vec<wasm::Val>::make(std::move(arg));
auto results = wasm::vec<wasm::Val>::make();
if (func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
Expand Down Expand Up @@ -95,10 +97,10 @@ void run() {

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
wasm::Extern* imports[] = {
auto imports = wasm::vec<wasm::Extern*>::make(
const_f32_import.get(), const_i64_import.get(),
var_f32_import.get(), var_i64_import.get()
};
);
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module!" << std::endl;
Expand Down
13 changes: 8 additions & 5 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// A function to be called from Wasm code.
own wasm_trap_t* hello_callback(
const wasm_val_t args[], wasm_val_t results[]
const wasm_val_vec_t* args, wasm_val_vec_t* results
) {
printf("Calling back...\n");
printf("> Hello World!\n");
Expand All @@ -25,7 +25,7 @@ int main(int argc, const char* argv[]) {

// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("hello.wasm", "r");
FILE* file = fopen("hello.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;
Expand Down Expand Up @@ -61,9 +61,10 @@ int main(int argc, const char* argv[]) {

// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) };
wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) };
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand All @@ -90,7 +91,9 @@ int main(int argc, const char* argv[]) {

// Call.
printf("Calling export...\n");
if (wasm_func_call(run_func, NULL, NULL)) {
wasm_val_vec_t args = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/c-api/tests/wasm_c_api/wasm-c-api/example/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// A function to be called from Wasm code.
auto hello_callback(
const wasm::Val args[], wasm::Val results[]
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl;
std::cout << "> Hello world!" << std::endl;
Expand Down Expand Up @@ -55,7 +55,7 @@ void run() {

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
wasm::Extern* imports[] = {hello_func.get()};
auto imports = wasm::vec<wasm::Extern*>::make(hello_func.get());
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module!" << std::endl;
Expand All @@ -73,7 +73,9 @@ void run() {

// Call.
std::cout << "Calling export..." << std::endl;
if (run_func->call()) {
auto args = wasm::vec<wasm::Val>::make();
auto results = wasm::vec<wasm::Val>::make();
if (run_func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
Expand Down
Loading