Skip to content

Commit 0ebe8d0

Browse files
committed
Hardwire: Introduce generic class for field/property access.
1 parent 58c51d6 commit 0ebe8d0

4 files changed

Lines changed: 493 additions & 326 deletions

File tree

src/WattleScript.HardwireGen.Test/WattleScript.HardwireGen.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
6-
6+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
7+
<CompilerGeneratedFilesOutputPath>obj\Generated</CompilerGeneratedFilesOutputPath>
78
<IsPackable>false</IsPackable>
89
</PropertyGroup>
910

src/WattleScript.HardwireGen/HardwireInterop.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ public static partial class HardwireInterop
3333

3434
static void GenerateField(TabbedWriter tw, string typeName, HardwireField f)
3535
{
36-
tw.Append("private sealed class ").Append(f.ClassName).Append(" : ").AppendLine(CLS_PROP_FIELD);
36+
tw.Append("private sealed class ").Append(f.ClassName).Append(" : ").Append(CLS_PROP_FIELD)
37+
.Append("<").Append(f.Type).AppendLine(">");
3738
tw.AppendLine("{").Indent();
3839
tw.Append("internal ").Append(f.ClassName).AppendLine("() :");
3940
var access = 0;
4041
if (f.Read) access += 1; //CanRead
4142
if (f.Write) access += 2; //CanWrite
42-
tw.Indent().Append("base(typeof(").Append(f.Type).Append("),")
43+
tw.Indent().Append("base(")
4344
.Append(f.Name.ToLiteral()).Append(",false,")
4445
.Append("(WattleScript.Interpreter.Interop.BasicDescriptors.MemberDescriptorAccess)")
4546
.Append(access.ToString()).AppendLine(")").UnIndent();
@@ -48,19 +49,19 @@ static void GenerateField(TabbedWriter tw, string typeName, HardwireField f)
4849
if (f.Read)
4950
{
5051
tw.AppendLine(
51-
"protected override object GetValueImpl(WattleScript.Interpreter.Script script, object obj) {")
52+
$"protected override {f.Type} GetValueImpl(WattleScript.Interpreter.Script script, object obj) {{")
5253
.Indent();
5354
tw.Append("var self = (").Append(typeName).AppendLine(")obj;");
54-
tw.Append("return (object)self.").Append(f.Name).AppendLine(";");
55+
tw.Append("return self.").Append(f.Name).AppendLine(";");
5556
tw.UnIndent().AppendLine("}");
5657
}
5758
if (f.Write)
5859
{
5960
tw.AppendLine(
60-
"protected override void SetValueImpl(WattleScript.Interpreter.Script script, object obj, object value) {")
61+
$"protected override void SetValueImpl(WattleScript.Interpreter.Script script, object obj, {f.Type} value) {{")
6162
.Indent();
6263
tw.Append("var self = (").Append(typeName).AppendLine(")obj;");
63-
tw.Append("self.").Append(f.Name).Append(" = (").Append(f.Type).AppendLine(")value;");
64+
tw.Append("self.").Append(f.Name).AppendLine(" = value;");
6465
tw.UnIndent().AppendLine("}");
6566
}
6667
tw.UnIndent().AppendLine("}");

0 commit comments

Comments
 (0)