Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from eosnetworkfoundation/backport-support-std…
Browse files Browse the repository at this point in the history
…array

Backport support stdarray
  • Loading branch information
oschwaldp-oci authored Jul 18, 2022
2 parents c170e67 + 49e05b1 commit d359fef
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 1 deletion.
50 changes: 50 additions & 0 deletions tests/toolchain/abigen-pass/using_std_array.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT ",
"version": "eosio::abi/1.2",
"types": [],
"structs": [
{
"name": "greeting",
"base": "",
"fields": [
{
"name": "id",
"type": "uint64"
},
{
"name": "t",
"type": "int32[32]"
}
]
},
{
"name": "hi",
"base": "",
"fields": [
{
"name": "user",
"type": "name"
}
]
}
],
"actions": [
{
"name": "hi",
"type": "hi",
"ricardian_contract": ""
}
],
"tables": [
{
"name": "greeting",
"type": "greeting",
"index_type": "i64",
"key_names": [],
"key_types": []
}
],
"ricardian_clauses": [],
"variants": [],
"action_results": []
}
24 changes: 24 additions & 0 deletions tests/toolchain/abigen-pass/using_std_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <eosio/eosio.hpp>
#include <eosio/print.hpp>
#include <array>

using std::array;
using namespace eosio;

class[[eosio::contract("using_std_array")]] using_std_array : public contract
{
public:
using contract::contract;

[[eosio::action]] void hi(name user) {
require_auth(user);
print("Hello, ", user);
}

struct [[eosio::table]] greeting {
uint64_t id;
array<int, 32> t;
uint64_t primary_key() const { return id; }
};
typedef multi_index<"greeting"_n, greeting> greeting_index;
};
9 changes: 9 additions & 0 deletions tests/toolchain/abigen-pass/using_std_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests" : [
{
"expected" : {
"abi-file" : "using_std_array.abi"
}
}
]
}
1 change: 1 addition & 0 deletions tests/unit/test_contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_contract(action_results_test action_results_test action_results_test.cpp)
add_contract(malloc_tests malloc_tests malloc_tests.cpp)
add_contract(malloc_tests old_malloc_tests malloc_tests.cpp)
add_contract(simple_tests simple_tests simple_tests.cpp)
add_contract(array_tests array_tests array_tests.cpp)
add_contract(transfer_contract transfer_contract transfer.cpp)
add_contract(minimal_tests minimal_tests minimal_tests.cpp)
add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp)
Expand Down
153 changes: 153 additions & 0 deletions tests/unit/test_contracts/array_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include <eosio/eosio.hpp>
#include <eosio/transaction.hpp>
#include <eosio/name.hpp>

using namespace eosio;

