mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-16 16:20:49 +00:00
Implementing instantiate and call
This commit is contained in:
parent
62f7bb607e
commit
be19e96669
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -806,6 +806,7 @@ name = "wasmer-runtime-c-api"
|
||||
version = "0.1.4"
|
||||
dependencies = [
|
||||
"cbindgen 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasmer-runtime 0.1.4",
|
||||
]
|
||||
|
||||
|
@ -10,6 +10,7 @@ readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
wasmer-runtime = { path = "../runtime", version = "0.1.2" }
|
||||
libc = "0.2"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
@ -1,8 +1,9 @@
|
||||
extern crate wasmer_runtime;
|
||||
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use wasmer_runtime::ImportObject;
|
||||
use libc::{c_char, c_int, uint32_t, uint8_t};
|
||||
use std::ffi::CStr;
|
||||
use std::str;
|
||||
use wasmer_runtime::{Instance, ImportObject, Value};
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct wasmer_import_object_t();
|
||||
@ -18,6 +19,14 @@ pub enum wasmer_compile_result_t {
|
||||
WASMER_COMPILE_ERROR = 2,
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[no_mangle]
|
||||
#[repr(C)]
|
||||
pub enum wasmer_call_result_t {
|
||||
WASMER_CALL_OK = 1,
|
||||
WASMER_CALL_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
|
||||
@ -35,16 +44,61 @@ pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_instantiate(
|
||||
mut instance: *mut wasmer_instance_t,
|
||||
bytes: *const c_char,
|
||||
wasm_bytes: *mut uint8_t,
|
||||
wasm_bytes_len: uint32_t,
|
||||
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 = &[];
|
||||
if wasm_bytes.is_null() {
|
||||
return wasmer_compile_result_t::WASMER_COMPILE_ERROR
|
||||
}
|
||||
let bytes: &[u8] = unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
|
||||
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,
|
||||
Err(error) => {
|
||||
println!("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
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_instance_call(instance: *mut wasmer_instance_t,
|
||||
name: *const c_char) -> wasmer_call_result_t {
|
||||
// TODO handle params and results
|
||||
if instance.is_null(){
|
||||
println!("Instance null");
|
||||
return wasmer_call_result_t::WASMER_CALL_ERROR;
|
||||
}
|
||||
if name.is_null(){
|
||||
println!("Name null");
|
||||
return wasmer_call_result_t::WASMER_CALL_ERROR;
|
||||
}
|
||||
let func_name_c = unsafe {
|
||||
CStr::from_ptr(name)
|
||||
};
|
||||
let func_name_r = func_name_c.to_str().unwrap();
|
||||
let instance = unsafe { Box::from_raw(instance as *mut Instance) };
|
||||
let result = instance.call(func_name_r, &[Value::I32(1), Value::I32(2)]);
|
||||
match result {
|
||||
Ok(res) => {
|
||||
wasmer_call_result_t::WASMER_CALL_OK
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Err: {:?}", err);
|
||||
wasmer_call_result_t::WASMER_CALL_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_instance_destroy(instance: *mut wasmer_instance_t) {
|
||||
if !instance.is_null() {
|
||||
drop(unsafe { Box::from_raw(instance as *mut Instance) });
|
||||
}
|
||||
}
|
BIN
lib/runtime-c-api/tests/sum.wasm
Normal file
BIN
lib/runtime-c-api/tests/sum.wasm
Normal file
Binary file not shown.
@ -1,9 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include "../wasmer.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
wasmer_import_object_t *import_object = wasmer_import_object_new();
|
||||
wasmer_import_object_destroy(import_object);
|
||||
|
||||
// Read the wasm file bytes
|
||||
FILE *file = fopen("sum.wasm", "r");
|
||||
fseek(file, 0, SEEK_END);
|
||||
long len = ftell(file);
|
||||
uint8_t *bytes = malloc(len);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fread(bytes, 1, len, file);
|
||||
fclose(file);
|
||||
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_compile_result_t compile_result = wasmer_instantiate(instance, bytes, len, import_object);
|
||||
printf("Compile result: %d\n", compile_result);
|
||||
assert(compile_result == WASMER_COMPILE_OK);
|
||||
|
||||
wasmer_call_result_t call_result = wasmer_instance_call(instance, "sum");
|
||||
printf("Call result: %d\n", call_result);
|
||||
assert(call_result == WASMER_CALL_OK);
|
||||
|
||||
printf("Destroy instance\n");
|
||||
wasmer_instance_destroy(instance);
|
||||
printf("Destroy import object\n");
|
||||
//wasmer_import_object_destroy(import_object); // error here
|
||||
return 0;
|
||||
}
|
@ -3,6 +3,11 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef enum {
|
||||
WASMER_CALL_OK = 1,
|
||||
WASMER_CALL_ERROR = 2,
|
||||
} wasmer_call_result_t;
|
||||
|
||||
typedef enum {
|
||||
WASMER_COMPILE_OK = 1,
|
||||
WASMER_COMPILE_ERROR = 2,
|
||||
@ -16,6 +21,11 @@ void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
|
||||
|
||||
wasmer_import_object_t *wasmer_import_object_new(void);
|
||||
|
||||
wasmer_call_result_t wasmer_instance_call(wasmer_instance_t *instance, const char *name);
|
||||
|
||||
void wasmer_instance_destroy(wasmer_instance_t *instance);
|
||||
|
||||
wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t *instance,
|
||||
const char *bytes,
|
||||
uint8_t *wasm_bytes,
|
||||
uint32_t wasm_bytes_len,
|
||||
wasmer_import_object_t *import_object);
|
||||
|
Loading…
x
Reference in New Issue
Block a user