Created
February 2, 2019 20:23
-
-
Save leshido/4f8ca3b9067eb0c45cebc7d3583c94b8 to your computer and use it in GitHub Desktop.
A hacky way to hijack methods in the Haxe standard library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--macro haxe.macro.Compiler.addMetadata("@:build(Hacky.build())", "Math") | |
# Replace 'Math' with any class that you intend to hijack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import haxe.macro.*; | |
class Hacky { | |
public static function build() { | |
var fields = Context.getBuildFields(); | |
for (field in fields) { | |
if (field.name == "random") { // Here we target the `random()` method of the Math class. | |
field.access.push(ADynamic); | |
} | |
} | |
return fields; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
class Main { | |
public static function main() { | |
} | |
private static function __init__() : Void { | |
Math.random = function() { | |
// custom PRNG code here | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment