|
| 1 | +package backend; |
| 2 | + |
| 3 | +import haxe.ds.StringMap; |
| 4 | +import haxe.io.Bytes; |
| 5 | +import haxe.io.Eof; |
| 6 | +import haxe.io.Input; |
| 7 | +import haxe.io.Output; |
| 8 | + |
| 9 | +class Pointer<T> { |
| 10 | + private var ref:Ref<T>; |
| 11 | + |
| 12 | + public function new(ref:Ref<T>) { |
| 13 | + this.ref = ref; |
| 14 | + } |
| 15 | + |
| 16 | + public function get():T { |
| 17 | + return ref.value; |
| 18 | + } |
| 19 | + |
| 20 | + public function set(value:T):Void { |
| 21 | + ref.value = value; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class Ref<T> { |
| 26 | + public var value:T; |
| 27 | + |
| 28 | + public function new(value:T) { |
| 29 | + this.value = value; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class HaxePointer { |
| 34 | + private var memory:StringMap<Dynamic>; |
| 35 | + private var pointer:Int; |
| 36 | + |
| 37 | + public function new() { |
| 38 | + memory = new StringMap<Dynamic>(); |
| 39 | + pointer = 0; |
| 40 | + } |
| 41 | + |
| 42 | + public function allocate(size:Int):Int { |
| 43 | + var addr = pointer; |
| 44 | + pointer += size; |
| 45 | + return addr; |
| 46 | + } |
| 47 | + |
| 48 | + public function free(addr:Int):Void { |
| 49 | + memory.remove(addr); |
| 50 | + } |
| 51 | + |
| 52 | + public function write(addr:Int, data:Dynamic):Void { |
| 53 | + memory.set(addr, data); |
| 54 | + } |
| 55 | + |
| 56 | + public function read(addr:Int):Dynamic { |
| 57 | + return memory.get(addr); |
| 58 | + } |
| 59 | + |
| 60 | + public function findByType(type:String):Array<Int> { |
| 61 | + var result:Array<Int> = []; |
| 62 | + for (key in memory.keys()) { |
| 63 | + if (Type.typeof(memory.get(key)).toString() == type) { |
| 64 | + result.push(key); |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + public function findByValue(value:Dynamic):Array<Int> { |
| 71 | + var result:Array<Int> = []; |
| 72 | + for (key in memory.keys()) { |
| 73 | + if (memory.get(key) == value) { |
| 74 | + result.push(key); |
| 75 | + } |
| 76 | + } |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + public function dumpMemory():Void { |
| 81 | + for (key in memory.keys()) { |
| 82 | + trace('Address: $key, Value: ${memory.get(key)}'); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static function createPointer<T>(value:T):Pointer<T> { |
| 87 | + var ref = new Ref<T>(value); |
| 88 | + return new Pointer<T>(ref); |
| 89 | + } |
| 90 | +} |
0 commit comments