forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasActors.h
320 lines (254 loc) · 7.11 KB
/
asActors.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* File: asVars.h
* Author: ghernan
*
* Async script basic data types. Contains the data types related to actor system.
* The rest are in 'jsVars.*'
*
* Created on December 25, 2016, 12:53 PM
*/
#ifndef ASVARS_H
#define ASVARS_H
#pragma once
#include "asObjects.h"
#include "microVM.h"
class AsEndPoint;
class MvmRoutine;
/**
* Actor class runtime object
*/
class AsActorClass : public JSValueBase<VT_ACTOR_CLASS>
{
public:
static Ref<AsActorClass> create (const std::string& name,
const VarMap& members,
const StringVector& params);
//virtual ASValue call (Ref<FunctionScope> scope);
virtual StringSet getFields(bool inherited = true)const;
virtual ASValue readField(const std::string& key)const;
Ref<AsEndPoint> getEndPoint (const std::string& name);
Ref<AsEndPoint> getConstructor ()
{
return getEndPoint("@start");
}
virtual const StringVector& getParams()const
{
return m_params;
}
virtual const std::string& getName()const
{
return m_name;
}
protected:
AsActorClass(const std::string& name,
const VarMap& members,
const StringVector& params);
static VarMap createDefaultEndPoints (const VarMap& members);
private:
std::string m_name;
VarMap m_members;
StringVector m_params;
};
class AsActorRef;
class AsEndPointRef;
typedef std::vector < Ref<AsActorRef> > AsActorList;
/**
* Actor runtime object
*/
class AsActor : public JSValueBase<VT_ACTOR>
{
public:
static Ref<AsActor> create (Ref<AsActorClass> cls,
Ref<GlobalScope> globals,
Ref<AsActorRef> parent)
{
return refFromNew(new AsActor(cls, globals, parent));
}
virtual JSValueTypes getType()const
{
return VT_ACTOR;
}
virtual ASValue readField(const std::string& key)const;
virtual ASValue writeField(const std::string& key, ASValue value, bool isConst);
void setOutputConnection (const std::string& msgName, Ref<AsEndPointRef> dst)
{
m_outputConections[msgName] = dst;
}
Ref<AsEndPointRef> getConnectedEp (const std::string& msgName)const;
bool isRunning()const
{
return !m_finished;
}
void stop(ASValue result, ASValue error);
ASValue getResult()
{
return m_result;
}
ASValue getError()
{
return m_error;
}
Ref<AsEndPoint> getEndPoint (const std::string& name)const
{
return m_cls->getEndPoint(name);
}
Ref<GlobalScope> getGlobals()const
{
return m_globals;
}
Ref<AsActorRef> getParent()const
{
return m_parent;
}
protected:
AsActor (Ref<AsActorClass> cls, Ref<GlobalScope> globals, Ref<AsActorRef> parent) :
m_cls (cls)
, m_globals (globals)
, m_parent (parent)
, m_result(jsNull())
, m_error(jsNull())
, m_finished(false)
{
}
private:
const Ref<AsActorClass> m_cls;
Ref<GlobalScope> m_globals;
Ref<AsActorRef> m_parent;
VarMap m_members;
AsActorList m_childActors;
typedef std::map<std::string, Ref<AsEndPointRef> > ConnectionMap;
ConnectionMap m_outputConections;
ASValue m_result;
ASValue m_error;
bool m_finished;
};
/**
* Actor reference object
* @param cls
* @return
*/
class AsActorRef : public JSValueBase<VT_ACTOR_REF>
{
public:
static Ref<AsActorRef> create (Ref<AsActor> actor)
{
return refFromNew(new AsActorRef(actor));
}
virtual ASValue readField(const std::string& key)const
{
return getEndPoint(key);
}
bool isRunning()
{
return m_ref->isRunning();
}
Ref<AsActor> getActor()const
{
return m_ref;
}
ASValue getResult()
{
if (isRunning())
return jsNull();
else
return m_ref->getResult();
}
ASValue getError()
{
if (isRunning())
return jsNull();
else
return m_ref->getError();
}
Ref<AsEndPointRef> getEndPoint (const std::string& name)const;
protected:
AsActorRef (Ref<AsActor> actor) : m_ref (actor)
{
}
private:
const Ref<AsActor> m_ref;
};
/**
* Message input or output end point object
*/
class AsEndPoint : public JSFunction
{
public:
static Ref<AsEndPoint> create (const std::string& name,
const StringVector& params,
bool input)
{
return refFromNew (new AsEndPoint (name, params, input));
}
static Ref<AsEndPoint> createInput (const std::string& name,
const StringVector& params,
Ref<MvmRoutine> code)
{
return refFromNew (new AsEndPoint (name, params, code));
}
static Ref<AsEndPoint> createNative(const std::string& name,
const StringVector& params,
JSNativeFn fnPtr
)
{
return refFromNew (new AsEndPoint (name, params, fnPtr));
}
bool isInput()const
{
return m_isInput;
}
virtual JSValueTypes getType()const
{
return isInput() ? VT_INPUT_EP : VT_OUTPUT_EP;
}
virtual std::string toString()const;
protected:
AsEndPoint (const std::string& name, const StringVector& params, bool input) :
JSFunction(name, params, NULL), m_isInput (input)
{
}
AsEndPoint (const std::string& name, const StringVector& params, Ref<MvmRoutine> code);
AsEndPoint (const std::string& name, const StringVector& params, JSNativeFn pNative);
private:
const bool m_isInput;
};
//TODO: May be, I am abusing of subclassing from 'JSFunction'. There should
//be something like a 'JSCallable' base class.
/**
* Async Script message reference.
* Holds a reference to the message, and to the actor.
*/
class AsEndPointRef : public JSValueBase<VT_INPUT_EP_REF>
{
public:
static Ref<AsEndPointRef> create (Ref<AsEndPoint> endPoint, Ref<AsActorRef> actor)
{
return refFromNew (new AsEndPointRef (endPoint, actor));
}
bool isInput()const
{
return m_endPoint->isInput();
}
virtual JSValueTypes getType()const
{
return (m_endPoint->getType() == VT_OUTPUT_EP) ? VT_OUTPUT_EP_REF : VT_INPUT_EP_REF;
}
ASValue call (Ref<FunctionScope> scope);
Ref<AsActorRef> getActor()const
{
return m_actor;
}
Ref<AsEndPoint> getEndPoint()const
{
return m_endPoint;
}
protected:
AsEndPointRef (Ref<AsEndPoint> endPoint, Ref<AsActorRef> actor)
: m_endPoint (endPoint), m_actor(actor)
{
}
private:
const Ref<AsEndPoint> m_endPoint;
const Ref<AsActorRef> m_actor;
};
#endif /* ASVARS_H */