mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 12:41:32 +00:00
Add foundational emscripten functions and types to C API
This commit is contained in:
58
lib/runtime-c-api/src/import/emscripten.rs
Normal file
58
lib/runtime-c-api/src/import/emscripten.rs
Normal file
@ -0,0 +1,58 @@
|
||||
//! Functions and types for dealing with Emscripten imports
|
||||
|
||||
use super::*;
|
||||
use crate::module::wasmer_module_t;
|
||||
use std::ptr;
|
||||
use wasmer_emscripten::EmscriptenGlobals;
|
||||
use wasmer_runtime::Module;
|
||||
|
||||
/// Type used to construct an import_object_t with Emscripten imports.
|
||||
#[repr(C)]
|
||||
pub struct wasmer_emscripten_globals_t;
|
||||
|
||||
/// Create a `wasmer_emscripten_globals_t` from a Wasm module.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmer_emscripten_get_emscripten_globals(
|
||||
module: *const wasmer_module_t,
|
||||
) -> *mut wasmer_emscripten_globals_t {
|
||||
if module.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let module = &*(module as *const Module);
|
||||
match EmscriptenGlobals::new(module) {
|
||||
Ok(globals) => Box::into_raw(Box::new(globals)) as *mut wasmer_emscripten_globals_t,
|
||||
Err(msg) => {
|
||||
update_last_error(CApiError { msg });
|
||||
return ptr::null_mut();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Destroy `wasmer_emscrpten_globals_t` created by
|
||||
/// `wasmer_emscripten_get_emscripten_globals`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmer_emscripten_destroy_emscripten_globals(
|
||||
globals: *mut wasmer_emscripten_globals_t,
|
||||
) {
|
||||
if globals.is_null() {
|
||||
return;
|
||||
}
|
||||
let _ = Box::from_raw(globals);
|
||||
}
|
||||
|
||||
/// Create a `wasmer_import_object_t` with Emscripten imports, use
|
||||
/// `wasmer_emscripten_get_emscripten_globals` to get a
|
||||
/// `wasmer_emscripten_globals_t` from a `wasmer_module_t`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmer_emscripten_generate_import_object(
|
||||
globals: *mut wasmer_emscripten_globals_t,
|
||||
) -> *mut wasmer_import_object_t {
|
||||
if globals.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
// TODO: figure out if we should be using UnsafeCell here or something
|
||||
let g = &mut *(globals as *mut EmscriptenGlobals);
|
||||
let import_object = Box::new(wasmer_emscripten::generate_emscripten_env(g));
|
||||
|
||||
Box::into_raw(import_object) as *mut wasmer_import_object_t
|
||||
}
|
@ -61,6 +61,12 @@ mod wasi;
|
||||
#[cfg(feature = "wasi")]
|
||||
pub use self::wasi::*;
|
||||
|
||||
#[cfg(feature = "emscripten")]
|
||||
mod emscripten;
|
||||
|
||||
#[cfg(feature = "emscripten")]
|
||||
pub use self::emscripten::*;
|
||||
|
||||
/// Gets an entry from an ImportObject at the name and namespace.
|
||||
/// Stores `name`, `namespace`, and `import_export_value` in `import`.
|
||||
/// Thus these must remain valid for the lifetime of `import`.
|
||||
@ -437,6 +443,9 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
|
||||
module: *const wasmer_module_t,
|
||||
import_descriptors: *mut *mut wasmer_import_descriptors_t,
|
||||
) {
|
||||
if module.is_null() {
|
||||
return;
|
||||
}
|
||||
let module = &*(module as *const Module);
|
||||
let total_imports = module.info().imported_functions.len()
|
||||
+ module.info().imported_tables.len()
|
||||
|
Reference in New Issue
Block a user