Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 3126e81

Browse files
author
Stephen Hawley
committed
Added CSInject and SLInject
1 parent 3107bd9 commit 3126e81

File tree

8 files changed

+54
-14
lines changed

8 files changed

+54
-14
lines changed

Dynamo/Dynamo.CSLang/CSInject.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Dynamo.CSLang {
5+
// CSInject is a way to more formalize the notion of code that is just plain easier to
6+
// inject as raw text. It's not strictly necessary, but when you see a CSInject, it will make
7+
// it clear that you're doing something not quite on the up and up.
8+
public class CSInject : CSIdentifier {
9+
public CSInject (string name)
10+
: base (name)
11+
{
12+
}
13+
14+
public static explicit operator CSInject (string name)
15+
{
16+
return new CSInject (name);
17+
}
18+
}
19+
}

Dynamo/Dynamo.SwiftLang/SLInject.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Dynamo.SwiftLang {
5+
public class SLInject : SLIdentifier {
6+
// SLInject is a way to more formalize the notion of code that is just plain easier to
7+
// inject as raw text. It's not strictly necessary, but when you see a CSInject, it will make
8+
// it clear that you're doing something not quite on the up and up.
9+
public SLInject (string name)
10+
: base (name)
11+
{
12+
}
13+
14+
public static explicit operator SLInject (string name)
15+
{
16+
return new SLInject (name);
17+
}
18+
}
19+
}

Dynamo/Dynamo.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
<Compile Include="Dynamo.SwiftLang\SLComment.cs" />
143143
<Compile Include="Dynamo.CSLang\CSInitializer.cs" />
144144
<Compile Include="Dynamo.CSLang\CSConditionalCompilation.cs" />
145+
<Compile Include="Dynamo.CSLang\CSInject.cs" />
146+
<Compile Include="Dynamo.SwiftLang\SLInject.cs" />
145147
</ItemGroup>
146148
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
147149
<ItemGroup>

