mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-05-12 15:27:19 +00:00
public api exposure and fix warnings
This commit is contained in:
parent
da5b2ca5f6
commit
d60340762b
92
src/graph.rs
92
src/graph.rs
@ -7,7 +7,7 @@ use std::borrow::ToOwned;
|
|||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
enum ImportedOrDeclared<T=()> {
|
pub enum ImportedOrDeclared<T=()> {
|
||||||
Imported(String, String),
|
Imported(String, String),
|
||||||
Declared(T),
|
Declared(T),
|
||||||
}
|
}
|
||||||
@ -18,82 +18,82 @@ impl<T> From<&elements::ImportEntry> for ImportedOrDeclared<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuncOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
pub type FuncOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
||||||
type GlobalOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
pub type GlobalOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
||||||
type MemoryOrigin = ImportedOrDeclared;
|
pub type MemoryOrigin = ImportedOrDeclared;
|
||||||
type TableOrigin = ImportedOrDeclared;
|
pub type TableOrigin = ImportedOrDeclared;
|
||||||
|
|
||||||
struct Func {
|
pub struct Func {
|
||||||
type_ref: EntryRef<elements::Type>,
|
pub type_ref: EntryRef<elements::Type>,
|
||||||
origin: FuncOrigin,
|
pub origin: FuncOrigin,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Global {
|
pub struct Global {
|
||||||
content: elements::ValueType,
|
pub content: elements::ValueType,
|
||||||
is_mut: bool,
|
pub is_mut: bool,
|
||||||
origin: GlobalOrigin,
|
pub origin: GlobalOrigin,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Instruction {
|
pub enum Instruction {
|
||||||
Plain(elements::Instruction),
|
Plain(elements::Instruction),
|
||||||
Call(EntryRef<Func>),
|
Call(EntryRef<Func>),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Memory {
|
pub struct Memory {
|
||||||
limits: elements::ResizableLimits,
|
pub limits: elements::ResizableLimits,
|
||||||
origin: MemoryOrigin,
|
pub origin: MemoryOrigin,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Table {
|
pub struct Table {
|
||||||
origin: TableOrigin,
|
pub origin: TableOrigin,
|
||||||
limits: elements::ResizableLimits,
|
pub limits: elements::ResizableLimits,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum SegmentLocation {
|
pub enum SegmentLocation {
|
||||||
Passive,
|
Passive,
|
||||||
Default(Vec<Instruction>),
|
Default(Vec<Instruction>),
|
||||||
WithIndex(u32, Vec<Instruction>),
|
WithIndex(u32, Vec<Instruction>),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DataSegment {
|
pub struct DataSegment {
|
||||||
location: SegmentLocation,
|
pub location: SegmentLocation,
|
||||||
value: Vec<u8>,
|
pub value: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ElementSegment {
|
pub struct ElementSegment {
|
||||||
location: SegmentLocation,
|
pub location: SegmentLocation,
|
||||||
value: Vec<u32>,
|
pub value: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ExportLocal {
|
pub enum ExportLocal {
|
||||||
Func(EntryRef<Func>),
|
Func(EntryRef<Func>),
|
||||||
Global(EntryRef<Global>),
|
Global(EntryRef<Global>),
|
||||||
Table(EntryRef<Table>),
|
Table(EntryRef<Table>),
|
||||||
Memory(EntryRef<Memory>),
|
Memory(EntryRef<Memory>),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Export {
|
pub struct Export {
|
||||||
name: String,
|
pub name: String,
|
||||||
local: ExportLocal,
|
pub local: ExportLocal,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Module {
|
pub struct Module {
|
||||||
types: RefList<elements::Type>,
|
pub types: RefList<elements::Type>,
|
||||||
funcs: RefList<Func>,
|
pub funcs: RefList<Func>,
|
||||||
memory: RefList<Memory>,
|
pub memory: RefList<Memory>,
|
||||||
tables: RefList<Table>,
|
pub tables: RefList<Table>,
|
||||||
globals: RefList<Global>,
|
pub globals: RefList<Global>,
|
||||||
start: Option<EntryRef<Func>>,
|
pub start: Option<EntryRef<Func>>,
|
||||||
exports: Vec<Export>,
|
pub exports: Vec<Export>,
|
||||||
elements: Vec<ElementSegment>,
|
pub elements: Vec<ElementSegment>,
|
||||||
data: Vec<DataSegment>,
|
pub data: Vec<DataSegment>,
|
||||||
other: BTreeMap<usize, elements::Section>,
|
pub other: BTreeMap<usize, elements::Section>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
|
|
||||||
fn from_elements(module: &elements::Module) -> Self {
|
pub fn from_elements(module: &elements::Module) -> Self {
|
||||||
|
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
let mut res = Module::default();
|
let mut res = Module::default();
|
||||||
@ -469,7 +469,6 @@ impl Module {
|
|||||||
ExportLocal::Memory(ref memory_ref) => {
|
ExportLocal::Memory(ref memory_ref) => {
|
||||||
elements::Internal::Memory(memory_ref.order().expect("detached memory ref") as u32)
|
elements::Internal::Memory(memory_ref.order().expect("detached memory ref") as u32)
|
||||||
},
|
},
|
||||||
_ => continue,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.push(elements::ExportEntry::new(export.name.to_owned(), internal));
|
exports.push(elements::ExportEntry::new(export.name.to_owned(), internal));
|
||||||
@ -521,7 +520,7 @@ impl Module {
|
|||||||
// CODE SECTION (10)
|
// CODE SECTION (10)
|
||||||
let mut code_section = elements::CodeSection::default();
|
let mut code_section = elements::CodeSection::default();
|
||||||
{
|
{
|
||||||
let mut funcs = code_section.bodies_mut();
|
let funcs = code_section.bodies_mut();
|
||||||
|
|
||||||
for func in self.funcs.iter() {
|
for func in self.funcs.iter() {
|
||||||
match func.read().origin {
|
match func.read().origin {
|
||||||
@ -587,11 +586,11 @@ fn custom_round(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(wasm: &[u8]) -> Module {
|
pub fn parse(wasm: &[u8]) -> Module {
|
||||||
Module::from_elements(&::parity_wasm::deserialize_buffer(wasm).expect("failed to parse wasm"))
|
Module::from_elements(&::parity_wasm::deserialize_buffer(wasm).expect("failed to parse wasm"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate(f: &Module) -> Vec<u8> {
|
pub fn generate(f: &Module) -> Vec<u8> {
|
||||||
let pm = f.generate();
|
let pm = f.generate();
|
||||||
::parity_wasm::serialize(pm).expect("failed to generate wasm")
|
::parity_wasm::serialize(pm).expect("failed to generate wasm")
|
||||||
}
|
}
|
||||||
@ -600,7 +599,6 @@ fn generate(f: &Module) -> Vec<u8> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
extern crate wabt;
|
extern crate wabt;
|
||||||
use parity_wasm;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn smoky() {
|
fn smoky() {
|
||||||
|
@ -29,6 +29,8 @@ pub use gas::inject_gas_counter;
|
|||||||
pub use ext::{externalize, externalize_mem, underscore_funcs, ununderscore_funcs, shrink_unknown_stack};
|
pub use ext::{externalize, externalize_mem, underscore_funcs, ununderscore_funcs, shrink_unknown_stack};
|
||||||
pub use pack::{pack_instance, Error as PackingError};
|
pub use pack::{pack_instance, Error as PackingError};
|
||||||
pub use runtime_type::inject_runtime_type;
|
pub use runtime_type::inject_runtime_type;
|
||||||
|
pub use graph::{Module, parse, generate};
|
||||||
|
pub use ref_list::{RefList, Entry, EntryRef, DeleteTransaction};
|
||||||
|
|
||||||
pub struct TargetRuntime {
|
pub struct TargetRuntime {
|
||||||
pub create_symbol: &'static str,
|
pub create_symbol: &'static str,
|
||||||
|
@ -110,9 +110,6 @@ impl<T> RefList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn done_delete(&mut self, indices: &[usize]) {
|
fn done_delete(&mut self, indices: &[usize]) {
|
||||||
|
|
||||||
let mut index = 0;
|
|
||||||
|
|
||||||
for idx in indices {
|
for idx in indices {
|
||||||
let mut detached = self.items.remove(*idx);
|
let mut detached = self.items.remove(*idx);
|
||||||
detached.write().index = EntryOrigin::Detached;
|
detached.write().index = EntryOrigin::Detached;
|
||||||
@ -174,13 +171,13 @@ pub struct DeleteTransaction<'a, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> DeleteTransaction<'a, T> {
|
impl<'a, T> DeleteTransaction<'a, T> {
|
||||||
pub fn push(mut self, idx: usize) -> Self {
|
pub fn push(self, idx: usize) -> Self {
|
||||||
let mut tx = self;
|
let mut tx = self;
|
||||||
tx.deleted.push(idx);
|
tx.deleted.push(idx);
|
||||||
tx
|
tx
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn done(mut self) {
|
pub fn done(self) {
|
||||||
let indices = self.deleted;
|
let indices = self.deleted;
|
||||||
let list = self.list;
|
let list = self.list;
|
||||||
list.done_delete(&indices[..]);
|
list.done_delete(&indices[..]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user