mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-03-31 10:21:04 +00:00
wasm-pack utility to pack wasm files into transactions payload
This commit is contained in:
parent
25110c32a8
commit
729dddc9cc
@ -23,3 +23,7 @@ path = "ext/src/main.rs"
|
|||||||
[[bin]]
|
[[bin]]
|
||||||
name = "wasm-gas"
|
name = "wasm-gas"
|
||||||
path = "gas/src/main.rs"
|
path = "gas/src/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "wasm-pack"
|
||||||
|
path = "pack/src/main.rs"
|
2
pack/.gitignore
vendored
Normal file
2
pack/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
target
|
||||||
|
Cargo.lock
|
9
pack/Cargo.toml
Normal file
9
pack/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "wasm-pack"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
parity-wasm = { git="https://github.com/nikvolf/parity-wasm" }
|
||||||
|
wasm-utils = { path = "../" }
|
||||||
|
clap = "2.24"
|
32
pack/src/main.rs
Normal file
32
pack/src/main.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
extern crate parity_wasm;
|
||||||
|
extern crate wasm_utils;
|
||||||
|
extern crate clap;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
wasm_utils::init_log();
|
||||||
|
|
||||||
|
let matches = App::new("wasm-opt")
|
||||||
|
.arg(Arg::with_name("input")
|
||||||
|
.index(1)
|
||||||
|
.required(true)
|
||||||
|
.help("Input WASM file"))
|
||||||
|
.arg(Arg::with_name("output")
|
||||||
|
.index(2)
|
||||||
|
.required(true)
|
||||||
|
.help("Output WASM file"))
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
let input = matches.value_of("input").expect("is required; qed");
|
||||||
|
let output = matches.value_of("output").expect("is required; qed");
|
||||||
|
|
||||||
|
// doing serialization roundtrip to make sure the input is a valid wasm module
|
||||||
|
let module = parity_wasm::deserialize_file(&input).expect("Failed to load wasm module from file");
|
||||||
|
let bytes = parity_wasm::serialize(module).expect("Failed to serialize wasm module");
|
||||||
|
|
||||||
|
// Wrap contract code into the wasm module that returns it
|
||||||
|
let packed_module = wasm_utils::pack_instance(bytes);
|
||||||
|
|
||||||
|
parity_wasm::serialize_to_file(&output, packed_module).unwrap();
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
use parity_wasm::{elements, builder};
|
use parity_wasm::{elements, builder};
|
||||||
|
|
||||||
|
|
||||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Opcode::*;
|
||||||
for opcode in opcodes.elements_mut().iter_mut() {
|
for opcode in opcodes.elements_mut().iter_mut() {
|
||||||
|
@ -8,8 +8,10 @@ mod gas;
|
|||||||
mod symbols;
|
mod symbols;
|
||||||
mod logger;
|
mod logger;
|
||||||
mod ext;
|
mod ext;
|
||||||
|
mod pack;
|
||||||
|
|
||||||
pub use optimizer::{optimize, Error as OptimizerError};
|
pub use optimizer::{optimize, Error as OptimizerError};
|
||||||
pub use gas::inject_gas_counter;
|
pub use gas::inject_gas_counter;
|
||||||
pub use logger::init_log;
|
pub use logger::init_log;
|
||||||
pub use ext::externalize;
|
pub use ext::externalize;
|
||||||
|
pub use pack::pack_instance;
|
35
src/pack.rs
Normal file
35
src/pack.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use parity_wasm::{elements, builder};
|
||||||
|
|
||||||
|
pub fn pack_instance(raw_module: Vec<u8>) -> elements::Module {
|
||||||
|
|
||||||
|
let raw_len = raw_module.len();
|
||||||
|
let mem_required = (raw_len / (64 * 1024) + 1) as u32;
|
||||||
|
|
||||||
|
let module = builder::module()
|
||||||
|
.import()
|
||||||
|
.module("env")
|
||||||
|
.field("memory")
|
||||||
|
.external()
|
||||||
|
.memory(mem_required as u32, Some(mem_required as u32))
|
||||||
|
.build()
|
||||||
|
.data()
|
||||||
|
.offset(elements::Opcode::I32Const(0))
|
||||||
|
.value(raw_module)
|
||||||
|
.build()
|
||||||
|
.function()
|
||||||
|
.signature().param().i32().build()
|
||||||
|
.body().with_opcodes(elements::Opcodes::new(vec![
|
||||||
|
elements::Opcode::GetLocal(0),
|
||||||
|
elements::Opcode::I32Const(raw_len as i32),
|
||||||
|
elements::Opcode::I32Store(0, 12),
|
||||||
|
elements::Opcode::End,
|
||||||
|
])).build()
|
||||||
|
.build()
|
||||||
|
.export()
|
||||||
|
.field("_call")
|
||||||
|
.internal().func(0)
|
||||||
|
.build()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
module
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user