SwiftReflector/MarshalEngineCSafeSwiftToCSharp.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ public List<ICodeElement> MarshalFromLambdaReceiverToCSProp (CSProperty prop, CS
460460
CSIdentifier csharpCall = null;
461461
var thisTypeName = hasAssociatedTypes ? csProxyName : thisType.ToString ();
462462
if (forProtocol && !hasAssociatedTypes) {
463-
csharpCall = new CSIdentifier ($"SwiftObjectRegistry.Registry.InterfaceForExistentialContainer<{thisTypeName}> (self).{prop.Name.Name}");
463+
csharpCall = new CSInject ($"SwiftObjectRegistry.Registry.InterfaceForExistentialContainer<{thisTypeName}> (self).{prop.Name.Name}");
464464
} else {
465465
var call = isObjC ?
466466
$"ObjCRuntime.Runtime.GetNSObject<{thisTypeName}> (self).{prop.Name.Name}" :
467467
$"SwiftObjectRegistry.Registry.CSObjectForSwiftObject<{thisTypeName}> (self).{prop.Name.Name}";
468468

469-
csharpCall = new CSIdentifier (call);
469+
csharpCall = new CSInject (call);
470470
}
471471

472472
if (funcDecl.IsGetter) {
@@ -559,7 +559,7 @@ public List<ICodeElement> MarshalFromLambdaReceiverToCSProp (CSProperty prop, CS
559559
var proxObjName = MarshalEngine.Uniqueify ("proxyObj", identifiersUsed);
560560
identifiersUsed.Add (proxObjName);
561561
var proxyObjId = new CSIdentifier (proxObjName);
562-
var proxyObjDecl = CSVariableDeclaration.VarLine (proxyObjId, new CSIdentifier ($"SwiftObjectRegistry.Registry.CSObjectForSwiftObject<{thisTypeName}> ({delegateParams [1].Name})"));
562+
var proxyObjDecl = CSVariableDeclaration.VarLine (proxyObjId, new CSInject ($"SwiftObjectRegistry.Registry.CSObjectForSwiftObject<{thisTypeName}> ({delegateParams [1].Name})"));
563563
body.Add (proxyObjDecl);
564564
valueExpr = new CSCastExpression (methodType, new CSParenthesisExpression (new CSBinaryExpression (CSBinaryOperator.NullCoalesce, proxyObjId.Dot (NewClassCompiler.kInterfaceImpl), proxyObjId)));
565565
} else {

SwiftReflector/MethodWrapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ SLClosure MakePayloadClosure (string elemName, SLType enumType, SLType elemType)
868868
var elseclause = new SLCodeBlock (null).And (new SLThrow (new SLIdentifier ("SwiftEnumError.undefined")));
869869

870870

871-
var ifcase = new SLIfElse (new SLIdentifier (String.Format (".{0}(let {1}) = {2}", elemName, patXId.Name, valId.Name)),
871+
var ifcase = new SLIfElse (new SLInject (String.Format (".{0}(let {1}) = {2}", elemName, patXId.Name, valId.Name)),
872872
ifclause, elseclause, true);
873873
body.Add (ifcase);
874874

tests/tom-swifty-test/SwiftReflector/CharacterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public init (c : Character) { C = c }
7070
CSLine instance = CSVariableDeclaration.VarLine ((CSSimpleType)"CharacterEcho", (CSIdentifier)"Foo", CSFunctionCall.Ctor ("CharacterEcho", ctorParam));
7171

7272
// First the properties
73-
var explicitCastProp = (CSIdentifier)"(string)Foo.Value";
73+
var explicitCastProp = (CSInject)"(string)Foo.Value";
7474
var toStringProp = (CSIdentifier)"Foo.Value.ToString ()";
7575

7676
// Then the function returns as well
77-
var explicitCastFun = (CSIdentifier)"(string)Foo.GetValue ()";
78-
var toStringFun = (CSIdentifier)"Foo.GetValue ().ToString ()";
77+
var explicitCastFun = (CSInject)"(string)Foo.GetValue ()";
78+
var toStringFun = (CSInject)"Foo.GetValue ().ToString ()";
7979

8080
block.Add (instance);
8181
foreach (var call in new CSBaseExpression [] { explicitCastProp, toStringProp, explicitCastFun, toStringFun })

tests/tom-swifty-test/SwiftReflector/NewClassCompilerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public enum PrivAccess {
778778
";
779779
var miID = new CSIdentifier ("mi");
780780
var miDecl = CSVariableDeclaration.VarLine (CSSimpleType.Var, miID, new CSFunctionCall ("typeof(PrivAccess).GetMethod", false, CSConstant.Val ("__GetValueIVal"),
781-
new CSIdentifier ("System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance")));
781+
new CSInject ("System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance")));
782782
var printer = CSFunctionCall.ConsoleWriteLine (miID.Dot (new CSIdentifier ("IsPublic")));
783783
var callingCode = new CodeElementCollection<ICodeElement> () { miDecl, printer };
784784
TestRunning.TestAndExecute (swiftCode, callingCode, "False\n");

tests/tom-swifty-test/TestRunning.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static CodeElementCollection<ICodeElement> CaptureSwiftOutputPostlude (string fi
191191
block.Add (new CSIdentifier ("\n#if _MAC_TS_TEST_\n"));
192192
block.Add (CSVariableDeclaration.VarLine (CSSimpleType.String, pathID,
193193
new CSFunctionCall ("Path.Combine", false,
194-
new CSIdentifier ("Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)") +
194+
new CSInject ("Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)") +
195195
CSConstant.Val ("/Documents/"),
196196
CSConstant.Val (fileName))));
197197
block.Add (new CSIdentifier ("\n#else\n"));
@@ -508,7 +508,7 @@ public static CSNamespace CreateManagedConsoleRedirect ()
508508
),
509509
CSCodeBlock.Create (
510510
new CSIfElse (
511-
new CSIdentifier ("string.IsNullOrEmpty (Filename)"),
511+
new CSInject ("string.IsNullOrEmpty (Filename)"),
512512
CSCodeBlock.Create (CSFunctionCall.FunctionCallLine ("global::System.Console.Write", new CSIdentifier ("value"))),
513513
CSCodeBlock.Create (CSFunctionCall.FunctionCallLine ("System.IO.File.AppendAllText", new CSIdentifier ("Filename"), new CSIdentifier ("value")))
514514
)
@@ -528,7 +528,7 @@ public static CSNamespace CreateManagedConsoleRedirect ()
528528
CSFunctionCall.FunctionCallLine (
529529
"write",
530530
false,
531-
new CSIdentifier ("value?.ToString ()")
531+
new CSInject ("value?.ToString ()")
532532
)
533533
)
534534
)
@@ -547,7 +547,7 @@ public static CSNamespace CreateManagedConsoleRedirect ()
547547
CSFunctionCall.FunctionCallLine (
548548
"write",
549549
false,
550-
new CSIdentifier ("value == null ? string.Empty : string.Format (value, args)")
550+
new CSInject ("value == null ? string.Empty : string.Format (value, args)")
551551
)
552552
)
553553
)
@@ -565,7 +565,7 @@ public static CSNamespace CreateManagedConsoleRedirect ()
565565
CSFunctionCall.FunctionCallLine (
566566
"write",
567567
false,
568-
new CSIdentifier ("value?.ToString () + Environment.NewLine")
568+
new CSInject ("value?.ToString () + Environment.NewLine")
569569
)
570570
)
571571
)
@@ -584,7 +584,7 @@ public static CSNamespace CreateManagedConsoleRedirect ()
584584
CSFunctionCall.FunctionCallLine (
585585
"write",
586586
false,
587-
new CSIdentifier ("(value == null ? string.Empty : string.Format (value, args)) + Environment.NewLine")
587+
new CSInject ("(value == null ? string.Empty : string.Format (value, args)) + Environment.NewLine")
588588
)
589589
)
590590
)

0 commit comments

Comments
 (0)