mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-05-30 18:11:05 +00:00
wasm-pack utility to pack wasm files into transactions payload
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use parity_wasm::{elements, builder};
|
||||
|
||||
|
||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
||||
use parity_wasm::elements::Opcode::*;
|
||||
for opcode in opcodes.elements_mut().iter_mut() {
|
||||
|
||||
+3
-1
@@ -8,8 +8,10 @@ mod gas;
|
||||
mod symbols;
|
||||
mod logger;
|
||||
mod ext;
|
||||
mod pack;
|
||||
|
||||
pub use optimizer::{optimize, Error as OptimizerError};
|
||||
pub use gas::inject_gas_counter;
|
||||
pub use logger::init_log;
|
||||
pub use ext::externalize;
|
||||
pub use ext::externalize;
|
||||
pub use pack::pack_instance;
|
||||
+35
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user