forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactorRuntime.cpp
More file actions
471 lines (389 loc) · 12.5 KB
/
actorRuntime.cpp
File metadata and controls
471 lines (389 loc) · 12.5 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* File: actorRuntime.cpp
* Author: ghernan
*
* Actor system runtime support.
*
* Created on December 24, 2016, 11:50 AM... Merry Christmas!!!
*/
#include "ascript_pch.hpp"
#include "actorRuntime.h"
#include "microVM.h"
#include "scriptMain.h"
#include "jsArray.h"
#include "ScriptException.h"
using namespace std;
void execMessageLoop (Ref<ActorRuntime> runtime, CodeMap* pMap);
ASValue connectOperator (ExecutionContext* ec);
/**
* Actor class which executes a piece of MVM code at startup and then finishes.
*/
class RoutineActor : public AsActor
{
public:
/**
* Creates a routine actor.
* @param code
* @param globals
* @param parent
* @return
*/
static Ref<AsActorRef> create(Ref<MvmRoutine> code,
Ref<GlobalScope> globals,
Ref<AsActorRef> parent)
{
auto constructor = AsEndPoint::createNative("@start",
StringVector(),
routineActorExec);
VarMap members;
members.varWrite(constructor->getName(), constructor, true);
auto ownClass = AsActorClass::create("", members, StringVector());
RoutineActor* newActor = new RoutineActor (ownClass, globals, parent);
newActor->m_code = code;
return AsActorRef::create(refFromNew(newActor));
}
/**
* Executes the routine actor.
* @param pScope
* @return
*/
static ASValue routineActorExec (ExecutionContext* ec)
{
auto actor = pScope->getThis().staticCast<RoutineActor>();
auto actorRef = AsActorRef::create(actor);
auto globals = ::getGlobals().staticCast<GlobalScope>();
globals->newNotSharedVar("@curActor", actorRef, true);
return mvmExecute(actor->m_code, globals, Ref<IScope>());
}
private:
RoutineActor (Ref<AsActorClass> cls, Ref<GlobalScope> globals, Ref<AsActorRef> parent) :
AsActor (cls, globals, parent)
{
}
Ref<MvmRoutine> m_code;
};
/**
* Executes a compiled actor script, and blocks the current thread until all created
* actors have been stopped.
* @param code
* @param globals
* @return pair of 'JSValue'. The first value is success, the second is error.
*/
pair<ASValue, ASValue > asBlockingExec(Ref<MvmRoutine> code,
Ref<GlobalScope> globals,
CodeMap* pMap)
{
auto rootActor = RoutineActor::create (code, globals, Ref<AsActorRef>());
auto runtime = ActorRuntime::create(rootActor);
globals->newVar("@actorRT", runtime, true);
addNative1("@connect", "src", connectOperator, globals);
execMessageLoop (runtime, pMap);
auto errVal = rootActor->getError();
if (!errVal->isNull())
return make_pair (jsNull(), errVal);
else
return make_pair (rootActor->getResult(), jsNull());
}
/**
* Executes the message loop, until there is no message left.
* @param runtime
*/
void execMessageLoop (Ref<ActorRuntime> runtime, CodeMap* pMap)
{
//TODO: This function must become more complex when taking I/O into account.
while (runtime->dispatchMessage(pMap));
}
/**
* The connect operator ('<-') establishes a connection between and output message
* and an input message.
* @param pScope
* @return
*/
ASValue connectOperator (ExecutionContext* ec)
{
auto runtime = ActorRuntime::getRuntime(pScope);
auto src = ec->getParam("src");
auto dst = pScope->getThis();
if (src->getType() != VT_OUTPUT_EP_REF)
rtError ("Source is not an output message");
if (dst->getType() != VT_INPUT_EP_REF)
rtError ("Destination is not an input message");
// runtime.staticCast<ActorRuntime>()->connectMessages(
// dst.staticCast<AsMessageRef>(), src.staticCast<AsMessageRef>());
const auto srcActorRef = src.staticCast<AsEndPointRef>()->getActor();
if (!srcActorRef->isRunning())
return jsNull();
const auto srcActor = srcActorRef->getActor();
const auto msgName = src.staticCast<AsEndPointRef>()->getEndPoint()->getName();
srcActor->setOutputConnection (msgName, dst.staticCast<AsEndPointRef>());
return jsNull();
}
/**
* Handles calls to input endpoints, which are executed as message sends
* @param endPoint
* @param scope
* @return
*/
ASValue inputEpCall(Ref<AsEndPointRef> endPoint, Ref<FunctionScope> scope)
{
auto runtime = ActorRuntime::getRuntime(scope.getPointer());
auto params = scope->get("arguments").staticCast<JSArray>();
runtime->sendMessage (endPoint, params);
return jsNull();
}
/**
* Handles calls to output endpoints. It looks for the connected input end point,
* and sends a message to it.
*
* @param endPoint
* @param scope
* @return
*/
ASValue outputEpCall(Ref<AsEndPointRef> endPoint, Ref<FunctionScope> scope)
{
//TODO: Check that it is invoked from the owning actor.
auto actor = endPoint->getActor()->getActor();
auto destination = actor->getConnectedEp(endPoint->getEndPoint()->getName());
if (destination.isNull())
return jsNull();
else
return inputEpCall(destination, scope);
}
/**
* Handles actor constructors calls
* @param actorClass
* @param scope
* @return
*/
ASValue actorConstructor(Ref<AsActorClass> actorClass, Ref<FunctionScope> scope)
{
auto curGlobals = ::getGlobals().staticCast<GlobalScope>();
auto newGlobals = curGlobals->share();
auto runtime = ActorRuntime::getRuntime(scope.getPointer());
auto curActor = curGlobals->get("@curActor").staticCast<AsActorRef>();
auto newActor = AsActor::create(actorClass, newGlobals, curActor);
auto actorRef = AsActorRef::create(newActor);
auto constructor = actorClass->getConstructor();
auto params = constructor->getParams();
auto msgParams = JSArray::create();
//Set the new current actor at the new global scope.
newGlobals->newNotSharedVar("@curActor", actorRef, true);
//Parameters become actor fields, and parameters for 'start' message
for (size_t i=0; i<params.size(); ++i)
{
auto value = scope->getParam(params[i]);
newActor->writeField(params[i], value, false);
msgParams->push(value);
}
//Send start message
runtime->sendMessage(actorRef, "@start", msgParams);
return actorRef;
}
/**
* Default handler for 'childStopped' message.
* @param pScope
* @return
*/
ASValue actorChildStoppedDefaultHandler(ExecutionContext* ec)
{
auto actor = pScope->getThis().staticCast<AsActor>();
auto actorRef = AsActorRef::create(actor);
auto runtime = ActorRuntime::getRuntime(pScope);
auto result = ec->getParam("result");
auto errVal = ec->getParam("error");
runtime->stopActor(actorRef, result, errVal);
return jsNull();
}
/**
* Creates the actor runtime.
* @param rootActor
* @return
*/
Ref<ActorRuntime> ActorRuntime::create(Ref<AsActorRef> rootActor)
{
auto result = refFromNew(new ActorRuntime(rootActor));
// result = deepFreeze(result).staticCast<ActorRuntime>();
//
result->sendMessage0 (rootActor, "@start");
return result;
}
/**
* Obtains the actor runtime from a function scope
* @param fnScope
* @return
*/
Ref<ActorRuntime> ActorRuntime::getRuntime(FunctionScope* fnScope)
{
auto runtime = ::getGlobals()->get("@actorRT");
if (!runtime->isObject())
rtError ("Missing actor runtime");
return runtime.staticCast<ActorRuntime>();
}
/**
* Sends a message with no parameters.
* @param dstActor
* @param epName
*/
void ActorRuntime::sendMessage0 (Ref<AsActorRef> dstActor, const std::string& epName)
{
auto params = JSArray::create();
sendMessage(dstActor, epName, params);
}
/**
* Sends a message with one parameter.
* @param dstActor
* @param epName
* @param p1
*/
void ActorRuntime::sendMessage1 (Ref<AsActorRef> dstActor,
const std::string& epName,
ASValue p1)
{
auto params = JSArray::create();
params->push(p1);
sendMessage(dstActor, epName, params);
}
/**
* Sends a message with two parameters.
* @param dstActor
* @param epName
* @param p1
* @param p2
*/
void ActorRuntime::sendMessage2 (Ref<AsActorRef> dstActor,
const std::string& epName,
ASValue p1,
ASValue p2)
{
auto params = JSArray::create();
params->push(p1);
params->push(p2);
sendMessage(dstActor, epName, params);
}
/**
* Sends a message with three parameters.
* @param dstActor
* @param epName
* @param p1
* @param p2
* @param p3
*/
void ActorRuntime::sendMessage3 (Ref<AsActorRef> dstActor,
const std::string& epName,
ASValue p1,
ASValue p2,
ASValue p3)
{
auto params = JSArray::create();
params->push(p1);
params->push(p2);
params->push(p3);
sendMessage(dstActor, epName, params);
}
/**
* 'sendMessage' version which looks up for the message name
* @param dstActor
* @param epName
* @param params
*/
void ActorRuntime::sendMessage (Ref<AsActorRef> dstActor,
const std::string& epName,
Ref<JSArray> params)
{
auto ep = dstActor->getEndPoint(epName);
if (ep.isNull())
rtError ("End point '%s' does not exist", epName.c_str());
else
sendMessage (ep, params);
}
/**
* Sends a message to an actor, through one of its input endpoints
* @param dstEndPoint
* @param params
*/
void ActorRuntime::sendMessage (Ref<AsEndPointRef> dstEndPoint, Ref<JSArray> params)
{
if (dstEndPoint->getType() != VT_INPUT_EP_REF)
rtError ("Destination is not an input end point");
m_messageQueue.push_back(SMessage(dstEndPoint, params));
}
/**
* Stops an actor execution
* @param actorRef
* @param value
* @param error
*/
void ActorRuntime::stopActor (Ref<AsActorRef> actorRef, ASValue value, ASValue error)
{
auto actor = actorRef->getActor();
actor->stop(value, error);
auto parent = actor->getParent();
if (parent.notNull())
sendMessage3 (parent, "childStopped", actorRef, value, error);
}
/**
* Dispatch next message in the queue.
* @return true if a message has been processed, false if the message queue is
* empty.
*/
bool ActorRuntime::dispatchMessage(CodeMap* pMap)
{
if (m_messageQueue.empty())
return false;
SMessage msg = m_messageQueue.front();
auto actorRef = msg.destination->getActor();
m_messageQueue.pop_front();
if (actorRef->isRunning())
{
auto actor = actorRef->getActor();
auto globals = actor->getGlobals();
GlobalsSetter g(globals);
try
{
try
{
auto endPoint = msg.destination->getEndPoint();
auto scope = FunctionScope::create(endPoint, actor, msg.params);
if (endPoint->isNative())
endPoint->nativePtr()(scope.getPointer());
else
{
auto routine = endPoint->getCodeMVM().staticCast<MvmRoutine>();
mvmExecute (routine, globals, scope);
}
}
catch (RuntimeError& ex)
{
ScriptPosition scPos;
if (pMap != NULL)
scPos = pMap->get(ex.Position);
errorAt(scPos, "%s", ex.what());
}
}
catch (std::exception& ex)
{
actorCrashed (actorRef, ex.what());
}
}
return true;
}
/**
* Handles exceptions inside actors.
* @param actorRef
* @param ex
*/
void ActorRuntime::actorCrashed(Ref<AsActorRef> actorRef, const char* msg)
{
auto actor = actorRef->getActor();
this->stopActor(actorRef, jsNull(), jsString(msg));
//TODO: Better error logging
if (actor->getParent().notNull())
fprintf (stderr, "Actor crashed: %s\n", msg);
else
fprintf (stderr, "Root actor crashed: %s\n", msg);
}
Ref<JSObject> ActorRuntime::clone (bool _mutable)
{
ASSERT (!"clone operation not allowed on 'ActorRuntime'");
return Ref<JSObject>(this);
}