Mana
Actor.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "Buffer.h"
10 #include "Event.h"
11 #include "Stack.h"
12 
13 #include <bitset>
14 #include <map>
15 #include <string>
16 #include <unordered_map>
17 
18 #include <cstdio>
19 #include <cstring>
20 
21 namespace mana
22 {
23  class VM;
24 
28  class Actor : Noncopyable, public std::enable_shared_from_this<Actor>
29  {
30  friend class VM;
31 
33  explicit Actor(const std::shared_ptr<VM>& vm, const address_t variableSize);
34 
35  public:
37  using Callback = int32_t (*)(const std::shared_ptr<Actor>& actor, void* parameter);
38 
39  public:
40  Actor(const Actor&) = delete;
41  Actor(Actor&&) noexcept = delete;
42  Actor& operator=(const Actor&) = delete;
43  Actor& operator=(Actor&&) noexcept = delete;
44  virtual ~Actor() = default;
45 
46  std::shared_ptr<Actor> Clone() const;
47 
48  bool Run();
49  bool SyncCall(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
50  bool AsyncCall(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
51 
52  bool Request(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
53  void Rollback(const int32_t priority);
54  void Restart();
55 
56  const std::string_view& GetName();
57  uint32_t GetAction(const std::string_view& actionName) const;
58 
59  [[nodiscard]] int32_t GetArgumentCount() const;
60  [[nodiscard]] int32_t GetArgumentCountByAddress(const uint32_t address) const;
61  [[nodiscard]] int32_t GetArgumentSize(const uint32_t address);
62  [[nodiscard]] bool HasReturnValue(const uint32_t address);
63  [[nodiscard]] int32_t GetParameterInteger(const int32_t value);
64  [[nodiscard]] float GetParameterFloat(const int32_t value);
65  [[nodiscard]] const char* GetParameterString(const int32_t value);
66  [[nodiscard]] Actor* GetParameterActor(const int32_t value);
67  [[nodiscard]] void* GetParameterPointer(const int32_t value);
68  [[nodiscard]] void* GetParameterAddress(const int32_t value);
69  void SetReturnInteger(const int32_t value);
70  void SetReturnFloat(const float value);
71  void SetReturnString(const char* string);
72  void SetReturnActor(const std::shared_ptr<Actor>& actor);
73  void SetReturnPointer(void* pointer);
74  void SetReturnData(const void* pointer, const int32_t size);
75 
76 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
77  [[nodiscard]] std::string_view GetActorName() const;
78  void SetActorName(const std::string_view& name);
79  [[nodiscard]] std::string GetActionName() const;
80  [[nodiscard]] const std::string_view GetFunctionName() const;
81 #endif
82 
83  std::shared_ptr<VM> GetVirtualMachine() const;
84  [[nodiscard]] bool IsCommandInitialized() const;
85  [[nodiscard]] bool IsCommandRepeat() const;
86  [[nodiscard]] bool IsRunning() const;
87  void Repeat(const bool initialComplete);
88  void Again();
89  void Halt();
90  void Stop();
91  void yield();
92  void Accept();
93  void Refuse();
94  [[nodiscard]] int32_t GetInterruptPriority() const;
95  [[nodiscard]] bool IsSynchronized() const;
96  void SetSynchronized(const bool synchronized);
97  void SetSynchronizedWithPriority(const int32_t priority, const bool synchronized);
98 
99  [[nodiscard]] EventNameType AddRequestEvent(const std::function<void(int32_t)>& function);
100  void RemoveRequestEvent(const EventNameType eventName);
101  [[nodiscard]] EventNameType AddRollbackEvent(const std::function<void(int32_t)>& function);
102  void RemoveRollbackEvent(const EventNameType eventName);
103 
104  [[nodiscard]] uintptr_t GetUserData() const;
105  void SetUserData(const uintptr_t userData);
106  [[nodiscard]] void* GetUserPointer() const;
107  void SetUserPointer(void* userPointer);
108 
109  [[nodiscard]] Stack& GetStack();
110  [[nodiscard]] const Stack& GetStack() const;
111 
112  private:
113  void SetAction(const std::string_view& actionName, const uint32_t address);
114  //void Initialize(const ActionInfoHeader* actionInfo);
115 
116  private:
117  static constexpr uint32_t Nil = static_cast<uint32_t>(~0);
118 
120  struct Interrupt final
121  {
122  std::shared_ptr<Actor> mSender;
123  uint32_t mAddress = Nil;
124  uint32_t mReturnAddress = Nil;
125  address_t mFramePointer = Nil;
126  address_t mStackPointer = Nil;
127  void* mFileCallbackParameter = nullptr;
128  std::bitset<8> mFlag = 0;
129 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
130  std::string mActionName;
131 #endif
132 
133  enum class Flag : uint8_t
134  {
135  Initialized,
136  IsInSyncCall,
137  Repeat,
138  Suspend,
139  Synchronized
140  };
141  };
142 
144  struct ReturnValue final
145  {
146  union
147  {
148  int_t mIntegerValue;
149  float_t mFloatValue;
150  const char* mStringValue;
151  void* mPointerValue;
152  Actor* mActorValue;
153  } mValues;
154  int32_t mSize;
155 
156  static constexpr int32_t Invalid = 0;
157  static constexpr int32_t Actor = -1;
158  static constexpr int32_t Integer = -2;
159  static constexpr int32_t Float = -3;
160  static constexpr int32_t String = -4;
161 
162  ReturnValue()
163  : mSize(0)
164  {
165  mValues.mPointerValue = nullptr;
166  }
167  };
168 
169  std::weak_ptr<VM> mVM;
170  std::unordered_map <std::string_view, uint32_t> mActions;
171  Buffer mFrame;
172  Stack mStack;
173  std::map<int32_t, Interrupt> mInterrupts;
174  ReturnValue mReturnValue;
175  Event<int32_t> mRequestEvent;
176  Event<int32_t> mRollbackEvent;
177  Buffer mVariable;
178  address_t mPc = Nil;
179  int32_t mInterruptPriority = 0;
180  std::bitset<8> mFlag = 0;
181  enum class Flag : uint8_t
182  {
183  Halt,
184  Running,
185  Touched,
186  Hided,
187  Requested,
188  Refused
189  };
190  uintptr_t mUserData = 0;
191  void* mUserPointer = nullptr;
192 
193 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
194  std::string_view mName;
195 #endif
196 
197  private:
198  static void CommandRestart(const std::shared_ptr<VM>& vm, Actor& self);
199  static void CommandHalt(const std::shared_ptr<VM>& vm, Actor& self);
200  static void CommandYield(const std::shared_ptr<VM>& vm, Actor& self);
201  static void CommandSetNonPreemptive(const std::shared_ptr<VM>& vm, Actor& self);
202  static void CommandSetPreemptive(const std::shared_ptr<VM>& vm, Actor& self);
203  static void CommandPushZeroInteger(const std::shared_ptr<VM>& vm, Actor& self);
204  static void CommandPushZeroFloat(const std::shared_ptr<VM>& vm, Actor& self);
205  static void CommandPushChar(const std::shared_ptr<VM>& vm, Actor& self);
206  static void CommandPushShort(const std::shared_ptr<VM>& vm, Actor& self);
207  static void CommandPushInteger(const std::shared_ptr<VM>& vm, Actor& self);
208  static void CommandPushFloat(const std::shared_ptr<VM>& vm, Actor& self);
209  static void CommandPushString(const std::shared_ptr<VM>& vm, Actor& self);
210  static void CommandPushPriority(const std::shared_ptr<VM>& vm, Actor& self);
211  static void CommandPushSelf(const std::shared_ptr<VM>& vm, Actor& self);
212  static void CommandPushSender(const std::shared_ptr<VM>& vm, Actor& self);
213  static void CommandAllocate(const std::shared_ptr<VM>& vm, Actor& self);
214  static void CommandRelease(const std::shared_ptr<VM>& vm, Actor& self);
215  static void CommandDuplicate(const std::shared_ptr<VM>& vm, Actor& self);
216  static void CommandRemove(const std::shared_ptr<VM>& vm, Actor& self);
217  static void CommandLoadStaticAddress(const std::shared_ptr<VM>& vm, Actor& self);
218  static void CommandLoadGlobalAddress(const std::shared_ptr<VM>& vm, Actor& self);
219  static void CommandLoadFrameAddress(const std::shared_ptr<VM>& vm, Actor& self);
220  static void CommandLoadSelfAddress(const std::shared_ptr<VM>& vm, Actor& self);
221  static void CommandLoadInt8(const std::shared_ptr<VM>& vm, Actor& self);
222  static void CommandLoadInt16(const std::shared_ptr<VM>& vm, Actor& self);
223  static void CommandLoadInt32(const std::shared_ptr<VM>& vm, Actor& self);
224  static void CommandLoadFloat(const std::shared_ptr<VM>& vm, Actor& self);
225  static void CommandLoadReference(const std::shared_ptr<VM>& vm, Actor& self);
226  static void CommandStoreInt8(const std::shared_ptr<VM>& vm, Actor& self);
227  static void CommandStoreInt16(const std::shared_ptr<VM>& vm, Actor& self);
228  static void CommandStoreInt32(const std::shared_ptr<VM>& vm, Actor& self);
229  static void CommandStoreFloat(const std::shared_ptr<VM>& vm, Actor& self);
230  static void CommandStoreReference(const std::shared_ptr<VM>& vm, Actor& self);
231  static void CommandBranchEqual(const std::shared_ptr<VM>& vm, Actor& self);
232  static void CommandBranchNotEqual(const std::shared_ptr<VM>& vm, Actor& self);
233  static void CommandBranchAway(const std::shared_ptr<VM>& vm, Actor& self);
234  static void CommandBranchSubRoutine(const std::shared_ptr<VM>& vm, Actor& self);
235  static void CommandCall(const std::shared_ptr<VM>& vm, Actor& self);
236  static void CommandRequest(const std::shared_ptr<VM>& vm, Actor& self);
237  static void CommandRequestWaitStarting(const std::shared_ptr<VM>& vm, Actor& self);
238  static void CommandRequestWaitEnding(const std::shared_ptr<VM>& vm, Actor& self);
239  static void CommandDynamicRequest(const std::shared_ptr<VM>& vm, Actor& self);
240  static void CommandDynamicRequestWaitStarting(const std::shared_ptr<VM>& vm, Actor& self);
241  static void CommandDynamicRequestWaitEnded(const std::shared_ptr<VM>& vm, Actor& self);
242  static void CommandJoin(const std::shared_ptr<VM>& vm, Actor& self);
243  static void CommandComply(const std::shared_ptr<VM>& vm, Actor& self);
244  static void CommandRefuse(const std::shared_ptr<VM>& vm, Actor& self);
245  static void CommandLoadReturnAddress(const std::shared_ptr<VM>& vm, Actor& self);
246  static void CommandStoreReturnAddress(const std::shared_ptr<VM>& vm, Actor& self);
247  static void CommandReturnFromFunction(const std::shared_ptr<VM>& vm, Actor& self);
248  static void CommandReturnFromAction(const std::shared_ptr<VM>& vm, Actor& self);
249  static void CommandRollback(const std::shared_ptr<VM>& vm, Actor& self);
250  static void CommandAddInteger(const std::shared_ptr<VM>& vm, Actor& self);
251  static void CommandAddFloat(const std::shared_ptr<VM>& vm, Actor& self);
252  static void CommandDivideInteger(const std::shared_ptr<VM>& vm, Actor& self);
253  static void CommandDivideFloat(const std::shared_ptr<VM>& vm, Actor& self);
254  static void CommandMinusInteger(const std::shared_ptr<VM>& vm, Actor& self);
255  static void CommandMinusFloat(const std::shared_ptr<VM>& vm, Actor& self);
256  static void CommandModInteger(const std::shared_ptr<VM>& vm, Actor& self);
257  static void CommandModFloat(const std::shared_ptr<VM>& vm, Actor& self);
258  static void CommandMultiplyInteger(const std::shared_ptr<VM>& vm, Actor& self);
259  static void CommandMultiplyFloat(const std::shared_ptr<VM>& vm, Actor& self);
260  static void CommandSubtractInteger(const std::shared_ptr<VM>& vm, Actor& self);
261  static void CommandSubtractFloat(const std::shared_ptr<VM>& vm, Actor& self);
262  static void CommandAnd(const std::shared_ptr<VM>& vm, Actor& self);
263  static void CommandExclusiveOr(const std::shared_ptr<VM>& vm, Actor& self);
264  static void CommandLogicalAnd(const std::shared_ptr<VM>& vm, Actor& self);
265  static void CommandLogicalOr(const std::shared_ptr<VM>& vm, Actor& self);
266  static void CommandNot(const std::shared_ptr<VM>& vm, Actor& self);
267  static void CommandLogicalNot(const std::shared_ptr<VM>& vm, Actor& self);
268  static void CommandOr(const std::shared_ptr<VM>& vm, Actor& self);
269  static void CommandShiftLeft(const std::shared_ptr<VM>& vm, Actor& self);
270  static void CommandShiftRight(const std::shared_ptr<VM>& vm, Actor& self);
271  static void CommandCompareEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
272  static void CommandCompareEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
273  static void CommandCompareGreaterEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
274  static void CommandCompareGreaterEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
275  static void CommandCompareGreaterInteger(const std::shared_ptr<VM>& vm, Actor& self);
276  static void CommandCompareGreaterFloat(const std::shared_ptr<VM>& vm, Actor& self);
277  static void CommandCompareNotEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
278  static void CommandCompareNotEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
279  static void CommandCompareLessEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
280  static void CommandCompareLessEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
281  static void CommandCompareLessInteger(const std::shared_ptr<VM>& vm, Actor& self);
282  static void CommandCompareLessFloat(const std::shared_ptr<VM>& vm, Actor& self);
283  static void CommandIntegerToFloat(const std::shared_ptr<VM>& vm, Actor& self);
284  static void CommandFloatToInteger(const std::shared_ptr<VM>& vm, Actor& self);
285  static void CommandPushActor(const std::shared_ptr<VM>& vm, Actor& self);
286  static void CommandLoadData(const std::shared_ptr<VM>& vm, Actor& self);
287  static void CommandStoreData(const std::shared_ptr<VM>& vm, Actor& self);
288  static void CommandDuplicateData(const std::shared_ptr<VM>& vm, Actor& self);
289  static void CommandRemoveData(const std::shared_ptr<VM>& vm, Actor& self);
290  static void CommandCompareEqualData(const std::shared_ptr<VM>& vm, Actor& self);
291  static void CommandCompareGreaterEqualData(const std::shared_ptr<VM>& vm, Actor& self);
292  static void CommandCompareGreaterData(const std::shared_ptr<VM>& vm, Actor& self);
293  static void CommandCompareNotEqualData(const std::shared_ptr<VM>& vm, Actor& self);
294  static void CommandCompareLessEqualData(const std::shared_ptr<VM>& vm, Actor& self);
295  static void CommandCompareLessData(const std::shared_ptr<VM>& vm, Actor& self);
296  static void CommandPrint(const std::shared_ptr<VM>& vm, Actor& self);
297  };
298 }
299 
300 #if MANA_BUILD_TARGET > MANA_BUILD_DEBUG
302 #define MANA_ASSERT_PARAMETER(P, I) { \
303  if(P->GetArgumentCount() != I) \
304  return; \
305 }
307 #define MANA_ASSERT_ILLIGAL_CALL_IN_INIT_ACTION(P) { \
308  if(P->GetVirtualMachine()->IsInInitAction()){ \
309  return; \
310 }
311 #else
313 #define MANA_ASSERT_PARAMETER(P, I) \
314  if((P)->GetArgumentCount() != (I)){ \
315  MANA_PRINT({ "ERROR: ", (P)->GetName(), ": function ", (P)->GetFunctionName(), " number of arguments ", std::to_string((P)->GetArgumentCount()), " correct ", std::to_string(I), "\n" });\
316  return; \
317  }
319 #define MANA_ASSERT_CANT_CALL_IN_INIT_ACTION(P) \
320  if((P)->GetVirtualMachine()->IsInInitAction()){ \
321  MANA_PRINT({ "ERROR: ", (P)->GetName(), ": init action ", (P)->GetFunctionName()," can not call\n" });\
322  return; \
323  }
324 #endif
325 
326 #include "Actor.inl"
Definition: Actor.h:29
bool Run()
Definition: Actor.inl:31
const std::string_view & GetName()
Definition: Actor.inl:500
void RemoveRollbackEvent(const EventNameType eventName)
Definition: Actor.inl:762
int32_t GetInterruptPriority() const
Definition: Actor.inl:716
void SetReturnActor(const std::shared_ptr< Actor > &actor)
Definition: Actor.inl:591
void * GetUserPointer() const
Definition: Actor.inl:777
void SetUserData(const uintptr_t userData)
Definition: Actor.inl:772
void Again()
Definition: Actor.inl:679
bool IsRunning() const
Definition: Actor.inl:661
void SetReturnString(const char *string)
Definition: Actor.inl:585
void Repeat(const bool initialComplete)
Definition: Actor.inl:668
uintptr_t GetUserData() const
Definition: Actor.inl:767
void yield()
Definition: Actor.inl:700
void SetUserPointer(void *userPointer)
Definition: Actor.inl:782
void Stop()
Definition: Actor.inl:695
void Restart()
Definition: Actor.inl:488
bool SyncCall(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:223
void SetSynchronized(const bool synchronized)
Definition: Actor.inl:729
void SetSynchronizedWithPriority(const int32_t priority, const bool synchronized)
Definition: Actor.inl:738
bool IsCommandRepeat() const
Definition: Actor.inl:653
int32_t GetParameterInteger(const int32_t value)
Definition: Actor.inl:536
void SetReturnFloat(const float value)
Definition: Actor.inl:579
bool Request(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:253
float GetParameterFloat(const int32_t value)
Definition: Actor.inl:544
bool IsCommandInitialized() const
Definition: Actor.inl:645
Actor * GetParameterActor(const int32_t value)
Definition: Actor.inl:556
int32_t GetArgumentCountByAddress(const uint32_t address) const
Definition: Actor.inl:521
bool IsSynchronized() const
Definition: Actor.inl:721
Actor(const Actor &)=delete
int32_t GetArgumentSize(const uint32_t address)
Definition: Actor.inl:526
void SetReturnPointer(void *pointer)
Definition: Actor.inl:597
const char * GetParameterString(const int32_t value)
Definition: Actor.inl:550
bool HasReturnValue(const uint32_t address)
Definition: Actor.inl:531
void SetReturnInteger(const int32_t value)
Definition: Actor.inl:573
void SetReturnData(const void *pointer, const int32_t size)
Definition: Actor.inl:603
void * GetParameterPointer(const int32_t value)
Definition: Actor.inl:561
Actor(Actor &&) noexcept=delete
EventNameType AddRollbackEvent(const std::function< void(int32_t)> &function)
Definition: Actor.inl:757
void Rollback(const int32_t priority)
Definition: Actor.inl:358
void Refuse()
Definition: Actor.inl:711
std::shared_ptr< Actor > Clone() const
Definition: Actor.inl:21
bool AsyncCall(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:238
void Accept()
Definition: Actor.inl:706
int32_t GetArgumentCount() const
Definition: Actor.inl:516
Stack & GetStack()
Definition: Actor.inl:787
std::shared_ptr< VM > GetVirtualMachine() const
Definition: Actor.inl:640
void * GetParameterAddress(const int32_t value)
Definition: Actor.inl:567
void Halt()
Definition: Actor.inl:685
uint32_t GetAction(const std::string_view &actionName) const
Definition: Actor.inl:505
void RemoveRequestEvent(const EventNameType eventName)
Definition: Actor.inl:752
EventNameType AddRequestEvent(const std::function< void(int32_t)> &function)
Definition: Actor.inl:747
int32_t(*)(const std::shared_ptr< Actor > &actor, void *parameter) Callback
request / rollbackコールバック
Definition: Actor.h:37
Definition: Stack.h:23
Definition: VM.h:21
Definition: CodeBuffer.cpp:12
std::uint32_t address_t
Definition: Type.h:31
std::int32_t int_t
Definition: Type.h:29
float float_t
Definition: Type.h:28
uint32_t EventNameType
Definition: Event.h:15
Definition: Noncopyable.h:18