From 62f7bb607e48d852884108d1e45d020bed7ec38a Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 1 Feb 2019 22:10:36 -0600 Subject: [PATCH] Add first c test to test the C api --- lib/runtime-c-api/README.md | 6 +++++- lib/runtime-c-api/build.rs | 2 ++ lib/runtime-c-api/src/lib.rs | 2 ++ lib/runtime-c-api/tests/.gitignore | 12 +++++++++++ lib/runtime-c-api/tests/CMakeLists.txt | 11 ++++++++++ .../tests/runtime_c_api_tests.rs | 4 ++++ lib/runtime-c-api/tests/test-instantiate.c | 9 ++++++++ lib/runtime-c-api/wasmer.h | 21 ++++++++----------- 8 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 lib/runtime-c-api/tests/.gitignore create mode 100644 lib/runtime-c-api/tests/CMakeLists.txt create mode 100644 lib/runtime-c-api/tests/runtime_c_api_tests.rs create mode 100644 lib/runtime-c-api/tests/test-instantiate.c diff --git a/lib/runtime-c-api/README.md b/lib/runtime-c-api/README.md index 954d2e0cf..7792a8c17 100644 --- a/lib/runtime-c-api/README.md +++ b/lib/runtime-c-api/README.md @@ -1,4 +1,8 @@ # Wasmer Runtime C API ## Generating header files -Run `make capi` \ No newline at end of file +Run `make capi` from wasmer project root directory + +## Running tests +`cmake . && make && make test` from runtime-c-api/tests directory +(TODO run this within a rust test) \ No newline at end of file diff --git a/lib/runtime-c-api/build.rs b/lib/runtime-c-api/build.rs index c1d68e431..40398c2f9 100644 --- a/lib/runtime-c-api/build.rs +++ b/lib/runtime-c-api/build.rs @@ -15,8 +15,10 @@ fn main() { fn build() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + use cbindgen::Language; cbindgen::Builder::new() .with_crate(crate_dir) + .with_language(Language::C) .generate() .expect("Unable to generate bindings") .write_to_file("wasmer.h"); diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 8f95e8331..5decf8d75 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -23,6 +23,7 @@ pub extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t { Box::into_raw(Box::new(ImportObject::new())) as *mut wasmer_import_object_t } +#[allow(clippy::cast_ptr_alignment)] #[no_mangle] pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) { if !import_object.is_null() { @@ -30,6 +31,7 @@ pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import } } +#[allow(clippy::cast_ptr_alignment)] #[no_mangle] pub extern "C" fn wasmer_instantiate( mut instance: *mut wasmer_instance_t, diff --git a/lib/runtime-c-api/tests/.gitignore b/lib/runtime-c-api/tests/.gitignore new file mode 100644 index 000000000..4e3d6fb5d --- /dev/null +++ b/lib/runtime-c-api/tests/.gitignore @@ -0,0 +1,12 @@ +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +test-instantiate \ No newline at end of file diff --git a/lib/runtime-c-api/tests/CMakeLists.txt b/lib/runtime-c-api/tests/CMakeLists.txt new file mode 100644 index 000000000..4687b27da --- /dev/null +++ b/lib/runtime-c-api/tests/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required (VERSION 2.6) +project (WasmerCApiTests) + +add_executable(test-instantiate test-instantiate.c) + +target_link_libraries(test-instantiate + general "${CMAKE_SOURCE_DIR}/../../../target/debug/libwasmer_runtime_c_api.dylib") + +enable_testing() +add_test(test-instantiate test-instantiate) + diff --git a/lib/runtime-c-api/tests/runtime_c_api_tests.rs b/lib/runtime-c-api/tests/runtime_c_api_tests.rs new file mode 100644 index 000000000..f9112c83c --- /dev/null +++ b/lib/runtime-c-api/tests/runtime_c_api_tests.rs @@ -0,0 +1,4 @@ +#[test] +fn test_c_api() { + // TODO run `cmake . && make && make test` +} diff --git a/lib/runtime-c-api/tests/test-instantiate.c b/lib/runtime-c-api/tests/test-instantiate.c new file mode 100644 index 000000000..f801fa044 --- /dev/null +++ b/lib/runtime-c-api/tests/test-instantiate.c @@ -0,0 +1,9 @@ +#include +#include "../wasmer.h" + +int main() +{ + wasmer_import_object_t *import_object = wasmer_import_object_new(); + wasmer_import_object_destroy(import_object); + return 0; +} \ No newline at end of file diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 3d57c81ea..6fd391dec 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -1,24 +1,21 @@ -#include -#include -#include +#include +#include +#include +#include -enum class wasmer_compile_result_t { +typedef enum { WASMER_COMPILE_OK = 1, WASMER_COMPILE_ERROR = 2, -}; +} wasmer_compile_result_t; -struct wasmer_import_object_t; +typedef struct wasmer_import_object_t wasmer_import_object_t; -struct wasmer_instance_t; - -extern "C" { +typedef struct wasmer_instance_t wasmer_instance_t; void wasmer_import_object_destroy(wasmer_import_object_t *import_object); -wasmer_import_object_t *wasmer_import_object_new(); +wasmer_import_object_t *wasmer_import_object_new(void); wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t *instance, const char *bytes, wasmer_import_object_t *import_object); - -} // extern "C"