Skip to content

Commit f1e43f5

Browse files
DYN-7112: Fix DS parser to error early on finding import statements in code block nodes (#15288)
1 parent c3be2aa commit f1e43f5

File tree

18 files changed

+141
-149
lines changed

18 files changed

+141
-149
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ runtimeGeneratedExtension/
1515
obj/
1616
int/
1717
packages/
18+
bin/
1819

1920
#find -type d -name bin -exec git rm -r {} \;
2021

src/Engine/ProtoCore/Core.cs

+41-44
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public void AddDLLExtensionAppType(System.Type type)
180180
/// </summary>
181181
public bool IsParsingCodeBlockNode { get; set; }
182182

183+
internal bool IsParsingInTestMode { get; set; }
184+
183185
// This is the AST node list of default imported libraries needed for Graph Compiler
184186
public CodeBlockNode ImportNodes { get; set; }
185187

@@ -424,19 +426,54 @@ public void ResetForPrecompilation()
424426
ForLoopBlockIndex = Constants.kInvalidIndex;
425427
}
426428

427-
private void ResetAll(Options options)
429+
// The unique subscript for SSA temporaries
430+
// TODO Jun: Organize these variables in core into proper enums/classes/struct
431+
public int SSASubscript { get; set; }
432+
public Guid SSASubscript_GUID { get; set; }
433+
public int SSAExprUID { get; set; }
434+
public int SSAExpressionUID { get; set; }
435+
436+
/// <summary>
437+
/// ExpressionUID is used as the unique id to identify an expression
438+
/// It is incremented by 1 after mapping its current value to an expression
439+
/// </summary>
440+
public int ExpressionUID { get; set; }
441+
442+
private int tempVarId = 0;
443+
private int tempLanguageId = 0;
444+
445+
446+
// TODO Jun: Cleansify me - i dont need to be here
447+
public AssociativeNode AssocNode { get; set; }
448+
public int watchStartPC { get; set; }
449+
450+
451+
//
452+
// TODO Jun: This is the expression interpreters executable.
453+
// It must be moved to its own core, whre each core is an instance of a compiler+interpreter
454+
//
455+
public Executable ExprInterpreterExe { get; set; }
456+
public int watchFunctionScope { get; set; }
457+
public int watchBaseOffset { get; set; }
458+
public List<SymbolNode> watchSymbolList { get; set; }
459+
460+
public CodeGen assocCodegen { get; set; }
461+
462+
public TextWriter ExecutionLog { get; set; }
463+
464+
public Core(Options options)
428465
{
429466
Heap = new Heap();
430467
//Rmem = new RuntimeMemory(Heap);
431468
Configurations = new Dictionary<string, object>();
432469
DllTypesToLoad = new List<System.Type>();
433470

434471
Options = options;
435-
472+
436473
Compilers = new Dictionary<Language, Compiler>();
437474
ClassIndex = Constants.kInvalidIndex;
438475

439-
FunctionTable = new FunctionTable();
476+
FunctionTable = new FunctionTable();
440477

441478

442479
watchFunctionScope = Constants.kInvalidIndex;
@@ -498,7 +535,7 @@ private void ResetAll(Options options)
498535

499536

500537
ParsingMode = ParseMode.Normal;
501-
538+
502539
IsParsingPreloadedAssembly = false;
503540
IsParsingCodeBlockNode = false;
504541
ImportHandler = null;
@@ -515,46 +552,6 @@ private void ResetAll(Options options)
515552
newEntryPoint = Constants.kInvalidIndex;
516553
}
517554

