mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-05-06 06:57:55 +00:00
wip
build contract constructor
This commit is contained in:
+32
-5
@@ -6,9 +6,11 @@ extern crate clap;
|
||||
extern crate parity_wasm;
|
||||
|
||||
use std::{fs, io};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use parity_wasm::elements;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
@@ -58,6 +60,14 @@ pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_ctor(module: &elements::Module) -> bool {
|
||||
if let Some(ref section) = module.export_section() {
|
||||
section.entries().iter().find(|e| "_create" == e.field()).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
wasm_utils::init_log();
|
||||
|
||||
@@ -102,10 +112,6 @@ fn main() {
|
||||
);
|
||||
}
|
||||
|
||||
if !matches.is_present("skip_optimization") {
|
||||
wasm_utils::optimize(&mut module, vec!["_call", "setTempRet0"]).expect("Optimizer to finish without errors");
|
||||
}
|
||||
|
||||
if let Some(runtime_type) = matches.value_of("runtime_type") {
|
||||
let runtime_type: &[u8] = runtime_type.as_bytes();
|
||||
if runtime_type.len() != 4 {
|
||||
@@ -116,5 +122,26 @@ fn main() {
|
||||
module = wasm_utils::inject_runtime_type(module, &runtime_type, runtime_version);
|
||||
}
|
||||
|
||||
parity_wasm::serialize_to_file(&path, module).unwrap();
|
||||
let mut ctor_module = module.clone();
|
||||
|
||||
if !matches.is_present("skip_optimization") {
|
||||
wasm_utils::optimize(&mut module, vec!["_call", "setTempRet0"]).expect("Optimizer to finish without errors");
|
||||
}
|
||||
|
||||
let raw_module = parity_wasm::serialize(module).expect("Failed to serialize module");
|
||||
|
||||
let mut file = fs::File::create(&path).expect("Failed to create file");;
|
||||
file.write_all(&raw_module).expect("Failed to write module to file");
|
||||
|
||||
// will pack into constructor
|
||||
if has_ctor(&ctor_module) {
|
||||
if !matches.is_present("skip_optimization") {
|
||||
wasm_utils::optimize(&mut ctor_module, vec!["_create", "setTempRet0"]).expect("Optimizer to finish without errors");
|
||||
}
|
||||
wasm_utils::pack_instance(raw_module, &mut ctor_module);
|
||||
|
||||
let ctor_path = wasm_path(target_dir, &format!("{}_ctor", wasm_binary));
|
||||
parity_wasm::serialize_to_file(ctor_path, ctor_module);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user