Skip to content

[BUGFIX] Prevent finals from being set from scripted Classes #197

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions include.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<!-- These values are added to the project XML when you include this library. -->
<haxelib name="thx.semver" />
<!-- <haxelib name="hscript" /> This library is now OPTIONAL. -->
<haxeflag name="--macro" value="polymod.hscript._internal.PolymodFinalMacro.locateAllFinals()" />
</project>
100 changes: 100 additions & 0 deletions polymod/hscript/_internal/PolymodFinalMacro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package polymod.hscript._internal;

import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.rtti.Meta;

class PolymodFinalMacro
{
private static var _allFinals:Map<String, Array<String>> = null;

public static function getAllFinals():Map<String, Array<String>>
{
if (_allFinals == null)
_allFinals = PolymodFinalMacro.fetchAllFinals();
return _allFinals;
}

public static macro function locateAllFinals():Void
{
Context.onAfterTyping((types) ->
{
if (calledBefore)
return;

var allFinals:Array<Expr> = [];

for (type in types)
{
switch (type)
{
case TClassDecl(t):
var classType:ClassType = t.get();
var classPath:String = t.toString();
if (classType.isInterface) continue;

var finals:Array<String> = [];
for (field in classType.statics.get())
{
if (!field.isFinal) continue;
finals.push(field.name);
}

var entryData = [
macro $v{classPath},
macro $v{finals}
];

allFinals.push(macro $a{entryData});
default:
continue;
}
}

var finalMacroType:Type = Context.getType('polymod.hscript._internal.PolymodFinalMacro');

switch (finalMacroType)
{
case TInst(t, _):
var finalMacroClassType:ClassType = t.get();
finalMacroClassType.meta.add('finals', allFinals, Context.currentPos());
default:
throw 'Could not find PolymodFinalMacro type';
}

calledBefore = true;
});
}

#if macro
static var calledBefore:Bool = false;
#end

public static function fetchAllFinals():Map<String, Array<String>>
{
var metaData = Meta.getType(PolymodFinalMacro);

if (metaData.finals != null)
{
var result:Map<String, Array<String>> = [];

for (element in metaData.finals)
{
if (element.length != 2)
throw 'Malformed element in finals: ' + element;

var classPath:String = element[0];
var finals:Array<String> = element[1];

result.set(classPath, finals);
}

return result;
}
else
{
throw 'No finals found in PolymodFinalMacro';
}
}
}
21 changes: 21 additions & 0 deletions polymod/hscript/_internal/PolymodInterpEx.hx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,27 @@ class PolymodInterpEx extends Interp
}
}
}
else
{
@:privateAccess
{
// Check if we are setting a final. If so, throw an error.
if (_proxy != null && _proxy._c != null)
{
for (imp in _proxy._c.imports)
{
if (imp.name != id0) continue;
var finals = PolymodFinalMacro.getAllFinals().get(imp.fullPath);
for (fin in finals)
{
if (fin != id) continue;
errorEx(EInvalidAccess(fin));
return null;
}
}
}
}
}
default:
// Do nothing
}
Expand Down