Skip to content

Commit 90a70a4

Browse files
committed
HardwireGen: Fix constructors
1 parent 41626d6 commit 90a70a4

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/WattleScript.HardwireGen.Test/MyWattleData.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ namespace WattleScript.HardwireGen.Test;
66
public class MyWattleData
77
{
88
public string Value = "";
9-
public void SetString(string value)
10-
{
11-
Value = value;
12-
}
139

1410
public int Add4(int a)
1511
{
1612
return a + 4;
1713
}
14+
15+
public MyWattleData()
16+
{
17+
}
18+
19+
public MyWattleData(string value)
20+
{
21+
Value = value;
22+
}
1823
}

src/WattleScript.HardwireGen.Test/SanityTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ public void Setup()
1212
LuaHardwire_WattleScript_HardwireGen_Test.Initialize();
1313
}
1414

15+
[Test]
16+
public void Constructor()
17+
{
18+
var sc = new Script(CoreModules.None);
19+
sc.Globals["MyWattleData"] = typeof(MyWattleData);
20+
var r = sc.DoString("return MyWattleData.__new('Hello')");
21+
Assert.IsInstanceOf<MyWattleData>(r.ToObject());
22+
var wd = r.ToObject<MyWattleData>();
23+
Assert.AreEqual("Hello", wd.Value);
24+
}
25+
1526
[Test]
1627
public void CallMethod()
1728
{

src/WattleScript.HardwireGen/HardwireSourceGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ static HardwireType FromTypeSymbol(INamedTypeSymbol symbol)
166166
if (method.MethodKind == MethodKind.Constructor)
167167
{
168168
if (!methods.ContainsKey("__new"))
169-
methods.Add("__new", new TypeMethod() { Constructor = true });
169+
{
170+
var ctor = new TypeMethod() { Constructor = true };
171+
ctor.SetName("__new");
172+
methods.Add("__new", ctor);
173+
}
170174
methods["__new"].AddMethod(0, method);
171175
}
172176
else

0 commit comments

Comments
 (0)