Skip to content

Instantly share code, notes, and snippets.

@leshido
Created February 2, 2019 20:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leshido/4f8ca3b9067eb0c45cebc7d3583c94b8 to your computer and use it in GitHub Desktop.
Save leshido/4f8ca3b9067eb0c45cebc7d3583c94b8 to your computer and use it in GitHub Desktop.
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