Skip to content

Commit e92186f

Browse files
refactor wasm_to_wasm interface
1 parent e520288 commit e92186f

File tree

14 files changed

+55
-137
lines changed

14 files changed

+55
-137
lines changed

contracts/eosiolib_native/vm_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ struct vm_api {
208208
void (*vm_set_debug_contract)(uint64_t account, const char* path);
209209
const char* (*vm_get_debug_contract)(uint64_t* account);
210210

211-
int (*wasm_to_wast)( const uint8_t* data, size_t size, uint8_t* wast, size_t wast_size );
211+
int (*wasm_to_wast)( const uint8_t* data, size_t size, uint8_t* wast, size_t wast_size, bool strip_names );
212212
int (*wast_to_wasm)( const uint8_t* data, size_t size, uint8_t* wasm, size_t wasm_size );
213213

214214
void (*send_deferred)(const __uint128* sender_id, uint64_t payer, const char *serialized_transaction, size_t size, uint32_t replace_existing);

libraries/chain/include/eosio/chain/wast_to_wasm.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@ std::vector<uint8_t> wast_to_wasm( const std::string& wast );
1212
std::string wasm_to_wast( const std::vector<uint8_t>& wasm, bool strip_names );
1313
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names );
1414

15-
std::vector<uint8_t> _wast_to_wasm( const std::string& wast );
16-
std::string _wasm_to_wast( const std::vector<uint8_t>& wasm );
17-
std::string _wasm_to_wast( const uint8_t* data, uint64_t size );
18-
std::string _wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names);
1915

2016
} } /// eosio::chain

libraries/chain/wast_to_wasm.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ namespace eosio { namespace chain {
6262

6363
} FC_CAPTURE_AND_RETHROW( (wast) ) } /// wast_to_wasm
6464

65-
std::string wasm_to_wast( const std::vector<uint8_t>& wasm, bool strip_names ) {
66-
return wasm_to_wast( wasm.data(), wasm.size(), strip_names, strip_names);
65+
std::string wasm_to_wast( const std::vector<uint8_t>& wasm , bool strip_names ) {
66+
return wasm_to_wast( wasm.data(), wasm.size(), strip_names);
6767
} /// wasm_to_wast
6868

69-
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names )
69+
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names )
7070
{ try {
7171
IR::Module module;
7272
Serialization::MemoryInputStream stream((const U8*)data,size);
@@ -78,4 +78,32 @@ namespace eosio { namespace chain {
7878
} FC_CAPTURE_AND_RETHROW() } /// wasm_to_wast
7979

8080

81+
int wasm_to_wast( const uint8_t* data, size_t size, uint8_t* wast, size_t wast_size, bool strip_names )
82+
{
83+
std::string s = wasm_to_wast(data, size, strip_names);
84+
if (wast == nullptr || wast_size == 0) {
85+
return s.size();
86+
}
87+
88+
if (wast_size > s.size()) {
89+
wast_size = s.size();
90+
}
91+
memcpy(wast, s.c_str(), wast_size);
92+
return wast_size;
93+
} /// wasm_to_wast
94+
95+
int wast_to_wasm( const uint8_t* data, size_t size, uint8_t* wasm, size_t wasm_size ) {
96+
std::string wast((char*)data, size);
97+
auto v = wast_to_wasm(wast);
98+
if (wasm == nullptr || wasm_size <=0) {
99+
return v.size();
100+
}
101+
if (v.size() < wasm_size) {
102+
wasm_size = v.size();
103+
}
104+
memcpy(wasm, v.data(), wasm_size);
105+
return wasm_size;
106+
}
107+
81108
} } // eosio::chain
109+

