Instantly share code, notes, and snippets.

@leshido leshido/Hacky.hx
Created Feb 2, 2019

Embed
What would you like to do?
A hacky way to hijack methods in the Haxe standard library
--macro haxe.macro.Compiler.addMetadata("@:build(Hacky.build())", "Math")
# Replace 'Math' with any class that you intend to hijack
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;
}
}
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