Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
svm-wasmer-c-api:
Browse files Browse the repository at this point in the history
* using the new wasmer `wasmer_module_import_instantiate` instead of `wasmer_svm_module_instantiate`.
* using `wasmer_import_object_extend`
* removed `src/import.rs` (code is has moved to `wasmer-c-api`)

see PR: wasmerio/wasmer#616
  • Loading branch information
YaronWittenstein committed Aug 4, 2019
1 parent 5070add commit 148e404
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 261 deletions.
1 change: 1 addition & 0 deletions crates/svm-wasmer-c-api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/svm-wasmer-c-api/examples/counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int main() {
assert(compile_res == WASMER_OK);

wasmer_instance_t *instance = NULL;
wasmer_result_t instance_res = wasmer_svm_module_instantiate(&instance, module, import_object);
wasmer_result_t instance_res = wasmer_module_import_instantiate(&instance, module, import_object);
assert(instance_res == WASMER_OK);

// First we want to assert that the counter has been initialized with `9`
Expand Down
Binary file modified crates/svm-wasmer-c-api/examples/libwasmer_runtime_c_api.dylib
Binary file not shown.
24 changes: 0 additions & 24 deletions crates/svm-wasmer-c-api/examples/svm_wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include <stdint.h>
#include <stdlib.h>

typedef struct {

} wasmer_import_object_t;

/**
* Returns a pointer to the `svm context node_data`.
* It will be used by the node vmcalls implementation.
Expand Down Expand Up @@ -46,24 +42,4 @@ wasmer_result_t wasmer_svm_import_object(wasmer_import_object_t** import_object,
wasmer_import_t *imports,
uint32_t imports_len);


/**
* Given a compiler wasmer module (param `module`) and a ready-made import object (param `import_object`),
* instantiates a new wasmer instance.
* The instance is returned via the param `instance` (that's why it's of type `wasmer_instance_t**`)
*
* Returns `wasmer_result_t::WASMER_OK` upon success.
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_svm_module_instantiate(wasmer_instance_t** instance,
wasmer_module_t* module,
wasmer_import_object_t* import_object);


/**
* Frees memory of the given ImportObject
*/
void wasmer_import_object_destroy(wasmer_import_object_t* import_object);

#endif /* WASMER_SVM_H */
42 changes: 37 additions & 5 deletions crates/svm-wasmer-c-api/examples/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ typedef struct {

typedef struct {

} wasmer_instance_t;

typedef struct {

} wasmer_instance_context_t;
} wasmer_import_object_t;

typedef struct {

Expand All @@ -119,6 +115,14 @@ typedef struct {
wasmer_import_export_value value;
} wasmer_import_t;

typedef struct {

} wasmer_instance_t;

typedef struct {

} wasmer_instance_context_t;

typedef struct {
bool has_some;
uint32_t some;
Expand Down Expand Up @@ -392,6 +396,24 @@ wasmer_result_t wasmer_import_func_returns(const wasmer_import_func_t *func,
wasmer_result_t wasmer_import_func_returns_arity(const wasmer_import_func_t *func,
uint32_t *result);

/**
* Frees memory of the given ImportObject
*/
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);

/**
* Extends an existing import object with new imports
*/
wasmer_result_t wasmer_import_object_extend(wasmer_import_object_t *import_object,
wasmer_import_t *imports,
unsigned int imports_len);

/**
* Creates a new empty import object.
* See also `wasmer_import_object_append`
*/
wasmer_import_object_t *wasmer_import_object_new(void);

/**
* Calls an instances exported function by `name` with the provided parameters.
* Results are set using the provided `results` pointer.
Expand Down Expand Up @@ -531,6 +553,16 @@ wasmer_result_t wasmer_module_deserialize(wasmer_module_t **module,
*/
void wasmer_module_destroy(wasmer_module_t *module);

/**
* Given:
* A prepared `wasmer svm` import-object
* A compiled wasmer module
* Instantiates a wasmer instance
*/
wasmer_result_t wasmer_module_import_instantiate(wasmer_instance_t **instance,
const wasmer_module_t *module,
const wasmer_import_object_t *import_object);

/**
* Creates a new Instance from the given module and imports.
* Returns `wasmer_result_t::WASMER_OK` upon success.
Expand Down
13 changes: 0 additions & 13 deletions crates/svm-wasmer-c-api/src/import.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/svm-wasmer-c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

//! This crate is responsible on providing a [FFI](https://doc.rust-lang.org/nomicon/ffi.html) interface for the `wasmer svm`.
/// Contains a definition for `wasmer_import_object_t`
pub mod import;

/// Will contain macros for the `svm wasmer C-API`
pub mod macros;

Expand Down
106 changes: 9 additions & 97 deletions crates/svm-wasmer-c-api/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ macro_rules! include_svm_wasmer_c_api {
($KV:ident, $PS:ident, $PC:ident) => {
use std::ffi::c_void;

use crate::import::wasmer_import_object_t;

use wasmer_runtime::{imports, Ctx, ImportObject, Instance, Module};
use wasmer_runtime_c_api::{
error::{update_last_error, CApiError},
export::wasmer_import_export_kind,
import::wasmer_import_t,
import::{wasmer_import_object_extend, wasmer_import_object_t, wasmer_import_t},
instance::{wasmer_instance_context_t, wasmer_instance_t},
module::wasmer_module_t,
wasmer_result_t,
Expand Down Expand Up @@ -62,45 +60,19 @@ macro_rules! include_svm_wasmer_c_api {
wasmer_data_node_data!(wasmer_ctx.data, $PC)
}

/// Given:
/// * A prepared `wasmer svm` import-object
/// * A compiled wasmer module
///
/// Instantiates a wasmer instance
#[no_mangle]
pub unsafe extern "C" fn wasmer_svm_module_instantiate(
instance_ptr_ptr: *mut *mut wasmer_instance_t,
module: *const wasmer_module_t,
import_object: *const wasmer_import_object_t,
) -> wasmer_runtime_c_api::wasmer_result_t {
let import_object: &ImportObject = &*(import_object as *const ImportObject);
let module: &Module = &*(module as *const Module);

let new_instance: Instance = match module.instantiate(import_object) {
Ok(instance) => instance,
Err(error) => {
update_last_error(error);
return wasmer_result_t::WASMER_ERROR;
}
};
*instance_ptr_ptr = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t;

return wasmer_result_t::WASMER_OK;
}

/// Creates a new `wasmer` import object.
/// The import object will include imports of two flavors:
/// * external vmcalls (i.e: node vmcalls)
/// * internal vmcalls (i.e: register/storage/etc vmcalls)
#[no_mangle]
pub unsafe extern "C" fn wasmer_svm_import_object(
imprt_obj_ptr_ptr: *mut *mut wasmer_import_object_t,
raw_import_object: *mut *mut wasmer_import_object_t,
addr_ptr: *const u8,
max_pages: libc::c_int,
max_page_slices: libc::c_int,
node_data_ptr: *const c_void,
imports: *mut wasmer_import_t,
imports_len: libc::c_int,
imports_len: libc::c_uint,
) -> wasmer_runtime_c_api::wasmer_result_t {
use wasmer_runtime::ImportObject;

Expand All @@ -114,18 +86,16 @@ macro_rules! include_svm_wasmer_c_api {
max_page_slices as usize
);

let mut import_obj = ImportObject::new_with_data(state_gen);

append_internal_imports(&mut import_obj);
let mut import_object = ImportObject::new_with_data(state_gen);
append_internal_imports(&mut import_object);

let _res = append_external_imports(&mut import_obj, imports, imports_len);
*raw_import_object = cast_import_obj_to_ptr(import_object);
let _res = wasmer_import_object_extend(*raw_import_object, imports, imports_len);
// TODO: assert result
// if res != wasmer_result_t::WASMER_OK {
// return res;
// }

*imprt_obj_ptr_ptr = cast_import_obj_to_ptr(import_obj);

wasmer_result_t::WASMER_OK
}

Expand All @@ -141,68 +111,10 @@ macro_rules! include_svm_wasmer_c_api {
import_obj.register("svm", ns);
}

unsafe fn append_external_imports(
import_object: &mut wasmer_runtime::ImportObject,
imports: *mut wasmer_import_t,
imports_len: libc::c_int,
) -> wasmer_result_t {
use std::collections::HashMap;
use std::slice;

/// original code has been takes from `wasmer_instantiate` located at:
/// https://github.com/wasmerio/wasmer/blob/master/lib/runtime-c-api/src/instance.rs
let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize);
let mut namespaces = HashMap::new();

for import in imports {
let module_name = slice::from_raw_parts(
import.module_name.bytes,
import.module_name.bytes_len as usize,
);
let module_name = if let Ok(s) = std::str::from_utf8(module_name) {
s
} else {
update_last_error(CApiError {
msg: "error converting module name to string".to_string(),
});
return wasmer_result_t::WASMER_ERROR;
};
let import_name = slice::from_raw_parts(
import.import_name.bytes,
import.import_name.bytes_len as usize,
);
let import_name = if let Ok(s) = std::str::from_utf8(import_name) {
s
} else {
update_last_error(CApiError {
msg: "error converting import_name to string".to_string(),
});
return wasmer_result_t::WASMER_ERROR;
};

let namespace = namespaces.entry(module_name).or_insert_with(Namespace::new);

let export = match import.tag {
wasmer_import_export_kind::WASM_FUNCTION => {
let func_export = import.value.func as *mut Export;
(&*func_export).clone()
}
_ => unreachable!(),
};
namespace.insert(import_name, export);
}

for (module_name, namespace) in namespaces.into_iter() {
import_object.register(module_name, namespace);
}

wasmer_result_t::WASMER_OK
}

fn cast_import_obj_to_ptr(
import_obj: wasmer_runtime::ImportObject,
import_object: wasmer_runtime::ImportObject,
) -> *mut wasmer_import_object_t {
let boxed_import_obj = Box::new(import_obj);
let boxed_import_obj = Box::new(import_object);
let import_obj_ptr: *mut _ = Box::into_raw(boxed_import_obj);

import_obj_ptr as *mut _
Expand Down
Loading

0 comments on commit 148e404

Please sign in to comment.