libraries/testing/tester.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ namespace eosio { namespace testing {
657657

658658

659659
void base_tester::set_code( account_name account, const char* wast, const private_key_type* signer ) try {
660-
set_code(account, _wast_to_wasm(wast), signer);
660+
set_code(account, wast_to_wasm(wast), signer);
661661
} FC_CAPTURE_AND_RETHROW( (account) )
662662

663663

libraries/vm/vm_wasm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set(HEADERS
1010

1111
set(COMMON_SOURCES
1212
vm_api.cpp
13-
wast_to_wasm.cpp
13+
${CMAKE_SOURCE_DIR}/libraries/chain/wast_to_wasm.cpp
1414
wasm_interface.cpp
1515
wasm_eosio_injection.cpp
1616
name.cpp)

libraries/vm/vm_wasm/vm_wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace eosio {
1818

1919
namespace eosio {
2020
namespace chain {
21-
int wasm_to_wast( const uint8_t* data, size_t size, uint8_t* wast, size_t wast_size );
21+
int wasm_to_wast( const uint8_t* data, size_t size, uint8_t* wast, size_t wast_size, bool strip_names );
2222
int wast_to_wasm( const uint8_t* data, size_t size, uint8_t* wasm, size_t wasm_size );
2323

2424
}

libraries/vm/vm_wasm/wast_to_wasm.cpp

Lines changed: 0 additions & 108 deletions
This file was deleted.

libraries/vm_manager/vm_manager.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ static uint64_t vm_names[] = {
135135
};
136136

137137
static const char* vm_libs_path[] = {
138-
"../libs/libvm_wasm_wabt" DYLIB_SUFFIX,
139-
// "../libs/libvm_wasm_binaryen" DYLIB_SUFFIX,
138+
// "../libs/libvm_wasm_wabt" DYLIB_SUFFIX,
139+
"../libs/libvm_wasm_binaryen" DYLIB_SUFFIX,
140140
"../libs/libvm_cpython_ss" DYLIB_SUFFIX,
141141
"../libs/libvm_eth2" DYLIB_SUFFIX,
142142
"../libs/libvm_wasm_wavm-0" DYLIB_SUFFIX,
@@ -706,23 +706,25 @@ void vm_manager::remove_trusted_account(uint64_t account) {
706706

707707
namespace eosio { namespace chain {
708708

709-
std::vector<uint8_t> _wast_to_wasm( const std::string& wast ) {
709+
std::vector<uint8_t> wast_to_wasm( const std::string& wast ) {
710710
int wasm_size = get_vm_api()->wast_to_wasm( (uint8_t*)wast.c_str(), wast.size(), nullptr, 0);
711711
std::vector<uint8_t> v(wasm_size);
712+
712713
int size = get_vm_api()->wast_to_wasm( (uint8_t*)wast.c_str(), wast.size(), v.data(), v.size());
713714
return std::vector<uint8_t>(v.data(), v.data()+size);
714715
}
715716

716-
std::string _wasm_to_wast( const uint8_t* data, uint64_t size ) {
717-
int wast_size = get_vm_api()->wasm_to_wast( (uint8_t*)data, size, nullptr, 0);
717+
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names ) {
718+
int wast_size = get_vm_api()->wasm_to_wast( (uint8_t*)data, size, nullptr, 0, strip_names);
718719
std::vector<uint8_t> v(wast_size);
719-
wast_size = get_vm_api()->wasm_to_wast( (uint8_t*)data, size, v.data(), v.size());
720+
721+
wast_size = get_vm_api()->wasm_to_wast( (uint8_t*)data, size, v.data(), v.size(), strip_names);
720722
return std::string((char*)v.data(), wast_size);
721723
}
722724

723-
std::string _wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names )
725+
std::string wasm_to_wast( const uint8_t* data, uint64_t size )
724726
{
725-
return _wasm_to_wast(data, size);
727+
return wasm_to_wast(data, size, true);
726728
}
727729
}
728730
}

plugins/chain_plugin/chain_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ read_only::get_code_results read_only::get_code( const get_code_params& params )
15491549
if (params.code_as_wasm) {
15501550
result.wasm = string(accnt.code.begin(), accnt.code.end());
15511551
} else {
1552-
result.wast = _wasm_to_wast( (const uint8_t*)accnt.code.data(), accnt.code.size() );
1552+
result.wast = wasm_to_wast( (const uint8_t*)accnt.code.data(), accnt.code.size(), true );
15531553
}
15541554
result.code_hash = fc::sha256::hash( accnt.code.data(), accnt.code.size() );
15551555
}

plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ struct txn_test_gen_plugin_impl {
170170
{
171171
signed_transaction trx;
172172

173-
vector<uint8_t> wasm = _wast_to_wasm(std::string(eosio_token_wast));
173+
vector<uint8_t> wasm = wast_to_wasm(std::string(eosio_token_wast));
174174

175175
setcode handler;
176176
handler.account = newaccountC;

programs/cleos/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ int main( int argc, char** argv ) {
19451945

19461946
wasm = string(wasm_v.begin(), wasm_v.end());
19471947
if(!code_as_wasm && wasm_v.size())
1948-
wast = _wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false);
1948+
wast = wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false);
19491949

19501950
abi_def abi_d;
19511951
if(abi_serializer::to_abi(abi_v, abi_d))

programs/pyeos/interface/eosapi_.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ int get_code_(string& name, string& wast, string& str_abi, string& code_hash, in
688688

689689
if (accnt.code.size()) {
690690
if (accnt.vm_type == 0) {
691-
result.wast = chain::_wasm_to_wast((uint8_t*)accnt.code.data(), accnt.code.size());
691+
result.wast = chain::wasm_to_wast((uint8_t*)accnt.code.data(), accnt.code.size(), true);
692692
} else {
693693
result.wast = string((const char*)accnt.code.data(), accnt.code.size());
694694
}
@@ -768,7 +768,7 @@ void mp_set_max_execution_time_(int _max) {
768768

769769
void wast2wasm_(string& wast, string& result) {
770770
try {
771-
auto wasm = _wast_to_wasm(wast);
771+
auto wasm = wast_to_wasm(wast);
772772
result = string((char *)wasm.data(), wasm.size());
773773
} catch (...) {
774774
elog("wast_to_wasm failed");
@@ -777,7 +777,7 @@ void wast2wasm_(string& wast, string& result) {
777777

778778
void wasm2wast_(string& wasm, string& result) {
779779
try {
780-
auto wast = _wasm_to_wast((uint8_t*)wasm.c_str(), wasm.size());
780+
auto wast = wasm_to_wast((uint8_t*)wasm.c_str(), wasm.size(), true);
781781
result = string((char *)wast.data(), wast.size());
782782
} catch (...) {
783783
elog("wast_to_wasm failed");

unittests/eosio.system_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ BOOST_FIXTURE_TEST_CASE(producers_upgrade_system_contract, eosio_system_tester)
17131713

17141714
transaction trx;
17151715
{
1716-
auto code = _wast_to_wasm( eosio_system_wast2 );
1716+
auto code = wast_to_wasm( eosio_system_wast2 );
17171717
variant pretty_trx = fc::mutable_variant_object()
17181718
("expiration", "2020-01-01T00:30")
17191719
("ref_block_num", 2)

unittests/multisig_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ BOOST_FIXTURE_TEST_CASE( propose_with_wrong_requested_auth, eosio_msig_tester )
319319

320320
BOOST_FIXTURE_TEST_CASE( big_transaction, eosio_msig_tester ) try {
321321
vector<permission_level> perm = { { N(alice), config::active_name }, { N(bob), config::active_name } };
322-
auto wasm = _wast_to_wasm( eosio_token_wast );
322+
auto wasm = wast_to_wasm( eosio_token_wast );
323323

324324
variant pretty_trx = fc::mutable_variant_object()
325325
("expiration", "2020-01-01T00:30")
@@ -425,7 +425,7 @@ BOOST_FIXTURE_TEST_CASE( update_system_contract_all_approve, eosio_msig_tester )
425425

426426
vector<permission_level> action_perm = {{config::system_account_name, config::active_name}};
427427

428-
auto wasm = _wast_to_wasm( test_api_wast );
428+
auto wasm = wast_to_wasm( test_api_wast );
429429

430430
variant pretty_trx = fc::mutable_variant_object()
431431
("expiration", "2020-01-01T00:30")
@@ -536,7 +536,7 @@ BOOST_FIXTURE_TEST_CASE( update_system_contract_major_approve, eosio_msig_tester
536536

537537
vector<permission_level> action_perm = {{config::system_account_name, config::active_name}};
538538

539-
auto wasm = _wast_to_wasm( test_api_wast );
539+
auto wasm = wast_to_wasm( test_api_wast );
540540

541541
variant pretty_trx = fc::mutable_variant_object()
542542
("expiration", "2020-01-01T00:30")

0 commit comments

Comments
 (0)