99#include " codec/cbor/cbor.hpp"
1010#include " common/outcome.hpp"
1111#include " vm/actor/actor.hpp"
12+ #include " vm/actor/actor_encoding.hpp"
1213#include " vm/exit_code/exit_code.hpp"
1314#include " vm/runtime/runtime.hpp"
1415
15- # define ACTOR_METHOD ( name ) \
16- outcome::result<InvocationOutput> name (Runtime &runtime, \
17- const MethodParams ¶ms)
16+ // / Declare actor method function
17+ # define ACTOR_METHOD_DECL () \
18+ static outcome::result<Result> call (Runtime &, const Params &);
1819
19- namespace fc::vm::actor {
20+ // / Define actor method function
21+ #define ACTOR_METHOD_IMPL (M ) \
22+ outcome::result<M::Result> M::call (Runtime &runtime, const Params ¶ms)
2023
24+ namespace fc::vm::actor {
2125 using common::Buffer;
2226 using runtime::InvocationOutput;
2327 using runtime::Runtime;
@@ -35,31 +39,26 @@ namespace fc::vm::actor {
3539 // / Actor methods exported by number
3640 using ActorExports = std::map<MethodNumber, ActorMethod>;
3741
38- // / Decode actor params, raises appropriate error
39- template <typename T>
40- outcome::result<T> decodeActorParams (MethodParams params_bytes) {
41- auto maybe_params = codec::cbor::decode<T>(params_bytes);
42- if (!maybe_params) {
43- return VMExitCode::DECODE_ACTOR_PARAMS_ERROR;
44- }
45- return maybe_params;
46- }
47-
48- using runtime::encodeActorParams;
42+ // / Actor method base class
43+ template <uint64_t number>
44+ struct ActorMethodBase {
45+ using Params = None;
46+ using Result = None;
47+ static constexpr MethodNumber Number{number};
48+ };
4949
50- template <typename T>
51- outcome::result<T> decodeActorReturn (const InvocationOutput &result) {
52- OUTCOME_TRY (decoded,
53- codec::cbor::decode<T>(result.return_value .toVector ()));
54- return std::move (decoded);
50+ // / Generate export table entry
51+ template <typename M>
52+ auto exportMethod () {
53+ return std::make_pair (
54+ M::Number,
55+ ActorMethod{[](auto &runtime,
56+ auto ¶ms) -> outcome::result<InvocationOutput> {
57+ OUTCOME_TRY (params2, decodeActorParams<typename M::Params>(params));
58+ OUTCOME_TRY (result, M::call (runtime, params2));
59+ return encodeActorReturn (result);
60+ }});
5561 }
56-
57- template <typename T>
58- outcome::result<InvocationOutput> encodeActorReturn (const T &result) {
59- OUTCOME_TRY (encoded, codec::cbor::encode (result));
60- return InvocationOutput{Buffer{encoded}};
61- }
62-
6362} // namespace fc::vm::actor
6463
6564#endif // CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP
0 commit comments