forked from deltaluca/nape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDummyMemory.cx
108 lines (91 loc) · 2.88 KB
/
DummyMemory.cx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package;
$(import);
class Reference {
public var name:String;
public var obj:TObject;
public function new(name:String,obj:TObject) {
this.name = name;
this.obj = obj;
}
}
class PushResult {
public var isnew:Bool;
public var obj:TObject;
public function new(isnew:Bool,obj:TObject) {
this.isnew = isnew;
this.obj = obj;
}
}
class TObject {
public static var nextId = 0;
public var id :Int;
public var obj :Dynamic;
public var type:String;
public var references:Array<Reference>;
public function new(obj:Dynamic) {
id = nextId++;
this.obj = obj;
type = Type.getClassName(Type.getClass(obj));
references = new Array<Reference>();
}
public function toString() {
var ret = "#"+id+" : "+type+";\n";
for(r in references)
ret += "\t "+r.name+" = #"+r.obj.id+"\n";
return ret.substr(0,ret.length-1);
}
}
#if haxe3 typedef IntHash<T> = Map<Int,T>; #end
class Graph {
public var objects:IntHash<TObject>;
var dict:flash.utils.Dictionary;
public function new(objs:Array<Dynamic>=null) {
objects = new IntHash<TObject>();
dict = new flash.utils.Dictionary(false);
if(objs==null) return;
for(o in objs) push(o);
}
public function push(obj:Dynamic):PushResult {
if(Reflect.isObject(obj)) {
if((untyped dict[obj])==null) {
var ob = new TObject(obj);
untyped dict[obj] = ob;
objects.set(ob.id, ob);
return new PushResult(true, ob);
}else
return new PushResult(false, untyped dict[obj]);
}else
return null;
}
public function toString() {
var ret = "";
for(o in objects)
ret += o.toString()+"\n";
return ret;
}
public function populate(continuation:Void->Void) {
var stack:Array<Dynamic> = [];
for(o in objects) {
o.references = [];
stack.push(o);
}
var tim = new haxe.Timer(0);
tim.run = function() {
if(stack.length==0) { tim.stop(); continuation(); return; }
var pt = flash.Lib.getTimer();
while(flash.Lib.getTimer()-pt<50) {
if(stack.length==0) { tim.stop(); continuation(); return; }
var obj:TObject = stack.shift();
var cls = Type.resolveClass(obj.type);
for(o in Type.getInstanceFields(cls)) {
var field:Dynamic = Reflect.field(obj.obj, o);
if(field!=null && Reflect.isObject(field)) {
var ref = push(field);
obj.references.push(new Reference(o,ref.obj));
if(ref.isnew) stack.push(ref.obj);
}
}
}
}
}
}