Skip to content

Commit 46dfa13

Browse files
committed
Fixed static variable initialization
SomeRanDev/reflaxe#46
1 parent ae04370 commit 46dfa13

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/rluacompiler/subcompilers/Classes.hx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@ class Classes extends SubCompiler
6262
if (properties.length > 0)
6363
output += '${classType.name}.__properties__ = {${properties.join(", ")}}\n';
6464

65+
var inlinesStatics:Array<ClassVarData> = [];
6566
for (varf in varFields)
6667
{
67-
if (varf.isStatic)
68+
if (!varf.isStatic)
69+
continue;
70+
var expr = varf.field.expr();
71+
if (expr == null)
72+
continue;
73+
74+
if (expr.expr.match(TBlock(_)))
75+
inlinesStatics.push(varf);
76+
else
6877
output += main.fieldsSubCompiler.compileStaticImpl(varf);
6978
}
7079

@@ -81,6 +90,18 @@ class Classes extends SubCompiler
8190
if (r != null)
8291
output += r;
8392
}
93+
94+
if (inlinesStatics.length > 0)
95+
{
96+
output += "\ndo\n";
97+
for (varf in inlinesStatics)
98+
{
99+
var r = main.expressionsSubCompiler.compileExpressionImpl(varf.field.expr(), 0);
100+
if (r != null)
101+
output += '\t' + StringTools.replace(r, "\n", "\n\t") + '\n';
102+
}
103+
output += 'end\n';
104+
}
84105
}
85106

86107
return output;

src/rluacompiler/subcompilers/Expressions.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ class Expressions extends SubCompiler
319319
});
320320
var body = exprImpl(tfunc.expr, 1);
321321

322-
buff += 'function(${args.join(", ")})${buff.enter}';
322+
buff += '(function(${args.join(", ")})${buff.enter}';
323323
buff += body;
324-
buff += '${buff.leave}end';
324+
buff += '${buff.leave}end)';
325325
buff;
326326

327327
case TVar(v, expr):

src/rluacompiler/subcompilers/Fields.hx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ class Fields extends SubCompiler
2727

2828
public function compileStaticImpl(varf:ClassVarData):Null<String>
2929
{
30-
var output = '${varf.classType.name}.${varf.field.name}';
31-
if (varf.field.expr() != null)
32-
output += ' = ${main.expressionsSubCompiler.compileExpressionImpl(varf.field.expr(), 0)}';
30+
var output = "";
31+
var expr = varf.field.expr();
32+
if (expr != null)
33+
output += main.expressionsSubCompiler.compileExpressionImpl(cast {
34+
expr: TBinop(OpAssign, {
35+
expr: TIdent('${varf.classType.name}.${varf.field.name}'),
36+
t: expr.t,
37+
pos: expr.pos
38+
}, expr),
39+
t: expr.t,
40+
pos: expr.pos
41+
}, 0);
3342
return output + "\n";
3443
}
3544

0 commit comments

Comments
 (0)