New Bytes implementation

This commit is contained in:
Joshua Granick
2015-06-25 10:48:17 -07:00
parent a617753137
commit 15fac4d6bd
6 changed files with 101 additions and 143 deletions

View File

@@ -57,13 +57,7 @@ class AudioBuffer {
var audioBuffer = new AudioBuffer (); var audioBuffer = new AudioBuffer ();
audioBuffer.bitsPerSample = data.bitsPerSample; audioBuffer.bitsPerSample = data.bitsPerSample;
audioBuffer.channels = data.channels; audioBuffer.channels = data.channels;
#if neko audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
var bytes = @:privateAccess (new Bytes (data.data.length, data.data.b));
#else
var bytes = Bytes.ofString (data.data.b);
@:privateAccess (bytes).length = data.data.length;
#end
audioBuffer.data = ByteArray.fromBytes (bytes);
audioBuffer.sampleRate = data.sampleRate; audioBuffer.sampleRate = data.sampleRate;
return audioBuffer; return audioBuffer;
@@ -87,13 +81,7 @@ class AudioBuffer {
var audioBuffer = new AudioBuffer (); var audioBuffer = new AudioBuffer ();
audioBuffer.bitsPerSample = data.bitsPerSample; audioBuffer.bitsPerSample = data.bitsPerSample;
audioBuffer.channels = data.channels; audioBuffer.channels = data.channels;
#if neko audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
var bytes = @:privateAccess (new Bytes (data.data.length, data.data.b));
#else
var bytes = Bytes.ofString (data.data.b);
@:privateAccess (bytes).length = data.data.length;
#end
audioBuffer.data = ByteArray.fromBytes (bytes);
audioBuffer.sampleRate = data.sampleRate; audioBuffer.sampleRate = data.sampleRate;
return audioBuffer; return audioBuffer;

View File

@@ -948,13 +948,7 @@ class Image {
if (data != null) { if (data != null) {
#if neko __fromImageBuffer (new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel));
var bytes = @:privateAccess (new Bytes (data.data.length, data.data.b));
#else
var bytes = Bytes.ofString (data.data.b);
@:privateAccess (bytes).length = data.data.length;
#end
__fromImageBuffer (new ImageBuffer (new UInt8Array (bytes), data.width, data.height, data.bitsPerPixel));
if (onload != null) { if (onload != null) {
@@ -1021,13 +1015,7 @@ class Image {
if (data != null) { if (data != null) {
#if neko var u8a = new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b));
var bytes = @:privateAccess (new Bytes (data.data.length, data.data.b));
#else
var bytes = Bytes.ofString (data.data.b);
@:privateAccess (bytes).length = data.data.length;
#end
var u8a = new UInt8Array (bytes);
buffer = new ImageBuffer (u8a, data.width, data.height, data.bitsPerPixel); buffer = new ImageBuffer (u8a, data.width, data.height, data.bitsPerPixel);
} }

View File

@@ -60,14 +60,7 @@ class JPEG {
#elseif (sys && (!disable_cffi || !format)) #elseif (sys && (!disable_cffi || !format))
var data:Dynamic = lime_image_encode (image.buffer, 1, quality); var data:Dynamic = lime_image_encode (image.buffer, 1, quality);
var bytes = @:privateAccess new Bytes (data.length, data.b);
#if neko
var bytes = @:privateAccess (new Bytes (data.length, data.b));
#else
var bytes = Bytes.ofString (data.b);
@:privateAccess (bytes).length = data.length;
#end
return ByteArray.fromBytes (bytes); return ByteArray.fromBytes (bytes);
#end #end

View File

@@ -69,14 +69,7 @@ class PNG {
if (!System.disableCFFI) { if (!System.disableCFFI) {
var data = lime_image_encode (image.buffer, 0, 0); var data = lime_image_encode (image.buffer, 0, 0);
var bytes = @:privateAccess new Bytes (data.length, data.b);
#if neko
var bytes = @:privateAccess (new Bytes (data.length, data.b));
#else
var bytes = Bytes.ofString (data.b);
@:privateAccess (bytes).length = data.length;
#end
return ByteArray.fromBytes (bytes); return ByteArray.fromBytes (bytes);
} }

View File

@@ -57,7 +57,7 @@ class TextLayout {
if (__buffer == null) { if (__buffer == null) {
__buffer = new ByteArray (); __buffer = new ByteArray (1);
__buffer.endian = "littleEndian"; __buffer.endian = "littleEndian";
} }

View File

@@ -8,10 +8,35 @@ namespace lime {
static int id_b; static int id_b;
static int id_length; static int id_length;
static bool init = false; static bool init = false;
static bool useBuffer = false;
inline void initialize () {
if (!init) {
id_b = val_id ("b");
id_length = val_id ("length");
buffer b = alloc_buffer_len (1);
if (buffer_data (b)) {
useBuffer = true;
}
init = true;
}
}
Bytes::Bytes () { Bytes::Bytes () {
initialize ();
_data = 0; _data = 0;
_length = 0; _length = 0;
_value = 0; _value = 0;
@@ -21,15 +46,21 @@ namespace lime {
Bytes::Bytes (int size) { Bytes::Bytes (int size) {
_data = (unsigned char*)malloc (size); initialize ();
_length = size;
_data = 0;
_length = 0;
_value = 0; _value = 0;
Resize (size);
} }
Bytes::Bytes (value bytes) { Bytes::Bytes (value bytes) {
initialize ();
Set (bytes); Set (bytes);
} }
@@ -37,6 +68,12 @@ namespace lime {
Bytes::Bytes (const char* path) { Bytes::Bytes (const char* path) {
initialize ();
_data = 0;
_length = 0;
_value = 0;
FILE_HANDLE *file = lime::fopen (path, "rb"); FILE_HANDLE *file = lime::fopen (path, "rb");
if (!file) { if (!file) {
@@ -46,22 +83,26 @@ namespace lime {
} }
lime::fseek (file, 0, SEEK_END); lime::fseek (file, 0, SEEK_END);
_length = lime::ftell (file);
lime::fseek (file, 0, SEEK_SET);
_data = (unsigned char*)malloc (_length); int size = lime::ftell (file);
if (size > 0) {
Resize (size);
int status = lime::fread (_data, _length, 1, file);
}
int status = lime::fread (_data, _length, 1, file);
lime::fclose (file); lime::fclose (file);
delete file; delete file;
_value = 0;
} }
Bytes::Bytes (const QuickVec<unsigned char> data) { Bytes::Bytes (const QuickVec<unsigned char> data) {
initialize ();
Set (data); Set (data);
} }
@@ -69,11 +110,11 @@ namespace lime {
Bytes::~Bytes () { Bytes::~Bytes () {
if (!_value && _data) { //if (!_value && _data) {
//
free (_data); //free (_data);
//
} //}
} }
@@ -101,31 +142,47 @@ namespace lime {
void Bytes::Resize (int size) { void Bytes::Resize (int size) {
if (_value) { if (size > _length) {
if (size > _length) { if (!_value) {
if (val_is_null (val_field (_value, id_b))) { _value = alloc_empty_object ();
buffer buf = alloc_buffer_len (size); }
_data = (unsigned char*)buffer_data (buf);
if (_data) { if (val_is_null (val_field (_value, id_b))) {
alloc_field (_value, id_b, buffer_val (buf)); value dataValue;
} else { if (useBuffer) {
value newString = alloc_raw_string (size); buffer b = alloc_buffer_len (size);
alloc_field (_value, id_b, newString); dataValue = buffer_val (b);
_data = (unsigned char*)val_string (val_field (_value, id_b)); _data = (unsigned char*)buffer_data (b);
memset (_data, 0, size);
}
} else { } else {
resizeByteData (_value, size); dataValue = alloc_raw_string (size);
_data = (unsigned char*)val_string (dataValue);
}
alloc_field (_value, id_b, dataValue);
} else {
if (useBuffer) {
buffer b = val_to_buffer (val_field (_value, id_b));
buffer_set_size (b, size);
_data = (unsigned char*)buffer_data (b);
} else {
value s = alloc_raw_string (size);
memcpy ((char *)val_string (s), val_string (val_field (_value, id_b)), size);
alloc_field (_value, id_b, s);
_data = (unsigned char*)val_string (s);
} }
@@ -133,22 +190,6 @@ namespace lime {
alloc_field (_value, id_length, alloc_int (size)); alloc_field (_value, id_length, alloc_int (size));
} else {
if (size > _length) {
if (_data) {
realloc (_data, size);
} else {
_data = (unsigned char*)malloc (size);
}
}
} }
_length = size; _length = size;
@@ -158,20 +199,6 @@ namespace lime {
void Bytes::Set (value bytes) { void Bytes::Set (value bytes) {
if (!init) {
id_b = val_id ("b");
id_length = val_id ("length");
init = true;
}
if (!_value && _data) {
free (_data);
}
if (val_is_null (bytes)) { if (val_is_null (bytes)) {
_length = 0; _length = 0;
@@ -210,27 +237,20 @@ namespace lime {
void Bytes::Set (const QuickVec<unsigned char> data) { void Bytes::Set (const QuickVec<unsigned char> data) {
if (!_value && _data) { int size = data.size ();
free (_data); if (size > 0) {
} Resize (size);
_length = data.size ();
if (_length > 0) {
_data = (unsigned char*)malloc (_length);
memcpy (_data, &data[0], _length); memcpy (_data, &data[0], _length);
} else { } else {
_data = 0; _data = 0;
_length = 0;
} }
_value = 0;
} }
@@ -242,31 +262,7 @@ namespace lime {
} else { } else {
if (!init) { return alloc_null ();
id_b = val_id ("b");
id_length = val_id ("length");
init = true;
}
_value = alloc_empty_object ();
if (_length > 0 && _data) {
value newString = alloc_raw_string (_length);
memcpy ((char*)val_string (newString), _data, _length);
alloc_field (_value, id_b, newString);
alloc_field (_value, id_length, alloc_int (_length));
} else {
alloc_field (_value, id_b, alloc_raw_string (0));
alloc_field (_value, id_length, alloc_int (_length));
}
return _value;
} }