mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-15 05:50:49 +00:00
refactor preparation
This commit is contained in:
parent
8ad42ba83f
commit
e1e873b2fc
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
#![deny(
|
||||
nonstandard_style,
|
||||
unused_imports,
|
||||
@ -23,14 +22,12 @@
|
||||
unused_unsafe,
|
||||
unreachable_patterns
|
||||
)]
|
||||
*/
|
||||
|
||||
/// Command-line tool intended to test Frank VM.
|
||||
mod vm;
|
||||
|
||||
use crate::vm::config::Config;
|
||||
use crate::vm::frank::Frank;
|
||||
use crate::vm::prepare::prepare_module;
|
||||
|
||||
use crate::vm::service::FluenceService;
|
||||
use clap::{App, AppSettings, Arg, SubCommand};
|
||||
@ -79,7 +76,6 @@ fn main() -> Result<(), ExitFailure> {
|
||||
let config = Config::default();
|
||||
let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap();
|
||||
let wasm_code = fs::read(in_module_path)?;
|
||||
let wasm_code = prepare_module(&wasm_code, &config)?;
|
||||
|
||||
let invoke_arg = arg.value_of(INVOKE_ARG).unwrap();
|
||||
let mut frank = Frank::new(&wasm_code, config)?;
|
||||
|
@ -46,9 +46,9 @@ impl Default for WASIConfig {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Config {
|
||||
/// Count of Wasm memory pages that will be preallocated on the VM startup.
|
||||
/// Maximum number of Wasm memory pages that loaded module can use.
|
||||
/// Each Wasm pages is 65536 bytes long.
|
||||
pub mem_pages_count: i32,
|
||||
pub mem_pages_count: u32,
|
||||
|
||||
/// If true, registers the logger Wasm module with name 'logger'.
|
||||
/// This functionality is just for debugging, and this module will be disabled in future.
|
||||
|
@ -15,21 +15,13 @@
|
||||
*/
|
||||
|
||||
use wasmer_runtime::error::{
|
||||
CallError,
|
||||
CompileError,
|
||||
CreationError,
|
||||
Error as WasmerError,
|
||||
ResolveError,
|
||||
RuntimeError,
|
||||
CallError, CompileError, CreationError, Error as WasmerError, ResolveError, RuntimeError,
|
||||
};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FrankError {
|
||||
/// Errors related to the preparation (instrumentation and so on) and compilation by Wasmer steps.
|
||||
InstantiationError(String),
|
||||
|
||||
/// Errors for I/O errors raising while opening a file.
|
||||
IOError(String),
|
||||
|
||||
@ -54,7 +46,6 @@ impl Error for FrankError {}
|
||||
impl std::fmt::Display for FrankError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
FrankError::InstantiationError(msg) => write!(f, "InstantiationError: {}", msg),
|
||||
FrankError::IOError(msg) => write!(f, "IOError: {}", msg),
|
||||
FrankError::WasmerResolveError(msg) => write!(f, "WasmerResolveError: {}", msg),
|
||||
FrankError::WasmerInvokeError(msg) => write!(f, "WasmerInvokeError: {}", msg),
|
||||
|
@ -15,10 +15,7 @@
|
||||
*/
|
||||
|
||||
use crate::vm::{
|
||||
config::Config,
|
||||
errors::FrankError,
|
||||
frank_result::FrankResult,
|
||||
prepare::prepare_module,
|
||||
config::Config, errors::FrankError, frank_result::FrankResult, prepare::prepare_module,
|
||||
service::FluenceService,
|
||||
};
|
||||
|
||||
@ -88,7 +85,7 @@ impl Frank {
|
||||
|
||||
/// Creates a new virtual machine executor.
|
||||
pub fn new(wasm_bytes: &[u8], config: Config) -> Result<Self, FrankError> {
|
||||
let prepared_wasm_bytes = prepare_module(wasm_bytes, &config)?;
|
||||
let prepared_wasm_bytes = prepare_module(wasm_bytes, config.mem_pages_count)?;
|
||||
|
||||
let logger_imports = imports! {
|
||||
"logger" => {
|
||||
|
@ -18,13 +18,11 @@
|
||||
// https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs
|
||||
// https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs
|
||||
|
||||
use crate::vm::config::Config;
|
||||
use crate::vm::errors::FrankError;
|
||||
|
||||
use parity_wasm::{
|
||||
builder, elements,
|
||||
elements::{MemorySection, MemoryType},
|
||||
builder,
|
||||
elements,
|
||||
};
|
||||
|
||||
struct ModulePreparator {
|
||||
@ -75,8 +73,8 @@ impl<'a> ModulePreparator {
|
||||
|
||||
/// Prepares a Wasm module:
|
||||
/// - set memory page count
|
||||
pub fn prepare_module(module: &[u8], config: &Config) -> Result<Vec<u8>, FrankError> {
|
||||
pub fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>, FrankError> {
|
||||
ModulePreparator::init(module)?
|
||||
.set_mem_pages_count(config.mem_pages_count as _)
|
||||
.set_mem_pages_count(mem_pages_count)
|
||||
.into_wasm()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user