mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-12 21:38:39 +00:00
Fixed memory grow
This commit is contained in:
33
lib/js-api/src/externals/memory.rs
vendored
33
lib/js-api/src/externals/memory.rs
vendored
@@ -4,11 +4,28 @@ use crate::externals::Extern;
|
||||
use crate::store::Store;
|
||||
use crate::{MemoryType, MemoryView};
|
||||
use std::convert::TryInto;
|
||||
use thiserror::Error;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasmer_types::{Bytes, Pages, ValueType};
|
||||
|
||||
pub type MemoryError = ();
|
||||
/// Error type describing things that can go wrong when operating on Wasm Memories.
|
||||
#[derive(Error, Debug, Clone, PartialEq, Hash)]
|
||||
pub enum MemoryError {
|
||||
/// The operation would cause the size of the memory to exceed the maximum or would cause
|
||||
/// an overflow leading to unindexable memory.
|
||||
#[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)]
|
||||
CouldNotGrow {
|
||||
/// The current size in pages.
|
||||
current: Pages,
|
||||
/// The attempted amount to grow by in pages.
|
||||
attempted_delta: Pages,
|
||||
},
|
||||
/// A user defined error value, used for error cases not listed above.
|
||||
#[error("A user-defined error occurred: {0}")]
|
||||
Generic(String),
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
@@ -219,9 +236,17 @@ impl Memory {
|
||||
IntoPages: Into<Pages>,
|
||||
{
|
||||
let pages = delta.into();
|
||||
// let new_pages = js_memory_grow(&self.vm_memory.unchecked_into::<JSMemory>(), pages.0).unwrap();
|
||||
// let new_pages = self.vm_memory.unchecked_ref::<JSMemory>().grow(pages.0);
|
||||
let new_pages = self.vm_memory.memory.grow(pages.0);
|
||||
let js_memory = self.vm_memory.memory.clone().unchecked_into::<JSMemory>();
|
||||
let new_pages = js_memory.grow(pages.0).map_err(|err| {
|
||||
if err.is_instance_of::<js_sys::RangeError>() {
|
||||
MemoryError::CouldNotGrow {
|
||||
current: self.size(),
|
||||
attempted_delta: pages,
|
||||
}
|
||||
} else {
|
||||
MemoryError::Generic(err.as_string().unwrap())
|
||||
}
|
||||
})?;
|
||||
Ok(Pages(new_pages))
|
||||
}
|
||||
|
||||
|
||||
2
lib/js-api/src/externals/mod.rs
vendored
2
lib/js-api/src/externals/mod.rs
vendored
@@ -8,7 +8,7 @@ pub use self::function::{
|
||||
};
|
||||
|
||||
pub use self::global::Global;
|
||||
pub use self::memory::Memory;
|
||||
pub use self::memory::{Memory, MemoryError};
|
||||
pub use self::table::Table;
|
||||
|
||||
use crate::export::Export;
|
||||
|
||||
@@ -310,7 +310,8 @@ pub use crate::cell::WasmCell;
|
||||
pub use crate::env::{HostEnvInitError, LazyInit, WasmerEnv};
|
||||
pub use crate::exports::{ExportError, Exportable, Exports, ExportsIterator};
|
||||
pub use crate::externals::{
|
||||
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, Table, WasmTypeList,
|
||||
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, Table,
|
||||
WasmTypeList,
|
||||
};
|
||||
pub use crate::import_object::{ImportObject, ImportObjectIterator, LikeNamespace};
|
||||
pub use crate::instance::{Instance, InstantiationError};
|
||||
|
||||
Reference in New Issue
Block a user