518-
// The unique subscript for SSA temporaries
519-
// TODO Jun: Organize these variables in core into proper enums/classes/struct
520-
public int SSASubscript { get; set; }
521-
public Guid SSASubscript_GUID { get; set; }
522-
public int SSAExprUID { get; set; }
523-
public int SSAExpressionUID { get; set; }
524-
525-
/// <summary>
526-
/// ExpressionUID is used as the unique id to identify an expression
527-
/// It is incremented by 1 after mapping its current value to an expression
528-
/// </summary>
529-
public int ExpressionUID { get; set; }
530-
531-
private int tempVarId = 0;
532-
private int tempLanguageId = 0;
533-
534-
535-
// TODO Jun: Cleansify me - i dont need to be here
536-
public AssociativeNode AssocNode { get; set; }
537-
public int watchStartPC { get; set; }
538-
539-
540-
//
541-
// TODO Jun: This is the expression interpreters executable.
542-
// It must be moved to its own core, whre each core is an instance of a compiler+interpreter
543-
//
544-
public Executable ExprInterpreterExe { get; set; }
545-
public int watchFunctionScope { get; set; }
546-
public int watchBaseOffset { get; set; }
547-
public List<SymbolNode> watchSymbolList { get; set; }
548-
549-
public CodeGen assocCodegen { get; set; }
550-
551-
public TextWriter ExecutionLog { get; set; }
552-
553-
public Core(Options options)
554-
{
555-
ResetAll(options);
556-
}
557-
558555
public SymbolNode GetFirstVisibleSymbol(string name, int classscope, int function, CodeBlock codeblock)
559556
{
560557
Validity.Assert(null != codeblock);

src/Engine/ProtoCore/Parser/Parser.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
//#define ENABLE_INC_DEC_FIX
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using ProtoCore.AST;
67
using ProtoCore.AST.AssociativeAST;
78
using ProtoCore.DSASM;
8-
using ProtoCore.Properties;
99
using ProtoCore.Utils;
10+
using ProtoCore.Properties;
1011

11-
namespace ProtoCore.DesignScriptParser
12-
{
12+
namespace ProtoCore.DesignScriptParser {
1313

1414

1515

16-
public class Parser {
16+
public class Parser {
1717
public const int _EOF = 0;
1818
public const int _ident = 1;
1919
public const int _number = 2;
@@ -791,7 +791,7 @@ bool WeakSeparator(int n, int syFol, int repFol) {
791791
void DesignScriptParser() {
792792
Node node = null;
793793
Hydrogen(out node);
794-
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded)
794+
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded && !core.IsParsingInTestMode)
795795
{
796796
CoreUtils.InsertPredefinedAndBuiltinMethods(core, node as CodeBlockNode);
797797
root = node;
@@ -890,6 +890,11 @@ void Hydrogen(out Node codeBlockNode) {
890890
}
891891

892892
void Import_Statement(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
893+
if (core.IsParsingCodeBlockNode)
894+
{
895+
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
896+
}
897+
893898
while (!(la.kind == 0 || la.kind == 34)) {SynErr(66); Get();}
894899
string moduleName = "", typeName = "", alias = "";
895900

src/Engine/ProtoCore/Parser/Scanner.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11

22
using System;
3-
using System.Collections;
43
using System.IO;
4+
using System.Collections;
55

6-
namespace ProtoCore.DesignScriptParser
7-
{
6+
namespace ProtoCore.DesignScriptParser {
87

9-
public class Token {
8+
public class Token {
109
public int kind; // token kind
1110
public int pos; // token position in bytes in the source text (starting at 0)
1211
public int charPos; // token position in characters in the source text (starting at 0)

src/Engine/ProtoCore/Parser/atg/Associative.atg

+7-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ Hydrogen<out Node codeBlockNode>
102102
.
103103

104104
Import_Statement<out ProtoCore.AST.AssociativeAST.AssociativeNode node>
105-
= SYNC
105+
= (.
106+
if (core.IsParsingCodeBlockNode)
107+
{
108+
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
109+
}
110+
.)
111+
SYNC
106112
(.
107113
string moduleName = "", typeName = "", alias = "";
108114
.)
@@ -2028,9 +2034,6 @@ Associative_IdentifierList<out ProtoCore.AST.AssociativeAST.AssociativeNode node
20282034
}
20292035
else
20302036
{
2031-
string rhsName = null;
2032-
ProtoCore.AST.AssociativeAST.ExprListNode dimList = null;
2033-
int dim = 0;
20342037
if (rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode)
20352038
{
20362039
ProtoCore.AST.AssociativeAST.FunctionCallNode rhsFNode = rnode as ProtoCore.AST.AssociativeAST.FunctionCallNode;

src/Engine/ProtoCore/Parser/atg/Start.atg

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ DesignScriptParser
726726
= (. Node node = null; .)
727727
Hydrogen<out node>
728728
(.
729-
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded)
729+
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded && !core.IsParsingInTestMode)
730730
{
731731
CoreUtils.InsertPredefinedAndBuiltinMethods(core, node as CodeBlockNode);
732732
root = node;

src/Engine/ProtoCore/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
[assembly: InternalsVisibleTo("ProtoScript")]
2020
[assembly: InternalsVisibleTo("NodeDocumentationMarkdownGenerator")]
2121
[assembly: InternalsVisibleTo("ProtoAssociative")]
22+
[assembly: InternalsVisibleTo("IntegrationTests")]
2223

src/Engine/ProtoCore/Utils/CompilerUtils.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private static bool CompileCodeBlockAST(Core core, ParseParam parseParams, IDict
314314

315315

316316
bool parsingPreloadFlag = core.IsParsingPreloadedAssembly;
317-
bool parsingCbnFlag = core.IsParsingPreloadedAssembly;
317+
bool parsingCbnFlag = core.IsParsingCodeBlockNode;
318318
core.IsParsingPreloadedAssembly = false;
319319
core.IsParsingCodeBlockNode = true;
320320

@@ -383,7 +383,7 @@ private static void ParseUserCode(ProtoCore.Core core, string expression, string
383383
}
384384
catch
385385
{
386-
// For class declarations, import statements etc. that are currently ignored
386+
// For class declarations etc. that are currently ignored
387387
}
388388
}
389389

@@ -415,11 +415,7 @@ private static void ParseUserCodeCore(Core core, string expression, out List<Ass
415415
// Append the temporaries only if it is not a function def or class decl
416416
bool isFunctionOrClassDef = n is FunctionDefinitionNode;
417417

418-
if (n is ImportNode)
419-
{
420-
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
421-
}
422-
else if (n is ClassDeclNode)
418+
if (n is ClassDeclNode)
423419
{
424420
core.BuildStatus.LogSemanticError(Resources.ClassDeclarationNotSupported);
425421
}

test/DynamoCoreTests/CodeBlockNodeTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Reflection;
56
using CoreNodeModels;
67
using Dynamo.Engine.CodeCompletion;
78
using Dynamo.Graph;
@@ -1800,6 +1801,22 @@ public void TypedIdentifier_AssignedToDifferentType_ThrowsWarning()
18001801
ProtoCore.Properties.Resources.kConvertNonConvertibleTypes)));
18011802
}
18021803

1804+
[Test]
1805+
public void ImportStatementInCodeBlock_DoesNotLoadAssemblyIntoProcess()
1806+
{
1807+
var codeBlockNode = CreateCodeBlockNode();
1808+
var guid = codeBlockNode.GUID.ToString();
1809+
1810+
string assemblyName = "FFITarget";
1811+
UpdateCodeBlockNodeContent(codeBlockNode, $"import(\"{assemblyName}.dll\")");
1812+
1813+
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
1814+
1815+
var ffiTargetAsm = loadedAssemblies.Any(assembly => assembly.GetName().Name.Equals(assemblyName, StringComparison.OrdinalIgnoreCase));
1816+
1817+
Assert.False(ffiTargetAsm);
1818+
}
1819+
18031820
[Test]
18041821
public void TypedIdentifier_AssignedToDifferentType_ThrowsWarning2()
18051822
{

0 commit comments

Comments
 (0)