mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 12:41:32 +00:00
Initial commit of C API library
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -728,6 +728,13 @@ dependencies = [
|
||||
"wasmer-runtime-core 0.1.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasmer-runtime-c-api"
|
||||
version = "0.1.4"
|
||||
dependencies = [
|
||||
"wasmer-runtime 0.1.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasmer-runtime-core"
|
||||
version = "0.1.2"
|
||||
|
@ -27,7 +27,7 @@ wasmer-runtime-core = { path = "lib/runtime-core" }
|
||||
wasmer-emscripten = { path = "lib/emscripten" }
|
||||
|
||||
[workspace]
|
||||
members = ["lib/clif-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests"]
|
||||
members = ["lib/clif-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/runtime-c-api"]
|
||||
|
||||
[build-dependencies]
|
||||
wabt = "0.7.2"
|
||||
|
13
lib/runtime-c-api/Cargo.toml
Normal file
13
lib/runtime-c-api/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "wasmer-runtime-c-api"
|
||||
version = "0.1.4"
|
||||
description = "Wasmer c-api library"
|
||||
license = "MIT"
|
||||
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
|
||||
repository = "https://github.com/wasmerio/wasmer"
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
wasmer-runtime = { path = "../runtime", version = "0.1.2" }
|
||||
|
6
lib/runtime-c-api/README.md
Normal file
6
lib/runtime-c-api/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Wasmer Runtime C API
|
||||
|
||||
|
||||
## Generating header files
|
||||
1. `cargo install cbindgen`
|
||||
2. `cbindgen lib/runtime-c-api/ -o lib/runtime-c-api/wasmer.h`
|
50
lib/runtime-c-api/src/lib.rs
Normal file
50
lib/runtime-c-api/src/lib.rs
Normal file
@ -0,0 +1,50 @@
|
||||
extern crate wasmer_runtime;
|
||||
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use wasmer_runtime::ImportObject;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[no_mangle]
|
||||
pub enum wasmer_import_object_t {}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[no_mangle]
|
||||
pub enum wasmer_instance_t {}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[no_mangle]
|
||||
#[repr(C)]
|
||||
pub enum wasmer_compile_result_t {
|
||||
WASMER_COMPILE_OK = 1,
|
||||
WASMER_COMPILE_ERROR = 2,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
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
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) {
|
||||
if !import_object.is_null() {
|
||||
drop(unsafe { Box::from_raw(import_object as *mut ImportObject) });
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_instantiate(
|
||||
mut instance: *mut wasmer_instance_t,
|
||||
bytes: *const c_char,
|
||||
import_object: *mut wasmer_import_object_t,
|
||||
) -> wasmer_compile_result_t {
|
||||
let import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
|
||||
let bytes = &[];
|
||||
let result = wasmer_runtime::instantiate(bytes, *import_object);
|
||||
let new_instance = match result {
|
||||
Ok(instance) => instance,
|
||||
Err(error) => return wasmer_compile_result_t::WASMER_COMPILE_ERROR,
|
||||
};
|
||||
instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t;
|
||||
wasmer_compile_result_t::WASMER_COMPILE_OK
|
||||
}
|
24
lib/runtime-c-api/wasmer.h
Normal file
24
lib/runtime-c-api/wasmer.h
Normal file
@ -0,0 +1,24 @@
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
enum class wasmer_compile_result_t {
|
||||
WASMER_COMPILE_OK = 1,
|
||||
WASMER_COMPILE_ERROR = 2,
|
||||
};
|
||||
|
||||
struct wasmer_import_object_t;
|
||||
|
||||
struct wasmer_instance_t;
|
||||
|
||||
extern "C" {
|
||||
|
||||
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
|
||||
|
||||
wasmer_import_object_t *wasmer_import_object_new();
|
||||
|
||||
wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t *instance,
|
||||
const char *bytes,
|
||||
wasmer_import_object_t *import_object);
|
||||
|
||||
} // extern "C"
|
Reference in New Issue
Block a user