Skip to content

Instantly share code, notes, and snippets.

@hexonaut
Created May 31, 2012 22:36
Show Gist options
  • Save hexonaut/2846851 to your computer and use it in GitHub Desktop.
Save hexonaut/2846851 to your computer and use it in GitHub Desktop.
flash.Memory patch to remove alchemy dependancies
package flash;
#if !flash_no_alchemy extern #end class Memory {
#if flash_no_alchemy
private static var mem:flash.utils.ByteArray = prepareInitialByteArray();
private static function prepareInitialByteArray ():flash.utils.ByteArray {
var b = new flash.utils.ByteArray();
b.endian = flash.utils.Endian.LITTLE_ENDIAN;
for (i in 0 ... 1024) b.writeByte(0);
return b;
}
#end
public static inline function select( b : flash.utils.ByteArray ) : Void {
#if flash_no_alchemy
if (b != null) b.endian = flash.utils.Endian.LITTLE_ENDIAN;
mem = b;
#else
flash.system.ApplicationDomain.currentDomain.domainMemory = b;
#end
}
public static inline function setByte( addr : Int, v : Int ) : Void {
#if flash_no_alchemy
mem.position = addr;
mem.writeByte(v);
#else
untyped __vmem_set__(0,addr,v);
#end
}
public static inline function setI16( addr : Int, v : Int ) : Void {
#if flash_no_alchemy
mem.position = addr;
mem.writeShort(v);
#else
untyped __vmem_set__(1,addr,v);
#end
}
public static inline function setI32( addr : Int, v : Int ) : Void {
#if flash_no_alchemy
mem.position = addr;
mem.writeInt(v);
#else
untyped __vmem_set__(2,addr,v);
#end
}
public static inline function setFloat( addr : Int, v : Float ) : Void {
#if flash_no_alchemy
mem.position = addr;
mem.writeFloat(v);
#else
untyped __vmem_set__(3,addr,v);
#end
}
public static inline function setDouble( addr : Int, v : Float ) : Void {
#if flash_no_alchemy
mem.position = addr;
mem.writeDouble(v);
#else
untyped __vmem_set__(4,addr,v);
#end
}
public static inline function getByte( addr : Int ) : Int {
#if flash_no_alchemy
mem.position = addr;
return mem.readByte();
#else
return untyped __vmem_get__(0,addr);
#end
}
public static inline function getUI16( addr : Int ) : Int {
#if flash_no_alchemy
mem.position = addr;
return mem.readShort();
#else
return untyped __vmem_get__(1,addr);
#end
}
public static inline function getI32( addr : Int ) : Int {
#if flash_no_alchemy
mem.position = addr;
return mem.readInt();
#else
return untyped __vmem_get__(2,addr);
#end
}
public static inline function getFloat( addr : Int ) : Float {
#if flash_no_alchemy
mem.position = addr;
return mem.readFloat();
#else
return untyped __vmem_get__(3,addr);
#end
}
public static inline function getDouble( addr : Int ) : Float {
#if flash_no_alchemy
mem.position = addr;
return mem.readDouble();
#else
return untyped __vmem_get__(4,addr);
#end
}
public static inline function signExtend1( v : Int ) : Int {
#if flash_no_alchemy
v &= 1;
v |= v << 1;
v |= v << 2;
v |= v << 4;
v |= v << 8;
v |= v << 16;
return v;
#else
return untyped __vmem_sign__(0,v);
#end
}
public static inline function signExtend8( v : Int ) : Int {
#if flash_no_alchemy
v &= 0xFF;
v |= (v << 1) & 0x100;
v |= (v << 2) & 0x600;
v |= (v << 4) & 0x7800;
v |= (v << 8) & 0x7F8000;
v |= (v << 5) & 0xF8000000;
return v;
#else
return untyped __vmem_sign__(1,v);
#end
}
public static inline function signExtend16( v : Int ) : Int {
#if flash_no_alchemy
v &= 0xFFFF;
v |= (v << 1) & 0x10000;
v |= (v << 2) & 0x60000;
v |= (v << 4) & 0x780000;
v |= (v << 8) & 0x7F800000;
v |= (v << 1) & 0x80000000;
return v;
#else
return untyped __vmem_sign__(2,v);
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment