Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DYN-2289: Fix crash with NodeToCode #10305

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/DynamoCore/Graph/Nodes/CodeBlockNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public override ProtoCore.Type GetTypeHintForOutput(int index)
if (statement == null)
return type;

BinaryExpressionNode expr = statement.AstNode as BinaryExpressionNode;
var expr = statement.AstNode as BinaryExpressionNode;
if (expr == null || expr.Optr != Operator.assign)
return type;

Expand All @@ -554,15 +554,21 @@ public override ProtoCore.Type GetTypeHintForOutput(int index)
return type;

var classIndex = core.ClassTable.IndexOf(fullyQualifiedName);
if (classIndex == ProtoCore.DSASM.Constants.kInvalidIndex)
if (classIndex == Constants.kInvalidIndex)
return type;

var targetClass = core.ClassTable.ClassNodes[classIndex];
var func = targetClass.GetFirstMemberFunctionBy(funcNode.Function.Name);
var funcName = funcNode.Function.Name;
var func = targetClass.GetFirstMemberFunctionBy(funcName);
if (func == null)
{
func = targetClass.ProcTable.GetPropertyGetterByName(funcName);
}
type = func.ReturnType;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crash occurred here before this change as func would be null.

return type;
}
else if (expr.RightNode is FunctionCallNode)

if (expr.RightNode is FunctionCallNode)
{
var functionCallNode = expr.RightNode as FunctionCallNode;
ProtoCore.FunctionGroup funcGroup;
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/ProtoCore/FFI/CLRDLLModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ private bool isPropertyAccessor(MethodInfo m)

string name = m.Name;
int nParams = 0;
if (name.StartsWith("get_"))
if (name.StartsWith(Constants.kGetterPrefix))
name.Remove(0, 4);
else if (name.StartsWith("set_"))
else if (name.StartsWith(Constants.kSetterPrefix))
{
name.Remove(0, 4);
nParams = 1;
Expand Down
13 changes: 13 additions & 0 deletions src/Engine/ProtoCore/ProcedureTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ public IEnumerable<ProcedureNode> GetFunctionsByName(string name)
return Procedures.Where(p => p.Name.Equals(name));
}

/// <summary>
/// Returns getter function for given property name.
/// Ex: for X property of Point, this method returns get_X(Point p).
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ProcedureNode GetPropertyGetterByName(string name)
{
var getterMethodName = Constants.kGetterPrefix + name;
return Procedures.FirstOrDefault(p => p.Name == getterMethodName &&
Copy link
Member

@mjkkirschner mjkkirschner Jan 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this potentially search the entire procedure table? Or only the procedures for a specific class?
Is this class actually not a implemented as a table????

Copy link
Contributor Author

@aparajit-pratap aparajit-pratap Jan 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This searches only the procedure table for the particular class. The ProcedureTable is essentially a list of ProcedureNodes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funny we call it a table...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the typical terminology you will find in compilers.

p.IsStatic && p.ArgumentTypes.Count == 1);
}

/// <summary>
/// Returns all functions with specified name and the number of argument.
///
Expand Down
16 changes: 16 additions & 0 deletions test/DynamoCoreTests/NodeToCodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,22 @@ public void TestUsingTypeDependentVariableName02()
Assert.IsNotNull(cbn);
Assert.IsTrue(cbn.GetAstIdentifierForOutputIndex(0).Value.StartsWith("num"));
}

[Test]
public void TestNodeToCodeWithPropertyInCodeBlock()
{
OpenModel(@"core\node2code\NodeToCode_CodeBlock.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;
SelectAll(nodes);

var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

AssertPreviewValue(cbn.GUID.ToString(), false);
}
}

[Category("NodeToCode")]
Expand Down
135 changes: 135 additions & 0 deletions test/core/node2code/NodeToCode_CodeBlock.dyn
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"Uuid": "815297e0-a634-4ca1-a1e3-35e16774573a",
"IsCustomNode": false,
"Description": null,
"Name": "Home",
"ElementResolver": {
"ResolutionMap": {
"Point": {
"Key": "Autodesk.DesignScript.Geometry.Point",
"Value": "ProtoGeometry.dll"
}
}
},
"Inputs": [],
"Outputs": [],
"Nodes": [
{
"ConcreteType": "Dynamo.Graph.Nodes.CodeBlockNodeModel, DynamoCore",
"NodeType": "CodeBlockNode",
"Code": "point1 = Point.ByCoordinates(10, 0, 0);\nnum1 = Point.X(point1);",
"Id": "459b7a03967e47edad45109fa1c02996",
"Inputs": [],
"Outputs": [
{
"Id": "7cacc376e22a442c8efead26169377ce",
"Name": "",
"Description": "point1",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
},
{
"Id": "193144eee6fc4eb0b842bee205a194fb",
"Name": "",
"Description": "num1",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Allows for DesignScript code to be authored directly"
},
{
"ConcreteType": "Dynamo.Graph.Nodes.ZeroTouch.DSFunction, DynamoCore",
"NodeType": "FunctionNode",
"FunctionSignature": "DSCore.List.IsEmpty@var[]..[]",
"Id": "19099719248c4d3ab5c336dd40243ddf",
"Inputs": [
{
"Id": "8266ff53c9e34ac1a5599779cc677f34",
"Name": "list",
"Description": "List to check for items.\n\nvar[]..[]",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "d4b15af56c4348c5bd67e5ba6832af59",
"Name": "bool",
"Description": "Whether the list is empty.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Auto",
"Description": "Determines if the given list is empty.\n\nList.IsEmpty (list: var[]..[]): bool"
}
],
"Connectors": [
{
"Start": "193144eee6fc4eb0b842bee205a194fb",
"End": "8266ff53c9e34ac1a5599779cc677f34",
"Id": "7a56817a34c7477f80197cc168946b9a"
}
],
"Dependencies": [],
"NodeLibraryDependencies": [],
"Bindings": [],
"View": {
"Dynamo": {
"ScaleFactor": 1.0,
"HasRunWithoutCrash": true,
"IsVisibleInDynamoLibrary": true,
"Version": "2.6.0.7474",
"RunType": "Automatic",
"RunPeriod": "1000"
},
"Camera": {
"Name": "Background Preview",
"EyeX": -17.0,
"EyeY": 24.0,
"EyeZ": 50.0,
"LookX": 12.0,
"LookY": -13.0,
"LookZ": -58.0,
"UpX": 0.0,
"UpY": 1.0,
"UpZ": 0.0
},
"NodeViews": [
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "459b7a03967e47edad45109fa1c02996",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 282.5,
"Y": 247.5
},
{
"ShowGeometry": true,
"Name": "List.IsEmpty",
"Id": "19099719248c4d3ab5c336dd40243ddf",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 723.0,
"Y": 262.0
}
],
"Annotations": [],
"X": 0.0,
"Y": 0.0,
"Zoom": 1.0
}
}