class [[eosio::contract]] array_tests : public contract {
public:
using contract::contract;

TABLE tests {
uint64_t id;
std::array<uint8_t,32> str;
uint64_t primary_key() const { return id; }
};

struct info {
int age;
std::string name;
};

typedef multi_index<name("tests"), tests> tests_table;
typedef std::array<std::string,4> array_string_4;
struct my_struct {
uint32_t id;
std::array<array_string_4,2> aastr;

bool operator==(const my_struct& b) const {
return id == b.id &&
aastr == b.aastr;
}
};

// test inside using std::array
[[eosio::action]]
void testin(std::string message) {
tests_table _tests(get_self(), get_self().value);

std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a' };
int len = message.length() < 32 ? message.length() : 32;
for(int i = 0; i < len ; ++i){
str[i] = (uint8_t)message[i];
}

std::array<uint8_t, 32> str2 = str;
eosio::cout << "size of std::array str is : " << str.size() << "\n";
for(int i = 0; i < 32; ++i){
eosio::cout << str[i] << " ";
}
eosio::cout << "\n";
for(int i = 0; i < 32; ++i){
eosio::cout << str2[i] << " ";
}
eosio::cout << "\n";
std::array<info, 2> info_arr;
info_arr[0].age = 20;
info_arr[0].name = "abc";
info_arr[1].age = 21;
info_arr[1].name = "cde";
for(int i = 0; i < 2; ++i){
eosio::cout << info_arr[i].age << " " << info_arr[i].name << "\n";
}
}

// test parameter using std::array
// not supported so far
[[eosio::action]]
void testpa(std::array<int,4> input){
std::array<int,4> arr = input;
for(int i = 0; i < 4; ++i){
eosio::cout << arr[i] << " ";
}
eosio::cout << "\n";
}

// test return using std::array, not supported so far
[[eosio::action]]
// cleos -v push action eosio testre '[[1,2,3,4]]' -p eosio@active
std::array<int,4> testre(std::array<int,4> input){
std::array<int,4> arr = input;
for(auto & v : arr) v += 1;
return arr;
}

// test return using std::vector
[[eosio::action]]
// cleos -v push action eosio testrev '[[1,2,3,4]]' -p eosio@active
std::vector<int> testrev(std::vector<int> input){
std::vector<int> vec = input;
for(auto & v : vec) v += 1;
return vec;
}

// test nested array
[[eosio::action]]
void testne() {
std::array<tests,2> nest;
std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a' };

nest[0].id = 1;
nest[0].str = str;
nest[1].id = 2;
nest[1].str = str;
for(int i = 0; i < nest.size(); ++i){
eosio::cout << nest[i].id << " " ;
for(int j = 0; j < nest[i].str.size(); ++j) {
eosio::cout << nest[i].str[j] + i << " ";
}
eosio::cout << "\n";
}
std::array<std::array<std::string, 5>, 3> nest2;
for(int i = 0; i < nest2.size(); ++i){
for(int j = 0; j < nest2[i].size(); ++j) {
nest2[i][j] = "test nested ";
eosio::cout << nest2[i][j] << " ";
}
eosio::cout << "\n";
}
}

// test complex data
[[eosio::action]]
void testcom(name user) {
require_auth(user);
tests_table _tests(get_self(), get_self().value);

std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a',
'a','a','a','a','a','a','a','a' };
_tests.emplace(user, [&](auto& t) {
t.id = user.value + std::time(0); // primary key can't be same
t.str = str;
});
auto it = _tests.begin();
auto ite = _tests.end();
while(it != ite){
eosio::cout << "id = " << it->id << "\n";
for(int i = 0; i < it->str.size(); ++i) {
eosio::cout << it->str[i] << " ";
}
eosio::cout << "\n";
++it;
}
}

};
4 changes: 3 additions & 1 deletion tools/include/eosio/abigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <memory>
#include <set>
#include <map>

#include <array>
#include <jsoncons/json.hpp>

using namespace llvm;
Expand Down Expand Up @@ -269,6 +269,8 @@ namespace eosio { namespace cdt {
add_pair(type);
else if (is_template_specialization(type, {"tuple"}))
add_tuple(type);
else if (is_template_specialization(type, {"array"}) )
add_type(std::get<clang::QualType>(get_template_argument(type, 0)));
else if (is_template_specialization(type, {"variant"}))
add_variant(type);
else if (is_template_specialization(type, {})) {
Expand Down
14 changes: 14 additions & 0 deletions tools/include/eosio/gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ struct generation_utils {
}
return replace_in_name(ret);
}
else if ( is_template_specialization( type, {"array"} )) {
std::string orig = type.getAsString();
std:: string ret = "";
ret += get_template_argument_as_string( type, 0 );
ret = replace_in_name(ret);
ret += '[';
auto pos1 = orig.find_last_of(',');
auto pos2 = orig.find_last_of('>');
std::string digits = orig.substr(pos1 + 1, pos2 - pos1 - 1);
digits.erase(std::remove(digits.begin(), digits.end(), ' '), digits.end());
ret += digits;
ret += ']';
return ret;
}
else if ( is_template_specialization( type, {} )) {
auto pt = llvm::dyn_cast<clang::ElaboratedType>(type.getTypePtr());
auto tst = llvm::dyn_cast<clang::TemplateSpecializationType>(pt ? pt->desugar().getTypePtr() : type.getTypePtr() );
Expand Down

0 comments on commit d359fef

Please sign in to comment.