This commit is contained in:
radrow 2021-05-11 14:19:59 +02:00
parent 9b1b36f4c1
commit a77261c1d6
3 changed files with 33 additions and 4 deletions

View File

@ -327,12 +327,12 @@ to_fcode(Env, [{Contract, Attrs, Con = {con, _, Name}, Decls}|Rest])
when ?IS_CONTRACT_HEAD(Contract) -> when ?IS_CONTRACT_HEAD(Contract) ->
case Contract =:= contract_interface of case Contract =:= contract_interface of
false -> false ->
#{ builtins := Builtins } = Env, #{ builtins := Builtins } = Env,
ConEnv = Env#{ context => {main_contract, Name}, ConEnv = Env#{ context => {main_contract, Name},
builtins => Builtins#{[Name, "state"] => {get_state, none}, builtins => Builtins#{[Name, "state"] => {get_state, none},
[Name, "put"] => {set_state, 1}, [Name, "put"] => {set_state, 1},
[Name, "Chain", "event"] => {chain_event, 1}} }, [Name, "Chain", "event"] => {chain_event, 1}} },
#{ functions := PrevFuns } = ConEnv,
#{ functions := Funs } = Env1 = #{ functions := Funs } = Env1 =
decls_to_fcode(ConEnv, Decls), decls_to_fcode(ConEnv, Decls),
StateType = lookup_type(Env1, [Name, "state"], [], {tuple, []}), StateType = lookup_type(Env1, [Name, "state"], [], {tuple, []}),
@ -350,7 +350,8 @@ to_fcode(Env, [{Contract, Attrs, Con = {con, _, Name}, Decls}|Rest])
contract_main -> Rest = [], {Env1, ConFcode}; contract_main -> Rest = [], {Env1, ConFcode};
contract_child -> contract_child ->
Env2 = add_child_con(Env1, Name, ConFcode), Env2 = add_child_con(Env1, Name, ConFcode),
to_fcode(Env2, Rest) Env3 = Env2#{ functions := PrevFuns },
to_fcode(Env3, Rest)
end; end;
true -> true ->
Env1 = decls_to_fcode(Env#{ context => {abstract_contract, Con} }, Decls), Env1 = decls_to_fcode(Env#{ context => {abstract_contract, Con} }, Decls),

View File

@ -26,14 +26,14 @@ run_test(Test) ->
simple_compile_test_() -> simple_compile_test_() ->
[ {"Testing the " ++ ContractName ++ " contract with the " ++ atom_to_list(Backend) ++ " backend", [ {"Testing the " ++ ContractName ++ " contract with the " ++ atom_to_list(Backend) ++ " backend",
fun() -> fun() ->
case compile(Backend, ContractName, [pp_assembler]) of case compile(Backend, ContractName) of
#{byte_code := ByteCode, #{byte_code := ByteCode,
contract_source := _, contract_source := _,
type_info := _} when Backend == aevm -> type_info := _} when Backend == aevm ->
?assertMatch(Code when is_binary(Code), ByteCode); ?assertMatch(Code when is_binary(Code), ByteCode);
#{fate_code := Code} when Backend == fate -> #{fate_code := Code} when Backend == fate ->
Code1 = aeb_fate_code:deserialize(aeb_fate_code:serialize(Code)), Code1 = aeb_fate_code:deserialize(aeb_fate_code:serialize(Code)),
?assertMatch({X, X}, {Code1, Code}), error(xd); ?assertMatch({X, X}, {Code1, Code});
ErrBin -> ErrBin ->
io:format("\n~s", [ErrBin]), io:format("\n~s", [ErrBin]),
error(ErrBin) error(ErrBin)

28
test/contracts/create.aes Normal file
View File

@ -0,0 +1,28 @@
contract IntegerAdder =
entrypoint init() = ()
entrypoint addIntegers(x, y) = x + y
contract IntegerAdderHolder =
type state = IntegerAdder
stateful entrypoint init() = Chain.create() : IntegerAdder
entrypoint get() = state
contract IntegerAdderFactory =
entrypoint init() = ()
stateful entrypoint new() =
let i = Chain.create() : IntegerAdderHolder
i.get()
payable contract ValueAdder =
entrypoint init() = ()
stateful entrypoint addValue(x) =
let integerAdderFactory = Chain.create()
let adder = integerAdderFactory.new()
adder.addIntegers(x, Contract.balance)
main contract EnterpriseContract =
entrypoint init() = ()
stateful payable entrypoint increaseByThree(x) =
require(Call.value >= 3, "Price for addition = 3AEtto, insufficient funds")
let threeAdder = Chain.create(value = 3)
threeAdder.addValue(x)