提交 0694f5f7 编写于 作者: M Matias Romeo

abi_generator: Add detection of actions in instance methods

issue #1891
上级 497be902
......@@ -48,36 +48,6 @@
"fields": [
{"name":"value", "type":"string"}
]
},{
"name": "transfer",
"base": "",
"fields": [
{"name":"from", "type":"account_name"},
{"name":"to", "type":"account_name"},
{"name":"quantity", "type":"asset"},
{"name":"memo", "type":"string"}
]
},{
"name": "issue",
"base": "",
"fields": [
{"name":"to", "type":"account_name"},
{"name":"quantity", "type":"asset"}
]
},{
"name": "account",
"base": "",
"fields": [
{"name":"currency", "type":"uint64"},
{"name":"balance", "type":"uint64"}
]
},{
"name": "currency_stats",
"base": "",
"fields": [
{"name":"currency", "type":"uint64"},
{"name":"supply", "type":"uint64"}
]
}],
"actions": [{
"name": "setalimits",
......@@ -97,25 +67,7 @@
},{
"name": "nonce",
"type": "nonce"
},{
"name": "transfer",
"type": "transfer"
},{
"name": "issue",
"type": "issue"
}
],
"tables": [{
"name": "account",
"type": "account",
"index_type": "i64",
"key_names" : ["currency"],
"key_types" : ["uint64"]
},{
"name": "stat",
"type": "currency_stats",
"index_type": "i64",
"key_names" : ["currency"],
"key_types" : ["uint64"]
}]
"tables": []
}
......@@ -77,23 +77,89 @@ string abi_generator::translate_type(const string& type_name) {
return built_in_type;
}
bool abi_generator::inspect_type_methods_for_actions(const Decl* decl) { try {
const auto* rec_decl = dyn_cast<CXXRecordDecl>(decl);
if(rec_decl == nullptr) return false;
const auto* type = rec_decl->getTypeForDecl();
ABI_ASSERT(type != nullptr);
if( !type->isStructureOrClassType() ) {
return false;
}
bool at_least_one_action = false;
for(const auto* method : rec_decl->methods()) {
const RawComment* raw_comment = ast_context->getRawCommentForDeclNoCache(method);
if( raw_comment == nullptr) continue;
SourceManager& source_manager = ast_context->getSourceManager();
string raw_text = raw_comment->getRawText(source_manager);
regex r(R"(@abi (action))");
smatch smatch;
if(regex_search(raw_text, smatch, r)){
auto method_name = method->getNameAsString();
ABI_ASSERT(find_struct(method_name) == nullptr, "action already exists ${method_name}", ("method_name",method_name));
struct_def abi_struct;
for(const auto* p : method->parameters() ) {
clang::QualType qt = p->getOriginalType().getNonReferenceType();
qt.setLocalFastQualifiers(0);
string field_name = p->getNameAsString();
string field_type_name = add_type(qt);
field_def struct_field{field_name, field_type_name};
ABI_ASSERT(is_builtin_type(get_vector_element_type(struct_field.type))
|| find_struct(get_vector_element_type(struct_field.type))
|| find_type(get_vector_element_type(struct_field.type))
, "Unknown type ${type} [${abi}]",("type",struct_field.type)("abi",*output));
type_size[string(struct_field.type)] = is_vector(struct_field.type) ? 0 : ast_context->getTypeSize(qt);
abi_struct.fields.push_back(struct_field);
}
abi_struct.name = method_name;
abi_struct.base = "";
output->structs.push_back(abi_struct);
full_types[method_name] = method_name;
output->actions.push_back({method_name, method_name});
at_least_one_action = true;
}
}
return at_least_one_action;
} FC_CAPTURE_AND_RETHROW() }
void abi_generator::handle_decl(const Decl* decl) { try {
ABI_ASSERT(decl != nullptr);
ABI_ASSERT(output != nullptr);
ABI_ASSERT(ast_context != nullptr);
//ASTContext& ctx = decl->getASTContext();
const RawComment* raw_comment = ast_context->getRawCommentForDeclNoCache(decl);
if(!raw_comment) return;
SourceManager& source_manager = ast_context->getSourceManager();
auto file_name = source_manager.getFilename(raw_comment->getLocStart());
auto file_name = source_manager.getFilename(decl->getLocStart());
if ( !abi_context.empty() && !file_name.startswith(abi_context) ) {
return;
}
if(inspect_type_methods_for_actions(decl)) {
return;
}
const RawComment* raw_comment = ast_context->getRawCommentForDeclNoCache(decl);
if(raw_comment == nullptr) {
return;
}
string raw_text = raw_comment->getRawText(source_manager);
regex r(R"(@abi (action|table)((?: [a-z0-9]+)*))");
......
......@@ -124,6 +124,7 @@ namespace eosio {
void handle_tagdecl_definition(TagDecl* tag_decl);
private:
bool inspect_type_methods_for_actions(const Decl* decl);
string remove_namespace(const string& full_name);
......
......@@ -105,7 +105,7 @@ struct dice_tester : tester {
action act( {{account, config::active_name}},
offer_bet_t{amount, account, commitment} );
trx.actions.push_back(act);
set_tapos(trx);
set_transaction_headers(trx);
trx.sign(get_private_key( account, "active" ), chain_id_type());
control->push_transaction(packed_transaction(trx,packed_transaction::none));
}
......@@ -115,7 +115,7 @@ struct dice_tester : tester {
action act( {{account, config::active_name}},
cancel_offer_t{commitment} );
trx.actions.push_back(act);
set_tapos(trx);
set_transaction_headers(trx);
trx.sign(get_private_key( account, "active" ), chain_id_type());
control->push_transaction(packed_transaction(trx,packed_transaction::none));
}
......@@ -125,7 +125,7 @@ struct dice_tester : tester {
action act( {{account, config::active_name}},
deposit_t{account, amount} );
trx.actions.push_back(act);
set_tapos(trx);
set_transaction_headers(trx);
trx.sign(get_private_key( account, "active" ), chain_id_type());
control->push_transaction(packed_transaction(trx,packed_transaction::none));
}
......@@ -135,7 +135,7 @@ struct dice_tester : tester {
action act( {{account, config::active_name}},
withdraw_t{account, amount} );
trx.actions.push_back(act);
set_tapos(trx);
set_transaction_headers(trx);
trx.sign(get_private_key( account, "active" ), chain_id_type());
control->push_transaction(packed_transaction(trx,packed_transaction::none));
}
......@@ -145,7 +145,7 @@ struct dice_tester : tester {
action act( {{account, config::active_name}},
reveal_t{commitment, source} );
trx.actions.push_back(act);
set_tapos(trx);
set_transaction_headers(trx);
trx.sign(get_private_key( account, "active" ), chain_id_type());
control->push_transaction(packed_transaction(trx,packed_transaction::none));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册