Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull in PR 3612 #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/api/src/js/function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ impl<T: Send + 'static> FunctionEnvMut<'_, T> {
func_env: self.func_env.clone(),
}
}

/// Borrows a new mutable reference of both the attached Store and host state
pub fn data_and_store_mut(&mut self) -> (&mut T, StoreMut) {
let data = self.func_env.as_mut(&mut self.store_mut) as *mut T;
// telling the borrow check to close his eyes here
// this is still relatively safe to do as func_env are
// stored in a specific vec of Store, separate from the other objects
// and not really directly accessible with the StoreMut
let data = unsafe { &mut *data };
(data, self.store_mut.as_store_mut())
}
}

impl<T> AsStoreRef for FunctionEnvMut<'_, T> {
Expand Down
33 changes: 17 additions & 16 deletions lib/api/src/sys/externals/global.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::ptr::NonNull;

use crate::sys::exports::{ExportError, Exportable};
use crate::sys::externals::Extern;
use crate::sys::store::{AsStoreMut, AsStoreRef};
use crate::sys::value::Value;
use crate::sys::GlobalType;
use crate::sys::Mutability;
use crate::sys::RuntimeError;
use wasmer_vm::{InternalStoreHandle, StoreHandle, VMExtern, VMGlobal};
use wasmer_vm::{InternalStoreHandle, StoreHandle, VMExtern, VMGlobal, VMGlobalDefinition};

/// A WebAssembly `global` instance.
///
Expand Down Expand Up @@ -110,13 +112,9 @@ impl Global {
/// ```
pub fn get(&self, store: &mut impl AsStoreMut) -> Value {
unsafe {
let raw = self
.handle
.get(store.as_store_ref().objects())
.vmglobal()
.as_ref()
.val;
let ty = self.handle.get(store.as_store_ref().objects()).ty().ty;
let global = self.handle.get(store.as_store_ref().objects());
let raw = global.vmglobal().as_ref().val;
let ty = global.ty().ty;
Value::from_raw(store, ty, raw)
}
}
Expand Down Expand Up @@ -168,22 +166,20 @@ impl Global {
"cross-`Context` values are not supported",
));
}
if self.ty(store).mutability != Mutability::Var {
let global = self.handle.get_mut(store.objects_mut());
if global.ty().mutability != Mutability::Var {
return Err(RuntimeError::new("Attempted to set an immutable global"));
}
if val.ty() != self.ty(store).ty {
if val.ty() != global.ty().ty {
return Err(RuntimeError::new(format!(
"Attempted to operate on a global of type {expected} as a global of type {found}",
expected = self.ty(store).ty,
expected = global.ty().ty,
found = val.ty(),
)));
}
unsafe {
self.handle
.get_mut(store.objects_mut())
.vmglobal()
.as_mut()
.val = val.as_raw(store);
let mut dest = global.vmglobal();
dest.as_mut().val = val.as_raw(store);
}
Ok(())
}
Expand All @@ -207,6 +203,11 @@ impl Global {
pub(crate) fn to_vm_extern(&self) -> VMExtern {
VMExtern::Global(self.handle.internal_handle())
}

/// Gets the underlying VM pointer
pub unsafe fn vmglobal(&self, store: &mut impl AsStoreMut) -> NonNull<VMGlobalDefinition> {
self.handle.get_mut(store.objects_mut()).vmglobal()
}
}

impl std::cmp::PartialEq for Global {
Expand Down
12 changes: 12 additions & 0 deletions lib/api/src/sys/function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ impl<T: Send + 'static> FunctionEnvMut<'_, T> {
func_env: self.func_env.clone(),
}
}

/// Borrows a new mutable reference of both the attached Store and host state
pub fn data_and_store_mut(&mut self) -> (&mut T, StoreMut) {
let data = self.func_env.as_mut(&mut self.store_mut) as *mut T;
// telling the borrow check to close his eyes here

// this is still relatively safe to do as func_env are
// stored in a specific vec of Store, separate from the other objects
// and not really directly accessible with the StoreMut
let data = unsafe { &mut *data };
(data, self.store_mut.as_store_mut())
}
}

impl<T> AsStoreRef for FunctionEnvMut<'_, T> {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl Module {
/// # }
/// ```
pub fn set_name(&mut self, name: &str) -> bool {
Arc::get_mut(&mut self.module_info).map_or(false, |mut module_info| {
Arc::get_mut(&mut self.module_info).map_or(false, |module_info| {
module_info.name = Some(name.to_string());
true
})
Expand Down
6 changes: 3 additions & 3 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Compiler for LLVMCompiler {
for (section_index, custom_section) in compiled_function.custom_sections.iter() {
// TODO: remove this call to clone()
let mut custom_section = custom_section.clone();
for mut reloc in &mut custom_section.relocations {
for reloc in &mut custom_section.relocations {
if let RelocationTarget::CustomSection(index) = reloc.reloc_target {
reloc.reloc_target = RelocationTarget::CustomSection(
SectionIndex::from_u32(first_section + index.as_u32()),
Expand All @@ -276,7 +276,7 @@ impl Compiler for LLVMCompiler {
.contains(&section_index)
{
let offset = frame_section_bytes.len() as u32;
for mut reloc in &mut custom_section.relocations {
for reloc in &mut custom_section.relocations {
reloc.offset += offset;
}
frame_section_bytes.extend_from_slice(custom_section.bytes.as_slice());
Expand All @@ -291,7 +291,7 @@ impl Compiler for LLVMCompiler {
module_custom_sections.push(custom_section);
}
}
for mut reloc in &mut compiled_function.compiled_function.relocations {
for reloc in &mut compiled_function.compiled_function.relocations {
if let RelocationTarget::CustomSection(index) = reloc.reloc_target {
reloc.reloc_target = RelocationTarget::CustomSection(
SectionIndex::from_u32(first_section + index.as_u32()),
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler-singlepass/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,7 @@ impl<'a, M: Machine> FuncGen<'a, M> {
self.release_locations_value(stack_depth)?;
self.value_stack.truncate(stack_depth);
self.fp_stack.truncate(fp_depth);
let mut frame = &mut self.control_stack.last_mut().unwrap();
let frame = &mut self.control_stack.last_mut().unwrap();

match frame.if_else {
IfElseState::If(label) => {
Expand Down