forked from openfl/openfl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.hx
165 lines (80 loc) · 3.07 KB
/
Memory.hx
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package openfl; #if !flash #if !openfl_legacy
import haxe.io.BytesData;
import openfl.utils.ByteArray;
class Memory {
private static var gcRef:ByteArray;
private static var len:Int;
@:noCompletion static private function _setPositionTemporarily<T> (position:Int, action:Void -> T):T {
var oldPosition:Int = gcRef.position;
gcRef.position = position;
var value:T = action ();
gcRef.position = oldPosition;
return value;
}
public static #if !debug inline #end function getByte (addr:Int):Int {
#if debug if (addr < 0 || addr + 1 > len) throw("Bad address"); #end
return gcRef.__get (addr);
}
public static #if !debug inline #end function getDouble (addr:Int):Float {
#if debug if (addr < 0 || addr + 8 > len) throw ("Bad address"); #end
return _setPositionTemporarily (addr, function () {
return gcRef.readDouble ();
});
}
public static #if !debug inline #end function getFloat (addr:Int):Float {
#if debug if (addr < 0 || addr + 4 > len) throw ("Bad address"); #end
return _setPositionTemporarily (addr, function () {
return gcRef.readFloat ();
});
}
public static #if !debug inline #end function getI32 (addr:Int):Int {
#if debug if (addr < 0 || addr + 4 > len) throw ("Bad address"); #end
return _setPositionTemporarily(addr, function () {
return gcRef.readInt ();
});
}
public static #if !debug inline #end function getUI16 (addr:Int):Int {
#if debug if (addr < 0 || addr + 2 > len) throw ("Bad address"); #end
return _setPositionTemporarily (addr, function () {
return gcRef.readUnsignedShort ();
});
}
public static function select (inBytes:ByteArray):Void {
gcRef = inBytes;
len = (inBytes != null) ? inBytes.length : 0;
}
public static #if !debug inline #end function setByte (addr:Int, v:Int):Void {
#if debug if (addr < 0 || addr + 1 > len) throw ("Bad address"); #end
gcRef.__set (addr, v);
}
public static #if !debug inline #end function setDouble (addr:Int, v:Float):Void {
#if debug if (addr < 0 || addr + 8 > len) throw ("Bad address"); #end
_setPositionTemporarily (addr, function () {
gcRef.writeDouble (v);
});
}
public static #if !debug inline #end function setFloat (addr:Int, v:Float):Void {
#if debug if (addr < 0 || addr + 4 > len) throw ("Bad address"); #end
_setPositionTemporarily (addr, function () {
gcRef.writeFloat (v);
});
}
public static #if !debug inline #end function setI16 (addr:Int, v:Int):Void {
#if debug if (addr < 0 || addr + 2 > len) throw ("Bad address"); #end
_setPositionTemporarily (addr, function () {
gcRef.writeUnsignedShort (v);
});
}
public static #if !debug inline #end function setI32 (addr:Int, v:Int):Void {
#if debug if (addr < 0 || addr + 4 > len) throw ("Bad address"); #end
_setPositionTemporarily (addr, function () {
gcRef.writeInt (v);
});
}
}
#else
typedef Memory = openfl._legacy.Memory;
#end
#else
typedef Memory = flash.Memory;
#end