This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Move build logic to lib #97
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54c586c
refactored out build to lib
lexfrl 03e4df3
save_raw returns
lexfrl 1124b50
fix indentations and other small fixes
lexfrl 789e884
fix build API
lexfrl e97ccd7
rename Target to SourceTarget
lexfrl 4dcab78
fix formatting
lexfrl dc2b350
make join runtime_type into runtime_type and runtime_version
lexfrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use std; | ||
use super::{ | ||
CREATE_SYMBOL, | ||
CALL_SYMBOL, | ||
optimize, | ||
pack_instance, | ||
ununderscore_funcs, | ||
externalize_mem, | ||
shrink_unknown_stack, | ||
inject_runtime_type, | ||
PackingError, | ||
OptimizerError, | ||
}; | ||
use parity_wasm; | ||
use parity_wasm::elements; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
Encoding(elements::Error), | ||
Packing(PackingError), | ||
NoCreateSymbolFound, | ||
Optimizer, | ||
} | ||
|
||
impl From<OptimizerError> for Error { | ||
fn from(_err: OptimizerError) -> Self { | ||
Error::Optimizer | ||
} | ||
} | ||
|
||
impl From<PackingError> for Error { | ||
fn from(err: PackingError) -> Self { | ||
Error::Packing(err) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum SourceTarget { | ||
Emscripten, | ||
Unknown, | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { | ||
use self::Error::*; | ||
match *self { | ||
Encoding(ref err) => write!(f, "Encoding error ({})", err), | ||
Optimizer => write!(f, "Optimization error due to missing export section. Pointed wrong file?"), | ||
Packing(ref e) => write!(f, "Packing failed due to module structure error: {}. Sure used correct libraries for building contracts?", e), | ||
NoCreateSymbolFound => write!(f, "Packing failed: no \"{}\" symbol found?", CREATE_SYMBOL), | ||
} | ||
} | ||
} | ||
|
||
fn has_ctor(module: &elements::Module) -> bool { | ||
if let Some(ref section) = module.export_section() { | ||
section.entries().iter().any(|e| CREATE_SYMBOL == e.field()) | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub fn build( | ||
mut module: elements::Module, | ||
constructor: bool, | ||
source_target: SourceTarget, | ||
runtime_type_version: Option<([u8; 4], u32)>, | ||
public_api_entries: &[&str], | ||
enforce_stack_adjustment: bool, | ||
stack_size: u32, | ||
skip_optimization: bool, | ||
) -> Result<(elements::Module, Option<elements::Module>), Error> { | ||
|
||
if let SourceTarget::Emscripten = source_target { | ||
module = ununderscore_funcs(module); | ||
} | ||
|
||
if let SourceTarget::Unknown = source_target { | ||
// 49152 is 48kb! | ||
if enforce_stack_adjustment { | ||
assert!(stack_size <= 1024*1024); | ||
let (new_module, new_stack_top) = shrink_unknown_stack(module, 1024 * 1024 - stack_size); | ||
module = new_module; | ||
let mut stack_top_page = new_stack_top / 65536; | ||
if new_stack_top % 65536 > 0 { stack_top_page += 1 }; | ||
module = externalize_mem(module, Some(stack_top_page), 16); | ||
} else { | ||
module = externalize_mem(module, None, 16); | ||
} | ||
} | ||
|
||
if let Some(runtime_type_version) = runtime_type_version { | ||
let (runtime_type, runtime_version) = runtime_type_version; | ||
module = inject_runtime_type(module, runtime_type, runtime_version); | ||
} | ||
|
||
let mut ctor_module = module.clone(); | ||
|
||
let mut public_api_entries = public_api_entries.to_vec(); | ||
public_api_entries.push(CALL_SYMBOL); | ||
if !skip_optimization { | ||
optimize( | ||
&mut module, | ||
public_api_entries, | ||
)?; | ||
} | ||
|
||
if constructor { | ||
if !has_ctor(&ctor_module) { | ||
Err(Error::NoCreateSymbolFound)? | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are the identation here? some spaces + tab? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cant find any spaces in indentation in this snippet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. probably github changed the way how it displays the diff There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks ok, sorry |
||
if !skip_optimization { | ||
optimize(&mut ctor_module, vec![CREATE_SYMBOL])?; | ||
} | ||
let ctor_module = pack_instance( | ||
parity_wasm::serialize(module.clone()).map_err(Error::Encoding)?, | ||
ctor_module.clone(), | ||
)?; | ||
Ok((module, Some(ctor_module))) | ||
} else { | ||
Ok((module, None)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad identations everywhere
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand, why you think so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like tabs everywhere, sorry