wasmer/lib/runtime-core/src/vmcalls.rs

161 lines
4.2 KiB
Rust
Raw Normal View History

2019-01-29 10:31:44 -08:00
#![allow(clippy::cast_ptr_alignment)]
2019-01-17 13:09:05 -08:00
use crate::{
memory::{DynamicMemory, StaticMemory},
2019-01-17 13:09:05 -08:00
structures::TypedIndex,
types::{ImportedMemoryIndex, LocalMemoryIndex, LocalTableIndex},
2019-01-29 15:44:15 -08:00
units::Pages,
2019-01-17 13:09:05 -08:00
vm,
};
2019-01-08 12:09:47 -05:00
2019-01-17 13:09:05 -08:00
// +*****************************+
// | LOCAL MEMORIES |
// +****************************+
pub unsafe extern "C" fn local_static_memory_grow(
ctx: &mut vm::Ctx,
2019-01-16 10:26:10 -08:00
memory_index: LocalMemoryIndex,
2019-01-29 15:44:15 -08:00
delta: Pages,
2019-01-08 12:09:47 -05:00
) -> i32 {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory;
2019-05-13 05:11:08 -07:00
let ret = match (*memory).grow(delta, &mut *local_memory) {
Ok(old) => old.0 as i32,
Err(_) => -1,
2019-05-13 05:11:08 -07:00
};
ctx.internal.memory_base = (*local_memory).base;
ctx.internal.memory_bound = (*local_memory).bound;
ret
2019-01-08 12:09:47 -05:00
}
2019-01-17 13:09:05 -08:00
pub unsafe extern "C" fn local_static_memory_size(
ctx: &vm::Ctx,
memory_index: LocalMemoryIndex,
2019-01-29 15:44:15 -08:00
) -> Pages {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory;
2019-01-29 15:44:15 -08:00
(*memory).size()
2019-01-08 12:09:47 -05:00
}
2019-01-17 13:09:05 -08:00
pub unsafe extern "C" fn local_dynamic_memory_grow(
ctx: &mut vm::Ctx,
2019-01-16 10:26:10 -08:00
memory_index: LocalMemoryIndex,
2019-01-29 15:44:15 -08:00
delta: Pages,
) -> i32 {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory;
2019-05-13 05:11:08 -07:00
let ret = match (*memory).grow(delta, &mut *local_memory) {
Ok(old) => old.0 as i32,
Err(_) => -1,
2019-05-13 05:11:08 -07:00
};
ctx.internal.memory_base = (*local_memory).base;
ctx.internal.memory_bound = (*local_memory).bound;
ret
2019-01-10 22:59:57 -05:00
}
2019-01-17 13:09:05 -08:00
pub unsafe extern "C" fn local_dynamic_memory_size(
ctx: &vm::Ctx,
memory_index: LocalMemoryIndex,
2019-01-29 15:44:15 -08:00
) -> Pages {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory;
2019-01-29 15:44:15 -08:00
(*memory).size()
}
2019-01-17 13:09:05 -08:00
// +*****************************+
// | IMPORTED MEMORIES |
// +****************************+
pub unsafe extern "C" fn imported_static_memory_grow(
ctx: &mut vm::Ctx,
import_memory_index: ImportedMemoryIndex,
2019-01-29 15:44:15 -08:00
delta: Pages,
2019-01-17 13:09:05 -08:00
) -> i32 {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx
.internal
.imported_memories
.add(import_memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory;
2019-05-13 05:11:08 -07:00
let ret = match (*memory).grow(delta, &mut *local_memory) {
Ok(old) => old.0 as i32,
Err(_) => -1,
2019-05-13 05:11:08 -07:00
};
ctx.internal.memory_base = (*local_memory).base;
ctx.internal.memory_bound = (*local_memory).bound;
ret
2019-01-17 13:09:05 -08:00
}
pub unsafe extern "C" fn imported_static_memory_size(
ctx: &vm::Ctx,
import_memory_index: ImportedMemoryIndex,
2019-01-29 15:44:15 -08:00
) -> Pages {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx
.internal
.imported_memories
.add(import_memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory;
2019-01-29 15:44:15 -08:00
(*memory).size()
}
pub unsafe extern "C" fn imported_dynamic_memory_grow(
ctx: &mut vm::Ctx,
memory_index: ImportedMemoryIndex,
2019-01-29 15:44:15 -08:00
delta: Pages,
) -> i32 {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.imported_memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory;
2019-05-13 05:11:08 -07:00
let ret = match (*memory).grow(delta, &mut *local_memory) {
Ok(old) => old.0 as i32,
Err(_) => -1,
2019-05-13 05:11:08 -07:00
};
ctx.internal.memory_base = (*local_memory).base;
ctx.internal.memory_bound = (*local_memory).bound;
ret
}
pub unsafe extern "C" fn imported_dynamic_memory_size(
ctx: &vm::Ctx,
memory_index: ImportedMemoryIndex,
2019-01-29 15:44:15 -08:00
) -> Pages {
2019-03-21 08:39:06 +08:00
let local_memory = *ctx.internal.imported_memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory;
2019-01-29 15:44:15 -08:00
(*memory).size()
2019-01-17 13:09:05 -08:00
}
// +*****************************+
// | LOCAL TABLES |
// +****************************+
pub unsafe extern "C" fn local_table_grow(
ctx: &mut vm::Ctx,
2019-01-17 13:09:05 -08:00
table_index: LocalTableIndex,
delta: u32,
2019-01-17 13:09:05 -08:00
) -> i32 {
let _ = table_index;
let _ = delta;
2019-01-17 13:09:05 -08:00
let _ = ctx;
unimplemented!()
}
pub unsafe extern "C" fn local_table_size(ctx: &vm::Ctx, table_index: LocalTableIndex) -> u32 {
2019-01-17 13:09:05 -08:00
let _ = table_index;
let _ = ctx;
unimplemented!()
}