-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathRuntimeStatus.cs
More file actions
592 lines (525 loc) · 21.3 KB
/
Copy pathRuntimeStatus.cs
File metadata and controls
592 lines (525 loc) · 21.3 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
using System;
using System.Collections.Generic;
using System.Linq;
using ProtoCore.DSASM;
using ProtoCore.DSDefinitions;
using ProtoCore.Properties;
using ProtoCore.Runtime;
using ProtoCore.Utils;
using DynamoUtilities;
namespace ProtoCore
{
namespace Runtime
{
public enum WarningID
{
Default,
AccessViolation,
AmbiguousMethodDispatch,
AurgumentIsNotExpected,
CallingConstructorOnInstance,
ConversionNotPossible,
DereferencingNonPointer,
FileNotExist,
IndexOutOfRange,
InvalidRecursion,
InvalidArguments,
CyclicDependency,
MethodResolutionFailure,
OverIndexing,
TypeConvertionCauseInfoLoss,
TypeMismatch,
ReplicationWarning,
InvalidIndexing,
ModuloByZero,
InvalidType,
RangeExpressionOutOfMemory,
MoreThanOneDominantList,
RunOutOfMemory,
InvalidArrayIndexType,
IntegerOverflow
}
public struct WarningEntry
{
public Runtime.WarningID ID;
public string Message;
public int Line;
public int Column;
public int ExpressionID;
public Guid GraphNodeGuid;
public int AstID;
public string Filename;
}
internal enum InfoID
{
Default
}
public struct InfoEntry
{
internal Runtime.InfoID ID;
internal string Message;
internal int ExpressionID;
internal Guid GraphNodeGuid;
internal int AstID;
internal string Filename;
}
}
public class RuntimeStatus
{
private readonly DynamoLock rwl = new DynamoLock();
private ProtoCore.RuntimeCore runtimeCore;
private List<Runtime.WarningEntry> warnings;
private List<Runtime.InfoEntry> infos;
public IOutputStream MessageHandler
{
get;
set;
}
public IOutputStream WebMessageHandler
{
get;
set;
}
public List<Runtime.WarningEntry> Warnings
{
get
{
using (rwl.CreateReadLock())
{
return warnings.ToList();
}
}
}
internal List<Runtime.InfoEntry> Infos
{
get
{
using (rwl.CreateReadLock())
{
return infos.ToList();
}
}
}
internal int InfosCount
{
get
{
using (rwl.CreateReadLock())
{
return infos.Count;
}
}
}
public int WarningCount
{
get
{
using (rwl.CreateReadLock())
{
return warnings.Count;
}
}
}
public void ClearWarningForExpression(int expressionID)
{
using (rwl.CreateWriteLock())
{
warnings.RemoveAll(w => w.ExpressionID == expressionID);
infos.RemoveAll(w => w.ExpressionID == expressionID);
}
}
public void ClearWarningsForGraph(Guid guid)
{
using (rwl.CreateWriteLock())
{
warnings.RemoveAll(w => w.GraphNodeGuid.Equals(guid));
infos.RemoveAll(w => w.GraphNodeGuid.Equals(guid));
}
}
/// <summary>
/// Clears runtime warnings and infos that belong to a specific graph node GUID
/// AND a specific expression ID. This is more precise than <see cref="ClearWarningsForGraph"/>
/// and is needed when a single Dynamo node compiles to multiple AST expressions that each
/// get a distinct exprUID — clearing by GUID alone would remove warnings from sibling
/// expressions that have not yet been re-executed.
/// </summary>
/// <param name="guid">The graph-node GUID of the Dynamo node being re-executed.</param>
/// <param name="exprUID">The expression UID of the specific AST expression being re-executed.</param>
public void ClearWarningsForGraphNode(Guid guid, int exprUID)
{
using (rwl.CreateWriteLock())
{
warnings.RemoveAll(w => w.GraphNodeGuid.Equals(guid) && w.ExpressionID == exprUID);
infos.RemoveAll(w => w.GraphNodeGuid.Equals(guid) && w.ExpressionID == exprUID);
}
}
public void ClearWarningsForAst(int astID)
{
using (rwl.CreateWriteLock())
{
warnings.RemoveAll(w => w.AstID.Equals(astID));
infos.RemoveAll(w => w.AstID.Equals(astID));
}
}
public RuntimeStatus(RuntimeCore runtimeCore,
bool warningAsError = false,
System.IO.TextWriter writer = null)
{
warnings = new List<Runtime.WarningEntry>();
infos = new List<Runtime.InfoEntry>();
this.runtimeCore = runtimeCore;
if (writer != null)
{
System.Console.SetOut(writer);
}
}
public void LogWarning(Runtime.WarningID ID, string message, string filename, int line, int col)
{
filename = filename ?? string.Empty;
if (!runtimeCore.Options.IsDeltaExecution && (string.IsNullOrEmpty(filename) ||
line == Constants.kInvalidIndex ||
col == Constants.kInvalidIndex))
{
AuditCodeLocation(ref filename, ref line, ref col);
}
var warningMsg = string.Format(Resources.kConsoleWarningMessage,
message, filename, line, col);
#if DEBUG
if (runtimeCore.Options.Verbose)
{
System.Console.WriteLine(warningMsg);
}
#endif
if (WebMessageHandler != null)
{
var outputMessage = new OutputMessage(warningMsg);
WebMessageHandler.Write(outputMessage);
}
if (MessageHandler != null)
{
var outputMessage = new OutputMessage(OutputMessage.MessageType.Warning,
message.Trim(), filename, line, col);
MessageHandler.Write(outputMessage);
}
AssociativeGraph.GraphNode executingGraphNode = null;
var executive = runtimeCore.CurrentExecutive.CurrentDSASMExec;
if (executive != null)
{
executingGraphNode = executive.Properties.executingGraphNode;
// In delta execution mode, it means the warning is from some
// internal graph node.
if (executingGraphNode != null && executingGraphNode.guid.Equals(System.Guid.Empty))
{
executingGraphNode = runtimeCore.DSExecutable.ExecutingGraphnode;
}
}
var entry = new Runtime.WarningEntry
{
ID = ID,
Message = message,
Column = col,
Line = line,
ExpressionID = runtimeCore.RuntimeExpressionUID,
GraphNodeGuid = executingGraphNode == null ? Guid.Empty : executingGraphNode.guid,
AstID = executingGraphNode == null ? Constants.kInvalidIndex : executingGraphNode.OriginalAstID,
Filename = filename
};
using (rwl.CreateWriteLock())
{
warnings.Add(entry);
}
}
internal void LogInfo(Runtime.InfoID ID, string message, string filename)
{
filename ??= string.Empty;
if (MessageHandler != null)
{
var outputMessage = new OutputMessage(OutputMessage.MessageType.Info,
message.Trim(), filename);
MessageHandler.Write(outputMessage);
}
AssociativeGraph.GraphNode executingGraphNode = null;
var executive = runtimeCore.CurrentExecutive.CurrentDSASMExec;
if (executive != null)
{
executingGraphNode = executive.Properties.executingGraphNode;
// In delta execution mode, it means the info is from some
// internal graph node.
if (executingGraphNode != null && executingGraphNode.guid.Equals(System.Guid.Empty))
{
executingGraphNode = runtimeCore.DSExecutable.ExecutingGraphnode;
}
}
var entry = new InfoEntry
{
ID = ID,
Message = message,
ExpressionID = runtimeCore.RuntimeExpressionUID,
GraphNodeGuid = executingGraphNode == null ? Guid.Empty : executingGraphNode.guid,
AstID = executingGraphNode == null ? Constants.kInvalidIndex : executingGraphNode.OriginalAstID,
Filename = filename
};
using (rwl.CreateWriteLock())
{
infos.Add(entry);
}
}
public void LogWarning(Runtime.WarningID ID, string message)
{
LogWarning(ID, message, string.Empty, Constants.kInvalidIndex, Constants.kInvalidIndex);
}
internal void LogInfo(Runtime.InfoID ID, string message)
{
LogInfo(ID, message, string.Empty);
}
private void AuditCodeLocation(ref string filePath, ref int line, ref int column)
{
// We don't attempt to change line and column numbers if
// they are already provided (caller can force update of
// them by setting either one of them to be -1).
if (!string.IsNullOrEmpty(filePath))
{
if (-1 != line && (-1 != column))
return;
}
// As we create internal functions like %dotarg() and %dot() and
// append them to the end of the script, it is possible that the
// location is in these functions so that the pc dictionary doesn't
// contain pc key and return maximum line number + 1.
//
// Need to check if is in internal function or not, If it is, need
// to go back the last stack frame to get the correct pc value
int pc = Constants.kInvalidPC;
int codeBlock = 0;
if (runtimeCore != null)
{
pc = runtimeCore.CurrentExecutive.CurrentDSASMExec.PC;
codeBlock = runtimeCore.RunningBlock;
if (String.IsNullOrEmpty(filePath))
{
filePath = runtimeCore.DSExecutable.CurrentDSFileName;
}
}
if (runtimeCore.Options.IsDeltaExecution)
{
GetLocationByGraphNode(ref line, ref column);
if (line == Constants.kInvalidIndex)
GetLocationByPC(pc, codeBlock, ref line, ref column);
}
else
GetLocationByPC(pc, codeBlock, ref line, ref column);
}
private void GetLocationByPC(int pc, int blk, ref int line, ref int column)
{
//--------Dictionary Structure:--------
//--------Name: codeToLocation---------
//----------KEY: ----------------------
//----------------mergedKey: ----------
//-------------------|- blk -----------
//-------------------|- pc ------------
//----------VALUE: --------------------
//----------------location: -----------
//-------------------|- line ----------
//-------------------|- col -----------
//Zip those integers into 64-bit ulong
ulong mergedKey = (((ulong)blk) << 32 | ((uint)pc));
ulong location = (((ulong)line) << 32 | ((uint)column));
if (runtimeCore.DSExecutable.CodeToLocation.ContainsKey(mergedKey))
{
location = runtimeCore.DSExecutable.CodeToLocation[mergedKey];
}
foreach (KeyValuePair<ulong, ulong> kv in runtimeCore.DSExecutable.CodeToLocation)
{
//Conditions: within same blk && find the largest key which less than mergedKey we want to find
if ((((int)(kv.Key >> 32)) == blk) && (kv.Key < mergedKey))
{
location = kv.Value;
}
}
//Unzip the location
line = ((int)(location >> 32));
column = ((int)(location & 0x00000000ffffffff));
}
private void GetLocationByGraphNode(ref int line, ref int col)
{
ulong location = (((ulong)line) << 32 | ((uint)col));
foreach (var prop in runtimeCore.InterpreterProps)
{
bool fileScope = false;
if (prop.executingGraphNode == null)
continue;
int startpc = prop.executingGraphNode.updateBlock.startpc;
int endpc = prop.executingGraphNode.updateBlock.endpc;
int block = prop.executingGraphNode.languageBlockId;
// Determine if the current executing graph node is in an imported file scope
// If so, continue searching in the outer graph nodes for the line and col in the outer-most context - pratapa
for (int i = startpc; i <= endpc; ++i)
{
var instruction = runtimeCore.DSExecutable.instrStreamList[block].instrList[i];
if (instruction.debug != null)
{
if (instruction.debug.Location.StartInclusive.SourceLocation.FilePath != null)
{
fileScope = true;
break;
}
else
{
fileScope = false;
break;
}
}
}
if (fileScope)
continue;
foreach (var kv in runtimeCore.DSExecutable.CodeToLocation)
{
if ((((int)(kv.Key >> 32)) == block) && (kv.Key >= (ulong)startpc && kv.Key <= (ulong)endpc))
{
location = kv.Value;
line = ((int)(location >> 32));
col = ((int)(location & 0x00000000ffffffff));
break;
}
}
if (line != -1)
break;
}
}
/// <summary>
/// Report that the method cannot be found.
/// </summary>
/// <param name="methodName">The method that cannot be found</param>
/// <param name="classScope">The class scope of object</param>
/// <param name="arguments">Arguments</param>
public void LogFunctionGroupNotFoundWarning(string methodName,
int classScope,
List<StackValue> arguments)
{
string className = runtimeCore.DSExecutable.classTable.ClassNodes[classScope].Name;
List<string> argumentTypes = new List<string>();
if (arguments == null || arguments.Count == 0)
{
string propertyName;
if (CoreUtils.TryGetPropertyName(methodName, out propertyName))
{
string message = string.Format(Resources.kPropertyOfClassNotFound, propertyName, className);
LogWarning(WarningID.MethodResolutionFailure, message);
}
else
{
string message = string.Format(Resources.FunctionGroupNotFound, methodName, className);
LogWarning(WarningID.MethodResolutionFailure, message);
}
}
else
{
foreach (var argument in arguments)
{
ProtoCore.Type type = runtimeCore.DSExecutable.TypeSystem.BuildTypeObject(argument.metaData.type, 0);
argumentTypes.Add(type.ToShortString());
}
string message = string.Format(Resources.FunctionGroupWithParameterNotFound, methodName, className, string.Join(",", argumentTypes));
LogWarning(WarningID.MethodResolutionFailure, message);
}
}
public void LogMethodResolutionWarning(FunctionGroup funcGroup,
string methodName,
int classScope = Constants.kGlobalScope,
List<StackValue> arguments = null)
{
string message;
var qualifiedMethodName = methodName;
var className = string.Empty;
var classNameSimple = string.Empty;
if (classScope != Constants.kGlobalScope)
{
if (methodName == nameof(DesignScript.Builtin.Get.ValueAtIndex))
{
if (arguments.Count == 2 && arguments[0].IsInteger && arguments[1].IsInteger)
{
LogWarning(WarningID.IndexOutOfRange, Resources.IndexIntoNonArrayObject);
return;
}
}
var classNode = runtimeCore.DSExecutable.classTable.ClassNodes[classScope];
className = classNode.Name;
classNameSimple = className.Split('.').Last();
qualifiedMethodName = classNameSimple + "." + methodName;
}
Operator op;
string propertyName;
if (CoreUtils.TryGetPropertyName(methodName, out propertyName))
{
if (classScope != Constants.kGlobalScope)
{
if (arguments != null && arguments.Any())
{
qualifiedMethodName = classNameSimple + "." + propertyName;
// if the property is found on the class, it must be a static getter being called on
// an instance argument type not matching the property
message = string.Format(Resources.NonOverloadMethodResolutionError, qualifiedMethodName,
className, GetTypeName(arguments[0]));
}
else
{
message = string.Format(Resources.kPropertyOfClassNotFound, propertyName, className);
}
}
else
{
message = string.Format(Resources.kPropertyNotFound, propertyName);
}
}
else if (CoreUtils.TryGetOperator(methodName, out op))
{
var strOp = Op.GetOpSymbol(op);
message = String.Format(Resources.kMethodResolutionFailureForOperator,
strOp,
GetTypeName(arguments[0]),
GetTypeName(arguments[1]));
}
else if (funcGroup.FunctionEndPoints.Count == 1) // non-overloaded case
{
var argsJoined = string.Join(", ", arguments.Select(GetTypeName));
var fep = funcGroup.FunctionEndPoints[0];
var formalParamsJoined = string.Join(", ", fep.FormalParams);
message = string.Format(Resources.NonOverloadMethodResolutionError, qualifiedMethodName, formalParamsJoined, argsJoined);
}
else // overloaded case
{
var argsJoined = string.Join(", ", arguments.Select(GetTypeName));
message = string.Format(Resources.kMethodResolutionFailureWithTypes, qualifiedMethodName, argsJoined);
}
LogWarning(WarningID.MethodResolutionFailure, message);
}
private string GetTypeName(StackValue v)
{
var type = runtimeCore.DSExecutable.TypeSystem.GetType(v.metaData.type);
if (type != Keyword.Array)
{
return type;
}
var c = ArrayUtils.GetGreatestCommonSubclassForArray(v, runtimeCore);
if (c == null) // empty array case
{
return "var[]";
}
return c.Name + "[]";
}
public void LogMethodNotAccessibleWarning(string methodName)
{
string message;
string propertyName;
if (CoreUtils.TryGetPropertyName(methodName, out propertyName))
{
message = String.Format(Resources.kPropertyInaccessible, propertyName);
}
else
{
message = String.Format(Resources.kMethodResolutionFailure, methodName);
}
LogWarning(ProtoCore.Runtime.WarningID.MethodResolutionFailure, message);
}
}
}