mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-24 20:21:59 +00:00
import inject example
This commit is contained in:
@ -3,6 +3,7 @@ extern crate parity_wasm;
|
||||
use std::env;
|
||||
|
||||
use parity_wasm::elements;
|
||||
use parity_wasm::builder;
|
||||
|
||||
pub fn inject_nop(opcodes: &mut elements::Opcodes) {
|
||||
use parity_wasm::elements::Opcode::*;
|
||||
@ -38,5 +39,19 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
parity_wasm::serialize_to_file(&args[2], module).unwrap();
|
||||
let mut build = builder::from_module(module);
|
||||
let import_sig = build.push_signature(
|
||||
builder::signature()
|
||||
.param().i32()
|
||||
.param().i32()
|
||||
.return_type().i32()
|
||||
.build_sig()
|
||||
);
|
||||
let build = build.import()
|
||||
.module("env")
|
||||
.field("log")
|
||||
.external().func(import_sig)
|
||||
.build();
|
||||
|
||||
parity_wasm::serialize_to_file(&args[2], build.build()).unwrap();
|
||||
}
|
@ -12,6 +12,12 @@ pub struct SignatureBuilder<F=Identity> {
|
||||
signature: elements::FunctionType,
|
||||
}
|
||||
|
||||
impl SignatureBuilder {
|
||||
pub fn new() -> Self {
|
||||
SignatureBuilder::with_callback(Identity)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> SignatureBuilder<F> where F: Invoke<elements::FunctionType> {
|
||||
pub fn with_callback(callback: F) -> Self {
|
||||
SignatureBuilder {
|
||||
@ -50,6 +56,10 @@ impl<F> SignatureBuilder<F> where F: Invoke<elements::FunctionType> {
|
||||
pub fn build(self) -> F::Result {
|
||||
self.callback.invoke(self.signature)
|
||||
}
|
||||
|
||||
pub fn build_sig(self) -> Signature {
|
||||
Signature::Inline(self.signature)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> Invoke<Vec<elements::ValueType>> for SignatureBuilder<F>
|
||||
@ -298,6 +308,11 @@ pub fn signatures() -> SignaturesBuilder {
|
||||
SignaturesBuilder::new()
|
||||
}
|
||||
|
||||
/// New signature builder
|
||||
pub fn signature() -> SignatureBuilder {
|
||||
SignatureBuilder::new()
|
||||
}
|
||||
|
||||
/// New builder of function (signature & body)
|
||||
pub fn function() -> FunctionBuilder {
|
||||
FunctionBuilder::new()
|
||||
|
@ -7,5 +7,5 @@ mod misc;
|
||||
mod import;
|
||||
|
||||
pub use self::module::{module, from_module, ModuleBuilder};
|
||||
pub use self::code::{signatures, function};
|
||||
pub use self::code::{signatures, signature, function};
|
||||
pub use self::import::import;
|
@ -123,22 +123,13 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
||||
pub fn push_function(&mut self, func: code::FunctionDefinition) -> CodeLocation {
|
||||
let signature = func.signature;
|
||||
let body = func.code;
|
||||
let module = &mut self.module;
|
||||
|
||||
let type_ref = match signature {
|
||||
code::Signature::Inline(func_type) => {
|
||||
module.types.types_mut().push(elements::Type::Function(func_type));
|
||||
module.types.types().len() as u32 - 1
|
||||
}
|
||||
code::Signature::TypeReference(type_ref) => {
|
||||
type_ref
|
||||
}
|
||||
};
|
||||
let type_ref = self.resolve_type_ref(signature);
|
||||
|
||||
module.functions.entries_mut().push(elements::Func::new(type_ref));
|
||||
let signature_index = module.functions.entries_mut().len() as u32 - 1;
|
||||
module.code.bodies_mut().push(body);
|
||||
let body_index = module.code.bodies_mut().len() as u32 - 1;
|
||||
self.module.functions.entries_mut().push(elements::Func::new(type_ref));
|
||||
let signature_index = self.module.functions.entries_mut().len() as u32 - 1;
|
||||
self.module.code.bodies_mut().push(body);
|
||||
let body_index = self.module.code.bodies_mut().len() as u32 - 1;
|
||||
|
||||
CodeLocation {
|
||||
signature: signature_index,
|
||||
@ -146,17 +137,36 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_type_ref(&mut self, signature: code::Signature) -> u32 {
|
||||
match signature {
|
||||
code::Signature::Inline(func_type) => {
|
||||
self.module.types.types_mut().push(elements::Type::Function(func_type));
|
||||
self.module.types.types().len() as u32 - 1
|
||||
}
|
||||
code::Signature::TypeReference(type_ref) => {
|
||||
type_ref
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Push one function signature, returning it's calling index.
|
||||
/// Can create corresponding type in type section.
|
||||
pub fn push_signature(&mut self, signature: code::Signature) -> u32 {
|
||||
let type_ref = self.resolve_type_ref(signature);
|
||||
self.module.functions.entries_mut().push(elements::Func::new(type_ref));
|
||||
self.module.functions.entries_mut().len() as u32 - 1
|
||||
}
|
||||
|
||||
/// Push signatures in the module, returning corresponding indices of pushed signatures
|
||||
pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
|
||||
let module = &mut self.module;
|
||||
let mut result = Vec::new();
|
||||
|
||||
// todo: maybe reuse existing types with the equal signatures
|
||||
let raw_functions: Vec<u32> = signatures.into_iter().map(|binding|
|
||||
match binding {
|
||||
code::Signature::Inline(func_type) => {
|
||||
module.types.types_mut().push(elements::Type::Function(func_type));
|
||||
module.types.types().len() as u32 - 1
|
||||
self.module.types.types_mut().push(elements::Type::Function(func_type));
|
||||
self.module.types.types().len() as u32 - 1
|
||||
}
|
||||
code::Signature::TypeReference(type_ref) => {
|
||||
type_ref
|
||||
@ -165,8 +175,8 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
||||
).collect();
|
||||
|
||||
for function in raw_functions {
|
||||
module.functions.entries_mut().push(elements::Func::new(function));
|
||||
result.push(module.functions.entries_mut().len() as u32 - 1);
|
||||
self.module.functions.entries_mut().push(elements::Func::new(function));
|
||||
result.push(self.module.functions.entries_mut().len() as u32 - 1);
|
||||
}
|
||||
|
||||
result
|
||||
|
Reference in New Issue
Block a user